]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Default to Utf8 instead of direct mapping in Font
[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, 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         glDrawArrays(GL_QUADS, i->second.index*4, 4);
109
110         glTranslatef(i->second.advance, 0, 0);
111 }
112
113 float Font::get_glyph_advance(unsigned code) const
114 {
115         GlyphMap::const_iterator i=glyphs.find(code);
116         if(i==glyphs.end())
117                 return 0;
118
119         return i->second.advance;
120 }
121
122
123 Font::Loader::Loader(Font &f):
124         font(f),
125         coll(0)
126 {
127         init();
128 }
129
130 Font::Loader::Loader(Font &f, Collection &c):
131         font(f),
132         coll(&c)
133 {
134         init();
135 }
136
137 DataFile::Collection &Font::Loader::get_collection()
138 {
139         if(!coll)
140                 throw InvalidState("No collection");
141         return *coll;
142 }
143
144 Font::Loader::~Loader()
145 {
146         font.create_glyph_vertices();
147 }
148
149 void Font::Loader::init()
150 {
151         add("default_size", &Font::default_size);
152         add("ascent",  &Font::ascent);
153         add("descent", &Font::descent);
154         add("texture", &Font::tex);
155         add("glyph",   &Loader::glyph);
156 }
157
158 void Font::Loader::glyph(unsigned c)
159 {
160         Glyph gl;
161         gl.code=c;
162         load_sub(gl);
163         font.glyphs.insert(GlyphMap::value_type(c, gl));
164 }
165
166
167 Font::Glyph::Loader::Loader(Glyph &g):
168         glyph(g)
169 {
170         add("texcoords", &Loader::texcoords);
171         add("size",      &Glyph::w,     &Glyph::h);
172         add("offset",    &Glyph::off_x, &Glyph::off_y);
173         add("advance",   &Glyph::advance);
174 }
175
176 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
177 {
178         glyph.x1=x1;
179         glyph.y1=y1;
180         glyph.x2=x2;
181         glyph.y2=y2;
182 }
183
184 } // namespace GL
185 } // namespace Msp