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