3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/datafile/collection.h>
12 #include "immediate.h"
13 #include "primitivetype.h"
14 #include "texture2d.h"
35 void Font::set_texture(const Texture2D &t)
44 const Texture2D &Font::get_texture() const
47 throw InvalidState("No texture");
51 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
64 glyphs.insert(GlyphMap::value_type(code, glyph));
67 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
71 for(string::const_iterator i=str.begin(); i!=str.end();)
72 x += get_glyph_advance(dec.decode_char(str, i));
77 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
79 Immediate imm((TEXCOORD2, VERTEX2));
80 draw_string(str, dec, imm);
83 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
86 throw InvalidState("No texture");
88 Bind bind_tex(tex, true);
93 pbuilder.begin(QUADS);
94 for(string::const_iterator i=str.begin(); i!=str.end();)
96 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
100 create_glyph_vertices(j->second, pbuilder, x);
101 x += j->second.advance;
107 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
109 vbuilder.texcoord(glyph.x1, glyph.y1);
110 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
111 vbuilder.texcoord(glyph.x2, glyph.y1);
112 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
113 vbuilder.texcoord(glyph.x2, glyph.y2);
114 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
115 vbuilder.texcoord(glyph.x1, glyph.y2);
116 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
119 float Font::get_glyph_advance(unsigned code) const
121 GlyphMap::const_iterator i = glyphs.find(code);
125 return i->second.advance;
129 Font::Loader::Loader(Font &f):
130 DataFile::CollectionObjectLoader<Font>(f, 0)
135 Font::Loader::Loader(Font &f, Collection &c):
136 DataFile::CollectionObjectLoader<Font>(f, &c)
141 void Font::Loader::init()
143 add("default_size", &Font::default_size);
144 add("ascent", &Font::ascent);
145 add("descent", &Font::descent);
146 add("texture", &Font::tex);
147 add("texture_inline", &Loader::texture_inline);
148 add("glyph", &Loader::glyph);
151 void Font::Loader::glyph(unsigned c)
156 obj.glyphs.insert(GlyphMap::value_type(c, gl));
159 void Font::Loader::texture_inline()
161 RefPtr<Texture2D> tex = new Texture2D;
163 obj.tex = tex.release();
168 Font::Glyph::Loader::Loader(Glyph &g):
169 DataFile::ObjectLoader<Glyph>(g)
171 add("texcoords", &Loader::texcoords);
172 add("size", &Glyph::w, &Glyph::h);
173 add("offset", &Glyph::off_x, &Glyph::off_y);
174 add("advance", &Glyph::advance);
177 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)