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, Collection *c):
147 DataFile::CollectionObjectLoader<Font>(f, c)
149 add("native_size", &Font::native_size);
150 add("ascent", &Font::ascent);
151 add("cap_height", &Font::cap_height);
152 add("descent", &Font::descent);
153 add("texture", &Loader::texture);
154 add("texture", &Loader::texture_ref);
155 add("glyph", &Loader::glyph);
156 add("kerning", &Loader::kerning);
157 add("ligature", &Loader::ligature);
158 add("x_height", &Font::x_height);
161 void Font::Loader::glyph(unsigned c)
166 obj.glyphs.insert(GlyphMap::value_type(c, gl));
169 void Font::Loader::kerning(unsigned l, unsigned r, float d)
171 obj.kerning[CodePair(l, r)] = d;
174 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
176 obj.ligatures[CodePair(l, r)] = g;
179 void Font::Loader::texture()
181 RefPtr<Texture2D> tex = new Texture2D;
183 get_collection().add(FS::basename(get_source())+".tex2d", tex.get());
184 obj.texture = tex.release();
187 void Font::Loader::texture_ref(const string &name)
189 obj.texture = &get_collection().get<Texture2D>(name);
193 Font::Glyph::Loader::Loader(Glyph &g):
194 DataFile::ObjectLoader<Glyph>(g)
196 add("texcoords", &Loader::texcoords);
197 add("size", &Glyph::w, &Glyph::h);
198 add("offset", &Glyph::off_x, &Glyph::off_y);
199 add("advance", &Glyph::advance);
202 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)