Introduce src dir.
[librarian.git] / src / librarian / font-optimizer / ext / Font-TTF / lib / Font / TTF / Vhea.pm
1 package Font::TTF::Vhea;
2
3 =head1 NAME
4
5 Font::TTF::Vhea - Vertical Header table
6
7 =head1 DESCRIPTION
8
9 This is a simple table with just standards specified instance variables
10
11 =head1 INSTANCE VARIABLES
12
13     version
14     Ascender
15     Descender
16     LineGap
17     advanceHeightMax
18     minTopSideBearing
19     minBottomSideBearing
20     yMaxExtent
21     caretSlopeRise
22     caretSlopeRun
23     metricDataFormat
24     numberOfVMetrics
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     'advanceHeightMax' => 'S',
44     'minTopSideBearing' => 's',
45     'minBottomSideBearing' => 's',
46     'yMaxExtent' => 's',
47     'caretSlopeRise' => 's',
48     'caretSlopeRun' => 's',
49     'metricDataFormat' => '+10s',
50     'numberOfVMetrics' => '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->{'numberOfVMetrics'} = $self->{' PARENT'}{'vmtx'}->numMetrics || $self->{'numberOfVMetrics'};
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, assuming
105 the C<hmtx> table is dirty.
106
107 =cut
108
109 sub update
110 {
111     my ($self) = @_;
112     my ($vmtx) = $self->{' PARENT'}{'vmtx'};
113     my ($glyphs);
114     my ($num);
115     my ($i, $maw, $mlsb, $mrsb, $mext, $aw, $lsb, $ext);
116
117     return undef unless ($self->SUPER::update);
118     return undef unless (defined $vmtx && defined $self->{' PARENT'}{'loca'});
119     $vmtx->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 = $vmtx->{'advance'}[$i];
127         $lsb = $vmtx->{'top'}[$i];
128         if (defined $glyphs->[$i])
129         { $ext = $lsb + $glyphs->[$i]->read->{'yMax'} - $glyphs->[$i]{'yMin'}; }
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->{'advanceHeightMax'} = $maw;
138     $self->{'minTopSideBearing'} = $mlsb;
139     $self->{'minBottomSideBearing'} = $mrsb;
140     $self->{'yMaxExtent'} = $mext;
141     $self->{'numberOfVMetrics'} = $vmtx->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