1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
7 #include "primitivetype.h"
21 // Avoid synthesizing ~RefPtr in files including font.h
25 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 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
47 for(string::const_iterator i=str.begin(); i!=str.end();)
48 x += get_glyph_advance(dec.decode_char(str, i));
53 void Font::draw_string(const string &str, StringCodec::Decoder &dec, const Color &color) const
55 Bind bind_tex(get_texture(), true);
56 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
58 build_string(str, dec, imm);
61 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
63 MatrixStack::Push push_mtx(bld.matrix());
66 for(string::const_iterator i=str.begin(); i!=str.end();)
68 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
72 create_glyph_vertices(j->second, bld);
73 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
78 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
80 bld.texcoord(glyph.x1, glyph.y1);
81 bld.vertex(glyph.off_x, glyph.off_y);
82 bld.texcoord(glyph.x2, glyph.y1);
83 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
84 bld.texcoord(glyph.x2, glyph.y2);
85 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
86 bld.texcoord(glyph.x1, glyph.y2);
87 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
90 float Font::get_glyph_advance(unsigned code) const
92 GlyphMap::const_iterator i = glyphs.find(code);
96 return i->second.advance;
100 Font::Loader::Loader(Font &f):
101 DataFile::CollectionObjectLoader<Font>(f, 0)
106 Font::Loader::Loader(Font &f, Collection &c):
107 DataFile::CollectionObjectLoader<Font>(f, &c)
112 void Font::Loader::init()
114 add("native_size", &Font::native_size);
115 add("ascent", &Font::ascent);
116 add("descent", &Font::descent);
117 add("texture", &Loader::texture);
118 add("texture", &Loader::texture_ref);
119 add("glyph", &Loader::glyph);
122 void Font::Loader::glyph(unsigned c)
127 obj.glyphs.insert(GlyphMap::value_type(c, gl));
130 void Font::Loader::texture()
132 RefPtr<Texture2D> tex = new Texture2D;
137 void Font::Loader::texture_ref(const string &name)
139 obj.texture = &get_collection().get<Texture2D>(name);
144 Font::Glyph::Loader::Loader(Glyph &g):
145 DataFile::ObjectLoader<Glyph>(g)
147 add("texcoords", &Loader::texcoords);
148 add("size", &Glyph::w, &Glyph::h);
149 add("offset", &Glyph::off_x, &Glyph::off_y);
150 add("advance", &Glyph::advance);
153 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)