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"
27 void Font::set_texture(const Texture2D &t)
33 const Texture2D &Font::get_texture() const
36 throw InvalidState("No texture");
40 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
53 glyphs.insert(GlyphMap::value_type(code, glyph));
56 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
60 for(string::const_iterator i=str.begin(); i!=str.end();)
61 x += get_glyph_advance(dec.decode_char(str, i));
66 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
68 Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
69 draw_string(str, dec, imm);
72 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
74 Bind bind_tex(get_texture(), true);
79 pbuilder.begin(QUADS);
80 for(string::const_iterator i=str.begin(); i!=str.end();)
82 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
86 create_glyph_vertices(j->second, pbuilder, x);
87 x += j->second.advance;
93 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
95 vbuilder.texcoord(glyph.x1, glyph.y1);
96 vbuilder.vertex(x+glyph.off_x, glyph.off_y);
97 vbuilder.texcoord(glyph.x2, glyph.y1);
98 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
99 vbuilder.texcoord(glyph.x2, glyph.y2);
100 vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
101 vbuilder.texcoord(glyph.x1, glyph.y2);
102 vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
105 float Font::get_glyph_advance(unsigned code) const
107 GlyphMap::const_iterator i = glyphs.find(code);
111 return i->second.advance;
115 Font::Loader::Loader(Font &f):
116 DataFile::CollectionObjectLoader<Font>(f, 0)
121 Font::Loader::Loader(Font &f, Collection &c):
122 DataFile::CollectionObjectLoader<Font>(f, &c)
127 void Font::Loader::init()
129 add("native_size", &Font::native_size);
130 add("ascent", &Font::ascent);
131 add("descent", &Font::descent);
132 add("texture", &Loader::texture);
133 add("texture", &Loader::texture_ref);
134 add("glyph", &Loader::glyph);
136 // Deprecated aliases
137 add("default_size", &Font::native_size);
138 add("texture_inline", &Loader::texture);
141 void Font::Loader::glyph(unsigned c)
146 obj.glyphs.insert(GlyphMap::value_type(c, gl));
149 void Font::Loader::texture()
151 RefPtr<Texture2D> tex = new Texture2D;
156 void Font::Loader::texture_ref(const string &name)
158 obj.texture = get_collection().get<Texture2D>(name);
163 Font::Glyph::Loader::Loader(Glyph &g):
164 DataFile::ObjectLoader<Glyph>(g)
166 add("texcoords", &Loader::texcoords);
167 add("size", &Glyph::w, &Glyph::h);
168 add("offset", &Glyph::off_x, &Glyph::off_y);
169 add("advance", &Glyph::advance);
172 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)