]> git.tdb.fi Git - libs/gl.git/blob - source/builders/font.h
Use default member initializers for simple types
[libs/gl.git] / source / builders / font.h
1 #ifndef MSP_GL_FONT_H_
2 #define MSP_GL_FONT_H_
3
4 #include <map>
5 #include <string>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/stringcodec/utf8.h>
8
9 namespace Msp {
10 namespace GL {
11
12 class PrimitiveBuilder;
13 class Texture2D;
14
15 /**
16 Stores a set of glyphs and creates strings out of them.
17 */
18 class Font
19 {
20 public:
21         class Loader: public DataFile::CollectionObjectLoader<Font>
22         {
23         public:
24                 Loader(Font &, Collection &);
25
26         private:
27                 void glyph(unsigned);
28                 void kerning(unsigned, unsigned, float);
29                 void ligature(unsigned, unsigned, unsigned);
30                 void texture();
31                 void texture_ref(const std::string &);
32         };
33
34         struct Glyph
35         {
36                 class Loader: public Msp::DataFile::ObjectLoader<Glyph>
37                 {
38                 public:
39                         Loader(Glyph &);
40                 private:
41                         void texcoords(float, float, float, float);
42                 };
43
44                 unsigned code = 0;
45                 float x1 = 0.0f;
46                 float y1 = 0.0f;
47                 float x2 = 1.0f;
48                 float y2 = 1.0f;
49                 float w = 1.0f;
50                 float h = 1.0f;
51                 float off_x = 0.0f;
52                 float off_y = 0.0f;
53                 float advance = 1.0f;
54         };
55
56 private:
57         typedef std::map<unsigned, Glyph> GlyphMap;
58         typedef std::pair<unsigned, unsigned> CodePair;
59         typedef std::map<CodePair, float> KerningMap;
60         typedef std::map<CodePair, unsigned> LigatureMap;
61
62         const Texture2D *texture = 0;
63         float native_size = 1.0f;
64         float ascent = 1.0f;
65         float descent = 0.0f;
66         float cap_height = 1.0f;
67         float x_height = 0.5f;
68         GlyphMap glyphs;
69         KerningMap kerning;
70         LigatureMap ligatures;
71
72 public:
73         void set_texture(const Texture2D &);
74         const Texture2D &get_texture() const;
75
76         /** Adds a glyph to the font.  There must not be an existing glyph with the
77         same code. */
78         void add_glyph(const Glyph &);
79         void set_kerning(unsigned, unsigned, float);
80
81         /** Returns the size used to generate the font texture.  This serves as a
82         hint for obtaining the best quality when rendering strings. */
83         float get_native_size() const { return native_size; }
84
85         float get_ascent() const { return ascent; }
86         float get_descent() const { return descent; }
87         float get_cap_height() const { return cap_height; }
88         float get_x_height() const { return x_height; }
89
90         /** Returns the width of a string, in multiples of the font size.  Scale the
91         result according to the size used in rendering. */
92         float get_string_width(const std::string &, StringCodec::Decoder &) const;
93
94         template<class C>
95         float get_string_width(const std::string &str) const
96         {
97                 typename C::Decoder dec;
98                 return get_string_width(str, dec);
99         }
100
101         float get_string_width(const std::string &str) const
102         { return get_string_width<StringCodec::Utf8>(str); }
103
104         /** Builds the primitives for a string.  Two-dimensional vertex and texture
105         coordinates are generated.  Size 1.0 is used for building; set up the
106         builder's matrix before the call.  The texture is not bound, to avoid
107         unnecessary bindings when creating meshes. */
108         void build_string(const std::string &, StringCodec::Decoder &, PrimitiveBuilder &) const;
109
110         template<class C>
111         void build_string(const std::string &str, PrimitiveBuilder &pb) const
112         {
113                 typename C::Decoder dec;
114                 build_string(str, dec, pb);
115         }
116
117         void build_string(const std::string &str, PrimitiveBuilder &pb) const
118         { return build_string<StringCodec::Utf8>(str, pb); }
119
120 private:
121         void create_glyph_quad(const Glyph &, PrimitiveBuilder &) const;
122         float get_glyph_advance(unsigned, unsigned = 0) const;
123         unsigned get_ligature(unsigned, unsigned) const;
124 };
125
126 } // namespace GL
127 } // namespace Msp
128
129 #endif