FontFaceHandleDefault.cpp 13 KB

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