FontFace.cpp 3.4 KB

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