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