Cafu Engine
FontTT.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_FONT_TRUETYPE_HPP_INCLUDED
8 #define CAFU_FONT_TRUETYPE_HPP_INCLUDED
9 
10 #include <string>
11 #include <map>
12 #include "Templates/Array.hpp"
13 
14 class MaterialT;
15 namespace MatSys { class RenderMaterialT; }
16 class TextParserT;
17 
18 
19 namespace cf
20 {
21  /// A class for rendering fonts that have been created with the Cafu MakeFont tool.
23  {
24  public:
25 
26  struct GlyphInfoT
27  {
28  GlyphInfoT(TextParserT& TP, const ArrayT<MatSys::RenderMaterialT*>& RenderMaterials);
29 
30  float BearingX; ///< The horizontal offset of the bitmap relative to the cursor position.
31  float BearingY; ///< The vertical offset of the bitmap relative to the cursor position (y-axis points up!).
32  float AdvanceX; ///< How much the cursor position should be advanced horizontally for rendering the next character.
33 
34  int Width; ///< The width in pixels of the bitmap of this glyph.
35  int Height; ///< The height in pixels of the bitmap of this glyph.
36 
37  MatSys::RenderMaterialT* RM; ///< The RenderMaterialT that represents the larger bitmap that this glyph is embedded in. Points into the FontInfoT::RenderMaterials array of its FontInfoT.
38  float s1; ///< The s1 tex-coord into the larger bitmap.
39  float t1; ///< The t1 tex-coord into the larger bitmap.
40  float s2; ///< The s2 tex-coord into the larger bitmap.
41  float t2; ///< The t2 tex-coord into the larger bitmap.
42  };
43 
44  class FontInfoT
45  {
46  public:
47 
48  /// The constructor.
49  /// @throws TextParserT::ParseErrorT if the cfont file for the desired pixel size could not be successfully opened and parsed.
50  FontInfoT(const std::string& FontName, int SizeInPixels_);
51 
52  /// The destructor.
53  ~FontInfoT();
54 
55  float SizeInPixels; ///< The size of this font in pixels, i.e. 12, 24 or 48.
56  float Ascender; ///< The highest coordinate above the baseline in this font face, in pixels.
57  float Descender; ///< The lowest coordinate above the baseline in this font face, in pixels.
58  float Height; ///< The height of this font face, in pixels. Usually larger than Ascender-Descender, as this is to be used as the default value for the line spacing ("line gap").
59  unsigned long CharToGlyphIndex[256]; ///< Maps each ASCII character to the index into GlyphInfos of its related GlyphInfoT.
60  ArrayT<GlyphInfoT*> GlyphInfos; ///< The GlyphInfos for this font.
61  ArrayT< std::map<int, float> > KerningTable; ///< The kerning table, expressed as a "half-sparse" matrix.
62  ArrayT<MaterialT*> Materials; ///< The materials with the larger bitmaps that contain the glyphs.
63  ArrayT<MatSys::RenderMaterialT*> RenderMaterials; ///< The render materials matching the Materials array.
64 
65 
66  private:
67 
68  FontInfoT(const FontInfoT&); ///< Use of the Copy Constructor is not allowed.
69  void operator = (const FontInfoT&); ///< Use of the Assignment Operator is not allowed.
70  };
71 
72  /// The constructor.
73  /// @throws TextParserT::ParseErrorT if one of the related cfont files could not be successfully opened and parsed.
74  TrueTypeFontT(const std::string& FontName_);
75 
76  /// The destructor.
78 
79  /// Returns the name of this font.
80  const std::string& GetName() const { return FontName; }
81 
82  /// Returns the width of string Text at scale Scale in pixels. Useful e.g. for aligning a string to the center or the right of a window.
83  /// This method does *not* take newline characters ('\n') into account, the caller should pass pre-parsed strings instead!
84  float GetWidth(const std::string& Text, float Scale) const;
85 
86  /// Returns how far the highest glyph of this font extends above the baseline ("____") at scale Scale in pixels.
87  float GetAscender(float Scale) const;
88 
89  /// Returns the default line-spacing of this font at scale Scale in pixels.
90  float GetLineSpacing(float Scale) const;
91 
92  /// Prints PrintString at (PosX, PosY) in color Color.
93  /// Note that this method does *not* setup any of the MatSys's model, view or projection matrices: it's up to the caller to do that!
94  void Print(float PosX, float PosY, float Scale, unsigned long Color, const char* PrintString, ...) const;
95 
96 
97  private:
98 
99  TrueTypeFontT(const TrueTypeFontT&); ///< Use of the Copy Constructor is not allowed.
100  void operator = (const TrueTypeFontT&); ///< Use of the Assignment Operator is not allowed.
101 
102  /// Returns the FontInfoT suitable for scale Scale (one of the members FontInfoSmall, FontInfoMedium and FontInfoLarge).
103  const FontInfoT& GetFontInfo(float Scale) const;
104 
105  std::string FontName;
106  FontInfoT* FontInfoSmall;
107  FontInfoT* FontInfoMedium;
108  FontInfoT* FontInfoLarge;
109  const float DEFAULT_FONT_SCALE; ///< This is set to 48.0f, so that a scale of 1.0 corresponds to 48.0 (virtual GUI-) pixels.
110  };
111 }
112 
113 #endif
float Ascender
The highest coordinate above the baseline in this font face, in pixels.
Definition: FontTT.hpp:56
This class represents a surface render material.
Definition: RenderMaterial.hpp:25
Definition: FontTT.hpp:44
float t1
The t1 tex-coord into the larger bitmap.
Definition: FontTT.hpp:39
float t2
The t2 tex-coord into the larger bitmap.
Definition: FontTT.hpp:41
~FontInfoT()
The destructor.
Definition: FontTT.cpp:131
float BearingX
The horizontal offset of the bitmap relative to the cursor position.
Definition: FontTT.hpp:30
FontInfoT(const std::string &FontName, int SizeInPixels_)
The constructor.
Definition: FontTT.cpp:50
int Width
The width in pixels of the bitmap of this glyph.
Definition: FontTT.hpp:34
ArrayT< MatSys::RenderMaterialT * > RenderMaterials
The render materials matching the Materials array.
Definition: FontTT.hpp:63
MatSys::RenderMaterialT * RM
The RenderMaterialT that represents the larger bitmap that this glyph is embedded in...
Definition: FontTT.hpp:37
float Descender
The lowest coordinate above the baseline in this font face, in pixels.
Definition: FontTT.hpp:57
float Height
The height of this font face, in pixels. Usually larger than Ascender-Descender, as this is to be use...
Definition: FontTT.hpp:58
~TrueTypeFontT()
The destructor.
Definition: FontTT.cpp:157
ArrayT< MaterialT * > Materials
The materials with the larger bitmaps that contain the glyphs.
Definition: FontTT.hpp:62
int Height
The height in pixels of the bitmap of this glyph.
Definition: FontTT.hpp:35
float SizeInPixels
The size of this font in pixels, i.e. 12, 24 or 48.
Definition: FontTT.hpp:55
This class represents a surface material ("A datastructural representation of a scripts material def...
Definition: Material.hpp:22
TrueTypeFontT(const std::string &FontName_)
The constructor.
Definition: FontTT.cpp:141
void Print(float PosX, float PosY, float Scale, unsigned long Color, const char *PrintString,...) const
Prints PrintString at (PosX, PosY) in color Color.
Definition: FontTT.cpp:213
const std::string & GetName() const
Returns the name of this font.
Definition: FontTT.hpp:80
float GetAscender(float Scale) const
Returns how far the highest glyph of this font extends above the baseline ("____") at scale Scale in ...
Definition: FontTT.cpp:197
unsigned long CharToGlyphIndex[256]
Maps each ASCII character to the index into GlyphInfos of its related GlyphInfoT. ...
Definition: FontTT.hpp:59
float BearingY
The vertical offset of the bitmap relative to the cursor position (y-axis points up!).
Definition: FontTT.hpp:31
ArrayT< std::map< int, float > > KerningTable
The kerning table, expressed as a "half-sparse" matrix.
Definition: FontTT.hpp:61
float AdvanceX
How much the cursor position should be advanced horizontally for rendering the next character...
Definition: FontTT.hpp:32
A class for rendering fonts that have been created with the Cafu MakeFont tool.
Definition: FontTT.hpp:22
float GetWidth(const std::string &Text, float Scale) const
Returns the width of string Text at scale Scale in pixels.
Definition: FontTT.cpp:165
float s2
The s2 tex-coord into the larger bitmap.
Definition: FontTT.hpp:40
ArrayT< GlyphInfoT * > GlyphInfos
The GlyphInfos for this font.
Definition: FontTT.hpp:60
float s1
The s1 tex-coord into the larger bitmap.
Definition: FontTT.hpp:38
Definition: FontTT.hpp:26
float GetLineSpacing(float Scale) const
Returns the default line-spacing of this font at scale Scale in pixels.
Definition: FontTT.cpp:205
This is a class for parsing text.
Definition: TextParser.hpp:21