]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
a96fd8f884b329ae74c8b520b82d52d1944d7776
[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 <msp/datafile/collection.h>
9 #include "bindable.h"
10 #include "gl.h"
11 #include "font.h"
12 #include "immediate.h"
13 #include "primitivetype.h"
14 #include "texture2d.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 Font::Font():
22         native_size(1),
23         ascent(1),
24         descent(0)
25 { }
26
27 void Font::set_texture(const Texture2D &t)
28 {
29         texture = &t;
30         texture.keep();
31 }
32
33 const Texture2D &Font::get_texture() const
34 {
35         if(!texture)
36                 throw InvalidState("No texture");
37         return *texture;
38 }
39
40 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
41 {
42         Glyph glyph;
43         glyph.code = code;
44         glyph.x1 = x1;
45         glyph.y1 = y1;
46         glyph.x2 = x2;
47         glyph.y2 = y2;
48         glyph.w = w;
49         glyph.h = h;
50         glyph.off_x = ox;
51         glyph.off_y = oy;
52         glyph.advance = adv;
53         glyphs.insert(GlyphMap::value_type(code, glyph));
54 }
55
56 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
57 {
58         float x = 0;
59
60         for(string::const_iterator i=str.begin(); i!=str.end();)
61                 x += get_glyph_advance(dec.decode_char(str, i));
62
63         return x;
64 }
65
66 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
67 {
68         Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
69         draw_string(str, dec, imm);
70 }
71
72 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &bld) const
73 {
74         MatrixStack::Push push_mtx(bld.matrix());
75         Bind bind_tex(get_texture(), true);
76
77         bld.begin(QUADS);
78         for(string::const_iterator i=str.begin(); i!=str.end();)
79         {
80                 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
81                 if(j==glyphs.end())
82                         continue;
83
84                 create_glyph_vertices(j->second, bld);
85                 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
86         }
87         bld.end();
88 }
89
90 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
91 {
92         bld.texcoord(glyph.x1, glyph.y1);
93         bld.vertex(glyph.off_x, glyph.off_y);
94         bld.texcoord(glyph.x2, glyph.y1);
95         bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
96         bld.texcoord(glyph.x2, glyph.y2);
97         bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
98         bld.texcoord(glyph.x1, glyph.y2);
99         bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
100 }
101
102 float Font::get_glyph_advance(unsigned code) const
103 {
104         GlyphMap::const_iterator i = glyphs.find(code);
105         if(i==glyphs.end())
106                 return 0;
107
108         return i->second.advance;
109 }
110
111
112 Font::Loader::Loader(Font &f):
113         DataFile::CollectionObjectLoader<Font>(f, 0)
114 {
115         init();
116 }
117
118 Font::Loader::Loader(Font &f, Collection &c):
119         DataFile::CollectionObjectLoader<Font>(f, &c)
120 {
121         init();
122 }
123
124 void Font::Loader::init()
125 {
126         add("native_size", &Font::native_size);
127         add("ascent",      &Font::ascent);
128         add("descent",     &Font::descent);
129         add("texture",     &Loader::texture);
130         add("texture",     &Loader::texture_ref);
131         add("glyph",       &Loader::glyph);
132
133         // Deprecated aliases
134         add("default_size",   &Font::native_size);
135         add("texture_inline", &Loader::texture);
136 }
137
138 void Font::Loader::glyph(unsigned c)
139 {
140         Glyph gl;
141         gl.code = c;
142         load_sub(gl);
143         obj.glyphs.insert(GlyphMap::value_type(c, gl));
144 }
145
146 void Font::Loader::texture()
147 {
148         RefPtr<Texture2D> tex = new Texture2D;
149         load_sub(*tex);
150         obj.texture = tex;
151 }
152
153 void Font::Loader::texture_ref(const string &name)
154 {
155         obj.texture = get_collection().get<Texture2D>(name);
156         obj.texture.keep();
157 }
158
159
160 Font::Glyph::Loader::Loader(Glyph &g):
161         DataFile::ObjectLoader<Glyph>(g)
162 {
163         add("texcoords", &Loader::texcoords);
164         add("size",      &Glyph::w,     &Glyph::h);
165         add("offset",    &Glyph::off_x, &Glyph::off_y);
166         add("advance",   &Glyph::advance);
167 }
168
169 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
170 {
171         obj.x1 = x1;
172         obj.y1 = y1;
173         obj.x2 = x2;
174         obj.y2 = y2;
175 }
176
177 } // namespace GL
178 } // namespace Msp