#!/usr/bin/python
################################################################################
# bg5ps.py which use the ttf2ps program to convert the Big5 Coding chinese     #
# text into printable postscript file.  Since it uses true type font, the      #
# quality is much better than using bitmap fonts.                              #
#******************************************************************************#
#
# $Log: bg5ps,v $
# Revision 1.6  2000/02/14 20:46:43  platin
# small fix about option name typo...
#
# Revision 1.5  2000/02/14 20:40:04  platin
#
# Change default config file to /etc/chinese/bg5ps.conf
#
# Revision 1.4  2000/02/14 20:28:35  platin
# Automatic selection of fontName in echo Encoding.
#
# Revision 1.3  2000/02/14 19:07:15  platin
# Adjusting the config files.
#
# Revision 1.2  2000/02/14 18:54:18  platin
#
# Modify to support GB2312.
# FIXME: Must change the range of picking up multibyte chars, LATER!!
#
#
#******************************************************************************#
# bg5ps.py ver. 1.1b2 Date:Oct 1 98                                            #
# Copyright (C) 1998  by Chen-Shan Chin                                        #
# This program is free software; you can redistribute it and/or modify it      #
# under the terms of the GNU General Public License as published by the Free   #
# Software Foundation; either version 2 of the License, or any later version.  #
#                                                                              #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even implied warranty of MERCHANTABILITY of FITTNESS   #
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more        # 
# details.                                                                     #
#                                                                              #
# You should have received a copy of the GNU General Public License along      #
# with this program; if not, write to the Free Sofeware Foundation, Inc., 675  #
# Mass Ave, Cambridge, MA 02139, USA                                           #
################################################################################

import sys,os,string

#Program global variables
true=1
false=0
#wrongChineseCodingError='wrongChineseCodingError'
bg5ChrDict={}
wd=os.path.dirname(sys.argv[0])
if wd=="": wd="."
is_utf8=0

#start default config file segment
#Font and font path
chineseFontPath=wd
fontName="ntu_kai"

#Output only even or odd
oddPages=true
evenPages=true

#size and margins are in unit of pt
size=12.0

#page layout
topMargin=72.0
bottomMargin=72.0
leftMargin=72.0
rightMargin=72.0

#paragraph parameter in pt
lineSpace=6.0
chrSpace=2.0

inputFileName=""
outputFileName=""
configFile=os.path.expanduser("~")+".bg5ps.conf"
ttf2psmPath=wd
ttf2psmoption=""
#end config file segment

#default: no netscape or mpage mode
npsMode="n"

argSetup={"-fp":["chineseFontPath","string"],
	  "-fn":["fontName","string"],
	  "-o":["oddPages","int"],
	  "-e":["evenPages","int"],
	  "-s":["size","float"],
	  "-tm":["topMargin","float"],
	  "-bm":["bottomMargin","float"],
	  "-lm":["leftMargin","float"],
	  "-rm":["rightMargin","float"],
	  "-if":["inputFileName","string"],
	  "-of":["outputFileName","string"],
	  "-ls":["lineSpace","float"],
	  "-cs":["chrSpace","float"],
          "-cf":["configFile","string"],
	  "-en":["Encoding","string"],
          "-nps":["npsMode","string"]}

# 0 ... character never appears in text
# 1 ... character appears in plain ASCII text
# 2 ... character appears in ISO-8859 text
# 3 ... character appears in non-ISO extended ASCII (Mac, IBM PC)
text_chars = [
    #                   BEL BS HT LF    FF CR
    0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, # 0x0X
    #                               ESC
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, # 0x1X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 0x2X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 0x3X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 0x4X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 0x5X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 0x6X
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, # 0x7X
    #             NEL
    3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, # 0x8X
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, # 0x9X
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xaX
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xbX
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xcX
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xdX
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xeX
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, # 0xfX
]

def looks_utf8(fp):
    buf = ""
    for line in fp:
        buf = buf + line

    nbytes = len (buf)
    i = 0
    c = 0
    following = 0
    gotone = 0
    while i < nbytes:
        if (ord(buf[i]) & 0x80) == 0:
            # 0xxxxxxx is plain ASCII
            if text_chars[ord(buf[i])] != 1:
                return 0
        elif (ord(buf[i]) & 0x40) == 0:
            # 10xxxxxx never 1st byte
            return 0
        else:
            if (ord(buf[i]) & 0x20) == 0:
                # 110xxxxx
                c = ord(buf[i]) & 0x1f
                following = 1
            elif (ord(buf[i]) & 0x10) == 0:
                # 1110xxxx
                c = ord(buf[i]) & 0x0f
                following = 2
            elif (ord(buf[i]) & 0x08) == 0:
                # 11110xxx
                c = ord(buf[i]) & 0x07
                following = 3
            elif (ord(buf[i]) & 0x04) == 0:
                # 111110xx
                c = ord(buf[i]) & 0x03
                following = 4
            elif (ord(buf[i]) & 0x02) == 0:
                # 1111110x
                c = ord(buf[i]) & 0x01
                following = 5
            else:
                return 0
            
            n = 0
            while n < following:
                i = i + 1
                if i >= nbytes:
                    return gotone
                if (ord(buf[i]) & 0x80) == 0 or (ord(buf[i]) & 0x40):
                    return 0
                
                c = (c << 6) + (ord(buf[i]) & 0x3f)
                n = n + 1
                
            gotone = 1
        i = i + 1
        
    return gotone

def utf8_length (strings):
    retval = 0
    c = ord (strings[0])

    if c < 128:
        retval = 1
    elif (c & 0xe0) == 0xc0:
        retval = 2
    elif (c & 0xf0) == 0xe0:
        retval = 3
    elif (c & 0xf8) == 0xf0:
        retval = 4
    elif (c & 0xfc) == 0xf8:
        retval = 5
    elif (c & 0xfe) == 0xfc:
        retval = 6
    else:
        sys.stderr.write ("utf8_length: Invalid UTF-8\n")
        retval = 1

    return retval

#
# This 'lang_dependent_file' function will return the best
# lang-dependent file for the configuration.
#
# If you have a fix or suggestion, please send it to
# Yukihiro Nakai <ynakai@redhat.com>
#
def lang_dependent_file(file):
    lang_full=os.environ['LANG']
    lang_file=file+"."+lang_full
    if os.path.exists(lang_file):
      return lang_file

    list=string.splitfields(lang_full, "_")
    lang_file=file+"."+list[0]
    if os.path.exists(lang_file):
      return lang_file

    list=string.splitfields(lang_full, ".")
    lang_file=file+"."+list[0]
    if os.path.exists(lang_file):
      return lang_file

    return file

def argvParser(ps,usage):
    from string import atoi,atof
    rtn={}
    for i in range(1,len(sys.argv),2):
        if sys.argv[i] in ps.keys():
            if ps[sys.argv[i]][1]=="int":
                try:
                    rtn[ps[sys.argv[i]][0]]=atoi(sys.argv[i+1])
                except ValueError:
                    sys.stderr.write("\nValueError on option %s\n" % sys.argv[i])
		    sys.stderr.write(usage)
                    sys.exit(1)

            elif ps[sys.argv[i]][1]=="float":
                try:
                    rtn[ps[sys.argv[i]][0]]=atof(sys.argv[i+1])
                except ValueError:
                    sys.stderr.write("\nValueError on option %s\n" % sys.argv[i])
		    sys.stderr.write(usage)
                    sys.exit(1)

            elif ps[sys.argv[i]][1]=="string":
                rtn[ps[sys.argv[i]][0]]=sys.argv[i+1]
        else:
	    if sys.argv[i]!="-h":
		sys.stderr.write("Wrong option %s\n" % sys.argv[i])
	    sys.stderr.write(usage)
            sys.exit(1)
    return rtn

def getChr16(cc):
    c1=ord(cc[0])
    c2=ord(cc[1])
    return c1*256+c2

def getUTF8Chr(cc):
    length = utf8_length (cc)
    i = 0
    utf = 0
    while i < length:
        utf <<= 8
        utf += ord(cc[i:i+1])
        i += 1
    return utf

def getOffset(cc):
    c1=ord(cc[0])
    c2=ord(cc[1])
    return c1*256+c2

def getUTF8Offset (cc):
    retval = 0
    c = ord(cc[0])
    len = 0
    mask = 0
    if c < 128:
        len = 1
        mask = 0x7f
    elif (c & 0xe0) == 0xc0:
        len = 2
        mask = 0x1f
    elif (c & 0xf0) == 0xe0:
        len = 3
        mask = 0x0f
    elif (c & 0xf8) == 0xf0:
        len = 4
        mask = 0x07
    elif (c & 0xfc) == 0xf8:
        len = 5
        mask = 0x03
    elif (c & 0xfe) == 0xfc:
        len = 6
        mask = 0x01
    else:
        sys.stderr.write ("getUTF8Offset: Invalid UTF-8 %02X\n" % c)
        len = 1
        mask = 0x7f

    retval = c & mask
    i = 1
    while i < len:
        c = ord(cc[i])
        if (c & 0xc0) != 0x80:
            sys.stderr.write ("getUTF8Offset:2: Invalid UTF-8\n")
            break
        retval <<= 6
        retval |= c & 0x3f
        i = i + 1

    return retval

#    if c1 >= 0xC9:
#	c=(c1-0xC9)*157+5401
#        sys.stderr.write(\
#"Warning: Non common character used(%s %s). \n\
#The result may not be right for some fonts.\n" % (hex(c1*256+c2),chr(c1)+chr(c2)))
#    elif c1 >= 0xA4:
#	c=(c1-0xA4)*157
#    else:
#	c=(c1-0xA1)*157+13094
#
#    if c2<128:
#	c=c+c2-64
#    else:
#	c=c+c2-161+63
#    return c

def scanUTF8File():
    for l in bg5FileLines:
        i = 0
        ln = len (l)
        while i < ln:
            length = utf8_length (l[i:])
            if length > 1:
                utfchr = l[i:i+length]
                offset = getUTF8Offset (utfchr)
                if not bg5ChrDict.has_key (utfchr):
                    bg5ChrDict[utfchr] = offset
            i = i + length

def scanFile():
    from string import rstrip
    nl=1
    for l_orig in bg5FileLines:
        l=rstrip(l_orig)
	i=0
        ln=len(l)
	while i < ln:
            if i+1 < ln and \
               (ord(l[i]) >= 161 and ord(l[i]) <= 249) and \
               ((ord(l[i+1]) >=64 and ord(l[i+1]) <=126) or \
                (ord(l[i+1]) >=161 and ord(l[i+1]) <=254)) : # bg5_c
                cnchr=l[i:i+2]
                offset=getOffset(cnchr)
                if not bg5ChrDict.has_key(cnchr):
                    bg5ChrDict[cnchr]=offset
                i=i+1
	    i=i+1

def npsModeScanFile():
    import regex
    sp=regex.compile('[^\]?(\(.*\)[^\]?) show')
    for l in bg5FileLines:
	if sp.search(l)!=-1:
	    cs=eval('""" '+sp.group(1)+' """') # magic code
	    i=0
            ln=len(cs)
	    while i < ln:
                if i+1 < ln and \
                   (ord(cs[i]) >= 161 and ord(cs[i]) <= 249) and \
                   ((ord(cs[i+1]) >=64 and ord(cs[i+1]) <=126) or \
                    (ord(cs[i+1]) >=161 and ord(cs[i+1]) <=254)): #bg5_c
                    cnchr=cs[i:i+2]
                    offset=getOffset(cnchr)
                    if not bg5ChrDict.has_key(cnchr):
                        bg5ChrDict[cnchr]=offset
                    i=i+1
	        i=i+1

def npsModeScanUTF8File():
    import regex
    sp=regex.compile('[^\]?(\(.*\)[^\]?) show')
    for l in bg5FileLines:
	if sp.search(l)!=-1:
	    cs=eval('""" '+sp.group(1)+' """') # magic code
	    i=0
            ln=len(cs)
	    while i < ln:
                length = utf8_length (cs[i:])
                if length > 1:
                    utfchr = cs[i:i+length]
                    offset = getUTF8Offset (utfchr)
                    if not bg5ChrDict.has_key (utfchr):
                        bg5ChrDict[utfchr] = offset
                i = i + length

def genPSF():
    if not os.path.exists(ttf2psmPath+"/ttf2psm"):
	sys.stderr.write("\nError:"+ttf2psmPath+"/ttf2psm doesn't exist\n")
	sys.exit(2)

    if not os.path.exists(fontFileName):
	sys.stderr.write("\nError: Font file %s not found.\n\n" % fontFileName)
	sys.exit(2)

    import tempfile
    tmpfilename=tempfile.mktemp()
    tmpfile=open(tmpfilename,"w")
    for i in bg5ChrDict.keys():
	tmpoffset=bg5ChrDict[i]
	if tmpoffset!=13094: #Big white space, no font data
	    tmpfile.write("%d\n" % tmpoffset)
    tmpfile.write("-1\n")
    tmpfile.close()

    sys.stderr.write("\nrunning "+ttf2psmPath+"/"+ttf2psmcommand+" "+ttf2psmoption+" %s < %s\n\n" % (fontFileName,tmpfilename))
    inpipe=os.popen(ttf2psmPath+"/"+ttf2psmcommand+" "+ttf2psmoption+" %s < %s" % (fontFileName,tmpfilename))
    rtnlist=inpipe.readlines()
#Reported as a bug here for inpipe.close(), comment it. Trying to figure why.
#    inpipe.close()
    os.system("rm %s" % tmpfilename)
    return rtnlist

def genbg5Dict():
    ow=outfile.write
    ow("/bg5dict <<\n")
    for ch in bg5ChrDict.keys():
        if is_utf8 == 0:
            ow("%d" % getChr16(ch))
        else:
            ow("%d" % getUTF8Chr(ch))
        ow("(w%d)\n" % bg5ChrDict[ch])
    ow(">> def\n")

    ow(\
"/dbg5w {/csize currentfont /ScaleMatrix get 0 get def "+\
"gsave "+\
"csize csize pt "+\
"currentpoint translate gsave "+\
"bg5dict exch get cvx exec grestore "+\
"grestore currentpoint exch csize add exch moveto " +\
"} bind def")


def genModShow():
    outfile.write(\
"""
/show {
/str exch def
/i 0 def
str length /ls exch def
{ i 1 add ls ge {exit} if
  str i get /c1 exch def
  str i 1 add get /c2 exch def  
  c1 161 ge c1 249 le and  
  c2 64 ge c2 126 le and  
  c2 161 ge c2 254 le and or and
  {/c c1 256 mul c2 add def c dbg5w /i i 2 add def} 
  { str i 1 getinterval show /i i 1 add def}
  ifelse
}
loop
i 1 add ls eq {str i 1 getinterval show} if
} bind def
""")

def genModUTF8Show():
    outfile.write(\
"""
% <string> <offset> utf8length <int>
/utf8length {
  get
  dup 128 lt {
    /len 1 def
  } {
    dup 224 and 192 eq {
      /len 2 def
    } {
      dup 240 and 224 eq {
        /len 3 def
      } {
        dup 248 and 240 eq {
          /len 4 def
        } {
          dup 252 and 248 eq {
            /len 5 def
          } {
            dup 254 and 252 eq {
              /len 6 def
            } {
              /utf8length errordict /rangecheck get exec exit
            } ifelse
          } ifelse
        } ifelse
      } ifelse
    } ifelse
  } ifelse
  pop len
} bind def
% <string> show -
/show {
  /str exch def
  /ls str length def
  /offset 0 def
  {
    str offset utf8length
    dup 1 eq {
      str offset 1 getinterval show
    } {
      /utfchar 0 def
      0 1 2 index 1 sub {
        /utfchar utfchar 8 bitshift
        3 -1 roll offset add str exch get add def
      } for
      utfchar dbg5w
    } ifelse
    offset add /offset exch def
    offset ls ge {exit} if
  } loop
} bind def
""")

def genPS():
    from string import rstrip, expandtabs
    if is_utf8 == 0:
        scanFile()
    else:
        scanUTF8File()
    if bg5FileLines==[]:
	sys.stderr.write("\nError: Empty input file\n\n")
	sys.exit(2)

    #begin debug code
    #print string.joinfields(bg5FileLines,"")
    #print bg5FileLines
    #print bg5ChrDict
    #end debug code
    ow=outfile.write
    ow(\
"""%!PS-Adobe-2.0
%%Creator: bg5ps
%%Pages: (atend)
%%BeginProlog
% English font
/Courier findfont """+\
"12 scalefont setfont"+\
"""/pt{/tmp exch def 12.0 div tmp 12.0 div scale} bind def /tr{translate} def """+\
"/gl{%.2f 0 tr} def " % (12.0+chrSpace/size*12.0,) +\
"/egl{%.2f 0 tr} def " % (6.0+chrSpace/size*6.0,)+\
"""/m{moveto} def /s{-1 2 m show} def 12 12 pt""")

    for l in genPSF():
	outfile.write(l+" ")

    ow(\
"""
%%EndProlog
%%BeginSetup
%%EndSetup
""")

    sys.stderr.write("Totally %d different Chinese characters have been processed.\n\n" % len(bg5ChrDict))

    newpage=true
    np=1
    gnp=0
    nl=1
    y=0.0

    for l_orig in bg5FileLines:
        if is_utf8 == 0:
            l=rstrip(expandtabs(l_orig))
        else:
            # don't strip whitespace.
            l = l_orig
	i=0
	x=0.0
	dc=("\n","\t","\\","(",")")
	allowed=(oddPages==true and np % 2 ==1) or (evenPages==true and np % 2==0)
	if newpage==true:
	    if allowed:
                ow("%%" + "Page: %d %d\n" % (np,np))
		ow("%%BeginPageSetup\n")
		ow("/pgsave save def ")	    
		ow("gsave /Utopia findfont 9 scalefont setfont ")
                ow("380 48 translate 0 0 moveto ")
                ow("(printed by bg5ps ver. 1.1b2 page: %d) show grestore " % np)
		ow("%.2f %.2f translate " % (leftMargin,792-topMargin-size))
		ow("gsave ")
		ow("%.3f %.3f pt\n" % (size,size))
		ow("%%EndPageSetup\n")
	    newpage=false
        ln=len(l)
	while i < ln:
            if is_utf8 == 0:
                if i+1 < ln and \
                       (ord(l[i]) >= 161 and ord(l[i]) <= 249) and \
                       ((ord(l[i+1]) >=64 and ord(l[i+1]) <=126) or \
                       (ord(l[i+1]) >=161 and ord(l[i+1]) <=254)) : # bg5_c
                    cnchr=l[i:i+2]
                    offset=getOffset(cnchr)
                    if offset==13094: #big space
                        if allowed:
                            ow(" gl")
                        x=x+12.0+chrSpace/size*12.0
                    else:
                        if allowed:
                            ow(" w%d gl" % offset)
                        x=x+12.0+chrSpace/size*12.0
                    i=i+1
                elif not l[i] in ("\n","(",")","\\"):
                    if allowed:
                        ow(" (%s)s egl" % l[i])
                    x=x+6.0+chrSpace/size*6.0
                elif l[i] in ("(", ")","\\"):
                    if allowed:
                        ow(" (\%s)s egl" % l[i])
                    x=x+6.0+chrSpace/size*6.0

                if x >= (610.0-leftMargin-rightMargin-size)*12.0/size:
                    if allowed:
                        ow(" %.2f %.2f tr" % (-x,-12-lineSpace/size*12))
                    y=y+12.0+lineSpace/size*12
                    x=0.0

                i=i+1
            else:
                length = utf8_length (l[i:])
                if length > 1:
                    utfchr = l[i:i+length]
                    offset = getUTF8Offset (utfchr)
                    # FIXME: handling of big space
                    # if offset == bigspace
                    #   if allowed:
                    #     ow(" gl")
                    #   x = x + 12.0 + chrSpace / size * 12.0
                    # else:
                    if allowed:
                        ow (" w%d gl" % offset)
                    x = x + 12.0 + chrSpace / size * 12.0
                    i = i + length - 1
                elif not l[i] in ("\n", "(", ")", "\\"):
                    if allowed:
                        ow (" (%s)s egl" % l[i])
                    x = x + 6.0 + chrSpace / size * 6.0
                elif l[i] in ("(", ")", "\\"):
                    if allowed:
                        ow (" (\%s)s egl" % l[i])
                    x = x + 6.0 + chrSpace / size * 6.0

                if x >= (610.0 - leftMargin - rightMargin - size) * 12.0 / size:
                    if allowed:
                        ow (" %.2f %.2f tr" % (-x, -12 - lineSpace / size * 12))
                    y = y + 12.0 + lineSpace / size * 12
                    x = 0.0

                i = i + 1
	ow("\n")
	nl=nl+1
	if allowed:
	    ow(" %.2f %.2f tr" % (-x,-12-lineSpace/size*12))
	y=y+12+lineSpace/size*12
	if y>(792.0-topMargin-bottomMargin-size)*12.0/size:
	    if allowed:
		ow(" grestore pgsave restore showpage\n")
		gnp=gnp+1
	    nl=1
	    y=0.0
	    newpage=true
	    np=np+1
	x=0.0

    if allowed:
	ow(" pgsave restore showpage\n")
	gnp=gnp+1
    ow("\n%%Trailer\n")
    ow("%%Pages: "+"%d\n" % gnp)
    ow("%%EOF\n")
    sys.stderr.write("%d of %d pages postscript file has been generated.\n" % (gnp,np))

#########################################################################################
# main

if __name__ == '__main__':
    sys.stderr.write("bg5ps version:1.1b2\nCopyright (C) 1998  Chen-Shan Chin\n")
    sys.stderr.write("bg5ps comes with ABSOULETY NO WARRANTY, see GNU General Public License for detail.\n")

    usage=\
"""
bg5ps -fp fontpath -fn fontname 
      -o [1|0] -e [1|0] 
      -s size -ls linespacing -cs charspacing 
      -tm topMargin -bm bottomMargin -lm leftMargin -rm rightMargin
      -if inputfile -of outputfile 
      -cf configFile
      -en encoding
      -nps y/n
      
Size, linespacing, charspacing, and margins are 
numbers in unit of pt(1 pt=1/72 in).

encoding can be big5 or gb2312.

bg5ps -h: print this help
bg5ps -nps y: toggle netscape or mpage mode

Run as a filter:
cat big5text | bg5ps options > outputfile
 
Examples:

bg5ps -en big5 -fn ntu_kai -o 1 -e 0 -s 15 -ls 10 -cs 2 -tm 72 -bm 72 -if test -of test.ps

cat netscape.ps | bg5ps -en big5 -nps y -fn ntu_kai > test.ps

mpage -2 big5.txt | bg5ps -en big5 -nps y > test.ps
"""

#get command options
    cmdOpts=argvParser(argSetup,usage)

#get config file
    if cmdOpts.has_key("configFile"):
        if cmdOpts["configFile"]!="":
            configFile=cmdOpts["configFile"]
            if os.path.isfile(configFile):
                sys.stderr.write("Use configuration file %s.\n" % configFile)
                cf=open(configFile,"r")
                for l in cf.readlines():
                    exec l
                cf.close()
            else:
                sys.stderr.write("Error: Wrong configuration file name'%s'\n" % configFile)
                sys.stderr.write("       Use default configuration instead\n")
    else:    
        sys.stderr.write("\nNo configuration file option is given.\n Trying ~/.bg5ps.conf")
        if os.path.isfile(os.path.expanduser("~")+"/.bg5ps.conf"):
            sys.stderr.write("\n~/.bg5ps.conf found. Use configuration file ~/.bg5ps.conf")
            cf=open(os.path.expanduser("~")+"/.bg5ps.conf","r")
            for l in cf.readlines():
                exec l
            cf.close()
        else:
            sys.stderr.write("\n~/.bg5ps.conf not found.\n Trying /etc/bg5ps.conf.")
            conffile=lang_dependent_file("/etc/bg5ps.conf")
            if os.path.isfile(conffile):
                sys.stderr.write("\n "+conffile+" found. Use configuration file "+conffile)
                cf=open(conffile,"r")
                for l in cf.readlines():
                    exec l
                cf.close()
            else:
                sys.stderr.write("\nNo configuration file found. Use default configuration.")

#get options
    for i in cmdOpts.keys():
        exec i+"=cmdOpts[i]"

#encoding
    ttf2psmcommand="ttf2psm"
    if Encoding=="big5":
        ttf2psmoption="-b"
        l="fontName=fontName_"+Encoding
        exec l
    elif Encoding=="gb2312":
        ttf2psmoption="-g"
        l="fontName=fontName_"+Encoding
        exec l
    else:
		sys.stderr.write("\nError: Wrong encoding '%s'!!\n\n" % Encoding)
		sys.exit(2)

    fontFileName=chineseFontPath+"/%s" % fontName

    if inputFileName=="":
        bg5File=sys.stdin
        sys.stderr.write("\nReading file from stdin....\n")
    else:
        try:
            bg5File=open(inputFileName,"r")
        except IOError:
            sys.stderr.write("\nError: No such file '%s'\n\n" % inputFileName)
            sys.exit(2)

    if outputFileName=="":
        outfile=sys.stdout
    else:
        outfile=open(outputFileName,"w")

    bg5FileLines=bg5File.readlines()
    is_utf8 = looks_utf8(bg5FileLines)
    if is_utf8 != 0:
        ttf2psmoption="-u"

    if npsMode!="y":
        genPS()
    else:
        import regex
        if is_utf8 == 0:
            npsModeScanFile()
        else:
            npsModeScanUTF8File()
        bp=regex.compile("%%EndProlog")
        ow=outfile.write
        for l in bg5FileLines:        
            if bp.search(l)==-1:
                ow(l),
            else:
                ow("%%Creator: bg5ps(nps/mpage mode)\n")
                for ll in genPSF():
                    ow(ll),
                ow("/pt{/tmp exch def 12.0 div tmp 12.0 div scale} bind def\n")
                ow("/w13094 {} def\n")
                genbg5Dict()
                if is_utf8 == 0:
                    genModShow()
                else:
                    genModUTF8Show()
                ow(l),

    bg5File.close()
    outfile.close()
