FontFace.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Core/Context.h"
  23. #include "../../IO/Log.h"
  24. #include "../../Graphics/Texture2D.h"
  25. #include "../../Resource/Image.h"
  26. #include "Font.h"
  27. #include "FontFace.h"
  28. #include "../../DebugNew.h"
  29. namespace Atomic
  30. {
  31. FontGlyph::FontGlyph() :
  32. page_(M_MAX_UNSIGNED),
  33. used_(false)
  34. {
  35. }
  36. FontFace::FontFace(Font* font) :
  37. font_(font)
  38. {
  39. }
  40. FontFace::~FontFace()
  41. {
  42. if (font_)
  43. {
  44. // When a face is unloaded, deduct the used texture data size from the parent font
  45. unsigned totalTextureSize = 0;
  46. for (unsigned i = 0; i < textures_.Size(); ++i)
  47. totalTextureSize += textures_[i]->GetWidth() * textures_[i]->GetHeight();
  48. font_->SetMemoryUse(font_->GetMemoryUse() - totalTextureSize);
  49. }
  50. }
  51. const FontGlyph* FontFace::GetGlyph(unsigned c)
  52. {
  53. HashMap<unsigned, FontGlyph>::Iterator i = glyphMapping_.Find(c);
  54. if (i != glyphMapping_.End())
  55. {
  56. FontGlyph& glyph = i->second_;
  57. glyph.used_ = true;
  58. return &glyph;
  59. }
  60. else
  61. return 0;
  62. }
  63. short FontFace::GetKerning(unsigned c, unsigned d) const
  64. {
  65. if (kerningMapping_.Empty())
  66. return 0;
  67. if (c == '\n' || d == '\n')
  68. return 0;
  69. if (c > 0xffff || d > 0xffff)
  70. return 0;
  71. unsigned value = (c << 16) + d;
  72. HashMap<unsigned, short>::ConstIterator i = kerningMapping_.Find(value);
  73. if (i != kerningMapping_.End())
  74. return i->second_;
  75. return 0;
  76. }
  77. bool FontFace::IsDataLost() const
  78. {
  79. for (unsigned i = 0; i < textures_.Size(); ++i)
  80. {
  81. if (textures_[i]->IsDataLost())
  82. return true;
  83. }
  84. return false;
  85. }
  86. SharedPtr<Texture2D> FontFace::CreateFaceTexture()
  87. {
  88. SharedPtr<Texture2D> texture(new Texture2D(font_->GetContext()));
  89. texture->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  90. texture->SetNumLevels(1); // No mipmaps
  91. texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
  92. texture->SetAddressMode(COORD_V, ADDRESS_BORDER),
  93. texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  94. return texture;
  95. }
  96. SharedPtr<Texture2D> FontFace::LoadFaceTexture(SharedPtr<Image> image)
  97. {
  98. SharedPtr<Texture2D> texture = CreateFaceTexture();
  99. if (!texture->SetData(image, true))
  100. {
  101. LOGERROR("Could not load texture from image resource");
  102. return SharedPtr<Texture2D>();
  103. }
  104. return texture;
  105. }
  106. }