| 1 | package Font::TTF::Glat; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Font::TTF::Glat - Hold glyph attributes | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Holds glyph attributes associated with each glyph. | ||
| 10 | |||
| 11 | =over 4 | ||
| 12 | |||
| 13 | =item Version | ||
| 14 | |||
| 15 | Table format version | ||
| 16 | |||
| 17 | =item attribs | ||
| 18 | |||
| 19 | An array of hashes. On array entry for each glyph id. Since the glyph attributes are usually in a sparse | ||
| 20 | array, they are stored in a hash keyed by the attribute id and with the value as attribute value. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use Font::TTF::Table; | ||
| 25 | use Font::TTF::Utils; | ||
| 26 | use strict; | ||
| 27 | use vars qw(@ISA); | ||
| 28 | @ISA = qw(Font::TTF::Table); | ||
| 29 | |||
| 30 | sub read | ||
| 31 | { | ||
| 32 | my ($self) = @_; | ||
| 33 | $self->SUPER::read or return $self; | ||
| 34 | |||
| 35 | my ($gloc) = $self->{' PARENT'}{'Gloc'}; | ||
| 36 | my ($fh) = $self->{' INFILE'}; | ||
| 37 | my ($numGlyphs); | ||
| 38 | my ($base) = $self->{' OFFSET'}; | ||
| 39 | my ($dat, $i); | ||
| 40 | |||
| 41 | $gloc->read; | ||
| 42 | $numGlyphs = $gloc->{'numGlyphs'}; | ||
| 43 | $fh->seek($base, 0); | ||
| 44 | $fh->read($dat, 4); | ||
| 45 | ($self->{'Version'}) = TTF_Unpack('v', $dat); | ||
| 46 | |||
| 47 | for ($i = 0; $i < $numGlyphs; $i++) | ||
| 48 | { | ||
| 49 | my ($j) = 0; | ||
| 50 | my ($num) = $gloc->{'locations'}[$i + 1] - $gloc->{'locations'}[$i]; | ||
| 51 | my ($first, $number, @vals); | ||
| 52 | $fh->seek($base + $gloc->{'locations'}[$i], 0); | ||
| 53 | $fh->read($dat, $num); | ||
| 54 | while ($j < $num) | ||
| 55 | { | ||
| 56 | if ($self->{'Version'} > 1) | ||
| 57 | { | ||
| 58 | ($first, $number) = unpack("n2", substr($dat, $j, 4)); | ||
| 59 | @vals = unpack("n$number", substr($dat, $j + 4, $number * 2)); | ||
| 60 | $j += ($number + 2) * 2; | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | ($first, $number) = unpack("C2", substr($dat, $j, 2)); | ||
| 65 | @vals = unpack("n$number", substr($dat, $j + 2, $number * 2)); | ||
| 66 | $j += $number * 2 + 2; | ||
| 67 | } | ||
| 68 | for (my $k = 0; $k < $number; $k++) | ||
| 69 | { $self->{'attribs'}[$i]{$first + $k} = $vals[$k]; } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub out | ||
| 75 | { | ||
| 76 | my ($self, $fh) = @_; | ||
| 77 | my ($gloc) = $self->{' PARENT'}{'Gloc'}; | ||
| 78 | my ($numGlyphs) = 0; | ||
| 79 | my ($base) = $fh->tell(); | ||
| 80 | my ($i, $type); | ||
| 81 | |||
| 82 | return $self->SUPER::out($fh) unless ($self->{' read'}); | ||
| 83 | $numGlyphs = scalar @{$self->{'attribs'}}; | ||
| 84 | if ($gloc->{'numAttrib'} > 256) | ||
| 85 | { | ||
| 86 | $self->{'Version'} = 2; | ||
| 87 | $type = "n"; | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | $self->{'Version'} = 1; | ||
| 92 | $type = "C"; | ||
| 93 | } | ||
| 94 | |||
| 95 | $gloc->{'locations'} = []; | ||
| 96 | $fh->print(TTF_Pack('v', $self->{'Version'})); | ||
| 97 | for ($i = 0; $i < $numGlyphs; $i++) | ||
| 98 | { | ||
| 99 | my (@a) = sort {$a <=> $b} keys %{$self->{'attribs'}[$i]}; | ||
| 100 | push(@{$gloc->{'locations'}}, $fh->tell() - $base); | ||
| 101 | while (@a) | ||
| 102 | { | ||
| 103 | my ($first) = shift(@a); | ||
| 104 | my ($next) = $first; | ||
| 105 | my (@v, $j); | ||
| 106 | while (@a and $a[0] <= $next + 2) | ||
| 107 | { $next = shift(@a); } | ||
| 108 | for ($j = $first; $j <= $next; $j++) | ||
| 109 | { push (@v, $self->{'attribs'}[$i]{$j}); } | ||
| 110 | { $fh->print(pack("${type}2n*", $first, $next - $first + 1, @v)); } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | push(@{$gloc->{'locations'}}, $fh->tell() - $base); | ||
| 114 | } | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head2 $t->minsize() | ||
| 119 | |||
| 120 | Returns the minimum size this table can be. If it is smaller than this, then the table | ||
| 121 | must be bad and should be deleted or whatever. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub minsize | ||
| 126 | { | ||
| 127 | return 4; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Martin Hosken L<http://scripts.sil.org/FontUtils>. | ||
| 135 | |||
| 136 | |||
| 137 | =head1 LICENSING | ||
| 138 | |||
| 139 | Copyright (c) 1998-2016, SIL International (http://www.sil.org) | ||
| 140 | |||
| 141 | This module is released under the terms of the Artistic License 2.0. | ||
| 142 | For details, see the full text of the license in the file LICENSE. | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | =cut |