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 Color &color) const
61 Bind bind_tex(get_texture(), true);
62 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
64 build_string(str, dec, imm);
67 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
69 MatrixStack::Push push_mtx(bld.matrix());
72 for(string::const_iterator i=str.begin(); i!=str.end();)
74 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
78 create_glyph_vertices(j->second, bld);
79 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
84 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
86 bld.texcoord(glyph.x1, glyph.y1);
87 bld.vertex(glyph.off_x, glyph.off_y);
88 bld.texcoord(glyph.x2, glyph.y1);
89 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
90 bld.texcoord(glyph.x2, glyph.y2);
91 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
92 bld.texcoord(glyph.x1, glyph.y2);
93 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
96 float Font::get_glyph_advance(unsigned code) const
98 GlyphMap::const_iterator i = glyphs.find(code);
102 return i->second.advance;
106 Font::Loader::Loader(Font &f):
107 DataFile::CollectionObjectLoader<Font>(f, 0)
112 Font::Loader::Loader(Font &f, Collection &c):
113 DataFile::CollectionObjectLoader<Font>(f, &c)
118 void Font::Loader::init()
120 add("native_size", &Font::native_size);
121 add("ascent", &Font::ascent);
122 add("descent", &Font::descent);
123 add("texture", &Loader::texture);
124 add("texture", &Loader::texture_ref);
125 add("glyph", &Loader::glyph);
127 // Deprecated aliases
128 add("default_size", &Font::native_size);
129 add("texture_inline", &Loader::texture);
132 void Font::Loader::glyph(unsigned c)
137 obj.glyphs.insert(GlyphMap::value_type(c, gl));
140 void Font::Loader::texture()
142 RefPtr<Texture2D> tex = new Texture2D;
147 void Font::Loader::texture_ref(const string &name)
149 obj.texture = &get_collection().get<Texture2D>(name);
154 Font::Glyph::Loader::Loader(Glyph &g):
155 DataFile::ObjectLoader<Glyph>(g)
157 add("texcoords", &Loader::texcoords);
158 add("size", &Glyph::w, &Glyph::h);
159 add("offset", &Glyph::off_x, &Glyph::off_y);
160 add("advance", &Glyph::advance);
163 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)