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>
11 #include "immediate.h"
12 #include "primitivetype.h"
13 #include "texture2d.h"
34 void Font::set_texture(const Texture2D &t)
43 const Texture2D &Font::get_texture() const
46 throw InvalidState("No texture");
50 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
63 glyphs.insert(GlyphMap::value_type(code, glyph));
66 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
70 for(string::const_iterator i=str.begin(); i!=str.end();)
71 x+=get_glyph_advance(dec.decode_char(str, i));
76 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
78 Immediate imm((TEXCOORD2, VERTEX2));
79 draw_string(str, dec, imm);
82 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
85 throw InvalidState("No texture");
92 pbuilder.begin(QUADS);
93 for(string::const_iterator i=str.begin(); i!=str.end();)
95 GlyphMap::const_iterator j=glyphs.find(dec.decode_char(str, i));
99 create_glyph_vertices(j->second, pbuilder, x);
100 x+=j->second.advance;
106 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
108 vbuilder.texcoord(glyph.x1, glyph.y1);
109 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
110 vbuilder.texcoord(glyph.x2, glyph.y1);
111 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
112 vbuilder.texcoord(glyph.x2, glyph.y2);
113 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
114 vbuilder.texcoord(glyph.x1, glyph.y2);
115 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
118 float Font::get_glyph_advance(unsigned code) const
120 GlyphMap::const_iterator i=glyphs.find(code);
124 return i->second.advance;
128 Font::Loader::Loader(Font &f):
129 DataFile::CollectionObjectLoader<Font>(f, 0)
134 Font::Loader::Loader(Font &f, Collection &c):
135 DataFile::CollectionObjectLoader<Font>(f, &c)
140 void Font::Loader::init()
142 add("default_size", &Font::default_size);
143 add("ascent", &Font::ascent);
144 add("descent", &Font::descent);
145 add("texture", &Font::tex);
146 add("texture_inline", &Loader::texture_inline);
147 add("glyph", &Loader::glyph);
150 void Font::Loader::glyph(unsigned c)
155 obj.glyphs.insert(GlyphMap::value_type(c, gl));
158 void Font::Loader::texture_inline()
160 RefPtr<Texture2D> tex=new Texture2D;
162 obj.tex=tex.release();
167 Font::Glyph::Loader::Loader(Glyph &g):
168 DataFile::ObjectLoader<Glyph>(g)
170 add("texcoords", &Loader::texcoords);
171 add("size", &Glyph::w, &Glyph::h);
172 add("offset", &Glyph::off_x, &Glyph::off_y);
173 add("advance", &Glyph::advance);
176 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)