Font/TTF/GrFeat.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 160 CC 16 SCAR 28.5
CC
16
Cov
0%
read
30.0
out
30.0
print
20.0
CRAP 17.3
1 package Font::TTF::GrFeat;
2  
3 =head1 NAME
4  
5 Font::TTF::GrFeat - Graphite Font Features
6  
7 =head1 DESCRIPTION
8  
9 =head1 INSTANCE VARIABLES
10  
11 =over 4
12  
13 =item version
14  
15 =item features
16  
17 An array of hashes of the following form
18  
19 =over 8
20  
21 =item feature
22  
23 feature id number
24  
25 =item name
26  
27 name index in name table
28  
29 =item exclusive
30  
31 exclusive flag
32  
33 =item default
34  
35 the default setting number
36  
37 =item settings
38  
39 hash of setting number against name string index
40  
41 =back
42  
43 =back
44  
45 =head1 METHODS
46  
47 =cut
48  
49 use strict;
50 use vars qw(@ISA);
51  
52 use Font::TTF::Utils;
53  
54 require Font::TTF::Table;
55  
56 @ISA = qw(Font::TTF::Table);
57  
58 =head2 $t->read
59  
60 Reads the features from the TTF file into memory
61  
62 =cut
63  
64 sub read
65 {
66 my ($self) = @_;
67 my ($featureCount, $features);
68  
69 return $self if $self->{' read'};
70 $self->SUPER::read_dat or return $self;
71  
72 ($self->{'version'}, $featureCount) = TTF_Unpack("vS", $self->{' dat'});
73  
74 $features = [];
75 foreach (1 .. $featureCount) {
76 my ($feature, $nSettings, $settingTable, $featureFlags, $nameIndex, $reserved);
77 if ($self->{'version'} == 1)
78 {
79 ($feature, $nSettings, $settingTable, $featureFlags, $nameIndex)
80 = TTF_Unpack("SSLSS", substr($self->{' dat'}, $_ * 12, 12));
81 #The version 1 Feat table ends with a feature (id 1) named NoName
82 #with zero settings but with an offset to the last entry in the setting
83 #array. This last setting has id 0 and an invalid name id. This last
84 #feature is changed to have one setting.
85 if ($_ == $featureCount && $nSettings == 0) {$nSettings = 1;}
86 }
87 else #version == 2
88 {($feature, $nSettings, $reserved, $settingTable, $featureFlags, $nameIndex)
89 = TTF_Unpack("LSSLSS", substr($self->{' dat'}, 12 + ($_ - 1) * 16, 16))};
90 $feature =
91 {
92 'feature' => $feature,
93 'name' => $nameIndex,
94 };
95
96 #interpret the featureFlags & store settings
97 $feature->{'exclusive'} = (($featureFlags & 0x8000) != 0);
98
99 my @settings = TTF_Unpack("S*", substr($self->{' dat'}, $settingTable, $nSettings * 4));
100 if ($featureFlags & 0x4000)
101 {$feature->{'default'} = $featureFlags & 0x00FF;}
102 else
103 {$feature->{'default'} = $settings[0];}
104 $feature->{'settings'} = {@settings};
105
106 push(@$features, $feature);
107 }
108
109 $self->{'features'} = $features;
110
111 delete $self->{' dat'}; # no longer needed, and may become obsolete
112 $self->{' read'} = 1;
113 $self;
114 }
115  
116 =head2 $t->out($fh)
117  
118 Writes the features to a TTF file
119  
120 =cut
121  
122 sub out
123 {
124 my ($self, $fh) = @_;
125 my ($features, $numFeatures, $settings, $featureFlags, $featuresData, $settingsData);
126
127 return $self->SUPER::out($fh) unless $self->{' read'};
128  
129 $features = $self->{'features'};
130 $numFeatures = @$features;
131 $featuresData = $settingsData = '';
132  
133 foreach (@$features) {
134 $settings = $_->{'settings'};
135 $featureFlags = ($_->{'exclusive'} ? 0x8000 : 0x0000);
136
137 # output default setting first instead of using the featureFlags (as done below)
138 # $featureFlags = ($_->{'exclusive'} ? 0x8000 : 0x0000) |
139 # ($_->{'default'} != 0 ? 0x4000 | ($_->{'default'} & 0x00FF)
140 # : 0x0000);
141 if ($self->{'version'} == 1)
142 {
143 $featuresData .= TTF_Pack("SSLSS",
144 $_->{'feature'},
145 scalar keys %$settings,
146 12 + 12 * $numFeatures + length $settingsData,
147 $featureFlags,
148 $_->{'name'});
149 }
150 else #version == 2
151 {
152 $featuresData .= TTF_Pack("LSSLSS",
153 $_->{'feature'},
154 scalar keys %$settings,
155 0,
156 12 + 16 * $numFeatures + length $settingsData,
157 $featureFlags,
158 $_->{'name'});
159 }
160
161 #output default setting first
162 #the settings may not be in their original order
163 my $defaultSetting = $_->{'default'};
164 $settingsData .= TTF_Pack("SS", $defaultSetting, $settings->{$defaultSetting});
165 foreach (sort {$a <=> $b} keys %$settings) {
166 if ($_ == $defaultSetting) {next;} #skip default setting
167 $settingsData .= TTF_Pack("SS", $_, $settings->{$_});
168 }
169 }
170  
171 $fh->print(TTF_Pack("vSSL", $self->{'version'}, $numFeatures, 0, 0));
172 $fh->print($featuresData);
173 $fh->print($settingsData);
174  
175 $self;
176 }
177  
178 =head2 $t->minsize()
179  
180 Returns the minimum size this table can be. If it is smaller than this, then the table
181 must be bad and should be deleted or whatever.
182  
183 =cut
184  
185 sub minsize
186 {
187 return 6;
188 }
189  
190 =head2 $t->print($fh)
191  
192 Prints a human-readable representation of the table
193  
194 =cut
195  
196 sub print
197 {
198 my ($self, $fh) = @_;
199 my ($names, $features, $settings);
200  
201 $self->read;
202  
203 $names = $self->{' PARENT'}->{'name'};
204 $names->read;
205  
206 $fh = 'STDOUT' unless defined $fh;
207  
208 $features = $self->{'features'};
209 foreach (@$features) {
210 $fh->printf("Feature %s, %s, default: %d name %d # '%s'\n",
211 $_->{'feature'} > 0x01000000 ? '"' . $self->num_to_tag($_->{'feature'}) . '"' : $_->{'feature'},
212 ($_->{'exclusive'} ? "exclusive" : "additive"),
213 $_->{'default'},
214 $_->{'name'},
215 $names->{'strings'}[$_->{'name'}][3][1]{1033});
216 $settings = $_->{'settings'};
217 foreach (sort { $a <=> $b } keys %$settings) {
218 $fh->printf("\tSetting %d, name %d # '%s'\n",
219 $_, $settings->{$_}, $names->{'strings'}[$settings->{$_}][3][1]{1033});
220 }
221 }
222
223 $self;
224 }
225  
226 sub settingName
227 {
228 my ($self, $feature, $setting) = @_;
229  
230 $self->read;
231  
232 my $names = $self->{' PARENT'}->{'name'};
233 $names->read;
234
235 my $features = $self->{'features'};
236 my ($featureEntry) = grep { $_->{'feature'} == $feature } @$features;
237 my $featureName = $names->{'strings'}[$featureEntry->{'name'}][3][1]{1033};
238 my $settingName = $featureEntry->{'exclusive'}
239 ? $names->{'strings'}[$featureEntry->{'settings'}->{$setting}][3][1]{1033}
240 : $names->{'strings'}[$featureEntry->{'settings'}->{$setting & ~1}][3][1]{1033}
241 . (($setting & 1) == 0 ? " On" : " Off");
242  
243 ($featureName, $settingName);
244 }
245  
246 =head2 $t->tag_to_num ($feat_str)
247  
248 Convert an alphanumeric feature id tag (string) to a number (32-bit).
249 Tags are normally 4 chars. Graphite ignores space
250 padding if it is present, so we do the same here.
251  
252 =cut
253  
254 sub tag_to_num
255 {
256 my ($self, $feat_tag) = @_;
257 my $new_feat_num;
258
259 if ($feat_tag > 0)
260 {$new_feat_num = $feat_tag;} # already a number, so just return it.
261 else
262 {
263 $feat_tag =~ s/[ \000]+$//o; # strip trailing nulls or space
264 $new_feat_num = unpack('N', pack('a4', $feat_tag)); #adds null padding on right if less than 4 chars
265 }
266
267 return $new_feat_num;
268 }
269  
270 =head2 $t->num_to_tag ($feat_num)
271  
272 Convert a feature id number (32-bit) back to a tag (string).
273 Trailing space or null padding is removed.
274 Feature id numbers that do not represent alphanumeric tags
275 are returned unchanged.
276  
277 =cut
278  
279 sub num_to_tag
280 {
281 my ($self, $feat_num) = @_;
282 my $new_feat_tag;
283
284 if ($feat_num > 0x01000000)
285 {
286 $new_feat_tag = unpack('a4', pack('N', $feat_num));
287 $new_feat_tag =~ s/[ \000]+$//o; # strip trailing nulls or space
288 }
289 else
290 {$new_feat_tag = $feat_num;}
291
292 return $new_feat_tag;
293 }
294  
295 1;
296  
297 =head1 BUGS
298  
299 The version 1 Feat table ends with a feature (id 1) named NoName
300 with zero settings but with an offset to the last entry in the setting
301 array. This last setting has id 0 and an invalid name id. This last
302 feature is changed to have one setting.
303  
304 =head1 AUTHOR
305  
306 Alan Ward (derived from Jonathan Kew's Feat.pm).
307  
308  
309 =head1 LICENSING
310  
311 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
312  
313 This module is released under the terms of the Artistic License 2.0.
314 For details, see the full text of the license in the file LICENSE.
315  
316  
317  
318 =cut
319  
320