#!/usr/local/bin/perl
#
# nh2ps_opt 
# - nh2ps hangul, hanja font optimizer
#
# Choi Jun Ho(junker@jazz.snu.ac.kr)
# Narae, CS Dept., SNU
#
# This program is originally from nhppf Netscape print filter
#

$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 "nh2ps_opt - nh2psƮ ȭ \n\n";
        print "  ׻ nh2ps Բ ؾ մϴ\n";
	print "  ) nh2ps aa.txt | nh2ps_opt | lpr\n";
    }else{
	print "nh2ps_opt - nh2ps hangul font optimizing filter\n\n";
        print "  It should be used with nh2ps\n";
	print "  e.g) nh2ps aa.txt | nh2ps_opt | lpr\n";
    }
    exit 0;
}

# Phase 1 - get font loading information
#
# clear up font loading list
#
@hanfont_list=(0,0,0,0,0,0,0,0,0,0);
@hanfont_bold_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);
@hanjafont_bold_list=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

while(<>){
    $line=$_;
    # translate chars
    if (/^(h.?b.?f)([0-9]+) setfont/){
	$type=$1; $number=$2;
	if ($type eq "hbf"){
	    $hanfont_list[$number]=1;
        }
	if ($type eq "hbdf"){
	    $hanfont_bold_list[$number]=1;
        }
        if ($type eq "hjbf"){
	    $hanjafont_list[$number]=1;
        }
        if ($type eq "hjbdf"){
	    $hanjafont_bold_list[$number]=1;
        }
    }

    push(@pass1, $line);
}

@pass2=reverse(@pass1);
# pass 2
#
# don't print unnecessary font loading commands
#
while($_=pop(@pass2)){
# check font definition and print other things
    if (/^\/(h.?b.?f)([0-9]+)/){
        $type=$1; $number=$2;
	if ($type eq "hbf"){
	    if ($hanfont_list[$number]==1){
                print $_;
            }
        }
	if ($type eq "hbdf"){
	    if ($hanfont_bold_list[$number]==1){
                print $_;
            }
        }
        if ($type eq "hjbf"){
	    if ($hanjafont_list[$number]==1){
                print $_;
            }
        }
        if ($type eq "hjbdf"){
	    if ($hanjafont_bold_list[$number]==1){
                print $_;
            }
        }
    }else{
	print $_;
    }
}
