| 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: OS_2.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::OS_2; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::OS_2 - the OS/2 table in a TTF font | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | The OS/2 table has two versions and forms, one an extension of the other. This | ||
| 36 | module supports both forms and the switching between them. | ||
| 37 | |||
| 38 | =head1 INSTANCE VARIABLES | ||
| 39 | |||
| 40 | No other variables than those in table and those in the standard: | ||
| 41 | |||
| 42 | Version | ||
| 43 | xAvgCharWidth | ||
| 44 | usWeightClass | ||
| 45 | usWidthClass | ||
| 46 | fsType | ||
| 47 | ySubscriptXSize | ||
| 48 | ySubScriptYSize | ||
| 49 | ySubscriptXOffset | ||
| 50 | ySubscriptYOffset | ||
| 51 | ySuperscriptXSize | ||
| 52 | ySuperscriptYSize | ||
| 53 | ySuperscriptXOffset | ||
| 54 | ySuperscriptYOffset | ||
| 55 | yStrikeoutSize | ||
| 56 | yStrikeoutPosition | ||
| 57 | sFamilyClass | ||
| 58 | bFamilyType | ||
| 59 | bSerifStyle | ||
| 60 | bWeight | ||
| 61 | bProportion | ||
| 62 | bContrast | ||
| 63 | bStrokeVariation | ||
| 64 | bArmStyle | ||
| 65 | bLetterform | ||
| 66 | bMidline | ||
| 67 | bXheight | ||
| 68 | ulUnicodeRange1 | ||
| 69 | ulUnicodeRange2 | ||
| 70 | ulUnicodeRange3 | ||
| 71 | ulUnicodeRange4 | ||
| 72 | achVendID | ||
| 73 | fsSelection | ||
| 74 | usFirstCharIndex | ||
| 75 | usLastCharIndex | ||
| 76 | sTypoAscender | ||
| 77 | sTypoDescender | ||
| 78 | sTypoLineGap | ||
| 79 | usWinAscent | ||
| 80 | usWinDescent | ||
| 81 | ulCodePageRange1 | ||
| 82 | ulCodePageRange2 | ||
| 83 | xHeight | ||
| 84 | CapHeight | ||
| 85 | defaultChar | ||
| 86 | breakChar | ||
| 87 | maxLookups | ||
| 88 | |||
| 89 | Notice that versions 0, 1 & 2 of the table are supported. Notice also that the | ||
| 90 | Panose variable has been broken down into its elements. | ||
| 91 | |||
| 92 | =head1 METHODS | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | use strict; | ||
| 97 | use vars qw(@ISA @fields @lens @field_info); | ||
| 98 | use PDF::API3::Compat::API2::Basic::TTF::Table; | ||
| 99 | |||
| 100 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table); | ||
| 101 | @field_info = ( | ||
| 102 | 'xAvgCharWidth' => 's', | ||
| 103 | 'usWeightClass' => 'S', | ||
| 104 | 'usWidthClass' => 'S', | ||
| 105 | 'fsType' => 's', | ||
| 106 | 'ySubscriptXSize' => 's', | ||
| 107 | 'ySubScriptYSize' => 's', | ||
| 108 | 'ySubscriptXOffset' => 's', | ||
| 109 | 'ySubscriptYOffset' => 's', | ||
| 110 | 'ySuperscriptXSize' => 's', | ||
| 111 | 'ySuperscriptYSize' => 's', | ||
| 112 | 'ySuperscriptXOffset' => 's', | ||
| 113 | 'ySuperscriptYOffset' => 's', | ||
| 114 | 'yStrikeoutSize' => 's', | ||
| 115 | 'yStrikeoutPosition' => 's', | ||
| 116 | 'sFamilyClass' => 's', | ||
| 117 | 'bFamilyType' => 'C', | ||
| 118 | 'bSerifStyle' => 'C', | ||
| 119 | 'bWeight' => 'C', | ||
| 120 | 'bProportion' => 'C', | ||
| 121 | 'bContrast' => 'C', | ||
| 122 | 'bStrokeVariation' => 'C', | ||
| 123 | 'bArmStyle' => 'C', | ||
| 124 | 'bLetterform' => 'C', | ||
| 125 | 'bMidline' => 'C', | ||
| 126 | 'bXheight' => 'C', | ||
| 127 | 'ulUnicodeRange1' => 'L', | ||
| 128 | 'ulUnicodeRange2' => 'L', | ||
| 129 | 'ulUnicodeRange3' => 'L', | ||
| 130 | 'ulUnicodeRange4' => 'L', | ||
| 131 | 'achVendID' => 'L', | ||
| 132 | 'fsSelection' => 'S', | ||
| 133 | 'usFirstCharIndex' => 'S', | ||
| 134 | 'usLastCharIndex' => 'S', | ||
| 135 | 'sTypoAscender' => 'S', | ||
| 136 | 'sTypoDescender' => 's', | ||
| 137 | 'sTypoLineGap' => 'S', | ||
| 138 | 'usWinAscent' => 'S', | ||
| 139 | 'usWinDescent' => 'S', | ||
| 140 | '' => '', | ||
| 141 | 'ulCodePageRange1' => 'L', | ||
| 142 | 'ulCodePageRange2' => 'L', | ||
| 143 | '' => '', | ||
| 144 | 'xHeight' => 's', | ||
| 145 | 'CapHeight' => 's', | ||
| 146 | 'defaultChar' => 'S', | ||
| 147 | 'breakChar' => 'S', | ||
| 148 | 'maxLookups' => 's'); | ||
| 149 | |||
| 150 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 151 | |||
| 152 | sub init | ||
| 153 | { | ||
| 154 | my ($k, $v, $c, $n, $i, $t, $j); | ||
| 155 | |||
| 156 | $n = 0; | ||
| 157 | @lens = (76, 84, 94); | ||
| 158 | for ($j = 0; $j < $#field_info; $j += 2) | ||
| 159 | { | ||
| 160 | if ($field_info[$j] eq '') | ||
| 161 | { | ||
| 162 | $n++; | ||
| 163 | next; | ||
| 164 | } | ||
| 165 | ($k, $v, $c) = TTF_Init_Fields($field_info[$j], $c, $field_info[$j+1]); | ||
| 166 | next unless defined $k && $k ne ""; | ||
| 167 | for ($i = $n; $i < 3; $i++) | ||
| 168 | { $fields[$i]{$k} = $v; } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | =head2 $t->read | ||
| 174 | |||
| 175 | Reads in the various values from disk (see details of OS/2 table) | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub read | ||
| 180 | { | ||
| 181 | my ($self) = @_; | ||
| 182 | my ($dat, $ver); | ||
| 183 | |||
| 184 | $self->SUPER::read or return $self; | ||
| 185 | |||
| 186 | init unless defined $fields[2]{'xAvgCharWidth'}; | ||
| 187 | $self->{' INFILE'}->read($dat, 2); | ||
| 188 | $ver = unpack("n", $dat); | ||
| 189 | $self->{'Version'} = $ver; | ||
| 190 | if ($ver < 3) | ||
| 191 | { | ||
| 192 | $self->{' INFILE'}->read($dat, $lens[$ver]); | ||
| 193 | TTF_Read_Fields($self, $dat, $fields[$ver]); | ||
| 194 | } | ||
| 195 | $self; | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | =head2 $t->out($fh) | ||
| 200 | |||
| 201 | Writes the table to a file either from memory or by copying. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub out | ||
| 206 | { | ||
| 207 | my ($self, $fh) = @_; | ||
| 208 | my ($ver); | ||
| 209 | |||
| 210 | return $self->SUPER::out($fh) unless $self->{' read'}; | ||
| 211 | |||
| 212 | $ver = $self->{'Version'}; | ||
| 213 | $fh->print(pack("n", $ver)); | ||
| 214 | $fh->print(TTF_Out_Fields($self, $fields[$ver], $lens[$ver])); | ||
| 215 | $self; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | =head2 $t->XML_element($context, $depth, $key, $value) | ||
| 220 | |||
| 221 | Tidies up the hex values to output them in hex | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub XML_element | ||
| 226 | { | ||
| 227 | my ($self) = shift; | ||
| 228 | my ($context, $depth, $key, $value) = @_; | ||
| 229 | my ($fh) = $context->{'fh'}; | ||
| 230 | |||
| 231 | if ($key =~ m/^ul(?:Unicode|CodePage)Range\d$/o) | ||
| 232 | { $fh->printf("%s<%s>%08X</%s>\n", $depth, $key, $value, $key); } | ||
| 233 | elsif ($key eq 'achVendID') | ||
| 234 | { $fh->printf("%s<%s name='%s'/>\n", $depth, $key, pack('N', $value)); } | ||
| 235 | else | ||
| 236 | { return $self->SUPER::XML_element(@_); } | ||
| 237 | $self; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | =head2 $t->XML_end($context, $tag, %attrs) | ||
| 242 | |||
| 243 | Now handle them on the way back in | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub XML_end | ||
| 248 | { | ||
| 249 | my ($self) = shift; | ||
| 250 | my ($context, $tag, %attrs) = @_; | ||
| 251 | |||
| 252 | if ($tag =~ m/^ul(?:Unicode|CodePage)Range\d$/o) | ||
| 253 | { return hex($context->{'text'}); } | ||
| 254 | elsif ($tag eq 'achVendID') | ||
| 255 | { return unpack('N', $attrs{'name'}); } | ||
| 256 | else | ||
| 257 | { return $self->SUPER::XML_end(@_); } | ||
| 258 | } | ||
| 259 | |||
| 260 | =head2 $t->update | ||
| 261 | |||
| 262 | Updates the OS/2 table by getting information from other sources: | ||
| 263 | |||
| 264 | Updates the C<firstChar> and C<lastChar> values based on the MS table in the | ||
| 265 | cmap. | ||
| 266 | |||
| 267 | Updates the sTypoAscender, sTypoDescender & sTypoLineGap to be the same values | ||
| 268 | as Ascender, Descender and Linegap from the hhea table (assuming it is dirty) | ||
| 269 | and also sets usWinAscent to be the sum of Ascender+Linegap and usWinDescent to | ||
| 270 | be the negative of Descender. | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | sub update | ||
| 275 | { | ||
| 276 | my ($self) = @_; | ||
| 277 | my ($map, @keys, $table); | ||
| 278 | |||
| 279 | return undef unless ($self->SUPER::update); | ||
| 280 | |||
| 281 | $self->{' PARENT'}{'cmap'}->update; | ||
| 282 | $map = $self->{' PARENT'}{'cmap'}->find_ms || return undef; | ||
| 283 | |||
| 284 | @keys = sort {$a <=> $b} keys %{$map->{'val'}}; | ||
| 285 | |||
| 286 | $self->{'usFirstCharIndex'} = $keys[0]; | ||
| 287 | $self->{'usLastCharIndex'} = $keys[-1]; | ||
| 288 | |||
| 289 | $table = $self->{' PARENT'}{'hhea'}->read; | ||
| 290 | |||
| 291 | # try any way we can to get some real numbers passed around! | ||
| 292 | if ($table->{'Ascender'} != 0 || $table->{'Descender'} != 0) | ||
| 293 | { | ||
| 294 | $self->{'sTypoAscender'} = $table->{'Ascender'}; | ||
| 295 | $self->{'sTypoDescender'} = $table->{'Descender'}; | ||
| 296 | $self->{'sTypoLineGap'} = $table->{'Linegap'}; | ||
| 297 | $self->{'usWinAscent'} = $self->{'sTypoAscender'} + $self->{'sTypoLineGap'}; | ||
| 298 | $self->{'usWinDescent'} = -$self->{'sTypoDescender'}; | ||
| 299 | } | ||
| 300 | elsif ($self->{'sTypoAscender'} != 0 || $self->{'sTypoDescender'} != 0) | ||
| 301 | { | ||
| 302 | $table->{'Ascender'} = $self->{'sTypoAscender'}; | ||
| 303 | $table->{'Descender'} = $self->{'sTypoDescender'}; | ||
| 304 | $table->{'Linegap'} = $self->{'sTypoLineGap'}; | ||
| 305 | $self->{'usWinAscent'} = $self->{'sTypoAscender'} + $self->{'sTypoLineGap'}; | ||
| 306 | $self->{'usWinDescent'} = -$self->{'sTypoDescender'}; | ||
| 307 | } | ||
| 308 | elsif ($self->{'usWinAscent'} != 0 || $self->{'usWinDescent'} != 0) | ||
| 309 | { | ||
| 310 | $self->{'sTypoAscender'} = $table->{'Ascender'} = $self->{'usWinAscent'}; | ||
| 311 | $self->{'sTypoDescender'} = $table->{'Descender'} = -$self->{'usWinDescent'}; | ||
| 312 | $self->{'sTypoLineGap'} = $table->{'Linegap'} = 0; | ||
| 313 | } | ||
| 314 | |||
| 315 | $self->{'Version'} = 1 if (defined $self->{'ulCodePageRange1'} && $self->{'Version'} < 1); | ||
| 316 | $self->{'Version'} = 2 if (defined $self->{'maxLookups'} && $self->{'Version'} < 2); | ||
| 317 | |||
| 318 | $self; | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | =head1 BUGS | ||
| 324 | |||
| 325 | None known | ||
| 326 | |||
| 327 | =head1 AUTHOR | ||
| 328 | |||
| 329 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 330 | licensing. | ||
| 331 | |||
| 332 | =cut |