Font.h 4.1 KB

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