FontFace.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2008-2020 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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Graphics/Texture2D.h"
  26. #include "../Resource/Image.h"
  27. #include "../UI/Font.h"
  28. #include "../UI/FontFace.h"
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. FontFace::FontFace(Font* font) :
  33. font_(font)
  34. {
  35. }
  36. FontFace::~FontFace()
  37. {
  38. if (font_)
  39. {
  40. // When a face is unloaded, deduct the used texture data size from the parent font
  41. unsigned totalTextureSize = 0;
  42. for (unsigned i = 0; i < textures_.Size(); ++i)
  43. totalTextureSize += textures_[i]->GetWidth() * textures_[i]->GetHeight();
  44. font_->SetMemoryUse(font_->GetMemoryUse() - totalTextureSize);
  45. }
  46. }
  47. const FontGlyph* FontFace::GetGlyph(unsigned c)
  48. {
  49. HashMap<unsigned, FontGlyph>::Iterator i = glyphMapping_.Find(c);
  50. if (i != glyphMapping_.End())
  51. {
  52. FontGlyph& glyph = i->second_;
  53. glyph.used_ = true;
  54. return &glyph;
  55. }
  56. else
  57. return nullptr;
  58. }
  59. float FontFace::GetKerning(unsigned c, unsigned d) const
  60. {
  61. if (kerningMapping_.Empty())
  62. return 0;
  63. if (c == '\n' || d == '\n')
  64. return 0;
  65. if (c > 0xffff || d > 0xffff)
  66. return 0;
  67. unsigned value = (c << 16u) + d;
  68. HashMap<unsigned, float>::ConstIterator i = kerningMapping_.Find(value);
  69. if (i != kerningMapping_.End())
  70. return i->second_;
  71. return 0;
  72. }
  73. bool FontFace::IsDataLost() const
  74. {
  75. for (unsigned i = 0; i < textures_.Size(); ++i)
  76. {
  77. if (textures_[i]->IsDataLost())
  78. return true;
  79. }
  80. return false;
  81. }
  82. SharedPtr<Texture2D> FontFace::CreateFaceTexture()
  83. {
  84. SharedPtr<Texture2D> texture(new Texture2D(font_->GetContext()));
  85. texture->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  86. texture->SetNumLevels(1); // No mipmaps
  87. texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
  88. texture->SetAddressMode(COORD_V, ADDRESS_BORDER);
  89. texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  90. return texture;
  91. }
  92. SharedPtr<Texture2D> FontFace::LoadFaceTexture(const SharedPtr<Image>& image)
  93. {
  94. SharedPtr<Texture2D> texture = CreateFaceTexture();
  95. if (!texture->SetData(image, true))
  96. {
  97. URHO3D_LOGERROR("Could not load texture from image resource");
  98. return SharedPtr<Texture2D>();
  99. }
  100. return texture;
  101. }
  102. }