| 1 | # RCS Status : $Id: TTtoType42.pm,v 1.12 2007-10-11 11:47:17+02 jv Exp $ | ||
| 2 | # Author : Johan Vromans | ||
| 3 | # Created On : Mon Dec 16 18:56:03 2002 | ||
| 4 | # Last Modified By: Johan Vromans | ||
| 5 | # Last Modified On: Thu Oct 11 11:47:15 2007 | ||
| 6 | # Update Count : 196 | ||
| 7 | # Status : Released | ||
| 8 | |||
| 9 | ################ Module Preamble ################ | ||
| 10 | |||
| 11 | package PostScript::Font::TTtoType42; | ||
| 12 | |||
| 13 | use 5.006; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | our $VERSION = "0.04"; | ||
| 19 | |||
| 20 | use base qw(Font::TTF::Font); | ||
| 21 | |||
| 22 | use constant CHUNK => 65534; | ||
| 23 | |||
| 24 | ################ Public Methods ################ | ||
| 25 | |||
| 26 | # my $f = new PostScript::Font::TTtoType42::("Arial.ttf"); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my $class = shift; | ||
| 30 | my $font = shift; | ||
| 31 | my (%atts) = (verbose => 0, trace => 0, debug => 0, | ||
| 32 | @_); | ||
| 33 | my $self = Font::TTF::Font->open($font); | ||
| 34 | $self->{' verbose'} = $atts{verbose}; | ||
| 35 | $self->{' trace'} = $atts{trace} || $self->{verbose}; | ||
| 36 | $self->{' debug'} = $atts{debug} || $self->{trace}; | ||
| 37 | bless $self, $class; | ||
| 38 | } | ||
| 39 | |||
| 40 | # $f->write("Arial.t42"); | ||
| 41 | |||
| 42 | sub write { | ||
| 43 | my ($self, $file) = @_; | ||
| 44 | |||
| 45 | CORE::open(my $fd, ">", $file) or die("$file: $!\n"); | ||
| 46 | print $fd (${$self->as_string}); | ||
| 47 | close($fd); | ||
| 48 | $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | # my $t42data = ${$f->as_string}; | ||
| 52 | |||
| 53 | sub as_string { | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | # Read some tables. | ||
| 57 | my $head = $self->{head}->read; | ||
| 58 | my $name = $self->{name}->read; | ||
| 59 | my $post = $self->{post}->read; | ||
| 60 | |||
| 61 | # Version. Try to normalize to nnn.nnn. | ||
| 62 | my $version = $self->_str(5); | ||
| 63 | $version = sprintf("%07.3f", $1) if $version =~ /(\d+\.\d+)/; | ||
| 64 | |||
| 65 | # Font bounding box. | ||
| 66 | # Some fonts have dimensions that are 1000 times too small. Let's | ||
| 67 | # do some auto-sensing. | ||
| 68 | my $fix = 1; | ||
| 69 | my $scale = do { | ||
| 70 | my $u = $head->{unitsPerEm}; | ||
| 71 | sub {int($_[0] * $fix / $u)} | ||
| 72 | }; | ||
| 73 | my @bb = map { $scale->($head->{$_}) } qw(xMin yMin xMax yMax); | ||
| 74 | if ( !$bb[0] || !$bb[1] || !$bb[2] || !$bb[3] ) { | ||
| 75 | $fix = 1000; | ||
| 76 | @bb = map { $scale->($head->{$_}) } qw(xMin yMin xMax yMax); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Glyph table. | ||
| 80 | my $glyphs = $self->glyphs; | ||
| 81 | |||
| 82 | # Start font information. | ||
| 83 | my $ret = "%!PS-TrueTypeFont" . | ||
| 84 | "-" . sprintf("%07.3f", $self->{head}{version}) . | ||
| 85 | "-" . $version . | ||
| 86 | "\n" . | ||
| 87 | "%%Creator: " . __PACKAGE__ . " " . $VERSION . | ||
| 88 | " by Johan Vromans\n" . | ||
| 89 | "%%CreationDate: " . localtime(time) . "\n"; | ||
| 90 | |||
| 91 | $ret .= "11 dict begin\n"; | ||
| 92 | $self->_addstr(\$ret, "FontName", 6, 1); | ||
| 93 | $ret .= "/FontType 42 def\n" . | ||
| 94 | "/FontMatrix [1 0 0 1 0 0] def\n" . | ||
| 95 | "/FontBBox [@bb] def\n" . | ||
| 96 | "/PaintType 0 def\n" . | ||
| 97 | "/FontInfo 9 dict dup begin\n" . | ||
| 98 | "/version (" . _psstr($version) . ") readonly def\n"; | ||
| 99 | # $self->_addstr(\$ret, "Notice", 0); | ||
| 100 | $self->_addstr(\$ret, "FullName", 4); | ||
| 101 | $self->_addstr(\$ret, "FamilyName", 1); | ||
| 102 | $self->_addstr(\$ret, "Weight", 2); | ||
| 103 | $self->_addnum(\$ret, "ItalicAngle", $post->{italicAngle}); | ||
| 104 | $self->_addbool(\$ret,"isFixedPitch", $post->{isFixedPitch}); | ||
| 105 | $self->_addnum(\$ret, "UnderlinePosition", $post->{underlinePosition}); | ||
| 106 | $self->_addnum(\$ret, "UnderlineThickness", $post->{underlineThickness}); | ||
| 107 | $ret .= "end readonly def\n" . | ||
| 108 | "/Encoding StandardEncoding def\n"; | ||
| 109 | |||
| 110 | # CharStrings definitions. | ||
| 111 | $glyphs = [ ".notdef" ] unless @$glyphs; | ||
| 112 | $ret .= "/CharStrings " . scalar(@$glyphs) . " dict dup begin\n"; | ||
| 113 | my $i = 0; | ||
| 114 | foreach ( @$glyphs ) { | ||
| 115 | $ret .= "/$_ $i def\n"; | ||
| 116 | $i++; | ||
| 117 | } | ||
| 118 | $ret .= "end readonly def\n"; | ||
| 119 | |||
| 120 | # TrueType strings table. | ||
| 121 | $ret .= "/sfnts[<\n"; | ||
| 122 | |||
| 123 | my @tables = ('cvt ', 'fpgm', 'glyf', 'head', 'hhea', 'hmtx', 'loca', | ||
| 124 | 'maxp', 'prep'); | ||
| 125 | |||
| 126 | # Count the number of tables actually present. | ||
| 127 | my $tables = 0; | ||
| 128 | foreach my $t ( @tables ) { | ||
| 129 | next unless $self->{$t}; | ||
| 130 | next unless $self->{$t}->{' LENGTH'}; | ||
| 131 | $tables++; | ||
| 132 | } | ||
| 133 | |||
| 134 | my $start = 12 + 16 * $tables; | ||
| 135 | my $dir = _dirhdr($tables); | ||
| 136 | my $fd = $self->{' INFILE'}; | ||
| 137 | |||
| 138 | # Create dir entries and calculate the new 'head' checksum. | ||
| 139 | my $csum = 0xB4DED201; | ||
| 140 | { use integer; | ||
| 141 | foreach my $t ( @tables ) { | ||
| 142 | next unless $self->{$t}; | ||
| 143 | my $off = $self->{$t}->{' OFFSET'}; | ||
| 144 | my $len = $self->{$t}->{' LENGTH'}; | ||
| 145 | my $sum = $self->{$t}->{' CSUM'}; | ||
| 146 | $dir .= sprintf("%s%08X%08X%08X\n", | ||
| 147 | uc unpack("H8", $t), $sum, $start, $len); | ||
| 148 | $csum += $sum + $sum + $start + $len; | ||
| 149 | $start += $len; | ||
| 150 | $start++ while $start % 4; | ||
| 151 | } | ||
| 152 | $csum &= 0xffffffff; | ||
| 153 | $csum = 0xb1b0afba - $csum; | ||
| 154 | } | ||
| 155 | |||
| 156 | # Add dir info and prepare for the tables. | ||
| 157 | $ret .= $dir; | ||
| 158 | my $tally = length($dir) / 2; | ||
| 159 | my $data = ""; | ||
| 160 | |||
| 161 | my $ship = sub { | ||
| 162 | $data =~ s/(.{72})/$1\n/g; | ||
| 163 | $ret .= $data . "\n00><\n"; | ||
| 164 | $data = ""; | ||
| 165 | $tally = 0; | ||
| 166 | }; | ||
| 167 | |||
| 168 | foreach my $t ( @tables ) { | ||
| 169 | next unless $self->{$t}; | ||
| 170 | my $len = $self->{$t}->{' LENGTH'}; | ||
| 171 | next unless $len; # to make sure. | ||
| 172 | |||
| 173 | printf STDERR ("$t: off = 0x%x, len = 0x%x, csum = 0x%x\n", | ||
| 174 | $self->{$t}->{' OFFSET'}, $len, | ||
| 175 | $self->{$t}->{' CSUM'}) if $self->{' trace'}; | ||
| 176 | |||
| 177 | # If the glyf table is bigger than a CHUNK, it must be split on | ||
| 178 | # a glyph boundary... | ||
| 179 | if ( $t eq "glyf" && $len > CHUNK ) { | ||
| 180 | $self->_glyftbl(\$data, \$tally, $ship, $fd, | ||
| 181 | $self->{glyf}->{' OFFSET'}, $len, | ||
| 182 | $self->{loca}->{' OFFSET'}, | ||
| 183 | $self->{loca}->{' LENGTH'}); | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | # Ship current sfnts string if this table does not fit. | ||
| 187 | if ( $tally + $len > CHUNK ) { | ||
| 188 | $ship->(); | ||
| 189 | } | ||
| 190 | |||
| 191 | # Read table, and convert to hex data. | ||
| 192 | my $off = $self->{$t}->{' OFFSET'}; | ||
| 193 | sysseek($fd, $off, 0); | ||
| 194 | while ( $len > 0 ) { | ||
| 195 | my $dat; | ||
| 196 | my $l = $len >= 1024 ? 1024 : $len; | ||
| 197 | sysread($fd, $dat, $l); | ||
| 198 | if ( $t eq "head" ) { | ||
| 199 | # Move new checksum in. | ||
| 200 | substr($dat,8,4) = pack("N",$csum) | ||
| 201 | } | ||
| 202 | $len -= $l; | ||
| 203 | $tally += $l; | ||
| 204 | $l += $l; | ||
| 205 | $data .= uc unpack("H$l", $dat); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | # Pad to 4-byte boundary if necessary. | ||
| 210 | if ( ($len = $self->{$t}->{' LENGTH'}) & 0x3 ) { | ||
| 211 | printf STDERR ("odd length 0x%x, adjusting...\n", $len) | ||
| 212 | if $self->{' debug'}; | ||
| 213 | $len = 4 - ($len & 0x3); | ||
| 214 | $data .= "00" x $len; | ||
| 215 | $tally += $len; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | # Format and terminate pending sfnts string. | ||
| 220 | $data =~ s/(.{72})/$1\n/g; | ||
| 221 | $ret .= $data . "\n00>"; | ||
| 222 | |||
| 223 | # Finish font info. | ||
| 224 | $ret .= "]def\n" . | ||
| 225 | "FontName currentdict end definefont pop\n"; | ||
| 226 | |||
| 227 | # Return ref to the info. | ||
| 228 | \$ret; | ||
| 229 | } | ||
| 230 | |||
| 231 | # my @glyphs = @{$f->glyphs}; | ||
| 232 | # Ordered set of glyphs, as they appear in the font. | ||
| 233 | sub glyphs { | ||
| 234 | my $self = shift; | ||
| 235 | $self->{glyphs} ||= $self->_getglyphs; | ||
| 236 | } | ||
| 237 | |||
| 238 | # my @glyphnames = @{$f->glyphnames}; | ||
| 239 | # Sorted list of glyph names, no duplicates. | ||
| 240 | sub glyphnames { | ||
| 241 | my $self = shift; | ||
| 242 | return $self->{glyphnames} if exists $self->{glyphnames}; | ||
| 243 | my %glyphs = map { $_ => 1 } @{$self->glyphs}; | ||
| 244 | $self->{glyphnames} = [ sort keys %glyphs ]; | ||
| 245 | } | ||
| 246 | |||
| 247 | # $f->write_afm("Arial.afm"); | ||
| 248 | sub write_afm { | ||
| 249 | my ($self, $file) = @_; | ||
| 250 | CORE::open(my $fd, ">", $file) or die("$file: $!\n"); | ||
| 251 | print $fd (${$self->afm_as_string}); | ||
| 252 | close($fd); | ||
| 253 | $self; | ||
| 254 | } | ||
| 255 | |||
| 256 | # my $afmdata = ${$f->afm_as_string}; | ||
| 257 | sub afm_as_string { | ||
| 258 | my ($self) = @_; | ||
| 259 | |||
| 260 | # Read some tables. | ||
| 261 | my $head = $self->{head}->read; | ||
| 262 | my $hhea = $self->{'OS/2'}->read; | ||
| 263 | my $name = $self->{name}->read; | ||
| 264 | my $post = $self->{post}->read; | ||
| 265 | |||
| 266 | # Version. Try to normalize to nnn.nnn. | ||
| 267 | my $version = $self->_str(5); | ||
| 268 | $version = sprintf("%07.3f", $1) if $version =~ /(\d+\.\d+)/; | ||
| 269 | |||
| 270 | # Font bounding box. | ||
| 271 | # Some fonts have dimensions that are 1000 times too small. Let's | ||
| 272 | # do some auto-sensing. | ||
| 273 | my $fix = 1; | ||
| 274 | my $scale = do { | ||
| 275 | my $u = $head->{unitsPerEm}; | ||
| 276 | sub {int($_[0] * $fix / $u)} | ||
| 277 | }; | ||
| 278 | my @bb = map { $scale->($head->{$_}) } qw(xMin yMin xMax yMax); | ||
| 279 | if ( !$bb[0] || !$bb[1] || !$bb[2] || !$bb[3] ) { | ||
| 280 | $fix = 1000; | ||
| 281 | @bb = map { $scale->($head->{$_}) } qw(xMin yMin xMax yMax); | ||
| 282 | } | ||
| 283 | |||
| 284 | # Glyph table. | ||
| 285 | my $glyphs = $self->glyphs; | ||
| 286 | |||
| 287 | # Start AFM information. | ||
| 288 | my $ret = "StartFontMetrics 4.1\n"; | ||
| 289 | |||
| 290 | $ret .= "Comment Creator: " . __PACKAGE__ . " " . $VERSION . | ||
| 291 | " by Johan Vromans\n" . | ||
| 292 | "Comment Creation Date: " . localtime(time) . "\n" . | ||
| 293 | "FontName " . $self->_str(6) . "\n" . | ||
| 294 | "FullName " . $self->_str(4) . "\n" . | ||
| 295 | "FamilyName " . $self->_str(1) . "\n" . | ||
| 296 | "Weight " . $self->_str(2) . "\n" . | ||
| 297 | sprintf("ItalicAngle %s\n" . | ||
| 298 | "IsFixedPitch %s\n" . | ||
| 299 | "FontBBox %d %d %d %d\n" . | ||
| 300 | "UnderlinePosition %d\n" . | ||
| 301 | "UnderlineThickness %d\n", | ||
| 302 | $post->{italicAngle}, | ||
| 303 | $post->{isFixedPitch} ? "true" : "false", | ||
| 304 | @bb, | ||
| 305 | $post->{underlinePosition}, | ||
| 306 | $post->{underlineThickness}, | ||
| 307 | ) . | ||
| 308 | "Version $version\n" . | ||
| 309 | "Notice " . _psstr($self->_str(0)) . "\n" . | ||
| 310 | "EncodingScheme AdobeStandardEncoding\n" . | ||
| 311 | "Ascender " . $scale->($hhea->{sTypoAscender}) . "\n" . | ||
| 312 | "Descender " . $scale->($hhea->{sTypoDescender}) . "\n"; | ||
| 313 | |||
| 314 | #### Encoding. | ||
| 315 | |||
| 316 | # Build reverse encoding hash. | ||
| 317 | my %enc; | ||
| 318 | { require PostScript::StandardEncoding; | ||
| 319 | my $enc = PostScript::StandardEncoding::->array; | ||
| 320 | foreach my $i ( 0..scalar(@$enc)-1 ) { | ||
| 321 | $enc{$enc->[$i]} = $i; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | #### Character Data | ||
| 326 | |||
| 327 | { my @metrics = ("") x 256; | ||
| 328 | my @xmetrics; | ||
| 329 | my $loca = $self->{loca}->read; | ||
| 330 | my $hmtx = $self->{hmtx}->read; | ||
| 331 | my $width = $hmtx->{advance}; | ||
| 332 | my $space; | ||
| 333 | my $nbspace; | ||
| 334 | my $nglyphs = 0; | ||
| 335 | |||
| 336 | $loca->glyphs_do | ||
| 337 | ( sub { my ($glyph, $gix) = @_; | ||
| 338 | $glyph->read_dat; | ||
| 339 | my $name = $glyphs->[$gix]; | ||
| 340 | return if $name eq ".notdef"; | ||
| 341 | my $ix = $enc{$name}; | ||
| 342 | $ix = -1 unless defined($ix); | ||
| 343 | my $ent = sprintf("C %d ; WX %d ; N %s ; B %d %d %d %d ;\n", | ||
| 344 | $ix, | ||
| 345 | $scale->($width->[$gix]), | ||
| 346 | $name, | ||
| 347 | map { $scale->($glyph->{$_}) } | ||
| 348 | qw(xMin yMin xMax yMax)); | ||
| 349 | if ( $ix >= 0 ) { | ||
| 350 | $metrics[$ix] = $ent; | ||
| 351 | } | ||
| 352 | else { | ||
| 353 | push(@xmetrics, $ent); | ||
| 354 | } | ||
| 355 | if ( $name eq "space" ) { | ||
| 356 | $space = $ent; | ||
| 357 | } | ||
| 358 | elsif ( $name =~ /^(nb|nonbreaking)space$/ ) { | ||
| 359 | $nbspace = $ent; | ||
| 360 | } | ||
| 361 | $nglyphs++; | ||
| 362 | } ); | ||
| 363 | |||
| 364 | # Add space and nbspace, if necessary; | ||
| 365 | my $wspace; | ||
| 366 | for ( qw(space nonbreakingspace) ) { | ||
| 367 | next if $_ eq "space" && $space; | ||
| 368 | next if $_ eq "nonbreakingspace" && $nbspace; | ||
| 369 | |||
| 370 | unless ( defined($wspace) ) { | ||
| 371 | my $spx = 0; | ||
| 372 | foreach ( @$glyphs ) { | ||
| 373 | last if $_ eq "space"; | ||
| 374 | $spx++; | ||
| 375 | } | ||
| 376 | $wspace = $scale->($width->[$spx]); | ||
| 377 | } | ||
| 378 | |||
| 379 | my $ix = $enc{$_}; | ||
| 380 | $ix = -1 unless defined($ix); | ||
| 381 | my $ent = "C $ix ; WX $wspace ; N $_ ; B 0 0 0 0 ;\n"; | ||
| 382 | if ( $ix >= 0 ) { | ||
| 383 | $metrics[$ix] = $ent; | ||
| 384 | } | ||
| 385 | else { | ||
| 386 | push(@xmetrics, $ent); | ||
| 387 | } | ||
| 388 | $nglyphs++; | ||
| 389 | } | ||
| 390 | |||
| 391 | # Sort the unencoded glyphs. | ||
| 392 | @xmetrics = map { $_->[1] } | ||
| 393 | sort { $a->[0] cmp $b->[0] } | ||
| 394 | map { [ (split(' ', $_))[7], $_ ] } @xmetrics; | ||
| 395 | |||
| 396 | $ret .= "StartCharMetrics $nglyphs\n" . | ||
| 397 | join("", @metrics, @xmetrics); | ||
| 398 | } | ||
| 399 | |||
| 400 | $ret .= "EndCharMetrics\n\n"; | ||
| 401 | |||
| 402 | #### Kerning Data | ||
| 403 | |||
| 404 | if ( $self->{kern} ) { | ||
| 405 | $ret .= "StartKernData\n"; | ||
| 406 | ### $ret .= "StartTrackKern\n"; | ||
| 407 | ### $ret .= "EndTrackKern\n"; | ||
| 408 | |||
| 409 | my $kern = $self->{kern}->read; | ||
| 410 | my $nkern = 0; | ||
| 411 | |||
| 412 | # Gather the contents of all the kern tables in a single hash. | ||
| 413 | my %k; | ||
| 414 | foreach my $table ( @{$kern->{tables}} ) { | ||
| 415 | my $kerns = $table->{kern}; | ||
| 416 | foreach my $left ( keys %$kerns ) { | ||
| 417 | foreach my $right ( keys %{$kerns->{$left}} ) { | ||
| 418 | $k{$glyphs->[$left]}{$glyphs->[$right]} = | ||
| 419 | $scale->($kerns->{$left}{$right}); | ||
| 420 | $nkern++; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | # Now print the hash, sorted. | ||
| 426 | $ret .= "StartKernPairs $nkern\n"; | ||
| 427 | foreach my $left ( sort keys %k ) { | ||
| 428 | foreach my $right ( sort keys %{$k{$left}} ) { | ||
| 429 | $ret .= "KPX $left $right " . $k{$left}{$right} . "\n"; | ||
| 430 | } | ||
| 431 | $ret .= "\n"; | ||
| 432 | } | ||
| 433 | $ret .= "EndKernPairs\n"; | ||
| 434 | $ret .= "EndKernData\n"; | ||
| 435 | } | ||
| 436 | |||
| 437 | ### $ret .= "StartComposites\n"; | ||
| 438 | ### $ret .= "EndComposites\n"; | ||
| 439 | |||
| 440 | $ret .= "EndFontMetrics\n"; | ||
| 441 | |||
| 442 | \$ret; | ||
| 443 | } | ||
| 444 | |||
| 445 | # Font::TTF::Font uses cyclic structures, so we need this. | ||
| 446 | sub DESTROY { | ||
| 447 | my $self = shift; | ||
| 448 | $self->release; | ||
| 449 | } | ||
| 450 | |||
| 451 | ################ Internal routines ################ | ||
| 452 | |||
| 453 | # Create the directory header for the sfnts strings. | ||
| 454 | sub _dirhdr { | ||
| 455 | my ($tables) = @_; | ||
| 456 | my $searchrange = 1; | ||
| 457 | my $entryselector = 0; | ||
| 458 | |||
| 459 | while ( $searchrange <= $tables ) { | ||
| 460 | $searchrange *= 2; | ||
| 461 | $entryselector++; | ||
| 462 | } | ||
| 463 | $searchrange = 16 * ($searchrange/2); | ||
| 464 | $entryselector--; | ||
| 465 | my $rangeshift = 16 * $tables - $searchrange; | ||
| 466 | |||
| 467 | sprintf("00010000%02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX\n", | ||
| 468 | $tables >> 8, $tables & 0xff, | ||
| 469 | $searchrange >> 8, $searchrange & 0xff, | ||
| 470 | $entryselector >> 8, $entryselector & 0xff, | ||
| 471 | $rangeshift >> 8, $rangeshift & 0xff); | ||
| 472 | } | ||
| 473 | |||
| 474 | # Fetch a PostScript string. | ||
| 475 | sub _str { | ||
| 476 | my ($self, $idx) = @_; | ||
| 477 | # [1] platform = 1 (Apple Unicode) | ||
| 478 | # [0] encoding = 0 (default) | ||
| 479 | # {0} language = 0 (default, English) | ||
| 480 | $self->{name}{strings}[$idx][1][0]{0} || ""; | ||
| 481 | } | ||
| 482 | |||
| 483 | # Generate the define code for a string, if it exists. | ||
| 484 | sub _addstr { | ||
| 485 | my ($self, $ret, $tag, $idx, $name) = @_; | ||
| 486 | my $t = $self->_str($idx); | ||
| 487 | return unless $t ne ""; | ||
| 488 | if ( $name && $t =~ /^[-\w]+$/ ) { | ||
| 489 | $$ret .= "/$tag /$t def\n"; | ||
| 490 | } | ||
| 491 | else { | ||
| 492 | $t = _psstr($t); | ||
| 493 | $$ret .= "/$tag ($t) readonly def\n"; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | # Escape PostScript characters. | ||
| 498 | sub _psstr { | ||
| 499 | my ($str) = @_; | ||
| 500 | $str =~ s/([\\()])/\\$1/g; | ||
| 501 | $str =~ s/([\000-\037\177-\377])/sprintf("\\%03o",ord($1))/eg; | ||
| 502 | $str; | ||
| 503 | } | ||
| 504 | |||
| 505 | # Generate the define code for a number, if it exists. | ||
| 506 | sub _addnum { | ||
| 507 | my ($self, $ret, $tag, $val) = @_; | ||
| 508 | return unless defined $val; | ||
| 509 | $$ret .= "/$tag $val def\n"; | ||
| 510 | } | ||
| 511 | |||
| 512 | # Generate the define code for a boolean, if it exists. | ||
| 513 | sub _addbool { | ||
| 514 | my ($self, $ret, $tag, $val) = @_; | ||
| 515 | return unless defined $val; | ||
| 516 | $$ret .= "/$tag " . | ||
| 517 | ( $val ? "true" : "false" ) . " def\n"; | ||
| 518 | } | ||
| 519 | |||
| 520 | # Get the list of glyphs. | ||
| 521 | sub _getglyphs { | ||
| 522 | my $self = shift; | ||
| 523 | $self->{post}->read; | ||
| 524 | $self->{glyphs} = $self->{post}{VAL}; | ||
| 525 | unless ( $self->{glyphs} ) { | ||
| 526 | warn(__PACKAGE__ . ": No glyphs?\n"); | ||
| 527 | $self->{glyphs} = []; | ||
| 528 | } | ||
| 529 | $self->{glyphs}; | ||
| 530 | } | ||
| 531 | |||
| 532 | # Generate the sfnts strings for the glyph table, splitting it on glyph | ||
| 533 | # boundaries. | ||
| 534 | sub _glyftbl { | ||
| 535 | my ($self, $rd, $rt, $ship, $fd, $glyf_off, $glyf_len, $loca_off, $loca_len) = @_; | ||
| 536 | |||
| 537 | # To split the glyph table we need to find an appropriate glyph | ||
| 538 | # boudary. This requires processing the 'loca' table. | ||
| 539 | |||
| 540 | my $loca = _read_tbl($fd, $loca_off, $loca_len); | ||
| 541 | my $glyf = _read_tbl($fd, $glyf_off, $glyf_len); | ||
| 542 | |||
| 543 | my $glyphs = $self->{maxp}->read->{numGlyphs}; | ||
| 544 | my $locfmt = $self->{head}->read->{indexToLocFormat}; | ||
| 545 | print STDERR ("glyphs = $glyphs, locfmt = $locfmt\n") if $self->{' debug'}; | ||
| 546 | |||
| 547 | my $start = 0; | ||
| 548 | my $off_old = 0; | ||
| 549 | my $off; | ||
| 550 | |||
| 551 | for ( my $i = 0; $i <= $glyphs; $i++ ) { | ||
| 552 | if ( $locfmt ) { | ||
| 553 | $off = unpack("N", substr($$loca, $i*4, 4)); | ||
| 554 | } | ||
| 555 | else { | ||
| 556 | $off = unpack("n", substr($$loca, $i*2, 2)) * 2; | ||
| 557 | } | ||
| 558 | if ( $$rt + $off - $start > CHUNK ) { | ||
| 559 | my $l = $off_old - $start; | ||
| 560 | $$rd .= uc unpack("H".($l+$l), substr($$glyf, $start, $l)); | ||
| 561 | $start += $l; | ||
| 562 | $ship->(); | ||
| 563 | } | ||
| 564 | $off_old = $off; | ||
| 565 | } | ||
| 566 | my $l = $glyf_len - $start; | ||
| 567 | $$rd .= uc unpack("H".($l+$l), substr($$glyf, $start, $l)); | ||
| 568 | $$rt += $l; | ||
| 569 | |||
| 570 | printf STDERR ("glyf ends: data = 0x%x, tally = 0x%x\n", | ||
| 571 | length($$rd), $$rt) if $self->{' debug'}; | ||
| 572 | } | ||
| 573 | |||
| 574 | # Fetch (read) a complete table. | ||
| 575 | sub _read_tbl { | ||
| 576 | my ($fd, $off, $len) = @_; | ||
| 577 | sysseek($fd, $off, 0); | ||
| 578 | my $data = ""; | ||
| 579 | while ( $len > 0 ) { | ||
| 580 | my $l = sysread($fd, $data, $len, length($data)); | ||
| 581 | last if $l == $len; | ||
| 582 | die("read: $!\n") if $l <= 0; | ||
| 583 | $len -= $l; | ||
| 584 | } | ||
| 585 | \$data; | ||
| 586 | } | ||
| 587 | |||
| 588 | 1; | ||
| 589 | |||
| 590 | __END__ |