FontFaceLayer.cpp 8.5 KB

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