]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Add class MeshBuilder
[libs/gl.git] / source / font.h
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GL_FONT_H_
9 #define MSP_GL_FONT_H_
10
11 #include <map>
12 #include <string>
13 #include <msp/datafile/collection.h>
14 #include <msp/strings/utf8.h>
15 #include "vertexarray.h"
16
17 namespace Msp {
18 namespace GL {
19
20 class PrimitiveBuilder;
21 class Texture2D;
22
23 class Font
24 {
25 public:
26         class Loader: public DataFile::Loader
27         {
28         private:
29                 Font &font;
30                 DataFile::Collection *coll;
31
32         public:
33                 typedef DataFile::Collection Collection;
34
35                 Loader(Font &);
36                 Loader(Font &, DataFile::Collection &);
37                 Font &get_object() { return font; }
38                 DataFile::Collection &get_collection();
39         private:
40                 void init();
41                 void texture(const std::string &);
42                 void glyph(unsigned);
43         };
44
45         Font();
46
47         void  set_texture(const Texture2D &);
48         const Texture2D &get_texture() const;
49         void  add_glyph(unsigned, float, float, float, float, float, float, float, float, float);
50         float get_default_size() const { return default_size; }
51         float get_ascent() const { return ascent; }
52         float get_descent() const { return descent; }
53         float get_string_width(const std::string &, Codecs::Decoder &) const;
54         void  draw_glyph(unsigned);
55         void  draw_string(const std::string &, Codecs::Decoder &) const;
56         void  draw_string(const std::string &, Codecs::Decoder &, PrimitiveBuilder &) const;
57
58         template<class C>
59         float get_string_width(const std::string &str) const
60         {
61                 typename C::Decoder dec;
62                 return get_string_width(str, dec);
63         }
64
65         float get_string_width(const std::string &str) const
66         { return get_string_width<Codecs::Utf8>(str); }
67
68         template<class C>
69         void draw_string(const std::string &str) const
70         {
71                 typename C::Decoder dec;
72                 draw_string(str, dec);
73         }
74
75         void draw_string(const std::string &str) const
76         { draw_string<Codecs::Utf8>(str); }
77
78         template<class C>
79         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
80         {
81                 typename C::Decoder dec;
82                 draw_string(str, dec, pb);
83         }
84
85         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
86         { return draw_string<Codecs::Utf8>(str, pb); }
87
88 private:
89         struct Glyph
90         {
91                 class Loader: public Msp::DataFile::Loader
92                 {
93                 public:
94                         Loader(Glyph &);
95                         Glyph &get_object() { return glyph; }
96                 private:
97                         Glyph &glyph;
98
99                         void texcoords(float, float, float, float);
100                 };
101
102                 unsigned code;
103                 float x1,y1;
104                 float x2,y2;
105                 float w,h;
106                 float off_x, off_y;
107                 float advance;
108         };
109         typedef std::map<unsigned, Glyph> GlyphMap;
110
111         const Texture2D *tex;
112         float    default_size;
113         float    ascent;
114         float    descent;
115         GlyphMap glyphs;
116
117         void create_glyph_vertices(const Glyph &, VertexBuilder &, float) const;
118         float get_glyph_advance(unsigned) const;
119 };
120
121 } // namespace GL
122 } // namespace Msp
123
124 #endif