1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
7 #include "primitivetype.h"
23 // Avoid synthesizing ~RefPtr in files including font.h
27 void Font::set_texture(const Texture2D &t)
33 const Texture2D &Font::get_texture() const
36 throw logic_error("No texture");
40 void Font::add_glyph(const Glyph &g)
42 insert_unique(glyphs, g.code, g);
45 void Font::set_kerning(unsigned l, unsigned r, float d)
47 kerning[CodePair(l, r)] = d;
50 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
55 for(string::const_iterator i=str.begin(); i!=str.end();)
57 unsigned c = dec.decode_char(str, i);
59 x += get_glyph_advance(prev, c);
62 x += get_glyph_advance(prev);
67 void Font::draw_string(const string &str, StringCodec::Decoder &dec, const Color &color) const
69 BindRestore bind_tex(get_texture());
70 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
72 build_string(str, dec, imm);
75 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
77 VertexBuilder::PushMatrix push_mtx(bld);
81 for(string::const_iterator i=str.begin(); (next || i!=str.end());)
83 unsigned c = (next ? next : dec.decode_char(str, i));
84 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
86 if(unsigned lig = get_ligature(c, next))
92 GlyphMap::const_iterator j = glyphs.find(c);
97 bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
99 create_glyph_quad(j->second, bld);
104 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
106 bld.begin(TRIANGLE_STRIP);
107 bld.texcoord(glyph.x1, glyph.y2);
108 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
109 bld.texcoord(glyph.x1, glyph.y1);
110 bld.vertex(glyph.off_x, glyph.off_y);
111 bld.texcoord(glyph.x2, glyph.y2);
112 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
113 bld.texcoord(glyph.x2, glyph.y1);
114 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
118 float Font::get_glyph_advance(unsigned code, unsigned next) const
120 GlyphMap::const_iterator i = glyphs.find(code);
124 float advance = i->second.advance;
128 KerningMap::const_iterator j = kerning.find(CodePair(code, next));
130 advance += j->second;
136 unsigned Font::get_ligature(unsigned code, unsigned next) const
138 LigatureMap::const_iterator i = ligatures.find(CodePair(code, next));
139 return (i!=ligatures.end() ? i->second : 0);
143 Font::Glyph::Glyph():
157 Font::Loader::Loader(Font &f):
158 DataFile::CollectionObjectLoader<Font>(f, 0)
163 Font::Loader::Loader(Font &f, Collection &c):
164 DataFile::CollectionObjectLoader<Font>(f, &c)
169 void Font::Loader::init()
171 add("native_size", &Font::native_size);
172 add("ascent", &Font::ascent);
173 add("cap_height", &Font::cap_height);
174 add("descent", &Font::descent);
175 add("texture", &Loader::texture);
176 add("texture", &Loader::texture_ref);
177 add("glyph", &Loader::glyph);
178 add("kerning", &Loader::kerning);
179 add("ligature", &Loader::ligature);
180 add("x_height", &Font::x_height);
183 void Font::Loader::glyph(unsigned c)
188 obj.glyphs.insert(GlyphMap::value_type(c, gl));
191 void Font::Loader::kerning(unsigned l, unsigned r, float d)
193 obj.kerning[CodePair(l, r)] = d;
196 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
198 obj.ligatures[CodePair(l, r)] = g;
201 void Font::Loader::texture()
203 RefPtr<Texture2D> tex = new Texture2D;
208 void Font::Loader::texture_ref(const string &name)
210 obj.texture = &get_collection().get<Texture2D>(name);
215 Font::Glyph::Loader::Loader(Glyph &g):
216 DataFile::ObjectLoader<Glyph>(g)
218 add("texcoords", &Loader::texcoords);
219 add("size", &Glyph::w, &Glyph::h);
220 add("offset", &Glyph::off_x, &Glyph::off_y);
221 add("advance", &Glyph::advance);
224 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)