API3/Compat/API2/Basic/TTF/Post.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 220 CC 21 SCAR 44.2
CC
21
Cov
0%
out
156.0
read
56.0
XML_element
12.0
CRAP 82.9
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: Post.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::Post;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::Post - Holds the Postscript names for each glyph
32
33 =head1 DESCRIPTION
34
35 Holds the postscript names for glyphs. Note that they are not held as an
36 array, but as indexes into two lists. The first list is the standard Postscript
37 name list defined by the TrueType standard. The second comes from the font
38 directly.
39
40 Looking up a glyph from a Postscript name or a name from a glyph number is
41 achieved through methods rather than variable lookup.
42
43 This class handles PostScript table types of 1, 2, 2.5 & 3, but not version 4.
44 Support for version 2.5 is as per Apple spec rather than MS.
45
46 The way to look up Postscript names or glyphs is:
47
48 $pname = $f->{'post'}{'VAL'}[$gnum];
49 $gnum = $f->{'post'}{'STRINGS'}{$pname};
50
51 =head1 INSTANCE VARIABLES
52
53 Due to different systems having different limitations, there are various class
54 variables available to control what post table types can be written.
55
56 =over 4
57
58 =item $PDF::API3::Compat::API2::Basic::TTF::Post::no25
59
60 If set tells PDF::API3::Compat::API2::Basic::TTF::Post::out to use table type 2 instead of 2.5 in case apps
61 can't handle version 2.5.
62
63 =item VAL
64
65 Contains an array indexed by glyph number of Postscript names. This is used when
66 writing out a font.
67
68 =item STRINGS
69
70 An associative array of Postscript names mapping to the highest glyph with that
71 name. These may not be in sync with VAL.
72
73 =back
74
75 In addition there are the standard introductory variables defined in the
76 standard:
77
78 FormatType
79 italicAngle
80 underlinePosition
81 underlineThickness
82 isFixedPitch
83 minMemType42
84 maxMemType42
85 minMemType1
86 maxMemType1
87
88 =head1 METHODS
89
90 =cut
91
92 use strict;
93 use vars qw(@ISA @base_set %base_set %fields $VERSION $no25 @field_info @base_set);
94 require PDF::API3::Compat::API2::Basic::TTF::Table;
95 use PDF::API3::Compat::API2::Basic::TTF::Utils;
96
97 $no25 = 1; # officially deprecated format 2.5 tables in MS spec 1.3
98
99 @ISA = qw(PDF::API3::Compat::API2::Basic::TTF::Table);
100 @field_info = (
101 'FormatType' => 'f',
102 'italicAngle' => 'f',
103 'underlinePosition' => 's',
104 'underlineThickness' => 's',
105 'isFixedPitch' => 'L',
106 'minMemType42' => 'L',
107 'maxMemType42' => 'L',
108 'minMemType1' => 'L',
109 'maxMemType1' => 'L');
110 @base_set = qw(.notdef .null nonmarkingreturn space exclam quotedbl numbersign dollar percent ampersand quotesingle
111 parenleft parenright asterisk plus comma hyphen period slash zero one two three four five six
112 seven eight nine colon semicolon less equal greater question at A B C D E F G H I J K L M N O P Q
113 R S T U V W X Y Z bracketleft backslash bracketright asciicircum underscore grave a b c d e f g h
114 i j k l m n o p q r s t u v w x y z braceleft bar braceright asciitilde Adieresis Aring Ccedilla
115 Eacute Ntilde Odieresis Udieresis aacute agrave acircumflex adieresis atilde aring ccedilla eacute
116 egrave ecircumflex edieresis iacute igrave icircumflex idieresis ntilde oacute ograve ocircumflex
117 odieresis otilde uacute ugrave ucircumflex udieresis dagger degree cent sterling section bullet
118 paragraph germandbls registered copyright trademark acute dieresis notequal AE Oslash infinity
119 plusminus lessequal greaterequal yen mu partialdiff summation product pi integral ordfeminine
120 ordmasculine Omega ae oslash questiondown exclamdown logicalnot radical florin approxequal
121 Delta guillemotleft guillemotright ellipsis nonbreakingspace Agrave Atilde Otilde OE oe endash emdash
122 quotedblleft quotedblright quoteleft quoteright divide lozenge ydieresis Ydieresis fraction currency
123 guilsinglleft guilsinglright fi fl daggerdbl periodcentered quotesinglbase quotedblbase perthousand
124 Acircumflex Ecircumflex Aacute Edieresis Egrave Iacute Icircumflex Idieresis Igrave Oacute Ocircumflex
125 apple Ograve Uacute Ucircumflex Ugrave dotlessi circumflex tilde macron breve dotaccent
126 ring cedilla hungarumlaut ogonek caron Lslash lslash Scaron scaron Zcaron zcaron brokenbar Eth eth
127 Yacute yacute Thorn thorn minus multiply onesuperior twosuperior threesuperior onehalf onequarter
128 threequarters franc Gbreve gbreve Idotaccent Scedilla scedilla Cacute cacute Ccaron ccaron dcroat);
129
130 $VERSION = 0.01; # MJPH 5-AUG-1998 Re-organise data structures
131
132 sub init
133 {
134 my ($k, $v, $c, $i);
135 for ($i = 0; $i < $#field_info; $i += 2)
136 {
137 ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]);
138 next unless defined $k && $k ne "";
139 $fields{$k} = $v;
140 }
141 $i = 0;
142 %base_set = map {$_ => $i++} @base_set;
143 }
144
145
146 =head2 $t->read
147
148 Reads the Postscript table into memory from disk
149
150 =cut
151
152 sub read
153 {
154 my ($self) = @_;
155 my ($dat, $dat1, $i, $off, $c, $maxoff, $form, $angle, $numGlyphs);
156 my ($fh) = $self->{' INFILE'};
157
158 $numGlyphs = $self->{' PARENT'}{'maxp'}->read->{'numGlyphs'};
159 $self->SUPER::read or return $self;
160 init unless ($fields{'FormatType'});
161 $fh->read($dat, 32);
162 TTF_Read_Fields($self, $dat, \%fields);
163
164 if (int($self->{'FormatType'} + .5) == 1)
165 {
166 for ($i = 0; $i < 258; $i++)
167 {
168 $self->{'VAL'}[$i] = $base_set[$i];
169 $self->{'STRINGS'}{$base_set[$i]} = $i;
170 }
171 } elsif (int($self->{'FormatType'} * 2 + .1) == 5)
172 {
173 $fh->read($dat, 2);
174 $numGlyphs = unpack("n", $dat);
175 $fh->read($dat, $numGlyphs);
176 for ($i = 0; $i < $numGlyphs; $i++)
177 {
178 $off = unpack("c", substr($dat, $i, 1));
179 $self->{'VAL'}[$i] = $base_set[$i + $off];
180 $self->{'STRINGS'}{$base_set[$i + $off]} = $i;
181 }
182 } elsif (int($self->{'FormatType'} + .5) == 2)
183 {
184 my (@strings);
185
186 $fh->read($dat, ($numGlyphs + 1) << 1);
187 for ($i = 0; $i < $numGlyphs; $i++)
188 {
189 $off = unpack("n", substr($dat, ($i + 1) << 1, 2));
190 $maxoff = $off if (!defined $maxoff || $off > $maxoff);
191 }
192 for ($i = 0; $i < $maxoff - 257; $i++)
193 {
194 $fh->read($dat1, 1);
195 $off = unpack("C", $dat1);
196 $fh->read($dat1, $off);
197 $strings[$i] = $dat1;
198 }
199 for ($i = 0; $i < $numGlyphs; $i++)
200 {
201 $off = unpack("n", substr($dat, ($i + 1) << 1, 2));
202 if ($off > 257)
203 {
204 $self->{'VAL'}[$i] = $strings[$off - 258];
205 $self->{'STRINGS'}{$strings[$off - 258]} = $i;
206 }
207 else
208 {
209 $self->{'VAL'}[$i] = $base_set[$off];
210 $self->{'STRINGS'}{$base_set[$off]} = $i;
211 }
212 }
213 }
214 $self;
215 }
216
217
218 =head2 $t->out($fh)
219
220 Writes out a new Postscript name table from memory or copies from disk
221
222 =cut
223
224 sub out
225 {
226 my ($self, $fh) = @_;
227 my ($i, $num);
228
229 return $self->SUPER::out($fh) unless $self->{' read'};
230
231 $num = $self->{' PARENT'}{'maxp'}{'numGlyphs'};
232
233 init unless ($fields{'FormatType'});
234
235 for ($i = $#{$self->{'VAL'}}; !defined $self->{'VAL'}[$i] && $i > 0; $i--)
236 { pop(@{$self->{'VAL'}}); }
237 if ($#{$self->{'VAL'}} < 0)
238 { $self->{'FormatType'} = 3; }
239 else
240 {
241 $self->{'FormatType'} = 1;
242 for ($i = 0; $i < $num; $i++)
243 {
244 if (!defined $base_set{$self->{'VAL'}[$i]})
245 {
246 $self->{'FormatType'} = 2;
247 last;
248 }
249 elsif ($base_set{$self->{'VAL'}[$i]} != $i)
250 { $self->{'FormatType'} = ($no25 ? 2 : 2.5); }
251 }
252 }
253
254 $fh->print(TTF_Out_Fields($self, \%fields, 32));
255
256 return $self if (int($self->{'FormatType'} + .4) == 3);
257
258 if (int($self->{'FormatType'} + .5) == 2)
259 {
260 my (@ind, $count);
261
262 $fh->print(pack("n", $num));
263 for ($i = 0; $i < $num; $i++)
264 {
265 if (defined $base_set{$self->{'VAL'}[$i]})
266 { $fh->print(pack("n", $base_set{$self->{'VAL'}[$i]})); }
267 else
268 {
269 $fh->print(pack("n", $count + 258));
270 $ind[$count++] = $i;
271 }
272 }
273 for ($i = 0; $i < $count; $i++)
274 {
275 $fh->print(pack("C", length($self->{'VAL'}[$ind[$i]])));
276 $fh->print($self->{'VAL'}[$ind[$i]]);
277 }
278 } elsif (int($self->{'FormatType'} * 2 + .5) == 5)
279 {
280 $fh->print(pack("n", $num));
281 for ($i = 0; $i < $num; $i++)
282 { $fh->print(pack("c", defined $base_set{$self->{'VAL'}[$i]} ?
283 $base_set{$self->{'VAL'}[$i]} - $i : -$i)); }
284 }
285
286 $self;
287 }
288
289
290 =head2 $t->XML_element($context, $depth, $key, $val)
291
292 Outputs the names as one block of XML
293
294 =cut
295
296 sub XML_element
297 {
298 my ($self) = shift;
299 my ($context, $depth, $key, $val) = @_;
300 my ($fh) = $context->{'fh'};
301 my ($i);
302
303 return $self->SUPER::XML_element(@_) unless ($key eq 'STRINGS' || $key eq 'VAL');
304 return unless ($key eq 'VAL');
305
306 $fh->print("$depth<names>\n");
307 for ($i = 0; $i <= $#{$self->{'VAL'}}; $i++)
308 { $fh->print("$depth$context->{'indent'}<name post='$self->{'VAL'}[$i]' gid='$i'/>\n"); }
309 $fh->print("$depth</names>\n");
310 $self;
311 }
312
313 1;
314
315 =head1 BUGS
316
317 =over 4
318
319 =item *
320
321 No support for type 4 tables
322
323 =back
324
325 =head1 AUTHOR
326
327 Martin Hosken Martin_Hosken@sil.org. See L<PDF::API3::Compat::API2::Basic::TTF::Font> for copyright and
328 licensing.
329
330 =cut
331