]> git.tdb.fi Git - libs/gl.git/blobdiff - source/font.cpp
Update gl.xml to latest version
[libs/gl.git] / source / font.cpp
index 53e3bd6261c0b11bfdbefc0e46a66adde4054d40..1005609eb16a8ed1a08377c276a7c152c61d22a0 100644 (file)
@@ -18,6 +18,10 @@ Font::Font():
        descent(0)
 { }
 
+// Avoid synthesizing ~RefPtr in files including font.h
+Font::~Font()
+{ }
+
 void Font::set_texture(const Texture2D &t)
 {
        texture = &t;
@@ -36,19 +40,31 @@ void Font::add_glyph(const Glyph &g)
        insert_unique(glyphs, g.code, g);
 }
 
+void Font::set_kerning(unsigned l, unsigned r, float d)
+{
+       kerning[KerningKey(l, r)] = d;
+}
+
 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
 {
        float x = 0;
 
+       unsigned prev = 0;
        for(string::const_iterator i=str.begin(); i!=str.end();)
-               x += get_glyph_advance(dec.decode_char(str, i));
+       {
+               unsigned c = dec.decode_char(str, i);
+               if(prev)
+                       x += get_glyph_advance(prev, c);
+               prev = c;
+       }
+       x += get_glyph_advance(prev);
 
        return x;
 }
 
 void Font::draw_string(const string &str, StringCodec::Decoder &dec, const Color &color) const
 {
-       Bind bind_tex(get_texture(), true);
+       BindRestore bind_tex(get_texture());
        Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
        imm.color(color);
        build_string(str, dec, imm);
@@ -59,14 +75,19 @@ void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveB
        MatrixStack::Push push_mtx(bld.matrix());
 
        bld.begin(QUADS);
+       unsigned prev = 0;
        for(string::const_iterator i=str.begin(); i!=str.end();)
        {
-               GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
+               unsigned c = dec.decode_char(str, i);
+               GlyphMap::const_iterator j = glyphs.find(c);
                if(j==glyphs.end())
                        continue;
 
+               if(prev)
+                       bld.matrix() *= Matrix::translation(get_glyph_advance(prev, c), 0, 0);
+
                create_glyph_vertices(j->second, bld);
-               bld.matrix() *= Matrix::translation(j->second.advance, 0, 0);
+               prev = c;
        }
        bld.end();
 }
@@ -83,13 +104,22 @@ void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &bld) const
        bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
 }
 
-float Font::get_glyph_advance(unsigned code) const
+float Font::get_glyph_advance(unsigned code, unsigned next) const
 {
        GlyphMap::const_iterator i = glyphs.find(code);
        if(i==glyphs.end())
                return 0;
 
-       return i->second.advance;
+       float advance = i->second.advance;
+
+       if(next)
+       {
+               KerningMap::const_iterator j = kerning.find(KerningKey(code, next));
+               if(j!=kerning.end())
+                       advance += j->second;
+       }
+
+       return advance;
 }
 
 
@@ -113,6 +143,7 @@ void Font::Loader::init()
        add("texture",     &Loader::texture);
        add("texture",     &Loader::texture_ref);
        add("glyph",       &Loader::glyph);
+       add("kerning",     &Loader::kerning);
 }
 
 void Font::Loader::glyph(unsigned c)
@@ -123,6 +154,11 @@ void Font::Loader::glyph(unsigned c)
        obj.glyphs.insert(GlyphMap::value_type(c, gl));
 }
 
+void Font::Loader::kerning(unsigned l, unsigned r, float d)
+{
+       obj.kerning[KerningKey(l, r)] = d;
+}
+
 void Font::Loader::texture()
 {
        RefPtr<Texture2D> tex = new Texture2D;
@@ -146,12 +182,12 @@ Font::Glyph::Loader::Loader(Glyph &g):
        add("advance",   &Glyph::advance);
 }
 
-void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
+void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)
 {
-       obj.x1 = x1;
-       obj.y1 = y1;
-       obj.x2 = x2;
-       obj.y2 = y2;
+       obj.x1 = x1_;
+       obj.y1 = y1_;
+       obj.x2 = x2_;
+       obj.y2 = y2_;
 }
 
 } // namespace GL