From 652cb91c2b6008c4407c16054085f20bf5888c4d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 2 Nov 2008 17:46:18 +0000 Subject: [PATCH] Support embedding textures inside font files Make maketex.py usable as an import Add makefont.py to create fonts from TTF files (needs ttf2png) --- makefont.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ maketex.py | 50 ++++++++++++++++++++++++++++++------------------- source/font.cpp | 20 ++++++++++++++++++++ source/font.h | 4 +++- 4 files changed, 104 insertions(+), 20 deletions(-) create mode 100755 makefont.py diff --git a/makefont.py b/makefont.py new file mode 100755 index 00000000..ac816633 --- /dev/null +++ b/makefont.py @@ -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 " + else: + out=file(sys.argv[2], "w") + out.write(make_font(sys.argv[1])) diff --git a/maketex.py b/maketex.py index 17012c8a..017aa121 100755 --- a/maketex.py +++ b/maketex.py @@ -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 "%sys.argv[0] + else: + out=file(os.path.splitext(sys.argv[1])[0]+".tex", "w") + out.write(make_tex(sys.argv[1])) diff --git a/source/font.cpp b/source/font.cpp index 766f011a..f82b45d8 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -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 tex=new Texture2D; + load_sub(*tex); + font.tex=tex.release(); + font.own_tex=true; +} + Font::Glyph::Loader::Loader(Glyph &g): glyph(g) diff --git a/source/font.h b/source/font.h index b48c6445..a2406b9e 100644 --- a/source/font.h +++ b/source/font.h @@ -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 GlyphMap; const Texture2D *tex; + bool own_tex; float default_size; float ascent; float descent; -- 2.43.0