#!/usr/bin/python

# This script converts the giant Chinese font picture

import sys, string, os, re

DIR = os.environ['GAMEROOT'];
ASSETDIR = DIR + "/i10n/assets/"
TOOLSDIR = DIR + "/i10n/tools/";
ROOT = os.environ['ROOT'];

FONTSOURCE = ASSETDIR + "/zh/a1_font/";
FINALDIR = DIR + "/select/a1_font/zh/";


def execute( command ):
    print ( command );
    os.system( command );

# convert the file

execute("/usr/X11R6/bin/convert sgi:" + FONTSOURCE +"mk_font.rgb rgb:mk_font_raw.rgb" );

# first, subdivide the fonts

# basically, grab every 26*16*3 bytes, write them out in a file, the
# run the font converter on it. 

f = open( "mk_font_raw.rgb" );

count = 0;

while( 1  ):
    input = f.read( 26 * 16 * 3 );

    if ( input == "" ): break

    finalFilename = ( "a1_zh_%3.3d.c" % count );

    texName = ( "a1_zh_%3.3d" % count );

    a = open( "tmp", "w+");
    a.write( input );
    a.close();

    execute( TOOLSDIR + "/raw2font.pl tmp " + FINALDIR + finalFilename + " " + texName );

    count = count+1;

f.close();    

# Assemble a list of font files in the a1_fontlist.c file

f = open( FINALDIR + "a1_fontlist.c", "w+" );

for i in range( 0, count ):
    f.write("#include \"a1_zh_%3.3d.c\"\n" % i )
f.close();

# Generate the list of textures that point to this include

f = open( FINALDIR + "a1_texlist.c", "w+" );

for i in range( 0, count ):
    f.write("TexBlock TexList_zh_%3.3d_[] = {\n\t{K_AFONT,(unsigned short *)a1_zh_%3.3d,26,16,  0,  0},\n\t{0,(unsigned short *)0,0,0,0,0}};\n" % ( i, i ) );
    
f.close();

# Generate the alphabet thing

f = open( FINALDIR + "a1_letterList.c","w+" )

for i in range( 0, count ):
    f.write(" TexList_zh_%3.3d_, \n" % i )

f.close()

# now make the giant list of externs

f = open( FINALDIR + "a1_externlist.c", "w+")

for i in range( 0, count ):
    f.write("extern TexBlock TexList_zh_%3.3d_[];\n" % i )
f.close();

# now make the other giant list of externs
f = open( FINALDIR + "a1_externlist_raw.c", "w+")

for i in range( 0, count ):
    texName = ( "a1_zh_%3.3d" % i );
    
    f.write("extern unsigned char %s[];\n" % texName )
f.close();

# and, finally, make the Giant List Of Widths

f = open ( DIR + "zh_font_width.c", "w+" );

for i in range( 0, count ):
    if ( i > 6):
        f.write("22,")
    else:
        f.write( "22,")


f.close();    
    
