]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Remove deprecated Font features
[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 texture();
29                 void texture_ref(const std::string &);
30         };
31
32 private:
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         typedef std::map<unsigned, Glyph> GlyphMap;
52
53         RefPtr<const Texture2D> texture;
54         float native_size;
55         float ascent;
56         float descent;
57         GlyphMap glyphs;
58
59 public:
60         Font();
61
62         void set_texture(const Texture2D &);
63         const Texture2D &get_texture() const;
64         void add_glyph(unsigned, float, float, float, float, float, float, float, float, float);
65         float get_native_size() const { return native_size; }
66         float get_ascent() const { return ascent; }
67         float get_descent() const { return descent; }
68
69         float get_string_width(const std::string &, StringCodec::Decoder &) const;
70
71         template<class C>
72         float get_string_width(const std::string &str) const
73         {
74                 typename C::Decoder dec;
75                 return get_string_width(str, dec);
76         }
77
78         float get_string_width(const std::string &str) const
79         { return get_string_width<StringCodec::Utf8>(str); }
80
81         /// Draws a string to the framebuffer with Immediate.
82         void draw_string(const std::string &, StringCodec::Decoder &, const Color & = Color()) const;
83
84         template<class C>
85         void draw_string(const std::string &str, const Color &color = Color()) const
86         {
87                 typename C::Decoder dec;
88                 draw_string(str, dec, color);
89         }
90
91         void draw_string(const std::string &str, const Color &color = Color()) const
92         { draw_string<StringCodec::Utf8>(str, color); }
93
94         /** Builds the primitives for a string.  The PrimitiveBuilder should be
95         associated with a target that has at least VERTEX2 and TEXCOORD2 components.
96         The texture is not bound, to avoid unnecessary bindings when creating
97         meshes. */
98         void build_string(const std::string &, StringCodec::Decoder &, PrimitiveBuilder &) const;
99
100         template<class C>
101         void build_string(const std::string &str, PrimitiveBuilder &pb) const
102         {
103                 typename C::Decoder dec;
104                 build_string(str, dec, pb);
105         }
106
107         void build_string(const std::string &str, PrimitiveBuilder &pb) const
108         { return build_string<StringCodec::Utf8>(str, pb); }
109
110 private:
111         void create_glyph_vertices(const Glyph &, VertexBuilder &) const;
112         float get_glyph_advance(unsigned) const;
113 };
114
115 } // namespace GL
116 } // namespace Msp
117
118 #endif