3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "texture2d.h"
22 varray((TEXCOORD2, VERTEX2))
25 void Font::set_texture(const Texture2D &t)
30 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
43 glyph.index=glyphs.size();
44 glyphs.insert(GlyphMap::value_type(code, glyph));
46 RefPtr<VertexArrayBuilder> va_builder=varray.modify();
47 create_glyph_vertices(glyph, *va_builder);
50 float Font::get_string_width(const string &str) const
54 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
55 x+=get_glyph_advance(static_cast<unsigned char>(*i));
60 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
64 for(string::const_iterator i=str.begin(); i!=str.end();)
65 x+=get_glyph_advance(dec.decode_char(str, i));
70 void Font::draw_string(const string &str) const
74 for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
75 draw_glyph(static_cast<unsigned char>(*i));
80 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
84 for(string::const_iterator i=str.begin(); i!=str.end();)
85 draw_glyph(dec.decode_char(str, i));
90 void Font::create_glyph_vertices()
93 RefPtr<VertexArrayBuilder> va_builder=varray.modify();
96 for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n)
99 create_glyph_vertices(i->second, *va_builder);
103 void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder)
105 va_builder.texcoord(glyph.x1, glyph.y1);
106 va_builder.vertex(glyph.off_x, glyph.off_y);
107 va_builder.texcoord(glyph.x2, glyph.y1);
108 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y);
109 va_builder.texcoord(glyph.x2, glyph.y2);
110 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
111 va_builder.texcoord(glyph.x1, glyph.y2);
112 va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h);
115 void Font::prepare_render() const
122 void Font::draw_glyph(unsigned code) const
124 GlyphMap::const_iterator i=glyphs.find(code);
128 glDrawArrays(GL_QUADS, i->second.index*4, 4);
130 glTranslatef(i->second.advance, 0, 0);
133 float Font::get_glyph_advance(unsigned code) const
135 GlyphMap::const_iterator i=glyphs.find(code);
139 return i->second.advance;
143 Font::Loader::Loader(Font &f):
150 Font::Loader::Loader(Font &f, Collection &c):
157 DataFile::Collection &Font::Loader::get_collection()
160 throw InvalidState("No collection");
164 Font::Loader::~Loader()
166 font.create_glyph_vertices();
169 void Font::Loader::init()
171 add("default_size", &Font::default_size);
172 add("ascent", &Font::ascent);
173 add("descent", &Font::descent);
174 add("texture", &Font::tex);
175 add("glyph", &Loader::glyph);
178 void Font::Loader::glyph(unsigned c)
183 font.glyphs.insert(GlyphMap::value_type(c, gl));
187 Font::Glyph::Loader::Loader(Glyph &g):
190 add("texcoords", &Loader::texcoords);
191 add("size", &Glyph::w, &Glyph::h);
192 add("offset", &Glyph::off_x, &Glyph::off_y);
193 add("advance", &Glyph::advance);
196 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)