FontFaceHandleDefault.h 6.8 KB

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