Font/TTF/Loca.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 68.4% 26 / 38 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 5 / 5 totaltotal 84.2% 91 / 108 CC 22 SCAR 22.9
CC
22
Cov
84%
read
15.1
out
8.3
glyphs_do
3.0
CRAP 9.9
« Font/TTF/Dumper.pm ↑ Summary Font/TTF/Ttc.pm »
1 package Font::TTF::Loca;
2  
3 =head1 NAME
4  
5 Font::TTF::Loca - the Locations table, which is intimately tied to the glyf table
6  
7 =head1 DESCRIPTION
8  
9 The location table holds the directory of locations of each glyph within the
10 glyf table. Due to this relationship and the unimportance of the actual locations
11 when it comes to holding glyphs in memory, reading the location table results
12 in the creation of glyph objects for each glyph and stores them here.
13 So if you are looking for glyphs, do not look in the C<glyf> table, look here
14 instead.
15  
16 Things get complicated if you try to change the glyph list within the one table.
17 The recommendation is to create another clean location object to replace this
18 table in the font, ensuring that the old table is read first and to transfer
19 or copy glyphs across from the read table to the new table.
20  
21 =head1 INSTANCE VARIABLES
22  
23 The instance variables do not start with a space
24  
25 =over 4
26  
27 =item glyphs
28  
29 An array of glyph objects for each glyph.
30  
31 =item glyphtype
32  
33 A string containing the class name to create for each new glyph. If empty,
34 defaults to L<Font::TTF::Glyph>.
35  
36 =back
37  
38 =head1 METHODS
39  
40 =cut
41  
42 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 @ISA = qw(Font::TTF::Table);
45  
46 1 require Font::TTF::Glyph;
47  
48  
49 =head2 $t->new
50  
51 Creates a new location table making sure it has a glyphs array
52  
53 =cut
54  
55 sub new
56 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
57 2 my ($class) = shift;
58 2 my ($res) = $class->SUPER::new(@_);
59 2 $res->{'glyphs'} = [];
60 2 $res;
61 }
62  
63 =head2 $t->read
64  
65 Reads the location table creating glyph objects (L<Font::TTF::Glyph>) for each glyph
66 allowing their later reading.
67  
68 =cut
69  
70 sub read
71 4 ▶{
Subroutine read: called CC=13 SCAR=27.1
Pod documented
72 4 my ($self) = @_;
73  
74 # Do this before $self->SUPER::read because this can alter the file pointer:
75 4 my ($glyfLoc) = $self->{' PARENT'}{'glyf'}->_read->{' OFFSET'}; # May seek on $fh!
76  
77 4 ▶ $self->SUPER::read or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::read or return $self 2 2 4
78  
79 2 my ($fh) = $self->{' INFILE'};
80 2 my ($locFmt) = $self->{' PARENT'}{'head'}{'indexToLocFormat'};
81 2 my ($numGlyphs) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
82 2 my ($dat, $last, $i, $loc);
83  
84 2 ▶ $fh->read($dat, $locFmt ? 4 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$locFmt ? : 0 2 2
85 2 ▶ $last = unpack($locFmt ? "N" : "n", $dat);
Branch: 1 branch
BranchTrueFalseTotal
$locFmt ? : 0 2 2
86 2 ▶ for ($i = 0; $i < $numGlyphs; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numGlyphs) 196 2 198
87 {
88 196 ▶ $fh->read($dat, $locFmt ? 4 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$locFmt ? : 0 196 196
89 196 ▶ $loc = unpack($locFmt ? "N" : "n", $dat);
Branch: 1 branch
BranchTrueFalseTotal
$locFmt ? : 0 196 196
90 $self->{'glyphs'}[$i] = ($self->{'glyphtype'} || "Font::TTF::Glyph")->new(
91 LOC => $last << ($locFmt ? 0 : 1),
92 OUTLOC => $last << ($locFmt ? 0 : 1),
93 PARENT => $self->{' PARENT'},
94 196 ▶ INFILE => $self->{' PARENT'}{'glyf'}{' INFILE'},
Branch: 5 branches
BranchTrueFalseTotal
if $loc != $last 188 8 196
$locFmt ? : 0 188 188
$locFmt ? : 0 188 188
$locFmt ? : 0 188 188
$locFmt ? : 0 188 188
Condition50%1/2
$self->{'glyphtype'} || 'Font::TTF::Glyph'
l!l
0188
MC/DC0%0/1
$self->{'glyphtype'} || 'Font::TTF::Glyph'
$self->{'glyphtype'}
Truth table50%1/2
A
A= $self->{'glyphtype'}
Aresult
11
00
95 BASE => $glyfLoc,
96 OUTLEN => ($loc - $last) << ($locFmt ? 0 : 1),
97 LEN => ($loc - $last) << ($locFmt ? 0 : 1)) if ($loc != $last);
98 196 $last = $loc;
99 }
100 2 $self;
101 }
102  
103  
104 =head2 $t->out($fh)
105  
106 Writes the location table out to $fh. Notice that not having read the location
107 table implies that the glyf table has not been read either, so the numbers in
108 the location table are still valid. Let's hope that C<maxp/numGlyphs> and
109 C<head/indexToLocFmt> haven't changed otherwise we are in big trouble.
110  
111 The function uses the OUTLOC location in the glyph calculated when the glyf
112 table was attempted to be output.
113  
114 =cut
115  
116 sub out
117 2 ▶{
Subroutine out: called CC=8 SCAR=21.1
Pod documented
118 2 my ($self, $fh) = @_;
119 2 my ($locFmt) = $self->{' PARENT'}{'head'}{'indexToLocFormat'};
120 2 my ($numGlyphs) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
121 2 my ($count, $i, $offset, $g);
122  
123 2 ▶ return $self->SUPER::out($fh) unless ($self->{' read'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" read"} 0 2 2
124  
125 2 $count = 0;
126 2 ▶ for ($i = 0; $i < $numGlyphs; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numGlyphs) 196 2 198
127 {
128 196 ▶ $g = ($self->{'glyphs'}[$i]) || "";
Condition100%2/2
$self->{'glyphs'}[$i] || ''
l!l
1888
MC/DC100%1/1
$self->{'glyphs'}[$i] || ''
$self->{'glyphs'}[$i]
Truth table100%2/2
A
A= $self->{'glyphs'}[$i]
Aresult
11
00
129 196 ▶ unless ($g)
Branch: 1 branch
BranchTrueFalseTotal
if ($g) { } 188 8 196
130 {
131 8 $count++;
132 8 next;
133 } else
134 {
135 188 ▶ if ($locFmt)
Branch: 1 branch
BranchTrueFalseTotal
if ($locFmt) { } 0 188 188
136 0 { $fh->print(pack("N", $g->{' OUTLOC'}) x ($count + 1)); }
137 else
138 188 { $fh->print(pack("n", $g->{' OUTLOC'} >> 1) x ($count + 1)); }
139 188 $count = 0;
140 188 $offset = $g->{' OUTLOC'} + $g->{' OUTLEN'};
141 }
142 }
143 2 ▶ $fh->print(pack($locFmt ? "N" : "n", ($locFmt ? $offset: $offset >> 1)) x ($count + 1));
Branch: 2 branches
BranchTrueFalseTotal
$locFmt ? : 0 2 2
$locFmt ? : 0 2 2
144 }
145  
146  
147 =head2 $t->out_xml($context, $depth)
148  
149 No need to output a loca table, this is dynamically generated
150  
151 =cut
152  
153 sub out_xml
154 0 ▶{ return $_[0]; }
Subroutine out_xml: not called CC=1 SCAR=6.9
Pod documented
155  
156  
157 =head2 $t->glyphs_do(&func)
158  
159 Calls func for each glyph in this location table in numerical order:
160  
161 &func($glyph, $glyph_num)
162  
163 =cut
164  
165 sub glyphs_do
166 2 ▶{
Subroutine glyphs_do: called CC=3 SCAR=11.0
Pod documented
167 2 my ($self, $func) = @_;
168 2 my ($i);
169  
170 2 ▶ for ($i = 0; $i <= $#{$self->{'glyphs'}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"glyphs"};}) 196 2 198
171 196 ▶ { &$func($self->{'glyphs'}[$i], $i) if defined $self->{'glyphs'}[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"glyphs"}[$i] 188 8 196
172 2 $self;
173 }
174  
175 1 1;
176  
177 =head1 BUGS
178  
179 None known
180  
181 =head1 AUTHOR
182  
183 Martin Hosken L<http://scripts.sil.org/FontUtils>.
184  
185  
186 =head1 LICENSING
187  
188 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
189  
190 This module is released under the terms of the Artistic License 2.0.
191 For details, see the full text of the license in the file LICENSE.
192  
193  
194  
195 =cut
196  
197  
« Font/TTF/Dumper.pm ↑ Summary Font/TTF/Ttc.pm »