FontFaceHandleDefault.cpp 12 KB

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