FontFace.cpp 4.0 KB

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