]> git.tdb.fi Git - libs/gl.git/commitdiff
Support embedding textures inside font files
authorMikko Rasa <tdb@tdb.fi>
Sun, 2 Nov 2008 17:46:18 +0000 (17:46 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 2 Nov 2008 17:46:18 +0000 (17:46 +0000)
Make maketex.py usable as an import
Add makefont.py to create fonts from TTF files (needs ttf2png)

makefont.py [new file with mode: 0755]
maketex.py
source/font.cpp
source/font.h

diff --git a/makefont.py b/makefont.py
new file mode 100755 (executable)
index 0000000..ac81663
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+# $Id$
+
+def convert_def(fn):
+       src=file(fn)
+
+       line=src.readline()
+       tw,th,fh,fa,fd=map(int, line.split())
+
+       result="default_size %d;\n"%fh
+       result+="ascent %.3f;\n"%(float(fa)/fh)
+       result+="descent %.3f;\n"%(float(fd)/fh)
+
+       for line in src.readlines():
+               g,x,y,w,h,ox,oy,a=map(int, line.split())
+               result+="glyph %d\n{\n"%g
+               result+="\ttexcoords %f %f %f %f;\n"%(float(x)/tw, float(th-y-h)/th, float(x+w)/tw, float(th-y)/th)
+               result+="\tsize %.3f %.3f;\n"%(float(w)/fh, float(h)/fh)
+               result+="\toffset %.3f %.3f;\n"%(float(ox)/fh, float(oy)/fh)
+               result+="\tadvance %.3f;\n"%(float(a)/fh)
+               result+="};\n"
+
+       return result
+
+def make_font(fn):
+       import maketex
+       import os
+
+       if os.system("ttf2png \"%s\" -o makefont-tmp.png -d makefont-tmp.def -t -p"%sys.argv[1]):
+               raise Exception("Could not execute ttf2png")
+
+       result="texture_inline\n{\n"
+       result+=maketex.make_tex("makefont-tmp.png")
+       result+="};\n"
+       result+=convert_def("makefont-tmp.def")
+
+       os.unlink("makefont-tmp.png")
+       os.unlink("makefont-tmp.def")
+
+       return result
+
+if __name__=="__main__":
+       import sys
+
+       if len(sys.argv)<3:
+               print "Usage: %s <font.ttf> <outfile>"
+       else:
+               out=file(sys.argv[2], "w")
+               out.write(make_font(sys.argv[1]))
index 17012c8a6e7f6d8925dae9ce41cc51aa683c369f..017aa121368057e42a7119ef6973f9a2aadc6ddc 100755 (executable)
@@ -2,10 +2,6 @@
 
 # $Id$
 
-import Image
-import sys
-import os
-
 def escape(str):
        result=""
        for c in str:
@@ -19,18 +15,34 @@ def escape(str):
                        result+=c
        return result;
 
-img=Image.open(sys.argv[1])
-out=file(os.path.splitext(sys.argv[1])[0]+".tex", "w")
-fmt="".join(img.getbands())
-if fmt=="LA":
-       fmt="LUMINANCE_ALPHA"
-elif fmt=="L":
-       fmt="LUMINANCE"
-out.write("storage %s %d %d 0;\n"%(fmt, img.size[0], img.size[1]))
-out.write("min_filter LINEAR;\n")
-out.write("raw_data \"")
-data=list(img.getdata())
-for y in range(img.size[1]):
-       i=(img.size[1]-1-y)*img.size[0]
-       out.write(escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]])))
-out.write("\";\n")
+def make_tex(fn):
+       import Image
+
+       img=Image.open(fn)
+
+       fmt="".join(img.getbands())
+       if fmt=="LA":
+               fmt="LUMINANCE_ALPHA"
+       elif fmt=="L":
+               fmt="LUMINANCE"
+
+       result="storage %s %d %d 0;\n"%(fmt, img.size[0], img.size[1])
+       result+="min_filter LINEAR;\n"
+       result+="raw_data \""
+       data=list(img.getdata())
+       for y in range(img.size[1]):
+               i=(img.size[1]-1-y)*img.size[0]
+               result+=escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
+       result+="\";\n"
+
+       return result
+
+if __name__=="__main__":
+       import sys
+       import os
+
+       if len(sys.argv)<2:
+               print "Usage: %s <image>"%sys.argv[0]
+       else:
+               out=file(os.path.splitext(sys.argv[1])[0]+".tex", "w")
+               out.write(make_tex(sys.argv[1]))
index 766f011a97dc55708b3fb11da395342facfb6a92..f82b45d8ac81fe7bb3ac30b7f70be453b2a720b7 100644 (file)
@@ -18,14 +18,25 @@ namespace GL {
 
 Font::Font():
        tex(0),
+       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;
 }
 
 const Texture2D &Font::get_texture() const
@@ -140,6 +151,7 @@ void Font::Loader::init()
        add("ascent",  &Font::ascent);
        add("descent", &Font::descent);
        add("texture", &Font::tex);
+       add("texture_inline", &Loader::texture_inline);
        add("glyph",   &Loader::glyph);
 }
 
@@ -151,6 +163,14 @@ 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)
index b48c6445bb53a6d4136cd1d3c2b040edbf913c40..a2406b9ee043009824dbe1787fd7a05d6b583038 100644 (file)
@@ -38,11 +38,12 @@ public:
                DataFile::Collection &get_collection();
        private:
                void init();
-               void texture(const std::string &);
                void glyph(unsigned);
+               void texture_inline();
        };
 
        Font();
+       ~Font();
 
        void  set_texture(const Texture2D &);
        const Texture2D &get_texture() const;
@@ -109,6 +110,7 @@ private:
        typedef std::map<unsigned, Glyph> GlyphMap;
 
        const Texture2D *tex;
+       bool     own_tex;
        float    default_size;
        float    ascent;
        float    descent;