]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
3178a4c2717ed2f64d6d4e2c72bb413b84056a60
[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/codec.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                 ~Loader();
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         void  add_glyph(unsigned, float, float, float, float, float, float, float, float, float);
49         float get_default_size() const { return default_size; }
50         float get_ascent() const { return ascent; }
51         float get_descent() const { return descent; }
52         float get_string_width(const std::string &) const;
53         float get_string_width(const std::string &, Codecs::Decoder &) const;
54         void  draw_glyph(unsigned);
55         void  draw_string(const std::string &) const;
56         void  draw_string(const std::string &, Codecs::Decoder &) const;
57         void  draw_multiline(const std::string &) 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         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 private:
73         struct Glyph
74         {
75                 class Loader: public Msp::DataFile::Loader
76                 {
77                 public:
78                         Loader(Glyph &);
79                         Glyph &get_object() { return glyph; }
80                 private:
81                         Glyph &glyph;
82
83                         void texcoords(float, float, float, float);
84                 };
85
86                 unsigned code;
87                 float x1,y1;
88                 float x2,y2;
89                 float w,h;
90                 float off_x, off_y;
91                 float advance;
92                 unsigned index;
93         };
94         typedef std::map<unsigned, Glyph> GlyphMap;
95
96         const Texture2D *tex;
97         float    default_size;
98         float    ascent;
99         float    descent;
100         GlyphMap glyphs;
101         VertexArray varray;
102
103         void create_glyph_vertices();
104         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
105         void prepare_render() const;
106         void draw_glyph(unsigned) const;
107         float get_glyph_advance(unsigned) const;
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif