FontFaceLayer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "precompiled.h"
  29. #include "FontFaceLayer.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "FontFaceHandleDefault.h"
  32. namespace Rml {
  33. namespace Core {
  34. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  35. FontFaceLayer::FontFaceLayer() : colour(255, 255, 255)
  36. {
  37. handle = nullptr;
  38. }
  39. FontFaceLayer::~FontFaceLayer()
  40. {
  41. }
  42. // Generates the character and texture data for the layer.
  43. bool FontFaceLayer::Initialise(const FontFaceHandleDefault* _handle, SharedPtr<const FontEffect> _effect, const FontFaceLayer* clone, bool deep_clone)
  44. {
  45. handle = _handle;
  46. effect = _effect;
  47. if (effect)
  48. colour = effect->GetColour();
  49. const FontGlyphList& glyphs = handle->GetGlyphs();
  50. // Clone the geometry and textures from the clone layer.
  51. if (clone != nullptr)
  52. {
  53. // Copy the cloned layer's characters.
  54. characters = clone->characters;
  55. // Copy (and reference) the cloned layer's textures.
  56. for (size_t i = 0; i < clone->textures.size(); ++i)
  57. textures.push_back(clone->textures[i]);
  58. // Request the effect (if we have one) adjust the origins as appropriate.
  59. if (!deep_clone &&
  60. effect != nullptr)
  61. {
  62. for (FontGlyphList::const_iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  63. {
  64. const FontGlyph& glyph = *i;
  65. if (glyph.character >= characters.size())
  66. continue;
  67. Character& character = characters[glyph.character];
  68. Vector2i glyph_origin(Math::RealToInteger(character.origin.x), Math::RealToInteger(character.origin.y));
  69. Vector2i glyph_dimensions(Math::RealToInteger(character.dimensions.x), Math::RealToInteger(character.dimensions.y));
  70. if (effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  71. {
  72. character.origin.x = (float) glyph_origin.x;
  73. character.origin.y = (float) glyph_origin.y;
  74. }
  75. else
  76. character.texture_index = -1;
  77. }
  78. }
  79. }
  80. else
  81. {
  82. // Initialise the texture layout for the glyphs.
  83. characters.resize(glyphs.size(), Character());
  84. for (FontGlyphList::const_iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  85. {
  86. const FontGlyph& glyph = *i;
  87. Vector2i glyph_origin(0, 0);
  88. Vector2i glyph_dimensions = glyph.bitmap_dimensions;
  89. // Adjust glyph origin / dimensions for the font effect.
  90. if (effect != nullptr)
  91. {
  92. if (!effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  93. continue;
  94. }
  95. Character character;
  96. character.origin = Vector2f((float) (glyph_origin.x + glyph.bearing.x), (float) (glyph_origin.y - glyph.bearing.y));
  97. character.dimensions = Vector2f((float) glyph_dimensions.x - glyph_origin.x, (float) glyph_dimensions.y - glyph_origin.y);
  98. characters[glyph.character] = character;
  99. // Add the character's dimensions into the texture layout engine.
  100. texture_layout.AddRectangle(glyph.character, glyph_dimensions - glyph_origin);
  101. }
  102. // Generate the texture layout; this will position the glyph rectangles efficiently and
  103. // allocate the texture data ready for writing.
  104. if (!texture_layout.GenerateLayout(512))
  105. return false;
  106. // Iterate over each rectangle in the layout, copying the glyph data into the rectangle as
  107. // appropriate and generating geometry.
  108. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  109. {
  110. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  111. const TextureLayoutTexture& texture = texture_layout.GetTexture(rectangle.GetTextureIndex());
  112. Character& character = characters[(word) rectangle.GetId()];
  113. // Set the character's texture index.
  114. character.texture_index = rectangle.GetTextureIndex();
  115. // Generate the character's texture coordinates.
  116. character.texcoords[0].x = float(rectangle.GetPosition().x) / float(texture.GetDimensions().x);
  117. character.texcoords[0].y = float(rectangle.GetPosition().y) / float(texture.GetDimensions().y);
  118. character.texcoords[1].x = float(rectangle.GetPosition().x + rectangle.GetDimensions().x) / float(texture.GetDimensions().x);
  119. character.texcoords[1].y = float(rectangle.GetPosition().y + rectangle.GetDimensions().y) / float(texture.GetDimensions().y);
  120. }
  121. // Generate the textures.
  122. for (int i = 0; i < texture_layout.GetNumTextures(); ++i)
  123. {
  124. Texture texture;
  125. if (!texture.Load(CreateString(64, "?font::%p/%p/%d", handle, effect.get(), i)))
  126. return false;
  127. textures.push_back(texture);
  128. }
  129. }
  130. return true;
  131. }
  132. // Generates the texture data for a layer (for the texture database).
  133. bool FontFaceLayer::GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id)
  134. {
  135. if (texture_id < 0 ||
  136. texture_id > texture_layout.GetNumTextures())
  137. return false;
  138. const FontGlyphList& glyphs = handle->GetGlyphs();
  139. // Generate the texture data.
  140. texture_data = texture_layout.GetTexture(texture_id).AllocateTexture();
  141. texture_dimensions = texture_layout.GetTexture(texture_id).GetDimensions();
  142. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  143. {
  144. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  145. Character& character = characters[(word) rectangle.GetId()];
  146. if (character.texture_index != texture_id)
  147. continue;
  148. const FontGlyph& glyph = glyphs[rectangle.GetId()];
  149. if (effect == nullptr)
  150. {
  151. // Copy the glyph's bitmap data into its allocated texture.
  152. if (glyph.bitmap_data != nullptr)
  153. {
  154. byte* destination = rectangle.GetTextureData();
  155. byte* source = glyph.bitmap_data;
  156. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  157. {
  158. for (int k = 0; k < glyph.bitmap_dimensions.x; ++k)
  159. destination[k * 4 + 3] = source[k];
  160. destination += rectangle.GetTextureStride();
  161. source += glyph.bitmap_dimensions.x;
  162. }
  163. }
  164. }
  165. else
  166. {
  167. effect->GenerateGlyphTexture(rectangle.GetTextureData(), Vector2i(Math::RealToInteger(character.dimensions.x), Math::RealToInteger(character.dimensions.y)), rectangle.GetTextureStride(), glyph);
  168. }
  169. }
  170. return true;
  171. }
  172. // Returns the effect used to generate the layer.
  173. const FontEffect* FontFaceLayer::GetFontEffect() const
  174. {
  175. return effect.get();
  176. }
  177. // Returns on the layer's textures.
  178. const Texture* FontFaceLayer::GetTexture(int index)
  179. {
  180. RMLUI_ASSERT(index >= 0);
  181. RMLUI_ASSERT(index < GetNumTextures());
  182. return &(textures[index]);
  183. }
  184. // Returns the number of textures employed by this layer.
  185. int FontFaceLayer::GetNumTextures() const
  186. {
  187. return (int) textures.size();
  188. }
  189. // Returns the layer's colour.
  190. const Colourb& FontFaceLayer::GetColour() const
  191. {
  192. return colour;
  193. }
  194. #endif
  195. }
  196. }