]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Add vertex arrays and buffers
[libs/gl.git] / source / font.h
1 #ifndef FONT_H_
2 #define FONT_H_
3
4 #include <map>
5 #include <string>
6 #include <msp/parser/loader.h>
7 #include "vertexarray.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Texture2D;
13
14 class Font
15 {
16 public:
17         class Loader: public Msp::Parser::Loader
18         {
19         public:
20                 Loader(Font &);
21                 ~Loader();
22                 Font &get_object() { return font; }
23         private:
24                 Font &font;
25
26                 void texture(const std::string &);
27                 void glyph(unsigned);
28         };
29
30         Font();
31         void  set_texture(const Texture2D &);
32         void  add_glyph(wchar_t, float, float, float, float, float, float, float, float, float);
33         float get_default_size() const { return default_size; }
34         float get_string_width(const std::string &) const;
35         float get_string_width(const std::wstring &) const;
36         void  draw_glyph(wchar_t);
37         void  draw_string(const std::string &) const;
38         void  draw_string(const std::wstring &) const;
39         void  draw_multiline(const std::string &) const;
40         ~Font();
41 private:
42         struct Glyph
43         {
44                 class Loader: public Msp::Parser::Loader
45                 {
46                 public:
47                         Loader(Glyph &);
48                         Glyph &get_object() { return glyph; }
49                 private:
50                         Glyph &glyph;
51
52                         void texcoords(float, float, float, float);
53                         void size(float, float);
54                         void offset(float, float);
55                 };
56
57                 wchar_t code;
58                 float x1,y1;
59                 float x2,y2;
60                 float w,h;
61                 float off_x, off_y;
62                 float advance;
63                 unsigned index;
64         };
65         typedef std::map<wchar_t, Glyph> GlyphMap;
66
67         const Texture2D *tex;
68         bool     own_tex;
69         float    default_size;
70         GlyphMap glyphs;
71         VertexArray varray;
72
73         void create_glyph_vertices();
74         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
75         void prepare_render() const;
76         void draw_glyph(wchar_t) const;
77         float get_glyph_advance(wchar_t) const;
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif