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