FontFace.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2020 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. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Container/List.h"
  25. #include "../Math/AreaAllocator.h"
  26. namespace Urho3D
  27. {
  28. class Font;
  29. class Image;
  30. class Texture2D;
  31. /// %Font glyph description.
  32. struct URHO3D_API FontGlyph
  33. {
  34. /// X position in texture.
  35. short x_{};
  36. /// Y position in texture.
  37. short y_{};
  38. /// Width in texture.
  39. short texWidth_{};
  40. /// Height in texture.
  41. short texHeight_{};
  42. /// Width on screen.
  43. float width_{};
  44. /// Height on screen.
  45. float height_{};
  46. /// Glyph X offset from origin.
  47. float offsetX_{};
  48. /// Glyph Y offset from origin.
  49. float offsetY_{};
  50. /// Horizontal advance.
  51. float advanceX_{};
  52. /// Texture page. M_MAX_UNSIGNED if not yet resident on any texture.
  53. unsigned page_{M_MAX_UNSIGNED};
  54. /// Used flag.
  55. bool used_{};
  56. };
  57. /// %Font face description.
  58. class URHO3D_API FontFace : public RefCounted
  59. {
  60. friend class Font;
  61. public:
  62. /// Construct.
  63. explicit FontFace(Font* font);
  64. /// Destruct.
  65. ~FontFace() override;
  66. /// Load font face.
  67. virtual bool Load(const unsigned char* fontData, unsigned fontDataSize, float pointSize) = 0;
  68. /// Return pointer to the glyph structure corresponding to a character. Return null if glyph not found.
  69. virtual const FontGlyph* GetGlyph(unsigned c);
  70. /// Return if font face uses mutable glyphs.
  71. virtual bool HasMutableGlyphs() const { return false; }
  72. /// Return the kerning for a character and the next character.
  73. float GetKerning(unsigned c, unsigned d) const;
  74. /// Return true when one of the texture has a data loss.
  75. bool IsDataLost() const;
  76. /// Return point size.
  77. float GetPointSize() const { return pointSize_; }
  78. /// Return row height.
  79. float GetRowHeight() const { return rowHeight_; }
  80. /// Return textures.
  81. const Vector<SharedPtr<Texture2D> >& GetTextures() const { return textures_; }
  82. protected:
  83. friend class FontFaceBitmap;
  84. /// Create a texture for font rendering.
  85. SharedPtr<Texture2D> CreateFaceTexture();
  86. /// Load font face texture from image resource.
  87. SharedPtr<Texture2D> LoadFaceTexture(const SharedPtr<Image>& image);
  88. /// Parent font.
  89. Font* font_{};
  90. /// Glyph mapping.
  91. HashMap<unsigned, FontGlyph> glyphMapping_;
  92. /// Kerning mapping.
  93. HashMap<unsigned, float> kerningMapping_;
  94. /// Glyph texture pages.
  95. Vector<SharedPtr<Texture2D> > textures_;
  96. /// Point size.
  97. float pointSize_{};
  98. /// Row height.
  99. float rowHeight_{};
  100. };
  101. }