FontFaceHandleDefault.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. metrics = {};
  40. base_layer = nullptr;
  41. }
  42. FontFaceHandleDefault::~FontFaceHandleDefault()
  43. {
  44. glyphs.clear();
  45. layers.clear();
  46. }
  47. // Returns the point size of this font face.
  48. int FontFaceHandleDefault::GetSize() const
  49. {
  50. return metrics.size;
  51. }
  52. // Returns the average advance of all glyphs in this font face.
  53. int FontFaceHandleDefault::GetCharacterWidth() const
  54. {
  55. return metrics.average_advance;
  56. }
  57. // Returns the pixel height of a lower-case x in this font face.
  58. int FontFaceHandleDefault::GetXHeight() const
  59. {
  60. return metrics.x_height;
  61. }
  62. // Returns the default height between this font face's baselines.
  63. int FontFaceHandleDefault::GetLineHeight() const
  64. {
  65. return metrics.line_height;
  66. }
  67. // Returns the font's baseline.
  68. int FontFaceHandleDefault::GetBaseline() const
  69. {
  70. return metrics.baseline;
  71. }
  72. // Returns the font's glyphs.
  73. const FontGlyphMap& FontFaceHandleDefault::GetGlyphs() const
  74. {
  75. return glyphs;
  76. }
  77. float FontFaceHandleDefault::GetUnderline(float *thickness) const
  78. {
  79. if (thickness != nullptr) {
  80. *thickness = metrics.underline_thickness;
  81. }
  82. return metrics.underline_position;
  83. }
  84. // Returns the width a string will take up if rendered with this handle.
  85. int FontFaceHandleDefault::GetStringWidth(const String& string, CodePoint prior_character)
  86. {
  87. int width = 0;
  88. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  89. {
  90. CodePoint code_point = *it_string;
  91. const FontGlyph* glyph = GetOrAppendGlyph(code_point);
  92. if (!glyph)
  93. continue;
  94. // Adjust the cursor for the kerning between this character and the previous one.
  95. if (prior_character != CodePoint::Null)
  96. width += GetKerning(prior_character, code_point);
  97. // Adjust the cursor for this character's advance.
  98. width += glyph->advance;
  99. prior_character = code_point;
  100. }
  101. return width;
  102. }
  103. // Generates, if required, the layer configuration for a given array of font effects.
  104. int FontFaceHandleDefault::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 FontFaceHandleDefault::GenerateLayerTexture(UniquePtr<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 FontFaceHandleDefault::GenerateString(GeometryList& geometry, const String& string, const Vector2f& position, const Colourb& colour, int layer_configuration_index)
  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. CodePoint prior_character = CodePoint::Null;
  187. geometry[geometry_index].GetIndices().reserve(string.size() * 6);
  188. geometry[geometry_index].GetVertices().reserve(string.size() * 4);
  189. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  190. {
  191. CodePoint code_point = *it_string;
  192. const FontGlyph* glyph = GetOrAppendGlyph(code_point);
  193. if (!glyph)
  194. continue;
  195. // Adjust the cursor for the kerning between this character and the previous one.
  196. if (prior_character != CodePoint::Null)
  197. line_width += GetKerning(prior_character, code_point);
  198. layer->GenerateGeometry(&geometry[geometry_index], code_point, Vector2f(position.x + line_width, position.y), layer_colour);
  199. line_width += glyph->advance;
  200. prior_character = code_point;
  201. }
  202. geometry_index += layer->GetNumTextures();
  203. }
  204. // Cull any excess geometry from a previous generation.
  205. geometry.resize(geometry_index);
  206. return line_width;
  207. }
  208. int FontFaceHandleDefault::UpdateOnDirty()
  209. {
  210. if(is_dirty)
  211. {
  212. is_dirty = false;
  213. ++version;
  214. // If we are dirty, regenerate the base layer and increment the version
  215. // TODO: Regenerate font effects as well.
  216. if (base_layer)
  217. {
  218. // Regenerate the base layer
  219. layers.erase(nullptr);
  220. base_layer = GenerateLayer(nullptr);
  221. layer_configurations[0][0] = base_layer;
  222. }
  223. }
  224. return version;
  225. }
  226. int FontFaceHandleDefault::GetVersion() const
  227. {
  228. return version;
  229. }
  230. FontGlyphMap& FontFaceHandleDefault::GetGlyphs() {
  231. return glyphs;
  232. }
  233. FontMetrics& FontFaceHandleDefault::GetMetrics() {
  234. return metrics;
  235. }
  236. void FontFaceHandleDefault::GenerateBaseLayer()
  237. {
  238. RMLUI_ASSERTMSG(layer_configurations.empty(), "This should only be called before any layers are generated.");
  239. base_layer = GenerateLayer(nullptr);
  240. layer_configurations.push_back(LayerConfiguration{ base_layer });
  241. }
  242. const FontGlyph* FontFaceHandleDefault::GetOrAppendGlyph(CodePoint& code_point)
  243. {
  244. // Don't try to render control characters
  245. if ((unsigned int)code_point < (unsigned int)' ')
  246. return nullptr;
  247. auto it_glyph = glyphs.find(code_point);
  248. if (it_glyph == glyphs.end())
  249. {
  250. bool result = AppendGlyph(code_point);
  251. if (result)
  252. {
  253. it_glyph = glyphs.find(code_point);
  254. if (it_glyph == glyphs.end())
  255. {
  256. RMLUI_ERROR;
  257. return nullptr;
  258. }
  259. is_dirty = true;
  260. }
  261. else
  262. {
  263. code_point = CodePoint::Replacement;
  264. it_glyph = glyphs.find(code_point);
  265. if (it_glyph == glyphs.end())
  266. return nullptr;
  267. }
  268. }
  269. const FontGlyph* glyph = &it_glyph->second;
  270. return glyph;
  271. }
  272. // Generates (or shares) a layer derived from a font effect.
  273. FontFaceLayer* FontFaceHandleDefault::GenerateLayer(const SharedPtr<const FontEffect>& font_effect)
  274. {
  275. // See if this effect has been instanced before, as part of a different configuration.
  276. FontLayerMap::iterator i = layers.find(font_effect.get());
  277. if (i != layers.end())
  278. return i->second.get();
  279. UniquePtr<FontFaceLayer> layer_ptr = std::make_unique<FontFaceLayer>();
  280. FontFaceLayer* layer = layer_ptr.get();
  281. layers[font_effect.get()] = std::move(layer_ptr);
  282. if (!font_effect)
  283. {
  284. layer->Initialise(this);
  285. }
  286. else
  287. {
  288. // Determine which, if any, layer the new layer should copy its geometry and textures from.
  289. FontFaceLayer* clone = nullptr;
  290. bool deep_clone = true;
  291. String generation_key;
  292. size_t fingerprint = font_effect->GetFingerprint();
  293. if (!font_effect->HasUniqueTexture())
  294. {
  295. clone = base_layer;
  296. deep_clone = false;
  297. }
  298. else
  299. {
  300. FontLayerCache::iterator cache_iterator = layer_cache.find(fingerprint);
  301. if (cache_iterator != layer_cache.end())
  302. clone = cache_iterator->second;
  303. }
  304. // Create a new layer.
  305. layer->Initialise(this, font_effect, clone, deep_clone);
  306. // Cache the layer in the layer cache if it generated its own textures (ie, didn't clone).
  307. if (!clone)
  308. layer_cache[fingerprint] = layer;
  309. }
  310. return layer;
  311. }
  312. #endif
  313. }
  314. }