]> git.tdb.fi Git - libs/gl.git/blob - source/builders/font.h
f46a287092f811a8a6c2b89a73b1aee0eaf97af4
[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;
45                 float x1, y1;
46                 float x2, y2;
47                 float w, h;
48                 float off_x, off_y;
49                 float advance;
50
51                 Glyph();
52         };
53
54 private:
55         typedef std::map<unsigned, Glyph> GlyphMap;
56         typedef std::pair<unsigned, unsigned> CodePair;
57         typedef std::map<CodePair, float> KerningMap;
58         typedef std::map<CodePair, unsigned> LigatureMap;
59
60         const Texture2D *texture;
61         float native_size;
62         float ascent;
63         float descent;
64         float cap_height;
65         float x_height;
66         GlyphMap glyphs;
67         KerningMap kerning;
68         LigatureMap ligatures;
69
70 public:
71         Font();
72         ~Font();
73
74         void set_texture(const Texture2D &);
75         const Texture2D &get_texture() const;
76
77         /** Adds a glyph to the font.  There must not be an existing glyph with the
78         same code. */
79         void add_glyph(const Glyph &);
80         void set_kerning(unsigned, unsigned, float);
81
82         /** Returns the size used to generate the font texture.  This serves as a
83         hint for obtaining the best quality when rendering strings. */
84         float get_native_size() const { return native_size; }
85
86         float get_ascent() const { return ascent; }
87         float get_descent() const { return descent; }
88         float get_cap_height() const { return cap_height; }
89         float get_x_height() const { return x_height; }
90
91         /** Returns the width of a string, in multiples of the font size.  Scale the
92         result according to the size used in rendering. */
93         float get_string_width(const std::string &, StringCodec::Decoder &) const;
94
95         template<class C>
96         float get_string_width(const std::string &str) const
97         {
98                 typename C::Decoder dec;
99                 return get_string_width(str, dec);
100         }
101
102         float get_string_width(const std::string &str) const
103         { return get_string_width<StringCodec::Utf8>(str); }
104
105         /** Builds the primitives for a string.  Two-dimensional vertex and texture
106         coordinates are generated.  Size 1.0 is used for building; set up the
107         builder's matrix before the call.  The texture is not bound, to avoid
108         unnecessary bindings when creating meshes. */
109         void build_string(const std::string &, StringCodec::Decoder &, PrimitiveBuilder &) const;
110
111         template<class C>
112         void build_string(const std::string &str, PrimitiveBuilder &pb) const
113         {
114                 typename C::Decoder dec;
115                 build_string(str, dec, pb);
116         }
117
118         void build_string(const std::string &str, PrimitiveBuilder &pb) const
119         { return build_string<StringCodec::Utf8>(str, pb); }
120
121 private:
122         void create_glyph_quad(const Glyph &, PrimitiveBuilder &) const;
123         float get_glyph_advance(unsigned, unsigned = 0) const;
124         unsigned get_ligature(unsigned, unsigned) const;
125 };
126
127 } // namespace GL
128 } // namespace Msp
129
130 #endif