FontFaceLayer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "FontFaceHandle.h"
  30. namespace Rocket {
  31. namespace Core {
  32. namespace BitmapFont {
  33. FontFaceLayer::FontFaceLayer() : Rocket::Core::FontFaceLayer()
  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 Rocket::Core::FontFaceHandle* _handle, FontEffect* _effect, const Rocket::Core::FontFaceLayer* clone, bool deep_clone)
  45. {
  46. Rocket::Core::BitmapFont::FontFaceHandle
  47. * bm_font_face_handle;
  48. handle = _handle;
  49. //effect = _effect;
  50. bm_font_face_handle = ( Rocket::Core::BitmapFont::FontFaceHandle * ) handle;
  51. if (effect != NULL)
  52. {
  53. //effect->AddReference();
  54. //Log::Message( Log::LT_WARNING, "Effects are not supported" );
  55. }
  56. const FontGlyphList& glyphs = handle->GetGlyphs();
  57. // Clone the geometry and textures from the clone layer.
  58. if (clone != NULL)
  59. {
  60. // Copy the cloned layer's characters.
  61. characters = clone->characters;
  62. // Copy (and reference) the cloned layer's textures.
  63. for (size_t i = 0; i < clone->textures.size(); ++i)
  64. textures.push_back(clone->textures[i]);
  65. }
  66. else
  67. {
  68. // Load texture from file
  69. Texture texture;
  70. if (!texture.Load( bm_font_face_handle->GetTextureSource() ))
  71. return false;
  72. textures.push_back(texture);
  73. // Initialise the texture layout for the glyphs.
  74. characters.resize(glyphs.size(), Character());
  75. for (FontGlyphList::const_iterator i = glyphs.begin(); i != glyphs.end(); ++i)
  76. {
  77. const FontGlyph& glyph = *i;
  78. if(glyph.dimensions.x <= 0 || glyph.dimensions.y <= 0)
  79. continue;
  80. Vector2i glyph_origin( glyph.bitmap_dimensions.x, glyph.bitmap_dimensions.y ); // position in texture
  81. Vector2i glyph_dimensions = glyph.dimensions; // size of char
  82. Character character;
  83. character.origin = Vector2f((float) (glyph.bearing.x), (float) (glyph.bearing.y));
  84. character.dimensions = Vector2f((float) glyph.dimensions.x, (float) glyph.dimensions.y);
  85. // Set the character's texture index.
  86. character.texture_index = 0;
  87. // Generate the character's texture coordinates.
  88. character.texcoords[0].x = float(glyph_origin.x) / float(bm_font_face_handle->GetTextureWidth());
  89. character.texcoords[0].y = float(glyph_origin.y) / float(bm_font_face_handle->GetTextureHeight());
  90. character.texcoords[1].x = float(glyph_origin.x + character.dimensions.x) / float(bm_font_face_handle->GetTextureWidth());
  91. character.texcoords[1].y = float(glyph_origin.y + character.dimensions.y) / float(bm_font_face_handle->GetTextureHeight());
  92. characters[glyph.character] = character;
  93. // Add the character's dimensions into the texture layout engine.
  94. texture_layout.AddRectangle(glyph.character, glyph_dimensions);
  95. }
  96. // Generate the texture layout; this will position the glyph rectangles efficiently and
  97. // allocate the texture data ready for writing.
  98. if (!texture_layout.GenerateLayout(512))
  99. return false;
  100. }
  101. return true;
  102. }
  103. // Generates the texture data for a layer (for the texture database).
  104. bool FontFaceLayer::GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id)
  105. {
  106. return true;
  107. }
  108. }
  109. }
  110. }