3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/datafile/collection.h>
12 #include "immediate.h"
13 #include "primitivetype.h"
14 #include "texture2d.h"
27 void Font::set_texture(const Texture2D &t)
33 const Texture2D &Font::get_texture() const
36 throw InvalidState("No texture");
40 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
53 glyphs.insert(GlyphMap::value_type(code, glyph));
56 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
60 for(string::const_iterator i=str.begin(); i!=str.end();)
61 x += get_glyph_advance(dec.decode_char(str, i));
66 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
68 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
69 draw_string(str, dec, imm);
72 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &bld) const
74 MatrixStack::Push push_mtx(bld.matrix());
75 Bind bind_tex(get_texture(), true);
78 for(string::const_iterator i=str.begin(); i!=str.end();)
80 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
84 create_glyph_vertices(j->second, bld);
85 bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
90 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
92 bld.texcoord(glyph.x1, glyph.y1);
93 bld.vertex(glyph.off_x, glyph.off_y);
94 bld.texcoord(glyph.x2, glyph.y1);
95 bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
96 bld.texcoord(glyph.x2, glyph.y2);
97 bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
98 bld.texcoord(glyph.x1, glyph.y2);
99 bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
102 float Font::get_glyph_advance(unsigned code) const
104 GlyphMap::const_iterator i = glyphs.find(code);
108 return i->second.advance;
112 Font::Loader::Loader(Font &f):
113 DataFile::CollectionObjectLoader<Font>(f, 0)
118 Font::Loader::Loader(Font &f, Collection &c):
119 DataFile::CollectionObjectLoader<Font>(f, &c)
124 void Font::Loader::init()
126 add("native_size", &Font::native_size);
127 add("ascent", &Font::ascent);
128 add("descent", &Font::descent);
129 add("texture", &Loader::texture);
130 add("texture", &Loader::texture_ref);
131 add("glyph", &Loader::glyph);
133 // Deprecated aliases
134 add("default_size", &Font::native_size);
135 add("texture_inline", &Loader::texture);
138 void Font::Loader::glyph(unsigned c)
143 obj.glyphs.insert(GlyphMap::value_type(c, gl));
146 void Font::Loader::texture()
148 RefPtr<Texture2D> tex = new Texture2D;
153 void Font::Loader::texture_ref(const string &name)
155 obj.texture = get_collection().get<Texture2D>(name);
160 Font::Glyph::Loader::Loader(Glyph &g):
161 DataFile::ObjectLoader<Glyph>(g)
163 add("texcoords", &Loader::texcoords);
164 add("size", &Glyph::w, &Glyph::h);
165 add("offset", &Glyph::off_x, &Glyph::off_y);
166 add("advance", &Glyph::advance);
169 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)