Version bump.
[librarian.git] / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / Hhea.pm
1 package Font::TTF::Hhea;
2
3 =head1 NAME
4
5 Font::TTF::Hhea - Horizontal Header table
6
7 =head1 DESCRIPTION
8
9 This is a simplte table with just standards specified instance variables
10
11 =head1 INSTANCE VARIABLES
12
13     version
14     Ascender
15     Descender
16     LineGap
17     advanceWidthMax
18     minLeftSideBearing
19     minRightSideBearing
20     xMaxExtent
21     caretSlopeRise
22     caretSlopeRun
23     metricDataFormat
24     numberOfHMetrics
25
26
27 =head1 METHODS
28
29 =cut
30
31 use strict;
32 use vars qw(@ISA %fields @field_info);
33
34 require Font::TTF::Table;
35 use Font::TTF::Utils;
36
37 @ISA = qw(Font::TTF::Table);
38 @field_info = (
39     'version' => 'v',
40     'Ascender' => 's',
41     'Descender' => 's',
42     'LineGap' => 's',
43     'advanceWidthMax' => 'S',
44     'minLeftSideBearing' => 's',
45     'minRightSideBearing' => 's',
46     'xMaxExtent' => 's',
47     'caretSlopeRise' => 's',
48     'caretSlopeRun' => 's',
49     'metricDataFormat' => '+10s',
50     'numberOfHMetrics' => 'S');
51
52 sub init
53 {
54     my ($k, $v, $c, $i);
55     for ($i = 0; $i < $#field_info; $i += 2)
56     {
57         ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]);
58         next unless defined $k && $k ne "";
59         $fields{$k} = $v;
60     }
61 }
62
63
64 =head2 $t->read
65
66 Reads the table into memory as instance variables
67
68 =cut
69
70 sub read
71 {
72     my ($self) = @_;
73     my ($dat);
74
75     $self->SUPER::read or return $self;
76     init unless defined $fields{'Ascender'};
77     $self->{' INFILE'}->read($dat, 36);
78
79     TTF_Read_Fields($self, $dat, \%fields);
80     $self;
81 }
82
83
84 =head2 $t->out($fh)
85
86 Writes the table to a file either from memory or by copying.
87
88 =cut
89
90 sub out
91 {
92     my ($self, $fh) = @_;
93
94     return $self->SUPER::out($fh) unless $self->{' read'};
95
96     $self->{'numberOfHMetrics'} = $self->{' PARENT'}{'hmtx'}->numMetrics || $self->{'numberOfHMetrics'};
97     $fh->print(TTF_Out_Fields($self, \%fields, 36));
98     $self;
99 }
100
101
102 =head2 $t->update
103
104 Updates various parameters in the hhea table from the hmtx table.
105
106 =cut
107
108 sub update
109 {
110     my ($self) = @_;
111     my ($hmtx) = $self->{' PARENT'}{'hmtx'};
112     my ($glyphs);
113     my ($num, $res);
114     my ($i, $maw, $mlsb, $mrsb, $mext, $aw, $lsb, $ext);
115
116     return undef unless ($self->SUPER::update);
117     return undef unless (defined $hmtx && defined $self->{' PARENT'}{'loca'});
118
119     $hmtx->read->update;
120     $self->{' PARENT'}{'loca'}->read->update;
121     $glyphs = $self->{' PARENT'}{'loca'}{'glyphs'};
122     $num = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
123
124     for ($i = 0; $i < $num; $i++)
125     {
126         $aw = $hmtx->{'advance'}[$i];
127         $lsb = $hmtx->{'lsb'}[$i];
128         if (defined $glyphs->[$i])
129         { $ext = $lsb + $glyphs->[$i]->read->{'xMax'} - $glyphs->[$i]{'xMin'}; }
130         else
131         { $ext = $aw; }
132         $maw = $aw if ($aw > $maw);
133         $mlsb = $lsb if ($lsb < $mlsb or $i == 0);
134         $mrsb = $aw - $ext if ($aw - $ext < $mrsb or $i == 0);
135         $mext = $ext if ($ext > $mext);
136     }
137     $self->{'advanceWidthMax'} = $maw;
138     $self->{'minLeftSideBearing'} = $mlsb;
139     $self->{'minRightSideBearing'} = $mrsb;
140     $self->{'xMaxExtent'} = $mext;
141     $self->{'numberOfHMetrics'} = $hmtx->numMetrics;
142     $self;
143 }
144
145
146 1;
147
148
149 =head1 BUGS
150
151 None known
152
153 =head1 AUTHOR
154
155 Martin Hosken Martin_Hosken@sil.org. See L<Font::TTF::Font> for copyright and
156 licensing.
157
158 =cut
159