1 #include <msp/datafile/collection.h>
6 #include "primitivetype.h"
20 void Font::set_texture(const Texture2D &t)
26 const Texture2D &Font::get_texture() const
29 throw logic_error("No texture");
33 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
46 glyphs.insert(GlyphMap::value_type(code, glyph));
49 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
53 for(string::const_iterator i=str.begin(); i!=str.end();)
54 x += get_glyph_advance(dec.decode_char(str, i));
59 void Font::draw_string(const string &str, StringCodec::Decoder &dec) const
61 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
62 draw_string(str, dec, imm);
65 void Font::draw_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
67 MatrixStack::Push push_mtx(bld.matrix());
68 Bind bind_tex(get_texture(), true);
71 for(string::const_iterator i=str.begin(); i!=str.end();)
73 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
77 create_glyph_vertices(j->second, bld);
78 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
83 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
85 bld.texcoord(glyph.x1, glyph.y1);
86 bld.vertex(glyph.off_x, glyph.off_y);
87 bld.texcoord(glyph.x2, glyph.y1);
88 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
89 bld.texcoord(glyph.x2, glyph.y2);
90 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
91 bld.texcoord(glyph.x1, glyph.y2);
92 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
95 float Font::get_glyph_advance(unsigned code) const
97 GlyphMap::const_iterator i = glyphs.find(code);
101 return i->second.advance;
105 Font::Loader::Loader(Font &f):
106 DataFile::CollectionObjectLoader<Font>(f, 0)
111 Font::Loader::Loader(Font &f, Collection &c):
112 DataFile::CollectionObjectLoader<Font>(f, &c)
117 void Font::Loader::init()
119 add("native_size", &Font::native_size);
120 add("ascent", &Font::ascent);
121 add("descent", &Font::descent);
122 add("texture", &Loader::texture);
123 add("texture", &Loader::texture_ref);
124 add("glyph", &Loader::glyph);
126 // Deprecated aliases
127 add("default_size", &Font::native_size);
128 add("texture_inline", &Loader::texture);
131 void Font::Loader::glyph(unsigned c)
136 obj.glyphs.insert(GlyphMap::value_type(c, gl));
139 void Font::Loader::texture()
141 RefPtr<Texture2D> tex = new Texture2D;
146 void Font::Loader::texture_ref(const string &name)
148 obj.texture = &get_collection().get<Texture2D>(name);
153 Font::Glyph::Loader::Loader(Glyph &g):
154 DataFile::ObjectLoader<Glyph>(g)
156 add("texcoords", &Loader::texcoords);
157 add("size", &Glyph::w, &Glyph::h);
158 add("offset", &Glyph::off_x, &Glyph::off_y);
159 add("advance", &Glyph::advance);
162 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)