FontFaceLayer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/String.h"
  34. #include "../../Include/RmlUi/Core/Texture.h"
  35. #include "TextureLayout.h"
  36. namespace Rml {
  37. namespace Core {
  38. class FontEffect;
  39. class FontFaceHandle;
  40. /**
  41. A textured layer stored as part of a font face handle. Each handle will have at least a base
  42. layer for the standard font. Further layers can be added to allow to rendering of text effects.
  43. @author Peter Curry
  44. */
  45. class FontFaceLayer
  46. {
  47. public:
  48. FontFaceLayer();
  49. virtual ~FontFaceLayer();
  50. /// Generates the character and texture data for the layer.
  51. /// @param[in] handle The handle generating this layer.
  52. /// @param[in] effect The effect to initialise the layer with.
  53. /// @param[in] clone The layer to optionally clone geometry and texture data from.
  54. /// @param[in] deep_clone If true, the clones geometry will be completely cloned and the effect will have no option to affect even the glyph origins.
  55. /// @return True if the layer was generated successfully, false if not.
  56. virtual bool Initialise(const FontFaceHandle* handle, SharedPtr<const FontEffect> effect = {}, const FontFaceLayer* clone = nullptr, bool deep_clone = false);
  57. /// Generates the texture data for a layer (for the texture database).
  58. /// @param[out] texture_data The pointer to be set to the generated texture data.
  59. /// @param[out] texture_dimensions The dimensions of the texture.
  60. /// @param[in] glyphs The glyphs required by the font face handle.
  61. /// @param[in] texture_id The index of the texture within the layer to generate.
  62. virtual bool GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id);
  63. /// Generates the geometry required to render a single character.
  64. /// @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.
  65. /// @param[in] character_code The character to generate geometry for.
  66. /// @param[in] position The position of the baseline.
  67. /// @param[in] colour The colour of the string.
  68. inline void GenerateGeometry(Geometry* geometry, const word character_code, const Vector2f& position, const Colourb& colour) const
  69. {
  70. if (character_code >= characters.size())
  71. return;
  72. const Character& character = characters[character_code];
  73. if (character.texture_index < 0)
  74. return;
  75. // Generate the geometry for the character.
  76. std::vector< Vertex >& character_vertices = geometry[character.texture_index].GetVertices();
  77. std::vector< int >& character_indices = geometry[character.texture_index].GetIndices();
  78. character_vertices.resize(character_vertices.size() + 4);
  79. character_indices.resize(character_indices.size() + 6);
  80. GeometryUtilities::GenerateQuad(
  81. &character_vertices[0] + (character_vertices.size() - 4),
  82. &character_indices[0] + (character_indices.size() - 6),
  83. Vector2f(position.x + character.origin.x, position.y + character.origin.y).Round(),
  84. character.dimensions,
  85. colour,
  86. character.texcoords[0],
  87. character.texcoords[1],
  88. (int)character_vertices.size() - 4
  89. );
  90. }
  91. /// Returns the effect used to generate the layer.
  92. /// @return The layer's effect.
  93. const FontEffect* GetFontEffect() const;
  94. /// Returns on the layer's textures.
  95. /// @param[in] index The index of the desired texture.
  96. /// @return The requested texture.
  97. const Texture* GetTexture(int index);
  98. /// Returns the number of textures employed by this layer.
  99. /// @return The number of used textures.
  100. int GetNumTextures() const;
  101. /// Returns the layer's colour.
  102. /// @return The layer's colour.
  103. const Colourb& GetColour() const;
  104. // protected:
  105. struct Character
  106. {
  107. Character() : texture_index(-1) { }
  108. // The offset, in pixels, of the baseline from the start of this character's geometry.
  109. Vector2f origin;
  110. // The width and height, in pixels, of this character's geometry.
  111. Vector2f dimensions;
  112. // The texture coordinates for the character's geometry.
  113. Vector2f texcoords[2];
  114. // The texture this character renders from.
  115. int texture_index;
  116. };
  117. typedef std::vector< Character > CharacterList;
  118. typedef std::vector< Texture > TextureList;
  119. const FontFaceHandle* handle;
  120. SharedPtr<const FontEffect> effect;
  121. TextureLayout texture_layout;
  122. CharacterList characters;
  123. TextureList textures;
  124. Colourb colour;
  125. };
  126. }
  127. }
  128. #endif