1 package Font::TTF::Loca;
5 Font::TTF::Loca - the Locations table, which is intimately tied to the glyf table
9 The location table holds the directory of locations of each glyph within the
10 glyf table. Due to this relationship and the unimportance of the actual locations
11 when it comes to holding glyphs in memory, reading the location table results
12 in the creation of glyph objects for each glyph and stores them here.
13 So if you are looking for glyphs, don't look in the C<glyf> table, look here
16 Things get complicated if you try to change the glyph list within the one table.
17 The recommendation is to create another clean location object to replace this
18 table in the font, ensuring that the old table is read first and to transfer
19 or copy glyphs across from the read table to the new table.
21 =head1 INSTANCE VARIABLES
23 The instance variables do not start with a space
29 An array of glyph objects for each glyph.
33 A string containing the class name to create for each new glyph. If empty,
34 defaults to L<Font::TTF::Glyph>.
44 @ISA = qw(Font::TTF::Table);
46 require Font::TTF::Glyph;
51 Creates a new location table making sure it has a glyphs array
58 my ($res) = $class->SUPER::new(@_);
59 $res->{'glyphs'} = [];
65 Reads the location table creating glyph objects (L<Font::TTF::Glyph>) for each glyph
66 allowing their later reading.
73 my ($fh) = $self->{' INFILE'};
74 my ($locFmt) = $self->{' PARENT'}{'head'}{'indexToLocFormat'};
75 my ($numGlyphs) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
76 my ($glyfLoc) = $self->{' PARENT'}{'glyf'}{' OFFSET'};
77 my ($dat, $last, $i, $loc);
79 $self->SUPER::read or return $self;
80 $fh->read($dat, $locFmt ? 4 : 2);
81 $last = unpack($locFmt ? "N" : "n", $dat);
82 for ($i = 0; $i < $numGlyphs; $i++)
84 $fh->read($dat, $locFmt ? 4 : 2);
85 $loc = unpack($locFmt ? "N" : "n", $dat);
86 $self->{'glyphs'}[$i] = ($self->{'glyphtype'} || "Font::TTF::Glyph")->new(
87 LOC => $last << ($locFmt ? 0 : 1),
88 OUTLOC => $last << ($locFmt ? 0 : 1),
89 PARENT => $self->{' PARENT'},
92 OUTLEN => ($loc - $last) << ($locFmt ? 0 : 1),
93 LEN => ($loc - $last) << ($locFmt ? 0 : 1)) if ($loc != $last);
102 Writes the location table out to $fh. Notice that not having read the location
103 table implies that the glyf table has not been read either, so the numbers in
104 the location table are still valid. Let's hope that C<maxp/numGlyphs> and
105 C<head/indexToLocFmt> haven't changed otherwise we are in big trouble.
107 The function uses the OUTLOC location in the glyph calculated when the glyf
108 table was attempted to be output.
114 my ($self, $fh) = @_;
115 my ($locFmt) = $self->{' PARENT'}{'head'}{'indexToLocFormat'};
116 my ($numGlyphs) = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
117 my ($count, $i, $offset, $g);
119 return $self->SUPER::out($fh) unless ($self->{' read'});
122 for ($i = 0; $i < $numGlyphs; $i++)
124 $g = ($self->{'glyphs'}[$i]) || "";
132 { $fh->print(pack("N", $g->{' OUTLOC'}) x ($count + 1)); }
134 { $fh->print(pack("n", $g->{' OUTLOC'} >> 1) x ($count + 1)); }
136 $offset = $g->{' OUTLOC'} + $g->{' OUTLEN'};
139 $fh->print(pack($locFmt ? "N" : "n", ($locFmt ? $offset: $offset >> 1)) x ($count + 1));
143 =head2 $t->out_xml($context, $depth)
145 No need to output a loca table, this is dynamically generated
153 =head2 $t->glyphs_do(&func)
155 Calls func for each glyph in this location table in numerical order:
157 &func($glyph, $glyph_num)
163 my ($self, $func) = @_;
166 for ($i = 0; $i <= $#{$self->{'glyphs'}}; $i++)
167 { &$func($self->{'glyphs'}[$i], $i) if defined $self->{'glyphs'}[$i]; }
179 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and