MyGUI  3.4.1
MyGUI_ResourceTrueTypeFont.h
Go to the documentation of this file.
1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #ifndef MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
8 #define MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
9 
10 #include "MyGUI_Prerequest.h"
11 #include "MyGUI_ITexture.h"
12 #include "MyGUI_IFont.h"
13 
14 #ifdef MYGUI_USE_FREETYPE
15 # include <ft2build.h>
16 # include FT_FREETYPE_H
17 
18 #ifdef MYGUI_MSDF_FONTS
19 namespace msdfgen
20 {
21  class FontHandle;
22  class Shape;
23 }
24 #endif
25 
26 #endif // MYGUI_USE_FREETYPE
27 
28 #include <unordered_map>
29 
30 namespace MyGUI
31 {
32 
34  public IFont,
36  {
38 
39  public:
41  ~ResourceTrueTypeFont() override;
42 
43  void deserialization(xml::ElementPtr _node, Version _version) override;
44 
45  // Returns the glyph info for the specified code point, or the glyph info for a substitute glyph if the code point does not
46  // exist in this font. Returns nullptr if there is a problem with the font.
47  const GlyphInfo* getGlyphInfo(Char _id) const override;
48 
49  ITexture* getTextureFont() const override;
50 
51  // получившаяся высота при генерации в пикселях
52  int getDefaultHeight() const override;
53 
54  // update texture after render device lost event
55  void textureInvalidate(ITexture* _texture) override;
56 
57  // Returns a collection of code-point ranges that are supported by this font. Each range is specified as [first, second];
58  // for example, a range containing a single code point will have the same value for both first and second.
59  std::vector<std::pair<Char, Char> > getCodePointRanges() const;
60 
61  // Returns the code point that is used as a substitute for code points that don't exist in the font. The default substitute
62  // code point is FontCodeType::NotDefined, but it can be customized in the font definition file.
63  Char getSubstituteCodePoint() const;
64 
65  // создаение ресурса по текущим значениям
66  void initialise();
67 
68  void setSource(const std::string& _value);
69  void setShader(const std::string& _value);
70  void setSize(float _value);
71  void setResolution(unsigned int _value);
72  void setHinting(const std::string& _value);
73  void setAntialias(bool _value);
74  void setTabWidth(float _value);
75  void setOffsetHeight(int _value);
76  void setSubstituteCode(int _value);
77  void setDistance(int _value);
78  void setMsdfMode(bool _value);
79  void setMsdfRange(int _value);
80 
81  void addCodePointRange(Char _first, Char _second);
82  void removeCodePointRange(Char _first, Char _second);
83 
84 #ifdef MYGUI_USE_FREETYPE
85  private:
86  enum Hinting
87  {
88  HintingUseNative,
89  HintingForceAuto,
90  HintingDisableAuto,
91  HintingDisableAll
92  };
93 
94  void addCodePoint(Char _codePoint);
95  void removeCodePoint(Char _codePoint);
96 
97  void clearCodePoints();
98 
99  // The following variables are set directly from values specified by the user.
100  std::string mSource; // Source (filename) of the font.
101  std::string mShader; // Optional shader, applied to the font.
102  float mSize; // Size of the font, in points (there are 72 points per inch).
103  unsigned int mResolution; // Resolution of the font, in pixels per inch.
104  Hinting mHinting; // What type of hinting to use when rendering the font.
105  bool mAntialias; // Whether or not to anti-alias the font by copying its alpha channel to its luminance channel.
106  float mSpaceWidth; // The width of a "Space" character, in pixels. If zero, the default width is used.
107  int mGlyphSpacing; // How far apart the glyphs are placed from each other in the font texture, in pixels.
108  float mTabWidth; // The width of the "Tab" special character, in pixels.
109  int mOffsetHeight; // How far up to nudge text rendered in this font, in pixels. May be negative to nudge text down.
110  Char mSubstituteCodePoint; // The code point to use as a substitute for code points that don't exist in the font.
111  bool mMsdfMode; // Signed distance field texture, designed to be used with shader (see https://github.com/Chlumsky/msdfgen)
112  int mMsdfRange; // Gragient area range in pixels for msdf mode (higher range is required for thick outlines)
113 
114  // The following variables are calculated automatically.
115  int mDefaultHeight; // The nominal height of the font in pixels.
116  GlyphInfo* mSubstituteGlyphInfo; // The glyph info to use as a substitute for code points that don't exist in the font.
117  MyGUI::ITexture* mTexture; // The texture that contains all of the rendered glyphs in the font.
118 
119  // The following constants used to be mutable, but they no longer need to be. Do not modify their values!
120  static const int mDefaultGlyphSpacing; // How far apart the glyphs are placed from each other in the font texture, in pixels.
121  static const float mDefaultTabWidth; // Default "Tab" width, used only when tab width is no specified.
122  static const float mSelectedWidth; // The width of the "Selected" and "SelectedBack" special characters, in pixels.
123  static const float mCursorWidth; // The width of the "Cursor" special character, in pixels.
124 
125  private:
126  // A map of code points to glyph indices.
127  typedef std::map<Char, FT_UInt> CharMap;
128 
129  // A map of glyph indices to glyph info objects.
130  typedef std::unordered_map<Char, GlyphInfo> GlyphMap;
131 
132  // A map of glyph heights to the set of paired glyph indices and glyph info objects that are of that height.
133  typedef std::map<int, std::map<FT_UInt, GlyphInfo*> > GlyphHeightMap;
134 
135  template<bool LAMode, bool Antialias>
136  void initialiseFreeType();
137 
138  // Loads the font face as specified by mSource, mSize, and mResolution. Automatically adjusts code-point ranges according
139  // to the capabilities of the font face.
140  // Returns a handle to the FreeType face object for the face, or nullptr if the face could not be loaded.
141  // Keeps the font file loaded in memory and stores its location in _fontBuffer. The caller is responsible for freeing this
142  // buffer when it is done using the face by calling delete[] on the buffer after calling FT_Done_Face() on the face itself.
143  FT_Face loadFace(const FT_Library& _ftLibrary, uint8*& _fontBuffer);
144 
145  // Wraps the current texture coordinates _texX and _texY to the beginning of the next line if the specified glyph width
146  // doesn't fit at the end of the current line. Automatically takes the glyph spacing into account.
147  void autoWrapGlyphPos(int _glyphWidth, int _texWidth, int _lineHeight, int& _texX, int& _texY) const;
148 
149  // Creates a GlyphInfo object using the specified information.
150  GlyphInfo createFaceGlyphInfo(Char _codePoint, int _fontAscent, FT_GlyphSlot _glyph) const;
151 
152  // Creates a glyph with the specified glyph index and assigns it to the specified code point.
153  // Automatically updates _glyphHeightMap, mCharMap, and mGlyphMap with data from the new glyph..
154  int createGlyph(FT_UInt _glyphIndex, const GlyphInfo& _glyphInfo, GlyphHeightMap& _glyphHeightMap);
155 
156  // Creates a glyph with the specified index from the specified font face and assigns it to the specified code point.
157  // Automatically updates _glyphHeightMap with data from the newly created glyph.
158  int createFaceGlyph(FT_UInt _glyphIndex, Char _codePoint, int _fontAscent, const FT_Face& _ftFace, FT_Int32 _ftLoadFlags, GlyphHeightMap& _glyphHeightMap);
159 
160  // Renders all of the glyphs in _glyphHeightMap into the specified texture buffer using data from the specified font face.
161  template<bool LAMode, bool Antialias>
162  void renderGlyphs(const GlyphHeightMap& _glyphHeightMap, const FT_Library& _ftLibrary, const FT_Face& _ftFace, FT_Int32 _ftLoadFlags, uint8* _texBuffer, int _texWidth, int _texHeight);
163 
164  // Renders the glyph described by the specified glyph info according to the specified parameters.
165  // Supports two types of rendering, depending on the value of UseBuffer: Texture block transfer and rectangular color fill.
166  // The _luminance0 value is used for even-numbered columns (from zero), while _luminance1 is used for odd-numbered ones.
167  template<bool LAMode, bool UseBuffer, bool Antialias>
168  void renderGlyph(GlyphInfo& _info, uint8 _luminance0, uint8 _luminance1, uint8 _alpha, int _lineHeight, uint8* _texBuffer, int _texWidth, int _texHeight, int& _texX, int& _texY, uint8* _glyphBuffer = nullptr);
169 
170  CharMap mCharMap; // A map of code points to glyph indices.
171  GlyphMap mGlyphMap; // A map of code points to glyph info objects.
172 
173 #ifdef MYGUI_MSDF_FONTS
174  GlyphInfo createMsdfFaceGlyphInfo(Char _codePoint, const msdfgen::Shape& _shape, double _advance, int _fontAscent);
175  int createMsdfGlyph(const GlyphInfo& _glyphInfo, GlyphHeightMap& _glyphHeightMap);
176  int createMsdfFaceGlyph(Char _codePoint, int _fontAscent, msdfgen::FontHandle* _fontHandle, GlyphHeightMap& _glyphHeightMap);
177 
178  void renderMsdfGlyphs(const GlyphHeightMap& _glyphHeightMap, msdfgen::FontHandle* _fontHandle, uint8* _texBuffer, int _texWidth, int _texHeight);
179 #endif
180 
181 #endif // MYGUI_USE_FREETYPE
182 
183  };
184 
185 } // namespace MyGUI
186 
187 #endif // MYGUI_RESOURCE_TRUE_TYPE_FONT_H_
#define MYGUI_EXPORT
#define MYGUI_RTTI_DERIVED(DerivedType)
Definition: MyGUI_RTTI.h:48
uint8_t uint8
Definition: MyGUI_Types.h:45
unsigned int Char
Definition: MyGUI_Types.h:49