FontFaceLayer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 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. {}
  42. bool FontFaceLayer::Generate(const FontFaceHandleDefault* handle, 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.clear();
  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. // Copy the cloned layer's textures.
  59. for (size_t i = 0; i < clone->textures.size(); ++i)
  60. textures.push_back(clone->textures[i]);
  61. // Request the effect (if we have one) and adjust the origins as appropriate.
  62. if (effect && !clone_glyph_origins)
  63. {
  64. for (auto& pair : glyphs)
  65. {
  66. Character character = pair.first;
  67. const FontGlyph& glyph = pair.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(glyphs.size());
  89. for (auto& pair : glyphs)
  90. {
  91. Character character = pair.first;
  92. const FontGlyph& glyph = pair.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. character_boxes[character] = box;
  106. // Add the character's dimensions into the texture layout engine.
  107. texture_layout.AddRectangle((int)character, glyph_dimensions);
  108. }
  109. constexpr int max_texture_dimensions = 1024;
  110. // Generate the texture layout; this will position the glyph rectangles efficiently and
  111. // allocate the texture data ready for writing.
  112. if (!texture_layout.GenerateLayout(max_texture_dimensions))
  113. return false;
  114. // Iterate over each rectangle in the layout, copying the glyph data into the rectangle as
  115. // appropriate and generating geometry.
  116. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  117. {
  118. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  119. const TextureLayoutTexture& texture = texture_layout.GetTexture(rectangle.GetTextureIndex());
  120. Character character = (Character)rectangle.GetId();
  121. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  122. TextureBox& box = character_boxes[character];
  123. // Set the character's texture index.
  124. box.texture_index = rectangle.GetTextureIndex();
  125. // Generate the character's texture coordinates.
  126. box.texcoords[0].x = float(rectangle.GetPosition().x) / float(texture.GetDimensions().x);
  127. box.texcoords[0].y = float(rectangle.GetPosition().y) / float(texture.GetDimensions().y);
  128. box.texcoords[1].x = float(rectangle.GetPosition().x + rectangle.GetDimensions().x) / float(texture.GetDimensions().x);
  129. box.texcoords[1].y = float(rectangle.GetPosition().y + rectangle.GetDimensions().y) / float(texture.GetDimensions().y);
  130. }
  131. const FontEffect* effect_ptr = effect.get();
  132. const int handle_version = handle->GetVersion();
  133. // Generate the textures.
  134. for (int i = 0; i < texture_layout.GetNumTextures(); ++i)
  135. {
  136. const int texture_id = i;
  137. TextureCallback texture_callback = [handle, effect_ptr, texture_id, handle_version](RenderInterface* render_interface,
  138. const String& /*name*/, TextureHandle& out_texture_handle, Vector2i& out_dimensions) -> bool {
  139. UniquePtr<const byte[]> data;
  140. if (!handle->GenerateLayerTexture(data, out_dimensions, effect_ptr, texture_id, handle_version) || !data)
  141. return false;
  142. if (!render_interface->GenerateTexture(out_texture_handle, data.get(), out_dimensions))
  143. return false;
  144. return true;
  145. };
  146. Texture texture;
  147. texture.Set("font-face-layer", texture_callback);
  148. textures.push_back(texture);
  149. }
  150. }
  151. return true;
  152. }
  153. // Generates the texture data for a layer (for the texture database).
  154. bool FontFaceLayer::GenerateTexture(UniquePtr<const byte[]>& texture_data, Vector2i& texture_dimensions, int texture_id, const FontGlyphMap& glyphs)
  155. {
  156. if (texture_id < 0 ||
  157. texture_id > texture_layout.GetNumTextures())
  158. return false;
  159. // Generate the texture data.
  160. texture_data = texture_layout.GetTexture(texture_id).AllocateTexture();
  161. texture_dimensions = texture_layout.GetTexture(texture_id).GetDimensions();
  162. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  163. {
  164. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  165. Character character = (Character)rectangle.GetId();
  166. RMLUI_ASSERT(character_boxes.find(character) != character_boxes.end());
  167. TextureBox& box = character_boxes[character];
  168. if (box.texture_index != texture_id)
  169. continue;
  170. auto it = glyphs.find((Character)rectangle.GetId());
  171. if (it == glyphs.end())
  172. continue;
  173. const FontGlyph& glyph = it->second;
  174. if (effect == nullptr)
  175. {
  176. // Copy the glyph's bitmap data into its allocated texture.
  177. if (glyph.bitmap_data)
  178. {
  179. byte* destination = rectangle.GetTextureData();
  180. const byte* source = glyph.bitmap_data;
  181. const int num_bytes_per_line = glyph.bitmap_dimensions.x * (glyph.color_format == ColorFormat::RGBA8 ? 4 : 1);
  182. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  183. {
  184. switch (glyph.color_format)
  185. {
  186. case ColorFormat::A8:
  187. {
  188. for (int k = 0; k < num_bytes_per_line; ++k)
  189. destination[k * 4 + 3] = 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. // Returns the effect used to generate the layer.
  211. const FontEffect* FontFaceLayer::GetFontEffect() const
  212. {
  213. return effect.get();
  214. }
  215. // Returns on the layer's textures.
  216. const Texture* FontFaceLayer::GetTexture(int index)
  217. {
  218. RMLUI_ASSERT(index >= 0);
  219. RMLUI_ASSERT(index < GetNumTextures());
  220. return &(textures[index]);
  221. }
  222. // Returns the number of textures employed by this layer.
  223. int FontFaceLayer::GetNumTextures() const
  224. {
  225. return (int)textures.size();
  226. }
  227. // Returns the layer's colour.
  228. Colourb FontFaceLayer::GetColour() const
  229. {
  230. return colour;
  231. }
  232. } // namespace Rml