Font.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// \file
  23. #pragma once
  24. #include "../Container/ArrayPtr.h"
  25. #include "../Resource/Resource.h"
  26. namespace Urho3D
  27. {
  28. class FontFace;
  29. static const int FONT_TEXTURE_MIN_SIZE = 128;
  30. static const int FONT_DPI = 96;
  31. /// %Font file type.
  32. enum FontType
  33. {
  34. FONT_NONE = 0,
  35. FONT_FREETYPE,
  36. FONT_BITMAP,
  37. MAX_FONT_TYPES
  38. };
  39. /// %Font resource.
  40. class URHO3D_API Font : public Resource
  41. {
  42. URHO3D_OBJECT(Font, Resource);
  43. public:
  44. /// Construct.
  45. explicit Font(Context* context);
  46. /// Destruct.
  47. ~Font() override;
  48. /// Register object factory.
  49. static void RegisterObject(Context* context);
  50. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  51. bool BeginLoad(Deserializer& source) override;
  52. /// Save resource as a new bitmap font type in XML format. Return true if successful.
  53. bool SaveXML(Serializer& dest, int pointSize, bool usedGlyphs = false, const String& indentation = "\t");
  54. /// Set absolute (in pixels) position adjustment for glyphs.
  55. /// @property
  56. void SetAbsoluteGlyphOffset(const IntVector2& offset);
  57. /// Set point size scaled position adjustment for glyphs.
  58. /// @property
  59. void SetScaledGlyphOffset(const Vector2& offset);
  60. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  61. FontFace* GetFace(float pointSize);
  62. /// Return font type.
  63. /// @property
  64. FontType GetFontType() const { return fontType_; }
  65. /// Is signed distance field font.
  66. bool IsSDFFont() const { return sdfFont_; }
  67. /// Return absolute position adjustment for glyphs.
  68. /// @property
  69. const IntVector2& GetAbsoluteGlyphOffset() const { return absoluteOffset_; }
  70. /// Return point size scaled position adjustment for glyphs.
  71. /// @property
  72. const Vector2& GetScaledGlyphOffset() const { return scaledOffset_; }
  73. /// Return the total effective offset for a point size.
  74. IntVector2 GetTotalGlyphOffset(float pointSize) const;
  75. /// Release font faces and recreate them next time when requested. Called when font textures lost or global font properties change.
  76. void ReleaseFaces();
  77. private:
  78. /// Load font glyph offset parameters from an optional XML file. Called internally when loading TrueType fonts.
  79. void LoadParameters();
  80. /// Return font face using FreeType. Called internally. Return null on error.
  81. FontFace* GetFaceFreeType(float pointSize);
  82. /// Return bitmap font face. Called internally. Return null on error.
  83. FontFace* GetFaceBitmap(float pointSize);
  84. /// Created faces.
  85. HashMap<int, SharedPtr<FontFace> > faces_;
  86. /// Font data.
  87. SharedArrayPtr<unsigned char> fontData_;
  88. /// Size of font data.
  89. unsigned fontDataSize_;
  90. /// Absolute position adjustment for glyphs.
  91. IntVector2 absoluteOffset_;
  92. /// Point size scaled position adjustment for glyphs.
  93. Vector2 scaledOffset_;
  94. /// Font type.
  95. FontType fontType_;
  96. /// Signed distance field font flag.
  97. bool sdfFont_;
  98. };
  99. }