2
0

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