| 1 |
#! /usr/bin/env perl |
| 2 |
|
| 3 |
=begin COPYRIGHT |
| 4 |
|
| 5 |
---------------------------------------------------------------------------- |
| 6 |
|
| 7 |
Copyright (C) 2005-2026 Marc Penninga. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU General Public License |
| 11 |
as published by the Free Software Foundation, either version 2 |
| 12 |
of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to |
| 21 |
Free Software Foundation, Inc., |
| 22 |
59 Temple Place, |
| 23 |
Suite 330, |
| 24 |
Boston, MA 02111-1307, |
| 25 |
USA |
| 26 |
|
| 27 |
---------------------------------------------------------------------------- |
| 28 |
|
| 29 |
=end COPYRIGHT |
| 30 |
|
| 31 |
=cut |
| 32 |
|
| 33 |
use strict; |
| 34 |
use warnings; |
| 35 |
|
| 36 |
use File::Basename; |
| 37 |
use Getopt::Long; |
| 38 |
use Pod::Usage; |
| 39 |
|
| 40 |
my $VERSION = "20260210"; |
| 41 |
|
| 42 |
parse_commandline(); |
| 43 |
|
| 44 |
my $afm = read_afm($ARGV[0]); |
| 45 |
$afm->{kernpairs} = read_kpx($ARGV{kpx}, $afm->{kernpairs}) if $ARGV{kpx}; |
| 46 |
|
| 47 |
if ($ARGV{output}) { |
| 48 |
$ARGV{output} .= '.afm' unless $ARGV{output} =~ m/[.]afm\z/xms; |
| 49 |
open my $output, '>', $ARGV{output} |
| 50 |
or die "[ERROR] can't open '$ARGV{output}': $!"; |
| 51 |
select $output; |
| 52 |
} |
| 53 |
|
| 54 |
# If user specified encoding, use that; else, use encoding from .afm file |
| 55 |
my $enc = $ARGV{encoding} ? read_enc($ARGV{encoding}) |
| 56 |
: get_enc($afm) |
| 57 |
; |
| 58 |
# Make lookup table to quickly see if a character is present in the encoding |
| 59 |
my %char_in_enc = map { ($_ => 1) } @{$enc->{vector}}; |
| 60 |
|
| 61 |
print_fontinfo($afm->{fontinfo}, $enc->{scheme}); |
| 62 |
print_charmetrics($afm->{metrics}, $enc->{vector}); |
| 63 |
print_kerndata($afm->{kernpairs}, $afm->{trackkerns}, \%char_in_enc); |
| 64 |
print_composites($afm->{composites}, \%char_in_enc); |
| 65 |
print "EndFontMetrics\n"; |
| 66 |
|
| 67 |
close select *STDOUT if $ARGV{output}; |
| 68 |
|
| 69 |
print_mapentry($afm->{fontinfo}, , $enc->{scheme}) |
| 70 |
if $ARGV{encoding} && $ARGV{output}; |
| 71 |
|
| 72 |
#----------------------------------------------------------------------- |
| 73 |
# Read the command-line options |
| 74 |
#----------------------------------------------------------------------- |
| 75 |
sub parse_commandline { |
| 76 |
Getopt::Long::GetOptions( |
| 77 |
'help|?' => sub { pod2usage(-verbose => 1) }, |
| 78 |
'version' => sub { print "$VERSION\n"; exit }, |
| 79 |
'encoding=s' => \$ARGV{encoding}, |
| 80 |
'kpx=s' => \$ARGV{kpx}, |
| 81 |
'output=s' => \$ARGV{output}, |
| 82 |
) |
| 83 |
or pod2usage(-verbose => 0); |
| 84 |
|
| 85 |
pod2usage(-verbose => 0) if @ARGV != 1; |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
#----------------------------------------------------------------------- |
| 91 |
# Read the input afm file |
| 92 |
#----------------------------------------------------------------------- |
| 93 |
sub read_afm { |
| 94 |
my $filename = shift; |
| 95 |
open my $afmfile, '<', $filename |
| 96 |
or die "[ERROR] can't open '$filename': $!"; |
| 97 |
|
| 98 |
my %afm = ( |
| 99 |
fontinfo => [], |
| 100 |
metrics => [], |
| 101 |
kernpairs => [], |
| 102 |
trackkerns => [], |
| 103 |
composites => [], |
| 104 |
); |
| 105 |
|
| 106 |
until ((my $line = <$afmfile>) =~ m/StartCharMetrics/xms) { |
| 107 |
push @{$afm{fontinfo}}, $line; |
| 108 |
} |
| 109 |
|
| 110 |
for (<$afmfile>) { |
| 111 |
if (m/\A\s* C \s/xms) { push @{$afm{metrics}}, $_; next } |
| 112 |
if (m/\A\s* KPX \s/xms) { push @{$afm{kernpairs}}, $_; next } |
| 113 |
if (m/\A\s* TrackKern \s/xms) { push @{$afm{trackkerns}}, $_; next } |
| 114 |
if (m/\A\s* CC \s/xms) { push @{$afm{composites}}, $_; next } |
| 115 |
} |
| 116 |
|
| 117 |
close $afmfile; |
| 118 |
|
| 119 |
return \%afm; |
| 120 |
} |
| 121 |
|
| 122 |
#----------------------------------------------------------------------- |
| 123 |
# Read additional kerning pairs from the kpx file |
| 124 |
#----------------------------------------------------------------------- |
| 125 |
sub read_kpx { |
| 126 |
my ($filename, $kpx) = @_; |
| 127 |
open my $kpxfile, '<', $filename |
| 128 |
or die "[ERROR] can't open '$filename': $!"; |
| 129 |
|
| 130 |
my %seen = map { ($_ => 1) } @$kpx; |
| 131 |
for (<$kpxfile>) { |
| 132 |
if (m/\A\s* KPX \s/xms) { push @$kpx, $_ unless $seen{$_}++ } |
| 133 |
} |
| 134 |
|
| 135 |
close $kpxfile; |
| 136 |
|
| 137 |
return $kpx; |
| 138 |
} |
| 139 |
|
| 140 |
#----------------------------------------------------------------------- |
| 141 |
# Get encoding name and vector from the specified .enc file |
| 142 |
#----------------------------------------------------------------------- |
| 143 |
sub read_enc { |
| 144 |
my $filename = shift; |
| 145 |
|
| 146 |
open my $encfile, '<', $filename |
| 147 |
or die "[ERROR] can't open '$filename': $!"; |
| 148 |
my $data = join ' ', map { s/%.+|\n//xmsg; $_ } <$encfile>; |
| 149 |
close $encfile; |
| 150 |
|
| 151 |
my ($scheme, $encoding) |
| 152 |
= $data =~ m{ /([-\w]+) \s* [[] (.+) []] \s* def }xms |
| 153 |
or die "[ERROR] parsing encoding file '$filename' failed"; |
| 154 |
|
| 155 |
return { scheme => $scheme, |
| 156 |
vector => [ $encoding =~ m{ /([.\w]+) }xmsg ], |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
#----------------------------------------------------------------------- |
| 161 |
# Get encoding from the input .afm file |
| 162 |
#----------------------------------------------------------------------- |
| 163 |
sub get_enc { |
| 164 |
my $afm = shift; |
| 165 |
|
| 166 |
my %enc = ( scheme => 'FontSpecific', vector => [ ('/.undef') x 256 ] ); |
| 167 |
|
| 168 |
for my $line (@{$afm->{fontinfo}}) { |
| 169 |
if ($line =~ m/EncodingScheme \s+ ([-\w]+)/xms) { |
| 170 |
$enc{scheme} = $1; |
| 171 |
last; |
| 172 |
} |
| 173 |
} |
| 174 |
for my $line (@{$afm->{metrics}}) { |
| 175 |
my ($pos, $name) |
| 176 |
= $line =~ m/C \s+ ([-]?\d+) .+? N \s+ ([.\w]+)/xms |
| 177 |
or die; |
| 178 |
$enc{vector}[$pos] = $name unless $pos == -1; |
| 179 |
} |
| 180 |
|
| 181 |
return \%enc; |
| 182 |
} |
| 183 |
|
| 184 |
#----------------------------------------------------------------------- |
| 185 |
# Print the 'fontinfo' part of the output afm file |
| 186 |
#----------------------------------------------------------------------- |
| 187 |
sub print_fontinfo { |
| 188 |
my ($fontinfo, $scheme) = @_; |
| 189 |
|
| 190 |
for my $line (@$fontinfo) { |
| 191 |
$line = "EncodingScheme $scheme\n" |
| 192 |
if $line =~ m/EncodingScheme/xms; |
| 193 |
print $line; |
| 194 |
} |
| 195 |
|
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
#----------------------------------------------------------------------- |
| 200 |
# Print character metrics for characters in specified encoding |
| 201 |
#----------------------------------------------------------------------- |
| 202 |
sub print_charmetrics { |
| 203 |
my ($metrics, $vector) = @_; |
| 204 |
|
| 205 |
my %metrics = map { m/(WX .+? N \s+ ([.\w]+) .+)/xms; ($2 => $1) } |
| 206 |
@$metrics; |
| 207 |
|
| 208 |
print "StartCharMetrics @{[ scalar grep { $metrics{$_} } @$vector ]}\n"; |
| 209 |
for my $i (0 .. 255) { |
| 210 |
next unless $metrics{$vector->[$i]}; |
| 211 |
print "C $i ; $metrics{$vector->[$i]}"; |
| 212 |
} |
| 213 |
print "EndCharMetrics\n"; |
| 214 |
|
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
#----------------------------------------------------------------------- |
| 219 |
# Print kerning data for characters in specified encoding |
| 220 |
#----------------------------------------------------------------------- |
| 221 |
sub print_kerndata { |
| 222 |
my ($kernpairs, $trackkerns, $char_in_enc) = @_; |
| 223 |
|
| 224 |
my @kernpairs = grep { m/\A\s* KPX \s+ ([.\w]+) \s+ ([.\w]+)/xms |
| 225 |
and $char_in_enc->{$1} |
| 226 |
and $char_in_enc->{$2} |
| 227 |
} |
| 228 |
@$kernpairs; |
| 229 |
|
| 230 |
if (@kernpairs || @$trackkerns) { |
| 231 |
print "StartKernData\n"; |
| 232 |
if (@kernpairs) { |
| 233 |
print "StartKernPairs @{[ scalar @kernpairs ]}\n"; |
| 234 |
print @kernpairs; |
| 235 |
print "EndKernPairs\n"; |
| 236 |
} |
| 237 |
if (@$trackkerns) { |
| 238 |
print "StartTrackKern @{[ scalar @$trackkerns ]}\n"; |
| 239 |
print @$trackkerns; |
| 240 |
print "EndTrackKern\n"; |
| 241 |
} |
| 242 |
print "EndKernData\n"; |
| 243 |
} |
| 244 |
|
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
#----------------------------------------------------------------------- |
| 249 |
# Print "composite" info for characters in specified encoding |
| 250 |
#----------------------------------------------------------------------- |
| 251 |
sub print_composites { |
| 252 |
my ($composites, $char_in_enc) = @_; |
| 253 |
|
| 254 |
# Each line mentions several characters: the composite and two or more |
| 255 |
# 'base' characters. We only print the line if all characters are |
| 256 |
# present in the specified encoding |
| 257 |
my @composites; |
| 258 |
COMPOSITE: |
| 259 |
for my $composite (@$composites) { |
| 260 |
my @chars = $composite =~ m/(?:CC \s+ ([.\w]+))/xmsg; |
| 261 |
for my $char (@chars) { |
| 262 |
next COMPOSITE unless $char_in_enc->{$char}; |
| 263 |
} |
| 264 |
push @composites, $composite; |
| 265 |
} |
| 266 |
|
| 267 |
if (@composites) { |
| 268 |
print "StartComposites @{[ scalar @composites ]}\n"; |
| 269 |
print @composites; |
| 270 |
print "EndComposites\n"; |
| 271 |
} |
| 272 |
|
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
#----------------------------------------------------------------------- |
| 277 |
# Print a mapfile entry for this re-encoded font |
| 278 |
#----------------------------------------------------------------------- |
| 279 |
sub print_mapentry { |
| 280 |
my ($fontinfo, $scheme) = @_; |
| 281 |
|
| 282 |
my $fontname = ''; |
| 283 |
(my $fontfile = basename($ARGV[0] // '')) =~ s/[.]afm\z//xms; |
| 284 |
(my $output = basename($ARGV{output} // '')) =~ s/[.]afm\z//xms; |
| 285 |
|
| 286 |
for my $line (@$fontinfo) { |
| 287 |
if ($line =~ m/FontName \s+ ([-\w]+)/xms) { |
| 288 |
$fontname = $1; |
| 289 |
last; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
print {*STDOUT} qq(${output} ${fontname} <${fontfile}.pfb ), |
| 294 |
qq(<[$ARGV{encoding} "${scheme} ReEncodeFont"\n); |
| 295 |
|
| 296 |
return; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
__END__ |
| 301 |
|
| 302 |
|
| 303 |
############################################################################ |
| 304 |
|
| 305 |
To create the documentation: |
| 306 |
|
| 307 |
pod2man --center="Marc Penninga" --release="fontools" --section=1 \ |
| 308 |
afm2afm - | groff -Tps -man - | ps2pdf - afm2afm.pdf |
| 309 |
|
| 310 |
=pod |
| 311 |
|
| 312 |
=head1 NAME |
| 313 |
|
| 314 |
afm2afm - reencode an F<afm> file |
| 315 |
|
| 316 |
=head1 SYNOPSIS |
| 317 |
|
| 318 |
=over 8 |
| 319 |
|
| 320 |
=item B<afm2afm> |
| 321 |
|
| 322 |
[B<-help>] |
| 323 |
[B<-version>] |
| 324 |
[B<-encoding>=I<< <encodingfile> >>] |
| 325 |
[B<-kpx>=I<< <kpxfile> >>] |
| 326 |
[B<-output>=I<< <outputfile> >>] |
| 327 |
B<< <afmfile> >> |
| 328 |
|
| 329 |
=back |
| 330 |
|
| 331 |
|
| 332 |
=head1 DESCRIPTION |
| 333 |
|
| 334 |
B<afm2afm> re-encodes an F<afm> file. |
| 335 |
|
| 336 |
Metrics (including kerning data) for characters not present in the |
| 337 |
chosen encoding are excluded from the output, which resuls in |
| 338 |
(potentially much) smaller files. |
| 339 |
|
| 340 |
Additional kerning pairs can be added to the output file. |
| 341 |
If you don't specify an encoding file, |
| 342 |
the F<afm> file isn't re-encoded; |
| 343 |
however, all unused (unencoded) data is still pruned. |
| 344 |
|
| 345 |
The program also generates an entry for a F<dvips>-style map file, |
| 346 |
but only if the F<afm> file has been re-encoded and |
| 347 |
the output was written to file |
| 348 |
(i.e., if both the I<-encoding> and I<-output> options were specified). |
| 349 |
|
| 350 |
|
| 351 |
=head1 OPTIONS AND ARGUMENTS |
| 352 |
|
| 353 |
=over 4 |
| 354 |
|
| 355 |
=item B<-help> |
| 356 |
|
| 357 |
Print a short description of the syntax |
| 358 |
|
| 359 |
=item B<-version> |
| 360 |
|
| 361 |
Print version number and exit |
| 362 |
|
| 363 |
=item B<-encoding>=I<< <encodingfile> >> |
| 364 |
|
| 365 |
Re-encode to the enconding in I<< <encodingfile> >> |
| 366 |
|
| 367 |
=item B<-kpx>=I<< <kpxfile> >> |
| 368 |
|
| 369 |
Read additional kerning pairs from I<< <kpxfile> >> and add these to the output. |
| 370 |
This option cannot be used to override values from the input F<afm> file, |
| 371 |
since B<afm2afm> will write both old and new values to the output! |
| 372 |
|
| 373 |
The I<< <kpxfile> >> should contain kerning data in standard F<afm> format, |
| 374 |
i.e. for each kerning pair there should be a line |
| 375 |
|
| 376 |
KPX <left_glyph> <right_glyph> <amount> |
| 377 |
|
| 378 |
All other lines in the I<< <kpxfile> >> are ignored. |
| 379 |
|
| 380 |
=item B<-output>=I<< <outputfile> >> |
| 381 |
|
| 382 |
Write the result to I<< <outputfile> >> instead of C<stdout>. |
| 383 |
|
| 384 |
=item B<< <afmfile> >> |
| 385 |
|
| 386 |
The F<afm> file to be re-encoded. |
| 387 |
|
| 388 |
=back |
| 389 |
|
| 390 |
You may use either one or two dashes before options, |
| 391 |
and option names may be shortened to a unique prefix. |
| 392 |
|
| 393 |
|
| 394 |
=head1 AUTHOR |
| 395 |
|
| 396 |
Marc Penninga <[email protected]> |
| 397 |
|
| 398 |
|
| 399 |
=head1 COPYRIGHT |
| 400 |
|
| 401 |
Copyright (C) 2005-2026 Marc Penninga. |
| 402 |
|
| 403 |
|
| 404 |
=head1 LICENSE |
| 405 |
|
| 406 |
This program is free software; you can redistribute it and/or modify |
| 407 |
it under the terms of the GNU General Public License as published |
| 408 |
by the Free Software Foundation, either version 2 of the License, |
| 409 |
or (at your option) any later version. |
| 410 |
A copy of the GNU General Public License is included with B<afm2afm>; |
| 411 |
see the file F<GPLv2.txt>. |
| 412 |
|
| 413 |
|
| 414 |
=head1 DISCLAIMER |
| 415 |
|
| 416 |
This program is distributed in the hope that it will be useful, |
| 417 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 418 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 419 |
See the GNU General Public License for more details. |
| 420 |
|
| 421 |
|
| 422 |
=head1 VERSION |
| 423 |
|
| 424 |
This document describes B<afm2afm> version 20260210. |
| 425 |
|
| 426 |
|
| 427 |
=head1 RECENT CHANGES |
| 428 |
|
| 429 |
(See the source code for the rest of the story.) |
| 430 |
|
| 431 |
=over 12 |
| 432 |
|
| 433 |
=item I<2019-05-20> |
| 434 |
|
| 435 |
Added the I<-version> option. |
| 436 |
|
| 437 |
=back |
| 438 |
|
| 439 |
|
| 440 |
=begin Really_old_history |
| 441 |
|
| 442 |
=over 12 |
| 443 |
|
| 444 |
=item I<2013-08-07> |
| 445 |
|
| 446 |
Added the I<-kpx> command-line option. |
| 447 |
Replaced all C<given/when> constructions in the source code by C<if>'s, |
| 448 |
to avoid warnings about experimental features in Perl 5.18 and later. |
| 449 |
|
| 450 |
=item I<2012-02-01> |
| 451 |
|
| 452 |
Refactored the code; added the "no re-encoding, only pruning" |
| 453 |
functionality. |
| 454 |
|
| 455 |
=item I<2005-01-10> |
| 456 |
|
| 457 |
First version |
| 458 |
|
| 459 |
=item I<2005-01-25> |
| 460 |
|
| 461 |
Added printing of mapfile entry |
| 462 |
|
| 463 |
=item I<2005-02-18> |
| 464 |
|
| 465 |
Rewrote some of the code |
| 466 |
|
| 467 |
=item I<2005-03-08> |
| 468 |
|
| 469 |
Input files searched via B<kpsewhich> (where available) |
| 470 |
|
| 471 |
=item I<2005-03-15> |
| 472 |
|
| 473 |
Input files searched using B<kpsewhich> or B<findtexmf> (if available) |
| 474 |
|
| 475 |
=item I<2005-04-15> |
| 476 |
|
| 477 |
Updated creation of mapfile entry; look for font file to deduce the correct |
| 478 |
font file format (pfb, pfa, ttf). If no font file is found, |
| 479 |
pfb is assumed. |
| 480 |
|
| 481 |
=item I<2005-04-20> |
| 482 |
|
| 483 |
Program quits if the specified output file exists |
| 484 |
|
| 485 |
=item I<2005-04-29> |
| 486 |
|
| 487 |
Improved the documentation |
| 488 |
|
| 489 |
=item I<2005-07-29> |
| 490 |
|
| 491 |
Some updates to the documentation |
| 492 |
|
| 493 |
=item I<2006-01-12> |
| 494 |
|
| 495 |
A few minor changes to the code |
| 496 |
|
| 497 |
=back |
| 498 |
|
| 499 |
=cut |
| 500 |
|