3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "immediate.h"
11 #include "primitivetype.h"
12 #include "texture2d.h"
33 void Font::set_texture(const Texture2D &t)
42 const Texture2D &Font::get_texture() const
45 throw InvalidState("No texture");
49 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
62 glyphs.insert(GlyphMap::value_type(code, glyph));
65 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
69 for(string::const_iterator i=str.begin(); i!=str.end();)
70 x+=get_glyph_advance(dec.decode_char(str, i));
75 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
77 Immediate imm((TEXCOORD2, VERTEX2));
78 draw_string(str, dec, imm);
81 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
84 throw InvalidState("No texture");
91 pbuilder.begin(QUADS);
92 for(string::const_iterator i=str.begin(); i!=str.end();)
94 GlyphMap::const_iterator j=glyphs.find(dec.decode_char(str, i));
98 create_glyph_vertices(j->second, pbuilder, x);
105 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
107 vbuilder.texcoord(glyph.x1, glyph.y1);
108 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
109 vbuilder.texcoord(glyph.x2, glyph.y1);
110 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
111 vbuilder.texcoord(glyph.x2, glyph.y2);
112 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
113 vbuilder.texcoord(glyph.x1, glyph.y2);
114 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
117 float Font::get_glyph_advance(unsigned code) const
119 GlyphMap::const_iterator i=glyphs.find(code);
123 return i->second.advance;
127 Font::Loader::Loader(Font &f):
134 Font::Loader::Loader(Font &f, Collection &c):
141 DataFile::Collection &Font::Loader::get_collection()
144 throw InvalidState("No collection");
148 void Font::Loader::init()
150 add("default_size", &Font::default_size);
151 add("ascent", &Font::ascent);
152 add("descent", &Font::descent);
153 add("texture", &Font::tex);
154 add("texture_inline", &Loader::texture_inline);
155 add("glyph", &Loader::glyph);
158 void Font::Loader::glyph(unsigned c)
163 font.glyphs.insert(GlyphMap::value_type(c, gl));
166 void Font::Loader::texture_inline()
168 RefPtr<Texture2D> tex=new Texture2D;
170 font.tex=tex.release();
175 Font::Glyph::Loader::Loader(Glyph &g):
178 add("texcoords", &Loader::texcoords);
179 add("size", &Glyph::w, &Glyph::h);
180 add("offset", &Glyph::off_x, &Glyph::off_y);
181 add("advance", &Glyph::advance);
184 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)