FontFaceLayer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <cstring>
  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 Vector<const FontGlyphMap::value_type*>& new_glyphs,
  42. const FontFaceLayer* clone, bool clone_glyph_origins)
  43. {
  44. // Clear the old layout if it exists.
  45. {
  46. // @performance: We could be much smarter about this, e.g. such as adding new glyphs to the existing texture layout and textures.
  47. // Right now we re-generate the whole thing, including textures.
  48. // texture_layout = TextureLayout{};
  49. // character_boxes.clear();
  50. textures_owned.clear();
  51. textures_ptr = &textures_owned;
  52. // sprite_set = {4, 1024, 0};
  53. }
  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. // Point our textures to the cloned layer's textures.
  60. textures_ptr = clone->textures_ptr;
  61. // Request the effect (if we have one) and adjust the origins as appropriate.
  62. if (effect && !clone_glyph_origins)
  63. {
  64. for (const auto entry : new_glyphs)
  65. {
  66. Character character = entry->first;
  67. const FontGlyph& glyph = entry->second;
  68. auto it = character_boxes.find(character);
  69. if (it == character_boxes.end())
  70. {
  71. // This can happen if the layers have been dirtied in FontHandleDefault. We will
  72. // probably be regenerated soon, just skip the character for now.
  73. continue;
  74. }
  75. TextureBox& box = it->second;
  76. Vector2i glyph_origin = Vector2i(box.origin);
  77. Vector2i glyph_dimensions = Vector2i(box.dimensions);
  78. if (effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  79. box.origin = Vector2f(glyph_origin);
  80. else
  81. box.texture_index = -1;
  82. }
  83. }
  84. }
  85. else
  86. {
  87. // Initialise the texture layout for the glyphs.
  88. character_boxes.reserve(character_boxes.size() + new_glyphs.size());
  89. for (const auto entry : new_glyphs)
  90. {
  91. Character character = entry->first;
  92. const FontGlyph& glyph = entry->second;
  93. Vector2i glyph_origin(0, 0);
  94. Vector2i glyph_dimensions = glyph.bitmap_dimensions;
  95. // Adjust glyph origin / dimensions for the font effect.
  96. if (effect)
  97. {
  98. if (!effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  99. continue;
  100. }
  101. TextureBox box;
  102. box.origin = Vector2f(float(glyph_origin.x + glyph.bearing.x), float(glyph_origin.y - glyph.bearing.y));
  103. box.dimensions = Vector2f(glyph_dimensions);
  104. RMLUI_ASSERT(box.dimensions.x >= 0 && box.dimensions.y >= 0);
  105. /*
  106. // Add the character's dimensions into the texture layout engine.
  107. texture_layout.AddRectangle((int)character, glyph_dimensions);
  108. */
  109. SpriteSet::Handle sprite_set_handle;
  110. if (effect == nullptr)
  111. {
  112. if (glyph.color_format == ColorFormat::RGBA8)
  113. {
  114. sprite_set_handle = sprite_set.Add(glyph_dimensions.x, glyph_dimensions.y, glyph.bitmap_data);
  115. }
  116. else
  117. {
  118. const int glyph_pixel_count = glyph_dimensions.x * glyph_dimensions.y;
  119. Vector<unsigned char> unpacked_bitmap(glyph_pixel_count * 4);
  120. for (int i = 0; i < glyph_pixel_count; ++i)
  121. for (int c = 0; c < 4; ++c)
  122. unpacked_bitmap[i * 4 + c] = glyph.bitmap_data[i];
  123. sprite_set_handle = sprite_set.Add(glyph_dimensions.x, glyph_dimensions.y, unpacked_bitmap.data());
  124. }
  125. }
  126. else
  127. {
  128. Vector<unsigned char> processed_bitmap(glyph_dimensions.x * glyph_dimensions.y * 4);
  129. effect->GenerateGlyphTexture(processed_bitmap.data(), glyph_dimensions, glyph_dimensions.x * 4, glyph);
  130. sprite_set_handle = sprite_set.Add(glyph_dimensions.x, glyph_dimensions.y, processed_bitmap.data());
  131. }
  132. sprite_set_handle_map.emplace(character, sprite_set_handle);
  133. const auto sprite_data = sprite_set.Get(sprite_set_handle);
  134. // Set the character's texture index.
  135. box.texture_index = sprite_data.texture_id;
  136. // Generate the character's texture coordinates.
  137. box.texcoords[0].x = static_cast<float>(sprite_data.x) / texture_size;
  138. box.texcoords[0].y = static_cast<float>(sprite_data.y) / texture_size;
  139. box.texcoords[1].x = static_cast<float>(sprite_data.x + sprite_data.width) / texture_size;
  140. box.texcoords[1].y = static_cast<float>(sprite_data.y + sprite_data.height) / texture_size;
  141. character_boxes[character] = box;
  142. }
  143. /*
  144. constexpr int max_texture_dimensions = 1024;
  145. // Generate the texture layout; this will position the glyph rectangles efficiently and
  146. // allocate the texture data ready for writing.
  147. if (!texture_layout.GenerateLayout(max_texture_dimensions))
  148. return false;
  149. // Iterate over each rectangle in the layout, copying the glyph data into the rectangle as
  150. // appropriate and generating geometry.
  151. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  152. {
  153. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  154. const TextureLayoutTexture& texture = texture_layout.GetTexture(rectangle.GetTextureIndex());
  155. Character character = (Character)rectangle.GetId();
  156. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  157. TextureBox& box = character_boxes[character];
  158. // Set the character's texture index.
  159. box.texture_index = rectangle.GetTextureIndex();
  160. // Generate the character's texture coordinates.
  161. box.texcoords[0].x = float(rectangle.GetPosition().x) / float(texture.GetDimensions().x);
  162. box.texcoords[0].y = float(rectangle.GetPosition().y) / float(texture.GetDimensions().y);
  163. box.texcoords[1].x = float(rectangle.GetPosition().x + rectangle.GetDimensions().x) / float(texture.GetDimensions().x);
  164. box.texcoords[1].y = float(rectangle.GetPosition().y + rectangle.GetDimensions().y) / float(texture.GetDimensions().y);
  165. }
  166. */
  167. sprite_set_textures = sprite_set.GetTextures();
  168. const FontEffect* effect_ptr = effect.get();
  169. const int handle_version = handle->GetVersion();
  170. // Generate the textures.
  171. const int texture_count = static_cast<int>(sprite_set_textures.size());
  172. for (int texture_id = 0; texture_id < texture_count; ++texture_id)
  173. {
  174. CallbackTextureFunction texture_callback = [handle, effect_ptr, texture_id, handle_version](
  175. const CallbackTextureInterface& texture_interface) -> bool {
  176. Vector2i dimensions;
  177. Span<const byte> data;
  178. if (!handle->GenerateLayerTexture(data, dimensions, effect_ptr, texture_id, handle_version) || data.empty())
  179. return false;
  180. if (!texture_interface.GenerateTexture(data, dimensions))
  181. return false;
  182. return true;
  183. };
  184. static_assert(std::is_nothrow_move_constructible<CallbackTextureSource>::value,
  185. "CallbackTextureSource must be nothrow move constructible so that it can be placed in the vector below.");
  186. textures_owned.emplace_back(std::move(texture_callback));
  187. }
  188. }
  189. return true;
  190. }
  191. bool FontFaceLayer::GenerateTexture(Span<const byte>& texture_data, Vector2i& texture_dimensions, int texture_id, const FontGlyphMap& /*glyphs*/)
  192. {
  193. if (texture_id < 0 || texture_id > static_cast<int>(sprite_set_textures.size()))
  194. return false;
  195. const unsigned char* const source = sprite_set_textures[texture_id];
  196. texture_data = {source, texture_size * texture_size * 4};
  197. texture_dimensions = {texture_size, texture_size};
  198. /*
  199. // Generate the texture data.
  200. texture_data = texture_layout.GetTexture(texture_id).AllocateTexture();
  201. texture_dimensions = texture_layout.GetTexture(texture_id).GetDimensions();
  202. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  203. {
  204. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  205. Character character = (Character)rectangle.GetId();
  206. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  207. TextureBox& box = character_boxes[character];
  208. if (box.texture_index != texture_id)
  209. continue;
  210. auto it = glyphs.find((Character)rectangle.GetId());
  211. if (it == glyphs.end())
  212. continue;
  213. const FontGlyph& glyph = it->second;
  214. if (effect == nullptr)
  215. {
  216. // Copy the glyph's bitmap data into its allocated texture.
  217. if (glyph.bitmap_data)
  218. {
  219. byte* destination = rectangle.GetTextureData();
  220. const byte* source = glyph.bitmap_data;
  221. const int num_bytes_per_line = glyph.bitmap_dimensions.x * (glyph.color_format == ColorFormat::RGBA8 ? 4 : 1);
  222. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  223. {
  224. switch (glyph.color_format)
  225. {
  226. case ColorFormat::A8:
  227. {
  228. // We use premultiplied alpha, so copy the alpha into all four channels.
  229. for (int k = 0; k < num_bytes_per_line; ++k)
  230. for (int c = 0; c < 4; ++c)
  231. destination[k * 4 + c] = source[k];
  232. }
  233. break;
  234. case ColorFormat::RGBA8:
  235. {
  236. memcpy(destination, source, num_bytes_per_line);
  237. }
  238. break;
  239. }
  240. destination += rectangle.GetTextureStride();
  241. source += num_bytes_per_line;
  242. }
  243. }
  244. }
  245. else
  246. {
  247. effect->GenerateGlyphTexture(rectangle.GetTextureData(), Vector2i(box.dimensions), rectangle.GetTextureStride(), glyph);
  248. }
  249. }
  250. */
  251. return true;
  252. }
  253. void FontFaceLayer::RemoveGlyphs(const Vector<Character>& characters)
  254. {
  255. for (const auto character : characters)
  256. {
  257. character_boxes.erase(character);
  258. sprite_set.Remove(sprite_set_handle_map[character]);
  259. sprite_set_handle_map.erase(character);
  260. }
  261. }
  262. const FontEffect* FontFaceLayer::GetFontEffect() const
  263. {
  264. return effect.get();
  265. }
  266. Texture FontFaceLayer::GetTexture(RenderManager& render_manager, int index)
  267. {
  268. RMLUI_ASSERT(index >= 0);
  269. RMLUI_ASSERT(index < GetNumTextures());
  270. return (*textures_ptr)[index].GetTexture(render_manager);
  271. }
  272. int FontFaceLayer::GetNumTextures() const
  273. {
  274. return (int)textures_ptr->size();
  275. }
  276. ColourbPremultiplied FontFaceLayer::GetColour(float opacity) const
  277. {
  278. return colour.ToPremultiplied(opacity);
  279. }
  280. } // namespace Rml