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