Work in progress: new PostScript/PDF generator back end
[platform/upstream/nasm.git] / doc / afmmetrics.pl
1 #!/usr/bin/perl
2 #
3 # Parse AFM metric files
4 #
5
6 @widths = ((undef)x256);
7
8 while ( $line = <STDIN> ) {
9     if ( $line =~ /^\s*FontName\s+(.*)\s*$/ ) {
10         $fontname = $1;
11     } elsif ( $line =~ /^\s*StartCharMetrics\b/ ) {
12         $charmetrics = 1;
13     } elsif ( $line =~ /^\s*EndCharMetrics\b/ ) {
14         $charmetrics = 0;
15     } elsif ( $line =~ /^\s*StartKernPairs\b/ ) {
16         $kerndata = 1;
17     } elsif ( $line =~ /^\s*EndKernPairs\b/ ) {
18         $kerndata = 0;
19     } elsif ( $charmetrics ) {
20         @data = split(/\s*;\s*/, $line);
21         undef $charcode, $width, $name;
22         foreach $d ( @data ) {
23             @dd = split(/\s+/, $d);
24             if ( $dd[0] eq 'C' ) {
25                 $charcode = $dd[1];
26             } elsif ( $dd[0] eq 'WX' ) {
27                 $width = $dd[1];
28             } elsif ( $dd[0] eq 'N' ) {
29                 $name = $dd[1];
30             }
31         }
32         if ( defined($charcode) && $charcode >= 0 && $charcode < 256 ) {
33             $charcodes{$name} = $charcode;
34             $widths[$charcode] = $width;
35         }
36     } elsif ( $kerndata ) {
37         @data = split(/\s+/, $line);
38         if ( $data[0] eq 'KPX' ) {
39             if ( defined($charcodes{$data[1]}) &&
40                  defined($charcodes{$data[2]}) &&
41                  $data[3] != 0 ) {
42                 $kernpairs{chr($charcodes{$data[1]}).
43                            chr($charcodes{$data[2]})} = $data[3];
44             }
45         }
46     }
47 }
48
49 sub qstr($) {
50     my($s) = @_;
51     my($o,$c,$i);
52     $o = '"';
53     for ( $i = 0 ; $i < length($s) ; $i++ ) {
54         $c = substr($s,$i,1);
55         if ( $c lt ' ' || $c gt '~' ) {
56             $o .= sprintf("\\%03o", ord($c));
57         } elsif ( $c eq "\'" || $c eq "\"" || $c eq "\\" ) {
58             $o .= "\\".$c;
59         } else {
60             $o .= $c;
61         }
62     }
63     return $o.'"';
64 }
65
66 $psfont = $fontname;
67 $psfont =~ s/[^A-Za-z0-9]/_/g;
68
69 print "%PS_${psfont} = (\n";
70 print "  name => \'$fontname\',\n";
71 print "  widths => [\n";
72 for ( $i = 0 ; $i < 256 ; $i += 8 ) {
73     print "    ";
74     for ( $j = 0 ; $j < 8 ; $j++ ) {
75         printf("%5d", $widths[$i+$j]);
76         print ',' if (($i+$j) < 255);
77         print ' ' if ($j < 7);
78     }
79     print "\n";
80 }
81 print "  ],\n";
82 print "  kern => {";
83 $lw = 100000;
84 foreach $kp ( keys(%kernpairs) ) {
85     $ss = sprintf('%s => %d, ', qstr($kp), $kernpairs{$kp});
86     $lw += length($ss);
87     if ( $lw > 72 ) {
88         print "\n    ";
89         $lw = 4 + length($ss);
90     }
91     print $ss;
92 }
93 print "  }\n";
94 print ");\n";
95
96