#!/usr/bin/python

# This script takes a font image, then chews on it and outputs a
# .c file.

import sys, string, os, re, tempfile

def execute( command ):
    print ( command );
    os.system( command );

if ( len( sys.argv ) < 7 ):
    print ("Usage: createFontFiles.py infile outfile charXsize charYsize columns rows textureName")
    sys.exit( -1 )

inFile = sys.argv[1]
outFile = sys.argv[2]
charX = string.atoi( sys.argv[3] )
charY = string.atoi( sys.argv[4] )
columns = string.atoi( sys.argv[5] )
rows = string.atoi( sys.argv[6] )
textureName = sys.argv[7]

s = " "

for j in range( 0, rows ):
   for i in range (0, columns):
       filename = "/tmp/font"+str(i)+"_"+str(j)+".sgi";
       execute("/usr/X11R6/bin/convert -crop "+str( charX )+"x"+str( charY )+"+"+str( charX*i )+"+"+str( charY*j )+" sgi:"+inFile+" "+filename)
       s = s + filename + " "

execute("/usr/X11R6/bin/convert -append " + s + " /tmp/tmp.sgi")

execute("$ROOT/usr/sbin/rgb2c -s 8 -F -f IA -m " + textureName + " /tmp/tmp.sgi > " + outFile )
