FontFaceHandleDefault.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-2023 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. #ifndef RMLUI_CORE_FONTENGINEDEFAULT_FONTFACEHANDLE_H
  29. #define RMLUI_CORE_FONTENGINEDEFAULT_FONTFACEHANDLE_H
  30. #include "../../../Include/RmlUi/Core/FontEffect.h"
  31. #include "../../../Include/RmlUi/Core/FontGlyph.h"
  32. #include "../../../Include/RmlUi/Core/FontMetrics.h"
  33. #include "../../../Include/RmlUi/Core/Geometry.h"
  34. #include "../../../Include/RmlUi/Core/Texture.h"
  35. #include "../../../Include/RmlUi/Core/Traits.h"
  36. #include "../../../Include/RmlUi/Core/Types.h"
  37. #include "FontTypes.h"
  38. #include "LruList.h"
  39. namespace Rml {
  40. class FontFaceLayer;
  41. /**
  42. @author Peter Curry
  43. */
  44. class FontFaceHandleDefault final : public NonCopyMoveable {
  45. public:
  46. FontFaceHandleDefault();
  47. ~FontFaceHandleDefault();
  48. bool Initialize(FontFaceHandleFreetype face, int font_size, bool load_default_glyphs);
  49. const FontMetrics& GetFontMetrics() const;
  50. const FontGlyphMap& GetGlyphs() const;
  51. void PurgeUnusedGlyphs();
  52. /// Returns the width a string will take up if rendered with this handle.
  53. /// @param[in] string The string to measure.
  54. /// @param[in] prior_character The optionally-specified character that immediately precedes the string. This may have an impact on the string
  55. /// width due to kerning.
  56. /// @return The width, in pixels, this string will occupy if rendered with this handle.
  57. int GetStringWidth(StringView string, float letter_spacing, Character prior_character = Character::Null);
  58. /// Generates, if required, the layer configuration for a given list of font effects.
  59. /// @param[in] font_effects The list of font effects to generate the configuration for.
  60. /// @return The index to use when generating geometry using this configuration.
  61. int GenerateLayerConfiguration(const FontEffectList& font_effects);
  62. /// Generates the texture data for a layer (for the texture database).
  63. /// @param[out] texture_data The generated texture data.
  64. /// @param[out] texture_dimensions The dimensions of the texture.
  65. /// @param[in] font_effect The font effect used for the layer.
  66. /// @param[in] texture_id The index of the texture within the layer to generate.
  67. /// @param[in] handle_version The version of the handle data. Function returns false if out of date.
  68. bool GenerateLayerTexture(Vector<byte>& texture_data, Vector2i& texture_dimensions, const FontEffect* font_effect, int texture_id,
  69. int handle_version) const;
  70. /// Generates the geometry required to render a single line of text.
  71. /// @param[in] render_manager The render manager responsible for rendering the string.
  72. /// @param[out] mesh_list A list to place the new meshes into.
  73. /// @param[in] string The string to render.
  74. /// @param[in] position The position of the baseline of the first character to render.
  75. /// @param[in] colour The colour to render the text.
  76. /// @param[in] opacity The opacity of the text, should be applied to font effects.
  77. /// @param[in] letter_spacing The letter spacing size in pixels.
  78. /// @param[in] layer_configuration Face configuration index to use for generating string.
  79. /// @return The width, in pixels, of the string geometry.
  80. int GenerateString(RenderManager& render_manager, TexturedMeshList& mesh_list, StringView string, Vector2f position, ColourbPremultiplied colour,
  81. float opacity, float letter_spacing, int layer_configuration);
  82. bool EnsureGlyphs(StringView string);
  83. /// Version is changed whenever the layers are dirtied, requiring regeneration of string geometry.
  84. int GetVersion() const;
  85. private:
  86. // Build and append glyph to 'glyphs'
  87. bool AppendGlyph(Character character);
  88. // Build a kerning cache for common characters.
  89. void FillKerningPairCache();
  90. // Return the kerning for a character pair.
  91. int GetKerning(Character lhs, Character rhs, bool& has_set_size) const;
  92. /// Retrieve a glyph from the given code point, building and appending a new glyph if not already built.
  93. /// @param[in-out] character The character, can be changed e.g. to the replacement character if no glyph is found.
  94. /// @param[in] look_in_fallback_fonts Look for the glyph in fallback fonts if not found locally, adding it to our glyphs.
  95. /// @return The font glyph for the returned code point.
  96. const FontGlyph* GetOrAppendGlyph(Character& character, bool look_in_fallback_fonts = true);
  97. // Regenerate layers if dirty, such as after adding new glyphs.
  98. bool UpdateLayersOnDirty();
  99. // Create a new layer from the given font effect if it does not already exist.
  100. FontFaceLayer* GetOrCreateLayer(const SharedPtr<const FontEffect>& font_effect);
  101. // (Re-)generate a layer in this font face handle.
  102. bool GenerateLayer(FontFaceLayer* layer);
  103. using GlyphLruList = LruList<Character>;
  104. FontGlyphMap glyphs;
  105. GlyphLruList glyph_lru_list;
  106. UnorderedMap<Character, GlyphLruList::Handle> glyph_lru_list_handle_map;
  107. Vector<Character> new_characters;
  108. Vector<const FontGlyphMap::value_type*> new_glyphs;
  109. struct EffectLayerPair {
  110. const FontEffect* font_effect;
  111. UniquePtr<FontFaceLayer> layer;
  112. };
  113. using FontLayerMap = Vector<EffectLayerPair>;
  114. using FontLayerCache = SmallUnorderedMap<size_t, FontFaceLayer*>;
  115. using LayerConfiguration = Vector<FontFaceLayer*>;
  116. using LayerConfigurationList = Vector<LayerConfiguration>;
  117. // The list of all font layers, index by the effect that instanced them.
  118. FontFaceLayer* base_layer;
  119. FontLayerMap layers;
  120. // Each font layer that generated geometry or textures, indexed by the font-effect's fingerprint key.
  121. FontLayerCache layer_cache;
  122. // Pre-cache kerning pairs for some ascii subset of all characters.
  123. using AsciiPair = uint16_t;
  124. using KerningIntType = int16_t;
  125. using KerningPairs = UnorderedMap<AsciiPair, KerningIntType>;
  126. KerningPairs kerning_pair_cache;
  127. bool has_kerning = false;
  128. bool is_layers_dirty = false;
  129. int version = 0;
  130. // All configurations currently in use on this handle. New configurations will be generated as required.
  131. LayerConfigurationList layer_configurations;
  132. FontMetrics metrics;
  133. FontFaceHandleFreetype ft_face;
  134. };
  135. } // namespace Rml
  136. #endif