]> git.tdb.fi Git - libs/gl.git/blob - source/font.h
Default to Utf8 instead of direct mapping in Font
[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                 ~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 &, Codecs::Decoder &) const;
53         void  draw_glyph(unsigned);
54         void  draw_string(const std::string &, Codecs::Decoder &) const;
55         void  draw_multiline(const std::string &) const;
56
57         template<class C>
58         float get_string_width(const std::string &str) const
59         {
60                 typename C::Decoder dec;
61                 return get_string_width(str, dec);
62         }
63
64         float get_string_width(const std::string &str) const
65         { return get_string_width<Codecs::Utf8>(str); }
66
67         template<class C>
68         void draw_string(const std::string &str) const
69         {
70                 typename C::Decoder dec;
71                 draw_string(str, dec);
72         }
73
74         void draw_string(const std::string &str) const
75         { draw_string<Codecs::Utf8>(str); }
76
77 private:
78         struct Glyph
79         {
80                 class Loader: public Msp::DataFile::Loader
81                 {
82                 public:
83                         Loader(Glyph &);
84                         Glyph &get_object() { return glyph; }
85                 private:
86                         Glyph &glyph;
87
88                         void texcoords(float, float, float, float);
89                 };
90
91                 unsigned code;
92                 float x1,y1;
93                 float x2,y2;
94                 float w,h;
95                 float off_x, off_y;
96                 float advance;
97                 unsigned index;
98         };
99         typedef std::map<unsigned, Glyph> GlyphMap;
100
101         const Texture2D *tex;
102         float    default_size;
103         float    ascent;
104         float    descent;
105         GlyphMap glyphs;
106         VertexArray varray;
107
108         void create_glyph_vertices();
109         void create_glyph_vertices(const Glyph &, VertexArrayBuilder &);
110         void prepare_render() const;
111         void draw_glyph(unsigned) const;
112         float get_glyph_advance(unsigned) const;
113 };
114
115 } // namespace GL
116 } // namespace Msp
117
118 #endif