| 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 integer; |
| 37 |
use Getopt::Long (); |
| 38 |
use List::Util @List::Util::EXPORT_OK; |
| 39 |
use Pod::Usage; |
| 40 |
|
| 41 |
my $VERSION = "20260210"; |
| 42 |
|
| 43 |
our ($NUM_GLYPHS, $UNITS_PER_EM, %kern); |
| 44 |
|
| 45 |
sub main { |
| 46 |
%ARGV = ( |
| 47 |
print_func => \&print_kpx, |
| 48 |
); |
| 49 |
|
| 50 |
Getopt::Long::GetOptions( |
| 51 |
'help|?' => sub { pod2usage(-verbose => 0) }, |
| 52 |
'version' => sub { print "$VERSION\n"; exit }, |
| 53 |
'doc' => sub { pod2usage(-verbose => 2) }, |
| 54 |
'afm' => sub { $ARGV{print_func} = \&print_kpx }, |
| 55 |
'kpx' => sub { $ARGV{print_func} = \&print_kpx }, |
| 56 |
'lua' => sub { $ARGV{print_func} = \&print_lua }, |
| 57 |
) or pod2usage(-verbose => 0); |
| 58 |
pod2usage(-verbose => 0) if @ARGV != 1; |
| 59 |
|
| 60 |
my %font = OTF::get_tables($ARGV[0]); |
| 61 |
|
| 62 |
$NUM_GLYPHS = unpack '@4n', $font{maxp}; |
| 63 |
$UNITS_PER_EM = unpack '@18n', $font{head}; |
| 64 |
|
| 65 |
my @glyph_name = map { sprintf "index%d", $_ } 0 .. $NUM_GLYPHS - 1; |
| 66 |
if (defined $font{CFF}) { |
| 67 |
OTF::CFF::get_glyph_names($font{CFF}, \@glyph_name); |
| 68 |
} |
| 69 |
|
| 70 |
if (defined $font{kern}) { |
| 71 |
OTF::Kern::parse_kerntable($font{kern}); |
| 72 |
} |
| 73 |
|
| 74 |
if (defined $font{GPOS}) { |
| 75 |
my @lookup_list_indices |
| 76 |
= OTF::GPOS::get_lookup_list_indices($font{GPOS}); |
| 77 |
my @all_lookups = OTF::GPOS::get_lookups($font{GPOS}); |
| 78 |
my @lookups = @all_lookups[@lookup_list_indices]; |
| 79 |
my @subtables = map { OTF::GPOS::get_subtables($_) } @lookups; |
| 80 |
|
| 81 |
for my $subtable (@subtables) { |
| 82 |
my $pos_format = unpack 'n', $subtable; |
| 83 |
if ($pos_format == 1) { |
| 84 |
OTF::GPOS::parse_pos_format_1($subtable); |
| 85 |
} |
| 86 |
elsif ($pos_format == 2) { |
| 87 |
OTF::GPOS::parse_pos_format_2($subtable); |
| 88 |
} |
| 89 |
else { |
| 90 |
warn "[ERROR] invalid PosFormat $pos_format ignored"; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
&{$ARGV{print_func}}( \%kern, \@glyph_name ); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
# ------------------------------------------------------------------------ |
| 100 |
# Print kerning data in Adobe's KPX format |
| 101 |
# ------------------------------------------------------------------------ |
| 102 |
sub print_kpx { |
| 103 |
my %kern = %{ shift @_ }; |
| 104 |
my @glyph = @{ shift @_ }; |
| 105 |
|
| 106 |
my $num_kernpairs = sum map { scalar keys %{$kern{$_}} } keys %kern; |
| 107 |
print "StartKernData\nStartKernPairs $num_kernpairs\n"; |
| 108 |
for my $l (sort { $glyph[$a] cmp $glyph[$b] } keys %kern) { |
| 109 |
my $l_glyph = $glyph[$l]; |
| 110 |
for my $r (sort { $glyph[$a] cmp $glyph[$b] } keys %{$kern{$l}}) { |
| 111 |
print "KPX $l_glyph $glyph[$r] ", |
| 112 |
"$kern{$l}{$r}\n"; |
| 113 |
} |
| 114 |
} |
| 115 |
print "EndKernPairs\nEndKernData\n"; |
| 116 |
} |
| 117 |
|
| 118 |
# ------------------------------------------------------------------------ |
| 119 |
# Print kerning data as a Luatex custom font feature |
| 120 |
# ------------------------------------------------------------------------ |
| 121 |
sub print_lua { |
| 122 |
my %kern = %{ shift @_ }; |
| 123 |
my @glyph = @{ shift @_ }; |
| 124 |
|
| 125 |
print <<'END_FEATURE_PREFIX'; |
| 126 |
fonts.handlers.otf.addfeature { |
| 127 |
name = 'kern', |
| 128 |
type = 'kern', |
| 129 |
data = { |
| 130 |
END_FEATURE_PREFIX |
| 131 |
|
| 132 |
for my $l ( sort { $glyph[$a] cmp $glyph[$b] } keys %kern ) { |
| 133 |
print ' ' x 8, "[ '$glyph[$l]' ] = {"; |
| 134 |
for my $r ( sort { $glyph[$a] cmp $glyph[$b] } keys %{$kern{$l}} ) { |
| 135 |
print "\n", ' ' x 12, "[ '$glyph[$r]' ] = $kern{$l}{$r},"; |
| 136 |
} |
| 137 |
print " },\n"; |
| 138 |
} |
| 139 |
|
| 140 |
print <<'END_FEATURE_POSTFIX'; |
| 141 |
} |
| 142 |
} |
| 143 |
END_FEATURE_POSTFIX |
| 144 |
} |
| 145 |
|
| 146 |
######################################################################## |
| 147 |
|
| 148 |
|
| 149 |
package OTF; |
| 150 |
|
| 151 |
#----------------------------------------------------------------------- |
| 152 |
# Return a hash with all tables (as strings) from the font |
| 153 |
#----------------------------------------------------------------------- |
| 154 |
sub get_tables { |
| 155 |
my $filename = shift; |
| 156 |
|
| 157 |
open my $fontfile, '<:raw', $filename |
| 158 |
or die "[ERROR] can't open $filename: $!"; |
| 159 |
my $fontdata = do { local $/; <$fontfile> }; |
| 160 |
close $fontfile; |
| 161 |
|
| 162 |
my ($sfnt_version, @tables) = unpack 'a4nx6/(a16)', $fontdata; |
| 163 |
|
| 164 |
die "[ERROR] $filename: unknown font type" |
| 165 |
unless $sfnt_version eq "\x00\x01\x00\x00" |
| 166 |
|| $sfnt_version eq 'OTTO'; |
| 167 |
|
| 168 |
my %table = map { my ($tag, $offset, $length) = unpack 'A4@8N@12N', $_; |
| 169 |
($tag => substr $fontdata, $offset, $length); |
| 170 |
} |
| 171 |
@tables; |
| 172 |
|
| 173 |
return %table; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
######################################################################## |
| 178 |
|
| 179 |
|
| 180 |
package OTF::CFF; |
| 181 |
|
| 182 |
my @STANDARD_STRINGS = qw( |
| 183 |
.notdef space exclam quotedbl |
| 184 |
numbersign dollar percent ampersand |
| 185 |
quoteright parenleft parenright asterisk |
| 186 |
plus comma hyphen period |
| 187 |
|
| 188 |
slash zero one two |
| 189 |
three four five six |
| 190 |
seven eight nine colon |
| 191 |
semicolon less equal greater |
| 192 |
|
| 193 |
question at A B |
| 194 |
C D E F |
| 195 |
G H I J |
| 196 |
K L M N |
| 197 |
|
| 198 |
O P Q R |
| 199 |
S T U V |
| 200 |
W X Y Z |
| 201 |
bracketleft backslash bracketright asciicircum |
| 202 |
|
| 203 |
underscore quoteleft a b |
| 204 |
c d e f |
| 205 |
g h i j |
| 206 |
k l m n |
| 207 |
|
| 208 |
o p q r |
| 209 |
s t u v |
| 210 |
w x y z |
| 211 |
braceleft bar braceright asciitilde |
| 212 |
|
| 213 |
exclamdown cent sterling fraction |
| 214 |
yen florin section currency |
| 215 |
quotesingle quotedblleft guillemotleft guilsinglleft |
| 216 |
guilsinglright fi fl endash |
| 217 |
|
| 218 |
dagger daggerdbl periodcentered paragraph |
| 219 |
bullet quotesinglbase quotedblbase quotedblright |
| 220 |
guillemotright ellipsis perthousand questiondown |
| 221 |
grave acute circumflex tilde |
| 222 |
|
| 223 |
macron breve dotaccent dieresis |
| 224 |
ring cedilla hungarumlaut ogonek |
| 225 |
caron emdash AE ordfeminine |
| 226 |
Lslash Oslash OE ordmasculine |
| 227 |
|
| 228 |
ae dotlessi lslash oslash |
| 229 |
oe germandbls onesuperior logicalnot |
| 230 |
mu trademark Eth onehalf |
| 231 |
plusminus Thorn onequarter divide |
| 232 |
|
| 233 |
brokenbar degree thorn threequarters |
| 234 |
twosuperior registered minus eth |
| 235 |
multiply threesuperior copyright Aacute |
| 236 |
Acircumflex Adieresis Agrave Aring |
| 237 |
|
| 238 |
Atilde Ccedilla Eacute Ecircumflex |
| 239 |
Edieresis Egrave Iacute Icircumflex |
| 240 |
Idieresis Igrave Ntilde Oacute |
| 241 |
Ocircumflex Odieresis Ograve Otilde |
| 242 |
|
| 243 |
Scaron Uacute Ucircumflex Udieresis |
| 244 |
Ugrave Yacute Ydieresis Zcaron |
| 245 |
aacute acircumflex adieresis agrave |
| 246 |
aring atilde ccedilla eacute |
| 247 |
|
| 248 |
ecircumflex edieresis egrave iacute |
| 249 |
icircumflex idieresis igrave ntilde |
| 250 |
oacute ocircumflex odieresis ograve |
| 251 |
otilde scaron uacute ucircumflex |
| 252 |
|
| 253 |
udieresis ugrave yacute ydieresis |
| 254 |
zcaron exclamsmall Hungarumlautsmall dollaroldstyle |
| 255 |
dollarsuperior ampersandsmall Acutesmall parenleftsuperior |
| 256 |
parenrightsuperior twodotenleader onedotenleader zerooldstyle |
| 257 |
|
| 258 |
oneoldstyle twooldstyle threeoldstyle fouroldstyle |
| 259 |
fiveoldstyle sixoldstyle sevenoldstyle eightoldstyle |
| 260 |
nineoldstyle commasuperior threequartersemdash periodsuperior |
| 261 |
questionsmall asuperior bsuperior centsuperior |
| 262 |
|
| 263 |
dsuperior esuperior isuperior lsuperior |
| 264 |
msuperior nsuperior osuperior rsuperior |
| 265 |
ssuperior tsuperior ff ffi |
| 266 |
ffl parenleftinferior parenrightinferior Circumflexsmall |
| 267 |
|
| 268 |
hyphensuperior Gravesmall Asmall Bsmall |
| 269 |
Csmall Dsmall Esmall Fsmall |
| 270 |
Gsmall Hsmall Ismall Jsmall |
| 271 |
Ksmall Lsmall Msmall Nsmall |
| 272 |
|
| 273 |
Osmall Psmall Qsmall Rsmall |
| 274 |
Ssmall Tsmall Usmall Vsmall |
| 275 |
Wsmall Xsmall Ysmall Zsmall |
| 276 |
colonmonetary onefitted rupiah Tildesmall |
| 277 |
|
| 278 |
exclamdownsmall centoldstyle Lslashsmall Scaronsmall |
| 279 |
Zcaronsmall Dieresissmall Brevesmall Caronsmall |
| 280 |
Dotaccentsmall Macronsmall figuredash hypheninferior |
| 281 |
Ogoneksmall Ringsmall Cedillasmall questiondownsmall |
| 282 |
|
| 283 |
oneeighth threeeighths fiveeighths seveneighths |
| 284 |
onethird twothirds zerosuperior foursuperior |
| 285 |
fivesuperior sixsuperior sevensuperior eightsuperior |
| 286 |
ninesuperior zeroinferior oneinferior twoinferior |
| 287 |
|
| 288 |
threeinferior fourinferior fiveinferior sixinferior |
| 289 |
seveninferior eightinferior nineinferior centinferior |
| 290 |
dollarinferior periodinferior commainferior Agravesmall |
| 291 |
Aacutesmall Acircumflexsmall Atildesmall Adieresissmall |
| 292 |
|
| 293 |
Aringsmall AEsmall Ccedillasmall Egravesmall |
| 294 |
Eacutesmall Ecircumflexsmall Edieresissmall Igravesmall |
| 295 |
Iacutesmall Icircumflexsmall Idieresissmall Ethsmall |
| 296 |
Ntildesmall Ogravesmall Oacutesmall Ocircumflexsmall |
| 297 |
|
| 298 |
Otildesmall Odieresissmall OEsmall Oslashsmall |
| 299 |
Ugravesmall Uacutesmall Ucircumflexsmall Udieresissmall |
| 300 |
Yacutesmall Thornsmall Ydieresissmall 001.000 |
| 301 |
001.001 001.002 001.003 Black |
| 302 |
|
| 303 |
Bold Book Light Medium |
| 304 |
Regular Roman Semibold |
| 305 |
); |
| 306 |
|
| 307 |
#----------------------------------------------------------------------- |
| 308 |
# |
| 309 |
#----------------------------------------------------------------------- |
| 310 |
sub get_glyph_names { |
| 311 |
my ($CFF, $glyph_name) = @_; |
| 312 |
|
| 313 |
# skip header; parse (and ignore) NAME index |
| 314 |
my ($rest) = _parse_index(substr $CFF, unpack '@2C', $CFF); |
| 315 |
|
| 316 |
# parse TopDict index |
| 317 |
($rest, my @top_dict) = _parse_index($rest); |
| 318 |
if (@top_dict > 1) { |
| 319 |
warn "[WARNING] multiple fonts in single file not supported"; |
| 320 |
} |
| 321 |
my %top_dict = _parse_dict($top_dict[0]); |
| 322 |
if ($NUM_GLYPHS != unpack 'n', substr($CFF, $top_dict{17}, 2)) { |
| 323 |
die "[ERROR] NumGlyphs different in 'maxp' and 'CFF' tables"; |
| 324 |
} |
| 325 |
|
| 326 |
# parse String index |
| 327 |
($rest, my @strings) = _parse_index($rest); |
| 328 |
@strings = (@STANDARD_STRINGS, @strings); |
| 329 |
|
| 330 |
my $charset = substr $CFF, $top_dict{15}; |
| 331 |
my $format = unpack 'C', $charset; |
| 332 |
if ($format == 0) { |
| 333 |
my @codes = unpack "\@1n@{[ $NUM_GLYPHS - 1 ]}", $charset; |
| 334 |
@$glyph_name = @strings[0, @codes]; |
| 335 |
} |
| 336 |
elsif ($format == 1) { |
| 337 |
my $i = 0; |
| 338 |
$glyph_name->[$i++] = $strings[0]; |
| 339 |
for (my $j = 0; $i < $NUM_GLYPHS; $j++) { |
| 340 |
my ($first, $n_left) |
| 341 |
= unpack "\@@{[ 1 + 3*$j ]}nC", $charset; |
| 342 |
$glyph_name->[$i + $_] = $strings[$first + $_] |
| 343 |
for 0 .. $n_left; |
| 344 |
$i += 1 + $n_left; |
| 345 |
} |
| 346 |
} |
| 347 |
elsif ($format == 2) { |
| 348 |
my $i = 0; |
| 349 |
$glyph_name->[$i++] = $strings[0]; |
| 350 |
for (my $j = 0; $i < $NUM_GLYPHS; $j++) { |
| 351 |
my ($first, $n_left) |
| 352 |
= unpack "\@@{[ 1 + 4*$j ]}nn", $charset; |
| 353 |
$glyph_name->[$i + $_] = $strings[$first + $_] |
| 354 |
for 0 .. $n_left; |
| 355 |
$i += 1 + $n_left; |
| 356 |
} |
| 357 |
} |
| 358 |
else { |
| 359 |
warn "[ERROR] invalid CharsetFormat '$format' ignored"; |
| 360 |
} |
| 361 |
|
| 362 |
return; |
| 363 |
} |
| 364 |
|
| 365 |
#----------------------------------------------------------------------- |
| 366 |
# Parse Adobe's INDEX format (see CFF spec); return "rest" plus data array |
| 367 |
#----------------------------------------------------------------------- |
| 368 |
sub _parse_index { |
| 369 |
my $index = shift; |
| 370 |
|
| 371 |
my ($count, $off_size) = unpack 'nC', $index; |
| 372 |
my @offsets = unpack "\@3(a$off_size)@{[ $count + 1 ]}", $index; |
| 373 |
if ($off_size == 1) { $_ = unpack 'C', $_ for @offsets } |
| 374 |
elsif ($off_size == 2) { $_ = unpack 'n', $_ for @offsets } |
| 375 |
elsif ($off_size == 4) { $_ = unpack 'N', $_ for @offsets } |
| 376 |
elsif ($off_size == 3) { |
| 377 |
for my $offset (@offsets) { |
| 378 |
my @bytes = map 'C3', $offset; |
| 379 |
$offset = ($bytes[0] << 16) + ($bytes[1] << 8) + $bytes[2]; |
| 380 |
} |
| 381 |
} |
| 382 |
else { |
| 383 |
warn "[ERROR] invalid OffSize $off_size ignored"; |
| 384 |
} |
| 385 |
$_ += 2 + $off_size * ($count + 1) for @offsets; |
| 386 |
|
| 387 |
my @data; |
| 388 |
for my $i (0 .. $count - 1) { |
| 389 |
$data[$i] |
| 390 |
= substr $index, $offsets[$i], $offsets[$i + 1] - $offsets[$i]; |
| 391 |
} |
| 392 |
|
| 393 |
return substr($index, $offsets[$count]), @data; |
| 394 |
} |
| 395 |
|
| 396 |
#----------------------------------------------------------------------- |
| 397 |
# Parse Adobe's DICT format (see CFF spec); return data hash |
| 398 |
#----------------------------------------------------------------------- |
| 399 |
sub _parse_dict { |
| 400 |
my $dict = shift; |
| 401 |
|
| 402 |
my @bytes = unpack 'C*', $dict; |
| 403 |
my (@operands, %dict); |
| 404 |
while (@bytes) { |
| 405 |
if (( 0 <= $bytes[0] && $bytes[0] <= 11) |
| 406 |
|| (13 <= $bytes[0] && $bytes[0] <= 22)) |
| 407 |
{ |
| 408 |
my $operator = shift @bytes; |
| 409 |
($dict{$operator} = join q{ }, @operands) =~ s/^\s+//xms; |
| 410 |
@operands = (); |
| 411 |
} |
| 412 |
elsif ($bytes[0] == 12) { |
| 413 |
my $operator = join q{ }, splice @bytes, 0, 2; |
| 414 |
($dict{$operator} = join q{ }, @operands) =~ s/^\s+//xms; |
| 415 |
@operands = (); |
| 416 |
} |
| 417 |
elsif ($bytes[0] == 28 |
| 418 |
|| $bytes[0] == 29 |
| 419 |
|| $bytes[0] == 30 |
| 420 |
|| (32 <= $bytes[0] && $bytes[0] <= 254)) |
| 421 |
{ |
| 422 |
push @operands, _get_number(\@bytes); |
| 423 |
} |
| 424 |
else { |
| 425 |
warn "[ERROR] invalid byte $bytes[0] in DICT ignored"; |
| 426 |
shift @bytes; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
return %dict; |
| 431 |
} |
| 432 |
|
| 433 |
#----------------------------------------------------------------------- |
| 434 |
# Parse Adobe's variable-length numbers (see CFF spec); shorten arg array |
| 435 |
#----------------------------------------------------------------------- |
| 436 |
sub _get_number { |
| 437 |
my $bytes = shift; |
| 438 |
|
| 439 |
my $b0 = shift @$bytes; |
| 440 |
if ($b0 == 28) { |
| 441 |
my ($b1, $b2) = splice @$bytes, 0, 2; |
| 442 |
return ($b1 << 8) | $b2; |
| 443 |
} |
| 444 |
elsif ($b0 == 29) { |
| 445 |
my ($b1, $b2, $b3, $b4) = splice @$bytes, 0, 4; |
| 446 |
return ((((($b1 << 8) + $b2) << 8) + $b3) << 8) + $b4; |
| 447 |
} |
| 448 |
elsif ($b0 == 30) { |
| 449 |
# Reals not implemented; just dump appropriate number of bytes |
| 450 |
until (($b0 & 0xf0) == 0xf0 || ($b0 & 0xf) == 0xf) { |
| 451 |
$b0 = shift @$bytes; |
| 452 |
} |
| 453 |
} |
| 454 |
elsif (32 <= $b0 && $b0 <= 246) { |
| 455 |
return $b0 - 139; |
| 456 |
} |
| 457 |
elsif (247 <= $b0 && $b0 <= 250) { |
| 458 |
my $b1 = shift @$bytes; |
| 459 |
return 256 * ($b0 - 247) + $b1 + 108; |
| 460 |
} |
| 461 |
elsif (251 <= $b0 && $b0 <= 254) { |
| 462 |
my $b1 = shift @$bytes; |
| 463 |
return -256 * ($b0 - 251) - $b1 - 108; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
######################################################################## |
| 469 |
|
| 470 |
|
| 471 |
package OTF::GPOS; |
| 472 |
|
| 473 |
#----------------------------------------------------------------------- |
| 474 |
# Return list of all Lookups from the GPOS table |
| 475 |
#----------------------------------------------------------------------- |
| 476 |
sub get_lookups { |
| 477 |
my $GPOS = shift; |
| 478 |
|
| 479 |
my $lookup_list = substr $GPOS, unpack '@8n', $GPOS; |
| 480 |
my @lookup_offsets = unpack 'n/n', $lookup_list; |
| 481 |
|
| 482 |
return map { substr $lookup_list, $_ } @lookup_offsets; |
| 483 |
} |
| 484 |
|
| 485 |
#----------------------------------------------------------------------- |
| 486 |
# Return list of all LookupListIndices pointed to by 'kern' feature |
| 487 |
#----------------------------------------------------------------------- |
| 488 |
sub get_lookup_list_indices { |
| 489 |
my $GPOS = shift; |
| 490 |
|
| 491 |
my $feature_list = substr $GPOS, unpack '@6n', $GPOS; |
| 492 |
my %lookup_list_index; |
| 493 |
for my $feature_record (unpack 'n/(a6)', $feature_list) { |
| 494 |
my ($tag, $offset) = unpack 'a4n', $feature_record; |
| 495 |
next if $tag ne 'kern'; |
| 496 |
|
| 497 |
my $feature = substr $feature_list, $offset; |
| 498 |
my @lookup_list_indices = unpack '@2n/n', $feature; |
| 499 |
$lookup_list_index{$_} = 1 for @lookup_list_indices; |
| 500 |
} |
| 501 |
|
| 502 |
return keys %lookup_list_index; |
| 503 |
} |
| 504 |
|
| 505 |
#----------------------------------------------------------------------- |
| 506 |
# Return list of all subtables in Lookup table |
| 507 |
#----------------------------------------------------------------------- |
| 508 |
sub get_subtables { |
| 509 |
my $lookup = shift; |
| 510 |
|
| 511 |
my ($lookup_type, @subtable_offsets) = unpack 'n@4n/n', $lookup; |
| 512 |
if ($lookup_type != 2) { |
| 513 |
warn "[WARNING] LookupType $lookup_type not implemented"; |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
return map { substr $lookup, $_ } @subtable_offsets; |
| 518 |
} |
| 519 |
|
| 520 |
#----------------------------------------------------------------------- |
| 521 |
# Parse subtable in PairPosFormat 1, store kern pairs in global %kern |
| 522 |
#----------------------------------------------------------------------- |
| 523 |
sub parse_pos_format_1 { |
| 524 |
my $subtable = shift; |
| 525 |
|
| 526 |
my ($coverage_offset, $value_format_1, $value_format_2, @pair_set_offsets) |
| 527 |
= unpack '@2nnnn/n', $subtable; |
| 528 |
|
| 529 |
my @coverage = _get_coverage(substr $subtable, $coverage_offset); |
| 530 |
my @pair_sets = map { substr $subtable, $_ } @pair_set_offsets; |
| 531 |
if (@coverage != @pair_sets) { |
| 532 |
warn "[ERROR] Coverage table doesn't match PairSet table"; |
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
my ($record_size, $value_offset) |
| 537 |
= _parse_value_formats($value_format_1, $value_format_2, 2, 2); |
| 538 |
|
| 539 |
while (1) { |
| 540 |
my $pair_set = shift @pair_sets or last; |
| 541 |
my $first = shift @coverage; |
| 542 |
|
| 543 |
my @pair_value_records = unpack "n/(a$record_size)", $pair_set; |
| 544 |
|
| 545 |
for my $pair_value_record (@pair_value_records) { |
| 546 |
my ($second, $value) |
| 547 |
= unpack "n\@${value_offset}s>", $pair_value_record; |
| 548 |
next if $value == 0; |
| 549 |
$kern{$first}{$second} ||= 1000 * $value / $UNITS_PER_EM; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
return; |
| 554 |
} |
| 555 |
|
| 556 |
#----------------------------------------------------------------------- |
| 557 |
# Parse subtable in PairPosFormat 2, store kern pairs in global %kern |
| 558 |
#----------------------------------------------------------------------- |
| 559 |
sub parse_pos_format_2 { |
| 560 |
my $subtable = shift; |
| 561 |
|
| 562 |
my ($coverage_offset, $value_format_1, $value_format_2, |
| 563 |
$class_def_1, $class_def_2, $class_1_count, $class_2_count) |
| 564 |
= unpack '@2nnnnnnn', $subtable; |
| 565 |
|
| 566 |
my @coverage = _get_coverage(substr $subtable, $coverage_offset); |
| 567 |
|
| 568 |
my ($class_2_record_size, $value_offset) |
| 569 |
= _parse_value_formats($value_format_1, $value_format_2, 0, 0); |
| 570 |
|
| 571 |
my @class_1 = _get_class(substr $subtable, $class_def_1); |
| 572 |
my @class_2 = _get_class(substr $subtable, $class_def_2); |
| 573 |
|
| 574 |
my $class_1_record_size = $class_2_count * $class_2_record_size; |
| 575 |
my @class_1_records |
| 576 |
= unpack "\@16(a$class_1_record_size)$class_1_count", $subtable; |
| 577 |
|
| 578 |
for my $class_1 (0 .. $class_1_count - 1) { |
| 579 |
my $class_1_record = $class_1_records[$class_1]; |
| 580 |
my @class_2_records |
| 581 |
= unpack "(a$class_2_record_size)$class_2_count", $class_1_record; |
| 582 |
|
| 583 |
for my $class_2 (0 .. $class_2_count - 1) { |
| 584 |
my $class_2_record = $class_2_records[$class_2]; |
| 585 |
my $value = unpack "\@${value_offset}s>", $class_2_record; |
| 586 |
next if $value == 0; |
| 587 |
$value = 1000 * $value / $UNITS_PER_EM; |
| 588 |
|
| 589 |
my @first = grep { $class_1[$_] == $class_1 } @coverage; |
| 590 |
my @second = grep { $class_2[$_] == $class_2 } 0 .. $#class_2; |
| 591 |
|
| 592 |
for my $first (@first) { |
| 593 |
for my $second (@second) { |
| 594 |
$kern{$first}{$second} ||= $value; |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
#----------------------------------------------------------------------- |
| 604 |
# Get class number for all glyphs |
| 605 |
#----------------------------------------------------------------------- |
| 606 |
sub _get_class { |
| 607 |
my $class = shift; |
| 608 |
|
| 609 |
my @class = (0) x $NUM_GLYPHS; |
| 610 |
|
| 611 |
my $class_format = unpack 'n', $class; |
| 612 |
if ($class_format == 1) { |
| 613 |
my ($start_glyph, @class_value) = unpack '@2nn/n', $class; |
| 614 |
@class[$start_glyph .. $start_glyph + $#class_value] |
| 615 |
= @class_value; |
| 616 |
} |
| 617 |
elsif ($class_format == 2) { |
| 618 |
my @class_ranges = unpack '@2n/(a6)', $class; |
| 619 |
for my $class_range (@class_ranges) { |
| 620 |
my ($start, $end, $class) = unpack 'nnn', $class_range; |
| 621 |
@class[$start .. $end] = ($class) x ($end - $start + 1); |
| 622 |
} |
| 623 |
} |
| 624 |
else { |
| 625 |
warn "[ERROR] invalid ClassFormat '$class_format' ignored"; |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
return @class; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
#----------------------------------------------------------------------- |
| 634 |
# Determine size of ValueRecord and offset of XAdvance value in it |
| 635 |
#----------------------------------------------------------------------- |
| 636 |
sub _parse_value_formats { |
| 637 |
my ($value_format_1, $value_format_2, $record_size, $value_offset) = @_; |
| 638 |
|
| 639 |
unless ($value_format_1 & 0x4 && $value_format_2 == 0) { |
| 640 |
warn "[WARNING] ValueFormat ($value_format_1, ", |
| 641 |
"$value_format_2) not implemented"; |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
for my $bit ((0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80)) { |
| 646 |
$record_size += 2 if $value_format_1 & $bit; |
| 647 |
} |
| 648 |
for my $bit ((0x1, 0x2)) { |
| 649 |
$value_offset += 2 if $value_format_1 & $bit; |
| 650 |
} |
| 651 |
|
| 652 |
return ($record_size, $value_offset); |
| 653 |
} |
| 654 |
|
| 655 |
#----------------------------------------------------------------------- |
| 656 |
# Return a list of GlyphIDs in a Coverage table |
| 657 |
#----------------------------------------------------------------------- |
| 658 |
sub _get_coverage { |
| 659 |
my $coverage = shift; |
| 660 |
|
| 661 |
my $coverage_format = unpack 'n', $coverage; |
| 662 |
if ($coverage_format == 1) { |
| 663 |
return unpack '@2n/n', $coverage; |
| 664 |
} |
| 665 |
elsif ($coverage_format == 2) { |
| 666 |
my @range_records = unpack '@2n/(a6)', $coverage; |
| 667 |
|
| 668 |
return map { my ($start, $end) = unpack 'nn', $_; |
| 669 |
$start .. $end; |
| 670 |
} |
| 671 |
@range_records; |
| 672 |
} |
| 673 |
else { |
| 674 |
warn "[WARNING] unknown CoverageFormat $coverage_format ignored"; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
######################################################################## |
| 680 |
|
| 681 |
|
| 682 |
package OTF::Kern; |
| 683 |
|
| 684 |
#----------------------------------------------------------------------- |
| 685 |
# Parse "kern"table, store kern pairs in global %kern |
| 686 |
#----------------------------------------------------------------------- |
| 687 |
sub parse_kerntable { |
| 688 |
my $kern = shift; |
| 689 |
|
| 690 |
my $num_tables = unpack '@2n', $kern; |
| 691 |
my $start_subtable = 4; |
| 692 |
for my $i (0 .. $num_tables - 1) { |
| 693 |
my $subtable = substr $kern, $start_subtable; |
| 694 |
my ($length, $coverage) = unpack '@2nn', $subtable; |
| 695 |
$start_subtable += $length; |
| 696 |
|
| 697 |
if ($coverage != 0x1) { |
| 698 |
warn "[WARNING] kern table format '$coverage' not implemented"; |
| 699 |
next; |
| 700 |
} |
| 701 |
|
| 702 |
my @kern_pairs = unpack '@6nx6/(a6)', $subtable; |
| 703 |
for my $kern_pair (@kern_pairs) { |
| 704 |
my ($first, $second, $value) = unpack 'nns>', $kern_pair; |
| 705 |
next if $value == 0; |
| 706 |
|
| 707 |
$kern{$first}{$second} ||= 1000 * $value / $UNITS_PER_EM; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
######################################################################## |
| 716 |
|
| 717 |
|
| 718 |
package main; |
| 719 |
|
| 720 |
main(); |
| 721 |
|
| 722 |
__END__ |
| 723 |
|
| 724 |
|
| 725 |
######################################################################## |
| 726 |
|
| 727 |
|
| 728 |
To create the documentation: |
| 729 |
|
| 730 |
pod2man --center="Marc Penninga" --release="fontools" --section=1 \ |
| 731 |
ot2kpx - | groff -Tps -man - | ps2pdf - ot2kpx.pdf |
| 732 |
|
| 733 |
=pod |
| 734 |
|
| 735 |
=head1 NAME |
| 736 |
|
| 737 |
ot2kpx - extract kerning information from OpenType fonts |
| 738 |
|
| 739 |
|
| 740 |
=head1 SYNOPSIS |
| 741 |
|
| 742 |
B<ot2kpx> [ I<-afm> | I<-kpx> | I<-lua> ] B<< <fontfile> >> |
| 743 |
|
| 744 |
|
| 745 |
=head1 DESCRIPTION |
| 746 |
|
| 747 |
B<ot2kpx> extract the kerning data from OpenType fonts and prints it |
| 748 |
to C<stdout>, either in Adobe's KPX format (for adding to an F<afm> file) |
| 749 |
or as a Luatex custom feature, for use with the C<\directlua> command. |
| 750 |
|
| 751 |
|
| 752 |
=head1 OPTIONS AND ARGUMENTS |
| 753 |
|
| 754 |
=over 4 |
| 755 |
|
| 756 |
=item B<-help> |
| 757 |
|
| 758 |
Print a short help text and exit. |
| 759 |
|
| 760 |
=item B<-version> |
| 761 |
|
| 762 |
Print B<ot2kpx>'s version and exit. |
| 763 |
|
| 764 |
=item B<-afm>, B<-kpx> |
| 765 |
|
| 766 |
Output the kerning data in Adobe's KPX format, as used in F<afm> files. |
| 767 |
This is the default output format. |
| 768 |
|
| 769 |
=item B<-lua> |
| 770 |
|
| 771 |
Output the kerning data as a Luatex custom font feature, |
| 772 |
to be included in a C<\directlua> command. |
| 773 |
|
| 774 |
=item B<< <fontfile> >> |
| 775 |
|
| 776 |
The OpenType font (both F<otf> and F<ttf> format are supported). |
| 777 |
|
| 778 |
=back |
| 779 |
|
| 780 |
|
| 781 |
=head1 RESTRICTIONS |
| 782 |
|
| 783 |
=over 4 |
| 784 |
|
| 785 |
=item B<-> |
| 786 |
|
| 787 |
B<ot2kpx> doesn't implement all of the OpenType specification. |
| 788 |
Things that are missing include: support for font files containing |
| 789 |
multiple fonts, LookupTables with LookupTypes other than 2, |
| 790 |
"kern" tables with format other than 0 and ValueRecords with |
| 791 |
other types of data than just XAdvance data. |
| 792 |
|
| 793 |
Most of these limitations won't matter, since the missing features |
| 794 |
are rare (the only fonts I know of that use them are the non-western fonts |
| 795 |
that come with Adobe Reader). Furthermore, many of these features define |
| 796 |
(according to the OpenType specification) I<"subtle, device-dependent |
| 797 |
adjustments at specific font sizes or device resolutions">; |
| 798 |
since there's no way to express such adjustments in F<afm> format, |
| 799 |
ignoring them seems to be the only option anyway. |
| 800 |
|
| 801 |
=item B<-> |
| 802 |
|
| 803 |
B<ot2kpx> collects kerning data first from the "kern" table, then from |
| 804 |
all LookupTables associated with the "kern" feature; if a kerning pair |
| 805 |
occurs multiple times, the first value seen is chosen. |
| 806 |
There are (or may be) several issues with this approach: |
| 807 |
|
| 808 |
=over 4 |
| 809 |
|
| 810 |
=item - |
| 811 |
|
| 812 |
The OpenType specification says that fonts in F<otf> format shouldn't |
| 813 |
use the "kern" table at all, just the lookups from the "GPOS" table. |
| 814 |
Many such fonts do, however, contain a "kern" table, but no "GPOS" table; |
| 815 |
so we use the "kern" table anyway. |
| 816 |
|
| 817 |
=item - |
| 818 |
|
| 819 |
Instead of reading all LookupTables, it might be better to let the user |
| 820 |
specify a script and language and process only the LookupTables for |
| 821 |
those values. |
| 822 |
However, at least in the fonts I checked, all script/language combinations |
| 823 |
eventually point to the I<same> "kern" LookupTables, so this approach |
| 824 |
wouldn't make any difference (apart from further complicating the code). |
| 825 |
|
| 826 |
=back |
| 827 |
|
| 828 |
=back |
| 829 |
|
| 830 |
|
| 831 |
=head1 AUTHOR |
| 832 |
|
| 833 |
Marc Penninga <[email protected]> |
| 834 |
|
| 835 |
|
| 836 |
=head1 COPYRIGHT |
| 837 |
|
| 838 |
Copyright (C) 2005-2026 Marc Penninga. |
| 839 |
|
| 840 |
|
| 841 |
=head1 LICENSE |
| 842 |
|
| 843 |
This program is free software; you can redistribute it and/or modify |
| 844 |
it under the terms of the GNU General Public License as published |
| 845 |
by the Free Software Foundation, either version 2 of the License, |
| 846 |
or (at your option) any later version. |
| 847 |
A copy of the GNU General Public License is included with B<ot2kpx>; |
| 848 |
see the file F<GPLv2.txt>. |
| 849 |
|
| 850 |
|
| 851 |
=head1 DISCLAIMER |
| 852 |
|
| 853 |
This program is distributed in the hope that it will be useful, |
| 854 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 855 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 856 |
See the GNU General Public License for more details. |
| 857 |
|
| 858 |
|
| 859 |
=head1 VERSION |
| 860 |
|
| 861 |
This document describes B<ot2kpx> version 20260210. |
| 862 |
|
| 863 |
|
| 864 |
=head1 RECENT CHANGES |
| 865 |
|
| 866 |
(See the source code for the rest of the story.) |
| 867 |
|
| 868 |
=over 12 |
| 869 |
|
| 870 |
=item I<2019-05-20> |
| 871 |
|
| 872 |
Added the I<-version> option. |
| 873 |
|
| 874 |
=item I<2019-04-15> |
| 875 |
|
| 876 |
Added the I<-lua> command-line option to get output in Luatex's |
| 877 |
custom feature format. |
| 878 |
|
| 879 |
=back |
| 880 |
|
| 881 |
|
| 882 |
=begin Really_old_history |
| 883 |
|
| 884 |
=over 12 |
| 885 |
|
| 886 |
=item I<2013-08-07> |
| 887 |
|
| 888 |
Replaced all C<given/when> constructions in the source code by C<if>'s, |
| 889 |
to avoid warnings about experimental features in Perl 5.18 and later. |
| 890 |
|
| 891 |
=item I<2012-02-01> |
| 892 |
|
| 893 |
Refactored the code, and fixed a number of bugs in the process. |
| 894 |
Updated the documentation. |
| 895 |
|
| 896 |
=item I<2005-01-10> |
| 897 |
|
| 898 |
First version |
| 899 |
|
| 900 |
=item I<2005-02-18> |
| 901 |
|
| 902 |
Rewrote some of the code |
| 903 |
|
| 904 |
=item I<2005-03-08> |
| 905 |
|
| 906 |
Input files searched via B<kpsewhich> (where available) |
| 907 |
|
| 908 |
=item I<2005-03-15> |
| 909 |
|
| 910 |
Input files searched using B<kpsewhich> or B<findtexmf> |
| 911 |
|
| 912 |
=item I<2005-03-21> |
| 913 |
|
| 914 |
Test if GPOS table is present before trying to read it |
| 915 |
|
| 916 |
=item I<2005-04-29> |
| 917 |
|
| 918 |
Improved the documentation |
| 919 |
|
| 920 |
=item I<2005-05-25> |
| 921 |
|
| 922 |
Changed warning that's given when the font contains no GPOS table, to an |
| 923 |
informational message. |
| 924 |
|
| 925 |
=item I<2005-07-29> |
| 926 |
|
| 927 |
A few updates to the documentation |
| 928 |
|
| 929 |
=back |
| 930 |
|
| 931 |
=cut |
| 932 |
|