API3/Compat/API2/Basic/TTF/Hmtx.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 132 CC 11 SCAR 23.5
CC
11
Cov
0%
_out
20.0
_read
12.0
numMetrics
12.0
CRAP 10.5
1 #=======================================================================
2 # ____ ____ _____ _ ____ ___ ____
3 # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4 # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5 # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6 # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7 #
8 # A Perl Module Chain to faciliate the Creation and Modification
9 # of High-Quality "Portable Document Format (PDF)" Files.
10 #
11 #=======================================================================
12 #
13 # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
14 #
15 #
16 # Copyright Martin Hosken <Martin_Hosken@sil.org>
17 #
18 # No warranty or expression of effectiveness, least of all regarding
19 # anyone's safety, is implied in this software or documentation.
20 #
21 # This specific module is licensed under the Perl Artistic License.
22 #
23 #
24 # $Id: Hmtx.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::Hmtx;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::Hmtx - Horizontal Metrics
32
33 =head1 DESCRIPTION
34
35 Contains the advance width and left side bearing for each glyph. Given the
36 compressability of the data onto disk, this table uses information from
37 other tables, and thus must do part of its output during the output of
38 other tables
39
40 =head1 INSTANCE VARIABLES
41
42 The horizontal metrics are kept in two arrays by glyph id. The variable names
43 do not start with a space
44
45 =over 4
46
47 =item advance
48
49 An array containing the advance width for each glyph
50
51 =item lsb
52
53 An array containing the left side bearing for each glyph
54
55 =back
56
57 =head1 METHODS
58
59 =cut
60
61 use strict;
62 use vars qw(@ISA);
63 require PDF::API3::Compat::API2::Basic::TTF::Table;
64
65 @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table);
66
67
68 =head2 $t->read
69
70 Reads the horizontal metrics from the TTF file into memory
71
72 =cut
73
74 sub read
75 {
76 my ($self) = @_;
77 my ($numh, $numg);
78
79 $numh = $self->{' PARENT'}{'hhea'}->read->{'numberOfHMetrics'};
80 $numg = $self->{' PARENT'}{'maxp'}->read->{'numGlyphs'};
81 $self->_read($numg, $numh, "advance", "lsb");
82 }
83
84 sub _read
85 {
86 my ($self, $numg, $numh, $tAdv, $tLsb) = @_;
87 my ($fh) = $self->{' INFILE'};
88 my ($i, $dat);
89
90 $self->SUPER::read or return $self;
91
92 for ($i = 0; $i < $numh; $i++)
93 {
94 $fh->read($dat, 4);
95 ($self->{$tAdv}[$i], $self->{$tLsb}[$i]) = unpack("nn", $dat);
96 $self->{$tLsb}[$i] -= 65536 if ($self->{$tLsb}[$i] >= 32768);
97 }
98
99 $i--;
100 while ($i++ < $numg)
101 {
102 $fh->read($dat, 2);
103 $self->{$tAdv}[$i] = $self->{$tAdv}[$numh - 1];
104 $self->{$tLsb}[$i] = unpack("n", $dat);
105 $self->{$tLsb}[$i] -= 65536 if ($self->{$tLsb}[$i] >= 32768);
106 }
107 $self;
108 }
109
110 =head2 $t->numMetrics
111
112 Calculates again the number of long metrics required to store the information
113 here. Returns undef if the table has not been read.
114
115 =cut
116
117 sub numMetrics
118 {
119 my ($self) = @_;
120 my ($numg) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
121 my ($i);
122
123 return undef unless $self->{' read'};
124
125 for ($i = $numg - 2; $i >= 0; $i--)
126 { last if ($self->{'advance'}[$i] != $self->{'advance'}[$i + 1]); }
127
128 return $i + 2;
129 }
130
131
132 =head2 $t->out($fh)
133
134 Writes the metrics to a TTF file. Assumes that the C<hhea> has updated the
135 numHMetrics from here
136
137 =cut
138
139 sub out
140 {
141 my ($self, $fh) = @_;
142 my ($numg) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
143 my ($numh) = $self->{' PARENT'}{'hhea'}->read->{'numberOfHMetrics'};
144 $self->_out($fh, $numg, $numh, "advance", "lsb");
145 }
146
147 sub _out
148 {
149 my ($self, $fh, $numg, $numh, $tAdv, $tLsb) = @_;
150 my ($i, $lsb);
151
152 return $self->SUPER::out($fh) unless ($self->{' read'});
153
154 for ($i = 0; $i < $numg; $i++)
155 {
156 $lsb = $self->{$tLsb}[$i];
157 $lsb += 65536 if $lsb < 0;
158 if ($i >= $numh)
159 { $fh->print(pack("n", $lsb)); }
160 else
161 { $fh->print(pack("n2", $self->{$tAdv}[$i], $lsb)); }
162 }
163 $self;
164 }
165
166
167 =head2 $t->update
168
169 Updates the lsb values from the xMin from the each glyph
170
171 =cut
172
173 sub update
174 {
175 my ($self) = @_;
176 my ($numg) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
177 my ($i);
178
179 return undef unless ($self->SUPER::update);
180 # lsb & xMin must always be the same, regardless of any flags!
181 # return $self unless ($self->{' PARENT'}{'head'}{'flags'} & 2); # lsb & xMin the same
182
183 $self->{' PARENT'}{'loca'}->update;
184 for ($i = 0; $i < $numg; $i++)
185 {
186 my ($g) = $self->{' PARENT'}{'loca'}{'glyphs'}[$i];
187 if ($g)
188 { $self->{'lsb'}[$i] = $g->read->update_bbox->{'xMin'}; }
189 else
190 { $self->{'lsb'}[$i] = 0; }
191 }
192 $self->{' PARENT'}{'head'}{'flags'} |= 2;
193 $self;
194 }
195
196
197 =head2 $t->out_xml($context, $depth)
198
199 Outputs the table in XML
200
201 =cut
202
203 sub out_xml
204 {
205 my ($self, $context, $depth) = @_;
206 my ($fh) = $context->{'fh'};
207 my ($numg) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
208 my ($addr) = ($self =~ m/\((.+)\)$/o);
209 my ($i);
210
211 if ($context->{'addresses'}{$addr})
212 {
213 $fh->printf("%s<%s id_ref='%s'/>\n", $depth, $context->{'name'}, $addr);
214 return $self;
215 }
216 else
217 { $fh->printf("%s<%s id='%s'>\n", $depth, $context->{'name'}, $addr); }
218
219 $self->read;
220
221 for ($i = 0; $i < $numg; $i++)
222 { $fh->print("$depth$context->{'indent'}<width adv='$self->{'advance'}[$i]' lsb='$self->{'lsb'}[$i]'/>\n"); }
223
224 $fh->print("$depth</$context->{'name'}>\n");
225 $self;
226 }
227
228 1;
229
230 =head1 BUGS
231
232 None known
233
234 =head1 AUTHOR
235
236 Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and
237 licensing.
238
239 =cut
240