| 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: Glyph.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Glyph; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Glyph - Holds a single glyph's information | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is a single glyph description as held in a TT font. On creation only its | ||
| 36 | header is read. Thus you can get the bounding box of each glyph without having | ||
| 37 | to read all the other information. | ||
| 38 | |||
| 39 | =head1 INSTANCE VARIABLES | ||
| 40 | |||
| 41 | In addition to the named variables in a glyph header (C<xMin> etc.), there are | ||
| 42 | also all capital instance variables for holding working information, mostly | ||
| 43 | from the location table. | ||
| 44 | |||
| 45 | The standard attributes each glyph has are: | ||
| 46 | |||
| 47 | numberOfContours | ||
| 48 | xMin | ||
| 49 | yMin | ||
| 50 | xMax | ||
| 51 | yMax | ||
| 52 | |||
| 53 | There are also other, derived, instance variables for each glyph which are read | ||
| 54 | when the whole glyph is read (via C<read_dat>): | ||
| 55 | |||
| 56 | =over 4 | ||
| 57 | |||
| 58 | =item instLen | ||
| 59 | |||
| 60 | Number of bytes in the hinting instructions (Warning this variable is deprecated, | ||
| 61 | use C<length($g->{'hints'})> instead). | ||
| 62 | |||
| 63 | =item hints | ||
| 64 | |||
| 65 | The string containing the hinting code for the glyph | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | In addition there are other attribute like instance variables for simple glyphs: | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | For each contour there is: | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item endPoints | ||
| 78 | |||
| 79 | An array of endpoints for each contour in the glyph. There are | ||
| 80 | C<numberOfContours> contours in a glyph. The number of points in a glyph is | ||
| 81 | equal to the highest endpoint of a contour. | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | There are also a number of arrays indexed by point number | ||
| 86 | |||
| 87 | =over 4 | ||
| 88 | |||
| 89 | =item flags | ||
| 90 | |||
| 91 | The flags associated with reading this point. The flags for a point are | ||
| 92 | recalculated for a point when it is C<update>d. Thus the flags are not very | ||
| 93 | useful. The only important bit is bit 0 which indicates whether the point is | ||
| 94 | an 'on' curve point, or an 'off' curve point. | ||
| 95 | |||
| 96 | =item x | ||
| 97 | |||
| 98 | The absolute x co-ordinate of the point. | ||
| 99 | |||
| 100 | =item y | ||
| 101 | |||
| 102 | The absolute y co-ordinate of the point | ||
| 103 | |||
| 104 | =back | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | For composite glyphs there are other variables | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item metric | ||
| 113 | |||
| 114 | This holds the component number (not its glyph number) of the component from | ||
| 115 | which the metrics for this glyph should be taken. | ||
| 116 | |||
| 117 | =item comps | ||
| 118 | |||
| 119 | This is an array of hashes for each component. Each hash has a number of | ||
| 120 | elements: | ||
| 121 | |||
| 122 | =over 4 | ||
| 123 | |||
| 124 | =item glyph | ||
| 125 | |||
| 126 | The glyph number of the glyph which comprises this component of the composite. | ||
| 127 | |||
| 128 | =item args | ||
| 129 | |||
| 130 | An array of two arguments which may be an x, y co-ordinate or two attachment | ||
| 131 | points (one on the base glyph the other on the component). See flags for details. | ||
| 132 | |||
| 133 | =item flag | ||
| 134 | |||
| 135 | The flag for this component | ||
| 136 | |||
| 137 | =item scale | ||
| 138 | |||
| 139 | A 4 number array for component scaling. This allows stretching, rotating, etc. | ||
| 140 | Note that scaling applies to placement co-ordinates (rather than attachment points) | ||
| 141 | before locating rather than after. | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =item numPoints | ||
| 146 | |||
| 147 | This is a generated value which contains the number of components read in for this | ||
| 148 | compound glyph. | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | The private instance variables are: | ||
| 153 | |||
| 154 | =over 4 | ||
| 155 | |||
| 156 | =item INFILE (P) | ||
| 157 | |||
| 158 | The input file form which to read any information | ||
| 159 | |||
| 160 | =item LOC (P) | ||
| 161 | |||
| 162 | Location relative to the start of the glyf table in the read file | ||
| 163 | |||
| 164 | =item BASE (P) | ||
| 165 | |||
| 166 | The location of the glyf table in the read file | ||
| 167 | |||
| 168 | =item LEN (P) | ||
| 169 | |||
| 170 | This is the number of bytes required by the glyph. It should be kept up to date | ||
| 171 | by calling the C<update> method whenever any of the glyph content changes. | ||
| 172 | |||
| 173 | =item OUTLOC (P) | ||
| 174 | |||
| 175 | Location relative to the start of the glyf table. This variable is only active | ||
| 176 | whilst the output process is going on. It is used to inform the location table | ||
| 177 | where the glyph's location is, since the glyf table is output before the loca | ||
| 178 | table due to alphabetical ordering. | ||
| 179 | |||
| 180 | =item OUTLEN (P) | ||
| 181 | |||
| 182 | This indicates the length of the glyph data when it is output. This more | ||
| 183 | accurately reflects the internal memory form than the C<LEN> variable which | ||
| 184 | only reflects the read file length. The C<OUTLEN> variable is only set after | ||
| 185 | calling C<out> or C<out_dat>. | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =head2 Editing | ||
| 190 | |||
| 191 | If you want to edit a glyph in some way, then you should read_dat the glyph, then | ||
| 192 | make your changes and then update the glyph or set the $g->{' isdirty'} variable. | ||
| 193 | It is the application's duty to ensure that the following instance variables are | ||
| 194 | correct, from which update will calculate the rest, including the bounding box | ||
| 195 | information. | ||
| 196 | |||
| 197 | numPoints | ||
| 198 | numberOfContours | ||
| 199 | endPoints | ||
| 200 | x, y, flags (only flags bit 0) | ||
| 201 | instLen | ||
| 202 | hints | ||
| 203 | |||
| 204 | For components, the numPoints, x, y, endPoints & flags are not required but | ||
| 205 | the following information is required for each component. | ||
| 206 | |||
| 207 | flag (bits 2, 10, 11, 12) | ||
| 208 | glyph | ||
| 209 | args | ||
| 210 | scale | ||
| 211 | metric (glyph instance variable) | ||
| 212 | |||
| 213 | |||
| 214 | =head1 METHODS | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | use strict; | ||
| 219 | use vars qw(%fields @field_info); | ||
| 220 | use PDF::API3::Compat::API2::Basic::TTF::Utils; | ||
| 221 | use PDF::API3::Compat::API2::Basic::TTF::Table; | ||
| 222 | |||
| 223 | @field_info = ( | ||
| 224 | 'numberOfContours' => 's', | ||
| 225 | 'xMin' => 's', | ||
| 226 | 'yMin' => 's', | ||
| 227 | 'xMax' => 's', | ||
| 228 | 'yMax' => 's'); | ||
| 229 | |||
| 230 | sub init | ||
| 231 | { | ||
| 232 | my ($k, $v, $c, $i); | ||
| 233 | for ($i = 0; $i < $#field_info; $i += 2) | ||
| 234 | { | ||
| 235 | ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]); | ||
| 236 | next unless defined $k && $k ne ""; | ||
| 237 | $fields{$k} = $v; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | =head1 PDF::API3::Compat::API2::Basic::TTF::Glyph->new(%parms) | ||
| 243 | |||
| 244 | Creates a new glyph setting various instance variables | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub new | ||
| 249 | { | ||
| 250 | my ($class, %parms) = @_; | ||
| 251 | my ($self) = {}; | ||
| 252 | my ($p); | ||
| 253 | |||
| 254 | bless $self, $class; | ||
| 255 | foreach $p (keys %parms) | ||
| 256 | { $self->{" $p"} = $parms{$p}; } | ||
| 257 | init unless defined $fields{'xMin'}; | ||
| 258 | $self; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | =head2 $g->read | ||
| 263 | |||
| 264 | Reads the header component of the glyph (bounding box, etc.) and also the | ||
| 265 | glyph content, but into a data field rather than breaking it down into | ||
| 266 | its constituent structures. Use read_dat for this. | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub read | ||
| 271 | { | ||
| 272 | my ($self) = @_; | ||
| 273 | my ($fh) = $self->{' INFILE'}; | ||
| 274 | my ($dat); | ||
| 275 | |||
| 276 | return $self if $self->{' read'}; | ||
| 277 | $self->{' read'} = 1; | ||
| 278 | $fh->seek($self->{' LOC'} + $self->{' BASE'}, 0); | ||
| 279 | $fh->read($self->{' DAT'}, $self->{' LEN'}); | ||
| 280 | TTF_Read_Fields($self, $self->{' DAT'}, \%fields); | ||
| 281 | $self; | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | =head2 $g->read_dat | ||
| 286 | |||
| 287 | Reads the contents of the glyph (components and curves, etc.) from the memory | ||
| 288 | store C<DAT> into structures within the object. Then, to indicate where the | ||
| 289 | master form of the data is, it deletes the C<DAT> instance variable. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub read_dat | ||
| 294 | { | ||
| 295 | my ($self) = @_; | ||
| 296 | my ($dat, $num, $max, $i, $flag, $len, $val, $val1, $fp); | ||
| 297 | |||
| 298 | return $self if $self->{' read'} > 1; | ||
| 299 | $self->read unless $self->{' read'}; | ||
| 300 | $dat = $self->{' DAT'}; | ||
| 301 | $fp = 10; | ||
| 302 | $num = $self->{'numberOfContours'}; | ||
| 303 | if ($num > 0) | ||
| 304 | { | ||
| 305 | $self->{'endPoints'} = [unpack("n*", substr($dat, $fp, $num << 1))]; | ||
| 306 | $fp += $num << 1; | ||
| 307 | $max = 0; | ||
| 308 | foreach (@{$self->{'endPoints'}}) | ||
| 309 | { $max = $_ if $_ > $max; } | ||
| 310 | $max++; | ||
| 311 | $self->{'numPoints'} = $max; | ||
| 312 | $self->{'instLen'} = unpack("n", substr($dat, $fp)); | ||
| 313 | $self->{'hints'} = substr($dat, $fp + 2, $self->{'instLen'}); | ||
| 314 | $fp += 2 + $self->{'instLen'}; | ||
| 315 | # read the flags array | ||
| 316 | for ($i = 0; $i < $max; $i++) | ||
| 317 | { | ||
| 318 | $flag = unpack("C", substr($dat, $fp++)); | ||
| 319 | $self->{'flags'}[$i] = $flag; | ||
| 320 | if ($flag & 8) | ||
| 321 | { | ||
| 322 | $len = unpack("C", substr($dat, $fp++)); | ||
| 323 | while ($len-- > 0) | ||
| 324 | { | ||
| 325 | $i++; | ||
| 326 | $self->{'flags'}[$i] = $flag; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | } | ||
| 330 | #read the x array | ||
| 331 | for ($i = 0; $i < $max; $i++) | ||
| 332 | { | ||
| 333 | $flag = $self->{'flags'}[$i]; | ||
| 334 | if ($flag & 2) | ||
| 335 | { | ||
| 336 | $val = unpack("C", substr($dat, $fp++)); | ||
| 337 | $val = -$val unless ($flag & 16); | ||
| 338 | } elsif ($flag & 16) | ||
| 339 | { $val = 0; } | ||
| 340 | else | ||
| 341 | { | ||
| 342 | $val = TTF_Unpack("s", substr($dat, $fp)); | ||
| 343 | $fp += 2; | ||
| 344 | } | ||
| 345 | $self->{'x'}[$i] = $i == 0 ? $val : $self->{'x'}[$i - 1] + $val; | ||
| 346 | } | ||
| 347 | #read the y array | ||
| 348 | for ($i = 0; $i < $max; $i++) | ||
| 349 | { | ||
| 350 | $flag = $self->{'flags'}[$i]; | ||
| 351 | if ($flag & 4) | ||
| 352 | { | ||
| 353 | $val = unpack("C", substr($dat, $fp++)); | ||
| 354 | $val = -$val unless ($flag & 32); | ||
| 355 | } elsif ($flag & 32) | ||
| 356 | { $val = 0; } | ||
| 357 | else | ||
| 358 | { | ||
| 359 | $val = TTF_Unpack("s", substr($dat, $fp)); | ||
| 360 | $fp += 2; | ||
| 361 | } | ||
| 362 | $self->{'y'}[$i] = $i == 0 ? $val : $self->{'y'}[$i - 1] + $val; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | # compound glyph | ||
| 367 | elsif ($num < 0) | ||
| 368 | { | ||
| 369 | $flag = 1 << 5; # cheat to get the loop going | ||
| 370 | for ($i = 0; $flag & 32; $i++) | ||
| 371 | { | ||
| 372 | ($flag, $self->{'comps'}[$i]{'glyph'}) = unpack("n2", substr($dat, $fp)); | ||
| 373 | $fp += 4; | ||
| 374 | $self->{'comps'}[$i]{'flag'} = $flag; | ||
| 375 | if ($flag & 1) # ARGS1_AND_2_ARE_WORDS | ||
| 376 | { | ||
| 377 | $self->{'comps'}[$i]{'args'} = [TTF_Unpack("s2", substr($dat, $fp))]; | ||
| 378 | $fp += 4; | ||
| 379 | } else | ||
| 380 | { | ||
| 381 | $self->{'comps'}[$i]{'args'} = [unpack("c2", substr($dat, $fp))]; | ||
| 382 | $fp += 2; | ||
| 383 | } | ||
| 384 | |||
| 385 | if ($flag & 8) | ||
| 386 | { | ||
| 387 | $val = TTF_Unpack("F", substr($dat, $fp)); | ||
| 388 | $fp += 2; | ||
| 389 | $self->{'comps'}[$i]{'scale'} = [$val, 0, 0, $val]; | ||
| 390 | } elsif ($flag & 64) | ||
| 391 | { | ||
| 392 | ($val, $val1) = TTF_Unpack("F2", substr($dat, $fp)); | ||
| 393 | $fp += 4; | ||
| 394 | $self->{'comps'}[$i]{'scale'} = [$val, 0, 0, $val1]; | ||
| 395 | } elsif ($flag & 128) | ||
| 396 | { | ||
| 397 | $self->{'comps'}[$i]{'scale'} = [TTF_Unpack("F4", substr($dat, $fp))]; | ||
| 398 | $fp += 8; | ||
| 399 | } | ||
| 400 | $self->{'metric'} = $i if ($flag & 512); | ||
| 401 | } | ||
| 402 | $self->{'numPoints'} = $i; | ||
| 403 | if ($flag & 256) # HAVE_INSTRUCTIONS | ||
| 404 | { | ||
| 405 | $self->{'instLen'} = unpack("n", substr($dat, $fp)); | ||
| 406 | $self->{'hints'} = substr($dat, $fp + 2, $self->{'instLen'}); | ||
| 407 | $fp += 2 + $self->{'instLen'}; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | return undef if ($fp > length($dat)); | ||
| 411 | $self->{' read'} = 2; | ||
| 412 | $self; | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | =head2 $g->out($fh) | ||
| 417 | |||
| 418 | Writes the glyph data to outfile | ||
| 419 | |||
| 420 | =cut | ||
| 421 | |||
| 422 | sub out | ||
| 423 | { | ||
| 424 | my ($self, $fh) = @_; | ||
| 425 | |||
| 426 | $self->read unless $self->{' read'}; | ||
| 427 | $self->update if $self->{' isDirty'}; | ||
| 428 | $fh->print($self->{' DAT'}); | ||
| 429 | $self->{' OUTLEN'} = length($self->{' DAT'}); | ||
| 430 | $self; | ||
| 431 | } | ||
| 432 | |||
| 433 | |||
| 434 | =head2 $g->out_xml($context, $depth) | ||
| 435 | |||
| 436 | Outputs an XML description of the glyph | ||
| 437 | |||
| 438 | =cut | ||
| 439 | |||
| 440 | sub out_xml | ||
| 441 | { | ||
| 442 | my ($self, $context, $depth) = @_; | ||
| 443 | my ($addr) = ($self =~ m/\((.+)\)$/o); | ||
| 444 | my ($k, $ndepth); | ||
| 445 | |||
| 446 | if ($context->{'addresses'}{$addr}) | ||
| 447 | { | ||
| 448 | $context->{'fh'}->printf("%s<glyph gid='%s' id_ref='%s'/>\n", $depth, $context->{'gid'}, $addr); | ||
| 449 | return $self; | ||
| 450 | } | ||
| 451 | else | ||
| 452 | { | ||
| 453 | $context->{'fh'}->printf("%s<glyph gid='%s' id='%s'>\n", $depth, $context->{'gid'}, $addr); | ||
| 454 | } | ||
| 455 | |||
| 456 | $ndepth = $depth . $context->{'indent'}; | ||
| 457 | $self->read_dat; | ||
| 458 | foreach $k (sort grep {$_ !~ m/^\s/o} keys %{$self}) | ||
| 459 | { | ||
| 460 | $self->XML_element($context, $ndepth, $k, $self->{$k}); | ||
| 461 | } | ||
| 462 | $context->{'fh'}->print("$depth</glyph>\n"); | ||
| 463 | delete $context->{'done_points'}; | ||
| 464 | $self; | ||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 468 | sub XML_element | ||
| 469 | { | ||
| 470 | my ($self, $context, $depth, $key, $val) = @_; | ||
| 471 | my ($fh) = $context->{'fh'}; | ||
| 472 | my ($dind) = $depth . $context->{'indent'}; | ||
| 473 | my ($i); | ||
| 474 | |||
| 475 | if ($self->{'numberOfContours'} >= 0 && ($key eq 'x' || $key eq 'y' || $key eq 'flags')) | ||
| 476 | { | ||
| 477 | return $self if ($context->{'done_points'}); | ||
| 478 | $context->{'done_points'} = 1; | ||
| 479 | |||
| 480 | $fh->print("$depth<points>\n"); | ||
| 481 | for ($i = 0; $i <= $#{$self->{'flags'}}; $i++) | ||
| 482 | { $fh->printf("%s<point x='%s' y='%s' flags='0x%02X'/>\n", $dind, | ||
| 483 | $self->{'x'}[$i], $self->{'y'}[$i], $self->{'flags'}[$i]); } | ||
| 484 | $fh->print("$depth</points>\n"); | ||
| 485 | } | ||
| 486 | elsif ($key eq 'hints') | ||
| 487 | { | ||
| 488 | my ($dat); | ||
| 489 | $fh->print("$depth<hints>\n"); | ||
| 490 | # PDF::API3::Compat::API2::Basic::TTF::Utils::XML_hexdump($context, $depth . $context->{'indent'}, $self->{'hints'}); | ||
| 491 | $dat = PDF::API3::Compat::API2::Basic::TTF::Utils::XML_binhint($self->{'hints'}); | ||
| 492 | $dat =~ s/\n(?!$)/\n$depth$context->{'indent'}/mg; | ||
| 493 | $fh->print("$depth$context->{'indent'}$dat"); | ||
| 494 | $fh->print("$depth</hints>\n"); | ||
| 495 | } | ||
| 496 | else | ||
| 497 | { return PDF::API3::Compat::API2::Basic::TTF::Table::XML_element(@_); } | ||
| 498 | |||
| 499 | $self; | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | =head2 $g->update | ||
| 504 | |||
| 505 | Generates a C<$self->{'DAT'}> from the internal structures, if the data has | ||
| 506 | been read into structures in the first place. If you are building a glyph | ||
| 507 | from scratch you will need to set the instance variable C<' read'> to 2 (or | ||
| 508 | something > 1) for the update to work. | ||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | sub update | ||
| 513 | { | ||
| 514 | my ($self) = @_; | ||
| 515 | my ($dat, $loc, $len, $flag, $x, $y, $i, $comp, $num); | ||
| 516 | |||
| 517 | return $self unless (defined $self->{' read'} && $self->{' read'} > 1); | ||
| 518 | $self->update_bbox; | ||
| 519 | $self->{' DAT'} = TTF_Out_Fields($self, \%fields, 10); | ||
| 520 | $num = $self->{'numberOfContours'}; | ||
| 521 | if ($num > 0) | ||
| 522 | { | ||
| 523 | $self->{' DAT'} .= pack("n*", @{$self->{'endPoints'}}); | ||
| 524 | $len = $self->{'instLen'}; | ||
| 525 | $self->{' DAT'} .= pack("n", $len); | ||
| 526 | $self->{' DAT'} .= pack("a" . $len, substr($self->{'hints'}, 0, $len)) if ($len > 0); | ||
| 527 | for ($i = 0; $i < $self->{'numPoints'}; $i++) | ||
| 528 | { | ||
| 529 | $flag = $self->{'flags'}[$i] & 1; | ||
| 530 | if ($i == 0) | ||
| 531 | { | ||
| 532 | $x = $self->{'x'}[$i]; | ||
| 533 | $y = $self->{'y'}[$i]; | ||
| 534 | } else | ||
| 535 | { | ||
| 536 | $x = $self->{'x'}[$i] - $self->{'x'}[$i - 1]; | ||
| 537 | $y = $self->{'y'}[$i] - $self->{'y'}[$i - 1]; | ||
| 538 | } | ||
| 539 | $flag |= 16 if ($x == 0); | ||
| 540 | $flag |= 32 if ($y == 0); | ||
| 541 | if (($flag & 16) == 0 && $x < 256 && $x > -256) | ||
| 542 | { | ||
| 543 | $flag |= 2; | ||
| 544 | $flag |= 16 if ($x >= 0); | ||
| 545 | } | ||
| 546 | if (($flag & 32) == 0 && $y < 256 && $y > -256) | ||
| 547 | { | ||
| 548 | $flag |= 4; | ||
| 549 | $flag |= 32 if ($y >= 0); | ||
| 550 | } | ||
| 551 | $self->{' DAT'} .= pack("C", $flag); # sorry no repeats | ||
| 552 | $self->{'flags'}[$i] = $flag; | ||
| 553 | } | ||
| 554 | for ($i = 0; $i < $self->{'numPoints'}; $i++) | ||
| 555 | { | ||
| 556 | $flag = $self->{'flags'}[$i]; | ||
| 557 | $x = $self->{'x'}[$i] - (($i == 0) ? 0 : $self->{'x'}[$i - 1]); | ||
| 558 | if (($flag & 18) == 0) | ||
| 559 | { $self->{' DAT'} .= TTF_Pack("s", $x); } | ||
| 560 | elsif (($flag & 18) == 18) | ||
| 561 | { $self->{' DAT'} .= pack("C", $x); } | ||
| 562 | elsif (($flag & 18) == 2) | ||
| 563 | { $self->{' DAT'} .= pack("C", -$x); } | ||
| 564 | } | ||
| 565 | for ($i = 0; $i < $self->{'numPoints'}; $i++) | ||
| 566 | { | ||
| 567 | $flag = $self->{'flags'}[$i]; | ||
| 568 | $y = $self->{'y'}[$i] - (($i == 0) ? 0 : $self->{'y'}[$i - 1]); | ||
| 569 | if (($flag & 36) == 0) | ||
| 570 | { $self->{' DAT'} .= TTF_Pack("s", $y); } | ||
| 571 | elsif (($flag & 36) == 36) | ||
| 572 | { $self->{' DAT'} .= pack("C", $y); } | ||
| 573 | elsif (($flag & 36) == 4) | ||
| 574 | { $self->{' DAT'} .= pack("C", -$y); } | ||
| 575 | } | ||
| 576 | } | ||
| 577 | |||
| 578 | elsif ($num < 0) | ||
| 579 | { | ||
| 580 | for ($i = 0; $i <= $#{$self->{'comps'}}; $i++) | ||
| 581 | { | ||
| 582 | $comp = $self->{'comps'}[$i]; | ||
| 583 | $flag = $comp->{'flag'} & 7158; # bits 2,10,11,12 | ||
| 584 | $flag |= 1 unless ($comp->{'args'}[0] > -129 && $comp->{'args'}[0] < 128 | ||
| 585 | && $comp->{'args'}[1] > -129 && $comp->{'args'}[1] < 128); | ||
| 586 | if (defined $comp->{'scale'}) | ||
| 587 | { | ||
| 588 | if ($comp->{'scale'}[1] == 0 && $comp->{'scale'}[2] == 0) | ||
| 589 | { | ||
| 590 | if ($comp->{'scale'}[0] == $comp->{'scale'}[3]) | ||
| 591 | { $flag |= 8 unless ($comp->{'scale'}[0] == 0 | ||
| 592 | || abs(abs($comp->{'scale'}[0]) - 1.) < .001); } | ||
| 593 | else | ||
| 594 | { $flag |= 64; } | ||
| 595 | } else | ||
| 596 | { $flag |= 128; } | ||
| 597 | } | ||
| 598 | |||
| 599 | $flag |= 512 if (defined $self->{'metric'} && $self->{'metric'} == $i); | ||
| 600 | if ($i == $#{$self->{'comps'}}) | ||
| 601 | { $flag |= 256 if (defined $self->{'instLen'} && $self->{'instLen'} > 0); } | ||
| 602 | else | ||
| 603 | { $flag |= 32; } | ||
| 604 | |||
| 605 | $self->{' DAT'} .= pack("n", $flag); | ||
| 606 | $self->{' DAT'} .= pack("n", $comp->{'glyph'}); | ||
| 607 | $comp->{'flag'} = $flag; | ||
| 608 | |||
| 609 | if ($flag & 1) | ||
| 610 | { $self->{' DAT'} .= TTF_Pack("s2", @{$comp->{'args'}}); } | ||
| 611 | else | ||
| 612 | { $self->{' DAT'} .= pack("CC", @{$comp->{'args'}}); } | ||
| 613 | |||
| 614 | if ($flag & 8) | ||
| 615 | { $self->{' DAT'} .= TTF_Pack("F", $comp->{'scale'}[0]); } | ||
| 616 | elsif ($flag & 64) | ||
| 617 | { $self->{' DAT'} .= TTF_Pack("F2", $comp->{'scale'}[0], $comp->{'scale'}[3]); } | ||
| 618 | elsif ($flag & 128) | ||
| 619 | { $self->{' DAT'} .= TTF_Pack("F4", @{$comp->{'scale'}}); } | ||
| 620 | } | ||
| 621 | if (defined $self->{'instLen'} && $self->{'instLen'} > 0) | ||
| 622 | { | ||
| 623 | $len = $self->{'instLen'}; | ||
| 624 | $self->{' DAT'} .= pack("n", $len); | ||
| 625 | $self->{' DAT'} .= pack("a" . $len, substr($self->{'hints'}, 0, $len)); | ||
| 626 | } | ||
| 627 | } | ||
| 628 | $self->{' DAT'} .= "\000" if (length($self->{' DAT'}) & 1); | ||
| 629 | $self->{' OUTLEN'} = length($self->{' DAT'}); | ||
| 630 | $self->{' read'} = 2; # changed from 1 to 2 so we don't read_dat() again | ||
| 631 | # we leave numPoints and instLen since maxp stats use this | ||
| 632 | $self; | ||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | =head2 $g->update_bbox | ||
| 637 | |||
| 638 | Updates the bounding box for this glyph according to the points in the glyph | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | sub update_bbox | ||
| 643 | { | ||
| 644 | my ($self) = @_; | ||
| 645 | my ($num, $maxx, $minx, $maxy, $miny, $i, $comp, $x, $y, $compg); | ||
| 646 | |||
| 647 | return $self unless $self->{' read'} > 1; # only if read_dat done | ||
| 648 | $miny = $minx = 65537; $maxx = $maxy = -65537; | ||
| 649 | $num = $self->{'numberOfContours'}; | ||
| 650 | if ($num > 0) | ||
| 651 | { | ||
| 652 | for ($i = 0; $i < $self->{'numPoints'}; $i++) | ||
| 653 | { | ||
| 654 | ($x, $y) = ($self->{'x'}[$i], $self->{'y'}[$i]); | ||
| 655 | |||
| 656 | $maxx = $x if ($x > $maxx); | ||
| 657 | $minx = $x if ($x < $minx); | ||
| 658 | $maxy = $y if ($y > $maxy); | ||
| 659 | $miny = $y if ($y < $miny); | ||
| 660 | } | ||
| 661 | } | ||
| 662 | |||
| 663 | elsif ($num < 0) | ||
| 664 | { | ||
| 665 | foreach $comp (@{$self->{'comps'}}) | ||
| 666 | { | ||
| 667 | my ($gnx, $gny, $gxx, $gxy); | ||
| 668 | my ($sxx, $sxy, $syx, $syy); | ||
| 669 | |||
| 670 | next unless(defined $self->{' PARENT'}{'loca'}{'glyphs'}[$comp->{'glyph'}]); | ||
| 671 | $compg = $self->{' PARENT'}{'loca'}{'glyphs'}[$comp->{'glyph'}]->read->update_bbox; | ||
| 672 | ($gnx, $gny, $gxx, $gxy) = @{$compg}{'xMin', 'yMin', 'xMax', 'yMax'}; | ||
| 673 | if (defined $comp->{'scale'}) | ||
| 674 | { | ||
| 675 | ($sxx, $sxy, $syx, $syy) = @{$comp->{'scale'}}; | ||
| 676 | ($gnx, $gny, $gxx, $gxy) = ($gnx*$sxx+$gny*$syx + $comp->{'args'}[0], | ||
| 677 | $gnx*$sxy+$gny*$syy + $comp->{'args'}[1], | ||
| 678 | $gxx*$sxx+$gxy*$syx + $comp->{'args'}[0], | ||
| 679 | $gxx*$sxy+$gxy*$syy + $comp->{'args'}[1]); | ||
| 680 | } elsif ($comp->{'args'}[0] || $comp->{'args'}[1]) | ||
| 681 | { | ||
| 682 | $gnx += $comp->{'args'}[0]; | ||
| 683 | $gny += $comp->{'args'}[1]; | ||
| 684 | $gxx += $comp->{'args'}[0]; | ||
| 685 | $gxy += $comp->{'args'}[1]; | ||
| 686 | } | ||
| 687 | $maxx = $gxx if $gxx > $maxx; | ||
| 688 | $minx = $gnx if $gnx < $minx; | ||
| 689 | $maxy = $gxy if $gxy > $maxy; | ||
| 690 | $miny = $gny if $gny < $miny; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | $self->{'xMax'} = $maxx; | ||
| 694 | $self->{'xMin'} = $minx; | ||
| 695 | $self->{'yMax'} = $maxy; | ||
| 696 | $self->{'yMin'} = $miny; | ||
| 697 | $self; | ||
| 698 | } | ||
| 699 | |||
| 700 | |||
| 701 | =head2 $g->maxInfo | ||
| 702 | |||
| 703 | Returns lots of information about a glyph so that the C<maxp> table can update | ||
| 704 | itself. | ||
| 705 | |||
| 706 | =cut | ||
| 707 | |||
| 708 | sub maxInfo | ||
| 709 | { | ||
| 710 | my ($self) = @_; | ||
| 711 | my (@res, $i, @n); | ||
| 712 | |||
| 713 | $self->read_dat; # make sure we've read some data | ||
| 714 | $res[4] = length($self->{'hints'}) if defined $self->{'hints'}; | ||
| 715 | if ($self->{'numberOfContours'} > 0) | ||
| 716 | { | ||
| 717 | $res[2] = $res[0] = $self->{'numPoints'}; | ||
| 718 | $res[3] = $res[1] = $self->{'numberOfContours'}; | ||
| 719 | $res[6] = 1; | ||
| 720 | } elsif ($self->{'numberOfContours'} < 0) | ||
| 721 | { | ||
| 722 | $res[6] = 1; | ||
| 723 | for ($i = 0; $i <= $#{$self->{'comps'}}; $i++) | ||
| 724 | { | ||
| 725 | @n = $self->{' PARENT'}{'loca'}{'glyphs'}[$self->{'comps'}[$i]{'glyph'}]->maxInfo; | ||
| 726 | $res[2] += $n[2] == 0 ? $n[0] : $n[2]; | ||
| 727 | $res[3] += $n[3] == 0 ? $n[1] : $n[3]; | ||
| 728 | $res[5]++; | ||
| 729 | $res[6] = $n[6] + 1 if ($n[6] >= $res[6]); | ||
| 730 | } | ||
| 731 | } | ||
| 732 | @res; | ||
| 733 | } | ||
| 734 | |||
| 735 | =head2 $g->empty | ||
| 736 | |||
| 737 | Empties the glyph of all information to the level of not having been read. | ||
| 738 | Useful for saving memory in apps with many glyphs being read | ||
| 739 | |||
| 740 | =cut | ||
| 741 | |||
| 742 | sub empty | ||
| 743 | { | ||
| 744 | my ($self) = @_; | ||
| 745 | my (%keep) = map {(" $_" => 1)} ('LOC', 'OUTLOC', 'PARENT', 'INFILE', 'BASE', | ||
| 746 | 'OUTLEN', 'LEN'); | ||
| 747 | map {delete $self->{$_} unless $keep{$_}} keys %$self; | ||
| 748 | |||
| 749 | $self; | ||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | =head2 $g->get_points | ||
| 754 | |||
| 755 | This method creates point information for a compound glyph. The information is | ||
| 756 | stored in the same place as if the glyph was not a compound, but since | ||
| 757 | numberOfContours is negative, the glyph is still marked as being a compound | ||
| 758 | |||
| 759 | =cut | ||
| 760 | |||
| 761 | sub get_points | ||
| 762 | { | ||
| 763 | my ($self) = @_; | ||
| 764 | my ($comp, $compg, $nump, $e, $i); | ||
| 765 | |||
| 766 | $self->read_dat; | ||
| 767 | return undef unless ($self->{'numberOfContours'} < 0); | ||
| 768 | |||
| 769 | foreach $comp (@{$self->{'comps'}}) | ||
| 770 | { | ||
| 771 | $compg = $self->{' PARENT'}{'loca'}{'glyphs'}[$comp->{'glyph'}]->read; | ||
| 772 | $compg->get_points; | ||
| 773 | |||
| 774 | for ($i = 0; $i < $compg->{'numPoints'}; $i++) | ||
| 775 | { | ||
| 776 | my ($x, $y) = ($compg->{'x'}[$i], $compg->{'y'}[$i]); | ||
| 777 | if (defined $comp->{'scale'}) | ||
| 778 | { | ||
| 779 | ($x, $y) = ($x * $comp->{'scale'}[0] + $y * $comp->{'scale'}[2], | ||
| 780 | $x * $comp->{'scale'}[1] + $y * $comp->{'scale'}[3]); | ||
| 781 | } | ||
| 782 | if (defined $comp->{'args'}) | ||
| 783 | { ($x, $y) = ($x + $comp->{'args'}[0], $y + $comp->{'args'}[1]); } | ||
| 784 | push (@{$self->{'x'}}, $x); | ||
| 785 | push (@{$self->{'y'}}, $y); | ||
| 786 | } | ||
| 787 | foreach $e (@{$compg->{'endPoints'}}) | ||
| 788 | { push (@{$self->{'endPoints'}}, $e + $nump); } | ||
| 789 | $nump += $compg->{'numPoints'}; | ||
| 790 | } | ||
| 791 | $self->{'numPoints'} = $nump; | ||
| 792 | $self; | ||
| 793 | } | ||
| 794 | |||
| 795 | |||
| 796 | =head2 $g->get_refs | ||
| 797 | |||
| 798 | Returns an array of all the glyph ids that are used to make up this glyph. That | ||
| 799 | is all the compounds and their references and so on. If this glyph is not a | ||
| 800 | compound, then returns an empty array | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | sub get_refs | ||
| 805 | { | ||
| 806 | my ($self) = @_; | ||
| 807 | my (@res, $g); | ||
| 808 | |||
| 809 | $self->read_dat; | ||
| 810 | return unless ($self->{'numberOfContours'} < 0); | ||
| 811 | foreach $g (@{$self->{'comps'}}) | ||
| 812 | { | ||
| 813 | next unless(defined $self->{' PARENT'}{'loca'}{'glyphs'}[$g->{'glyph'}]); | ||
| 814 | my (@list) = $self->{' PARENT'}{'loca'}{'glyphs'}[$g->{'glyph'}]->get_points; | ||
| 815 | push (@res, $g->{'glyph'}); | ||
| 816 | push (@res, @list) if ($list[0]); | ||
| 817 | } | ||
| 818 | return @res; | ||
| 819 | } | ||
| 820 | |||
| 821 | 1; | ||
| 822 | |||
| 823 | =head1 BUGS | ||
| 824 | |||
| 825 | =over 4 | ||
| 826 | |||
| 827 | =item * | ||
| 828 | |||
| 829 | The instance variables used here are somewhat clunky and inconsistent with | ||
| 830 | the other tables. | ||
| 831 | |||
| 832 | =item * | ||
| 833 | |||
| 834 | C<update> doesn't re-calculate the bounding box or C<numberOfContours>. | ||
| 835 | |||
| 836 | =back | ||
| 837 | |||
| 838 | =head1 AUTHOR | ||
| 839 | |||
| 840 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 841 | licensing. | ||
| 842 | |||
| 843 | =cut |