]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Add ascent and descent to Font
[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 void Font::set_texture(const Texture2D &t)
20 {
21         tex=&t;
22 }
23
24 void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
25 {
26         Glyph glyph;
27         glyph.code=code;
28         glyph.x1=x1;
29         glyph.y1=y1;
30         glyph.x2=x2;
31         glyph.y2=y2;
32         glyph.w=w;
33         glyph.h=h;
34         glyph.off_x=ox;
35         glyph.off_y=oy;
36         glyph.advance=adv;
37         glyph.index=glyphs.size();
38         glyphs.insert(GlyphMap::value_type(code, glyph));
39
40         RefPtr<VertexArrayBuilder> va_builder=varray.modify();
41         create_glyph_vertices(glyph, *va_builder);
42 }
43
44 float Font::get_string_width(const string &str) const
45 {
46         float x=0;
47
48         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
49                 x+=get_glyph_advance(static_cast<unsigned char>(*i));
50
51         return x;
52 }
53
54 float Font::get_string_width(const wstring &str) const
55 {
56         float x=0;
57
58         for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i)
59                 x+=get_glyph_advance(*i);
60
61         return x;
62 }
63
64 void Font::draw_string(const string &str) const
65 {
66         prepare_render();
67
68         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
69                 draw_glyph(static_cast<unsigned char>(*i));
70
71         glPopMatrix();
72 }
73
74 void Font::draw_string(const wstring &str) const
75 {
76         prepare_render();
77
78         for(wstring::const_iterator i=str.begin(); i!=str.end(); ++i)
79                 draw_glyph(*i);
80
81         glPopMatrix();
82 }
83
84 Font::~Font()
85 {
86         if(own_tex)
87                 delete tex;
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(wchar_t 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(wchar_t 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