1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/fs/utils.h>
6 #include "primitivebuilder.h"
22 // Avoid synthesizing ~RefPtr in files including font.h
26 void Font::set_texture(const Texture2D &t)
31 const Texture2D &Font::get_texture() const
34 throw logic_error("No texture");
38 void Font::add_glyph(const Glyph &g)
40 insert_unique(glyphs, g.code, g);
43 void Font::set_kerning(unsigned l, unsigned r, float d)
45 kerning[CodePair(l, r)] = d;
48 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
53 for(auto i=str.begin(); i!=str.end();)
55 unsigned c = dec.decode_char(str, i);
57 x += get_glyph_advance(prev, c);
60 x += get_glyph_advance(prev);
65 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
67 VertexBuilder::PushMatrix push_mtx(bld);
71 for(auto i=str.begin(); (next || i!=str.end());)
73 unsigned c = (next ? next : dec.decode_char(str, i));
74 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
76 if(unsigned lig = get_ligature(c, next))
82 auto j = glyphs.find(c);
87 bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
89 create_glyph_quad(j->second, bld);
94 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
96 bld.begin(TRIANGLE_STRIP);
97 bld.texcoord(glyph.x1, glyph.y2);
98 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
99 bld.texcoord(glyph.x1, glyph.y1);
100 bld.vertex(glyph.off_x, glyph.off_y);
101 bld.texcoord(glyph.x2, glyph.y2);
102 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
103 bld.texcoord(glyph.x2, glyph.y1);
104 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
108 float Font::get_glyph_advance(unsigned code, unsigned next) const
110 auto i = glyphs.find(code);
114 float advance = i->second.advance;
118 auto j = kerning.find(CodePair(code, next));
120 advance += j->second;
126 unsigned Font::get_ligature(unsigned code, unsigned next) const
128 auto i = ligatures.find(CodePair(code, next));
129 return (i!=ligatures.end() ? i->second : 0);
133 Font::Glyph::Glyph():
147 Font::Loader::Loader(Font &f):
148 DataFile::CollectionObjectLoader<Font>(f, 0)
153 Font::Loader::Loader(Font &f, Collection &c):
154 DataFile::CollectionObjectLoader<Font>(f, &c)
159 void Font::Loader::init()
161 add("native_size", &Font::native_size);
162 add("ascent", &Font::ascent);
163 add("cap_height", &Font::cap_height);
164 add("descent", &Font::descent);
165 add("texture", &Loader::texture);
166 add("texture", &Loader::texture_ref);
167 add("glyph", &Loader::glyph);
168 add("kerning", &Loader::kerning);
169 add("ligature", &Loader::ligature);
170 add("x_height", &Font::x_height);
173 void Font::Loader::glyph(unsigned c)
178 obj.glyphs.insert(GlyphMap::value_type(c, gl));
181 void Font::Loader::kerning(unsigned l, unsigned r, float d)
183 obj.kerning[CodePair(l, r)] = d;
186 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
188 obj.ligatures[CodePair(l, r)] = g;
191 void Font::Loader::texture()
193 RefPtr<Texture2D> tex = new Texture2D;
195 get_collection().add(FS::basename(get_source())+".tex2d", tex.get());
196 obj.texture = tex.release();
199 void Font::Loader::texture_ref(const string &name)
201 obj.texture = &get_collection().get<Texture2D>(name);
205 Font::Glyph::Loader::Loader(Glyph &g):
206 DataFile::ObjectLoader<Glyph>(g)
208 add("texcoords", &Loader::texcoords);
209 add("size", &Glyph::w, &Glyph::h);
210 add("offset", &Glyph::off_x, &Glyph::off_y);
211 add("advance", &Glyph::advance);
214 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)