Font.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #pragma once
  23. #include "ArrayPtr.h"
  24. #include "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 FONT_TYPE
  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. OBJECT(Font);
  42. public:
  43. /// Construct.
  44. Font(Context* context);
  45. /// Destruct.
  46. virtual ~Font();
  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);
  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);
  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(int pointSize);
  59. /// Is signed distance field font.
  60. bool IsSDFFont() const { return sdfFont_; }
  61. /// Return absolute position adjustment for glyphs.
  62. const IntVector2& GetAbsoluteGlyphOffset() const { return absoluteOffset_; }
  63. /// Return point size scaled position adjustment for glyphs.
  64. const Vector2& GetScaledGlyphOffset() const { return scaledOffset_; }
  65. /// Return the total effective offset for a point size.
  66. IntVector2 GetTotalGlyphOffset(int pointSize) const;
  67. /// Release font faces and recreate them next time when requested. Called when font textures lost or global font properties change.
  68. void ReleaseFaces();
  69. private:
  70. /// Load font glyph offset parameters from an optional XML file. Called internally when loading TrueType fonts.
  71. void LoadParameters();
  72. /// Return font face using FreeType. Called internally. Return null on error.
  73. FontFace* GetFaceFreeType(int pointSize);
  74. /// Return bitmap font face. Called internally. Return null on error.
  75. FontFace* GetFaceBitmap(int pointSize);
  76. /// Created faces.
  77. HashMap<int, SharedPtr<FontFace> > faces_;
  78. /// Font data.
  79. SharedArrayPtr<unsigned char> fontData_;
  80. /// Size of font data.
  81. unsigned fontDataSize_;
  82. /// Absolute position adjustment for glyphs.
  83. IntVector2 absoluteOffset_;
  84. /// Point size scaled position adjustment for glyphs.
  85. Vector2 scaledOffset_;
  86. /// Font type.
  87. FONT_TYPE fontType_;
  88. /// Signed distance field font flag.
  89. bool sdfFont_;
  90. };
  91. }