1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
7 #include "primitivetype.h"
23 // Avoid synthesizing ~RefPtr in files including font.h
27 void Font::set_texture(const Texture2D &t)
33 const Texture2D &Font::get_texture() const
36 throw logic_error("No texture");
40 void Font::add_glyph(const Glyph &g)
42 insert_unique(glyphs, g.code, g);
45 void Font::set_kerning(unsigned l, unsigned r, float d)
47 kerning[KerningKey(l, r)] = d;
50 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
55 for(string::const_iterator i=str.begin(); i!=str.end();)
57 unsigned c = dec.decode_char(str, i);
59 x += get_glyph_advance(prev, c);
62 x += get_glyph_advance(prev);
67 void Font::draw_string(const string &str, StringCodec::Decoder &dec, const Color &color) const
69 BindRestore bind_tex(get_texture());
70 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
72 build_string(str, dec, imm);
75 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
77 MatrixStack::Push push_mtx(bld.matrix());
80 for(string::const_iterator i=str.begin(); i!=str.end();)
82 unsigned c = dec.decode_char(str, i);
83 GlyphMap::const_iterator j = glyphs.find(c);
88 bld.matrix() *= Matrix::translation(get_glyph_advance(prev, c), 0, 0);
90 create_glyph_quad(j->second, bld);
95 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
97 bld.begin(TRIANGLE_STRIP);
98 bld.texcoord(glyph.x1, glyph.y2);
99 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
100 bld.texcoord(glyph.x1, glyph.y1);
101 bld.vertex(glyph.off_x, glyph.off_y);
102 bld.texcoord(glyph.x2, glyph.y2);
103 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
104 bld.texcoord(glyph.x2, glyph.y1);
105 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
109 float Font::get_glyph_advance(unsigned code, unsigned next) const
111 GlyphMap::const_iterator i = glyphs.find(code);
115 float advance = i->second.advance;
119 KerningMap::const_iterator j = kerning.find(KerningKey(code, next));
121 advance += j->second;
128 Font::Loader::Loader(Font &f):
129 DataFile::CollectionObjectLoader<Font>(f, 0)
134 Font::Loader::Loader(Font &f, Collection &c):
135 DataFile::CollectionObjectLoader<Font>(f, &c)
140 void Font::Loader::init()
142 add("native_size", &Font::native_size);
143 add("ascent", &Font::ascent);
144 add("cap_height", &Font::cap_height);
145 add("descent", &Font::descent);
146 add("texture", &Loader::texture);
147 add("texture", &Loader::texture_ref);
148 add("glyph", &Loader::glyph);
149 add("kerning", &Loader::kerning);
150 add("x_height", &Font::x_height);
153 void Font::Loader::glyph(unsigned c)
158 obj.glyphs.insert(GlyphMap::value_type(c, gl));
161 void Font::Loader::kerning(unsigned l, unsigned r, float d)
163 obj.kerning[KerningKey(l, r)] = d;
166 void Font::Loader::texture()
168 RefPtr<Texture2D> tex = new Texture2D;
173 void Font::Loader::texture_ref(const string &name)
175 obj.texture = &get_collection().get<Texture2D>(name);
180 Font::Glyph::Loader::Loader(Glyph &g):
181 DataFile::ObjectLoader<Glyph>(g)
183 add("texcoords", &Loader::texcoords);
184 add("size", &Glyph::w, &Glyph::h);
185 add("offset", &Glyph::off_x, &Glyph::off_y);
186 add("advance", &Glyph::advance);
189 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)