Font/TTF/Font.pm

filter: Statementstmt 54.5% 156 / 286 Branchbran 39.3% 48 / 122 Conditioncond 23.2% 13 / 56 MC/DCmcdc 6.4% 2 / 31 Subroutinesub 42.8% 9 / 21 Podpod 92.3% 12 / 13 totaltotal 45.3% 240 / 529 CC 92 SCAR 43.0
CC
92
Cov
47%
out
141.6
read
58.4
XML_end
56.0
CRAP 73.5
« Font/TTF/Hhea.pm ↑ Summary Font/TTF/Head.pm »
1 package Font::TTF::Font;
2  
3 =head1 NAME
4  
5 Font::TTF::Font - Memory representation of a font
6  
7 =head1 SYNOPSIS
8  
9 Here is the regression test (you provide your own font). Run it once and then
10 again on the output of the first run. There should be no differences between
11 the outputs of the two runs.
12  
13 $f = Font::TTF::Font->open($ARGV[0]);
14  
15 # force a read of all the tables
16 $f->tables_do(sub { $_[0]->read; });
17  
18 # force read of all glyphs (use read_dat to use lots of memory!)
19 # $f->{'loca'}->glyphs_do(sub { $_[0]->read; });
20 $f->{'loca'}->glyphs_do(sub { $_[0]->read_dat; });
21 # NB. no need to $g->update since $f->{'glyf'}->out will do it for us
22  
23 $f->out($ARGV[1]);
24 $f->release; # clear up memory forcefully!
25  
26 =head1 DESCRIPTION
27  
28 A Truetype font consists of a header containing a directory of tables which
29 constitute the rest of the file. This class holds that header and directory and
30 also creates objects of the appropriate type for each table within the font.
31 Note that it does not read each table into memory, but creates a short reference
32 which can be read using the form:
33  
34 $f->{$tablename}->read;
35  
36 Classes are included that support many of the different TrueType tables. For
37 those for which no special code exists, the table type C<table> is used, which
38 defaults to L<Font::TTF::Table>. The current tables which are supported are:
39  
40 table Font::TTF::Table - for unknown tables
41 EBDT Font::TTF::EBDT
42 EBLC Font::TTF::EBLC
43 Feat Font::TTF::GrFeat
44 GDEF Font::TTF::GDEF
45 GPOS Font::TTF::GPOS
46 GSUB Font::TTF::GSUB
47 Glat Font::TTF::Glat
48 Gloc Font::TTF::Gloc
49 LTSH Font::TTF::LTSH
50 OS/2 Font::TTF::OS_2
51 PCLT Font::TTF::PCLT
52 Sill Font::TTF::Sill
53 Silf Font::TTF::Silf
54 bsln Font::TTF::Bsln
55 cmap Font::TTF::Cmap - see also Font::TTF::OldCmap
56 cvt Font::TTF::Cvt_
57 fdsc Font::TTF::Fdsc
58 feat Font::TTF::Feat
59 fmtx Font::TTF::Fmtx
60 fpgm Font::TTF::Fpgm
61 glyf Font::TTF::Glyf - see also Font::TTF::Glyph
62 hdmx Font::TTF::Hdmx
63 head Font::TTF::Head
64 hhea Font::TTF::Hhea
65 hmtx Font::TTF::Hmtx
66 kern Font::TTF::Kern - see alternative Font::TTF::AATKern
67 loca Font::TTF::Loca
68 maxp Font::TTF::Maxp
69 mort Font::TTF::Mort - see also Font::TTF::OldMort
70 name Font::TTF::Name
71 post Font::TTF::Post
72 prep Font::TTF::Prep
73 prop Font::TTF::Prop
74 vhea Font::TTF::Vhea
75 vmtx Font::TTF::Vmtx
76 DSIG FONT::TTF::DSIG
77  
78 Links are:
79  
80 L<Font::TTF::Table>
81 L<Font::TTF::EBDT> L<Font::TTF::EBLC> L<Font::TTF::GrFeat>
82 L<Font::TTF::GDEF> L<Font::TTF::GPOS> L<Font::TTF::GSUB> L<Font::TTF::Glat> L<Font::TTF::Gloc> L<Font::TTF::LTSH>
83 L<Font::TTF::OS_2> L<Font::TTF::PCLT> L<Font::TTF::Sill> L<Font::TTF::Silf> L<Font::TTF::Bsln> L<Font::TTF::Cmap> L<Font::TTF::Cvt_>
84 L<Font::TTF::Fdsc> L<Font::TTF::Feat> L<Font::TTF::Fmtx> L<Font::TTF::Fpgm> L<Font::TTF::Glyf>
85 L<Font::TTF::Hdmx> L<Font::TTF::Head> L<Font::TTF::Hhea> L<Font::TTF::Hmtx> L<Font::TTF::Kern>
86 L<Font::TTF::Loca> L<Font::TTF::Maxp> L<Font::TTF::Mort> L<Font::TTF::Name> L<Font::TTF::Post>
87 L<Font::TTF::Prep> L<Font::TTF::Prop> L<Font::TTF::Vhea> L<Font::TTF::Vmtx> L<Font::TTF::OldCmap>
88 L<Font::TTF::Glyph> L<Font::TTF::AATKern> L<Font::TTF::OldMort>
89 L<Font::TTF::DSIG>
90  
91  
92 =head1 INSTANCE VARIABLES
93  
94 Instance variables begin with a space (and have lengths greater than the 4
95 characters which make up table names).
96  
97 =over
98  
99 =item nocsum
100  
101 This is used during output to disable the creation of the file checksum in the
102 head table. For example, during DSIG table creation, this flag will be set to
103 ensure that the file checksum is left at zero.
104  
105 =item noharmony
106  
107 If set, do not harmonize the script and lang trees of GPOS and GSUB tables. See L<Font::TTF::Ttopen> for more info.
108  
109 =item nocompress
110  
111 Is the default value controlling WOFF output table compression. If undef, all tables will be compressed if there is
112 a size benefit in doing so.
113 It may be set to an array of tagnames naming tables that should not be compressed, or to a scalar integer specifying a
114 table size threshold below which tables will not be compressed.
115 Note that individual L<Font::TTF::Table> objects may override this default. See L<Font::TTF::Table> for more info.
116  
117 =item fname (R)
118  
119 Contains the filename of the font which this object was read from.
120  
121 =item INFILE (P)
122  
123 The file handle which reflects the source file for this font.
124  
125 =item OFFSET (P)
126  
127 Contains the offset from the beginning of the read file of this particular
128 font directory, thus providing support for TrueType Collections.
129  
130 =item WOFF
131  
132 Contains a reference to a C<Font::TTF::Woff> object.
133  
134 =back
135  
136 =head1 METHODS
137  
138 =cut
139  
140 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use vars qw(%tables $VERSION $dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶use Symbol();
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 1 require 5.004;
147  
148 1 my $havezlib = eval {require Compress::Zlib};
149  
150 1 $VERSION = 0.39; # MJPH 2-FEB-2008 Add DSIG table
151 # $VERSION = 0.38; # MJPH 2-FEB-2008 Add Sill table
152 # $VERSION = 0.37; # MJPH 7-OCT-2005 Force hhea update if dirty, give more OS/2 stuff in update
153 # $VERSION = 0.36; # MJPH 19-AUG-2005 Change cmap::reverse api to be opts based
154 # $VERSION = 0.35; # MJPH 4-MAY-2004 Various fixes to OpenType stuff, separate off scripts
155 # $VERSION = 0.34; # MJPH 22-MAY-2003 Update PSNames to latest AGL
156 # $VERSION = 0.33; # MJPH 9-OCT-2002 Support CFF OpenType (just by version=='OTTO'?!)
157 # $VERSION = 0.32; # MJPH 2-OCT-2002 Bug fixes to TTFBuilder, new methods and some
158 # extension table support in Ttopen and Coverage
159 # $VERSION = 0.31; # MJPH 1-JUL-2002 fix read format 12 cmap (bart@cs.pdx.edu)
160 # improve surrogate support in ttfremap
161 # fix return warn to return warn,undef
162 # ensure correct indexToLocFormat
163 # $VERSION = 0.30; # MJPH 28-MAY-2002 add updated release
164 # $VERSION = 0.29; # MJPH 9-APR-2002 update ttfbuilder, sort out surrogates
165 # $VERSION = 0.28; # MJPH 13-MAR-2002 update ttfbuilder, add Font::TTF::Cmap::ms_enc()
166 # $VERSION = 0.27; # MJPH 6-FEB-2002 update ttfbuilder, support no fpgm, no more __DATA__
167 # $VERSION = 0.26; # MJPH 19-SEP-2001 Update ttfbuilder
168 # $VERSION = 0.25; # MJPH 18-SEP-2001 problems in update of head
169 # $VERSION = 0.24; # MJPH 1-AUG-2001 Sort out update
170 # $VERSION = 0.23; # GST 30-MAY-2001 Memory leak fixed
171 # $VERSION = 0.22; # MJPH 09-APR-2001 Ensure all of AAT stuff included
172 # $VERSION = 0.21; # MJPH 23-MAR-2001 Improve Opentype support
173 # $VERSION = 0.20; # MJPH 13-JAN-2001 Add XML output and some of XML input, AAT & OT tables
174 # $VERSION = 0.19; # MJPH 29-SEP-2000 Add cmap::is_unicode, debug makefile.pl
175 # $VERSION = 0.18; # MJPH 21-JUL-2000 Debug Utils::TTF_bininfo
176 # $VERSION = 0.17; # MJPH 16-JUN-2000 Add utf8 support to names
177 # $VERSION = 0.16; # MJPH 26-APR-2000 Mark read tables as read, tidy up POD
178 # $VERSION = 0.15; # MJPH 5-FEB-2000 Ensure right versions released
179 # $VERSION = 0.14; # MJPH 11-SEP-1999 Sort out Unixisms, agian!
180 # $VERSION = 0.13; # MJPH 9-SEP-1999 Add empty, debug update_bbox
181 # $VERSION = 0.12; # MJPH 22-JUL-1999 Add update_bbox
182 # $VERSION = 0.11; # MJPH 7-JUL-1999 Don't store empties in cmaps
183 # $VERSION = 0.10; # MJPH 21-JUN-1999 Use IO::File
184 # $VERSION = 0.09; # MJPH 9-JUN-1999 Add 5.004 require, minor tweeks in cmap
185 # $VERSION = 0.08; # MJPH 19-MAY-1999 Sort out line endings for Unix
186 # $VERSION = 0.07; # MJPH 28-APR-1999 Get the regression tests to work
187 # $VERSION = 0.06; # MJPH 26-APR-1999 Start to add to CVS, correct MANIFEST.SKIP
188 # $VERSION = 0.05; # MJPH 13-APR-1999 See changes for 0.05
189 # $VERSION = 0.04; # MJPH 13-MAR-1999 Tidy up Tarball
190 # $VERSION = 0.03; # MJPH 9-MAR-1999 Move to Font::TTF for CPAN
191 # $VERSION = 0.02; # MJPH 12-FEB-1999 Add support for ' nocsum' for DSIGS
192 # $VERSION = 0.0001;
193  
194 1 %tables = (
195 'table' => 'Font::TTF::Table',
196 'DSIG' => 'Font::TTF::DSIG',
197 'EBDT' => 'Font::TTF::EBDT',
198 'EBLC' => 'Font::TTF::EBLC',
199 'Feat' => 'Font::TTF::GrFeat',
200 'GDEF' => 'Font::TTF::GDEF',
201 'Glat' => 'Font::TTF::Glat',
202 'Gloc' => 'Font::TTF::Gloc',
203 'GPOS' => 'Font::TTF::GPOS',
204 'GSUB' => 'Font::TTF::GSUB',
205 'Glat' => 'Font::TTF::Glat',
206 'Gloc' => 'Font::TTF::Gloc',
207 'LTSH' => 'Font::TTF::LTSH',
208 'OS/2' => 'Font::TTF::OS_2',
209 'PCLT' => 'Font::TTF::PCLT',
210 'Sill' => 'Font::TTF::Sill',
211 'Silf' => 'Font::TTF::Silf',
212 'bsln' => 'Font::TTF::Bsln',
213 'cmap' => 'Font::TTF::Cmap',
214 'cvt ' => 'Font::TTF::Cvt_',
215 'fdsc' => 'Font::TTF::Fdsc',
216 'feat' => 'Font::TTF::Feat',
217 'fmtx' => 'Font::TTF::Fmtx',
218 'fpgm' => 'Font::TTF::Fpgm',
219 'glyf' => 'Font::TTF::Glyf',
220 'hdmx' => 'Font::TTF::Hdmx',
221 'head' => 'Font::TTF::Head',
222 'hhea' => 'Font::TTF::Hhea',
223 'hmtx' => 'Font::TTF::Hmtx',
224 'kern' => 'Font::TTF::Kern',
225 'loca' => 'Font::TTF::Loca',
226 'maxp' => 'Font::TTF::Maxp',
227 'mort' => 'Font::TTF::Mort',
228 'name' => 'Font::TTF::Name',
229 'post' => 'Font::TTF::Post',
230 'prep' => 'Font::TTF::Prep',
231 'prop' => 'Font::TTF::Prop',
232 'vhea' => 'Font::TTF::Vhea',
233 'vmtx' => 'Font::TTF::Vmtx',
234 );
235  
236 # This is special code because I am fed up of every time I x a table in the debugger
237 # I get the whole font printed. Thus substitutes my 3 line change to dumpvar into
238 # the debugger. Clunky, but nice. You are welcome to a copy if you want one.
239
240 1 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.1
241 1 my ($p);
242  
243 1 foreach $p (@INC)
244 {
245 8 ▶ if (-f "$p/mydumpvar.pl")
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$p/mydumpvar.pl") 0 8 8
246 {
247 0 $dumper = 'mydumpvar.pl';
248 0 last;
249 }
250 }
251 1 ▶ $dumper ||= 'dumpvar.pl';
Condition50%1/2
$Font::TTF::Font::dumper ||= "dumpvar.pl"
l!l
01
MC/DC0%0/1
$Font::TTF::Font::dumper ||= "dumpvar.pl"
$Font::TTF::Font::dumper
Truth table50%1/2
A
A= $Font::TTF::Font::dumper
Aresult
11
00
252 }
253  
254 sub main::dumpValue
255 0 ▶{ do $dumper; &main::dumpValue; }
Subroutine dumpValue: not called CC=1 SCAR=6.9
256
257  
258 =head2 Font::TTF::Font->AddTable($tablename, $class)
259  
260 Adds the given class to be used when representing the given table name. It also
261 'requires' the class for you.
262  
263 =cut
264  
265 sub AddTable
266 0 ▶{
Subroutine AddTable: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($class, $table, $useclass) = @_;
268  
269 0 $tables{$table} = $useclass;
270 # $useclass =~ s|::|/|oig;
271 # require "$useclass.pm";
272 }
273  
274  
275 =head2 Font::TTF::Font->Init
276  
277 For those people who like making fonts without reading them. This subroutine
278 will require all the table code for the various table types for you. Not
279 needed if using Font::TTF::Font::read before using a table.
280  
281 =cut
282  
283 sub Init
284 0 ▶{
Subroutine Init: not called CC=2 SCAR=17.9
Pod documented
285 0 my ($class) = @_;
286 0 my ($t);
287  
288 0 foreach $t (values %tables)
289 {
290 0 $t =~ s|::|/|oig;
291 0 require "$t.pm";
292 }
293 }
294  
295 =head2 Font::TTF::Font->new(%props)
296  
297 Creates a new font object and initialises with the given properties. This is
298 primarily for use when a TTF is embedded somewhere. Notice that the properties
299 are automatically preceded by a space when inserted into the object. This is in
300 order that fields do not clash with tables.
301  
302 =cut
303  
304 sub new
305 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
306 0 my ($class, %props) = @_;
307 0 my ($self) = {};
308  
309 0 bless $self, $class;
310  
311 0 foreach (keys %props)
312 0 { $self->{" $_"} = $props{$_}; }
313 0 $self;
314 }
315  
316  
317 =head2 Font::TTF::Font->open($fname)
318  
319 Reads the header and directory for the given font file and creates appropriate
320 objects for each table in the font.
321  
322 =cut
323  
324 sub open
325 2 ▶{
Subroutine open: called CC=3 SCAR=11.2
Pod documented
326 2 my ($class, $fname) = @_;
327 2 my ($fh);
328 2 my ($self) = {};
329
330 2 ▶ unless (ref($fname))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fname) { } 0 2 2
331 {
332 2 ▶ $fh = IO::File->new($fname) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($fname) or return undef 2 0 2
333 2 binmode $fh;
334 } else
335 0 { $fh = $fname; }
336  
337 2 $self->{' INFILE'} = $fh;
338 2 $self->{' fname'} = $fname;
339 2 $self->{' OFFSET'} = 0;
340 2 bless $self, $class;
341
342 2 $self->read;
343 }
344  
345 =head2 $f->read
346  
347 Reads a Truetype font directory starting from location C<$self->{' OFFSET'}> in the file.
348 This has been separated from the C<open> function to allow support for embedded
349 TTFs for example in TTCs. Also reads the C<head> and C<maxp> tables immediately.
350  
351 =cut
352  
353 sub read
354 2 ▶{
Subroutine read: called CC=23 SCAR=40.7
Pod documented
355 2 my ($self) = @_;
356 2 my ($fh) = $self->{' INFILE'};
357 2 my ($dat, $i, $ver, $dir_num, $type, $name, $check, $off, $len, $t);
358 2 my ($iswoff, $woffLength, $sfntSize, $zlen); # needed for WOFF files
359  
360 2 $fh->seek($self->{' OFFSET'}, 0);
361 2 $fh->read($dat, 4);
362 2 $ver = unpack("N", $dat);
363 2 $iswoff = ($ver == unpack('N', 'wOFF'));
364 2 ▶ if ($iswoff)
Branch: 1 branch
BranchTrueFalseTotal
if ($iswoff) { } 1 1 2
365 {
366 1 require Font::TTF::Woff;
367 1 my $woff = Font::TTF::Woff->new(PARENT => $self);
368 1 $fh->read($dat, 32);
369 1 ($ver, $woffLength, $dir_num, undef, $sfntSize, $woff->{'majorVersion'}, $woff->{'minorVersion'},
370 $off, $zlen, $len) = unpack('NNnnNnnNNN', $dat);
371 # TODO: According to WOFF spec we should verify $woffLength and $sfntSize, and fail if the values are wrong.
372 1 ▶ if ($off)
Branch: 1 branch
BranchTrueFalseTotal
if ($off) 0 1 1
373 {
374 # Font has metadata
375 0 ▶ if ($off + $zlen > $woffLength)
Branch: 1 branch
BranchTrueFalseTotal
if ($off + $zlen > $woffLength) 0 0 0
376 {
377 0 warn "invalid WOFF header in $self->{' fname'}: meta data beyond end.";
378 0 return undef;
379 }
380 0 require Font::TTF::Woff::MetaData;
381 0 $woff->{'metaData'} = Font::TTF::Woff::MetaData->new(
382 PARENT => $woff,
383 INFILE => $fh,
384 OFFSET => $off,
385 LENGTH => $len,
386 ZLENGTH => $zlen);
387 }
388
389 1 $fh->read($dat, 8);
390 1 ($off, $len) = unpack('NN', $dat);
391 1 ▶ if ($off)
Branch: 1 branch
BranchTrueFalseTotal
if ($off) 0 1 1
392 {
393 # Font has private data
394 0 ▶ if ($off + $len > $woffLength)
Branch: 1 branch
BranchTrueFalseTotal
if ($off + $len > $woffLength) 0 0 0
395 {
396 0 warn "invalid WOFF header in $self->{' fname'}: private data beyond end.";
397 0 return undef;
398 }
399 0 require Font::TTF::Woff::PrivateData;
400 0 $woff->{'privateData'} = Font::TTF::Woff::PrivateData->new(
401 PARENT => $woff,
402 INFILE => $fh,
403 OFFSET => $off,
404 LENGTH => $len);
405 }
406
407 1 $self->{' WOFF'} = $woff;
408 }
409 else
410 {
411 1 $fh->read($dat, 8);
412 1 $dir_num = unpack("n", $dat);
413 }
414
415 2 ▶ $ver == 1 << 16 # TrueType outlines
Branch: 1 branch
BranchTrueFalseTotal
$ver == 65536 or $ver == unpack("N", "OTTO") or $ver == unpack("N", "true") or return undef 2 0 2
Condition33%2/6
$ver == 65536 or $ver == unpack("N", "OTTO") or $ver == unpack("N", "true")
l!l&&r!l&&!r
200
$ver == 65536 or $ver == unpack("N", "OTTO")
l!l&&r!l&&!r
200
MC/DC0%0/3
$ver == 65536 or $ver == unpack("N", "OTTO") or $ver == unpack("N", "true")
$ver == 65536 $ver == unpack("N", "OTTO") $ver == unpack("N", "true")
Truth table25%1/4
A or B or C
A= $ver == 65536B= $ver == unpack("N", "OTTO")C= $ver == unpack("N", "true")
ABCresult
1XX1
01X1
0011
0000
416 || $ver == unpack('N', 'OTTO') # 0x4F54544F CFF outlines
417 || $ver == unpack('N', 'true') # 0x74727565 Mac sfnts
418 or return undef; # else unrecognized type
419
420
421 2 ▶ for ($i = 0; $i < $dir_num; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $dir_num) 28 2 30
422 {
423 28 ▶ $fh->read($dat, $iswoff ? 20 : 16) || die "Reading table entry";
Branch: 2 branches
BranchTrueFalseTotal
$fh->read($dat, $iswoff ? 20 : 16) or die "Reading table entry" 28 0 28
$iswoff ? : 14 14 28
424 28 ▶ if ($iswoff)
Branch: 1 branch
BranchTrueFalseTotal
if ($iswoff) { } 14 14 28
425 {
426 14 ($name, $off, $zlen, $len, $check) = unpack("a4NNNN", $dat);
427 14 ▶ if ($off + $zlen > $woffLength || $zlen > $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($off + $zlen > $woffLength or $zlen > $len) 0 14 14
Condition33%1/3
$off + $zlen > $woffLength or $zlen > $len
l!l&&r!l&&!r
0014
MC/DC0%0/2
$off + $zlen > $woffLength or $zlen > $len
$off + $zlen > $woffLength $zlen > $len
Truth table33%1/3
A or B
A= $off + $zlen > $woffLengthB= $zlen > $len
ABresult
1X1
011
000
428 {
429 0 my $err;
430 0 ▶ $err = "Offset + compressed length > total length. " if $off + $zlen > $woffLength;
Branch: 1 branch
BranchTrueFalseTotal
if $off + $zlen > $woffLength 0 0 0
431 0 ▶ $err = "Compressed length > uncompressed length. " if $zlen > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $zlen > $len 0 0 0
432 0 warn "invalid WOFF '$name' table in $self->{' fname'}: $err\n";
433 0 return undef;
434 }
435 }
436 else
437 {
438 14 ($name, $check, $off, $len) = unpack("a4NNN", $dat);
439 14 $zlen = $len;
440 }
441 $self->{$name} = $self->{' PARENT'}->find($self, $name, $check, $off, $len) and next
442 28 ▶ if (defined $self->{' PARENT'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{" PARENT"} 0 28 28
Condition25%1/4
defined $self->{' PARENT'} && (($self->{$name} = $self->{' PARENT'}->find($self, $name, $check, $off, $len)) && (next))
!ll
280
$self->{$name} = $self->{" PARENT"}->find($self, $name, $check, $off, $len) and next
!ll
00
MC/DC0%0/2
defined $self->{' PARENT'} && (($self->{$name} = $self->{' PARENT'}->find($self, $name, $check, $off, $len)) && (next))
defined $self->{' PARENT'} $self->{$name} = $self->{" PARENT"}->find($self, $name, $check, $off, $len)
Truth table33%1/3
A && B
A= defined $self->{' PARENT'}B= $self->{$name} = $self->{" PARENT"}->find($self, $name, $check, $off, $len)
ABresult
0X0
100
111
443 28 ▶ $type = $tables{$name} || 'Font::TTF::Table';
Condition100%2/2
$Font::TTF::Font::tables{$name} || 'Font::TTF::Table'
l!l
262
MC/DC100%1/1
$Font::TTF::Font::tables{$name} || 'Font::TTF::Table'
$Font::TTF::Font::tables{$name}
Truth table100%2/2
A
A= $Font::TTF::Font::tables{$name}
Aresult
11
00
444 28 $t = $type;
445 28 ▶ if ($^O eq "MacOS")
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MacOS') { } 0 28 28
446 0 { $t =~ s/^|::/:/oig; }
447 else
448 28 { $t =~ s|::|/|oig; }
449 28 require "$t.pm";
450 28 $self->{$name} = $type->new(PARENT => $self,
451 NAME => $name,
452 INFILE => $fh,
453 OFFSET => $off,
454 LENGTH => $len,
455 ZLENGTH => $zlen,
456 CSUM => $check);
457 }
458
459 2 foreach $t ('head', 'maxp')
460 4 ▶ { $self->{$t}->read if defined $self->{$t}; }
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$t} 4 0 4
461  
462 2 $self;
463 }
464  
465  
466 =head2 $f->out($fname [, @tablelist])
467  
468 Writes a TTF file consisting of the tables in tablelist. The list is checked to
469 ensure that only tables that exist are output. (This means that you cannot have
470 non table information stored in the font object with key length of exactly 4)
471  
472 In many cases the user simply wants to output all the tables in alphabetical order.
473 This can be done by not including a @tablelist, in which case the subroutine will
474 output all the defined tables in the font in alphabetical order.
475  
476 Returns $f on success and undef on failure, including warnings.
477  
478 All output files must include the C<head> table.
479  
480 =cut
481  
482 sub out
483 2 ▶{
Subroutine out: called CC=40 SCAR=49.5
Pod documented
484 2 my ($self, $fname, @tlist) = @_;
485 2 my ($fh);
486 2 my ($dat, $numTables, $sRange, $eSel);
487 2 my (%dir, $k, $mloc, $count);
488 2 my ($csum, $lsum, $msum, $loc, $oldloc, $len, $shift);
489  
490 2 my ($iswoff); # , $woffLength, $sfntSize, $zlen); # needed for WOFF files
491  
492 2 ▶ unless (ref($fname))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fname) { } 0 2 2
493 {
494 2 ▶ $fh = IO::File->new("+>$fname") || return warn("Unable to open $fname for writing"), undef;
Condition50%1/2
'IO::File'->new("+>$fname") || (return warn("Unable to open $fname for writing"), undef)
l!l
20
MC/DC0%0/1
'IO::File'->new("+>$fname") || (return warn("Unable to open $fname for writing"), undef)
'IO::File'->new("+>$fname")
Truth table50%1/2
A
A= 'IO::File'->new("+>$fname")
Aresult
11
00
495 2 binmode $fh;
496 } else
497 0 { $fh = $fname; }
498
499 2 $self->{' oname'} = $fname;
500 2 $self->{' outfile'} = $fh;
501  
502 2 ▶ if ($self->{' wantsig'})
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{' wantsig'}) { } 0 2 2
elsif ($#tlist < 0) { } 2 0 2
503 {
504 0 $self->{' nocsum'} = 1;
505 # $self->{'head'}{'checkSumAdjustment'} = 0;
506 0 $self->{' tempDSIG'} = $self->{'DSIG'};
507 0 $self->{' tempcsum'} = $self->{'head'}{' CSUM'};
508 0 delete $self->{'DSIG'};
509 0 ▶ @tlist = sort {$self->{$a}{' OFFSET'} <=> $self->{$b}{' OFFSET'}}
Condition0%0/3
length $_ == 4 && defined $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ == 4 && defined $self->{$_}
length $_ == 4 defined $self->{$_}
Truth table0%0/3
A && B
A= length $_ == 4B= defined $self->{$_}
ABresult
0X0
100
111
510 0 ▶ grep (length($_) == 4 && defined $self->{$_}, keys %$self) if ($#tlist < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#tlist < 0 0 0 0
511 }
512 elsif ($#tlist < 0)
513 2 { @tlist = sort keys %$self; }
514
515 2 ▶ @tlist = grep(length($_) == 4 && defined $self->{$_}, @tlist);
Condition66%2/3
length $_ == 4 && defined $self->{$_}
!ll&&!rl&&r
11028
MC/DC50%1/2
length $_ == 4 && defined $self->{$_}
length $_ == 4 defined $self->{$_}
Truth table66%2/3
A && B
A= length $_ == 4B= defined $self->{$_}
ABresult
0X0
100
111
516 2 $numTables = $#tlist + 1;
517 2 ▶ $numTables++ if ($self->{' wantsig'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" wantsig"} 0 2 2
518
519 2 ▶ if ($iswoff)
Branch: 1 branch
BranchTrueFalseTotal
if ($iswoff) { } 0 2 2
520 {
521 }
522 else
523 {
524 2 ($numTables, $sRange, $eSel, $shift) = Font::TTF::Utils::TTF_bininfo($numTables, 16);
525 2 $dat = pack("Nnnnn", 1 << 16, $numTables, $sRange, $eSel, $shift);
526 2 $fh->print($dat);
527 2 $msum = unpack("%32N*", $dat);
528 }
529  
530 # reserve place holders for each directory entry
531 2 foreach $k (@tlist)
532 {
533 28 $dir{$k} = pack("A4NNN", $k, 0, 0, 0);
534 28 $fh->print($dir{$k});
535 }
536  
537 2 ▶ $fh->print(pack('A4NNN', '', 0, 0, 0)) if ($self->{' wantsig'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" wantsig"} 0 2 2
538  
539 2 $loc = $fh->tell();
540 2 ▶ if ($loc & 3)
Branch: 1 branch
BranchTrueFalseTotal
if ($loc & 3) 0 2 2
541 {
542 0 $fh->print(substr("\000" x 4, $loc & 3));
543 0 $loc += 4 - ($loc & 3);
544 }
545  
546 2 foreach $k (@tlist)
547 {
548 28 $oldloc = $loc;
549 28 ▶ if ($iswoff && $havezlib &&
Branch: 3 branches
BranchTrueFalseTotal
if ($iswoff && $havezlib && exists $self->{$k}{' nocompress'} ? $self->{$k}{' nocompress'} != -1 : (ref $self->{' nocompress'} eq 'ARRAY' ? !exists $self->{' nocompress'}{$k} : ref $self->{' nocompress'} eq 'SCALAR' && $self->{' nocompress'} != -1)) { } 0 28 28
$iswoff && $havezlib && exists $self->{$k}{' nocompress'} ? : 0 28 28
ref $self->{' nocompress'} eq 'ARRAY' ? : 0 28 28
Condition33%3/9
$iswoff and $havezlib and exists $self->{$k}{" nocompress"}
!ll&&!rl&&r
2800
$iswoff and $havezlib
!ll&&!rl&&r
2800
ref $self->{" nocompress"} eq "SCALAR" and $self->{" nocompress"} != -1
!ll&&!rl&&r
2800
MC/DC0%0/3
$iswoff and $havezlib and exists $self->{$k}{" nocompress"}
$iswoff $havezlib exists $self->{$k}{" nocompress"}
MC/DC0%0/2
ref $self->{" nocompress"} eq "SCALAR" and $self->{" nocompress"} != -1
ref $self->{" nocompress"} eq "SCALAR" $self->{" nocompress"} != -1
Truth table25%1/4
A and B and C
A= $iswoffB= $havezlibC= exists $self->{$k}{" nocompress"}
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A and B
A= ref $self->{" nocompress"} eq "SCALAR"B= $self->{" nocompress"} != -1
ABresult
0X0
100
111
550 # output font is WOFF -- should we try to compress this table?
551 exists ($self->{$k}->{' nocompress'}) ? $self->{$k}->{' nocompress'} != -1 :
552 ref($self->{' nocompress'}) eq 'ARRAY' ? !exists($self->{' nocompress'}{$k}) :
553 ref($self->{' nocompress'}) eq 'SCALAR' && $self->{' nocompress'} != -1)
554 {
555 # Yes -- we may want to compress this table.
556 # Create string file handle to hold uncompressed table
557 0 my $dat;
558 0 my $fh2 = IO::String->new($dat);
559 0 binmode $fh2;
560 0 $self->{$k}->out($fh2);
561 0 $len = $fh2->tell();
562 0 close $fh2;
563
564 # Is table long enough to try compression?
565 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$k}{' nocompress'} and $len <= $self->{$k}{' nocompress'} or ref $self->{' nocompress'} eq 'SCALAR' and $len <= $self->{' nocompress'}) { } 0 0 0
Condition0%0/9
exists $self->{$k}{" nocompress"} and $len <= $self->{$k}{" nocompress"} or ref $self->{" nocompress"} eq "SCALAR" and $len <= $self->{" nocompress"}
l!l&&r!l&&!r
000
exists $self->{$k}{" nocompress"} and $len <= $self->{$k}{" nocompress"}
!ll&&!rl&&r
000
ref $self->{" nocompress"} eq "SCALAR" and $len <= $self->{" nocompress"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $self->{$k}{" nocompress"} and $len <= $self->{$k}{" nocompress"} or ref $self->{" nocompress"} eq "SCALAR" and $len <= $self->{" nocompress"}
exists $self->{$k}{" nocompress"} $len <= $self->{$k}{" nocompress"} ref $self->{" nocompress"} eq "SCALAR" $len <= $self->{" nocompress"}
Truth table0%0/7
A and B or C and D
A= exists $self->{$k}{" nocompress"}B= $len <= $self->{$k}{" nocompress"}C= ref $self->{" nocompress"} eq "SCALAR"D= $len <= $self->{" nocompress"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
566 exists ($self->{$k}->{' nocompress'}) && $len <= $self->{$k}->{' nocompress'} ||
567 ref($self->{' nocompress'}) eq 'SCALAR' && $len <= $self->{' nocompress'})
568 {
569 # Yes -- so compress and check lengths:
570 0 my $zdat = Compress::Zlib::compress($dat);
571 0 my $zlen = bytes::length($zdat);
572 0 ▶ if ($zlen < $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($zlen < $len) { } 0 0 0
573 {
574 # write the compressed $zdat
575
576 }
577 else
578 {
579 # write the uncompressed $dat
580 }
581 }
582 else
583 {
584 # write uncompressed $dat
585 }
586
587
588 }
589 else
590 {
591 # Output table normally
592 28 $self->{$k}->out($fh);
593 28 $loc = $fh->tell();
594 28 $len = $loc - $oldloc;
595 }
596 28 ▶ if ($loc & 3)
Branch: 1 branch
BranchTrueFalseTotal
if ($loc & 3) 12 16 28
597 {
598 12 $fh->print(substr("\000" x 4, $loc & 3));
599 12 $loc += 4 - ($loc & 3);
600 }
601 28 $fh->seek($oldloc, 0);
602 28 $csum = 0; $mloc = $loc;
603 28 ▶ while ($mloc > $oldloc)
Branch: 1 branch
BranchTrueFalseTotal
if ($mloc > $oldloc) 48 28 76
604 {
605 48 ▶ $count = ($mloc - $oldloc > 4096) ? 4096 : $mloc - $oldloc;
Branch: 1 branch
BranchTrueFalseTotal
$mloc - $oldloc > 4096 ? : 20 28 48
606 48 $fh->read($dat, $count);
607 48 $csum += unpack("%32N*", $dat);
608 # this line ensures $csum stays within 32 bit bounds, clipping as necessary
609 48 ▶ if ($csum > 0xffffffff) { $csum -= 0xffffffff; $csum--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($csum > 4294967295) 12 36 48
610 48 $mloc -= $count;
611 }
612 28 $dir{$k} = pack("A4NNN", $k, $csum, $oldloc, $len);
613 28 $msum += $csum + unpack("%32N*", $dir{$k});
614 28 ▶ while ($msum > 0xffffffff) { $msum -= 0xffffffff; $msum--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($msum > 4294967295) 22 28 50
615 28 $fh->seek($loc, 0);
616 }
617  
618 2 ▶ unless ($self->{' nocsum'}) # assuming we want a file checksum
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{' nocsum'}) { } 0 2 2
if (do { $self->{" wantsig"} }) 0 0 0
619 {
620 # Now we need to sort out the head table's checksum
621 2 ▶ if (!defined $dir{'head'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir{"head"}) 0 2 2
622 { # you have to have a head table
623 0 $fh->close();
624 0 return warn("No 'head' table to output in $fname"), undef;
625 }
626 2 ($csum, $loc, $len) = unpack("x4NNN", $dir{'head'});
627 2 $fh->seek($loc + 8, 0);
628 2 $fh->read($dat, 4);
629 2 $lsum = unpack("N", $dat);
630 2 ▶ if ($lsum != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($lsum != 0) 2 0 2
631 {
632 2 $csum -= $lsum;
633 2 ▶ if ($csum < 0) { $csum += 0xffffffff; $csum++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($csum < 0) 2 0 2
634 2 $msum -= $lsum * 2; # twice (in head and in csum)
635 2 ▶ while ($msum < 0) { $msum += 0xffffffff; $msum++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($msum < 0) 2 2 4
636 }
637 2 $lsum = 0xB1B0AFBA - $msum;
638 2 $fh->seek($loc + 8, 0);
639 2 $fh->print(pack("N", $lsum));
640 2 $dir{'head'} = pack("A4NNN", 'head', $csum, $loc, $len);
641 0 } elsif ($self->{' wantsig'})
642 {
643 0 ▶ if (!defined $dir{'head'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir{"head"}) 0 0 0
644 { # you have to have a head table
645 0 $fh->close();
646 0 return warn("No 'head' table to output in $fname"), undef;
647 }
648 0 ($csum, $loc, $len) = unpack("x4NNN", $dir{'head'});
649 0 $fh->seek($loc + 8, 0);
650 0 $fh->print(pack("N", 0));
651 # $dir{'head'} = pack("A4NNN", 'head', $self->{' tempcsum'}, $loc, $len);
652 }
653  
654 # Now we can output the directory again
655 2 ▶ if ($self->{' wantsig'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" wantsig"}) 0 2 2
656 0 { @tlist = sort @tlist; }
657 2 $fh->seek(12, 0);
658 2 foreach $k (@tlist)
659 28 { $fh->print($dir{$k}); }
660 2 ▶ $fh->print(pack('A4NNN', '', 0, 0, 0)) if ($self->{' wantsig'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" wantsig"} 0 2 2
661 2 $fh->close();
662 2 $self;
663 }
664  
665  
666 =head2 $f->out_xml($filename [, @tables])
667  
668 Outputs the font in XML format
669  
670 =cut
671  
672 sub out_xml
673 0 ▶{
Subroutine out_xml: not called CC=6 SCAR=37.4
Pod documented
674 0 my ($self, $fname, @tlist) = @_;
675 0 my ($fh, $context, $numTables, $k);
676  
677 0 $context->{'indent'} = ' ' x 4;
678  
679 0 ▶ unless (ref($fname))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fname) { } 0 0 0
680 {
681 0 ▶ $fh = IO::File->new("+>$fname") || return warn("Unable to open $fname"), undef;
Condition0%0/2
'IO::File'->new("+>$fname") || (return warn("Unable to open $fname"), undef)
l!l
00
MC/DC0%0/1
'IO::File'->new("+>$fname") || (return warn("Unable to open $fname"), undef)
'IO::File'->new("+>$fname")
Truth table0%0/2
A
A= 'IO::File'->new("+>$fname")
Aresult
11
00
682 0 binmode $fh;
683 } else
684 0 { $fh = $fname; }
685  
686 0 ▶ unless (scalar @tlist > 0)
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @tlist > 0) 0 0 0
687 {
688 0 @tlist = sort keys %$self;
689 0 ▶ @tlist = grep(length($_) == 4 && defined $self->{$_}, @tlist);
Condition0%0/3
length $_ == 4 && defined $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ == 4 && defined $self->{$_}
length $_ == 4 defined $self->{$_}
Truth table0%0/3
A && B
A= length $_ == 4B= defined $self->{$_}
ABresult
0X0
100
111
690 }
691 0 $numTables = $#tlist + 1;
692  
693 0 $context->{'fh'} = $fh;
694 0 $fh->print("<?xml version='1.0' encoding='UTF-8'?>\n");
695 0 $fh->print("<font tables='$numTables'>\n\n");
696
697 0 foreach $k (@tlist)
698 {
699 0 $fh->print("<table name='$k'>\n");
700 0 $self->{$k}->out_xml($context, $context->{'indent'});
701 0 $fh->print("</table>\n");
702 }
703  
704 0 $fh->print("</font>\n");
705 0 $fh->close;
706 0 $self;
707 }
708  
709  
710 =head2 $f->XML_start($context, $tag, %attrs)
711  
712 Handles start messages from the XML parser. Of particular interest to us are <font> and
713 <table>.
714  
715 =cut
716  
717 sub XML_start
718 0 ▶{
Subroutine XML_start: not called CC=6 SCAR=37.4
Pod documented
719 0 my ($self, $context, $tag, %attrs) = @_;
720 0 my ($name, $type, $t);
721  
722 0 ▶ if ($tag eq 'font')
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'font') { } 0 0 0
elsif ($tag eq 'table') { } 0 0 0
723 0 { $context->{'tree'}[-1] = $self; }
724 elsif ($tag eq 'table')
725 {
726 0 $name = $attrs{'name'};
727 0 ▶ unless (defined $self->{$name})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$name}) 0 0 0
728 {
729 0 ▶ $type = $tables{$name} || 'Font::TTF::Table';
Condition0%0/2
$Font::TTF::Font::tables{$name} || 'Font::TTF::Table'
l!l
00
MC/DC0%0/1
$Font::TTF::Font::tables{$name} || 'Font::TTF::Table'
$Font::TTF::Font::tables{$name}
Truth table0%0/2
A
A= $Font::TTF::Font::tables{$name}
Aresult
11
00
730 0 $t = $type;
731 0 ▶ if ($^O eq "MacOS")
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MacOS') { } 0 0 0
732 0 { $t =~ s/^|::/:/oig; }
733 else
734 0 { $t =~ s|::|/|oig; }
735 0 require "$t.pm";
736 0 $self->{$name} = $type->new('PARENT' => $self, 'NAME' => $name, 'read' => 1);
737 }
738 0 $context->{'receiver'} = ($context->{'tree'}[-1] = $self->{$name});
739 }
740 0 $context;
741 }
742  
743  
744 sub XML_end
745 0 ▶{
Subroutine XML_end: not called CC=7 SCAR=40.3
Pod undocumented
746 0 my ($self) = @_;
747 0 my ($context, $tag, %attrs) = @_;
748 0 my ($i);
749  
750 0 ▶ return undef unless ($tag eq 'table' && $attrs{'name'} eq 'loca');
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq "table" and $attrs{"name"} eq "loca" 0 0 0
Condition0%0/3
$tag eq "table" and $attrs{"name"} eq "loca"
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "table" and $attrs{"name"} eq "loca"
$tag eq "table" $attrs{"name"} eq "loca"
Truth table0%0/3
A and B
A= $tag eq "table"B= $attrs{"name"} eq "loca"
ABresult
0X0
100
111
751 0 ▶ if (defined $context->{'glyphs'} && $context->{'glyphs'} ne $self->{'loca'}{'glyphs'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"glyphs"} and $context->{"glyphs"} ne $self->{"loca"}{"glyphs"}) 0 0 0
Condition0%0/3
defined $context->{"glyphs"} and $context->{"glyphs"} ne $self->{"loca"}{"glyphs"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $context->{"glyphs"} and $context->{"glyphs"} ne $self->{"loca"}{"glyphs"}
defined $context->{"glyphs"} $context->{"glyphs"} ne $self->{"loca"}{"glyphs"}
Truth table0%0/3
A and B
A= defined $context->{"glyphs"}B= $context->{"glyphs"} ne $self->{"loca"}{"glyphs"}
ABresult
0X0
100
111
752 {
753 0 ▶ for ($i = 0; $i <= $#{$context->{'glyphs'}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$context{"glyphs"};}) 0 0 0
754 0 ▶ { $self->{'loca'}{'glyphs'}[$i] = $context->{'glyphs'}[$i] if defined $context->{'glyphs'}[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"glyphs"}[$i] 0 0 0
755 0 $context->{'glyphs'} = $self->{'loca'}{'glyphs'};
756 }
757 0 return undef;
758 }
759  
760 =head2 $f->update
761  
762 Sends update to all the tables in the font and then resets all the isDirty
763 flags on each table. The data structure in now consistent as a font (we hope).
764  
765 =cut
766  
767 sub update
768 0 ▶{
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
769 0 my ($self) = @_;
770
771 0 ▶ $self->tables_do(sub { $_[0]->update; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
772  
773 0 $self;
774 }
775  
776 =head2 $f->dirty
777  
778 Dirties all the tables in the font
779  
780 =cut
781  
782 sub dirty
783 0 ▶{ $_[0]->tables_do(sub { $_[0]->dirty; }); $_[0]; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine dirty: not called CC=1 SCAR=6.9
Pod documented
784  
785 =head2 $f->tables_do(&func [, tables])
786  
787 Calls &func for each table in the font. Calls the table in alphabetical sort
788 order as per the order in the directory:
789  
790 &func($table, $name);
791  
792 May optionally take a list of table names in which case func is called
793 for each of them in the given order.
794  
795 =cut
796  
797 sub tables_do
798 2 ▶{
Subroutine tables_do: called CC=3 SCAR=11.0
Pod documented
799 2 my ($self, $func, @tables) = @_;
800 2 my ($t);
801  
802 2 ▶ foreach $t (@tables ? @tables : sort grep {length($_) == 4} keys %$self)
Branch: 1 branch
BranchTrueFalseTotal
@tables ? : 0 2 2
803 28 { &$func($self->{$t}, $t); }
804 2 $self;
805 }
806  
807  
808 =head2 $f->release
809  
810 Releases ALL of the memory used by the TTF font and all of its component
811 objects. After calling this method, do B<NOT> expect to have anything left in
812 the C<Font::TTF::Font> object.
813  
814 B<NOTE>, that it is important that you call this method on any
815 C<Font::TTF::Font> object when you wish to destruct it and free up its memory.
816 Internally, we track things in a structure that can result in circular
817 references, and without calling 'C<release()>' these will not properly get
818 cleaned up by Perl. Once you've called this method, though, don't expect to be
819 able to do anything else with the C<Font::TTF::Font> object; it'll have B<no>
820 internal state whatsoever.
821  
822 B<Developer note:> As part of the brute-force cleanup done here, this method
823 will throw a warning message whenever unexpected key values are found within
824 the C<Font::TTF::Font> object. This is done to help ensure that any unexpected
825 and unfreed values are brought to your attention so that you can bug us to keep
826 the module updated properly; otherwise the potential for memory leaks due to
827 dangling circular references will exist.
828  
829 =cut
830  
831 sub release
832 0 ▶{
Subroutine release: not called CC=6 SCAR=37.4
Pod documented
833 0 my ($self) = @_;
834  
835 # delete stuff that we know we can, here
836  
837 0 my @tofree = map { delete $self->{$_} } keys %{$self};
838  
839 0 ▶ while (my $item = shift @tofree)
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 0 0 0
840 {
841 0 my $ref = ref($item);
842 0 ▶ if (UNIVERSAL::can($item, 'release'))
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($item, 'release')) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($ref, 'HASH')) { } 0 0 0
843 0 { $item->release(); }
844 elsif ($ref eq 'ARRAY')
845 0 { push( @tofree, @{$item} ); }
846 elsif (UNIVERSAL::isa($ref, 'HASH'))
847 0 { release($item); }
848 }
849  
850 # check that everything has gone - it better had!
851 0 foreach my $key (keys %{$self})
852 0 { warn ref($self) . " still has '$key' key left after release.\n"; }
853 }
854  
855 1 1;
856  
857 =head1 BUGS
858  
859 Bugs abound aplenty I am sure. There is a lot of code here and plenty of scope.
860 The parts of the code which haven't been implemented yet are:
861  
862 =over 4
863  
864 =item Post
865  
866 Version 4 format types are not supported yet.
867  
868 =item Cmap
869  
870 Format type 2 (MBCS) has not been implemented yet and therefore may cause
871 somewhat spurious results for this table type.
872  
873 =item Kern
874  
875 Only type 0 & type 2 tables are supported (type 1 & type 3 yet to come).
876  
877 =item TTC and WOFF
878  
879 The current Font::TTF::Font::out method does not support the writing of TrueType
880 Collections or WOFF files.
881  
882 =item DSIG
883  
884 Haven't figured out how to correctly calculate and output digital signature (DSIG) table
885  
886 =back
887  
888 In addition there are weaknesses or features of this module library
889  
890 =over 4
891  
892 =item *
893  
894 There is very little (or no) error reporting. This means that if you have
895 garbled data or garbled data structures, then you are liable to generate duff
896 fonts.
897  
898 =item *
899  
900 The exposing of the internal data structures everywhere means that doing
901 radical re-structuring is almost impossible. But it stop the code from becoming
902 ridiculously large.
903  
904 =back
905  
906 Apart from these, I try to keep the code in a state of "no known bugs", which
907 given the amount of testing this code has had, is not a guarantee of high
908 quality, yet.
909  
910 For more details see the appropriate class files.
911  
912 =head1 AUTHOR
913  
914 Martin Hosken L<http://scripts.sil.org/FontUtils>.
915  
916  
917 =head1 LICENSING
918  
919 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
920  
921 This module is released under the terms of the Artistic License 2.0.
922 For details, see the full text of the license in the file LICENSE.
923  
924  
925  
926 =cut
927  
« Font/TTF/Hhea.pm ↑ Summary Font/TTF/Head.pm »