1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/fs/utils.h>
5 #include "primitivebuilder.h"
13 void Font::set_texture(const Texture2D &t)
18 const Texture2D &Font::get_texture() const
21 throw logic_error("No texture");
25 void Font::add_glyph(const Glyph &g)
27 insert_unique(glyphs, g.code, g);
30 void Font::set_kerning(unsigned l, unsigned r, float d)
32 kerning[CodePair(l, r)] = d;
35 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
40 for(auto i=str.begin(); i!=str.end();)
42 unsigned c = dec.decode_char(str, i);
44 x += get_glyph_advance(prev, c);
47 x += get_glyph_advance(prev);
52 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
54 VertexBuilder::PushMatrix push_mtx(bld);
58 for(auto i=str.begin(); (next || i!=str.end());)
60 unsigned c = (next ? next : dec.decode_char(str, i));
61 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
63 if(unsigned lig = get_ligature(c, next))
69 auto j = glyphs.find(c);
74 bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
76 create_glyph_quad(j->second, bld);
81 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
83 bld.begin(TRIANGLE_STRIP);
84 bld.texcoord(glyph.x1, glyph.y2);
85 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
86 bld.texcoord(glyph.x1, glyph.y1);
87 bld.vertex(glyph.off_x, glyph.off_y);
88 bld.texcoord(glyph.x2, glyph.y2);
89 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
90 bld.texcoord(glyph.x2, glyph.y1);
91 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
95 float Font::get_glyph_advance(unsigned code, unsigned next) const
97 auto i = glyphs.find(code);
101 float advance = i->second.advance;
105 auto j = kerning.find(CodePair(code, next));
107 advance += j->second;
113 unsigned Font::get_ligature(unsigned code, unsigned next) const
115 auto i = ligatures.find(CodePair(code, next));
116 return (i!=ligatures.end() ? i->second : 0);
120 Font::Loader::Loader(Font &f, Collection &c):
121 DataFile::CollectionObjectLoader<Font>(f, &c)
123 add("native_size", &Font::native_size);
124 add("ascent", &Font::ascent);
125 add("cap_height", &Font::cap_height);
126 add("descent", &Font::descent);
127 add("texture", &Loader::texture);
128 add("texture", &Loader::texture_ref);
129 add("glyph", &Loader::glyph);
130 add("kerning", &Loader::kerning);
131 add("ligature", &Loader::ligature);
132 add("x_height", &Font::x_height);
135 void Font::Loader::glyph(unsigned c)
140 obj.glyphs.insert(GlyphMap::value_type(c, gl));
143 void Font::Loader::kerning(unsigned l, unsigned r, float d)
145 obj.kerning[CodePair(l, r)] = d;
148 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
150 obj.ligatures[CodePair(l, r)] = g;
153 void Font::Loader::texture()
155 RefPtr<Texture2D> tex = new Texture2D;
157 get_collection().add(FS::basename(get_source())+".tex", tex.get());
158 obj.texture = tex.release();
161 void Font::Loader::texture_ref(const string &name)
163 obj.texture = &get_collection().get<Texture2D>(name);
167 Font::Glyph::Loader::Loader(Glyph &g):
168 DataFile::ObjectLoader<Glyph>(g)
170 add("texcoords", &Loader::texcoords);
171 add("size", &Glyph::w, &Glyph::h);
172 add("offset", &Glyph::off_x, &Glyph::off_y);
173 add("advance", &Glyph::advance);
176 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)