2
0

FontFaceLayer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_FONTFACELAYER_H
  29. #define RMLUI_CORE_FONTENGINEDEFAULT_FONTFACELAYER_H
  30. #include "../../../Include/RmlUi/Core/CallbackTexture.h"
  31. #include "../../../Include/RmlUi/Core/FontGlyph.h"
  32. #include "../../../Include/RmlUi/Core/Geometry.h"
  33. #include "../../../Include/RmlUi/Core/MeshUtilities.h"
  34. #include "../TextureLayout.h"
  35. namespace Rml {
  36. class FontEffect;
  37. class FontFaceHandleDefault;
  38. /**
  39. A textured layer stored as part of a font face handle. Each handle will have at least a base
  40. layer for the standard font. Further layers can be added to allow rendering of text effects.
  41. @author Peter Curry
  42. */
  43. class FontFaceLayer {
  44. public:
  45. FontFaceLayer(const SharedPtr<const FontEffect>& _effect);
  46. ~FontFaceLayer();
  47. /// Generates or re-generates the character and texture data for the layer.
  48. /// @param[in] handle The handle generating this layer.
  49. /// @param[in] clone The layer to optionally clone geometry and texture data from.
  50. /// @param[in] clone_glyph_origins True to keep the character origins from the cloned layer, false to generate new ones.
  51. /// @return True if the layer was generated successfully, false if not.
  52. bool Generate(const FontFaceHandleDefault* handle, const FontFaceLayer* clone = nullptr, bool clone_glyph_origins = false);
  53. /// Generates the texture data for a layer (for the texture database).
  54. /// @param[out] texture_data The generated texture data.
  55. /// @param[out] texture_dimensions The dimensions of the texture.
  56. /// @param[in] texture_id The index of the texture within the layer to generate.
  57. /// @param[in] glyphs The glyphs required by the font face handle.
  58. bool GenerateTexture(Vector<byte>& texture_data, Vector2i& texture_dimensions, int texture_id, const FontGlyphMap& glyphs);
  59. /// Generates the geometry required to render a single character.
  60. /// @param[out] mesh_list An array of meshes this layer will write to. It must be at least as big as the number of textures in this layer.
  61. /// @param[in] character_code The character to generate geometry for.
  62. /// @param[in] position The position of the baseline.
  63. /// @param[in] colour The colour of the string.
  64. inline void GenerateGeometry(TexturedMesh* mesh_list, const Character character_code, const Vector2f position,
  65. const ColourbPremultiplied colour) const
  66. {
  67. auto it = character_boxes.find(character_code);
  68. if (it == character_boxes.end())
  69. return;
  70. const TextureBox& box = it->second;
  71. if (box.texture_index < 0)
  72. return;
  73. // Generate the geometry for the character.
  74. Mesh& mesh = mesh_list[box.texture_index].mesh;
  75. MeshUtilities::GenerateQuad(mesh, (position + box.origin).Round(), box.dimensions, colour, box.texcoords[0], box.texcoords[1]);
  76. }
  77. /// Returns the effect used to generate the layer.
  78. const FontEffect* GetFontEffect() const;
  79. /// Returns one of the layer's textures.
  80. Texture GetTexture(RenderManager& render_manager, int index);
  81. /// Returns the number of textures employed by this layer.
  82. int GetNumTextures() const;
  83. /// Returns the layer's colour after applying the given opacity.
  84. ColourbPremultiplied GetColour(float opacity) const;
  85. private:
  86. struct TextureBox {
  87. // The offset, in pixels, of the baseline from the start of this character's geometry.
  88. Vector2f origin;
  89. // The width and height, in pixels, of this character's geometry.
  90. Vector2f dimensions;
  91. // The texture coordinates for the character's geometry.
  92. Vector2f texcoords[2];
  93. // The texture this character renders from.
  94. int texture_index = -1;
  95. };
  96. using CharacterMap = UnorderedMap<Character, TextureBox>;
  97. using TextureList = Vector<CallbackTextureSource>;
  98. SharedPtr<const FontEffect> effect;
  99. TextureList textures_owned;
  100. TextureList* textures_ptr = &textures_owned;
  101. TextureLayout texture_layout;
  102. CharacterMap character_boxes;
  103. Colourb colour;
  104. };
  105. } // namespace Rml
  106. #endif