]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Add Id tags and copyright notices to 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/loader.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 Msp::DataFile::Loader
26         {
27         public:
28                 Loader(Font &);
29                 ~Loader();
30                 Font &get_object() { return font; }
31         private:
32                 Font &font;
33
34                 void texture(const std::string &);
35                 void glyph(unsigned);
36         };
37
38         Font();
39         ~Font();
40
41         void  set_texture(const Texture2D &);
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 &) const;
47         float get_string_width(const std::string &, Codecs::Decoder &) const;
48         void  draw_glyph(unsigned);
49         void  draw_string(const std::string &) const;
50         void  draw_string(const std::string &, Codecs::Decoder &) const;
51         void  draw_multiline(const std::string &) const;
52
53         template<class C>
54         float get_string_width(const std::string &str) const
55         {
56                 typename C::Decoder dec;
57                 return get_string_width(str, dec);
58         }
59
60         template<class C>
61         void draw_string(const std::string &str) const
62         {
63                 typename C::Decoder dec;
64                 draw_string(str, dec);
65         }
66 private:
67         struct Glyph
68         {
69                 class Loader: public Msp::DataFile::Loader
70                 {
71                 public:
72                         Loader(Glyph &);
73                         Glyph &get_object() { return glyph; }
74                 private:
75                         Glyph &glyph;
76
77                         void texcoords(float, float, float, float);
78                 };
79
80                 unsigned code;
81                 float x1,y1;
82                 float x2,y2;
83                 float w,h;
84                 float off_x, off_y;
85                 float advance;
86                 unsigned index;
87         };
88         typedef std::map<unsigned, Glyph> GlyphMap;
89
90         const Texture2D *tex;
91         bool     own_tex;
92         float    default_size;
93         float    ascent;
94         float    descent;
95         GlyphMap glyphs;
96         VertexArray varray;
97
98         void create_glyph_vertices();
99         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
100         void prepare_render() const;
101         void draw_glyph(unsigned) const;
102         float get_glyph_advance(unsigned) const;
103 };
104
105 } // namespace GL
106 } // namespace Msp
107
108 #endif