| 1 | package Text::PDF::TTFont0; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Text::PDF::TTFont0 - Inherits from L<PDF::Dict> and represents a TrueType Type 0 | ||
| 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); | ||
| 25 | # no warnings qw(uninitialized); | ||
| 26 | |||
| 27 | use Text::PDF::TTFont; | ||
| 28 | use Text::PDF::Dict; | ||
| 29 | @ISA = qw(Text::PDF::TTFont); | ||
| 30 | |||
| 31 | use Font::TTF::Font; | ||
| 32 | use Text::PDF::Utils; | ||
| 33 | |||
| 34 | =head2 Text::PDF::TTFont->new($parent, $fontfname. $pdfname) | ||
| 35 | |||
| 36 | Creates a new font resource for the given fontfile. This includes the font | ||
| 37 | descriptor and the font stream. The $pdfname is the name by which this font | ||
| 38 | resource will be known throughout a particular PDF file. | ||
| 39 | |||
| 40 | All font resources are full PDF objects. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new | ||
| 45 | { | ||
| 46 | my ($class, $parent, $fontname, $pdfname, %opt) = @_; | ||
| 47 | my ($desc, $sinfo, $unistr, $touni, @rev); | ||
| 48 | my ($i, $first, $num, $upem, @wid, $name, $ff2, $ffh); | ||
| 49 | |||
| 50 | my ($self) = $class->SUPER::new($parent, $fontname, $pdfname, -istype0 => 1, %opt); | ||
| 51 | my ($font) = $self->{' font'}; | ||
| 52 | |||
| 53 | $self->{'Subtype'} = PDFName('Type0'); | ||
| 54 | $self->{'Encoding'} = PDFName('Identity-H'); | ||
| 55 | |||
| 56 | $parent->{' version'} = 3 unless (defined $parent->{' version'} && $parent->{' version'} > 3); | ||
| 57 | $desc = PDFDict(); | ||
| 58 | $parent->new_obj($desc); | ||
| 59 | $desc->{'Type'} = $self->{'Type'}; | ||
| 60 | $desc->{'Subtype'} = PDFName('CIDFontType2'); | ||
| 61 | $desc->{'BaseFont'} = $self->{'BaseFont'}; | ||
| 62 | # $name = $self->{'BaseFont'}->val; | ||
| 63 | # $name =~ s/^.*\+//oi; | ||
| 64 | # $self->{'BaseFont'} = PDF::Name->new($parent, $name . "-Identity-H"); | ||
| 65 | $desc->{'FontDescriptor'} = $self->{'FontDescriptor'}; | ||
| 66 | delete $self->{'FontDescriptor'}; | ||
| 67 | |||
| 68 | $num = $font->{'maxp'}{'numGlyphs'}; | ||
| 69 | $upem = $font->{'head'}{'unitsPerEm'}; | ||
| 70 | $desc->{'DW'} = $desc->{'FontDescriptor'}{'MissingWidth'}; | ||
| 71 | $desc->{'W'} = PDFArray(); | ||
| 72 | $parent->new_obj($desc->{'W'}); | ||
| 73 | $font->{'hmtx'}->read; | ||
| 74 | unless ($opt{-subset}) | ||
| 75 | { | ||
| 76 | $first = 1; | ||
| 77 | for ($i = 1; $i < $num; $i++) | ||
| 78 | { | ||
| 79 | push(@wid, PDFNum(int($font->{'hmtx'}{'advance'}[$i] * 1000 / $upem))); | ||
| 80 | if ($i % 20 == 19 || $i + 1 >= $num) | ||
| 81 | { | ||
| 82 | $desc->{'W'}->add_elements(PDFNum($first), | ||
| 83 | PDFArray(@wid)); | ||
| 84 | @wid = (); | ||
| 85 | $first = $i + 1; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | $self->{'DescendantFonts'} = PDFArray($desc); | ||
| 91 | |||
| 92 | $sinfo = PDFDict(); | ||
| 93 | # $parent->new_obj($sinfo); | ||
| 94 | $sinfo->{'Registry'} = PDFStr('Adobe'); | ||
| 95 | $sinfo->{'Ordering'} = PDFStr('Identity'); | ||
| 96 | $sinfo->{'Supplement'} = PDFNum(0); | ||
| 97 | $desc->{'CIDSystemInfo'} = $sinfo; | ||
| 98 | $ff2 = $desc->{'FontDescriptor'}{'FontFile2'}; | ||
| 99 | delete $ff2->{' streamfile'}; | ||
| 100 | # $ff2->{' stream'} = ""; | ||
| 101 | # $ffh = Text::PDF::TTIOString->new(\$ff2->{' stream'}); | ||
| 102 | # $font->out($ffh, 'cvt ', 'fpgm', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'prep'); | ||
| 103 | # $ff2->{'Filter'} = PDFArray(PDFName("FlateDecode")); | ||
| 104 | # $ff2->{'Length1'} = PDFNum(length($ff2->{' stream'})); | ||
| 105 | |||
| 106 | if ($opt{'ToUnicode'}) | ||
| 107 | { | ||
| 108 | @rev = $font->{'cmap'}->read->reverse; | ||
| 109 | $unistr = '/CIDInit /ProcSet findresource being 12 dict begin begincmap | ||
| 110 | /CIDSystemInfo << /Registry (' . $self->{'BaseFont'}->val . '+0) /Ordering (XYZ) | ||
| 111 | /Supplement 0 >> def | ||
| 112 | /CMapName /' . $self->{'BaseFont'}->val . '+0 def | ||
| 113 | 1 begincodespacerange <'; | ||
| 114 | $unistr .= sprintf("%04x> <%04x> endcodespacerange\n", 1, $num - 1); | ||
| 115 | for ($i = 1; $i < $num; $i++) | ||
| 116 | { | ||
| 117 | if ($i % 100 == 0) | ||
| 118 | { | ||
| 119 | $unistr .= "endbfrange\n"; | ||
| 120 | $unistr .= $num - $i > 100 ? 100 : $num - $i; | ||
| 121 | $unistr .= " beginbfrange\n"; | ||
| 122 | } | ||
| 123 | $unistr .= sprintf("<%04x> <%04x> <%04x>\n", $i, $i, $rev[$i]); | ||
| 124 | } | ||
| 125 | $unistr .= "endbfrange\nendcmap CMapName currendict /CMap defineresource pop end end"; | ||
| 126 | $touni = PDFDict(); | ||
| 127 | $parent->new_obj($touni); | ||
| 128 | $touni->{' stream'} = $unistr; | ||
| 129 | $touni->{'Filter'} = PDFArray(PDFName("FlateDecode")); | ||
| 130 | $self->{'ToUnicode'} = $touni; | ||
| 131 | } | ||
| 132 | |||
| 133 | $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head2 out_text($text) | ||
| 138 | |||
| 139 | Returns the string to be put into a content stream for text to be output in this font. | ||
| 140 | The text is assumed to be UTF8 encoded and the return string is a glyph sequence for | ||
| 141 | the text. If subsetting is enabled, then all the glyphs returned are also marked for | ||
| 142 | output. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub out_text | ||
| 147 | { | ||
| 148 | my ($self, $text) = @_; | ||
| 149 | my (@clist) = Text::PDF::Utils::unpacku($text); | ||
| 150 | my ($f) = $self->{' font'}; | ||
| 151 | my ($g, $res); | ||
| 152 | |||
| 153 | foreach $g (map {$f->{'cmap'}->ms_lookup($_)} (@clist)) | ||
| 154 | { | ||
| 155 | vec($self->{' subvec'}, $g, 1) = 1 if ($self->{' subset'}); | ||
| 156 | $res .= sprintf("%04X", $g); | ||
| 157 | } | ||
| 158 | "<$res>"; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | =head2 out_glyphs(@n) | ||
| 163 | |||
| 164 | Marks the glyphs as being needed in the output font when subsetting. Returns a string | ||
| 165 | to render the glyphs as specified. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub out_glyphs | ||
| 170 | { | ||
| 171 | my ($self, @list) = @_; | ||
| 172 | my ($g, $res); | ||
| 173 | |||
| 174 | foreach $g (@list) | ||
| 175 | { | ||
| 176 | vec($self->{' subvec'}, $g, 1) = 1 if ($self->{' subset'}); | ||
| 177 | $res .= sprintf("%04X", $g); | ||
| 178 | } | ||
| 179 | "<$res>"; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | =head2 width($text) | ||
| 184 | |||
| 185 | Returns the width of the string, assuming it to be UTF8 encoded. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub width | ||
| 190 | { | ||
| 191 | my ($self, $text) = @_; | ||
| 192 | my (@clist) = Text::PDF::Utils::unpacku($text); | ||
| 193 | my ($f) = $self->{' font'}; | ||
| 194 | my ($width, $g); | ||
| 195 | |||
| 196 | foreach $g (map {$f->{'cmap'}->ms_lookup($_)} (@clist)) | ||
| 197 | { $width += $f->{'hmtx'}{'advance'}[$g]; } | ||
| 198 | $width / $f->{'head'}{'unitsPerEm'}; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | =head2 outobjdeep($fh, $pdf, %opts) | ||
| 203 | |||
| 204 | Handles the creation of the font stream including subsetting at this point. So | ||
| 205 | if you get this far, that's it for subsetting. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub outobjdeep | ||
| 210 | { | ||
| 211 | my ($self, $fh, $pdf, %opts) = @_; | ||
| 212 | my ($d) = $self->{'DescendantFonts'}->val->[0]; | ||
| 213 | my ($f) = $self->{' font'}; | ||
| 214 | my ($s) = $d->{'FontDescriptor'}{'FontFile2'}; | ||
| 215 | my ($ffh); | ||
| 216 | |||
| 217 | if ($self->{' subset'}) | ||
| 218 | { | ||
| 219 | my ($max) = length($self->{' subvec'}) * 8; | ||
| 220 | my ($upem) = $f->{'head'}{'unitsPerEm'}; | ||
| 221 | my ($mode, $miniArr, $i, $j, $first, @minilist); | ||
| 222 | |||
| 223 | $f->{'glyf'}->read; | ||
| 224 | |||
| 225 | for ($i = 0; $i <= $max; $i++) | ||
| 226 | { | ||
| 227 | next unless(vec($self->{' subvec'},$i,1)); | ||
| 228 | next unless($f->{'loca'}{glyphs}[$i]); | ||
| 229 | map { vec($self->{' subvec'},$_,1)=1; } $f->{loca}{glyphs}[$i]->get_refs; | ||
| 230 | } | ||
| 231 | |||
| 232 | $max = length($self->{' subvec'}) * 8; | ||
| 233 | |||
| 234 | for ($i = 0; $i <= $max; $i++) | ||
| 235 | { | ||
| 236 | if (!$mode && vec($self->{' subvec'}, $i, 1)) | ||
| 237 | { | ||
| 238 | $first = $i; | ||
| 239 | $mode = 1; | ||
| 240 | @minilist = (); | ||
| 241 | } elsif ($mode && !vec($self->{' subvec'}, $i, 1)) | ||
| 242 | { | ||
| 243 | for ($j = 0; $j < scalar @minilist; $j++) | ||
| 244 | { | ||
| 245 | if ($j % 20 == 0) | ||
| 246 | { | ||
| 247 | $miniArr = PDFArray(); | ||
| 248 | $d->{'W'}->add_elements(PDFNum($first + $j), $miniArr) | ||
| 249 | } | ||
| 250 | $miniArr->add_elements(PDFNum($minilist[$j])); | ||
| 251 | } | ||
| 252 | $mode = 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | if ($mode) | ||
| 256 | { push(@minilist, int($f->{'hmtx'}{'advance'}[$i] / $upem * 1000)); } | ||
| 257 | else | ||
| 258 | { $f->{'loca'}{glyphs}[$i] = undef; } | ||
| 259 | } | ||
| 260 | for ( ; $i < $f->{'maxp'}{'numGlyphs'}; $i++) | ||
| 261 | { $f->{'loca'}{'glyphs'}[$i] = undef; } | ||
| 262 | } | ||
| 263 | $s->{' stream'} = ""; | ||
| 264 | $ffh = Text::PDF::TTIOString->new(\$s->{' stream'}); | ||
| 265 | $f->out($ffh, 'cvt ', 'fpgm', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'prep'); | ||
| 266 | $s->{'Filter'} = PDFArray(PDFName("FlateDecode")); | ||
| 267 | $s->{'Length1'} = PDFNum(length($s->{' stream'})); | ||
| 268 | |||
| 269 | $self->SUPER::outobjdeep($fh, $pdf, %opts, 'passthru' => 1); | ||
| 270 | $self; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | =head2 ship_out($pdf) | ||
| 275 | |||
| 276 | Ship this font out to the given $pdf file context | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub ship_out | ||
| 281 | { | ||
| 282 | my ($self, $pdf) = @_; | ||
| 283 | my ($d); | ||
| 284 | |||
| 285 | foreach $d ($self->{'DescendantFonts'}->elementsof) | ||
| 286 | { $pdf->ship_out($self, $d, $d->{'FontDescriptor'}, | ||
| 287 | $d->{'FontDescriptor'}{'FontFile2'}); } | ||
| 288 | $pdf->ship_out($self->{'ToUnicode'}) if (defined $self->{'ToUnicode'}); | ||
| 289 | $self; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | =head2 empty | ||
| 294 | |||
| 295 | Empty the font of as much as possible in order to save memory | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub empty | ||
| 300 | { | ||
| 301 | my ($self) = @_; | ||
| 302 | my ($d); | ||
| 303 | |||
| 304 | if (defined $self->{'DescendantFonts'}) | ||
| 305 | { | ||
| 306 | foreach $d ($self->{'DescendantFonts'}->elementsof) | ||
| 307 | { | ||
| 308 | $d->{'FontDescriptor'}{'FontFile2'}->empty; | ||
| 309 | $d->{'FontDescriptor'}->empty; | ||
| 310 | $d->empty; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | $self->{'ToUnicode'}->empty if (defined $self->{'ToUnicode'}); | ||
| 314 | $self->SUPER::empty; | ||
| 315 | } | ||
| 316 | |||
| 317 | 1; | ||
| 318 |