--- /dev/null
+#!/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]))
# $Id$
-import Image
-import sys
-import os
-
def escape(str):
result=""
for c in 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]))
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
add("ascent", &Font::ascent);
add("descent", &Font::descent);
add("texture", &Font::tex);
+ add("texture_inline", &Loader::texture_inline);
add("glyph", &Loader::glyph);
}
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)
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;
typedef std::map<unsigned, Glyph> GlyphMap;
const Texture2D *tex;
+ bool own_tex;
float default_size;
float ascent;
float descent;