Text3DFont.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Atomic
  26. {
  27. class Text3DFontFace;
  28. static const int TEXT3D_FONT_TEXTURE_MIN_SIZE = 128;
  29. static const int TEXT3D_DEFAULT_FONT_TEXTURE_MAX_SIZE = 2048;
  30. static const int TEXT3D_FONT_DPI = 96;
  31. /// %Font file type.
  32. enum Text3DFontType
  33. {
  34. FONT_NONE = 0,
  35. FONT_FREETYPE,
  36. FONT_BITMAP,
  37. MAX_FONT_TYPES
  38. };
  39. /// %Font resource.
  40. class ATOMIC_API Text3DFont : public Resource
  41. {
  42. ATOMIC_OBJECT(Text3DFont, Resource)
  43. public:
  44. /// Construct.
  45. Text3DFont(Context* context);
  46. /// Destruct.
  47. virtual ~Text3DFont();
  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. virtual bool BeginLoad(Deserializer& source);
  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. void SetAbsoluteGlyphOffset(const IntVector2& offset);
  56. /// Set point size scaled position adjustment for glyphs.
  57. void SetScaledGlyphOffset(const Vector2& offset);
  58. /// Return font face. Pack and render to a texture if not rendered yet. Return null on error.
  59. Text3DFontFace* GetFace(float pointSize);
  60. /// Return font type.
  61. Text3DFontType GetFontType() const { return fontType_; }
  62. /// Is signed distance field font.
  63. bool IsSDFFont() const { return sdfFont_; }
  64. /// Return absolute position adjustment for glyphs.
  65. const IntVector2& GetAbsoluteGlyphOffset() const { return absoluteOffset_; }
  66. /// Return point size scaled position adjustment for glyphs.
  67. const Vector2& GetScaledGlyphOffset() const { return scaledOffset_; }
  68. /// Return the total effective offset for a point size.
  69. IntVector2 GetTotalGlyphOffset(float pointSize) const;
  70. /// Release font faces and recreate them next time when requested. Called when font textures lost or global font properties change.
  71. void ReleaseFaces();
  72. private:
  73. /// Load font glyph offset parameters from an optional XML file. Called internally when loading TrueType fonts.
  74. void LoadParameters();
  75. /// Return font face using FreeType. Called internally. Return null on error.
  76. Text3DFontFace* GetFaceFreeType(float pointSize);
  77. /// Return bitmap font face. Called internally. Return null on error.
  78. Text3DFontFace* GetFaceBitmap(float pointSize);
  79. /// Created faces.
  80. HashMap<int, SharedPtr<Text3DFontFace> > faces_;
  81. /// Font data.
  82. SharedArrayPtr<unsigned char> fontData_;
  83. /// Size of font data.
  84. unsigned fontDataSize_;
  85. /// Absolute position adjustment for glyphs.
  86. IntVector2 absoluteOffset_;
  87. /// Point size scaled position adjustment for glyphs.
  88. Vector2 scaledOffset_;
  89. /// Font type.
  90. Text3DFontType fontType_;
  91. /// Signed distance field font flag.
  92. bool sdfFont_;
  93. };
  94. }