FontFaceHandleDefault.h 7.0 KB

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