FontFaceHandleDefault.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "FontDatabaseDefault.h"
  31. #include <algorithm>
  32. #include "../../Include/RmlUi/Core.h"
  33. #include "FontFaceLayer.h"
  34. #include "TextureLayout.h"
  35. namespace Rml {
  36. namespace Core {
  37. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  38. FontFaceHandleDefault::FontFaceHandleDefault()
  39. {
  40. metrics = {};
  41. base_layer = nullptr;
  42. }
  43. FontFaceHandleDefault::~FontFaceHandleDefault()
  44. {
  45. glyphs.clear();
  46. layers.clear();
  47. }
  48. // Returns the point size of this font face.
  49. int FontFaceHandleDefault::GetSize() const
  50. {
  51. return metrics.size;
  52. }
  53. // Returns the pixel height of a lower-case x in this font face.
  54. int FontFaceHandleDefault::GetXHeight() const
  55. {
  56. return metrics.x_height;
  57. }
  58. // Returns the default height between this font face's baselines.
  59. int FontFaceHandleDefault::GetLineHeight() const
  60. {
  61. return metrics.line_height;
  62. }
  63. // Returns the font's baseline.
  64. int FontFaceHandleDefault::GetBaseline() const
  65. {
  66. return metrics.baseline;
  67. }
  68. // Returns the font's glyphs.
  69. const FontGlyphMap& FontFaceHandleDefault::GetGlyphs() const
  70. {
  71. return glyphs;
  72. }
  73. float FontFaceHandleDefault::GetUnderline(float& thickness) const
  74. {
  75. thickness = metrics.underline_thickness;
  76. return metrics.underline_position;
  77. }
  78. // Returns the width a string will take up if rendered with this handle.
  79. int FontFaceHandleDefault::GetStringWidth(const String& string, CodePoint prior_character)
  80. {
  81. int width = 0;
  82. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  83. {
  84. CodePoint code_point = *it_string;
  85. const FontGlyph* glyph = GetOrAppendGlyph(code_point);
  86. if (!glyph)
  87. continue;
  88. // Adjust the cursor for the kerning between this character and the previous one.
  89. if (prior_character != CodePoint::Null)
  90. width += GetKerning(prior_character, code_point);
  91. // Adjust the cursor for this character's advance.
  92. width += glyph->advance;
  93. prior_character = code_point;
  94. }
  95. return width;
  96. }
  97. // Generates, if required, the layer configuration for a given array of font effects.
  98. int FontFaceHandleDefault::GenerateLayerConfiguration(const FontEffectList& font_effects)
  99. {
  100. if (font_effects.empty())
  101. return 0;
  102. // Check each existing configuration for a match with this arrangement of effects.
  103. int configuration_index = 1;
  104. for (; configuration_index < (int) layer_configurations.size(); ++configuration_index)
  105. {
  106. const LayerConfiguration& configuration = layer_configurations[configuration_index];
  107. // Check the size is correct. For a match, there should be one layer in the configuration
  108. // plus an extra for the base layer.
  109. if (configuration.size() != font_effects.size() + 1)
  110. continue;
  111. // Check through each layer, checking it was created by the same effect as the one we're
  112. // checking.
  113. size_t effect_index = 0;
  114. for (size_t i = 0; i < configuration.size(); ++i)
  115. {
  116. // Skip the base layer ...
  117. if (configuration[i]->GetFontEffect() == nullptr)
  118. continue;
  119. // If the ith layer's effect doesn't match the equivalent effect, then this
  120. // configuration can't match.
  121. if (configuration[i]->GetFontEffect() != font_effects[effect_index].get())
  122. break;
  123. // Check the next one ...
  124. ++effect_index;
  125. }
  126. if (effect_index == font_effects.size())
  127. return configuration_index;
  128. }
  129. // No match, so we have to generate a new layer configuration.
  130. layer_configurations.push_back(LayerConfiguration());
  131. LayerConfiguration& layer_configuration = layer_configurations.back();
  132. bool added_base_layer = false;
  133. for (size_t i = 0; i < font_effects.size(); ++i)
  134. {
  135. if (!added_base_layer && font_effects[i]->GetLayer() == FontEffect::Layer::Front)
  136. {
  137. layer_configuration.push_back(base_layer);
  138. added_base_layer = true;
  139. }
  140. FontFaceLayer* new_layer = GetOrCreateLayer(font_effects[i]);
  141. layer_configuration.push_back(new_layer);
  142. }
  143. // Add the base layer now if we still haven't added it.
  144. if (!added_base_layer)
  145. layer_configuration.push_back(base_layer);
  146. return (int) (layer_configurations.size() - 1);
  147. }
  148. // Generates the texture data for a layer (for the texture database).
  149. bool FontFaceHandleDefault::GenerateLayerTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, FontEffect* layer_id, int texture_id, int handle_version)
  150. {
  151. if (handle_version != version)
  152. return false;
  153. FontLayerMap::iterator layer_iterator = layers.find(layer_id);
  154. if (layer_iterator == layers.end())
  155. return false;
  156. return layer_iterator->second->GenerateTexture(glyphs, texture_data, texture_dimensions, texture_id);
  157. }
  158. // Generates the geometry required to render a single line of text.
  159. int FontFaceHandleDefault::GenerateString(GeometryList& geometry, const String& string, const Vector2f& position, const Colourb& colour, int layer_configuration_index)
  160. {
  161. int geometry_index = 0;
  162. int line_width = 0;
  163. RMLUI_ASSERT(layer_configuration_index >= 0);
  164. RMLUI_ASSERT(layer_configuration_index < (int) layer_configurations.size());
  165. UpdateLayersOnDirty();
  166. // Fetch the requested configuration and generate the geometry for each one.
  167. const LayerConfiguration& layer_configuration = layer_configurations[layer_configuration_index];
  168. for (size_t i = 0; i < layer_configuration.size(); ++i)
  169. {
  170. FontFaceLayer* layer = layer_configuration[i];
  171. Colourb layer_colour;
  172. if (layer == base_layer)
  173. layer_colour = colour;
  174. else
  175. layer_colour = layer->GetColour();
  176. // Resize the geometry list if required.
  177. if ((int) geometry.size() < geometry_index + layer->GetNumTextures())
  178. geometry.resize(geometry_index + layer->GetNumTextures());
  179. RMLUI_ASSERT(!geometry.empty());
  180. // Bind the textures to the geometries.
  181. for (int i = 0; i < layer->GetNumTextures(); ++i)
  182. geometry[geometry_index + i].SetTexture(layer->GetTexture(i));
  183. line_width = 0;
  184. CodePoint prior_character = CodePoint::Null;
  185. geometry[geometry_index].GetIndices().reserve(string.size() * 6);
  186. geometry[geometry_index].GetVertices().reserve(string.size() * 4);
  187. for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
  188. {
  189. CodePoint code_point = *it_string;
  190. const FontGlyph* glyph = GetOrAppendGlyph(code_point);
  191. if (!glyph)
  192. continue;
  193. // Adjust the cursor for the kerning between this character and the previous one.
  194. if (prior_character != CodePoint::Null)
  195. line_width += GetKerning(prior_character, code_point);
  196. layer->GenerateGeometry(&geometry[geometry_index], code_point, Vector2f(position.x + line_width, position.y), layer_colour);
  197. line_width += glyph->advance;
  198. prior_character = code_point;
  199. }
  200. geometry_index += layer->GetNumTextures();
  201. }
  202. // Cull any excess geometry from a previous generation.
  203. geometry.resize(geometry_index);
  204. return line_width;
  205. }
  206. bool FontFaceHandleDefault::UpdateLayersOnDirty()
  207. {
  208. bool result = false;
  209. // If we are dirty, regenerate all the layers and increment the version
  210. if(is_layers_dirty && base_layer)
  211. {
  212. is_layers_dirty = false;
  213. ++version;
  214. // Regenerate all the layers
  215. for (auto& pair : layers)
  216. {
  217. FontFaceLayer* layer = pair.second.get();
  218. GenerateLayer(layer);
  219. }
  220. result = true;
  221. }
  222. return result;
  223. }
  224. int FontFaceHandleDefault::GetVersion() const
  225. {
  226. return version;
  227. }
  228. FontGlyphMap& FontFaceHandleDefault::GetGlyphs() {
  229. return glyphs;
  230. }
  231. FontMetrics& FontFaceHandleDefault::GetMetrics() {
  232. return metrics;
  233. }
  234. void FontFaceHandleDefault::GenerateBaseLayer()
  235. {
  236. RMLUI_ASSERTMSG(layer_configurations.empty(), "GenerateBaseLayer should only be called before any layers are generated.");
  237. base_layer = GetOrCreateLayer(nullptr);
  238. layer_configurations.push_back(LayerConfiguration{ base_layer });
  239. }
  240. const FontGlyph* FontFaceHandleDefault::GetOrAppendGlyph(CodePoint& code_point, bool look_in_fallback_fonts)
  241. {
  242. // Don't try to render control characters
  243. if ((unsigned int)code_point < (unsigned int)' ')
  244. return nullptr;
  245. auto it_glyph = glyphs.find(code_point);
  246. if (it_glyph == glyphs.end())
  247. {
  248. bool result = AppendGlyph(code_point);
  249. if (result)
  250. {
  251. it_glyph = glyphs.find(code_point);
  252. if (it_glyph == glyphs.end())
  253. {
  254. RMLUI_ERROR;
  255. return nullptr;
  256. }
  257. is_layers_dirty = true;
  258. }
  259. else if (look_in_fallback_fonts)
  260. {
  261. const int num_fallback_faces = FontDatabaseDefault::CountFallbackFontFaces();
  262. for (int i = 0; i < num_fallback_faces; i++)
  263. {
  264. FontFaceHandleDefault* fallback_face = FontDatabaseDefault::GetFallbackFontFace(i, metrics.size).get();
  265. if (!fallback_face || fallback_face == this)
  266. continue;
  267. const FontGlyph* glyph = fallback_face->GetOrAppendGlyph(code_point, false);
  268. if (glyph)
  269. {
  270. // Insert the new glyph into our own set of glyphs
  271. auto pair = glyphs.emplace(code_point, glyph->WeakCopy());
  272. it_glyph = pair.first;
  273. if(pair.second)
  274. is_layers_dirty = true;
  275. break;
  276. }
  277. }
  278. // If we still have not found a glyph, use the replacement character.
  279. if(it_glyph == glyphs.end())
  280. {
  281. code_point = CodePoint::Replacement;
  282. it_glyph = glyphs.find(code_point);
  283. if (it_glyph == glyphs.end())
  284. return nullptr;
  285. }
  286. }
  287. else
  288. {
  289. return nullptr;
  290. }
  291. }
  292. const FontGlyph* glyph = &it_glyph->second;
  293. return glyph;
  294. }
  295. // Generates (or shares) a layer derived from a font effect.
  296. FontFaceLayer* FontFaceHandleDefault::GetOrCreateLayer(const SharedPtr<const FontEffect>& font_effect)
  297. {
  298. // Try inserting the font effect, it may have been instanced before as part of a different configuration.
  299. auto pair = layers.emplace(font_effect.get(), nullptr);
  300. bool inserted = pair.second;
  301. auto& layer = pair.first->second;
  302. if (!inserted)
  303. return layer.get();
  304. // The new effect was inserted, generate a new layer.
  305. layer = std::make_unique<FontFaceLayer>(font_effect);
  306. GenerateLayer(layer.get());
  307. return layer.get();
  308. }
  309. bool FontFaceHandleDefault::GenerateLayer(FontFaceLayer* layer)
  310. {
  311. RMLUI_ASSERT(layer);
  312. const FontEffect* font_effect = layer->GetFontEffect();
  313. bool result = false;
  314. if (!font_effect)
  315. {
  316. result = layer->Generate(this);
  317. }
  318. else
  319. {
  320. // Determine which, if any, layer the new layer should copy its geometry and textures from.
  321. FontFaceLayer* clone = nullptr;
  322. bool clone_glyph_origins = true;
  323. String generation_key;
  324. size_t fingerprint = font_effect->GetFingerprint();
  325. if (!font_effect->HasUniqueTexture())
  326. {
  327. clone = base_layer;
  328. clone_glyph_origins = false;
  329. }
  330. else
  331. {
  332. auto cache_iterator = layer_cache.find(fingerprint);
  333. if (cache_iterator != layer_cache.end() && cache_iterator->second != layer)
  334. clone = cache_iterator->second;
  335. }
  336. // Create a new layer.
  337. result = layer->Generate(this, clone, clone_glyph_origins);
  338. // Cache the layer in the layer cache if it generated its own textures (ie, didn't clone).
  339. if (!clone)
  340. layer_cache[fingerprint] = layer;
  341. }
  342. return result;
  343. }
  344. #endif
  345. }
  346. }