FontFaceHandleDefault.cpp 9.6 KB

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