2
0

FontFaceLayer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "FontFaceLayer.h"
  2. #include "../../../Include/RmlUi/Core/RenderManager.h"
  3. #include "FontFaceHandleDefault.h"
  4. #include <string.h>
  5. #include <type_traits>
  6. namespace Rml {
  7. FontFaceLayer::FontFaceLayer(const SharedPtr<const FontEffect>& _effect) : colour(255, 255, 255)
  8. {
  9. effect = _effect;
  10. if (effect)
  11. colour = effect->GetColour();
  12. }
  13. FontFaceLayer::~FontFaceLayer() {}
  14. bool FontFaceLayer::Generate(const FontFaceHandleDefault* handle, const FontFaceLayer* clone, bool clone_glyph_origins)
  15. {
  16. // Clear the old layout if it exists.
  17. {
  18. // @performance: We could be much smarter about this, e.g. such as adding new glyphs to the existing texture layout and textures.
  19. // Right now we re-generate the whole thing, including textures.
  20. texture_layout = TextureLayout{};
  21. character_boxes.clear();
  22. textures_owned.clear();
  23. textures_ptr = &textures_owned;
  24. }
  25. const FontGlyphMap& glyphs = handle->GetGlyphs();
  26. // Generate the new layout.
  27. if (clone)
  28. {
  29. // Clone the geometry and textures from the clone layer.
  30. character_boxes = clone->character_boxes;
  31. // Point our textures to the cloned layer's textures.
  32. textures_ptr = clone->textures_ptr;
  33. // Request the effect (if we have one) and adjust the origins as appropriate.
  34. if (effect && !clone_glyph_origins)
  35. {
  36. for (auto& pair : glyphs)
  37. {
  38. Character character = pair.first;
  39. const FontGlyph& glyph = pair.second;
  40. auto it = character_boxes.find(character);
  41. if (it == character_boxes.end())
  42. {
  43. // This can happen if the layers have been dirtied in FontHandleDefault. We will
  44. // probably be regenerated soon, just skip the character for now.
  45. continue;
  46. }
  47. TextureBox& box = it->second;
  48. Vector2i glyph_origin = Vector2i(box.origin);
  49. Vector2i glyph_dimensions = Vector2i(box.dimensions);
  50. if (effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  51. box.origin = Vector2f(glyph_origin);
  52. else
  53. box.texture_index = -1;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. // Initialise the texture layout for the glyphs.
  60. character_boxes.reserve(glyphs.size());
  61. for (auto& pair : glyphs)
  62. {
  63. Character character = pair.first;
  64. const FontGlyph& glyph = pair.second;
  65. Vector2i glyph_origin(0, 0);
  66. Vector2i glyph_dimensions = glyph.bitmap_dimensions;
  67. // Adjust glyph origin / dimensions for the font effect.
  68. if (effect)
  69. {
  70. if (!effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  71. continue;
  72. }
  73. TextureBox box;
  74. box.origin = Vector2f(float(glyph_origin.x + glyph.bearing.x), float(glyph_origin.y - glyph.bearing.y));
  75. box.dimensions = Vector2f(glyph_dimensions);
  76. RMLUI_ASSERT(box.dimensions.x >= 0 && box.dimensions.y >= 0);
  77. character_boxes[character] = box;
  78. // Add the character's dimensions into the texture layout engine.
  79. texture_layout.AddRectangle((int)character, glyph_dimensions);
  80. }
  81. constexpr int max_texture_dimensions = 1024;
  82. // Generate the texture layout; this will position the glyph rectangles efficiently and
  83. // allocate the texture data ready for writing.
  84. if (!texture_layout.GenerateLayout(max_texture_dimensions))
  85. return false;
  86. // Iterate over each rectangle in the layout, copying the glyph data into the rectangle as
  87. // appropriate and generating geometry.
  88. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  89. {
  90. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  91. const TextureLayoutTexture& texture = texture_layout.GetTexture(rectangle.GetTextureIndex());
  92. Character character = (Character)rectangle.GetId();
  93. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  94. TextureBox& box = character_boxes[character];
  95. // Set the character's texture index.
  96. box.texture_index = rectangle.GetTextureIndex();
  97. // Generate the character's texture coordinates.
  98. box.texcoords[0].x = float(rectangle.GetPosition().x) / float(texture.GetDimensions().x);
  99. box.texcoords[0].y = float(rectangle.GetPosition().y) / float(texture.GetDimensions().y);
  100. box.texcoords[1].x = float(rectangle.GetPosition().x + rectangle.GetDimensions().x) / float(texture.GetDimensions().x);
  101. box.texcoords[1].y = float(rectangle.GetPosition().y + rectangle.GetDimensions().y) / float(texture.GetDimensions().y);
  102. }
  103. const FontEffect* effect_ptr = effect.get();
  104. const int handle_version = handle->GetVersion();
  105. // Generate the textures.
  106. for (int i = 0; i < texture_layout.GetNumTextures(); ++i)
  107. {
  108. const int texture_id = i;
  109. CallbackTextureFunction texture_callback = [handle, effect_ptr, texture_id, handle_version](
  110. const CallbackTextureInterface& texture_interface) -> bool {
  111. Vector2i dimensions;
  112. Vector<byte> data;
  113. if (!handle->GenerateLayerTexture(data, dimensions, effect_ptr, texture_id, handle_version) || data.empty())
  114. return false;
  115. if (!texture_interface.GenerateTexture(data, dimensions))
  116. return false;
  117. return true;
  118. };
  119. static_assert(std::is_nothrow_move_constructible<CallbackTextureSource>::value,
  120. "CallbackTextureSource must be nothrow move constructible so that it can be placed in the vector below.");
  121. textures_owned.emplace_back(std::move(texture_callback));
  122. }
  123. }
  124. return true;
  125. }
  126. bool FontFaceLayer::GenerateTexture(Vector<byte>& texture_data, Vector2i& texture_dimensions, int texture_id, const FontGlyphMap& glyphs)
  127. {
  128. if (texture_id < 0 || texture_id > texture_layout.GetNumTextures())
  129. return false;
  130. // Generate the texture data.
  131. texture_data = texture_layout.GetTexture(texture_id).AllocateTexture();
  132. texture_dimensions = texture_layout.GetTexture(texture_id).GetDimensions();
  133. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  134. {
  135. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  136. Character character = (Character)rectangle.GetId();
  137. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  138. TextureBox& box = character_boxes[character];
  139. if (box.texture_index != texture_id)
  140. continue;
  141. auto it = glyphs.find((Character)rectangle.GetId());
  142. if (it == glyphs.end())
  143. continue;
  144. const FontGlyph& glyph = it->second;
  145. if (effect == nullptr)
  146. {
  147. // Copy the glyph's bitmap data into its allocated texture.
  148. if (glyph.bitmap_data)
  149. {
  150. byte* destination = rectangle.GetTextureData();
  151. const byte* source = glyph.bitmap_data;
  152. const int num_bytes_per_line = glyph.bitmap_dimensions.x * (glyph.color_format == ColorFormat::RGBA8 ? 4 : 1);
  153. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  154. {
  155. switch (glyph.color_format)
  156. {
  157. case ColorFormat::A8:
  158. {
  159. // We use premultiplied alpha, so copy the alpha into all four channels.
  160. for (int k = 0; k < num_bytes_per_line; ++k)
  161. for (int c = 0; c < 4; ++c)
  162. destination[k * 4 + c] = source[k];
  163. }
  164. break;
  165. case ColorFormat::RGBA8:
  166. {
  167. memcpy(destination, source, num_bytes_per_line);
  168. }
  169. break;
  170. }
  171. destination += rectangle.GetTextureStride();
  172. source += num_bytes_per_line;
  173. }
  174. }
  175. }
  176. else
  177. {
  178. effect->GenerateGlyphTexture(rectangle.GetTextureData(), Vector2i(box.dimensions), rectangle.GetTextureStride(), glyph);
  179. }
  180. }
  181. return true;
  182. }
  183. const FontEffect* FontFaceLayer::GetFontEffect() const
  184. {
  185. return effect.get();
  186. }
  187. Texture FontFaceLayer::GetTexture(RenderManager& render_manager, int index)
  188. {
  189. RMLUI_ASSERT(index >= 0);
  190. RMLUI_ASSERT(index < GetNumTextures());
  191. return (*textures_ptr)[index].GetTexture(render_manager);
  192. }
  193. int FontFaceLayer::GetNumTextures() const
  194. {
  195. return (int)textures_ptr->size();
  196. }
  197. ColourbPremultiplied FontFaceLayer::GetColour(float opacity) const
  198. {
  199. return colour.ToPremultiplied(opacity);
  200. }
  201. } // namespace Rml