FontFaceLayer.cpp 8.7 KB

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