FontFaceLayer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "FontFaceLayer.h"
  29. #include "../../Include/Rocket/Core/Core.h"
  30. #include "FontFaceHandle.h"
  31. namespace Rocket {
  32. namespace Core {
  33. FontFaceLayer::FontFaceLayer() : colour(255, 255, 255)
  34. {
  35. handle = NULL;
  36. effect = NULL;
  37. }
  38. FontFaceLayer::~FontFaceLayer()
  39. {
  40. if (effect != NULL)
  41. effect->RemoveReference();
  42. }
  43. // Generates the character and texture data for the layer.
  44. bool FontFaceLayer::Initialise(const FontFaceHandle* _handle, FontEffect* _effect, const FontFaceLayer* clone, bool deep_clone)
  45. {
  46. handle = _handle;
  47. effect = _effect;
  48. if (effect != NULL)
  49. {
  50. effect->AddReference();
  51. colour = effect->GetColour();
  52. }
  53. const FontGlyphList& glyphs = handle->GetGlyphs();
  54. // Clone the geometry and textures from the clone layer.
  55. if (clone != NULL)
  56. {
  57. // Copy the cloned layer's characters.
  58. characters = clone->characters;
  59. // Copy (and reference) the cloned layer's textures.
  60. for (size_t i = 0; i < clone->textures.size(); ++i)
  61. textures.push_back(clone->textures[i]);
  62. // Request the effect (if we have one) adjust the origins as appropriate.
  63. if (!deep_clone &&
  64. effect != NULL)
  65. {
  66. for (FontGlyphList::const_iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  67. {
  68. const FontGlyph& glyph = *i;
  69. if (glyph.character >= characters.size())
  70. continue;
  71. Character& character = characters[glyph.character];
  72. Vector2i glyph_origin(Math::RealToInteger(character.origin.x), Math::RealToInteger(character.origin.y));
  73. Vector2i glyph_dimensions(Math::RealToInteger(character.dimensions.x), Math::RealToInteger(character.dimensions.y));
  74. if (effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  75. {
  76. character.origin.x = (float) glyph_origin.x;
  77. character.origin.y = (float) glyph_origin.y;
  78. }
  79. else
  80. character.texture_index = -1;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. // Initialise the texture layout for the glyphs.
  87. characters.resize(glyphs.size(), Character());
  88. for (FontGlyphList::const_iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  89. {
  90. const FontGlyph& glyph = *i;
  91. Vector2i glyph_origin(0, 0);
  92. Vector2i glyph_dimensions = glyph.bitmap_dimensions;
  93. // Adjust glyph origin / dimensions for the font effect.
  94. if (effect != NULL)
  95. {
  96. if (!effect->GetGlyphMetrics(glyph_origin, glyph_dimensions, glyph))
  97. continue;
  98. }
  99. Character character;
  100. character.origin = Vector2f((float) (glyph_origin.x + glyph.bearing.x), (float) (glyph_origin.y - glyph.bearing.y));
  101. character.dimensions = Vector2f((float) glyph_dimensions.x - glyph_origin.x, (float) glyph_dimensions.y - glyph_origin.y);
  102. characters[glyph.character] = character;
  103. // Add the character's dimensions into the texture layout engine.
  104. texture_layout.AddRectangle(glyph.character, glyph_dimensions - glyph_origin);
  105. }
  106. // Generate the texture layout; this will position the glyph rectangles efficiently and
  107. // allocate the texture data ready for writing.
  108. if (!texture_layout.GenerateLayout(512))
  109. return false;
  110. // Iterate over each rectangle in the layout, copying the glyph data into the rectangle as
  111. // appropriate and generating geometry.
  112. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  113. {
  114. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  115. const TextureLayoutTexture& texture = texture_layout.GetTexture(rectangle.GetTextureIndex());
  116. Character& character = characters[(word) rectangle.GetId()];
  117. // Set the character's texture index.
  118. character.texture_index = rectangle.GetTextureIndex();
  119. // Generate the character's texture coordinates.
  120. character.texcoords[0].x = float(rectangle.GetPosition().x) / float(texture.GetDimensions().x);
  121. character.texcoords[0].y = float(rectangle.GetPosition().y) / float(texture.GetDimensions().y);
  122. character.texcoords[1].x = float(rectangle.GetPosition().x + rectangle.GetDimensions().x) / float(texture.GetDimensions().x);
  123. character.texcoords[1].y = float(rectangle.GetPosition().y + rectangle.GetDimensions().y) / float(texture.GetDimensions().y);
  124. }
  125. // Generate the textures.
  126. for (int i = 0; i < texture_layout.GetNumTextures(); ++i)
  127. {
  128. Texture texture;
  129. if (!texture.Load(String(64, "?font::%p/%p/%d", handle, effect, i)))
  130. return false;
  131. textures.push_back(texture);
  132. }
  133. }
  134. return true;
  135. }
  136. // Generates the texture data for a layer (for the texture database).
  137. bool FontFaceLayer::GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id)
  138. {
  139. if (texture_id < 0 ||
  140. texture_id > texture_layout.GetNumTextures())
  141. return false;
  142. const FontGlyphList& glyphs = handle->GetGlyphs();
  143. // Generate the texture data.
  144. texture_data = texture_layout.GetTexture(texture_id).AllocateTexture();
  145. texture_dimensions = texture_layout.GetTexture(texture_id).GetDimensions();
  146. for (int i = 0; i < texture_layout.GetNumRectangles(); ++i)
  147. {
  148. TextureLayoutRectangle& rectangle = texture_layout.GetRectangle(i);
  149. Character& character = characters[(word) rectangle.GetId()];
  150. if (character.texture_index != texture_id)
  151. continue;
  152. const FontGlyph& glyph = glyphs[rectangle.GetId()];
  153. if (effect == NULL)
  154. {
  155. // Copy the glyph's bitmap data into its allocated texture.
  156. if (glyph.bitmap_data != NULL)
  157. {
  158. byte* destination = rectangle.GetTextureData();
  159. byte* source = glyph.bitmap_data;
  160. for (int j = 0; j < glyph.bitmap_dimensions.y; ++j)
  161. {
  162. for (int k = 0; k < glyph.bitmap_dimensions.x; ++k)
  163. destination[k * 4 + 3] = source[k];
  164. destination += rectangle.GetTextureStride();
  165. source += glyph.bitmap_dimensions.x;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. effect->GenerateGlyphTexture(rectangle.GetTextureData(), Vector2i(Math::RealToInteger(character.dimensions.x), Math::RealToInteger(character.dimensions.y)), rectangle.GetTextureStride(), glyph);
  172. }
  173. }
  174. return true;
  175. }
  176. // Returns the effect used to generate the layer.
  177. const FontEffect* FontFaceLayer::GetFontEffect() const
  178. {
  179. return effect;
  180. }
  181. // Returns on the layer's textures.
  182. const Texture* FontFaceLayer::GetTexture(int index)
  183. {
  184. ROCKET_ASSERT(index >= 0);
  185. ROCKET_ASSERT(index < GetNumTextures());
  186. return &(textures[index]);
  187. }
  188. // Returns the number of textures employed by this layer.
  189. int FontFaceLayer::GetNumTextures() const
  190. {
  191. return (int) textures.size();
  192. }
  193. // Returns the layer's colour.
  194. const Colourb& FontFaceLayer::GetColour() const
  195. {
  196. return colour;
  197. }
  198. }
  199. }