FontFaceLayer.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/RenderManager.h"
  30. #include "FontFaceHandleDefault.h"
  31. #include <string.h>
  32. #include <type_traits>
  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_owned.clear();
  50. textures_ptr = &textures_owned;
  51. }
  52. const FontGlyphMap& glyphs = handle->GetGlyphs();
  53. // Generate the new layout.
  54. if (clone)
  55. {
  56. // Clone the geometry and textures from the clone layer.
  57. character_boxes = clone->character_boxes;
  58. // Point our textures to the cloned layer's textures.
  59. textures_ptr = clone->textures_ptr;
  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. CallbackTextureFunction texture_callback = [handle, effect_ptr, texture_id, handle_version](
  137. const CallbackTextureInterface& texture_interface) -> bool {
  138. Vector2i dimensions;
  139. Vector<byte> data;
  140. if (!handle->GenerateLayerTexture(data, dimensions, effect_ptr, texture_id, handle_version) || data.empty())
  141. return false;
  142. if (!texture_interface.GenerateTexture(data, dimensions))
  143. return false;
  144. return true;
  145. };
  146. static_assert(std::is_nothrow_move_constructible<CallbackTextureSource>::value,
  147. "CallbackTextureSource must be nothrow move constructible so that it can be placed in the vector below.");
  148. textures_owned.emplace_back(std::move(texture_callback));
  149. }
  150. }
  151. return true;
  152. }
  153. bool FontFaceLayer::GenerateTexture(Vector<byte>& texture_data, Vector2i& texture_dimensions, int texture_id, const FontGlyphMap& glyphs)
  154. {
  155. if (texture_id < 0 || 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. Character character = (Character)rectangle.GetId();
  164. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  165. TextureBox& box = character_boxes[character];
  166. if (box.texture_index != texture_id)
  167. continue;
  168. auto it = glyphs.find((Character)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)
  176. {
  177. byte* destination = rectangle.GetTextureData();
  178. const byte* source = glyph.bitmap_data;
  179. const int num_bytes_per_line = glyph.bitmap_dimensions.x * (glyph.color_format == ColorFormat::RGBA8 ? 4 : 1);
  180. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  181. {
  182. switch (glyph.color_format)
  183. {
  184. case ColorFormat::A8:
  185. {
  186. // We use premultiplied alpha, so copy the alpha into all four channels.
  187. for (int k = 0; k < num_bytes_per_line; ++k)
  188. for (int c = 0; c < 4; ++c)
  189. destination[k * 4 + c] = source[k];
  190. }
  191. break;
  192. case ColorFormat::RGBA8:
  193. {
  194. memcpy(destination, source, num_bytes_per_line);
  195. }
  196. break;
  197. }
  198. destination += rectangle.GetTextureStride();
  199. source += num_bytes_per_line;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. effect->GenerateGlyphTexture(rectangle.GetTextureData(), Vector2i(box.dimensions), rectangle.GetTextureStride(), glyph);
  206. }
  207. }
  208. return true;
  209. }
  210. const FontEffect* FontFaceLayer::GetFontEffect() const
  211. {
  212. return effect.get();
  213. }
  214. Texture FontFaceLayer::GetTexture(RenderManager& render_manager, int index)
  215. {
  216. RMLUI_ASSERT(index >= 0);
  217. RMLUI_ASSERT(index < GetNumTextures());
  218. return (*textures_ptr)[index].GetTexture(render_manager);
  219. }
  220. int FontFaceLayer::GetNumTextures() const
  221. {
  222. return (int)textures_ptr->size();
  223. }
  224. ColourbPremultiplied FontFaceLayer::GetColour(float opacity) const
  225. {
  226. return colour.ToPremultiplied(opacity);
  227. }
  228. } // namespace Rml