| 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: Loca.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $ | ||
| 25 | # | ||
| 26 | #======================================================================= | ||
| 27 | package PDF::API3::Compat::API2::Basic::TTF::Loca; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | PDF::API3::Compat::API2::Basic::TTF::Loca - the Locations table, which is intimately tied to the glyf table | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | The location table holds the directory of locations of each glyph within the | ||
| 36 | glyf table. Due to this relationship and the unimportance of the actual locations | ||
| 37 | when it comes to holding glyphs in memory, reading the location table results | ||
| 38 | in the creation of glyph objects for each glyph and stores them here. | ||
| 39 | So if you are looking for glyphs, don't look in the C<glyf> table, look here | ||
| 40 | instead. | ||
| 41 | |||
| 42 | Things get complicated if you try to change the glyph list within the one table. | ||
| 43 | The recommendation is to create another clean location object to replace this | ||
| 44 | table in the font, ensuring that the old table is read first and to transfer | ||
| 45 | or copy glyphs across from the read table to the new table. | ||
| 46 | |||
| 47 | =head1 INSTANCE VARIABLES | ||
| 48 | |||
| 49 | The instance variables do not start with a space | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item glyphs | ||
| 54 | |||
| 55 | An array of glyph objects for each glyph. | ||
| 56 | |||
| 57 | =item glyphtype | ||
| 58 | |||
| 59 | A string containing the class name to create for each new glyph. If empty, | ||
| 60 | defaults to L<PDF::API3::Compat::API2::Basic::TTF::Glyph>. | ||
| 61 | |||
| 62 | =back | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | use strict; | ||
| 69 | use vars qw(@ISA); | ||
| 70 | @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table); | ||
| 71 | |||
| 72 | require PDF::API3::Compat::API2::Basic::TTF::Glyph; | ||
| 73 | |||
| 74 | |||
| 75 | =head2 $t->new | ||
| 76 | |||
| 77 | Creates a new location table making sure it has a glyphs array | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub new | ||
| 82 | { | ||
| 83 | my ($class) = shift; | ||
| 84 | my ($res) = $class->SUPER::new(@_); | ||
| 85 | $res->{'glyphs'} = []; | ||
| 86 | $res; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 $t->read | ||
| 90 | |||
| 91 | Reads the location table creating glyph objects (L<PDF::API3::Compat::API2::Basic::TTF::Glyph>) for each glyph | ||
| 92 | allowing their later reading. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub read | ||
| 97 | { | ||
| 98 | my ($self) = @_; | ||
| 99 | my ($fh) = $self->{' INFILE'}; | ||
| 100 | my ($locFmt) = $self->{' PARENT'}{'head'}->read->{'indexToLocFormat'}; | ||
| 101 | my ($numGlyphs) = $self->{' PARENT'}{'maxp'}->read->{'numGlyphs'}; | ||
| 102 | my ($glyfLoc) = $self->{' PARENT'}{'glyf'}{' OFFSET'}; | ||
| 103 | my ($dat, $last, $i, $loc); | ||
| 104 | |||
| 105 | $self->SUPER::read or return $self; | ||
| 106 | $fh->read($dat, $locFmt ? 4 : 2); | ||
| 107 | $last = unpack($locFmt ? "N" : "n", $dat); | ||
| 108 | for ($i = 0; $i < $numGlyphs; $i++) | ||
| 109 | { | ||
| 110 | $fh->read($dat, $locFmt ? 4 : 2); | ||
| 111 | $loc = unpack($locFmt ? "N" : "n", $dat); | ||
| 112 | $self->{'glyphs'}[$i] = ($self->{'glyphtype'} || "PDF::API3::Compat::API2::Basic::TTF::Glyph")->new( | ||
| 113 | LOC => $last << ($locFmt ? 0 : 1), | ||
| 114 | OUTLOC => $last << ($locFmt ? 0 : 1), | ||
| 115 | PARENT => $self->{' PARENT'}, | ||
| 116 | INFILE => $fh, | ||
| 117 | BASE => $glyfLoc, | ||
| 118 | OUTLEN => ($loc - $last) << ($locFmt ? 0 : 1), | ||
| 119 | LEN => ($loc - $last) << ($locFmt ? 0 : 1)) if ($loc != $last); | ||
| 120 | $last = $loc; | ||
| 121 | } | ||
| 122 | $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | =head2 $t->out($fh) | ||
| 127 | |||
| 128 | Writes the location table out to $fh. Notice that not having read the location | ||
| 129 | table implies that the glyf table has not been read either, so the numbers in | ||
| 130 | the location table are still valid. Let's hope that C<maxp/numGlyphs> and | ||
| 131 | C<head/indexToLocFmt> haven't changed otherwise we are in big trouble. | ||
| 132 | |||
| 133 | The function uses the OUTLOC location in the glyph calculated when the glyf | ||
| 134 | table was attempted to be output. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub out | ||
| 139 | { | ||
| 140 | my ($self, $fh) = @_; | ||
| 141 | my ($locFmt) = $self->{' PARENT'}{'head'}->read->{'indexToLocFormat'}; | ||
| 142 | my ($numGlyphs) = $self->{' PARENT'}{'maxp'}->read->{'numGlyphs'}; | ||
| 143 | my ($count, $i, $offset, $g); | ||
| 144 | |||
| 145 | return $self->SUPER::out($fh) unless ($self->{' read'}); | ||
| 146 | |||
| 147 | $count = 0; | ||
| 148 | for ($i = 0; $i < $numGlyphs; $i++) | ||
| 149 | { | ||
| 150 | $g = ($self->{'glyphs'}[$i]) || ""; | ||
| 151 | unless ($g) | ||
| 152 | { | ||
| 153 | $count++; | ||
| 154 | next; | ||
| 155 | } else | ||
| 156 | { | ||
| 157 | if ($locFmt) | ||
| 158 | { $fh->print(pack("N", $g->{' OUTLOC'}) x ($count + 1)); } | ||
| 159 | else | ||
| 160 | { $fh->print(pack("n", $g->{' OUTLOC'} >> 1) x ($count + 1)); } | ||
| 161 | $count = 0; | ||
| 162 | $offset = $g->{' OUTLOC'} + $g->{' OUTLEN'}; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | $fh->print(pack($locFmt ? "N" : "n", ($locFmt ? $offset: $offset >> 1)) x ($count + 1)); | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | =head2 $t->out_xml($context, $depth) | ||
| 170 | |||
| 171 | No need to output a loca table, this is dynamically generated | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub out_xml | ||
| 176 | { return $_[0]; } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 $t->glyphs_do(&func) | ||
| 180 | |||
| 181 | Calls func for each glyph in this location table in numerical order: | ||
| 182 | |||
| 183 | &func($glyph, $glyph_num) | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub glyphs_do | ||
| 188 | { | ||
| 189 | my ($self, $func) = @_; | ||
| 190 | my ($i); | ||
| 191 | |||
| 192 | for ($i = 0; $i <= $#{$self->{'glyphs'}}; $i++) | ||
| 193 | { &$func($self->{'glyphs'}[$i], $i) if defined $self->{'glyphs'}[$i]; } | ||
| 194 | $self; | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | =head1 BUGS | ||
| 200 | |||
| 201 | None known | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and | ||
| 206 | licensing. | ||
| 207 | |||
| 208 | =cut | ||
| 209 |