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