]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Windows compatibility:
[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.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, Codecs::Decoder &dec) const
51 {
52         float x=0;
53
54         for(string::const_iterator i=str.begin(); i!=str.end();)
55                 x+=get_glyph_advance(dec.decode_char(str, i));
56
57         return x;
58 }
59
60 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
61 {
62         prepare_render();
63
64         for(string::const_iterator i=str.begin(); i!=str.end();)
65                 draw_glyph(dec.decode_char(str, i));
66
67         glPopMatrix();
68 }
69
70 void Font::create_glyph_vertices()
71 {
72         varray.clear();
73         RefPtr<VertexArrayBuilder> va_builder=varray.modify();
74
75         unsigned n=0;
76         for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n)
77         {
78                 i->second.index=n;
79                 create_glyph_vertices(i->second, *va_builder);
80         }
81 }
82
83 void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder)
84 {
85         va_builder.texcoord(glyph.x1, glyph.y1);
86         va_builder.vertex(glyph.off_x, glyph.off_y);
87         va_builder.texcoord(glyph.x2, glyph.y1);
88         va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y);
89         va_builder.texcoord(glyph.x2, glyph.y2);
90         va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
91         va_builder.texcoord(glyph.x1, glyph.y2);
92         va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h);
93 }
94
95 void Font::prepare_render() const
96 {
97         tex->bind();
98         varray.apply();
99         glPushMatrix();
100 }
101
102 void Font::draw_glyph(unsigned code) const
103 {
104         GlyphMap::const_iterator i=glyphs.find(code);
105         if(i==glyphs.end())
106                 return;
107
108         const Glyph &glyph=i->second;
109         (void)glyph;
110
111         glDrawArrays(GL_QUADS, i->second.index*4, 4);
112
113         glTranslatef(i->second.advance, 0, 0);
114 }
115
116 float Font::get_glyph_advance(unsigned code) const
117 {
118         GlyphMap::const_iterator i=glyphs.find(code);
119         if(i==glyphs.end())
120                 return 0;
121
122         return i->second.advance;
123 }
124
125
126 Font::Loader::Loader(Font &f):
127         font(f),
128         coll(0)
129 {
130         init();
131 }
132
133 Font::Loader::Loader(Font &f, Collection &c):
134         font(f),
135         coll(&c)
136 {
137         init();
138 }
139
140 DataFile::Collection &Font::Loader::get_collection()
141 {
142         if(!coll)
143                 throw InvalidState("No collection");
144         return *coll;
145 }
146
147 Font::Loader::~Loader()
148 {
149         font.create_glyph_vertices();
150 }
151
152 void Font::Loader::init()
153 {
154         add("default_size", &Font::default_size);
155         add("ascent",  &Font::ascent);
156         add("descent", &Font::descent);
157         add("texture", &Font::tex);
158         add("glyph",   &Loader::glyph);
159 }
160
161 void Font::Loader::glyph(unsigned c)
162 {
163         Glyph gl;
164         gl.code=c;
165         load_sub(gl);
166         font.glyphs.insert(GlyphMap::value_type(c, gl));
167 }
168
169
170 Font::Glyph::Loader::Loader(Glyph &g):
171         glyph(g)
172 {
173         add("texcoords", &Loader::texcoords);
174         add("size",      &Glyph::w,     &Glyph::h);
175         add("offset",    &Glyph::off_x, &Glyph::off_y);
176         add("advance",   &Glyph::advance);
177 }
178
179 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
180 {
181         glyph.x1=x1;
182         glyph.y1=y1;
183         glyph.x2=x2;
184         glyph.y2=y2;
185 }
186
187 } // namespace GL
188 } // namespace Msp