summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/fontools/font2afm
blob: b0a60420d2384ca0a1cdf2a2388dd7b7c5c9f614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl

=pod

=head1 NAME

font2afm - create font metrics (in F<afm> format) for (almost) any font file 

=head1 SYNOPSIS

font2afm [options] I<font> ...

font2afm [options] @I<filelist>

=head1 DESCRIPTION

B<font2afm> generates font metrics (in Adobe's F<afm> format) for Type1,
TrueType and OpenType fonts.

B<font2afm> is just a wrapper script around several utilities (B<cfftot1>, 
B<pf2afm>, B<ttf2afm>, B<pfm2kpx> and B<ot2kpx>) that do the real work. 
All these utilities need to be available on your system.

=head1 OPTIONS

=over 4

=item B<-f>

Force overwriting of existing F<afm> files.

=back

=head1 FILES

=over 4

=item I<font>

This can be any Type1 (in either F<pfa> or F<pfb> format), TrueType or 
OpenType (both PostScript- and TrueType-flavored) font.

=item I<filelist>

This should contain a newline-separated list of font filenames.

=back

=head1 SEE ALSO

F<pf2afm> (part of I<GhostScript>), F<ttf2afm> (part of I<pdfTeX>),
F<afm2afm>, F<autoinst>, F<cmap2enc>, F<ot2kpx>, F<pfm2kpx>, F<cfftot1> 
(part of Eddie Kohler's I<LCDF TypeTools>).

=head1 AUTHOR

Marc Penninga <marc@penninga.info>

=head1 HISTORY

=over 12

=item I<2005-04-15>

First version

=item I<2005-04-29>

Improved the documentation

=item I<2005-05-23>

Bugfix.

=item I<2005-07-29>

Added support for PostScript-flavored (F<otf>) OpenType fonts.

=item I<2005-08-08>

Bugfix.

=item I<2010-07-19>

Another small bugfix.

=back

=cut

##############################################################################

use Getopt::Std;
use integer;
use warnings; no warnings qw(uninitialized);

getopts "f", \%options;

$0 =~ s!.*/!!;
die "Usage: $0 fontfile ...\n   or: $0 \@fontlist\n" if @ARGV < 1;

if ($ARGV[0] =~ m!^@(.+)!) {
    open LIST, "<$1" or die "Error: can't open `$1' - $!\n";
    chop(@ARGV = <LIST>);
    map {m!\S+! and $_ = $&} @ARGV;
}

for (@ARGV) {
    if (m!(.+)\.(pfb|pfa|ttf|otf)!) {
        ($base, $ext) = ($1, $2);
    }
    else {
        warn "Warning: unrecognised font file `$_' skipped\n";
        next;
    }
    
    next if -e "${base}.afm" and !$options{f};
    
    if ($ext =~ m!pf(?:a|b)!) {
        if (-e "${base}.pfm") {
            system "pfm2kpx '${base}.pfm'";
        }
        else {
            system "pf2afm '$_'";
        }
        if (-e "${base}.otf") {
            system "ot2kpx '${base}.otf' >'${base}.kpx'";
            open AFM, "<${base}.afm" or 
                    die "Error: can't open `${base}.afm' - $!\n";
            open KPX, "<${base}.kpx" or 
                    warn("Warning: can't open `${base}.kpx' - $!\n"), next;
            {
                local $/;
                $afm = <AFM>;
                $kpx = <KPX>;
            }
	    close AFM;
            open AFM, ">${base}.afm" or 
                    die "Error: can't create `${base}.afm' - $!\n";
		    
	    if ($afm =~ m!^(.+?)(?:StartKernData.+EndKernData\n)?
	    	    	    EndFontMetrics!sx) 
	    {
		print AFM "$1${kpx}EndFontMetrics\n";
	    }
	    else {
	    	warn "Warning: `${base}.afm' seems rotten - please check\n";
	    	print AFM $afm;
	    }
	    unlink "${base}.kpx";
        }
    }
    elsif ($ext =~ m!ttf!) {
        system "ttf2afm -o '${base}.afm' '$_'";
        system "ot2kpx '${base}.ttf' >'${base}.kpx'";
        open AFM, "<${base}.afm" or 
                die "Error: can't open `${base}.afm' - $!\n";
        open KPX, "<${base}.kpx" or 
                warn("Warning: can't open `${base}.kpx' - $!\n"), next;
        {
            local $/;
            $afm = <AFM>;
            $kpx = <KPX>;
        }
	close AFM;
        open AFM, ">${base}.afm" or 
                die "Error: can't create `${base}.afm' - $!\n";

	if ($afm =~ m!^(.+?)(?:StartKernData.+EndKernData\n)?
	    	    	EndFontMetrics!sx) 
	{
	    print AFM "$1${kpx}EndFontMetrics\n";
	}
	else {
	    warn "Warning: `${base}.afm' seems rotten - please check\n";
	    print AFM $afm;
	}
	unlink "${base}.kpx";
    }
    elsif ($ext =~ m!otf!) {
        system "cfftot1 -b -o ${base}.pfb $_";
        $_ = $base . ".pfb";
        redo;
    }
    else {
    	die "Error: you've just found a bug in `$0' - congratulations!\n";
    }
}

__END__