#!/usr/local/bin/perl
###############################################################################
##                                                                           ##
## 	kdraw 2.6 to idraw 3.0.1j $@%U%!%$%kJQ49%D!<%k(J '92/1/30		     ##
##                                                                           ##
	$version="3.0";
##                                                                           ##
	$usage=	"Usage: kdraw2idraw [-d] <input file> <output file>\n" .
		"	-d   debug option\n";
##                                                                           ##
###############################################################################
##                                                                           ##
##        Copyright (C) 1992, FUJITSU LIMITED (Kiyoshi Hayashi)              ##
##          $@O"Mm@h!'(J $@IY;NDL3t<02q<R!!8r0l%=(J) $@Bh0l3+H/2](J                      ##
##		 $@NS(J	$@7i(J                                                   ##
##	    hayashi@koukan.fujitsu.co.jp                                     ##
##                                                                           ##
## $@1DMx$rL\E*(J$@$H$7$J$$8B$j!"$3$N%=!<%9$N$$$+$J$kJ#<L!$:FG[I[(J, $@2~JQ!$=$@5$r(J    ##
## $@5vBz$7$^$9!#$?$@$7!"$=$N:]$K$O(J$@!"I,$:$3$NItJ,$r;D$7$F$/$@$5$$!#(J            ##
## $@$J$*!"K\%D!<%k$N;HMQ$K$h$C$F@8$8$?B;32$K$D$$$F$OJ](J$@>Z$7$+$M$^$9!#(J          ##
##                                                                           ##
###############################################################################
#
if ($ARGV[0] eq '-d') { shift; $dbg="yes"; }
die "\n\t$usage\007\n\n" if (($ARGV[0] eq '') || ($ARGV[1] eq '') || (substr($ARGV[0],0,1) eq '-'));
#
# $@4D6-JQ?t!'4A;z%U%)%s%HJQ49%F!<%V%k(J
$descf="/usr/local/lib/iv-fontdesc" unless ($descf=$ENV{'IV_FONTDESC'}); 
open(FONTDESC, $descf) || die "cannot open iv fontdesc file $descf\007\n";
@fontdesc=grep(/^[A-Z]/,<FONTDESC>);close(FONTDESC);
print "\nIV_FONTDESC = \"$descf\"\n\n" if ($dbg);
#
for ($i=0;$i<=$#fontdesc;$i++) { 
	chop(@fontdesc[$i]); @fontdesc[$i]=~tr/\t/ /d; 
	(@orf[$j],@ors[$j],@ivf[$j],@psf[$j],@pss[$j])=split(/[ ]* /,@fontdesc[$i]);
	if((@orf[$j] ne '') && (@ors[$j] > 0) && (@ivf[$j] ne '') && 
		(@psf[$j] ne '') && (@pss[$j] > 0)) {
		if ((grep(/^@orf[$j] @psf[$j]$/,@cvtfont))[0] eq '') {
			push(@cvtfont,"@orf[$j] @psf[$j]");
		}
		$j++;
	}
}
die "\nThere is no font conversion description!\n" .
	"Check your fontdesc file.\007\n\n" unless $j;
;
if ($dbg) {
	for ($i=0;$i<$j;$i++) {
		print "@orf[$i]  @ors[$i]\t @ivf[$i]\t @psf[$i]  @pss[$i]\n"; 
	}
} 
open(IN,$ARGV[0]) || die "cannot open input file $ARGV[0]\007\n";
open(OUT,">$ARGV[1]") || die "cannot open output file $ARGV[1]\007\n";
while(<IN>){ chop;
	if (substr($_,0,11) eq "%%Creator: ") { # kdraw or idraw ?
	} elsif (substr($_,0,16) eq "%%DocumentFonts:"){
		@font=split(/ /,substr($_,17));
		for ($i=0;$i<=$#font;$i++) { 	# kdraw$@%U%)%s%HJQ49%k!<%k=PNO(J
			@tfont=grep(/^@font[$i] /,@cvtfont);
			undef @target;
			foreach $i (@tfont) {
				($or,$ps)=split(/ /,$i); push(@target,$ps);
			}
			push(@docfont,@target);
		}
		$_=~s/Ryumin-Light-H//; 	# $@%3%a%s%HIt%U%)%s%HL>JQ49(J
		$_=~s/GothicBBB-Medium-H//;
		print OUT "%%Creator: kdraw2idraw(ver $version)\n";
		push(@docfont,split(/ /,substr($_,17)));
		foreach $i (@docfont) { $doc{$i}="ki"; }
		@docfonts=sort keys %doc;
                print OUT "%%DocumentFonts: @docfonts\n"; 
	} elsif ($_ eq "Begin %I KText"){	# $@4A;z%F%-%9%HJQ49(J
		$kt=1; print OUT "Begin %I Text\n";
	} elsif ($_ eq "Begin %I Text"){	# $@1Q;z%F%-%9%HJQ49(J
		$et=1; print OUT "$_\n";
	} elsif (($kt + $et) > 0) {
		if ($_ eq "End") {
			$kt=$et=0; print OUT "End\n";
		} elsif (substr($_,(length($_) - 5),5) eq " SetF") { 
			$_=~tr/^\///d; undef $match;	# $@%U%)%s%HDj5A$NJQ49(J
			for ($i=0;$i<=$#orf;$i++) {
				if ($_ eq "@orf[$i] @ors[$i] SetF"){
					print OUT "%I f @ivf[$i]\n";
			        	print OUT "@psf[$i] @pss[$i] SetF\n";
					$match="yes";
				}
			}
			unless ($match) {
				if ($et >0) {
					print OUT "$ef\n$_\n";
				} else {
					die "no $psfont in fontdesc file\007\n";
				}
			}
		} elsif (substr($_,0,4) eq "%I k"){
		} elsif (substr($_,0,4) eq "%I f"){ 	# IV$@%U%)%s%HL>Dj5A(J
	                $ef=$_;
		} elsif (($kt > 0) && (substr($_,0,1) eq "<")) { 
			# $@4A;z%3!<%IJQ49(J JIS -> EUC
			print OUT "(";
			$jis=substr($_,1,(length)-2);
			for ($i=0; $i < length($jis); $i=$i + 2 ) {
				if ($dbg) { $fmt="%c"; } else { $fmt="\\%o"; }
		               	printf OUT ($fmt,(hex(substr($jis,$i,2)) + 0x80));
		        }
			print OUT ")\n";
		} else {
			print OUT "$_\n";
		}
	} elsif ($_ eq "Begin %I RRect"){	# $@3Q$N4]$$;M3Q$rJD6JLL$K$9$k(J
		print OUT "Begin %I CBSpl\n";
	} else {
		print OUT "$_\n" if (substr($_,0,4) ne "%I k");
	}
}
close(IN);close(OUT);
