#!/usr/local/bin/perl
#
# bdfe2i -- Convert a bdf file in ELOT 928 Encoding into a 737 file
#
# 			!!!WARNING!!!
# The 737 sets created (or ISO if a 737 is used as source)
# will NOT be complete. This is because the 2 sets have many disjoint
# characters (esp the ones for box drawing in 737). However for the
# purposes of greek text the fonts produced will be functional.
#
# Copyright (c) 1995 Angelo Haritsis <ah@doc.ic.ac.uk>
#
# DISTRIBUTE FREELY, PROVIDED THE ABOVE COPYRIGHT IS KEPT. NO WARRANTIES.
#
# $Id$
#

$progname = "bdfe2i";
$usage = "$progname: BDF ELOT928(ISO-8859-7) -> 737
Copyright (c) 1995 A. Haritsis
usage: $progname [options] BDFfile [BDFfile-737]
-i2e 	Do opposite: 737 -> ELOT928 (ISO) conversion
-h	help
";


$#ARGV >= -1 || die $usage;

$e2i = 1;	# elot to 737 is default

# process command line options
while ($_ = $ARGV[0], /^-/) {
	shift;
	last if /^--$/;
	/^-i2e/ && ($e2i =  0);
	/-h|-\?/ && die $usage;
	die "$progname: illegal option\n\n$usage";
}

open(IN, $ARGV[0]) || open(IN, "<&STDIN") || die $usage;
open(OUT, $ARGV[1]) || open(OUT, ">&STDOUT") || die $usage;

$char_min = 100000;
$char_max = 0;
$in_char = 0;
while (<IN>) {
	if (! $in_char) {
		/^AVERAGE_WIDTH\s+(\d+)/ && ( $f_avgw = $1);
		/^CHARS\s+(\d+)/ && ( $f_nchars = $1, next );
		if (/^STARTCHAR/) {
			$in_char = 1;
			next;
		}
		/^ENDFONT/ && last;
		if ($e2i) {
			s/iso8859-7/grpc-737/ig;
			s/CHARSET_REGISTRY "ISO8859"/CHARSET_REGISTRY "GRPC"/ig;
			s/CHARSET_ENCODING "7"/CHARSET_ENCODING "737"/ig;
		} else {
			s/grpc-737/iso8859-7/ig;
			s/CHARSET_REGISTRY "GRPC"/CHARSET_REGISTRY "ISO8859"/ig;
			s/CHARSET_ENCODING "737"/CHARSET_ENCODING "7"/ig;
		}
		print OUT;
	} else {		# in char def - collect definitions
		chop;
		/^ENCODING\s+(\d+)/ && ($char_now = $1);
		#print "IN: [$char_now]: $_\n";
		/^SWIDTH\s+(\d+)\s+(\d+)/ && do {
			$sx = $1; $sy=$2;
			$swidth[$char_now] = "$sx $sy";
		};
		/^DWIDTH\s+(\d+)\s+(\d+)/ && do {
			$dx = $1; $dy=$2;
			$dwidth[$char_now] = "$dx $dy";
		};
		/^BBX\s+([-+\d]+)\s+([-+\d]+)\s+([-+\d]+)\s+([-+\d]+)/ && do {
			$x1 = $1; $y1 = $2; $x2 = $3; $y2 = $4;
			$bbox[$char_now] = "$x1 $y1 $x2 $y2";
		};
		if (/^BITMAP/ .. /^ENDCHAR/) {
			$bmap[$char_now] .= "\n$_";
		}
		/^ENDCHAR/ && ($in_char = 0);
		$char_now < $char_min && ($char_min = $char_now);
		$char_now > $char_max && ($char_max = $char_now);
	}
	#print;
}

# scan char definitions and copy to the equivalent new locations
for ($c = $char_min; $c <= $char_max; $c++) {
	$res_c = pack("C", $c);
	if ($e2i) {
		$res_c =~ y/ٶڼۿ/送/;
	} else {
		$res_c = y/送/ٶڼۿ/;
	}

	$res_c = unpack("C", $res_c);
	#print OUT "$res_c takes glyph of $c\n";
			
	if ($swidth[$c]) {		# use chars we have a def for
		if ($res_c != $c) {
			$res_swidth[$res_c] = $swidth[$c];
			$res_dwidth[$res_c] = $dwidth[$c];
			$res_bbox[$res_c] = $bbox[$c];
			$res_bmap[$res_c] = $bmap[$c];
		} elsif (!$res_swidth[$res_c]) { # leave it alone if defined!
			$res_swidth[$c] = $swidth[$c];
			$res_dwidth[$c] = $dwidth[$c];
			$res_bbox[$c] = $bbox[$c];
			$res_bmap[$c] = $bmap[$c];
		}
	}
}

# count the defined chars in new set
$res_nchars = 0;
for ($c = 0; $c <= 255; $c++) {
	$res_nchars++ if $res_swidth[$c];
}
print OUT "CHARS $res_nchars\n";

# now print the char defs in the NEW set.
for ($c = 0; $c <= 255; $c++) {
	next if !$res_swidth[$c];
	$co = sprintf("%03o", $c);
	print OUT <<EOF;
STARTCHAR C$co
ENCODING $c
SWIDTH $res_swidth[$c]
DWIDTH $res_dwidth[$c]
BBX $res_bbox[$c]$res_bmap[$c]
EOF
}
print OUT "ENDFONT\n";

close IN, OUT;
#print "NUM=$f_nchars AVW=$f_avgw\n";

sub a_sub {
	local ($arg1, $arg2) = @_;
}
