FontFace.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../IO/Log.h"
  6. #include "../GraphicsAPI/Texture2D.h"
  7. #include "../Resource/Image.h"
  8. #include "../UI/Font.h"
  9. #include "../UI/FontFace.h"
  10. #include "../DebugNew.h"
  11. namespace Urho3D
  12. {
  13. FontFace::FontFace(Font* font) :
  14. font_(font)
  15. {
  16. }
  17. FontFace::~FontFace()
  18. {
  19. if (font_)
  20. {
  21. // When a face is unloaded, deduct the used texture data size from the parent font
  22. unsigned totalTextureSize = 0;
  23. for (unsigned i = 0; i < textures_.Size(); ++i)
  24. totalTextureSize += textures_[i]->GetWidth() * textures_[i]->GetHeight();
  25. font_->SetMemoryUse(font_->GetMemoryUse() - totalTextureSize);
  26. }
  27. }
  28. const FontGlyph* FontFace::GetGlyph(c32 c)
  29. {
  30. HashMap<c32, FontGlyph>::Iterator i = glyphMapping_.Find(c);
  31. if (i != glyphMapping_.End())
  32. {
  33. FontGlyph& glyph = i->second_;
  34. glyph.used_ = true;
  35. return &glyph;
  36. }
  37. else
  38. return nullptr;
  39. }
  40. float FontFace::GetKerning(c32 c, c32 d) const
  41. {
  42. if (kerningMapping_.Empty())
  43. return 0;
  44. if (c == '\n' || d == '\n')
  45. return 0;
  46. if (c > 0xffff || d > 0xffff)
  47. return 0;
  48. u32 value = (c << 16u) + d;
  49. HashMap<u32, float>::ConstIterator i = kerningMapping_.Find(value);
  50. if (i != kerningMapping_.End())
  51. return i->second_;
  52. return 0;
  53. }
  54. bool FontFace::IsDataLost() const
  55. {
  56. for (unsigned i = 0; i < textures_.Size(); ++i)
  57. {
  58. if (textures_[i]->IsDataLost())
  59. return true;
  60. }
  61. return false;
  62. }
  63. SharedPtr<Texture2D> FontFace::CreateFaceTexture()
  64. {
  65. SharedPtr<Texture2D> texture(new Texture2D(font_->GetContext()));
  66. texture->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  67. texture->SetNumLevels(1); // No mipmaps
  68. texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
  69. texture->SetAddressMode(COORD_V, ADDRESS_BORDER);
  70. texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  71. return texture;
  72. }
  73. SharedPtr<Texture2D> FontFace::LoadFaceTexture(const SharedPtr<Image>& image)
  74. {
  75. SharedPtr<Texture2D> texture = CreateFaceTexture();
  76. if (!texture->SetData(image, true))
  77. {
  78. URHO3D_LOGERROR("Could not load texture from image resource");
  79. return SharedPtr<Texture2D>();
  80. }
  81. return texture;
  82. }
  83. }