Text3DFontFace.cpp 3.5 KB

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