FontFaceHandle.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "FontFaceHandle.h"
  30. #include <algorithm>
  31. #include "../../Include/RmlUi/Core.h"
  32. #include "FontFaceLayer.h"
  33. #include "TextureLayout.h"
  34. namespace Rml {
  35. namespace Core {
  36. FontFaceHandle::FontFaceHandle()
  37. {
  38. metrics = {};
  39. base_layer = nullptr;
  40. }
  41. FontFaceHandle::~FontFaceHandle()
  42. {
  43. glyphs.clear();
  44. layers.clear();
  45. }
  46. // Returns the point size of this font face.
  47. int FontFaceHandle::GetSize() const
  48. {
  49. return metrics.size;
  50. }
  51. // Returns the average advance of all glyphs in this font face.
  52. int FontFaceHandle::GetCharacterWidth() const
  53. {
  54. return metrics.average_advance;
  55. }
  56. // Returns the pixel height of a lower-case x in this font face.
  57. int FontFaceHandle::GetXHeight() const
  58. {
  59. return metrics.x_height;
  60. }
  61. // Returns the default height between this font face's baselines.
  62. int FontFaceHandle::GetLineHeight() const
  63. {
  64. return metrics.line_height;
  65. }
  66. // Returns the font's baseline.
  67. int FontFaceHandle::GetBaseline() const
  68. {
  69. return metrics.baseline;
  70. }
  71. // Returns the font's glyphs.
  72. const FontGlyphMap& FontFaceHandle::GetGlyphs() const
  73. {
  74. return glyphs;
  75. }
  76. // Returns the width a string will take up if rendered with this handle.
  77. int FontFaceHandle::GetStringWidth(const String& string, CodePoint prior_character) const
  78. {
  79. int width = 0;
  80. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  81. {
  82. CodePoint code_point = *it_string;
  83. auto it_glyph = glyphs.find(code_point);
  84. if (it_glyph == glyphs.end())
  85. continue;
  86. const FontGlyph& glyph = it_glyph->second;
  87. // Adjust the cursor for the kerning between this character and the previous one.
  88. if (prior_character != CodePoint::Null)
  89. width += GetKerning(prior_character, code_point);
  90. // Adjust the cursor for this character's advance.
  91. width += glyph.advance;
  92. prior_character = code_point;
  93. }
  94. return width;
  95. }
  96. // Generates, if required, the layer configuration for a given array of font effects.
  97. int FontFaceHandle::GenerateLayerConfiguration(const FontEffectList& font_effects)
  98. {
  99. if (font_effects.empty())
  100. return 0;
  101. // Check each existing configuration for a match with this arrangement of effects.
  102. int configuration_index = 1;
  103. for (; configuration_index < (int) layer_configurations.size(); ++configuration_index)
  104. {
  105. const LayerConfiguration& configuration = layer_configurations[configuration_index];
  106. // Check the size is correct. For a match, there should be one layer in the configuration
  107. // plus an extra for the base layer.
  108. if (configuration.size() != font_effects.size() + 1)
  109. continue;
  110. // Check through each layer, checking it was created by the same effect as the one we're
  111. // checking.
  112. size_t effect_index = 0;
  113. for (size_t i = 0; i < configuration.size(); ++i)
  114. {
  115. // Skip the base layer ...
  116. if (configuration[i]->GetFontEffect() == nullptr)
  117. continue;
  118. // If the ith layer's effect doesn't match the equivalent effect, then this
  119. // configuration can't match.
  120. if (configuration[i]->GetFontEffect() != font_effects[effect_index].get())
  121. break;
  122. // Check the next one ...
  123. ++effect_index;
  124. }
  125. if (effect_index == font_effects.size())
  126. return configuration_index;
  127. }
  128. // No match, so we have to generate a new layer configuration.
  129. layer_configurations.push_back(LayerConfiguration());
  130. LayerConfiguration& layer_configuration = layer_configurations.back();
  131. bool added_base_layer = false;
  132. for (size_t i = 0; i < font_effects.size(); ++i)
  133. {
  134. if (!added_base_layer && font_effects[i]->GetLayer() == FontEffect::Layer::Front)
  135. {
  136. layer_configuration.push_back(base_layer);
  137. added_base_layer = true;
  138. }
  139. layer_configuration.push_back(GenerateLayer(font_effects[i]));
  140. }
  141. // Add the base layer now if we still haven't added it.
  142. if (!added_base_layer)
  143. layer_configuration.push_back(base_layer);
  144. return (int) (layer_configurations.size() - 1);
  145. }
  146. // Generates the texture data for a layer (for the texture database).
  147. bool FontFaceHandle::GenerateLayerTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id)
  148. {
  149. FontLayerMap::iterator layer_iterator = layers.find(layer_id);
  150. if (layer_iterator == layers.end())
  151. return false;
  152. return layer_iterator->second->GenerateTexture(texture_data, texture_dimensions, texture_id);
  153. }
  154. // Generates the geometry required to render a single line of text.
  155. int FontFaceHandle::GenerateString(GeometryList& geometry, const String& string, const Vector2f& position, const Colourb& colour, int layer_configuration_index) const
  156. {
  157. int geometry_index = 0;
  158. int line_width = 0;
  159. RMLUI_ASSERT(layer_configuration_index >= 0);
  160. RMLUI_ASSERT(layer_configuration_index < (int) layer_configurations.size());
  161. // Fetch the requested configuration and generate the geometry for each one.
  162. const LayerConfiguration& layer_configuration = layer_configurations[layer_configuration_index];
  163. for (size_t i = 0; i < layer_configuration.size(); ++i)
  164. {
  165. FontFaceLayer* layer = layer_configuration[i];
  166. Colourb layer_colour;
  167. if (layer == base_layer)
  168. layer_colour = colour;
  169. else
  170. layer_colour = layer->GetColour();
  171. // Resize the geometry list if required.
  172. if ((int) geometry.size() < geometry_index + layer->GetNumTextures())
  173. geometry.resize(geometry_index + layer->GetNumTextures());
  174. RMLUI_ASSERT(!geometry.empty());
  175. // Bind the textures to the geometries.
  176. for (int i = 0; i < layer->GetNumTextures(); ++i)
  177. geometry[geometry_index + i].SetTexture(layer->GetTexture(i));
  178. line_width = 0;
  179. CodePoint prior_character = CodePoint::Null;
  180. geometry[geometry_index].GetIndices().reserve(string.size() * 6);
  181. geometry[geometry_index].GetVertices().reserve(string.size() * 4);
  182. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  183. {
  184. CodePoint code_point = *it_string;
  185. auto it_glyph = glyphs.find(code_point);
  186. if (it_glyph == glyphs.end())
  187. continue;
  188. const FontGlyph& glyph = it_glyph->second;
  189. // Adjust the cursor for the kerning between this character and the previous one.
  190. if (prior_character != CodePoint::Null)
  191. line_width += GetKerning(prior_character, code_point);
  192. layer->GenerateGeometry(&geometry[geometry_index], code_point, Vector2f(position.x + line_width, position.y), layer_colour);
  193. line_width += glyph.advance;
  194. prior_character = code_point;
  195. }
  196. geometry_index += layer->GetNumTextures();
  197. }
  198. // Cull any excess geometry from a previous generation.
  199. geometry.resize(geometry_index);
  200. return line_width;
  201. }
  202. // Generates the geometry required to render a line above, below or through a line of text.
  203. void FontFaceHandle::GenerateLine(Geometry* geometry, const Vector2f& position, int width, Style::TextDecoration height, const Colourb& colour) const
  204. {
  205. std::vector< Vertex >& line_vertices = geometry->GetVertices();
  206. std::vector< int >& line_indices = geometry->GetIndices();
  207. float offset;
  208. switch (height)
  209. {
  210. case Style::TextDecoration::Underline: offset = -metrics.underline_position; break;
  211. case Style::TextDecoration::Overline: offset = -metrics.underline_position - (float)metrics.size; break;
  212. case Style::TextDecoration::LineThrough: offset = -0.65f * (float)metrics.x_height; break; // or maybe: -underline_position - (float)size * 0.5f
  213. default: return;
  214. }
  215. line_vertices.resize(line_vertices.size() + 4);
  216. line_indices.resize(line_indices.size() + 6);
  217. GeometryUtilities::GenerateQuad(
  218. &line_vertices[0] + ((int)line_vertices.size() - 4),
  219. &line_indices[0] + ((int)line_indices.size() - 6),
  220. Vector2f(position.x, position.y + offset).Round(),
  221. Vector2f((float) width, metrics.underline_thickness),
  222. colour,
  223. (int)line_vertices.size() - 4
  224. );
  225. }
  226. FontGlyphMap& FontFaceHandle::GetGlyphs() {
  227. return glyphs;
  228. }
  229. FontMetrics& FontFaceHandle::GetMetrics() {
  230. return metrics;
  231. }
  232. void FontFaceHandle::GenerateBaseLayer()
  233. {
  234. base_layer = GenerateLayer(nullptr);
  235. layer_configurations.push_back(LayerConfiguration());
  236. layer_configurations.back().push_back(base_layer);
  237. }
  238. // Generates (or shares) a layer derived from a font effect.
  239. FontFaceLayer* FontFaceHandle::GenerateLayer(const SharedPtr<const FontEffect>& font_effect)
  240. {
  241. // See if this effect has been instanced before, as part of a different configuration.
  242. FontLayerMap::iterator i = layers.find(font_effect.get());
  243. if (i != layers.end())
  244. return i->second.get();
  245. UniquePtr<FontFaceLayer> layer_ptr = std::make_unique<FontFaceLayer>();
  246. FontFaceLayer* layer = layer_ptr.get();
  247. layers[font_effect.get()] = std::move(layer_ptr);
  248. if (!font_effect)
  249. {
  250. layer->Initialise(this);
  251. }
  252. else
  253. {
  254. // Determine which, if any, layer the new layer should copy its geometry and textures from.
  255. FontFaceLayer* clone = nullptr;
  256. bool deep_clone = true;
  257. String generation_key;
  258. size_t fingerprint = font_effect->GetFingerprint();
  259. if (!font_effect->HasUniqueTexture())
  260. {
  261. clone = base_layer;
  262. deep_clone = false;
  263. }
  264. else
  265. {
  266. FontLayerCache::iterator cache_iterator = layer_cache.find(fingerprint);
  267. if (cache_iterator != layer_cache.end())
  268. clone = cache_iterator->second;
  269. }
  270. // Create a new layer.
  271. layer->Initialise(this, font_effect, clone, deep_clone);
  272. // Cache the layer in the layer cache if it generated its own textures (ie, didn't clone).
  273. if (!clone)
  274. layer_cache[fingerprint] = layer;
  275. }
  276. return layer;
  277. }
  278. }
  279. }