]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Make ~Program virtual
[libs/gl.git] / source / font.cpp
1 #include <GL/gl.h>
2 #include "font.h"
3 #include "texture2d.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Font::Font():
11         tex(0),
12         own_tex(false),
13         default_size(1),
14         ascent(1),
15         descent(0),
16         varray((TEXCOORD2, VERTEX2))
17 { }
18
19 Font::~Font()
20 {
21         if(own_tex)
22                 delete tex;
23 }
24
25 void Font::set_texture(const Texture2D &t)
26 {
27         tex=&t;
28 }
29
30 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
31 {
32         Glyph glyph;
33         glyph.code=code;
34         glyph.x1=x1;
35         glyph.y1=y1;
36         glyph.x2=x2;
37         glyph.y2=y2;
38         glyph.w=w;
39         glyph.h=h;
40         glyph.off_x=ox;
41         glyph.off_y=oy;
42         glyph.advance=adv;
43         glyph.index=glyphs.size();
44         glyphs.insert(GlyphMap::value_type(code, glyph));
45
46         RefPtr<VertexArrayBuilder> va_builder=varray.modify();
47         create_glyph_vertices(glyph, *va_builder);
48 }
49
50 float Font::get_string_width(const string &str) const
51 {
52         float x=0;
53
54         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
55                 x+=get_glyph_advance(static_cast<unsigned char>(*i));
56
57         return x;
58 }
59
60 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
61 {
62         float x=0;
63
64         for(string::const_iterator i=str.begin(); i!=str.end();)
65                 x+=get_glyph_advance(dec.decode_char(str, i));
66
67         return x;
68 }
69
70 void Font::draw_string(const string &str) const
71 {
72         prepare_render();
73
74         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
75                 draw_glyph(static_cast<unsigned char>(*i));
76
77         glPopMatrix();
78 }
79
80 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
81 {
82         prepare_render();
83
84         for(string::const_iterator i=str.begin(); i!=str.end();)
85                 draw_glyph(dec.decode_char(str, i));
86
87         glPopMatrix();
88 }
89
90 void Font::create_glyph_vertices()
91 {
92         varray.clear();
93         RefPtr<VertexArrayBuilder> va_builder=varray.modify();
94
95         unsigned n=0;
96         for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n)
97         {
98                 i->second.index=n;
99                 create_glyph_vertices(i->second, *va_builder);
100         }
101 }
102
103 void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder)
104 {
105         va_builder.texcoord(glyph.x1, glyph.y1);
106         va_builder.vertex(glyph.off_x, glyph.off_y);
107         va_builder.texcoord(glyph.x2, glyph.y1);
108         va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y);
109         va_builder.texcoord(glyph.x2, glyph.y2);
110         va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
111         va_builder.texcoord(glyph.x1, glyph.y2);
112         va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h);
113 }
114
115 void Font::prepare_render() const
116 {
117         tex->bind();
118         varray.apply();
119         glPushMatrix();
120 }
121
122 void Font::draw_glyph(unsigned code) const
123 {
124         GlyphMap::const_iterator i=glyphs.find(code);
125         if(i==glyphs.end())
126                 return;
127
128         glDrawArrays(GL_QUADS, i->second.index*4, 4);
129
130         glTranslatef(i->second.advance, 0, 0);
131 }
132
133 float Font::get_glyph_advance(unsigned code) const
134 {
135         GlyphMap::const_iterator i=glyphs.find(code);
136         if(i==glyphs.end())
137                 return 0;
138
139         return i->second.advance;
140 }
141
142
143 Font::Loader::Loader(Font &f):
144         font(f)
145 {
146         add("default_size", &Font::default_size);
147         add("ascent",  &Font::ascent);
148         add("descent", &Font::descent);
149         add("texture", &Loader::texture);
150         add("glyph",   &Loader::glyph);
151 }
152
153 Font::Loader::~Loader()
154 {
155         font.create_glyph_vertices();
156 }
157
158 void Font::Loader::texture(const string &t)
159 {
160         Texture2D *tex=new Texture2D;
161         tex->parameter(GL_GENERATE_MIPMAP, 1);
162         font.tex=tex;
163         tex->image(t);
164         font.own_tex=true;
165 }
166
167 void Font::Loader::glyph(unsigned c)
168 {
169         Glyph gl;
170         gl.code=c;
171         load_sub(gl);
172         font.glyphs.insert(GlyphMap::value_type(c, gl));
173 }
174
175
176 Font::Glyph::Loader::Loader(Glyph &g):
177         glyph(g)
178 {
179         add("texcoords", &Loader::texcoords);
180         add("size",      &Loader::size);
181         add("offset",    &Loader::offset);
182         add("advance",   &Glyph::advance);
183 }
184
185 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
186 {
187         glyph.x1=x1;
188         glyph.y1=y1;
189         glyph.x2=x2;
190         glyph.y2=y2;
191 }
192
193 void Font::Glyph::Loader::size(float w, float h)
194 {
195         glyph.w=w;
196         glyph.h=h;
197 }
198
199 void Font::Glyph::Loader::offset(float x, float y)
200 {
201         glyph.off_x=x;
202         glyph.off_y=y;
203 }
204
205 } // namespace GL
206 } // namespace Msp