FontFaceHandleDefault.h 7.1 KB

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