]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Fix some incorrect whitespace
[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 class Font
17 {
18 public:
19         class Loader: public DataFile::CollectionObjectLoader<Font>
20         {
21         public:
22                 Loader(Font &);
23                 Loader(Font &, Collection &);
24
25         private:
26                 void init();
27                 void glyph(unsigned);
28                 void kerning(unsigned, unsigned, float);
29                 void texture();
30                 void texture_ref(const std::string &);
31         };
32
33         struct Glyph
34         {
35                 class Loader: public Msp::DataFile::ObjectLoader<Glyph>
36                 {
37                 public:
38                         Loader(Glyph &);
39                 private:
40                         void texcoords(float, float, float, float);
41                 };
42
43                 unsigned code;
44                 float x1, y1;
45                 float x2, y2;
46                 float w, h;
47                 float off_x, off_y;
48                 float advance;
49         };
50
51 private:
52         typedef std::map<unsigned, Glyph> GlyphMap;
53         typedef std::pair<unsigned, unsigned> KerningKey;
54         typedef std::map<KerningKey, float> KerningMap;
55
56         RefPtr<const Texture2D> texture;
57         float native_size;
58         float ascent;
59         float descent;
60         GlyphMap glyphs;
61         KerningMap kerning;
62
63 public:
64         Font();
65         ~Font();
66
67         void set_texture(const Texture2D &);
68         const Texture2D &get_texture() const;
69         void add_glyph(const Glyph &);
70         void set_kerning(unsigned, unsigned, float);
71         float get_native_size() const { return native_size; }
72         float get_ascent() const { return ascent; }
73         float get_descent() const { return descent; }
74
75         float get_string_width(const std::string &, StringCodec::Decoder &) const;
76
77         template<class C>
78         float get_string_width(const std::string &str) const
79         {
80                 typename C::Decoder dec;
81                 return get_string_width(str, dec);
82         }
83
84         float get_string_width(const std::string &str) const
85         { return get_string_width<StringCodec::Utf8>(str); }
86
87         /// Draws a string to the framebuffer with Immediate.
88         void draw_string(const std::string &, StringCodec::Decoder &, const Color & = Color()) const;
89
90         template<class C>
91         void draw_string(const std::string &str, const Color &color = Color()) const
92         {
93                 typename C::Decoder dec;
94                 draw_string(str, dec, color);
95         }
96
97         void draw_string(const std::string &str, const Color &color = Color()) const
98         { draw_string<StringCodec::Utf8>(str, color); }
99
100         /** Builds the primitives for a string.  The PrimitiveBuilder should be
101         associated with a target that has at least VERTEX2 and TEXCOORD2 components.
102         The texture is not bound, to avoid unnecessary bindings when creating
103         meshes. */
104         void build_string(const std::string &, StringCodec::Decoder &, PrimitiveBuilder &) const;
105
106         template<class C>
107         void build_string(const std::string &str, PrimitiveBuilder &pb) const
108         {
109                 typename C::Decoder dec;
110                 build_string(str, dec, pb);
111         }
112
113         void build_string(const std::string &str, PrimitiveBuilder &pb) const
114         { return build_string<StringCodec::Utf8>(str, pb); }
115
116 private:
117         void create_glyph_vertices(const Glyph &, VertexBuilder &) const;
118         float get_glyph_advance(unsigned, unsigned = 0) const;
119 };
120
121 } // namespace GL
122 } // namespace Msp
123
124 #endif