]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Make use of the ability to load multiple arguments diretly into member variables
[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                 };
72
73                 unsigned code;
74                 float x1,y1;
75                 float x2,y2;
76                 float w,h;
77                 float off_x, off_y;
78                 float advance;
79                 unsigned index;
80         };
81         typedef std::map<unsigned, Glyph> GlyphMap;
82
83         const Texture2D *tex;
84         bool     own_tex;
85         float    default_size;
86         float    ascent;
87         float    descent;
88         GlyphMap glyphs;
89         VertexArray varray;
90
91         void create_glyph_vertices();
92         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
93         void prepare_render() const;
94         void draw_glyph(unsigned) const;
95         float get_glyph_advance(unsigned) const;
96 };
97
98 } // namespace GL
99 } // namespace Msp
100
101 #endif