1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/fs/utils.h>
5 #include "primitivebuilder.h"
21 // Avoid synthesizing ~RefPtr in files including font.h
25 void Font::set_texture(const Texture2D &t)
30 const Texture2D &Font::get_texture() const
33 throw logic_error("No texture");
37 void Font::add_glyph(const Glyph &g)
39 insert_unique(glyphs, g.code, g);
42 void Font::set_kerning(unsigned l, unsigned r, float d)
44 kerning[CodePair(l, r)] = d;
47 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
52 for(auto i=str.begin(); i!=str.end();)
54 unsigned c = dec.decode_char(str, i);
56 x += get_glyph_advance(prev, c);
59 x += get_glyph_advance(prev);
64 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
66 VertexBuilder::PushMatrix push_mtx(bld);
70 for(auto i=str.begin(); (next || i!=str.end());)
72 unsigned c = (next ? next : dec.decode_char(str, i));
73 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
75 if(unsigned lig = get_ligature(c, next))
81 auto j = glyphs.find(c);
86 bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
88 create_glyph_quad(j->second, bld);
93 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
95 bld.begin(TRIANGLE_STRIP);
96 bld.texcoord(glyph.x1, glyph.y2);
97 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
98 bld.texcoord(glyph.x1, glyph.y1);
99 bld.vertex(glyph.off_x, glyph.off_y);
100 bld.texcoord(glyph.x2, glyph.y2);
101 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
102 bld.texcoord(glyph.x2, glyph.y1);
103 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
107 float Font::get_glyph_advance(unsigned code, unsigned next) const
109 auto i = glyphs.find(code);
113 float advance = i->second.advance;
117 auto j = kerning.find(CodePair(code, next));
119 advance += j->second;
125 unsigned Font::get_ligature(unsigned code, unsigned next) const
127 auto i = ligatures.find(CodePair(code, next));
128 return (i!=ligatures.end() ? i->second : 0);
132 Font::Glyph::Glyph():
146 Font::Loader::Loader(Font &f):
147 DataFile::CollectionObjectLoader<Font>(f, 0)
152 Font::Loader::Loader(Font &f, Collection &c):
153 DataFile::CollectionObjectLoader<Font>(f, &c)
158 void Font::Loader::init()
160 add("native_size", &Font::native_size);
161 add("ascent", &Font::ascent);
162 add("cap_height", &Font::cap_height);
163 add("descent", &Font::descent);
164 add("texture", &Loader::texture);
165 add("texture", &Loader::texture_ref);
166 add("glyph", &Loader::glyph);
167 add("kerning", &Loader::kerning);
168 add("ligature", &Loader::ligature);
169 add("x_height", &Font::x_height);
172 void Font::Loader::glyph(unsigned c)
177 obj.glyphs.insert(GlyphMap::value_type(c, gl));
180 void Font::Loader::kerning(unsigned l, unsigned r, float d)
182 obj.kerning[CodePair(l, r)] = d;
185 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
187 obj.ligatures[CodePair(l, r)] = g;
190 void Font::Loader::texture()
192 RefPtr<Texture2D> tex = new Texture2D;
194 get_collection().add(FS::basename(get_source())+".tex2d", tex.get());
195 obj.texture = tex.release();
198 void Font::Loader::texture_ref(const string &name)
200 obj.texture = &get_collection().get<Texture2D>(name);
204 Font::Glyph::Loader::Loader(Glyph &g):
205 DataFile::ObjectLoader<Glyph>(g)
207 add("texcoords", &Loader::texcoords);
208 add("size", &Glyph::w, &Glyph::h);
209 add("offset", &Glyph::off_x, &Glyph::off_y);
210 add("advance", &Glyph::advance);
213 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)