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