Font.h 3.0 KB

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