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