FontFaceHandleDefault.cpp 12 KB

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