1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
6 #include "primitivebuilder.h"
22 // Avoid synthesizing ~RefPtr in files including font.h
26 void Font::set_texture(const Texture2D &t)
32 const Texture2D &Font::get_texture() const
35 throw logic_error("No texture");
39 void Font::add_glyph(const Glyph &g)
41 insert_unique(glyphs, g.code, g);
44 void Font::set_kerning(unsigned l, unsigned r, float d)
46 kerning[CodePair(l, r)] = d;
49 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
54 for(string::const_iterator i=str.begin(); i!=str.end();)
56 unsigned c = dec.decode_char(str, i);
58 x += get_glyph_advance(prev, c);
61 x += get_glyph_advance(prev);
66 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
68 VertexBuilder::PushMatrix push_mtx(bld);
72 for(string::const_iterator i=str.begin(); (next || i!=str.end());)
74 unsigned c = (next ? next : dec.decode_char(str, i));
75 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
77 if(unsigned lig = get_ligature(c, next))
83 GlyphMap::const_iterator j = glyphs.find(c);
88 bld.transform(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(CodePair(code, next));
121 advance += j->second;
127 unsigned Font::get_ligature(unsigned code, unsigned next) const
129 LigatureMap::const_iterator i = ligatures.find(CodePair(code, next));
130 return (i!=ligatures.end() ? i->second : 0);
134 Font::Glyph::Glyph():
148 Font::Loader::Loader(Font &f):
149 DataFile::CollectionObjectLoader<Font>(f, 0)
154 Font::Loader::Loader(Font &f, Collection &c):
155 DataFile::CollectionObjectLoader<Font>(f, &c)
160 void Font::Loader::init()
162 add("native_size", &Font::native_size);
163 add("ascent", &Font::ascent);
164 add("cap_height", &Font::cap_height);
165 add("descent", &Font::descent);
166 add("texture", &Loader::texture);
167 add("texture", &Loader::texture_ref);
168 add("glyph", &Loader::glyph);
169 add("kerning", &Loader::kerning);
170 add("ligature", &Loader::ligature);
171 add("x_height", &Font::x_height);
174 void Font::Loader::glyph(unsigned c)
179 obj.glyphs.insert(GlyphMap::value_type(c, gl));
182 void Font::Loader::kerning(unsigned l, unsigned r, float d)
184 obj.kerning[CodePair(l, r)] = d;
187 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
189 obj.ligatures[CodePair(l, r)] = g;
192 void Font::Loader::texture()
194 RefPtr<Texture2D> tex = new Texture2D;
199 void Font::Loader::texture_ref(const string &name)
201 obj.texture = &get_collection().get<Texture2D>(name);
206 Font::Glyph::Loader::Loader(Glyph &g):
207 DataFile::ObjectLoader<Glyph>(g)
209 add("texcoords", &Loader::texcoords);
210 add("size", &Glyph::w, &Glyph::h);
211 add("offset", &Glyph::off_x, &Glyph::off_y);
212 add("advance", &Glyph::advance);
215 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)