]> git.tdb.fi Git - libs/gl.git/blobdiff - source/font.cpp
Support embedding textures inside font files
[libs/gl.git] / source / font.cpp
index 31f43f7347802f6124c0388c15519cea4864444b..f82b45d8ac81fe7bb3ac30b7f70be453b2a720b7 100644 (file)
@@ -1,5 +1,14 @@
-#include <GL/gl.h>
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include "gl.h"
 #include "font.h"
+#include "immediate.h"
+#include "primitivetype.h"
 #include "texture2d.h"
 
 using namespace std;
@@ -9,15 +18,35 @@ namespace GL {
 
 Font::Font():
        tex(0),
-       own_tex(false)
+       own_tex(false),
+       default_size(1),
+       ascent(1),
+       descent(0)
 { }
 
+Font::~Font()
+{
+       if(own_tex)
+               delete tex;
+}
+
 void Font::set_texture(const Texture2D &t)
 {
+       if(own_tex)
+               delete tex;
+
        tex=&t;
+       own_tex=false;
 }
 
-void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float w, float h, float desc, float adv)
+const Texture2D &Font::get_texture() const
+{
+       if(!tex)
+               throw InvalidState("No texture");
+       return *tex;
+}
+
+void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
 {
        Glyph glyph;
        glyph.code=code;
@@ -27,94 +56,103 @@ void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float
        glyph.y2=y2;
        glyph.w=w;
        glyph.h=h;
-       glyph.descent=desc;
+       glyph.off_x=ox;
+       glyph.off_y=oy;
        glyph.advance=adv;
+       glyphs.insert(GlyphMap::value_type(code, glyph));
 }
 
-float Font::get_string_width(const string &str) const
+float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
 {
        float x=0;
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
-       {
-               GlyphMap::const_iterator j=glyphs.find((unsigned char)*i);
-               if(j==glyphs.end())
-                       continue;
-
-               const Glyph &glyph=j->second;
-
-               x+=glyph.advance;
-       }
+       for(string::const_iterator i=str.begin(); i!=str.end();)
+               x+=get_glyph_advance(dec.decode_char(str, i));
 
        return x;
 }
 
-void Font::draw_string(const string &str) const
+void Font::draw_string(const string &str, Codecs::Decoder &dec) const
 {
-       float x=0;
-       float data[16];
+       Immediate imm((TEXCOORD2, VERTEX2));
+       draw_string(str, dec, imm);
+}
 
-       prepare(data);
+void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
+{
+       if(!tex)
+               throw InvalidState("No texture");
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
-               draw_glyph((unsigned char)*i, data, x);
+       tex->bind();
 
-       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-}
+       float x=0;
+       unsigned count=0;
 
-Font::~Font()
-{
-       if(own_tex)
-               delete tex;
+       pbuilder.begin(QUADS);
+       for(string::const_iterator i=str.begin(); i!=str.end();)
+       {
+               GlyphMap::const_iterator j=glyphs.find(dec.decode_char(str, i));
+               if(j==glyphs.end())
+                       continue;
+
+               create_glyph_vertices(j->second, pbuilder, x);
+               x+=j->second.advance;
+               count+=4;
+       }
+       pbuilder.end();
 }
 
-void Font::prepare(float *data) const
+void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
 {
-       tex->bind();
-
-       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-       glEnableClientState(GL_VERTEX_ARRAY);
-       glTexCoordPointer(2, GL_FLOAT, 4*sizeof(float), data);
-       glVertexPointer(2, GL_FLOAT, 4*sizeof(float), data+2);
+       vbuilder.texcoord(glyph.x1, glyph.y1);
+       vbuilder.vertex(x+glyph.off_x, glyph.off_y);
+       vbuilder.texcoord(glyph.x2, glyph.y1);
+       vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
+       vbuilder.texcoord(glyph.x2, glyph.y2);
+       vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
+       vbuilder.texcoord(glyph.x1, glyph.y2);
+       vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
 }
 
-void Font::draw_glyph(wchar_t code, float *data, float &x) const
+float Font::get_glyph_advance(unsigned code) const
 {
        GlyphMap::const_iterator i=glyphs.find(code);
        if(i==glyphs.end())
-               return;
-
-       const Glyph &glyph=i->second;
+               return 0;
 
-       data[0]=data[12]=glyph.x1;
-       data[1]=data[5]=glyph.y1;
-       data[4]=data[8]=glyph.x2;
-       data[9]=data[13]=glyph.y2;
+       return i->second.advance;
+}
 
-       data[2]=data[14]=x;
-       data[3]=data[7]=glyph.descent;
-       data[6]=data[10]=x+glyph.w;
-       data[11]=data[15]=glyph.h+glyph.descent;
 
-       glDrawArrays(GL_QUADS, 0, 4);
+Font::Loader::Loader(Font &f):
+       font(f),
+       coll(0)
+{
+       init();
+}
 
-       x+=glyph.advance;
+Font::Loader::Loader(Font &f, Collection &c):
+       font(f),
+       coll(&c)
+{
+       init();
 }
 
-Font::Loader::Loader(Font &f):
-       font(f)
+DataFile::Collection &Font::Loader::get_collection()
 {
-       add("texture", &Loader::texture);
-       add("glyph",   &Loader::glyph);
+       if(!coll)
+               throw InvalidState("No collection");
+       return *coll;
 }
 
-void Font::Loader::texture(const string &t)
+void Font::Loader::init()
 {
-       Texture2D *tex=new Texture2D;
-       tex->parameter(GL_GENERATE_MIPMAP, 1);
-       font.tex=tex;
-       tex->image(t);
-       font.own_tex=true;
+       add("default_size", &Font::default_size);
+       add("ascent",  &Font::ascent);
+       add("descent", &Font::descent);
+       add("texture", &Font::tex);
+       add("texture_inline", &Loader::texture_inline);
+       add("glyph",   &Loader::glyph);
 }
 
 void Font::Loader::glyph(unsigned c)
@@ -125,12 +163,21 @@ void Font::Loader::glyph(unsigned c)
        font.glyphs.insert(GlyphMap::value_type(c, gl));
 }
 
+void Font::Loader::texture_inline()
+{
+       RefPtr<Texture2D> tex=new Texture2D;
+       load_sub(*tex);
+       font.tex=tex.release();
+       font.own_tex=true;
+}
+
+
 Font::Glyph::Loader::Loader(Glyph &g):
        glyph(g)
 {
        add("texcoords", &Loader::texcoords);
-       add("size",      &Loader::size);
-       add("descent",   &Glyph::descent);
+       add("size",      &Glyph::w,     &Glyph::h);
+       add("offset",    &Glyph::off_x, &Glyph::off_y);
        add("advance",   &Glyph::advance);
 }
 
@@ -142,11 +189,5 @@ void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
        glyph.y2=y2;
 }
 
-void Font::Glyph::Loader::size(float w, float h)
-{
-       glyph.w=w;
-       glyph.h=h;
-}
-
 } // namespace GL
 } // namespace Msp