#!/usr/local/bin/perl
# 
# Netscape 3.0 Hangul Print Filter - for using only "Print" menu
#
# Choi Jun Ho(junker@vishnu.snu.ac.kr)
# Narae, SNU
#
# History
#   96/10/30 version 0.5 only printing Hangul
#   96/10/31 version 0.6 including Hanja
#   96/10/31 version 1.0 optimizing fonts
#   96/12/5  version 1.1 adjust fixed-width fonts
#            multiply 0.8 to courier-font size
#   97/1/29  version 1.2 adjust font-style to select automatically
# 

# default hangul and hanja font and style
$hanfont="Myoungjo";
%hanfontstyle=("Myoungjo", "Regular" ,
               "Gothic", "Regular" ,
               "Graphic", "Regular" ,
               "Gungseo", "Bold" ,
               "Mokgak", "Bold" ,
               "Yetgul", "Bold");

$hanjafont="Myoungjo";
$nohanja=0; # Hanja use is enabled

# option processing
if ($ARGV[0] =~ /^(-h$|-help|--help)/){
    if ($ENV{'LANG'} =~ /^(ko|ko_KR|korean|ko_KR.euc)/){
	print "nhppf - Netscape 3.0 ݽ ѱ   v1.2\n";
        print "        Print()޴ ϼ.\n\n";
	print ": nhppf [-hanfont ۲̸] [-style Ÿ]\n"; 
	print "             [-nohanja] [-hanjafont ۲̸] \n";
	print "       -hanfont ۲̸   ϳ   ֽϴ(⺻ Myoungjo)\n";  
	print "             Myoungjo, Gothic, Graphic, Gungseo, Mokgak, Yetgul\n";
	print "       -hanjafont ۲̸   ϳ   ֽϴ(⺻ Myoungjo)\n";
	print "             Myoungjo, Gothic\n";
	print "       -nohanja ó  ʽϴ.\n";
	print "              ɼ Ǹ -hanjafont ȿ ϴ.\n";
	print "        ǥ ɴϴ.  ͸ ϴ  Ϲ\n";
	print "        ݽ \"Print\" ¸ ʵ忡  \n";
	print "        Դϴ.\n";
	print "       ) nhppf -hanfont Gothic | lpr\n";
    }else{
	print "nhppf - Netscape 3.0 Hangul Print Filter v1.2\n";
	print "        for using only Print menu\n\n";
	print "Usage: nhppf [-hanfont fontname] [-style fontstyle]\n";
	print "             [-nohanja] [-hanjafont fontname] file\n";  
	print "       -hanfont fontname can be any of the following:(default Myoungjo)\n";
	print "             Myoungjo, Gothic, Graphic, Gungseo, Mokgak, Yetgul\n";
	print "       -hanjafont fontname can be any of the following:(default Myoungjo)\n";
	print "             Myoungjo, Gothic\n";
	print "       -nohanja  will not generate hanja processing.\n";
	print "             -hanjafont have no effect if -nohanja is defined.\n";
	print "       Result can be redirected into printer directly.\n";
	print "       Typical use is in Print Command field in \"Print\" command.\n";
	print "       eg) nhppf -hanfont Gothic | lpr\n";
    }
    exit 0;
}
if ($ARGV[0] =~ /^-hanfont/){
    $hanfont=$ARGV[1];
    
    shift; shift;
}
if ($ARGV[0] =~ /^-hanjafont/){
    $hanjafont=$ARGV[1];
    shift; shift;
}
if ($ARGV[0] =~ /^-style/){
    $hanfontstyle=$ARGV[1];
    shift; shift;
}
if ($ARGV[0] =~ /^-nohanja/){
    $nohanja=1;
    shift;
}

# Phase 1 - convert chars and get font loading information
#
# clear up font loading list
@hanfont_list=(0,0,0,0,0,0,0,0,0,0);
@hanjafont_list=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

while(<>){
    # translate chars
    if (/^[0-9]+ of/){
	$font=$_;
	chomp($font);
	$hline=<>;
	chomp($hline);
	$hline=~s/<(....+)> show/\1/;
	$nc=length($hline)/4;
	for($i=0; $i<$nc; $i++){
	    # split chars
	    $ch1=hex(substr($hline,$i*4,2));
	    $ch2=hex(substr($hline,$i*4+2,2));

	    # in case of Hangul char
	    if ($ch1>=0xb0 && $ch1<=0xc8){
		# calculate location
		$pos=($ch1-0xb0)*94+($ch2-0xa1);
		# font
		$fn=65+$pos/256;
		# loc
		$loc=$pos%256;
		# print out
		push(@pass1,
		     sprintf("%s%c <%02x> show", $font, $fn, $loc).
		     " % ".
		     sprintf("%c%c\n", $ch1, $ch2));
		# check font loading
		$hanfont_list[$fn-65]=1;
	    }

	    # in case of Hanja char
	    if ($nohanja!=1){
		if ($ch1>=0xca && $ch1<=0xfd){
		    # calculate location
		    $pos=($ch1-0xca)*94+($ch2-0xa1);
		    # font
		    $fn=65+$pos/256;
		    # loc
		    $loc=$pos%256;
		    # print out
		    push(@pass1, 
			 sprintf("%sh%c <%02x> show", $font, $fn, $loc).
			 " % ".
			 sprintf("%c%c\n", $ch1, $ch2));
		    # check font loading
		    $hanjafont_list[$fn-65]=1;
		}
	    }
	}
    }else{
        /(^[0-9]+) f([0-7])/;
        $fontsize=$1;
        if ($2>=4 && $2<=7){ # Courier fonts
            push(@pass1,
                 sprintf("%f f%d", $fontsize*0.8, $2));
        }else{
	   push(@pass1, $_);
        }
    }
}

@pass2=reverse(@pass1);
# pass 2
#

while($_=pop(@pass2)){
# search header and print
    if (/^\/of /){
	for($i=ord('A'); $i<=ord('J'); $i++){
	    if ($hanfont_list[$i-65]==1){
		print "/of".chr($i)." { /Hangul${hanfont}".chr($i)."-$hanfontstyle{$hanfont} findfont exch scalefont setfont } bind def\n";
	    }
	}
	if ($nohanja!=1){
	    for($i=ord('A'); $i<=ord('T'); $i++){
		if ($hanjafont_list[$i-65]==1){
		    print "/ofh".chr($i)." { /Hanja${hanjafont}".chr($i)."-Regular findfont exch scalefont setfont } bind def\n";
		}
	    }
	}
    }else{
	print $_;
    }
}
