]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Inherit Loaders from the ObjectLoader classes
[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/objectloader.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::CollectionObjectLoader<Font>
27         {
28         public:
29                 Loader(Font &);
30                 Loader(Font &, Collection &);
31         private:
32                 void init();
33                 void glyph(unsigned);
34                 void texture_inline();
35         };
36
37         Font();
38         ~Font();
39
40         void  set_texture(const Texture2D &);
41         const Texture2D &get_texture() const;
42         void  add_glyph(unsigned, float, float, float, float, float, float, float, float, float);
43         float get_default_size() const { return default_size; }
44         float get_ascent() const { return ascent; }
45         float get_descent() const { return descent; }
46         float get_string_width(const std::string &, Codecs::Decoder &) const;
47         void  draw_glyph(unsigned);
48         void  draw_string(const std::string &, Codecs::Decoder &) const;
49         void  draw_string(const std::string &, Codecs::Decoder &, PrimitiveBuilder &) const;
50
51         template<class C>
52         float get_string_width(const std::string &str) const
53         {
54                 typename C::Decoder dec;
55                 return get_string_width(str, dec);
56         }
57
58         float get_string_width(const std::string &str) const
59         { return get_string_width<Codecs::Utf8>(str); }
60
61         template<class C>
62         void draw_string(const std::string &str) const
63         {
64                 typename C::Decoder dec;
65                 draw_string(str, dec);
66         }
67
68         void draw_string(const std::string &str) const
69         { draw_string<Codecs::Utf8>(str); }
70
71         template<class C>
72         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
73         {
74                 typename C::Decoder dec;
75                 draw_string(str, dec, pb);
76         }
77
78         void draw_string(const std::string &str, PrimitiveBuilder &pb) const
79         { return draw_string<Codecs::Utf8>(str, pb); }
80
81 private:
82         struct Glyph
83         {
84                 class Loader: public Msp::DataFile::ObjectLoader<Glyph>
85                 {
86                 public:
87                         Loader(Glyph &);
88                 private:
89                         void texcoords(float, float, float, float);
90                 };
91
92                 unsigned code;
93                 float x1,y1;
94                 float x2,y2;
95                 float w,h;
96                 float off_x, off_y;
97                 float advance;
98         };
99         typedef std::map<unsigned, Glyph> GlyphMap;
100
101         const Texture2D *tex;
102         bool     own_tex;
103         float    default_size;
104         float    ascent;
105         float    descent;
106         GlyphMap glyphs;
107
108         void create_glyph_vertices(const Glyph &, VertexBuilder &, float) const;
109         float get_glyph_advance(unsigned) const;
110 };
111
112 } // namespace GL
113 } // namespace Msp
114
115 #endif