| 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: Head.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Head; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Head - The head table for a TTF Font | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is a very basic table with just instance variables as described in the | ||
| 36 | TTF documentation, using the same names. One of the most commonly used is | ||
| 37 | C<unitsPerEm>. | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | The C<head> table has no internal instance variables beyond those common to all | ||
| 42 | tables and those specified in the standard: | ||
| 43 | |||
| 44 | version | ||
| 45 | fontRevision | ||
| 46 | checkSumAdjustment | ||
| 47 | magicNumber | ||
| 48 | flags | ||
| 49 | unitsPerEm | ||
| 50 | created | ||
| 51 | modified | ||
| 52 | xMin | ||
| 53 | yMin | ||
| 54 | xMax | ||
| 55 | yMax | ||
| 56 | macStyle | ||
| 57 | lowestRecPPEM | ||
| 58 | fontDirectionHint | ||
| 59 | indexToLocFormat | ||
| 60 | glyphDataFormat | ||
| 61 | |||
| 62 | The two dates are held as an array of two unsigned longs (32-bits) | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | use strict; | ||
| 69 | use vars qw(@ISA %fields @field_info); | ||
| 70 | |||
| 71 | require PDF::API3::Compat::API2::Basic::TTF::Table; | ||
| 72 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 73 | |||
| 74 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table); | ||
| 75 | @field_info = ( | ||
| 76 | 'version' => 'f', | ||
| 77 | 'fontRevision' => 'f', | ||
| 78 | 'checkSumAdjustment' => 'L', | ||
| 79 | 'magicNumber' => 'L', | ||
| 80 | 'flags' => 'S', | ||
| 81 | 'unitsPerEm' => 'S', | ||
| 82 | 'created' => 'L2', | ||
| 83 | 'modified' => 'L2', | ||
| 84 | 'xMin' => 's', | ||
| 85 | 'yMin' => 's', | ||
| 86 | 'xMax' => 's', | ||
| 87 | 'yMax' => 's', | ||
| 88 | 'macStyle' => 'S', | ||
| 89 | 'lowestRecPPEM' => 'S', | ||
| 90 | 'fontDirectionHint' => 's', | ||
| 91 | 'indexToLocFormat' => 's', | ||
| 92 | 'glyphDataFormat' => 's'); | ||
| 93 | |||
| 94 | sub init | ||
| 95 | { | ||
| 96 | my ($k, $v, $c, $i); | ||
| 97 | for ($i = 0; $i < $#field_info; $i += 2) | ||
| 98 | { | ||
| 99 | ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]); | ||
| 100 | next unless defined $k && $k ne ""; | ||
| 101 | $fields{$k} = $v; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | =head2 $t->read | ||
| 107 | |||
| 108 | Reads the table into memory thanks to some utility functions | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub read | ||
| 113 | { | ||
| 114 | my ($self) = @_; | ||
| 115 | my ($dat); | ||
| 116 | |||
| 117 | $self->SUPER::read || return $self; | ||
| 118 | |||
| 119 | init unless defined $fields{'Ascender'}; | ||
| 120 | $self->{' INFILE'}->read($dat, 54); | ||
| 121 | |||
| 122 | TTF_Read_Fields($self, $dat, \%fields); | ||
| 123 | $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | =head2 $t->out($fh) | ||
| 128 | |||
| 129 | Writes the table to a file either from memory or by copying. If in memory | ||
| 130 | (which is usually) the checkSumAdjustment field is set to 0 as per the default | ||
| 131 | if the file checksum is not to be considered. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub out | ||
| 136 | { | ||
| 137 | my ($self, $fh) = @_; | ||
| 138 | |||
| 139 | return $self->SUPER::out($fh) unless $self->{' read'}; # this is never true | ||
| 140 | # $self->{'checkSumAdjustment'} = 0 unless $self->{' PARENT'}{' wantsig'}; | ||
| 141 | $fh->print(TTF_Out_Fields($self, \%fields, 54)); | ||
| 142 | $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | =head2 $t->XML_element($context, $depth, $key, $value) | ||
| 147 | |||
| 148 | Handles date process for the XML exporter | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub XML_element | ||
| 153 | { | ||
| 154 | my ($self) = shift; | ||
| 155 | my ($context, $depth, $key, $value) = @_; | ||
| 156 | my ($fh) = $context->{'fh'}; | ||
| 157 | my ($output, @time); | ||
| 158 | my (@month) = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC); | ||
| 159 | |||
| 160 | return $self->SUPER::XML_element(@_) unless ($key eq 'created' || $key eq 'modified'); | ||
| 161 | |||
| 162 | @time = gmtime($self->getdate($key eq 'created')); | ||
| 163 | $output = sprintf("%d/%s/%d %d:%d:%d", $time[3], $month[$time[4]], $time[5] + 1900, | ||
| 164 | $time[2], $time[1], $time[0]); | ||
| 165 | $fh->print("$depth<$key>$output</$key>\n"); | ||
| 166 | $self; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | =head2 $t->update | ||
| 171 | |||
| 172 | Updates the head table based on the glyph data and the hmtx table | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub update | ||
| 177 | { | ||
| 178 | my ($self) = @_; | ||
| 179 | my ($num, $i, $loc, $hmtx); | ||
| 180 | my ($xMin, $yMin, $xMax, $yMax, $lsbx); | ||
| 181 | |||
| 182 | return undef unless ($self->SUPER::update); | ||
| 183 | |||
| 184 | $num = $self->{' PARENT'}{'maxp'}{'numGlyphs'}; | ||
| 185 | return undef unless (defined $self->{' PARENT'}{'hmtx'} && defined $self->{' PARENT'}{'loca'}); | ||
| 186 | $hmtx = $self->{' PARENT'}{'hmtx'}->read; | ||
| 187 | |||
| 188 | $self->{' PARENT'}{'loca'}->update; | ||
| 189 | $hmtx->update; # if we updated, then the flags will be set anyway. | ||
| 190 | $lsbx = 1; | ||
| 191 | for ($i = 0; $i < $num; $i++) | ||
| 192 | { | ||
| 193 | $loc = $self->{' PARENT'}{'loca'}{'glyphs'}[$i]; | ||
| 194 | next unless defined $loc; | ||
| 195 | $loc->read->update_bbox; | ||
| 196 | $xMin = $loc->{'xMin'} if ($loc->{'xMin'} < $xMin || $i == 0); | ||
| 197 | $yMin = $loc->{'yMin'} if ($loc->{'yMin'} < $yMin || $i == 0); | ||
| 198 | $xMax = $loc->{'xMax'} if ($loc->{'xMax'} > $xMax); | ||
| 199 | $yMax = $loc->{'yMax'} if ($loc->{'yMax'} > $yMax); | ||
| 200 | $lsbx &= ($loc->{'xMin'} == $hmtx->{'lsb'}[$i]); | ||
| 201 | } | ||
| 202 | $self->{'xMin'} = $xMin; | ||
| 203 | $self->{'yMin'} = $yMin; | ||
| 204 | $self->{'xMax'} = $xMax; | ||
| 205 | $self->{'yMax'} = $yMax; | ||
| 206 | if ($lsbx) | ||
| 207 | { $self->{'flags'} |= 2; } | ||
| 208 | else | ||
| 209 | { $self->{'flags'} &= ~2; } | ||
| 210 | $self; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | =head2 $t->getdate($is_create) | ||
| 215 | |||
| 216 | Converts font modification time (or creation time if $is_create is set) to a 32-bit integer as returned | ||
| 217 | from time(). Returns undef if the value is out of range, either before the epoch or after the maximum | ||
| 218 | storable time. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub getdate | ||
| 223 | { | ||
| 224 | my ($self, $is_create) = @_; | ||
| 225 | my ($arr) = $self->{$is_create ? 'created' : 'modified'}; | ||
| 226 | |||
| 227 | $arr->[1] -= 2082844800; # seconds between 1/Jan/1904 and 1/Jan/1970 (midnight) | ||
| 228 | if ($arr->[1] < 0) | ||
| 229 | { | ||
| 230 | $arr->[1] += 0xFFFFFFF; $arr->[1]++; | ||
| 231 | $arr->[0]--; | ||
| 232 | } | ||
| 233 | return undef if $arr->[0] != 0; | ||
| 234 | return $arr->[1]; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | =head2 $t->setdate($time, $is_create) | ||
| 239 | |||
| 240 | Sets the time information for modification (or creation time if $is_create is set) according to the 32-bit | ||
| 241 | time information. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub setdate | ||
| 246 | { | ||
| 247 | my ($self, $time, $is_create) = @_; | ||
| 248 | my (@arr); | ||
| 249 | |||
| 250 | $arr[1] = $time; | ||
| 251 | if ($arr[1] >= 0x83DA4F80) | ||
| 252 | { | ||
| 253 | $arr[1] -= 0xFFFFFFFF; | ||
| 254 | $arr[1]--; | ||
| 255 | $arr[0]++; | ||
| 256 | } | ||
| 257 | $arr[1] += 2082844800; | ||
| 258 | $self->{$is_create ? 'created' : 'modified'} = \@arr; | ||
| 259 | $self; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | 1; | ||
| 264 | |||
| 265 | |||
| 266 | =head1 BUGS | ||
| 267 | |||
| 268 | None known | ||
| 269 | |||
| 270 | =head1 AUTHOR | ||
| 271 | |||
| 272 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 273 | licensing. | ||
| 274 | |||
| 275 | =cut | ||
| 276 |