FontFaceLayer.cpp 4.4 KB

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