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, Codecs::Decoder &dec) const
54 for(string::const_iterator i=str.begin(); i!=str.end();)
55 x+=get_glyph_advance(dec.decode_char(str, i));
60 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
64 for(string::const_iterator i=str.begin(); i!=str.end();)
65 draw_glyph(dec.decode_char(str, i));
70 void Font::create_glyph_vertices()
73 RefPtr<VertexArrayBuilder> va_builder=varray.modify();
76 for(GlyphMap::iterator i=glyphs.begin(); i!=glyphs.end(); ++i, ++n)
79 create_glyph_vertices(i->second, *va_builder);
83 void Font::create_glyph_vertices(const Glyph &glyph, VertexArrayBuilder &va_builder)
85 va_builder.texcoord(glyph.x1, glyph.y1);
86 va_builder.vertex(glyph.off_x, glyph.off_y);
87 va_builder.texcoord(glyph.x2, glyph.y1);
88 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y);
89 va_builder.texcoord(glyph.x2, glyph.y2);
90 va_builder.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
91 va_builder.texcoord(glyph.x1, glyph.y2);
92 va_builder.vertex(glyph.off_x, glyph.off_y+glyph.h);
95 void Font::prepare_render() const
102 void Font::draw_glyph(unsigned code) const
104 GlyphMap::const_iterator i=glyphs.find(code);
108 glDrawArrays(GL_QUADS, i->second.index*4, 4);
110 glTranslatef(i->second.advance, 0, 0);
113 float Font::get_glyph_advance(unsigned code) const
115 GlyphMap::const_iterator i=glyphs.find(code);
119 return i->second.advance;
123 Font::Loader::Loader(Font &f):
130 Font::Loader::Loader(Font &f, Collection &c):
137 DataFile::Collection &Font::Loader::get_collection()
140 throw InvalidState("No collection");
144 Font::Loader::~Loader()
146 font.create_glyph_vertices();
149 void Font::Loader::init()
151 add("default_size", &Font::default_size);
152 add("ascent", &Font::ascent);
153 add("descent", &Font::descent);
154 add("texture", &Font::tex);
155 add("glyph", &Loader::glyph);
158 void Font::Loader::glyph(unsigned c)
163 font.glyphs.insert(GlyphMap::value_type(c, gl));
167 Font::Glyph::Loader::Loader(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)