]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Drop Id tags and copyright notices from files
[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/strings/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_default_size() const { return native_size; }  // Deprecated
67         float get_ascent() const { return ascent; }
68         float get_descent() const { return descent; }
69
70         float get_string_width(const std::string &, Codecs::Decoder &) const;
71
72         template<class C>
73         float get_string_width(const std::string &str) const
74         {
75                 typename C::Decoder dec;
76                 return get_string_width(str, dec);
77         }
78
79         float get_string_width(const std::string &str) const
80         { return get_string_width<Codecs::Utf8>(str); }
81
82         void draw_string(const std::string &, Codecs::Decoder &) const;
83         void draw_string(const std::string &, Codecs::Decoder &, PrimitiveBuilder &) const;
84
85         template<class C>
86         void draw_string(const std::string &str) const
87         {
88                 typename C::Decoder dec;
89                 draw_string(str, dec);
90         }
91
92         void draw_string(const std::string &str) const
93         { draw_string<Codecs::Utf8>(str); }
94
95         template<class C>
96         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
97         {
98                 typename C::Decoder dec;
99                 draw_string(str, dec, pb);
100         }
101
102         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
103         { return draw_string<Codecs::Utf8>(str, pb); }
104
105 private:
106         void create_glyph_vertices(const Glyph &, VertexBuilder &) const;
107         float get_glyph_advance(unsigned) const;
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif