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.
7 #ifndef CAFU_FONT_TRUETYPE_HPP_INCLUDED
8 #define CAFU_FONT_TRUETYPE_HPP_INCLUDED
12 #include "Templates/Array.hpp"
15 namespace MatSys
{ class RenderMaterialT
; }
21 /// A class for rendering fonts that have been created with the Cafu MakeFont tool.
28 GlyphInfoT(TextParserT
& TP
, const ArrayT
<MatSys::RenderMaterialT
*>& RenderMaterials
);
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.
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.
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.
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_
);
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.
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.
73 /// @throws TextParserT::ParseErrorT if one of the related cfont files could not be successfully opened and parsed.
74 TrueTypeFontT(const std::string
& FontName_
);
79 /// Returns the name of this font.
80 const std::string
& GetName() const { return FontName
; }
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;
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;
89 /// Returns the default line-spacing of this font at scale Scale in pixels.
90 float GetLineSpacing(float Scale
) const;
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;
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.
102 /// Returns the FontInfoT suitable for scale Scale (one of the members FontInfoSmall, FontInfoMedium and FontInfoLarge).
103 const FontInfoT
& GetFontInfo(float Scale
) const;
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.