FontFaceHandleDefault.cpp 12 KB

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