FontFaceLayer.cpp 11 KB

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