/usr/share/perl5/Text/PDF/TTFont.pm is in libtext-pdf-perl 0.29a-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | package Text::PDF::TTFont;
=head1 NAME
Text::PDF::TTFont - Inherits from L<Text::PDF::Dict> and represents a TrueType
font within a PDF file.
=head1 DESCRIPTION
A font consists of two primary parts in a PDF file: the header and the font
descriptor. Whilst two fonts may share font descriptors, they will have their
own header dictionaries including encoding and widhth information.
=head1 INSTANCE VARIABLES
There are no instance variables beyond the variables which directly correspond
to entries in the appropriate PDF dictionaries.
=head1 METHODS
=cut
use strict;
use vars qw(@ISA @cp1252 $subcount);
# no warnings qw(uninitialized);
use Text::PDF::Dict;
use Text::PDF::Utils;
@ISA = qw(Text::PDF::Dict);
use Font::TTF::Font 0.23;
@cp1252 = (0 .. 127,
0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F,
0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
0xA0 .. 0xFF);
$subcount = "BXCJIM";
=head2 Text::PDF::TTFont->new($parent, $fontfname, $pdfname, %opts)
Creates a new font resource for the given fontfile. This includes the font
descriptor and the font stream. The $pdfname is the name by which this font
resource will be known through a particular PDF file.
All font resources are full PDF objects.
=cut
sub new
{
my ($class, $parent, $fontname, $pdfname, %opts) = @_;
my ($self) = $class->SUPER::new;
my ($f, $flags, $name, $subf, $s, $upem);
my ($font, $w);
foreach $f (keys %opts)
{
$f =~ s/^\-//o || next;
$self->{" $f"} = $opts{"-$f"};
}
$self->{' outto'} = $parent; # only one host for a font
if (ref($fontname)) # $fontname is a font object
{ $font = $fontname; }
else
{ $font = Font::TTF::Font->open($fontname) || return undef; }
$self->{' font'} = $font;
$Font::TTF::Name::utf8 = 1;
$self->{'Type'} = PDFName("Font");
$self->{'Subtype'} = PDFName("TrueType");
if ($self->{' subset'})
{
$self->{' subname'} = "$subcount+";
$subcount++;
}
$name = $font->{'name'}->read->find_name(4) || return undef;
$subf = $font->{'name'}->find_name(2);
$name =~ s/\s//og;
$name .= $subf if ($subf =~ m/^Regular$/oi);
$self->{'BaseFont'} = PDFName($self->{' subname'} . $name);
$subcount++;
$self->{'Name'} = PDFName($pdfname);
$parent->new_obj($self);
# leave the encoding & widths, etc. until we know the glyph list
$f = PDFDict();
$parent->new_obj($f); # make this thing a true object
$self->{'FontDescriptor'} = $f;
$f->{'Type'} = PDFName("FontDescriptor");
$upem = $font->{'head'}->read->{'unitsPerEm'};
$f->{'Ascent'} = PDFNum(int($font->{'hhea'}->read->{'Ascender'} * 1000 / $upem));
$f->{'Descent'} = PDFNum(int($font->{'hhea'}{'Descender'} * 1000 / $upem));
# find the top of an H or the null box! Or maybe we should just duck and say 0?
$f->{'CapHeight'} = PDFNum(0);
# int($font->{'loca'}->read->{'glyphs'}[$font->{'post'}{'STRINGS'}{"H"}]->read->{'yMax'}
# * 1000 / $upem));
$f->{'StemV'} = PDFNum(0); # no way!
$f->{'FontName'} = $self->{'BaseFont'};
$f->{'ItalicAngle'} = PDFNum($font->{'post'}->read->{'italicAngle'});
$f->{'FontBBox'} = PDFArray(
PDFNum(int($font->{'head'}{'xMin'} * 1000 / $upem)),
PDFNum(int($font->{'head'}{'yMin'} * 1000 / $upem)),
PDFNum(int($font->{'head'}{'xMax'} * 1000 / $upem)),
PDFNum(int($font->{'head'}{'yMax'} * 1000 / $upem)));
$flags = 4;
$flags = 0;
$flags |= 1 if ($font->{'OS/2'}->read->{'bProportion'} == 9);
$flags |= 2 unless ($font->{'OS/2'}{'bSerifStyle'} > 10 && $font->{'OS/2'}{'bSerifStyle'} < 14);
$flags |= 32; # if ($font->{'OS/2'}{'bFamilyType'} > 3);
$flags |= 8 if ($font->{'OS/2'}{'bFamilyType'} == 2);
$flags |= 64 if ($font->{'OS/2'}{'bLetterform'} > 8);
$f->{'Flags'} = PDFNum($flags);
# $f->{'MaxWidth'} = PDFNum(int($font->{'hhea'}{'advanceWidthMax'} * 1000 / $upem));
$f->{'MissingWidth'} = PDFNum(int($font->{'hhea'}{'advanceWidthMax'} * 1000 / $upem) + 2);
$f->{' notdef'} = PDFNum(".notdef");
$s = PDFDict();
$parent->new_obj($s);
$f->{'FontFile2'} = $s;
$s->{'Length1'} = PDFNum(-s $font->{' fname'});
$s->{'Filter'} = PDFArray(PDFName("FlateDecode"));
$s->{' streamfile'} = $fontname unless ($self->{' subset'});
$font->{'cmap'}->read->find_ms;
$self->{' issymbol'} = $font->{'cmap'}{' mstable'}{'Platform'} == 3 && $font->{'cmap'}{' mstable'}{'Encoding'} == 0;
$font->{'hmtx'}->read;
unless ($opts{'-istype0'})
{
$w = PDFArray(map {PDFNum(int($font->{'hmtx'}{'advance'}[$font->{'cmap'}->ms_lookup($_)] / $font->{'head'}{'unitsPerEm'} * 1000))}
$self->{' issymbol'} ? (0xf000 .. 0xf0ff) : @cp1252);
$parent->new_obj($w);
$self->{'Widths'} = $w;
}
if ($self->{' subset'})
{
$self->{' minCode'} = 255;
$self->{' maxCode'} = 32;
} else
{
$self->{' minCode'} = 32;
$self->{' maxCode'} = 255;
}
$self;
}
=head2 $t->width($text)
Measures the width of the given text according to the widths in the font
=cut
sub width
{
my ($self, $text) = @_;
my (@unis, $width);
if ($self->{' issymbol'})
{ @unis = map {$_ + 0xf000} unpack("C*", $text); }
else
{ @unis = map {$cp1252[$_]} unpack("C*", $text); }
foreach (@unis)
{ $width += $self->{' font'}{'hmtx'}{'advance'}[$self->{' font'}{'cmap'}->ms_lookup($_)]; }
$width / $self->{' font'}{'head'}{'unitsPerEm'};
}
=head2 $t->trim($text, $len)
Trims the given text to the given length (in per mille em) returning the trimmed
text
=cut
sub trim
{
my ($self, $text, $len) = @_;
my ($i, $width);
$len *= $self->{' font'}{'head'}{'unitsPerEm'};
foreach (unpack("C*", $text))
{
$width += $self->{' font'}{'hmtx'}{'advance'}[$self->{' font'}{'cmap'}->ms_lookup(
$self->{' issymbol'} ? $_ + 0xf000 : $cp1252[$_])];
last if ($width > $len);
$i++;
}
return substr($text, 0, $i);
}
=head2 $t->out_text($text)
Indicates to the font that the text is to be output and returns the text to be output
=cut
sub out_text
{
my ($self, $text) = @_;
if ($self->{' subset'})
{
foreach (unpack("C*", $text))
{
vec($self->{' subvec'}, $_, 1) = 1;
$self->{' minCode'} = $_ if $_ < $self->{' minCode'};
$self->{' maxCode'} = $_ if $_ > $self->{' maxCode'};
}
}
return asPDFStr($text);
}
=head2 $f->copy
Copies the font object excluding the name, widths and encoding, etc.
=cut
sub copy
{
my ($self, $pdf) = @_;
my ($res) = {};
my ($k);
bless $res, ref($self);
foreach $k ('Name', 'FirstChar', 'LastChar')
{ $res->{$k} = ""; }
return $self->SUPER::copy($pdf, $res);
}
sub outobjdeep
{
my ($self, $fh, $pdf, %opts) = @_;
return $self->SUPER::outobjdeep($fh, $pdf) if defined $opts{'passthru'};
my ($f) = $self->{' font'};
my ($d) = $self->{'FontDescriptor'};
my ($s) = $d->{'FontFile2'};
my ($vec, $ffh, $i, $t, $k, $maxuni, $minuni);
$self->{'FirstChar'} = PDFNum($self->{' minCode'});
$self->{'LastChar'} = PDFNum($self->{' maxCode'});
splice(@{$self->{'Widths'}{' val'}}, 0, $self->{' minCode'});
splice(@{$self->{'Widths'}{' val'}}, $self->{' maxCode'} - $self->{' minCode'} + 1, $#{$self->{'Widths'}{' val'}});
if ($self->{' subset'})
{
$maxuni = 0; $minuni = 0xffff;
for ($i = 0; $i < 256; $i++)
{
if (vec($self->{' subvec'}, $i, 1))
{
$t = $self->{' issymbol'} ? $i + 0xf000 : $cp1252[$i];
$maxuni = $t if $t > $maxuni;
$minuni = $t if $t < $minuni;
vec($vec, $f->{'cmap'}->ms_lookup($t), 1) = 1;
}
elsif ($i >= $self->{' minCode'} && $i <= $self->{' maxCode'})
{ $self->{'Widths'}{' val'}[$i - $self->{' minCode'}] = $d->{'MissingWidth'}; }
}
$f->{'glyf'}->read;
for ($i = 0; $i < scalar @{$f->{'loca'}{'glyphs'}}; $i++)
{
next if vec($vec, $i, 1);
$f->{'loca'}{'glyphs'}[$i] = undef;
}
$s->{' stream'} = "";
$ffh = Text::PDF::TTIOString->new(\$s->{' stream'});
$f->out($ffh, 'cmap', 'cvt ', 'fpgm', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'prep');
$s->{'Length1'} = PDFNum(length($s->{' stream'}));
}
$self->SUPER::outobjdeep($fh, $pdf, %opts);
}
1;
package Text::PDF::TTIOString;
=head1 TITLE
Text::PDF::TTIOString - internal IO type handle for string output for font
embedding. This code is ripped out of IO::Scalar, to save the direct dependence
for so little. See IO::Scalar for details
=cut
sub new {
my $self = bless {}, shift;
$self->open(@_) if @_;
$self;
}
sub DESTROY {
shift->close;
}
sub open {
my ($self, $sref) = @_;
# Sanity:
defined($sref) or do {my $s = ''; $sref = \$s};
(ref($sref) eq "SCALAR") or die "open() needs a ref to a scalar";
# Setup:
$self->{Pos} = 0;
$self->{SR} = $sref;
$self;
}
sub close {
my $self = shift;
%$self = ();
1;
}
sub getc {
my $self = shift;
# Return undef right away if at EOF; else, move pos forward:
return undef if $self->eof;
substr(${$self->{SR}}, $self->{Pos}++, 1);
}
if(0)
{
sub getline {
my $self = shift;
# Return undef right away if at EOF:
return undef if $self->eof;
# Get next line:
pos(${$self->{SR}}) = $self->{Pos}; # start matching at this point
${$self->{SR}} =~ m/(.*?)(\n|\Z)/g; # match up to newline or EOS
my $line = $1.$2; # save it
$self->{Pos} += length($line); # everybody remember where we parked!
return $line;
}
sub getlines {
my $self = shift;
wantarray or croak("Can't call getlines in scalar context!");
my ($line, @lines);
push @lines, $line while (defined($line = $self->getline));
@lines;
}
}
sub print {
my $self = shift;
my $eofpos = length(${$self->{SR}});
my $str = join('', @_);
if ($self->{'Pos'} == $eofpos)
{
${$self->{SR}} .= $str;
$self->{Pos} = length(${$self->{SR}});
} else
{
substr(${$self->{SR}}, $self->{Pos}, length($str)) = $str;
$self->{Pos} += length($str);
}
1;
}
sub read {
my ($self, $buf, $n, $off) = @_;
die "OFFSET not yet supported" if defined($off);
my $read = substr(${$self->{SR}}, $self->{Pos}, $n);
$self->{Pos} += length($read);
$_[1] = $read;
return length($read);
}
sub eof {
my $self = shift;
($self->{Pos} >= length(${$self->{SR}}));
}
sub seek {
my ($self, $pos, $whence) = @_;
my $eofpos = length(${$self->{SR}});
# Seek:
if ($whence == 0) { $self->{Pos} = $pos } # SEEK_SET
elsif ($whence == 1) { $self->{Pos} += $pos } # SEEK_CUR
elsif ($whence == 2) { $self->{Pos} = $eofpos + $pos} # SEEK_END
else { die "bad seek whence ($whence)" }
# Fixup:
if ($self->{Pos} < 0) { $self->{Pos} = 0 }
if ($self->{Pos} > $eofpos) { $self->{Pos} = $eofpos }
1;
}
sub tell { shift->{Pos} }
1;
|