1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
7 #include "primitivetype.h"
21 void Font::set_texture(const Texture2D &t)
27 const Texture2D &Font::get_texture() const
30 throw logic_error("No texture");
34 void Font::add_glyph(const Glyph &g)
36 insert_unique(glyphs, g.code, g);
39 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
43 for(string::const_iterator i=str.begin(); i!=str.end();)
44 x += get_glyph_advance(dec.decode_char(str, i));
49 void Font::draw_string(const string &str, StringCodec::Decoder &dec, const Color &color) const
51 Bind bind_tex(get_texture(), true);
52 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
54 build_string(str, dec, imm);
57 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
59 MatrixStack::Push push_mtx(bld.matrix());
62 for(string::const_iterator i=str.begin(); i!=str.end();)
64 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
68 create_glyph_vertices(j->second, bld);
69 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
74 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
76 bld.texcoord(glyph.x1, glyph.y1);
77 bld.vertex(glyph.off_x, glyph.off_y);
78 bld.texcoord(glyph.x2, glyph.y1);
79 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
80 bld.texcoord(glyph.x2, glyph.y2);
81 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
82 bld.texcoord(glyph.x1, glyph.y2);
83 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
86 float Font::get_glyph_advance(unsigned code) const
88 GlyphMap::const_iterator i = glyphs.find(code);
92 return i->second.advance;
96 Font::Loader::Loader(Font &f):
97 DataFile::CollectionObjectLoader<Font>(f, 0)
102 Font::Loader::Loader(Font &f, Collection &c):
103 DataFile::CollectionObjectLoader<Font>(f, &c)
108 void Font::Loader::init()
110 add("native_size", &Font::native_size);
111 add("ascent", &Font::ascent);
112 add("descent", &Font::descent);
113 add("texture", &Loader::texture);
114 add("texture", &Loader::texture_ref);
115 add("glyph", &Loader::glyph);
118 void Font::Loader::glyph(unsigned c)
123 obj.glyphs.insert(GlyphMap::value_type(c, gl));
126 void Font::Loader::texture()
128 RefPtr<Texture2D> tex = new Texture2D;
133 void Font::Loader::texture_ref(const string &name)
135 obj.texture = &get_collection().get<Texture2D>(name);
140 Font::Glyph::Loader::Loader(Glyph &g):
141 DataFile::ObjectLoader<Glyph>(g)
143 add("texcoords", &Loader::texcoords);
144 add("size", &Glyph::w, &Glyph::h);
145 add("offset", &Glyph::off_x, &Glyph::off_y);
146 add("advance", &Glyph::advance);
149 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)