FontFace.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "Font.h"
  25. #include "FontFace.h"
  26. #include "Log.h"
  27. #include "Texture2D.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. MutableGlyph::MutableGlyph() :
  32. glyphIndex_(M_MAX_UNSIGNED)
  33. {
  34. }
  35. FontGlyph::FontGlyph() :
  36. used_(false),
  37. page_(M_MAX_UNSIGNED)
  38. {
  39. }
  40. FontFace::FontFace(Font* font) :
  41. font_(font),
  42. hasKerning_(false)
  43. {
  44. }
  45. FontFace::~FontFace()
  46. {
  47. if (font_)
  48. {
  49. // When a face is unloaded, deduct the used texture data size from the parent font
  50. unsigned totalTextureSize = 0;
  51. for (unsigned i = 0; i < textures_.Size(); ++i)
  52. totalTextureSize += textures_[i]->GetWidth() * textures_[i]->GetHeight();
  53. font_->SetMemoryUse(font_->GetMemoryUse() - totalTextureSize);
  54. }
  55. }
  56. const FontGlyph* FontFace::GetGlyph(unsigned c)
  57. {
  58. HashMap<unsigned, unsigned>::ConstIterator i = glyphMapping_.Find(c);
  59. if (i != glyphMapping_.End())
  60. {
  61. FontGlyph& glyph = glyphs_[i->second_];
  62. glyph.used_ = true;
  63. return &glyph;
  64. }
  65. else
  66. return 0;
  67. }
  68. short FontFace::GetKerning(unsigned c, unsigned d) const
  69. {
  70. if (!hasKerning_)
  71. return 0;
  72. if (c == '\n' || d == '\n')
  73. return 0;
  74. unsigned leftIndex = 0;
  75. unsigned rightIndex = 0;
  76. HashMap<unsigned, unsigned>::ConstIterator leftIt = glyphMapping_.Find(c);
  77. if (leftIt != glyphMapping_.End())
  78. leftIndex = leftIt->second_;
  79. else
  80. return 0;
  81. HashMap<unsigned, unsigned>::ConstIterator rightIt = glyphMapping_.Find(d);
  82. if (rightIt != glyphMapping_.End())
  83. rightIndex = rightIt->second_;
  84. else
  85. return 0;
  86. HashMap<unsigned, unsigned>::ConstIterator kerningIt = glyphs_[leftIndex].kerning_.Find(rightIndex);
  87. if (kerningIt != glyphs_[leftIndex].kerning_.End())
  88. return kerningIt->second_;
  89. else
  90. return 0;
  91. }
  92. bool FontFace::IsDataLost() const
  93. {
  94. for (unsigned i = 0; i < textures_.Size(); ++i)
  95. {
  96. if (textures_[i]->IsDataLost())
  97. return true;
  98. }
  99. return false;
  100. }
  101. SharedPtr<Texture2D> FontFace::CreateFaceTexture()
  102. {
  103. SharedPtr<Texture2D> texture(new Texture2D(font_->GetContext()));
  104. texture->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  105. texture->SetNumLevels(1); // No mipmaps
  106. texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
  107. texture->SetAddressMode(COORD_V, ADDRESS_BORDER),
  108. texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  109. return texture;
  110. }
  111. SharedPtr<Texture2D> FontFace::LoadFaceTexture(SharedPtr<Image> image)
  112. {
  113. SharedPtr<Texture2D> texture = CreateFaceTexture();
  114. if (!texture->Load(image, true))
  115. {
  116. LOGERROR("Could not load texture from image resource");
  117. return SharedPtr<Texture2D>();
  118. }
  119. return texture;
  120. }
  121. }