FontFaceHandle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. class FontEffectSort
  37. {
  38. public:
  39. bool operator()(const FontEffect* lhs, const FontEffect* rhs)
  40. {
  41. return lhs->GetZIndex() < rhs->GetZIndex();
  42. }
  43. };
  44. FontFaceHandle::FontFaceHandle()
  45. {
  46. size = 0;
  47. average_advance = 0;
  48. x_height = 0;
  49. line_height = 0;
  50. baseline = 0;
  51. underline_position = 0;
  52. underline_thickness = 0;
  53. base_layer = NULL;
  54. }
  55. FontFaceHandle::~FontFaceHandle()
  56. {
  57. for (FontGlyphList::iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  58. delete[] i->bitmap_data;
  59. for (FontLayerMap::iterator i = layers.begin(); i != layers.end(); ++i)
  60. delete i->second;
  61. }
  62. // Returns the point size of this font face.
  63. int FontFaceHandle::GetSize() const
  64. {
  65. return size;
  66. }
  67. // Returns the average advance of all glyphs in this font face.
  68. int FontFaceHandle::GetCharacterWidth() const
  69. {
  70. return average_advance;
  71. }
  72. // Returns the pixel height of a lower-case x in this font face.
  73. int FontFaceHandle::GetXHeight() const
  74. {
  75. return x_height;
  76. }
  77. // Returns the default height between this font face's baselines.
  78. int FontFaceHandle::GetLineHeight() const
  79. {
  80. return line_height;
  81. }
  82. // Returns the font's baseline.
  83. int FontFaceHandle::GetBaseline() const
  84. {
  85. return baseline;
  86. }
  87. // Returns the font's glyphs.
  88. const FontGlyphList& FontFaceHandle::GetGlyphs() const
  89. {
  90. return glyphs;
  91. }
  92. // Returns the width a string will take up if rendered with this handle.
  93. int FontFaceHandle::GetStringWidth(const WString& string, word prior_character) const
  94. {
  95. int width = 0;
  96. for (size_t i = 0; i < string.size(); i++)
  97. {
  98. word character_code = string[i];
  99. if (character_code >= glyphs.size())
  100. continue;
  101. const FontGlyph &glyph = glyphs[character_code];
  102. // Adjust the cursor for the kerning between this character and the previous one.
  103. if (prior_character != 0)
  104. width += GetKerning(prior_character, string[i]);
  105. // Adjust the cursor for this character's advance.
  106. width += glyph.advance;
  107. prior_character = character_code;
  108. }
  109. return width;
  110. }
  111. // Generates, if required, the layer configuration for a given array of font effects.
  112. int FontFaceHandle::GenerateLayerConfiguration(FontEffectMap& font_effects)
  113. {
  114. if (font_effects.empty())
  115. return 0;
  116. // Prepare a list of effects, sorted by z-index.
  117. FontEffectList sorted_effects;
  118. for (FontEffectMap::const_iterator i = font_effects.begin(); i != font_effects.end(); ++i)
  119. sorted_effects.push_back(i->second);
  120. std::sort(sorted_effects.begin(), sorted_effects.end(), FontEffectSort());
  121. // Check each existing configuration for a match with this arrangement of effects.
  122. int configuration_index = 1;
  123. for (; configuration_index < (int) layer_configurations.size(); ++configuration_index)
  124. {
  125. const LayerConfiguration& configuration = layer_configurations[configuration_index];
  126. // Check the size is correct. For a math, there should be one layer in the configuration
  127. // plus an extra for the base layer.
  128. if (configuration.size() != sorted_effects.size() + 1)
  129. continue;
  130. // Check through each layer, checking it was created by the same effect as the one we're
  131. // checking.
  132. size_t effect_index = 0;
  133. for (size_t i = 0; i < configuration.size(); ++i)
  134. {
  135. // Skip the base layer ...
  136. if (configuration[i]->GetFontEffect() == NULL)
  137. continue;
  138. // If the ith layer's effect doesn't match the equivalent effect, then this
  139. // configuration can't match.
  140. if (configuration[i]->GetFontEffect() != sorted_effects[effect_index])
  141. break;
  142. // Check the next one ...
  143. ++effect_index;
  144. }
  145. if (effect_index == sorted_effects.size())
  146. return configuration_index;
  147. }
  148. // No match, so we have to generate a new layer configuration.
  149. layer_configurations.push_back(LayerConfiguration());
  150. LayerConfiguration& layer_configuration = layer_configurations.back();
  151. bool added_base_layer = false;
  152. for (size_t i = 0; i < sorted_effects.size(); ++i)
  153. {
  154. if (!added_base_layer &&
  155. sorted_effects[i]->GetZIndex() >= 0)
  156. {
  157. layer_configuration.push_back(base_layer);
  158. added_base_layer = true;
  159. }
  160. layer_configuration.push_back(GenerateLayer(sorted_effects[i]));
  161. }
  162. // Add the base layer now if we still haven't added it.
  163. if (!added_base_layer)
  164. layer_configuration.push_back(base_layer);
  165. return (int) (layer_configurations.size() - 1);
  166. }
  167. // Generates the texture data for a layer (for the texture database).
  168. bool FontFaceHandle::GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id)
  169. {
  170. FontLayerMap::iterator layer_iterator = layers.find(layer_id);
  171. if (layer_iterator == layers.end())
  172. return false;
  173. return layer_iterator->second->GenerateTexture(texture_data, texture_dimensions, texture_id);
  174. }
  175. // Generates the geometry required to render a single line of text.
  176. int FontFaceHandle::GenerateString(GeometryList& geometry, const WString& string, const Vector2f& position, const Colourb& colour, int layer_configuration_index) const
  177. {
  178. int geometry_index = 0;
  179. int line_width = 0;
  180. RMLUI_ASSERT(layer_configuration_index >= 0);
  181. RMLUI_ASSERT(layer_configuration_index < (int) layer_configurations.size());
  182. // Fetch the requested configuration and generate the geometry for each one.
  183. const LayerConfiguration& layer_configuration = layer_configurations[layer_configuration_index];
  184. for (size_t i = 0; i < layer_configuration.size(); ++i)
  185. {
  186. FontFaceLayer* layer = layer_configuration[i];
  187. Colourb layer_colour;
  188. if (layer == base_layer)
  189. layer_colour = colour;
  190. else
  191. layer_colour = layer->GetColour();
  192. // Resize the geometry list if required.
  193. if ((int) geometry.size() < geometry_index + layer->GetNumTextures())
  194. geometry.resize(geometry_index + layer->GetNumTextures());
  195. // Bind the textures to the geometries.
  196. for (int i = 0; i < layer->GetNumTextures(); ++i)
  197. geometry[geometry_index + i].SetTexture(layer->GetTexture(i));
  198. line_width = 0;
  199. word prior_character = 0;
  200. const word* string_iterator = string.c_str();
  201. const word* string_end = string.c_str() + string.size();
  202. for (; string_iterator != string_end; string_iterator++)
  203. {
  204. if (*string_iterator >= glyphs.size())
  205. continue;
  206. const FontGlyph &glyph = glyphs[*string_iterator];
  207. // Adjust the cursor for the kerning between this character and the previous one.
  208. if (prior_character != 0)
  209. line_width += GetKerning(prior_character, *string_iterator);
  210. layer->GenerateGeometry(&geometry[geometry_index], *string_iterator, Vector2f(position.x + line_width, position.y), layer_colour);
  211. line_width += glyph.advance;
  212. prior_character = *string_iterator;
  213. }
  214. geometry_index += layer->GetNumTextures();
  215. }
  216. // Cull any excess geometry from a previous generation.
  217. geometry.resize(geometry_index);
  218. return line_width;
  219. }
  220. // Generates the geometry required to render a line above, below or through a line of text.
  221. void FontFaceHandle::GenerateLine(Geometry* geometry, const Vector2f& position, int width, Font::Line height, const Colourb& colour) const
  222. {
  223. std::vector< Vertex >& line_vertices = geometry->GetVertices();
  224. std::vector< int >& line_indices = geometry->GetIndices();
  225. float offset;
  226. switch (height)
  227. {
  228. case Font::UNDERLINE: offset = -underline_position; break;
  229. case Font::OVERLINE: // where to place? offset = -line_height - underline_position; break;
  230. case Font::STRIKE_THROUGH: // where to place? offset = -line_height * 0.5f; break;
  231. default: return;
  232. }
  233. line_vertices.resize(line_vertices.size() + 4);
  234. line_indices.resize(line_indices.size() + 6);
  235. GeometryUtilities::GenerateQuad(&line_vertices[0] + (line_vertices.size() - 4), &line_indices[0] + (line_indices.size() - 6), Vector2f(position.x, position.y + offset).Round(), Vector2f((float) width, underline_thickness), colour, (int)line_vertices.size() - 4);
  236. }
  237. // Returns the font face's raw charset (the charset range as a string).
  238. const String& FontFaceHandle::GetRawCharset() const
  239. {
  240. return raw_charset;
  241. }
  242. // Returns the font face's charset.
  243. const UnicodeRangeList& FontFaceHandle::GetCharset() const
  244. {
  245. return charset;
  246. }
  247. // Destroys the handle.
  248. void FontFaceHandle::OnReferenceDeactivate()
  249. {
  250. delete this;
  251. }
  252. // Generates (or shares) a layer derived from a font effect.
  253. FontFaceLayer* FontFaceHandle::GenerateLayer(FontEffect* font_effect)
  254. {
  255. // See if this effect has been instanced before, as part of a different configuration.
  256. FontLayerMap::iterator i = layers.find(font_effect);
  257. if (i != layers.end())
  258. return i->second;
  259. FontFaceLayer* layer = new FontFaceLayer();
  260. layers[font_effect] = layer;
  261. if (font_effect == NULL)
  262. {
  263. layer->Initialise(this);
  264. }
  265. else
  266. {
  267. // Determine which, if any, layer the new layer should copy its geometry and textures from.
  268. FontFaceLayer* clone = NULL;
  269. bool deep_clone = true;
  270. String generation_key;
  271. if (!font_effect->HasUniqueTexture())
  272. {
  273. clone = base_layer;
  274. deep_clone = false;
  275. }
  276. else
  277. {
  278. generation_key = font_effect->GetName() + ";" + font_effect->GetGenerationKey();
  279. FontLayerCache::iterator cache_iterator = layer_cache.find(generation_key);
  280. if (cache_iterator != layer_cache.end())
  281. clone = cache_iterator->second;
  282. }
  283. // Create a new layer.
  284. layer->Initialise(this, font_effect, clone, deep_clone);
  285. // Cache the layer in the layer cache if it generated its own textures (ie, didn't clone).
  286. if (clone == NULL)
  287. layer_cache[generation_key] = layer;
  288. }
  289. return layer;
  290. }
  291. }
  292. }