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