3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "primitivetype.h"
11 #include "texture2d.h"
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 glyphs.insert(GlyphMap::value_type(code, glyph));
46 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
50 for(string::const_iterator i=str.begin(); i!=str.end();)
51 x+=get_glyph_advance(dec.decode_char(str, i));
56 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
60 VertexArray va((TEXCOORD2, VERTEX2));
61 va.reserve(str.size()*4);
62 RefPtr<VertexArrayBuilder> vab=va.modify();
65 for(string::const_iterator i=str.begin(); i!=str.end();)
67 GlyphMap::const_iterator j=glyphs.find(dec.decode_char(str, i));
71 create_glyph_vertices(j->second, *vab, x);
77 glDrawArrays(QUADS, 0, count);
80 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
82 vbuilder.texcoord(glyph.x1, glyph.y1);
83 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
84 vbuilder.texcoord(glyph.x2, glyph.y1);
85 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
86 vbuilder.texcoord(glyph.x2, glyph.y2);
87 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
88 vbuilder.texcoord(glyph.x1, glyph.y2);
89 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
92 float Font::get_glyph_advance(unsigned code) const
94 GlyphMap::const_iterator i=glyphs.find(code);
98 return i->second.advance;
102 Font::Loader::Loader(Font &f):
109 Font::Loader::Loader(Font &f, Collection &c):
116 DataFile::Collection &Font::Loader::get_collection()
119 throw InvalidState("No collection");
123 void Font::Loader::init()
125 add("default_size", &Font::default_size);
126 add("ascent", &Font::ascent);
127 add("descent", &Font::descent);
128 add("texture", &Font::tex);
129 add("glyph", &Loader::glyph);
132 void Font::Loader::glyph(unsigned c)
137 font.glyphs.insert(GlyphMap::value_type(c, gl));
141 Font::Glyph::Loader::Loader(Glyph &g):
144 add("texcoords", &Loader::texcoords);
145 add("size", &Glyph::w, &Glyph::h);
146 add("offset", &Glyph::off_x, &Glyph::off_y);
147 add("advance", &Glyph::advance);
150 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)