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"
26 void Font::set_texture(const Texture2D &t)
31 const Texture2D &Font::get_texture() const
34 throw InvalidState("No texture");
38 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
51 glyphs.insert(GlyphMap::value_type(code, glyph));
54 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
58 for(string::const_iterator i=str.begin(); i!=str.end();)
59 x+=get_glyph_advance(dec.decode_char(str, i));
64 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
66 Immediate imm((TEXCOORD2, VERTEX2));
67 draw_string(str, dec, imm);
70 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
73 throw InvalidState("No texture");
80 pbuilder.begin(QUADS);
81 for(string::const_iterator i=str.begin(); i!=str.end();)
83 GlyphMap::const_iterator j=glyphs.find(dec.decode_char(str, i));
87 create_glyph_vertices(j->second, pbuilder, x);
94 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
96 vbuilder.texcoord(glyph.x1, glyph.y1);
97 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
98 vbuilder.texcoord(glyph.x2, glyph.y1);
99 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
100 vbuilder.texcoord(glyph.x2, glyph.y2);
101 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
102 vbuilder.texcoord(glyph.x1, glyph.y2);
103 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
106 float Font::get_glyph_advance(unsigned code) const
108 GlyphMap::const_iterator i=glyphs.find(code);
112 return i->second.advance;
116 Font::Loader::Loader(Font &f):
123 Font::Loader::Loader(Font &f, Collection &c):
130 DataFile::Collection &Font::Loader::get_collection()
133 throw InvalidState("No collection");
137 void Font::Loader::init()
139 add("default_size", &Font::default_size);
140 add("ascent", &Font::ascent);
141 add("descent", &Font::descent);
142 add("texture", &Font::tex);
143 add("glyph", &Loader::glyph);
146 void Font::Loader::glyph(unsigned c)
151 font.glyphs.insert(GlyphMap::value_type(c, gl));
155 Font::Glyph::Loader::Loader(Glyph &g):
158 add("texcoords", &Loader::texcoords);
159 add("size", &Glyph::w, &Glyph::h);
160 add("offset", &Glyph::off_x, &Glyph::off_y);
161 add("advance", &Glyph::advance);
164 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)