Introduce src dir.
[librarian.git] / src / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / Glyf.pm
1 package Font::TTF::Glyf;
2
3 =head1 NAME
4
5 Font::TTF::Glyf - The Glyf data table
6
7 =head1 DESCRIPTION
8
9 This is a stub table. The real data is held in the loca table. If you want to get a glyf
10 look it up in the loca table as C<$f->{'loca'}{'glyphs'}[$num]>. It won't be here!
11
12 The difference between reading this table as opposed to the loca table is that
13 reading this table will cause updated glyphs to be written out rather than just
14 copying the glyph information from the input file. This causes font writing to be
15 slower. So read the glyf as opposed to the loca table if you want to change glyf
16 data. Read the loca table only if you are just wanting to read the glyf information.
17
18 This class is used when writing the glyphs though.
19
20 =head1 METHODS
21
22 =cut
23
24
25 use strict;
26 use vars qw(@ISA);
27 @ISA = qw(Font::TTF::Table);
28
29 =head2 $t->read
30
31 Reads the C<loca> table instead!
32
33 =cut
34
35 sub read
36 {
37     my ($self) = @_;
38     
39     $self->{' PARENT'}{'loca'}->read;
40     $self->{' read'} = 1;
41     $self;
42 }
43
44
45 =head2 $t->out($fh)
46
47 Writes out all the glyphs in the parent's location table, calculating a new
48 output location for each one.
49
50 =cut
51
52 sub out
53 {
54     my ($self, $fh) = @_;
55     my ($i, $loca, $offset, $numGlyphs);
56
57     return $self->SUPER::out($fh) unless $self->{' read'};
58
59     $loca = $self->{' PARENT'}{'loca'}{'glyphs'};
60     $numGlyphs = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
61
62     $offset = 0;
63     for ($i = 0; $i < $numGlyphs; $i++)
64     {
65         next unless defined $loca->[$i];
66         $loca->[$i]->update;
67         $loca->[$i]{' OUTLOC'} = $offset;
68         $loca->[$i]->out($fh);
69         $offset += $loca->[$i]{' OUTLEN'};
70     }
71     $self->{' PARENT'}{'head'}{'indexToLocFormat'} = ($offset >= 0x20000);
72     $self;
73 }
74
75
76 =head2 $t->out_xml($context, $depth)
77
78 Outputs all the glyphs in the glyph table just where they are supposed to be output!
79
80 =cut
81
82 sub out_xml
83 {
84     my ($self, $context, $depth) = @_;
85     my ($fh) = $context->{'fh'};
86     my ($loca, $i, $numGlyphs);
87
88     $loca = $self->{' PARENT'}{'loca'}{'glyphs'};
89     $numGlyphs = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
90     
91     for ($i = 0; $i < $numGlyphs; $i++)
92     {
93         $context->{'gid'} = $i;
94         $loca->[$i]->out_xml($context, $depth) if (defined $loca->[$i]);
95     }
96
97     $self;
98 }
99
100
101 =head2 $t->XML_start($context, $tag, %attrs)
102
103 Pass control to glyphs as they occur
104
105 =cut
106
107 sub XML_start
108 {
109     my ($self) = shift;
110     my ($context, $tag, %attrs) = @_;
111
112     if ($tag eq 'glyph')
113     {
114         $context->{'tree'}[-1] = Font::TTF::Glyph->new(read => 2, PARENT => $self->{' PARENT'});
115         $context->{'receiver'} = $context->{'tree'}[-1];
116     }
117 }
118
119
120 =head2 $t->XML_end($context, $tag, %attrs)
121
122 Collect up glyphs and put them into the loca table
123
124 =cut
125
126 sub XML_end
127 {
128     my ($self) = shift;
129     my ($context, $tag, %attrs) = @_;
130
131     if ($tag eq 'glyph')
132     {
133         unless (defined $context->{'glyphs'})
134         {
135             if (defined $self->{' PARENT'}{'loca'})
136             { $context->{'glyphs'} = $self->{' PARENT'}{'loca'}{'glyphs'}; }
137             else
138             { $context->{'glyphs'} = []; }
139         }
140         $context->{'glyphs'}[$attrs{'gid'}] = $context->{'tree'}[-1];
141         return $context;
142     } else
143     { return $self->SUPER::XML_end(@_); }
144 }
145
146 1;
147
148 =head1 BUGS
149
150 None known
151
152 =head1 AUTHOR
153
154 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and
155 licensing.
156
157 =cut
158