#!/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 + "/alpha/zh/";
FINALDIR = DIR + "/IMAGES/zh/";

#default
FONTSYMBOLSTRING = "tex_mojimenu_zh_%3.3d" 
FONTFILESTRING = "zh_mojimenu_%3.3d"
INFOFILEPREFIX = "menu_" 
FONTFILENAME = "abcmenus.RGB"
TEXLISTMACRO = "MENUFONT_TEXLIST(zh_%3.3d)"
TEXLISTSTRING = "TexList_menu_zh_"
stupidCompression = 0;

if ( len( sys.argv ) >= 2 and sys.argv[1]=="4I" ):
    FONTSYMBOLSTRING = "tex_moji4_zh_%3.3d" 
    FONTFILESTRING = "zh_moji4_%3.3d"
    INFOFILEPREFIX = "abc4_" 
    FONTFILENAME = "abc4s.RGB"
    TEXLISTMACRO = "ALPHABET4_TEXLIST(zh_%3.3d)"
    TEXLISTSTRING = "TexList_ABC4_zh_"

if ( len( sys.argv ) >= 2 and sys.argv[1]=="ABC" ):
    stupidCompression = 1;
    FONTSYMBOLSTRING = "tex_moji_zh_%3.3d" 
    FONTFILESTRING = "zh_moji_%3.3d"
    INFOFILEPREFIX = "abcs_" 
    FONTFILENAME = "abcs.RGB"
    TEXLISTMACRO = "ALPHABET_TEXLIST(zh_%3.3d, %s)"
    TEXLISTSTRING = "TexList_ABC_zh_"
    
def getCommandOutput( command ):
    print ( command )
    temp = os.popen( command, "r" );
    lines = temp.readlines();
    temp.close();
    return lines;


def execute( command ):
    print ( command );
    if ( os.system( command ) != 0 ):
        print ("ERROR! on command: " + command );
        sys.exit(-1);

# convert the file

execute("convert sgi:" + FONTSOURCE + FONTFILENAME+ " rgb:menu_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.

FONTSIZEX = 16
FONTSIZEY = 16

f = open( "menu_font_raw.rgb" );

count = 0;
size = []

while( 1  ):
    input = f.read( FONTSIZEX * FONTSIZEY * 3 );

    if ( input == "" ): break

    finalFilename = ( (FONTFILESTRING + ".tex") % count );

    texName = ( FONTSYMBOLSTRING % count );

    a = open( "tmp", "w+");
    a.write( input );
    a.close();

    if ( stupidCompression ):
        execute( "genPPMfromRGB.pl tmp tmp.ppm 16 16; convert ppm:tmp.ppm rgba:tmp.rgba")
        execute(  "rgba2i8.pl tmp.rgba tmp.i8; $GAMEROOT/../tools/compress/sliutil -cm  tmp.i8 > tmp.sli" );
        lines = getCommandOutput( "wc tmp.sli" );        
        tokens = string.split ( lines[ 0 ]  )
        size.append( tokens[ 2 ] );
        execute( "raw2tex.pl tmp.sli "+ FINALDIR + finalFilename + " " + texName + " ");
    else:
        execute( "genPPMfromRGB.pl tmp tmp.ppm " + str( FONTSIZEX ) + " " + str( FONTSIZEY ) + "; convert tmp.ppm sgi:" + finalFilename + ".rgb");
        execute(  "$ROOT/usr/sbin/rgb2c -m " + texName + " -F -s 4 -f I " + finalFilename + ".rgb > " + FINALDIR + finalFilename );


    count = count+1;

f.close();    

# Assemble a list of font files in the a1_fontlist.c file

f = open( FINALDIR + INFOFILEPREFIX + "fontlist.c", "w+" );

for i in range( 0, count ):    
    f.write( ("#include \"" + FONTFILESTRING +".tex\"\n") % i )
f.close();

# Generate the list of textures that point to this include

f = open( FINALDIR + INFOFILEPREFIX + "texlist.c", "w+" );

for i in range( 0, count ):
    if ( stupidCompression ):
        f.write(( TEXLISTMACRO +";\n") % ( i, size[ i ] ) );    
    else:
        f.write(( TEXLISTMACRO +";\n") % ( i ) );    
f.close();

# Generate the alphabet thing

f = open( FINALDIR + INFOFILEPREFIX +  "letterList.c","w+" )

for i in range( 0, count ):
    f.write( (" " + TEXLISTSTRING +"%3.3d, \n") % i )

f.close()

# now make the giant list of externs

f = open( FINALDIR + INFOFILEPREFIX +  "externlist.c", "w+")

for i in range( 0, count ):
    f.write( (FONTSYMBOLSTRING +"[],\n") % i )
f.close();


#---
# Not used here

# 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 ( FINALDIR + INFOFILEPREFIX + "width.c", "w+" );

for i in range( 0, count ):
        f.write( "16,")
f.close();    
    
