#! /bin/awk -f
# Translate Adobe afm file to TeX pl file.
# usage: aftopl afm-file-name >pl-file-name
# Greg Lee, lee@uhccux.uhcc.hawaii.edu, 12/87
# extensively modified by Mario Wolczko, mario@uk.ac.man.cs.ux, 4/88
BEGIN {
	tan[ 0]=0;        tan[ 1]=.017454;  tan[ 2]=.034919;  tan[ 3]=.052406;  
	tan[ 4]=.069926;  tan[ 5]=.087487;  tan[ 6]=.105102;  tan[ 7]=.122783;  
	tan[ 8]=.140539;  tan[ 9]=.158383;  tan[10]=.176325;  tan[11]=.194379;  
	tan[12]=.212555;  tan[13]=.230867;  tan[14]=.249325;  tan[15]=.267948;  
	tan[16]=.286744;  tan[17]=.305728;  tan[18]=.324918;  tan[19]=.344326;  
	tan[20]=.363969;  tan[21]=.383863;  tan[22]=.404025;  tan[23]=.424473;  
	tan[24]=.445227;  tan[25]=.466306;  tan[26]=.487731;  tan[27]=.509524;  
	tan[28]=.531707;  tan[29]=.554308;  tan[30]=.577349;  tan[31]=.600859;  
	tan[32]=.624867;  tan[33]=.649406;  tan[34]=.674507;  tan[35]=.700205;  
	tan[36]=.726540;  tan[37]=.753552;  tan[38]=.781283;  tan[39]=.809782;  
	tan[40]=.839099;  tan[41]=.869284;  tan[42]=.900401;  tan[43]=.932512;  
	tan[44]=.965686;  tan[45]=.999998;  tan[46]=1.035526; tan[47]=1.072368; 
	tan[48]=1.110610; tan[49]=1.150364; tan[50]=1.191750; tan[51]=1.234894; 
	tan[52]=1.279939; tan[53]=1.327040; tan[54]=1.376380; tan[55]=1.428144; 
	tan[56]=1.482555; tan[57]=1.539859; tan[58]=1.600330; tan[59]=1.664273; 
	tan[60]=1.732044; tan[61]=1.804039; tan[62]=1.880723; tan[63]=1.962604; 
	tan[64]=2.050297; tan[65]=2.144501; tan[66]=2.246033; tan[67]=2.355844; 
	tan[68]=2.475075; tan[69]=2.605080; tan[70]=2.747468; tan[71]=2.904201; 
	tan[72]=3.077671; tan[73]=3.270835; tan[74]=3.487391; tan[75]=3.732019; 
	tan[76]=4.010759; tan[77]=4.331452; tan[78]=4.704597; tan[79]=5.144493; 
	tan[80]=5.671250; tan[81]=6.313687; tan[82]=7.115264; tan[83]=8.144301; 
	tan[84]=9.514216; tan[85]=11.42988; tan[86]=14.30034; tan[87]=19.08075;
	tan[88]=28.63500; tan[89]=57.28469;

	# extra kerning and ligature info is added to these chars
	extra= " exclam question hyphen endash quoteleft quoteright f ";
	# same, except for \tt fonts
	extraf= " exclam question ";

	LIG = "   (LIG O %o O %o)\n"

	print "(DESIGNSIZE R 10.0)"
	print "(COMMENT DESIGNSIZE IS IN POINTS)"
	print "(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)"
  }

/^FontName/ {
	smallcaps = index($2, "SmallCaps");
}

/^FamilyName/	{
	family = substr($0, length("FamilyName  "));
#	if (family ~ /Symbol/) {
#		print "Can't deal with Symbol fonts" >"/dev/tty";
#		exit 1;
#	}
	if (family ~ /Dingbats/ || family ~ /Symbol/)
	  nonchar = 1;
	print "(FAMILY " family ")";
}

/^EncodingScheme/ {	print "(CODINGSCHEME " $2 ")"; }

/^ItalicAngle/	{
	angle  = int($2 + .5); slant = 0;
	if (angle < 0)
	  slant = tan[-angle];
	else
	  slant = -tan[angle];
}

/^IsFixedPitch/	{	fixed = $2; }

/^XHeight/	{	xheight  = $2/1000; }

/^C 32/		 {	if (fixed == "true")
			  space = $5/1000
			else
			  space = ($5 * 1.1)/1000 # this is a frig...
			print "(FONTDIMEN"
			printf("   (SLANT R %1.6f)\n", slant)
			printf("   (SPACE R %1.6f)\n", space)
 			if (fixed == "false") {
			 # ...and this...
			 printf("   (STRETCH R %1.6f)\n", space/2)
			 # ...and this...
			 printf("   (SHRINK R %1.6f)\n", space/3)
			 }
			printf("   (XHEIGHT R %1.6f)\n", xheight)
			# ... and this ...
			print "   (QUAD R 1.0)"
			# ...and this
			if (fixed == "true")
			  printf("   (EXTRASPACE R %1.6f)\n", space)
			else
			  printf("   (EXTRASPACE R %1.6f)\n", space/3)
			print "   )"
			}

/WX/ && $2 >= 0 {

	ch = $2;
	w = $5;
	h = $14;
	d = -$12;
	rp = $13 - w;
	name[$8] = ch;
	c = ch;

	if (c <  48)		  printf("(CHARACTER O %o\n", c);
	if (c >= 48 && c <= 57)   printf("(CHARACTER C %c\n", c);
	if (c >  57 && c < 65)	  printf("(CHARACTER O %o\n", c);
	if (c >= 65 && c <= 90)	  printf("(CHARACTER C %c\n", c);
	if (c >  90 && c < 97)	  printf("(CHARACTER O %o\n", c);
	if (c >= 97 && c <= 122)  printf("(CHARACTER C %c\n", c);
	if (c > 122)		  printf("(CHARACTER O %o\n", c);

	printf("   (CHARWD R %1.6f)\n", w/1000);

	if (h > 0)	  printf("   (CHARHT R %1.6f)\n", h/1000);
	if (d > 0)	  printf("   (CHARDP R %1.6f)\n", d/1000);
	if (rp > 0)	  printf("   (CHARIC R %1.6f)\n", rp/1000);

	print "   )";
}

/^EndCharMetrics/ {
	# all TeX fonts (except DingBats)
	# map !` to upside-down! and ?` to upside-down?
	if (nonchar) next;
	printf("(LIGTABLE\n");
}

/^KPX/	{
	if ($2=="quoteleft" && $3=="quoteleft") next;  # these are ligatured,
	if ($2=="quoteright" && $3=="quoteright") next;# not kerned.
	if (ck != $2) {
		if (ck != "") printf("   (STOP)\n");
		c = name[$2];
		printf("   (LABEL O %o)\n", c);
		ck = $2;
		if ((fixed == "false" && index(extra," "ck" ")) || (fixed == "true" && index(extraf," "ck" "))) {
		  	if (ck == "exclam")
		    		printf(LIG,name["quoteleft"], name["exclamdown"]);
		  	else if (ck == "question")
		    		printf(LIG,name["quoteleft"], name["questiondown"]);
		  	else if (ck == "hyphen")
		      		printf(LIG,name["hyphen"], name["endash"]);
		    	else if (ck == "endash")
		      		printf(LIG,name["hyphen"], name["emdash"]);
		    	else if (ck == "quoteleft")
		      		printf(LIG,name["quoteleft"], name["quotedblleft"]);
			else if (ck == "quoteright")
		      		printf(LIG,name["quoteright"], name["quotedblright"]);
			else if (ck == "f") {
				printf("   (LIG C i O %o)\n",name["fi"]);
				printf("   (LIG C l O %o)\n",name["fl"]);
		  	}
		  	done[ck]=1;
		}

	}
	cm = name[$3];
	printf("   (KRN O %o R %1.6f)\n", cm, $4/1000)
  }

/^EndKernPairs/ {
	printf("   (STOP)\n");
}

/^EndFontMetrics/ {
	if (nonchar) next;

	if (fixed=="true")
		todo= substr(extraf, 2);
	else
		todo= substr(extra, 2);
	while (todo) {
		n = index(todo, " ");
		ck = substr(todo, 1, n-1);
		todo = substr(todo, n+1);
		if (!done[ck]) {
			printf("   (LABEL O %o)\n", name[ck]);
			# this is replica of the code in KPX
		  	if (ck == "exclam")
		    		printf(LIG,name["quoteleft"], name["exclamdown"]);
		  	else if (ck == "question")
		    		printf(LIG,name["quoteleft"], name["questiondown"]);
		  	else if (ck == "f") {
				printf("   (LIG C i O %o)\n",name["fi"]);
				printf("   (LIG C l O %o)\n",name["fl"]);
		  	} else if (fixed == "false") {
		    	    if (ck == "hyphen")
		      	       printf(LIG,name["hyphen"], name["endash"]);
		    	    else if (ck == "endash")
		      	       printf(LIG,name["hyphen"], name["emdash"]);
		    	    else if (ck == "quoteleft")
		      	       printf(LIG,name["quoteleft"], name["quotedblleft"]);
			    else if (ck == "quoteright")
		      	       printf(LIG,name["quoteright"], name["quotedblright"]);
		  	}
			printf("   (STOP)\n");
		}
	}
	printf("   )\n");
}