FontFaceHandle.cpp 10 KB

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