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