FontFaceLayer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREFONTFACELAYER_H
  28. #define ROCKETCOREFONTFACELAYER_H
  29. #include "../../Include/Rocket/Core/FontGlyph.h"
  30. #include "../../Include/Rocket/Core/Geometry.h"
  31. #include "../../Include/Rocket/Core/GeometryUtilities.h"
  32. #include "../../Include/Rocket/Core/String.h"
  33. #include "../../Include/Rocket/Core/Texture.h"
  34. #include "TextureLayout.h"
  35. namespace Rocket {
  36. namespace Core {
  37. class FontEffect;
  38. class FontFaceHandle;
  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 to rendering of text effects.
  42. @author Peter Curry
  43. */
  44. class FontFaceLayer
  45. {
  46. public:
  47. FontFaceLayer();
  48. ~FontFaceLayer();
  49. /// 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. /// @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.
  54. /// @return True if the layer was generated successfully, false if not.
  55. virtual bool Initialise(const FontFaceHandle* handle, FontEffect* effect = NULL, const FontFaceLayer* clone = NULL, bool deep_clone = false);
  56. /// Generates the texture data for a layer (for the texture database).
  57. /// @param[out] texture_data The pointer to be set to the generated texture data.
  58. /// @param[out] texture_dimensions The dimensions of the texture.
  59. /// @param[in] glyphs The glyphs required by the font face handle.
  60. /// @param[in] texture_id The index of the texture within the layer to generate.
  61. virtual bool GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id);
  62. /// Generates the geometry required to render a single character.
  63. /// @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.
  64. /// @param[in] character_code The character to generate geometry for.
  65. /// @param[in] position The position of the baseline.
  66. /// @param[in] colour The colour of the string.
  67. inline void GenerateGeometry(Geometry* geometry, const word character_code, const Vector2f& position, const Colourb& colour) const
  68. {
  69. if (character_code >= characters.size())
  70. return;
  71. const Character& character = characters[character_code];
  72. if (character.texture_index < 0)
  73. return;
  74. // Generate the geometry for the character.
  75. std::vector< Vertex >& character_vertices = geometry[character.texture_index].GetVertices();
  76. std::vector< int >& character_indices = geometry[character.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 + character.origin.x, position.y + character.origin.y).Round(),
  83. character.dimensions,
  84. colour,
  85. character.texcoords[0],
  86. character.texcoords[1],
  87. (int)character_vertices.size() - 4
  88. );
  89. }
  90. /// Returns the effect used to generate the layer.
  91. /// @return The layer's effect.
  92. const FontEffect* GetFontEffect() const;
  93. /// Returns on the layer's textures.
  94. /// @param[in] index The index of the desired texture.
  95. /// @return The requested texture.
  96. const Texture* GetTexture(int index);
  97. /// Returns the number of textures employed by this layer.
  98. /// @return The number of used textures.
  99. int GetNumTextures() const;
  100. /// Returns the layer's colour.
  101. /// @return The layer's colour.
  102. const Colourb& GetColour() const;
  103. // protected:
  104. struct Character
  105. {
  106. Character() : texture_index(-1) { }
  107. // The offset, in pixels, of the baseline from the start of this character's geometry.
  108. Vector2f origin;
  109. // The width and height, in pixels, of this character's geometry.
  110. Vector2f dimensions;
  111. // The texture coordinates for the character's geometry.
  112. Vector2f texcoords[2];
  113. // The texture this character renders from.
  114. int texture_index;
  115. };
  116. typedef std::vector< Character > CharacterList;
  117. typedef std::vector< Texture > TextureList;
  118. const FontFaceHandle* handle;
  119. FontEffect* effect;
  120. TextureLayout texture_layout;
  121. CharacterList characters;
  122. TextureList textures;
  123. Colourb colour;
  124. };
  125. }
  126. }
  127. #endif