Font/TTF/EBDT.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 176 CC 4 SCAR 14.8
CC
4
Cov
0%
read
6.0
get_regions
6.0
out
6.0
CRAP 4.4
1 package Font::TTF::EBDT;
2  
3 =head1 NAME
4  
5 Font::TTF::EBDT - Embeeded Bitmap Data Table
6  
7 =head1 DESCRIPTION
8  
9 Contains the metrics and bitmap image data.
10  
11 =head1 INSTANCE VARIABLES
12  
13 Only has 'bitmap' instance variable. It is an array of assosiative
14 array keyed by glyph-id. The element is an object which consists
15 of metric information and image data.
16  
17 =over 4
18  
19 =item bitmap object
20  
21 =over 8
22  
23 =item format
24  
25 Only 7 is supported.
26  
27 =item height
28  
29 =item width
30  
31 =item horiBearingX
32  
33 =item horiBearingY
34  
35 =item horiAdvance
36  
37 =item vertBearingX
38  
39 =item vertBearingY
40  
41 =item vertAdvance
42  
43 =item imageData
44  
45 =back
46  
47 =back
48  
49 =head1 METHODS
50  
51 =cut
52  
53 use strict;
54 use vars qw(@ISA);
55 require Font::TTF::Table;
56  
57 @ISA = qw(Font::TTF::Table);
58  
59  
60 =head2 $t->read
61  
62 Reads the embedded bitmap data from the TTF file into memory.
63 This routine should be called _after_ {'EBLC'}->read.
64  
65 =cut
66  
67 sub read
68 {
69 my ($self) = shift;
70 my ($fh);
71 my ($i, $dat);
72 my ($eblc) = $self->{' PARENT'}->{'EBLC'};
73 my ($bst_array);
74  
75 $eblc->read;
76 $self->SUPER::read || return $self;
77 $fh = $self->{' INFILE'};
78  
79 # ebdtHeader
80 $fh->read($dat, 4); # version
81  
82 $bst_array = $eblc->{'bitmapSizeTable'};
83  
84 for ($i = 0; $i < $eblc->{'Num'}; $i++)
85 {
86 my ($bst) = $bst_array->[$i];
87 my ($format) = $bst->{'imageFormat'};
88 my ($offset) = $bst->{'imageDataOffset'};
89 my ($j);
90 my ($ist_array) = $eblc->{'indexSubTableArray'}[$i];
91 my ($bitmap) = {};
92  
93 die "Only EBDT format 7 is implemented." unless ($format == 7);
94  
95 $self->{'bitmap'}[$i] = $bitmap;
96  
97 for ($j = 0; $j < $bst->{'numberOfIndexSubTables'}; $j++) {
98 my ($ista) = $ist_array->[$j];
99 my ($offsetArray) = $eblc->{'indexSubTable'}[$i][$j];
100 my ($p, $o0, $c);
101  
102 # if ($fh->tell != $self->{' OFFSET'} + $offset) {
103 # $fh->seek($self->{' OFFSET'} + $offset, 0);
104 # }
105  
106 $p = 0;
107 $o0 = $offsetArray->[$p++];
108 for ($c = $ista->{'firstGlyphIndex'}; $c <= $ista->{'lastGlyphIndex'}; $c++)
109 {
110 my ($b) = {};
111 my ($o1) = $offsetArray->[$p++];
112 my ($len) = $o1 - $o0 - 8;
113  
114 # if ($fh->tell != $self->{' OFFSET'} + $offset + $o0) {
115 # $fh->seek($self->{' OFFSET'} + $offset + $o0, 0);
116 # }
117  
118 $fh->read($dat, 8);
119 ($b->{'height'},
120 $b->{'width'},
121 $b->{'horiBearingX'},
122 $b->{'horiBearingY'},
123 $b->{'horiAdvance'},
124 $b->{'vertBearingX'},
125 $b->{'vertBearingY'},
126 $b->{'vertAdvance'})
127 = unpack("cccccccc", $dat);
128  
129 $fh->read($dat, $len);
130 $b->{'imageData'} = $dat;
131 $b->{'format'} = 7; # bitmap and bigMetrics
132  
133 $bitmap->{$c} = $b;
134 $o0 = $o1;
135 }
136  
137 $offset += $o0;
138 }
139 }
140  
141 $self;
142 }
143  
144  
145 =head2 $t->update
146  
147 Update EBLC information using EBDT data.
148  
149 =cut
150  
151 sub get_regions
152 {
153 my (@l) = @_;
154 my (@r) = ();
155 my ($e);
156 my ($first);
157 my ($last);
158  
159 $first = $l[0];
160 $last = $first - 1;
161 foreach $e (@l) {
162 if ($last + 1 != $e) { # not contiguous
163 $r[++$#r] = [$first, $last];
164 $first = $e;
165 }
166  
167 $last = $e;
168 }
169  
170 $r[++$#r] = [$first, $last];
171 @r;
172 }
173  
174 sub update
175 {
176 my ($self) = @_;
177 my ($eblc) = $self->{' PARENT'}->{'EBLC'};
178 my ($bst_array) = [];
179 my ($offset) = 4;
180 my ($i);
181 my ($bitmap_array) = $self->{'bitmap'};
182 my ($istao) = 8 + 48 * $eblc->{'Num'};
183  
184 $eblc->{'bitmapSizeTable'} = $bst_array;
185  
186 for ($i = 0; $i < $eblc->{'Num'}; $i++) {
187 my ($bst) = {};
188 my ($ist_array) = [];
189 my ($j);
190 my ($bitmap) = $bitmap_array->[$i];
191 my (@regions) = get_regions(sort {$a <=> $b} keys (%$bitmap));
192 my ($aotis) = 8 * (1+$#regions);
193  
194 $bst->{'indexFormat'} = 1;
195 $bst->{'imageFormat'} = 7;
196 $bst->{'imageDataOffset'} = $offset;
197 $bst->{'numberOfIndexSubTables'} = 1+$#regions;
198 $bst->{'indexSubTableArrayOffset'} = $istao;
199 $bst->{'colorRef'} = 0;
200  
201 $bst->{'startGlyphIndex'} = $regions[0][0];
202 $bst->{'endGlyphIndex'} = $regions[-1][1];
203 $bst->{'bitDepth'} = 1;
204 $bst->{'flags'} = 1; # Horizontal
205 $bst_array->[$i] = $bst;
206  
207 $eblc->{'indexSubTableArray'}[$i] = $ist_array;
208 for ($j = 0; $j <= $#regions; $j++) {
209 my ($ista) = {};
210 my ($offsetArray) = [];
211 my ($p, $o0, $c);
212 $ist_array->[$j] = $ista;
213  
214 $ista->{'firstGlyphIndex'} = $regions[$j][0];
215 $ista->{'lastGlyphIndex'} = $regions[$j][1];
216 $ista->{'additionalOffsetToIndexSubtable'} = $aotis;
217 $eblc->{'indexSubTable'}[$i][$j] = $offsetArray;
218 $p = 0;
219 $o0 = 0;
220 for ($c = $regions[$j][0]; $c <= $regions[$j][1]; $c++) {
221 my ($b) = $bitmap->{$c};
222  
223 $offsetArray->[$p++] = $o0;
224 $o0 += 8 + length($b->{'imageData'});
225 }
226  
227 $offsetArray->[$p++] = $o0;
228  
229 $aotis += ($regions[$j][1] - $regions[$j][0] + 1 + 1)*4;
230 $offset += $o0;
231  
232 # Do we need the element of 0x10007 and absolute offset here,
233 # at the end of offsetArray?
234 # if ($j + 1 <= $#regions) {
235 # $offsetArray->[$p++] = 0x10007;
236 # $offsetArray->[$p++] = $offset;
237 # $aotis += 8;
238 # }
239 }
240  
241 $istao += $aotis + 8;
242 $bst->{'indexTablesSize'} = $aotis + 8;
243 }
244 }
245  
246 =head2 $t->out($fh)
247  
248 Outputs the bitmap data of embedded bitmap for this font.
249  
250 =cut
251  
252 sub out
253 {
254 my ($self, $fh) = @_;
255  
256 return $self->SUPER::out($fh) unless $self->{' read'};
257  
258 my ($eblc) = $self->{' PARENT'}->{'EBLC'};
259 my ($i);
260 my ($bitmap_array) = $self->{'bitmap'};
261  
262 $fh->print(pack("N", 0x00020000));
263  
264 for ($i = 0; $i < $eblc->{'Num'}; $i++) {
265 my ($j);
266 my ($bitmap) = $bitmap_array->[$i];
267 my (@regions) = get_regions(sort {$a <=> $b} keys (%$bitmap));
268  
269 for ($j = 0; $j <= $#regions; $j++) {
270 my ($c);
271  
272 for ($c = $regions[$j][0]; $c <= $regions[$j][1]; $c++) {
273 my ($b) = $bitmap->{$c};
274  
275 $fh->print(pack("cccccccc",
276 $b->{'height'}, $b->{'width'},
277 $b->{'horiBearingX'}, $b->{'horiBearingY'},
278 $b->{'horiAdvance'}, $b->{'vertBearingX'},
279 $b->{'vertBearingY'}, $b->{'vertAdvance'}));
280 $fh->print($b->{'imageData'});
281 }
282 }
283 }
284 }
285  
286 1;
287  
288 =head1 BUGS
289  
290 Only Format 7 is implemented. XML output is not supported (yet).
291  
292 =head1 AUTHOR
293  
294 NIIBE Yutaka L<mailto:gniibe@fsij.org>.
295 This was written at the CodeFest Akihabara 2006 hosted by FSIJ.
296  
297 ?? patch sent with licensing requirements or not?
298  
299  
300 =head1 LICENSING
301  
302 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
303  
304 This module is released under the terms of the Artistic License 2.0.
305 For details, see the full text of the license in the file LICENSE.
306  
307  
308  
309  
310 =cut
311