| 1 | package Text::PDF::TTFont; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Text::PDF::TTFont - Inherits from L<Text::PDF::Dict> and represents a TrueType | ||
| 6 | font within a PDF file. | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | A font consists of two primary parts in a PDF file: the header and the font | ||
| 11 | descriptor. Whilst two fonts may share font descriptors, they will have their | ||
| 12 | own header dictionaries including encoding and widhth information. | ||
| 13 | |||
| 14 | =head1 INSTANCE VARIABLES | ||
| 15 | |||
| 16 | There are no instance variables beyond the variables which directly correspond | ||
| 17 | to entries in the appropriate PDF dictionaries. | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use vars qw(@ISA @cp1252 $subcount); | ||
| 25 | # no warnings qw(uninitialized); | ||
| 26 | |||
| 27 | use Text::PDF::Dict; | ||
| 28 | use Text::PDF::Utils; | ||
| 29 | @ISA = qw(Text::PDF::Dict); | ||
| 30 | |||
| 31 | use Font::TTF::Font 0.23; | ||
| 32 | |||
| 33 | @cp1252 = (0 .. 127, | ||
| 34 | 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, | ||
| 35 | 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, | ||
| 36 | 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, | ||
| 37 | 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178, | ||
| 38 | 0xA0 .. 0xFF); | ||
| 39 | |||
| 40 | $subcount = "BXCJIM"; | ||
| 41 | |||
| 42 | =head2 Text::PDF::TTFont->new($parent, $fontfname, $pdfname, %opts) | ||
| 43 | |||
| 44 | Creates a new font resource for the given fontfile. This includes the font | ||
| 45 | descriptor and the font stream. The $pdfname is the name by which this font | ||
| 46 | resource will be known throught a particular PDF file. | ||
| 47 | |||
| 48 | All font resources are full PDF objects. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub new | ||
| 53 | { | ||
| 54 | my ($class, $parent, $fontname, $pdfname, %opts) = @_; | ||
| 55 | my ($self) = $class->SUPER::new; | ||
| 56 | my ($f, $flags, $name, $subf, $s, $upem); | ||
| 57 | my ($font, $w); | ||
| 58 | |||
| 59 | foreach $f (keys %opts) | ||
| 60 | { | ||
| 61 | $f =~ s/^\-//o || next; | ||
| 62 | $self->{" $f"} = $opts{"-$f"}; | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->{' outto'} = $parent; # only one host for a font | ||
| 66 | if (ref($fontname)) # $fontname is a font object | ||
| 67 | { $font = $fontname; } | ||
| 68 | else | ||
| 69 | { $font = Font::TTF::Font->open($fontname) || return undef; } | ||
| 70 | |||
| 71 | $self->{' font'} = $font; | ||
| 72 | $Font::TTF::Name::utf8 = 1; | ||
| 73 | |||
| 74 | $self->{'Type'} = PDFName("Font"); | ||
| 75 | $self->{'Subtype'} = PDFName("TrueType"); | ||
| 76 | if ($self->{' subset'}) | ||
| 77 | { | ||
| 78 | $self->{' subname'} = "$subcount+" . $name; | ||
| 79 | $subcount++; | ||
| 80 | } | ||
| 81 | else | ||
| 82 | { $self->{' subname'} = $name; } | ||
| 83 | $name = $font->{'name'}->read->find_name(4) || return undef; | ||
| 84 | $subf = $font->{'name'}->find_name(2); | ||
| 85 | $name =~ s/\s//og; | ||
| 86 | $name .= $subf if ($subf =~ m/^Regular$/oi); | ||
| 87 | $self->{'BaseFont'} = PDFName($self->{' subname'}); | ||
| 88 | $self->{'Name'} = PDFName($pdfname); | ||
| 89 | $parent->new_obj($self); | ||
| 90 | # leave the encoding & widths, etc. until we know the glyph list | ||
| 91 | |||
| 92 | $f = PDFDict(); | ||
| 93 | $parent->new_obj($f); # make this thing a true object | ||
| 94 | $self->{'FontDescriptor'} = $f; | ||
| 95 | $f->{'Type'} = PDFName("FontDescriptor"); | ||
| 96 | $upem = $font->{'head'}->read->{'unitsPerEm'}; | ||
| 97 | $f->{'Ascent'} = PDFNum(int($font->{'hhea'}->read->{'Ascender'} * 1000 / $upem)); | ||
| 98 | $f->{'Descent'} = PDFNum(int($font->{'hhea'}{'Descender'} * 1000 / $upem)); | ||
| 99 | |||
| 100 | # find the top of an H or the null box! Or maybe we should just duck and say 0? | ||
| 101 | $f->{'CapHeight'} = PDFNum(0); | ||
| 102 | # int($font->{'loca'}->read->{'glyphs'}[$font->{'post'}{'STRINGS'}{"H"}]->read->{'yMax'} | ||
| 103 | # * 1000 / $upem)); | ||
| 104 | $f->{'StemV'} = PDFNum(0); # no way! | ||
| 105 | $f->{'FontName'} = $self->{'BaseFont'}; | ||
| 106 | $f->{'ItalicAngle'} = PDFNum($font->{'post'}->read->{'italicAngle'}); | ||
| 107 | $f->{'FontBBox'} = PDFArray( | ||
| 108 | PDFNum(int($font->{'head'}{'xMin'} * 1000 / $upem)), | ||
| 109 | PDFNum(int($font->{'head'}{'yMin'} * 1000 / $upem)), | ||
| 110 | PDFNum(int($font->{'head'}{'xMax'} * 1000 / $upem)), | ||
| 111 | PDFNum(int($font->{'head'}{'yMax'} * 1000 / $upem))); | ||
| 112 | |||
| 113 | $flags = 4; | ||
| 114 | $flags = 0; | ||
| 115 | $flags |= 1 if ($font->{'OS/2'}->read->{'bProportion'} == 9); | ||
| 116 | $flags |= 2 unless ($font->{'OS/2'}{'bSerifStyle'} > 10 && $font->{'OS/2'}{'bSerifStyle'} < 14); | ||
| 117 | $flags |= 32; # if ($font->{'OS/2'}{'bFamilyType'} > 3); | ||
| 118 | $flags |= 8 if ($font->{'OS/2'}{'bFamilyType'} == 2); | ||
| 119 | $flags |= 64 if ($font->{'OS/2'}{'bLetterform'} > 8); | ||
| 120 | $f->{'Flags'} = PDFNum($flags); | ||
| 121 | |||
| 122 | # $f->{'MaxWidth'} = PDFNum(int($font->{'hhea'}{'advanceWidthMax'} * 1000 / $upem)); | ||
| 123 | $f->{'MissingWidth'} = PDFNum(int($font->{'hhea'}{'advanceWidthMax'} * 1000 / $upem) + 2); | ||
| 124 | $f->{' notdef'} = PDFNum(".notdef"); | ||
| 125 | |||
| 126 | $s = PDFDict(); | ||
| 127 | $parent->new_obj($s); | ||
| 128 | $f->{'FontFile2'} = $s; | ||
| 129 | $s->{'Length1'} = PDFNum(-s $font->{' fname'}); | ||
| 130 | $s->{'Filter'} = PDFArray(PDFName("FlateDecode")); | ||
| 131 | $s->{' streamfile'} = $fontname unless ($self->{' subset'}); | ||
| 132 | |||
| 133 | $font->{'cmap'}->read->find_ms; | ||
| 134 | $self->{' issymbol'} = $font->{'cmap'}{' mstable'}{'Platform'} == 3 && $font->{'cmap'}{' mstable'}{'Encoding'} == 0; | ||
| 135 | $font->{'hmtx'}->read; | ||
| 136 | unless ($opts{'-istype0'}) | ||
| 137 | { | ||
| 138 | $w = PDFArray(map {PDFNum(int($font->{'hmtx'}{'advance'}[$font->{'cmap'}->ms_lookup($_)] / $font->{'head'}{'unitsPerEm'} * 1000))} | ||
| 139 | $self->{' issymbol'} ? (0xf000 .. 0xf0ff) : @cp1252); | ||
| 140 | $parent->new_obj($w); | ||
| 141 | $self->{'Widths'} = $w; | ||
| 142 | } | ||
| 143 | if ($self->{' subset'}) | ||
| 144 | { | ||
| 145 | $self->{' minCode'} = 255; | ||
| 146 | $self->{' maxCode'} = 32; | ||
| 147 | } else | ||
| 148 | { | ||
| 149 | $self->{' minCode'} = 32; | ||
| 150 | $self->{' maxCode'} = 255; | ||
| 151 | } | ||
| 152 | $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 $t->width($text) | ||
| 156 | |||
| 157 | Measures the width of the given text according to the widths in the font | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub width | ||
| 162 | { | ||
| 163 | my ($self, $text) = @_; | ||
| 164 | my (@unis, $width); | ||
| 165 | |||
| 166 | if ($self->{' issymbol'}) | ||
| 167 | { @unis = map {$_ + 0xf000} unpack("C*", $text); } | ||
| 168 | else | ||
| 169 | { @unis = map {$cp1252[$_]} unpack("C*", $text); } | ||
| 170 | |||
| 171 | foreach (@unis) | ||
| 172 | { $width += $self->{' font'}{'hmtx'}{'advance'}[$self->{' font'}{'cmap'}->ms_lookup($_)]; } | ||
| 173 | $width / $self->{' font'}{'head'}{'unitsPerEm'}; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | =head2 $t->trim($text, $len) | ||
| 178 | |||
| 179 | Trims the given text to the given length (in per mille em) returning the trimmed | ||
| 180 | text | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub trim | ||
| 185 | { | ||
| 186 | my ($self, $text, $len) = @_; | ||
| 187 | my ($i, $width); | ||
| 188 | |||
| 189 | $len *= $self->{' font'}{'head'}{'unitsPerEm'}; | ||
| 190 | |||
| 191 | foreach (unpack("C*", $text)) | ||
| 192 | { | ||
| 193 | $width += $self->{' font'}{'hmtx'}{'advance'}[$self->{' font'}{'cmap'}->ms_lookup( | ||
| 194 | $self->{' issymbol'} ? $_ + 0xf000 : $cp1252[$_])]; | ||
| 195 | last if ($width > $len); | ||
| 196 | $i++; | ||
| 197 | } | ||
| 198 | return substr($text, 0, $i); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | =head2 $t->out_text($text) | ||
| 203 | |||
| 204 | Indicates to the font that the text is to be output and returns the text to be output | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub out_text | ||
| 209 | { | ||
| 210 | my ($self, $text) = @_; | ||
| 211 | |||
| 212 | if ($self->{' subset'}) | ||
| 213 | { | ||
| 214 | foreach (unpack("C*", $text)) | ||
| 215 | { | ||
| 216 | vec($self->{' subvec'}, $_, 1) = 1; | ||
| 217 | $self->{' minCode'} = $_ if $_ < $self->{' minCode'}; | ||
| 218 | $self->{' maxCode'} = $_ if $_ > $self->{' maxCode'}; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return asPDFStr($text); | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | =head2 $f->copy | ||
| 226 | |||
| 227 | Copies the font object excluding the name, widths and encoding, etc. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub copy | ||
| 232 | { | ||
| 233 | my ($self, $pdf) = @_; | ||
| 234 | my ($res) = {}; | ||
| 235 | my ($k); | ||
| 236 | |||
| 237 | bless $res, ref($self); | ||
| 238 | foreach $k ('Name', 'FirstChar', 'LastChar') | ||
| 239 | { $res->{$k} = ""; } | ||
| 240 | return $self->SUPER::copy($pdf, $res); | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub outobjdeep | ||
| 245 | { | ||
| 246 | my ($self, $fh, $pdf, %opts) = @_; | ||
| 247 | |||
| 248 | return $self->SUPER::outobjdeep($fh, $pdf) if defined $opts{'passthru'}; | ||
| 249 | |||
| 250 | my ($f) = $self->{' font'}; | ||
| 251 | my ($d) = $self->{'FontDescriptor'}; | ||
| 252 | my ($s) = $d->{'FontFile2'}; | ||
| 253 | my ($vec, $ffh, $i, $t, $k, $maxuni, $minuni); | ||
| 254 | |||
| 255 | $self->{'FirstChar'} = PDFNum($self->{' minCode'}); | ||
| 256 | $self->{'LastChar'} = PDFNum($self->{' maxCode'}); | ||
| 257 | splice(@{$self->{'Widths'}{' val'}}, 0, $self->{' minCode'}); | ||
| 258 | splice(@{$self->{'Widths'}{' val'}}, $self->{' maxCode'} - $self->{' minCode'} + 1, $#{$self->{'Widths'}{' val'}}); | ||
| 259 | if ($self->{' subset'}) | ||
| 260 | { | ||
| 261 | $maxuni = 0; $minuni = 0xffff; | ||
| 262 | for ($i = 0; $i < 256; $i++) | ||
| 263 | { | ||
| 264 | if (vec($self->{' subvec'}, $i, 1)) | ||
| 265 | { | ||
| 266 | $t = $self->{' issymbol'} ? $i + 0xf000 : $cp1252[$i]; | ||
| 267 | $maxuni = $t if $t > $maxuni; | ||
| 268 | $minuni = $t if $t < $minuni; | ||
| 269 | vec($vec, $f->{'cmap'}->ms_lookup($t), 1) = 1; | ||
| 270 | } | ||
| 271 | elsif ($i >= $self->{' minCode'} && $i <= $self->{' maxCode'}) | ||
| 272 | { $self->{'Widths'}{' val'}[$i - $self->{' minCode'}] = $d->{'MissingWidth'}; } | ||
| 273 | } | ||
| 274 | $f->{'glyf'}->read; | ||
| 275 | for ($i = 0; $i < scalar @{$f->{'loca'}{'glyphs'}}; $i++) | ||
| 276 | { | ||
| 277 | next if vec($vec, $i, 1); | ||
| 278 | $f->{'loca'}{'glyphs'}[$i] = undef; | ||
| 279 | } | ||
| 280 | $s->{' stream'} = ""; | ||
| 281 | $ffh = Text::PDF::TTIOString->new(\$s->{' stream'}); | ||
| 282 | $f->out($ffh, 'cmap', 'cvt ', 'fpgm', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'prep'); | ||
| 283 | $s->{'Length1'} = PDFNum(length($s->{' stream'})); | ||
| 284 | } | ||
| 285 | |||
| 286 | $self->SUPER::outobjdeep($fh, $pdf, %opts); | ||
| 287 | } | ||
| 288 | |||
| 289 | 1; | ||
| 290 | |||
| 291 | package Text::PDF::TTIOString; | ||
| 292 | |||
| 293 | =head1 TITLE | ||
| 294 | |||
| 295 | Text::PDF::TTIOString - internal IO type handle for string output for font | ||
| 296 | embedding. This code is ripped out of IO::Scalar, to save the direct dependence | ||
| 297 | for so little. See IO::Scalar for details | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | sub new { | ||
| 302 | my $self = bless {}, shift; | ||
| 303 | $self->open(@_) if @_; | ||
| 304 | $self; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub DESTROY { | ||
| 308 | shift->close; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | sub open { | ||
| 313 | my ($self, $sref) = @_; | ||
| 314 | |||
| 315 | # Sanity: | ||
| 316 | defined($sref) or do {my $s = ''; $sref = \$s}; | ||
| 317 | (ref($sref) eq "SCALAR") or die "open() needs a ref to a scalar"; | ||
| 318 | |||
| 319 | # Setup: | ||
| 320 | $self->{Pos} = 0; | ||
| 321 | $self->{SR} = $sref; | ||
| 322 | $self; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub close { | ||
| 326 | my $self = shift; | ||
| 327 | %$self = (); | ||
| 328 | 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub getc { | ||
| 332 | my $self = shift; | ||
| 333 | |||
| 334 | # Return undef right away if at EOF; else, move pos forward: | ||
| 335 | return undef if $self->eof; | ||
| 336 | substr(${$self->{SR}}, $self->{Pos}++, 1); | ||
| 337 | } | ||
| 338 | |||
| 339 | if(0) | ||
| 340 | { | ||
| 341 | sub getline { | ||
| 342 | my $self = shift; | ||
| 343 | |||
| 344 | # Return undef right away if at EOF: | ||
| 345 | return undef if $self->eof; | ||
| 346 | |||
| 347 | # Get next line: | ||
| 348 | pos(${$self->{SR}}) = $self->{Pos}; # start matching at this point | ||
| 349 | ${$self->{SR}} =~ m/(.*?)(\n|\Z)/g; # match up to newline or EOS | ||
| 350 | my $line = $1.$2; # save it | ||
| 351 | $self->{Pos} += length($line); # everybody remember where we parked! | ||
| 352 | return $line; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub getlines { | ||
| 356 | my $self = shift; | ||
| 357 | wantarray or croak("Can't call getlines in scalar context!"); | ||
| 358 | my ($line, @lines); | ||
| 359 | push @lines, $line while (defined($line = $self->getline)); | ||
| 360 | @lines; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | sub print { | ||
| 365 | my $self = shift; | ||
| 366 | my $eofpos = length(${$self->{SR}}); | ||
| 367 | my $str = join('', @_); | ||
| 368 | |||
| 369 | if ($self->{'Pos'} == $eofpos) | ||
| 370 | { | ||
| 371 | ${$self->{SR}} .= $str; | ||
| 372 | $self->{Pos} = length(${$self->{SR}}); | ||
| 373 | } else | ||
| 374 | { | ||
| 375 | substr(${$self->{SR}}, $self->{Pos}, length($str)) = $str; | ||
| 376 | $self->{Pos} += length($str); | ||
| 377 | } | ||
| 378 | 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub read { | ||
| 382 | my ($self, $buf, $n, $off) = @_; | ||
| 383 | die "OFFSET not yet supported" if defined($off); | ||
| 384 | my $read = substr(${$self->{SR}}, $self->{Pos}, $n); | ||
| 385 | $self->{Pos} += length($read); | ||
| 386 | $_[1] = $read; | ||
| 387 | return length($read); | ||
| 388 | } | ||
| 389 | |||
| 390 | sub eof { | ||
| 391 | my $self = shift; | ||
| 392 | ($self->{Pos} >= length(${$self->{SR}})); | ||
| 393 | } | ||
| 394 | |||
| 395 | sub seek { | ||
| 396 | my ($self, $pos, $whence) = @_; | ||
| 397 | my $eofpos = length(${$self->{SR}}); | ||
| 398 | |||
| 399 | # Seek: | ||
| 400 | if ($whence == 0) { $self->{Pos} = $pos } # SEEK_SET | ||
| 401 | elsif ($whence == 1) { $self->{Pos} += $pos } # SEEK_CUR | ||
| 402 | elsif ($whence == 2) { $self->{Pos} = $eofpos + $pos} # SEEK_END | ||
| 403 | else { die "bad seek whence ($whence)" } | ||
| 404 | |||
| 405 | # Fixup: | ||
| 406 | if ($self->{Pos} < 0) { $self->{Pos} = 0 } | ||
| 407 | if ($self->{Pos} > $eofpos) { $self->{Pos} = $eofpos } | ||
| 408 | 1; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub tell { shift->{Pos} } | ||
| 412 | |||
| 413 | 1; | ||
| 414 |