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