]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Allow retrieving data from Mesh
[libs/gl.git] / source / font.cpp
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 #include <GL/gl.h>
9 #include "font.h"
10 #include "texture2d.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Font::Font():
18         tex(0),
19         default_size(1),
20         ascent(1),
21         descent(0),
22         varray((TEXCOORD2, VERTEX2))
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         coll(0)
146 {
147         init();
148 }
149
150 Font::Loader::Loader(Font &f, Collection &c):
151         font(f),
152         coll(&c)
153 {
154         init();
155 }
156
157 DataFile::Collection &Font::Loader::get_collection()
158 {
159         if(!coll)
160                 throw InvalidState("No collection");
161         return *coll;
162 }
163
164 Font::Loader::~Loader()
165 {
166         font.create_glyph_vertices();
167 }
168
169 void Font::Loader::init()
170 {
171         add("default_size", &Font::default_size);
172         add("ascent",  &Font::ascent);
173         add("descent", &Font::descent);
174         add("texture", &Font::tex);
175         add("glyph",   &Loader::glyph);
176 }
177
178 void Font::Loader::glyph(unsigned c)
179 {
180         Glyph gl;
181         gl.code=c;
182         load_sub(gl);
183         font.glyphs.insert(GlyphMap::value_type(c, gl));
184 }
185
186
187 Font::Glyph::Loader::Loader(Glyph &g):
188         glyph(g)
189 {
190         add("texcoords", &Loader::texcoords);
191         add("size",      &Glyph::w,     &Glyph::h);
192         add("offset",    &Glyph::off_x, &Glyph::off_y);
193         add("advance",   &Glyph::advance);
194 }
195
196 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
197 {
198         glyph.x1=x1;
199         glyph.y1=y1;
200         glyph.x2=x2;
201         glyph.y2=y2;
202 }
203
204 } // namespace GL
205 } // namespace Msp