FontFaceLayer.h 5.3 KB

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