also dont add tuples to lists sleepy man.
[librarian.git] / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / GrFeat.pm
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                 my $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->print($fh)
179
180 Prints a human-readable representation of the table
181
182 =cut
183
184 sub print
185 {
186         my ($self, $fh) = @_;
187         my ($names, $features, $settings);
188
189         $self->read;
190
191         $names = $self->{' PARENT'}->{'name'};
192         $names->read;
193
194         $fh = 'STDOUT' unless defined $fh;
195
196         $features = $self->{'features'};
197         foreach (@$features) {
198                 $fh->printf("Feature %d, %s, default: %d name %d # '%s'\n",
199                                         $_->{'feature'},
200                                         ($_->{'exclusive'} ? "exclusive" : "additive"),
201                                         $_->{'default'}, 
202                                         $_->{'name'},
203                                         $names->{'strings'}[$_->{'name'}][3][1]{1033});
204                 $settings = $_->{'settings'};
205                 foreach (sort { $a <=> $b } keys %$settings) {
206                         $fh->printf("\tSetting %d, name %d # '%s'\n",
207                                                 $_, $settings->{$_}, $names->{'strings'}[$settings->{$_}][3][1]{1033});
208                 }
209         }
210         
211         $self;
212 }
213
214 sub settingName
215 {
216         my ($self, $feature, $setting) = @_;
217
218         $self->read;
219
220         my $names = $self->{' PARENT'}->{'name'};
221         $names->read;
222         
223         my $features = $self->{'features'};
224         my ($featureEntry) = grep { $_->{'feature'} == $feature } @$features;
225         my $featureName = $names->{'strings'}[$featureEntry->{'name'}][3][1]{1033};
226         my $settingName = $featureEntry->{'exclusive'}
227                         ? $names->{'strings'}[$featureEntry->{'settings'}->{$setting}][3][1]{1033}
228                         : $names->{'strings'}[$featureEntry->{'settings'}->{$setting & ~1}][3][1]{1033}
229                                 . (($setting & 1) == 0 ? " On" : " Off");
230
231         ($featureName, $settingName);
232 }
233
234 1;
235
236 =head1 BUGS
237
238 The version 1 Feat table ends with a feature (id 1) named NoName
239 with zero settings but with an offset to the last entry in the setting
240 array. This last setting has id 0 and an invalid name id. This last
241 feature is changed to have one setting.
242
243 =head1 AUTHOR
244
245 Alan Ward (derived from Jonathan Kew's Feat.pm).
246 See L<Font::TTF::Font> for copyright and licensing.
247
248 =cut
249