]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Make ~Program virtual
[libs/gl.git] / source / font.h
1 #ifndef FONT_H_
2 #define FONT_H_
3
4 #include <map>
5 #include <string>
6 #include <msp/datafile/loader.h>
7 #include <msp/strings/codec.h>
8 #include "vertexarray.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Texture2D;
14
15 class Font
16 {
17 public:
18         class Loader: public Msp::DataFile::Loader
19         {
20         public:
21                 Loader(Font &);
22                 ~Loader();
23                 Font &get_object() { return font; }
24         private:
25                 Font &font;
26
27                 void texture(const std::string &);
28                 void glyph(unsigned);
29         };
30
31         Font();
32         ~Font();
33
34         void  set_texture(const Texture2D &);
35         void  add_glyph(unsigned, float, float, float, float, float, float, float, float, float);
36         float get_default_size() const { return default_size; }
37         float get_ascent() const { return ascent; }
38         float get_descent() const { return descent; }
39         float get_string_width(const std::string &) const;
40         float get_string_width(const std::string &, Codecs::Decoder &) const;
41         void  draw_glyph(unsigned);
42         void  draw_string(const std::string &) const;
43         void  draw_string(const std::string &, Codecs::Decoder &) const;
44         void  draw_multiline(const std::string &) const;
45         
46         template<class C>
47         float get_string_width(const std::string &str) const
48         {
49                 typename C::Decoder dec;
50                 return get_string_width(str, dec);
51         }       
52         
53         template<class C>
54         void draw_string(const std::string &str) const
55         {
56                 typename C::Decoder dec;
57                 draw_string(str, dec);
58         }       
59 private:
60         struct Glyph
61         {
62                 class Loader: public Msp::DataFile::Loader
63                 {
64                 public:
65                         Loader(Glyph &);
66                         Glyph &get_object() { return glyph; }
67                 private:
68                         Glyph &glyph;
69
70                         void texcoords(float, float, float, float);
71                         void size(float, float);
72                         void offset(float, float);
73                 };
74
75                 unsigned code;
76                 float x1,y1;
77                 float x2,y2;
78                 float w,h;
79                 float off_x, off_y;
80                 float advance;
81                 unsigned index;
82         };
83         typedef std::map<unsigned, Glyph> GlyphMap;
84
85         const Texture2D *tex;
86         bool     own_tex;
87         float    default_size;
88         float    ascent;
89         float    descent;
90         GlyphMap glyphs;
91         VertexArray varray;
92
93         void create_glyph_vertices();
94         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
95         void prepare_render() const;
96         void draw_glyph(unsigned) const;
97         float get_glyph_advance(unsigned) const;
98 };
99
100 } // namespace GL
101 } // namespace Msp
102
103 #endif