]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Add ascent and descent to Font
[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_ascent() const { return ascent; }
35         float get_descent() const { return descent; }
36         float get_string_width(const std::string &) const;
37         float get_string_width(const std::wstring &) const;
38         void  draw_glyph(wchar_t);
39         void  draw_string(const std::string &) const;
40         void  draw_string(const std::wstring &) const;
41         void  draw_multiline(const std::string &) const;
42         ~Font();
43 private:
44         struct Glyph
45         {
46                 class Loader: public Msp::Parser::Loader
47                 {
48                 public:
49                         Loader(Glyph &);
50                         Glyph &get_object() { return glyph; }
51                 private:
52                         Glyph &glyph;
53
54                         void texcoords(float, float, float, float);
55                         void size(float, float);
56                         void offset(float, float);
57                 };
58
59                 wchar_t code;
60                 float x1,y1;
61                 float x2,y2;
62                 float w,h;
63                 float off_x, off_y;
64                 float advance;
65                 unsigned index;
66         };
67         typedef std::map<wchar_t, Glyph> GlyphMap;
68
69         const Texture2D *tex;
70         bool     own_tex;
71         float    default_size;
72         float    ascent;
73         float    descent;
74         GlyphMap glyphs;
75         VertexArray varray;
76
77         void create_glyph_vertices();
78         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
79         void prepare_render() const;
80         void draw_glyph(wchar_t) const;
81         float get_glyph_advance(wchar_t) const;
82 };
83
84 } // namespace GL
85 } // namespace Msp
86
87 #endif