1 package Font::TTF::Head;
5 Font::TTF::Head - The head table for a TTF Font
9 This is a very basic table with just instance variables as described in the
10 TTF documentation, using the same names. One of the most commonly used is
13 =head1 INSTANCE VARIABLES
15 The C<head> table has no internal instance variables beyond those common to all
16 tables and those specified in the standard:
36 The two dates are held as an array of two unsigned longs (32-bits)
43 use vars qw(@ISA %fields @field_info);
45 require Font::TTF::Table;
48 @ISA = qw(Font::TTF::Table);
51 'fontRevision' => 'f',
52 'checkSumAdjustment' => 'L',
63 'lowestRecPPEM' => 'S',
64 'fontDirectionHint' => 's',
65 'indexToLocFormat' => 's',
66 'glyphDataFormat' => 's');
71 for ($i = 0; $i < $#field_info; $i += 2)
73 ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]);
74 next unless defined $k && $k ne "";
82 Reads the table into memory thanks to some utility functions
91 $self->SUPER::read || return $self;
93 init unless defined $fields{'Ascender'};
94 $self->{' INFILE'}->read($dat, 54);
96 TTF_Read_Fields($self, $dat, \%fields);
103 Writes the table to a file either from memory or by copying. If in memory
104 (which is usually) the checkSumAdjustment field is set to 0 as per the default
105 if the file checksum is not to be considered.
111 my ($self, $fh) = @_;
113 return $self->SUPER::out($fh) unless $self->{' read'}; # this is never true
114 # $self->{'checkSumAdjustment'} = 0 unless $self->{' PARENT'}{' wantsig'};
115 $fh->print(TTF_Out_Fields($self, \%fields, 54));
120 =head2 $t->XML_element($context, $depth, $key, $value)
122 Handles date process for the XML exporter
129 my ($context, $depth, $key, $value) = @_;
130 my ($fh) = $context->{'fh'};
132 my (@month) = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC);
134 return $self->SUPER::XML_element(@_) unless ($key eq 'created' || $key eq 'modified');
136 @time = gmtime($self->getdate($key eq 'created'));
137 $output = sprintf("%d/%s/%d %d:%d:%d", $time[3], $month[$time[4]], $time[5] + 1900,
138 $time[2], $time[1], $time[0]);
139 $fh->print("$depth<$key>$output</$key>\n");
146 Updates the head table based on the glyph data and the hmtx table
153 my ($num, $i, $loc, $hmtx);
154 my ($xMin, $yMin, $xMax, $yMax, $lsbx);
156 return undef unless ($self->SUPER::update);
158 $num = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
159 return undef unless (defined $self->{' PARENT'}{'hmtx'} && defined $self->{' PARENT'}{'loca'});
160 $hmtx = $self->{' PARENT'}{'hmtx'}->read;
162 $self->{' PARENT'}{'loca'}->update;
163 $hmtx->update; # if we updated, then the flags will be set anyway.
165 for ($i = 0; $i < $num; $i++)
167 $loc = $self->{' PARENT'}{'loca'}{'glyphs'}[$i];
168 next unless defined $loc;
169 $loc->read->update_bbox;
170 $xMin = $loc->{'xMin'} if ($loc->{'xMin'} < $xMin || $i == 0);
171 $yMin = $loc->{'yMin'} if ($loc->{'yMin'} < $yMin || $i == 0);
172 $xMax = $loc->{'xMax'} if ($loc->{'xMax'} > $xMax);
173 $yMax = $loc->{'yMax'} if ($loc->{'yMax'} > $yMax);
174 $lsbx &= ($loc->{'xMin'} == $hmtx->{'lsb'}[$i]);
176 $self->{'xMin'} = $xMin;
177 $self->{'yMin'} = $yMin;
178 $self->{'xMax'} = $xMax;
179 $self->{'yMax'} = $yMax;
181 { $self->{'flags'} |= 2; }
183 { $self->{'flags'} &= ~2; }
188 =head2 $t->getdate($is_create)
190 Converts font modification time (or creation time if $is_create is set) to a 32-bit integer as returned
191 from time(). Returns undef if the value is out of range, either before the epoch or after the maximum
198 my ($self, $is_create) = @_;
199 my ($arr) = $self->{$is_create ? 'created' : 'modified'};
201 $arr->[1] -= 2082844800; # seconds between 1/Jan/1904 and 1/Jan/1970 (midnight)
204 $arr->[1] += 0xFFFFFFF; $arr->[1]++;
207 return undef if $arr->[0] != 0;
212 =head2 $t->setdate($time, $is_create)
214 Sets the time information for modification (or creation time if $is_create is set) according to the 32-bit
221 my ($self, $time, $is_create) = @_;
225 if ($arr[1] >= 0x83DA4F80)
227 $arr[1] -= 0xFFFFFFFF;
231 $arr[1] += 2082844800;
232 $self->{$is_create ? 'created' : 'modified'} = \@arr;
246 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and