FontFaceHandleDefault.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 RMLUICOREFONTFACEHANDLE_H
  29. #define RMLUICOREFONTFACEHANDLE_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. namespace Core {
  38. class FontFaceLayer;
  39. /**
  40. @author Peter Curry
  41. */
  42. class FontFaceHandleDefault final : public NonCopyMoveable
  43. {
  44. public:
  45. FontFaceHandleDefault();
  46. ~FontFaceHandleDefault();
  47. bool Initialize(FontFaceHandleFreetype face, int font_size);
  48. /// Returns the point size of this font face.
  49. int GetSize() const;
  50. /// Returns the pixel height of a lower-case x in this font face.
  51. int GetXHeight() const;
  52. /// Returns the default height between this font face's baselines.
  53. int GetLineHeight() const;
  54. /// Returns the font's baseline, as a pixel offset from the bottom of the font.
  55. int GetBaseline() const;
  56. /// Returns the font's underline, as a pixel offset from the bottom of the font.
  57. float GetUnderline(float& thickness) const;
  58. /// Returns the font's glyphs.
  59. const FontGlyphMap& GetGlyphs() const;
  60. /// Returns the width a string will take up if rendered with this handle.
  61. /// @param[in] string The string to measure.
  62. /// @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.
  63. /// @return The width, in pixels, this string will occupy if rendered with this handle.
  64. int GetStringWidth(const String& string, Character prior_character = Character::Null);
  65. /// Generates, if required, the layer configuration for a given list of font effects.
  66. /// @param[in] font_effects The list of font effects to generate the configuration for.
  67. /// @return The index to use when generating geometry using this configuration.
  68. int GenerateLayerConfiguration(const FontEffectList& font_effects);
  69. /// Generates the texture data for a layer (for the texture database).
  70. /// @param[out] texture_data The pointer to be set to the generated texture data.
  71. /// @param[out] texture_dimensions The dimensions of the texture.
  72. /// @param[in] layer_id The id of the layer to request the texture data from.
  73. /// @param[in] texture_id The index of the texture within the layer to generate.
  74. /// @param[in] handle_version The version of the handle data. Function returns false if out of date.
  75. bool GenerateLayerTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, const FontEffect* layer_id, int texture_id, int handle_version) const;
  76. /// Generates the geometry required to render a single line of text.
  77. /// @param[out] geometry An array of geometries to generate the geometry into.
  78. /// @param[in] string The string to render.
  79. /// @param[in] position The position of the baseline of the first character to render.
  80. /// @param[in] colour The colour to render the text.
  81. /// @return The width, in pixels, of the string geometry.
  82. int GenerateString(GeometryList& geometry, const String& string, const Vector2f& position, const Colourb& colour, int layer_configuration = 0);
  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. int GetKerning(Character lhs, Character rhs) const;
  89. /// Retrieve a glyph from the given code point, building and appending a new glyph if not already built.
  90. /// @param[in-out] character The character, can be changed e.g. to the replacement character if no glyph is found.
  91. /// @param[in] look_in_fallback_fonts Look for the glyph in fallback fonts if not found locally, adding it to our glyphs.
  92. /// @return The font glyph for the returned code point.
  93. const FontGlyph* GetOrAppendGlyph(Character& character, bool look_in_fallback_fonts = true);
  94. // Regenerate layers if dirty, such as after adding new glyphs.
  95. bool UpdateLayersOnDirty();
  96. // Create a new layer from the given font effect if it does not already exist.
  97. FontFaceLayer* GetOrCreateLayer(const SharedPtr<const FontEffect>& font_effect);
  98. // (Re-)generate a layer in this font face handle.
  99. bool GenerateLayer(FontFaceLayer* layer);
  100. FontGlyphMap glyphs;
  101. using FontLayerMap = SmallUnorderedMap< const FontEffect*, UniquePtr<FontFaceLayer> >;
  102. using FontLayerCache = SmallUnorderedMap< size_t, FontFaceLayer* >;
  103. using LayerConfiguration = std::vector< FontFaceLayer* >;
  104. using LayerConfigurationList = std::vector< LayerConfiguration >;
  105. // The list of all font layers, index by the effect that instanced them.
  106. FontFaceLayer* base_layer;
  107. FontLayerMap layers;
  108. // Each font layer that generated geometry or textures, indexed by the respective generation key.
  109. FontLayerCache layer_cache;
  110. int version = 0;
  111. bool is_layers_dirty = false;
  112. // All configurations currently in use on this handle. New configurations will be generated as required.
  113. LayerConfigurationList layer_configurations;
  114. FontMetrics metrics;
  115. FontFaceHandleFreetype ft_face;
  116. };
  117. }
  118. }
  119. #endif