Font/TTF/Silf.pm untested

filter: Statementstmt 0.0% 0 / 553 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 709 CC 39 SCAR 38.0
CC
39
Cov
0%
read
90.0
out
72.0
XML_element
72.0
CRAP 44.9
1 package Font::TTF::Silf;
2  
3 =head1 NAME
4  
5 Font::TTF::Silf - The main Graphite table
6  
7 =head1 DESCRIPTION
8  
9 The Silf table holds the core of the Graphite rules for a font. A Silf table has
10 potentially multiple silf subtables, although there is usually only one. Within a silf subtable,
11 there are a number of passes which contain the actual finite state machines to match rules
12 and the constraint and action code to be executed when a rule matches.
13  
14 =head1 INSTANCE VARIABLES
15  
16 =over 4
17  
18 =item Version
19  
20 Silf table format version
21  
22 =item Compiler
23  
24 Lowest compiler version necessary to fully support the semantics expressed in this
25 Graphite description
26  
27 =item SILF
28  
29 An array of Silf subtables
30  
31 =over 4
32  
33 =item maxGlyphID
34  
35 The maximum glyph id referenced including pseudo and non glyphs
36  
37 =item Ascent
38  
39 Extra ascent to be added to the font ascent.
40  
41 =item Descent
42  
43 Extra descent to be added to the font descent. Both values are assumed to be
44 positive for a descender below the base line.
45  
46 =item substPass
47  
48 Pass index into PASS of the first substitution pass.
49  
50 =item posPass
51  
52 Pass index into PASS of the first positioning pass.
53  
54 =item justPass
55  
56 Pass index into PASS of the first justification pass.
57  
58 =item bidiPass
59  
60 Pass index of the pass before which the bidirectional processing pass will be executed.
61 0xFF indicates that there is no bidi pass to be executed.
62  
63 =item Flags
64  
65 A bitfield of flags:
66  
67 0 - Indicates there are line end contextual rules in one of the passes
68  
69 =item maxPreContext
70  
71 Maximum length of a context preceding a cross line boundary contextualisation.
72  
73 =item maxPostContext
74  
75 Maximum length of a context following a cross line boundary contextualsation.
76  
77 =item attrPseudo
78  
79 Glyph attribute for the actual glyph id associated with a pseudo glyph.
80  
81 =item attrBreakWeight
82  
83 Glyph attribute number of the attribute holding the default breakweight associated with a glyph.
84  
85 =item attrDirectionality
86  
87 Glyph attribute number of the attribute holding the default directionality value associated with a glyph.
88  
89 =item JUST
90  
91 The may be a number of justification levels each with their own property values.
92 This points to an array of hashes, one for each justification level.
93  
94 =over 4
95  
96 =item attrStretch
97  
98 Glyph attribute number for the amount of stretch allowed before this glyph.
99  
100 =item attrShrink
101  
102 Glyph attribute number for the amount of shrink allowed before this glyph.
103  
104 =item attrStep
105  
106 Glyph attribute number specifying the minimum granularity of actual spacing associated with this glyph at this level.
107  
108 =item attrWeight
109  
110 Glyph attribute number giving the weight associated with spreading space across a run of glyphs.
111  
112 =item runto
113  
114 Which level starts the next stage.
115  
116 =back
117  
118 =item numLigComp
119  
120 Number of initial glyph attributes that represent ligature components
121  
122 =item numUserAttr
123  
124 Number of user defined slot attributes referenced. Tells the engine how much space to
125 allocate to a slot for user attributes.
126  
127 =item maxCompPerLig
128  
129 Maximum number of components per ligature.
130  
131 =item direction
132  
133 Supported directions for this writing system
134  
135 =item CRIT_FEATURE
136  
137 Array of critical features.
138  
139 =item scripts
140  
141 Array of script tags that indicate which set of GDL rules to execute if there is more than one in a font.
142  
143 =item lbGID
144  
145 Glyph ID of the linebreak pseudo glyph.
146  
147 =item pseudos
148  
149 Hash of Unicode values to pseduo glyph ids.
150  
151 =item classes
152  
153 This is an array of classes, each of which is an array of glyph ids in class order.
154  
155 =item PASS
156  
157 The details of rules and actions are stored in passes. This value is an array of pass subobjects one for each pass.
158  
159 =over 4
160  
161 =item flags
162  
163 This is a bitfield:
164  
165 0 - If true, this pass makes no change to the slot stream considered as a sequence of glyph ids.
166 Only slot attributes are expected to change (for example during positioning).
167  
168 =item maxRuleLoop
169  
170 How many times the engine will allow rules to be tested and run without the engine advancing through the
171 input slot stream.
172  
173 =item maxRuleContext
174  
175 Number of slots of input needed to run this pass.
176  
177 =item maxBackup
178  
179 Number of slots by which the following pass needs to trail this pass (i.e. the maximum this pass is allowed to back up).
180  
181 =item numRules
182  
183 Number of action code blocks, and so uncompressed rules, in this pass.
184  
185 =item numRows
186  
187 Number of rows in the finite state machine.
188  
189 =item numTransitional
190  
191 Number of rows in the finite state machine that are not final states. This specifies the number of rows in the fsm
192 element.
193  
194 =item numSuccess
195  
196 Number of success states. A success state may also be a transitional state.
197  
198 =item numColumns
199  
200 Number of columns in the finite state machine.
201  
202 =item colmap
203  
204 A hash, indexed by glyphid, that gives the fsm column number associated with that glyphid. If not present, then
205 the glyphid is not part of the fsm and will finish fsm processing if it occurs.
206  
207 =item rulemap
208  
209 An array of arrays, one for each success state. Each array holds a list of rule numbers associated with that state.
210  
211 =item minRulePreContext
212  
213 Minimum number of items in a rule's precontext.
214  
215 =item maxRulePreContext
216  
217 The maximum number of items in any rule's precontext.
218  
219 =item startStates
220  
221 Array of starting state numbers dependeing on the length of actual precontext.
222 There are maxRulePreContext - minRulePreContext + 1 of these.
223  
224 =item ruleSortKeys
225  
226 An array of sort keys one for each rule giving the length of the rule including its precontext.
227  
228 =item rulePreContexts
229  
230 An array of precontext lengths for each rule.
231  
232 =item fsm
233  
234 A two dimensional array such that $p->{'fsm'}[$row][$col] gives the row of the next node to try in the fsm.
235  
236 =item passConstraintLen
237  
238 Length in bytes of the passConstraint code.
239  
240 =item passConstraintCode
241  
242 A byte string holding the pass constraint code.
243  
244 =item constraintCode
245  
246 An array of byte strings holding the constraint code for each rule.
247  
248 =item actionCode
249  
250 An array of byte strings holding the action code for each rule.
251  
252 =back
253  
254 =back
255  
256 =back
257  
258 =cut
259  
260 use Font::TTF::Table;
261 use Font::TTF::Utils;
262 use strict;
263 use vars qw(@ISA);
264  
265 @ISA = qw(Font::TTF::Table);
266  
267 =head2 @opcodes
268  
269 Each array holds the name of the opcode, the number of operand bytes and a string describing the operands.
270 The characters in the string have the following meaning:
271  
272 c - lsb of class id
273 C - msb of class id
274 f - feature index
275 g - lsb of glyph attribute id
276 G - msb of glyph attribute id
277 l - lsb of a 32-bit extension to a 16-bit number
278 L - msb of a 32-bit number
279 m - glyph metric id
280 n - lsb of a number
281 N - msb of a 16-bit number
282 o - offset (jump)
283 s - slot reference
284 S - slot attribute id
285 v - variable number of following arguments
286  
287 =cut
288  
289 our @opcodes = ( ["nop", 0, ""], ["push_byte", 1, "n"], ["push_byte_u", 1, "n"], ["push_short", 2, "Nn"],
290 ["push_short_u", 2, "Nn"], ["push_long", 4, "LlNn"], ["add", 0, ""], ["sub", 0, ""],
291 ["mul", 0, ""], ["div", 0, ""], ["min", 0, ""], ["max", 0, ""],
292 ["neg", 0, ""], ["trunc8", 0, ""], ["trunc16", 0, ""], ["cond", 0, ""],
293 ["and", 0, ""], ["or", 0, ""], ["not", 0, ""], ["equal", 0, ""], # 16
294 ["not_eq", 0, ""], ["less", 0, ""], ["gtr", 0, ""], ["less_eq", 0, ""],
295 ["gtr_eq", 0, ""], ["next", 0, ""], ["next_n", 1, "n"], ["copy_next", 0, ""],
296 ["put_glyph_8bit_obs", 1, "c"], ["put_subs_8bit_obs", 3, "scc"], ["put_copy", 1, "s"], ["insert", 0, ""],
297 ["delete", 0, ""], ["assoc", -1, "v"], ["cntxt_item", 2, "so"], ["attr_set", 1, "S"], # 32
298 ["attr_add", 1, "S"], ["attr_sub", 1, "S"], ["attr_set_slot", 1, "S"], ["iattr_set_slot", 2, "Sn"],
299 ["push_slot_attr", 2, "Ss"], ["push_glyph_attr_obs", 2, "gs"], ["push_glyph_metric", 3, "msn"], ["push_feat", 2, "fs"],
300 ["push_att_to_gattr_obs", 2, "gs"], ["push_att_to_glyph_metric", 3, "msn"], ["push_islot_attr", 3, "Ssn"], ["push_iglyph_attr", 3, "gsn"],
301 ["pop_ret", 0, ""], ["ret_zero", 0, ""], ["ret_true", 0, ""], ["iattr_set", 2, "Sn"], # 48
302 ["iattr_add", 2, "Sn"], ["iattr_sub", 2, "Sn"], ["push_proc_state", 1, "n"], ["push_version", 0, ""],
303 ["put_subs", 5, "sCcCc"], ["put_subs2", 4, "cscc"], ["put_subs3", 7, "scscscc"], ["put_glyph", 2, "Cc"],
304 ["push_glyph_attr", 3, "Ggs"], ["push_att_to_glyph_attr", 3, "Ggs"], ["bitand", 0, ""], ["bitor", 0, ""],
305 ["bitnot", 0, ""], ["setbits", 4, "NnNn"], ["setfeat", 2, "fs"] ); # 64
306  
307 my ($i) = 0;
308 our %opnames = map {$_->[0] => $i++} @opcodes;
309  
310 =head2 read
311  
312 Reads the Silf table into the internal data structure
313  
314 =cut
315  
316 sub read
317 {
318 my ($self) = @_;
319 $self->SUPER::read or return $self;
320  
321 my ($dat, $d);
322 my ($fh) = $self->{' INFILE'};
323 my ($moff) = $self->{' OFFSET'};
324 my ($numsilf, @silfo);
325
326 $fh->read($dat, 4);
327 ($self->{'Version'}) = TTF_Unpack("v", $dat);
328 if ($self->{'Version'} >= 3)
329 {
330 $fh->read($dat, 4);
331 ($self->{'Compiler'}) = TTF_Unpack("v", $dat);
332 }
333 $fh->read($dat, 4);
334 ($numsilf) = TTF_Unpack("S", $dat);
335 $fh->read($dat, $numsilf * 4);
336 foreach my $i (0 .. $numsilf - 1)
337 { push (@silfo, TTF_Unpack("L", substr($dat, $i * 4, 4))); }
338  
339 foreach my $sili (0 .. $numsilf - 1)
340 {
341 my ($silf) = {};
342 my (@passo, @classo, $classbase, $numJust, $numCritFeatures, $numScript, $numPasses, $numPseudo, $i);
343  
344 push (@{$self->{'SILF'}}, $silf);
345 $fh->seek($moff + $silfo[$sili], 0);
346 if ($self->{'Version'} >= 3)
347 {
348 $fh->read($dat, 8);
349 ($silf->{'Version'}) = TTF_Unpack("v", $dat);
350 }
351 $fh->read($dat, 20);
352 ($silf->{'maxGlyphID'}, $silf->{'Ascent'}, $silf->{'Descent'},
353 $numPasses, $silf->{'substPass'}, $silf->{'posPass'}, $silf->{'justPass'}, $silf->{'bidiPass'},
354 $silf->{'Flags'}, $silf->{'maxPreContext'}, $silf->{'maxPostContext'}, $silf->{'attrPseudo'},
355 $silf->{'attrBreakWeight'}, $silf->{'attrDirectionality'}, $silf->{'attrMirror'}, $silf->{'passBits'}, $numJust) =
356 TTF_Unpack("SssCCCCCCCCCCCCCC", $dat);
357 if ($numJust)
358 {
359 foreach my $j (0 .. $silf->{'numJust'} - 1)
360 {
361 my ($just) = {};
362 push (@{$silf->{'JUST'}}, $just);
363 $fh->read($dat, 8);
364 ($just->{'attrStretch'}, $just->{'attrShrink'}, $just->{'attrStep'}, $just->{'attrWeight'},
365 $just->{'runto'}) = TTF_Unpack("CCCCC", $dat);
366 }
367 }
368 $fh->read($dat, 10);
369 ($silf->{'numLigComp'}, $silf->{'numUserAttr'}, $silf->{'maxCompPerLig'}, $silf->{'direction'},
370 $silf->{'attCollisions'}, $d, $d, $d, $numCritFeatures) = TTF_Unpack("SCCCCCCCC", $dat);
371 if ($numCritFeatures)
372 {
373 $fh->read($dat, $numCritFeatures * 2);
374 $silf->{'CRIT_FEATURE'} = [TTF_Unpack("S$numCritFeatures", $dat)];
375 }
376 $fh->read($dat, 2);
377 ($d, $numScript) = TTF_Unpack("CC", $dat);
378 if ($numScript)
379 {
380 $fh->read($dat, $numScript * 4);
381 foreach (0 .. $numScript - 1)
382 { push (@{$silf->{'scripts'}}, unpack('a4', substr($dat, $_ * 4, 4))); }
383 }
384 $fh->read($dat, 2);
385 ($silf->{'lbGID'}) = TTF_Unpack("S", $dat);
386 $fh->read($dat, $numPasses * 4 + 4);
387 @passo = unpack("N*", $dat);
388 $fh->read($dat, 8);
389 ($numPseudo) = TTF_Unpack("S", $dat);
390 if ($numPseudo)
391 {
392 $fh->read($dat, $numPseudo * 6);
393 foreach (0 .. $numPseudo - 1)
394 {
395 my ($uni, $gid) = TTF_Unpack("LS", substr($dat, $_ * 6, 6));
396 $silf->{'pseudos'}{$uni} = $gid;
397 }
398 }
399 $classbase = $fh->tell();
400 $fh->read($dat, 4);
401 my ($numClasses, $numLinearClasses) = TTF_Unpack("SS", $dat);
402 $silf->{'numLinearClasses'} = $numLinearClasses;
403 $fh->read($dat, ($numClasses + 1) * ($self->{'Version'} >= 4 ? 4 : 2));
404 @classo = unpack($self->{'Version'} >= 4 ? "N*" : "n*", $dat);
405 $fh->read($dat, $classo[-1] - $classo[0]);
406 for ($i = 0; $i < $numLinearClasses; $i++)
407 {
408 push (@{$silf->{'classes'}}, [unpack("n*", substr($dat, $classo[$i] - $classo[0],
409 $classo[$i+1] - $classo[$i]))])
410 }
411 for ($i = $numLinearClasses; $i < $numClasses; $i++)
412 {
413 my (@res);
414 my (@c) = unpack("n*", substr($dat, $classo[$i] - $classo[0] + 8, $classo[$i+1] - $classo[$i] - 8));
415 for (my $j = 0; $j < @c; $j += 2)
416 { $res[$c[$j+1]] = $c[$j]; }
417 push (@{$silf->{'classes'}}, \@res);
418 }
419 foreach (0 .. $numPasses - 1)
420 { $self->read_pass($fh, $passo[$_], $moff + $silfo[$sili], $silf, $_); }
421 }
422 return $self;
423 }
424  
425 sub chopcode
426 {
427 my ($dest, $dat, $offsets, $isconstraint) = @_;
428 my ($last) = $offsets->[-1];
429 my ($i);
430  
431 for ($i = $#{$offsets} - 1; $i >= 0; $i--)
432 {
433 if ((!$isconstraint || $offsets->[$i]) && $offsets->[$i] != $last)
434 {
435 unshift(@{$dest}, substr($dat, $offsets->[$i], $last - $offsets->[$i]));
436 $last = $offsets->[$i];
437 }
438 else
439 { unshift(@{$dest}, ""); }
440 }
441 }
442  
443  
444 sub read_pass
445 {
446 my ($self, $fh, $offset, $base, $silf, $id) = @_;
447 my ($pass) = {'id' => $id};
448 my ($d, $dat, $i, @orulemap, @oconstraints, @oactions, $numRanges);
449  
450 $fh->seek($offset + $base, 0);
451 # printf "pass base = %04X\n", $offset;
452 push (@{$silf->{'PASS'}}, $pass);
453 $fh->read($dat, 40);
454 ($pass->{'flags'}, $pass->{'maxRuleLoop'}, $pass->{'maxRuleContext'}, $pass->{'maxBackup'},
455 $pass->{'numRules'}, $d, $d, $d, $d, $d, $pass->{'numRows'}, $pass->{'numTransitional'},
456 $pass->{'numSuccess'}, $pass->{'numColumns'}, $numRanges) =
457 TTF_Unpack("CCCCSSLLLLSSSSS", $dat);
458 $fh->read($dat, $numRanges * 6);
459 foreach $i (0 .. $numRanges - 1)
460 {
461 my ($first, $last, $col) = TTF_Unpack('SSS', substr($dat, $i * 6, 6));
462 foreach ($first .. $last)
463 { $pass->{'colmap'}{$_} = $col; }
464 }
465 $fh->read($dat, $pass->{'numSuccess'} * 2 + 2);
466 @orulemap = unpack("n*", $dat);
467 $fh->read($dat, $orulemap[-1] * 2);
468 foreach (0 .. $pass->{'numSuccess'} - 1)
469 { push (@{$pass->{'rulemap'}}, [unpack("n*", substr($dat, $orulemap[$_] * 2, ($orulemap[$_+1] - $orulemap[$_]) * 2))]); }
470 $fh->read($dat, 2);
471 ($pass->{'minRulePreContext'}, $pass->{'maxRulePreContext'}) = TTF_Unpack("CC", $dat);
472 $fh->read($dat, ($pass->{'maxRulePreContext'} - $pass->{'minRulePreContext'} + 1) * 2);
473 $pass->{'startStates'} = [unpack('n*', $dat)];
474 $fh->read($dat, $pass->{'numRules'} * 2);
475 $pass->{'ruleSortKeys'} = [unpack('n*', $dat)];
476 $fh->read($dat, $pass->{'numRules'});
477 $pass->{'rulePreContexts'} = [unpack('C*', $dat)];
478 $fh->read($dat, 3);
479 ($pass->{'collisionThreshold'}, $pass->{'passConstraintLen'}) = TTF_Unpack("CS", $dat);
480 $fh->read($dat, ($pass->{'numRules'} + 1) * 2);
481 @oconstraints = unpack('n*', $dat);
482 $fh->read($dat, ($pass->{'numRules'} + 1) * 2);
483 @oactions = unpack('n*', $dat);
484 foreach (0 .. $pass->{'numTransitional'} - 1)
485 {
486 $fh->read($dat, $pass->{'numColumns'} * 2);
487 push (@{$pass->{'fsm'}}, [unpack('n*', $dat)]);
488 }
489 $fh->read($dat, 1);
490 if ($pass->{'passConstraintLen'})
491 { $fh->read($pass->{'passConstraintCode'}, $pass->{'passConstraintLen'}); }
492 $fh->read($dat, $oconstraints[-1]);
493 $pass->{'constraintCode'} = [];
494 chopcode($pass->{'constraintCode'}, $dat, \@oconstraints, 1);
495 $fh->read($dat, $oactions[-1]);
496 $pass->{'actionCode'} = [];
497 chopcode($pass->{'actionCode'}, $dat, \@oactions, 0);
498 return $pass;
499 }
500  
501 sub chopranges
502 {
503 my ($map, $numg) = @_;
504 my ($dat, $numRanges);
505 my (@keys) = sort {$a <=> $b} keys %{$map};
506 my ($first, $last, $col, $g);
507  
508 $first = -1;
509 $last = -1;
510 $col = -1;
511 foreach $g (@keys)
512 {
513 next unless ($g > 0 or $g eq '0');
514 if ($g != $last + 1 || $map->{$g} != $col)
515 {
516 if ($col != -1)
517 {
518 $dat .= pack("nnn", $first, $last, $col);
519 $numRanges++;
520 }
521 $first = $last = $g;
522 $col = $map->{$g};
523 }
524 else
525 { $last++; }
526 }
527 if ($col != -1)
528 {
529 $dat .= pack("nnn", $first, $last, $col);
530 $numRanges++;
531 }
532 return ($numRanges, $dat);
533 }
534  
535 sub unpack_code
536 {
537 my ($self, $str) = @_;
538 my (@res, $i, $j);
539 my ($l) = length($str);
540  
541 for ($i = 0; $i < $l; )
542 {
543 my ($a) = unpack('C', substr($str, $i, 1));
544 my ($o) = $opcodes[$a];
545 my (@args);
546 my (@types) = split('', $o->[2]);
547 ++$i;
548 for ($j = 0; $j < @types; ++$j)
549 {
550 my ($t) = $types[$j];
551 if ($t eq 'v')
552 {
553 my ($n) = unpack('C', substr($str, $i, 1));
554 push (@args, unpack('C*', substr($str, $i + 1, $n)));
555 $i += $n + 1;
556 }
557 elsif ($t eq 'L' or $t eq 'N' or $t eq 'G' or $t eq 'C')
558 {
559 push (@args, unpack('n', substr($str, $i, 2)));
560 $i += 2;
561 $j++;
562 }
563 else
564 {
565 push (@args, unpack($t eq 's' ? 'c' : 'C', substr($str, $i, 1)));
566 $i++;
567 }
568 }
569 push (@res, [$o->[0], @args]);
570 }
571 return @res;
572 }
573  
574 sub pack_code
575 {
576 my ($self, $cmds) = @_;
577 my ($res);
578  
579 foreach my $c (@{$cmds})
580 {
581 my ($ind) = $opnames{$c->[0]};
582 my ($i) = 1;
583 $res .= pack('C', $ind);
584 # my (@types) = unpack('C*', $opcodes[$ind][2]);
585 my (@types) = split('', $opcodes[$ind][2]);
586 for (my $j = 0; $j < @types; $j++)
587 {
588 my ($t) = $types[$j];
589 if ($t eq 'v')
590 {
591 my ($n) = scalar @{$c} - 1;
592 $res .= pack('C*', $n, @{$c}[1..$#{$c}]);
593 $i += $n;
594 }
595 elsif ($t eq 'C' or $t eq 'G' or $t eq 'L' or $t eq 'N')
596 {
597 $res .= pack('n', $c->[$i]);
598 $j++;
599 }
600 else
601 { $res .= pack($t eq 's' ? 'c' : 'C', $c->[$i]); }
602 $i++;
603 }
604 }
605 return $res;
606 }
607  
608 sub packcode
609 {
610 my ($code, $isconstraint) = @_;
611 my ($dat, $c, $res);
612  
613 $c = 1;
614 $dat = "\000";
615 foreach (@{$code})
616 {
617 if ($_)
618 {
619 push(@{$res}, $c);
620 $dat .= $_;
621 $c += length($_);
622 }
623 else
624 { push(@{$res}, $isconstraint ? 0 : $c); }
625 }
626 push(@{$res}, $c);
627 return ($res, $dat);
628 }
629  
630 sub out_pass
631 {
632 my ($self, $fh, $pass, $silf, $subbase) = @_;
633 my (@orulemap, $dat, $actiondat, $numRanges, $c);
634 my (@offsets, $res, $pbase);
635  
636 $pbase = $fh->tell();
637 # printf "pass base = %04X, ", $pbase - $subbase;
638 $fh->print(TTF_Pack("CCCCSSLLLLSSSS", $pass->{'flags'}, $pass->{'maxRuleLoop'}, $pass->{'maxRuleContext'},
639 $pass->{'maxBackup'}, $pass->{'numRules'}, 24, 0, 0, 0, 0, $pass->{'numRows'},
640 $pass->{'numTransitional'}, $pass->{'numSuccess'}, $pass->{'numColumns'}));
641 ($numRanges, $dat) = chopranges($pass->{'colmap'});
642 # print "numranges = $numRanges\n";
643 $fh->print(TTF_Pack("SSSS", TTF_bininfo($numRanges, 6)));
644 $fh->print($dat);
645 $dat = "";
646 $c = 0;
647 # print "transitions = $pass->{'numTransitional'}, success = $pass->{'numSuccess'}, rows = $pass->{'numRows'}\n";
648 my ($sucbase) = $pass->{'numRows'} - $pass->{'numSuccess'};
649 foreach (0 .. ($pass->{'numSuccess'} - 1))
650 {
651 push(@orulemap, $c);
652 if (defined $pass->{'rulemap'}[$_])
653 {
654 $dat .= pack("n*", @{$pass->{'rulemap'}[$_]});
655 $c += @{$pass->{'rulemap'}[$_]};
656 }
657 else
658 {
659 print "No rules for " . ($sucbase + $_);
660 if ($sucbase + $_ < $pass->{'numTransitional'})
661 { print ": (" . join(",", @{$pass->{'fsm'}[$sucbase + $_]}) . ")"; }
662 print "\n";
663 }
664 }
665 push (@orulemap, $c);
666 $fh->print(pack("n*", @orulemap));
667 $fh->print($dat);
668 $fh->print(TTF_Pack("CC", $pass->{'minRulePreContext'}, $pass->{'maxRulePreContext'}));
669 $fh->print(pack("n*", @{$pass->{'startStates'}}));
670 $fh->print(pack("n*", @{$pass->{'ruleSortKeys'}}));
671 $fh->print(pack("C*", @{$pass->{'rulePreContexts'}}));
672 $fh->print(TTF_Pack("CS", 0, $pass->{'passConstraintLen'}));
673 my ($oconstraints, $oactions);
674 ($oconstraints, $dat) = packcode($pass->{'constraintCode'}, 1);
675 ($oactions, $actiondat) = packcode($pass->{'actionCode'}, 0);
676 # printf "constraint offsets @ %X\n", $fh->tell();
677 $fh->print(pack("n*", @{$oconstraints}));
678 # printf "action offsets @ %X\n", $fh->tell();
679 $fh->print(pack("n*", @{$oactions}));
680 # printf "fsm @ %X\n", $fh->tell();
681 foreach (@{$pass->{'fsm'}})
682 { $fh->print(pack("n*", @{$_})); }
683 # printf "end of fsm @ %X\n", $fh->tell();
684 $fh->print(pack("C", $pass->{'collisionThreshold'}));
685 push(@offsets, $fh->tell() - $subbase);
686 $fh->print($pass->{'passConstraintCode'});
687 push(@offsets, $fh->tell() - $subbase);
688 $fh->print($dat);
689 push(@offsets, $fh->tell() - $subbase);
690 $fh->print($actiondat);
691 push(@offsets, 0);
692 print join(", ", @offsets) . "\n";
693 $res = $fh->tell();
694 $fh->seek($pbase + 8, 0);
695 $fh->print(pack("N*", @offsets));
696 $fh->seek($res, 0);
697 # printf "end = %04X\n", $res - $subbase;
698 return $res;
699 }
700  
701 =head2 out
702  
703 Outputs a Silf data structure to a font file in binary format
704  
705 =cut
706  
707 sub out
708 {
709 my ($self, $fh) = @_;
710 my ($silf, $base, $subbase, $silfc, $end);
711  
712 return $self->SUPER::out($fh) unless ($self->{' read'});
713 $base = $fh->tell();
714 if ($self->{'Version'} >= 3)
715 { $fh->print(TTF_Pack("vvSS", $self->{'Version'}, $self->{'Compiler'}, $#{$self->{'SILF'}} + 1, 0)); }
716 else
717 { $fh->print(TTF_Pack("vSS", $self->{'Version'}, $#{$self->{'SILF'}} + 1, 0)); }
718 $fh->print(pack('N*', (0) x (@{$self->{'SILF'}})));
719 foreach $silf (@{$self->{'SILF'}})
720 {
721 my ($subbase) = $fh->tell();
722 my ($numlin, $i, @opasses, $oPasses, $oPseudo, $ooPasses);
723 if ($self->{'Version'} >= 3)
724 {
725 $fh->seek($base + 12 + $silfc * 4, 0);
726 $fh->print(pack('N', $subbase - $base));
727 $fh->seek($subbase, 0);
728 $fh->print(TTF_Pack("vSS", $silf->{'Version'}, $ooPasses, $oPseudo));
729 }
730 else
731 {
732 $fh->seek($base + 8 + $silfc * 4, 0);
733 $fh->print(pack('N', $subbase - $base));
734 $fh->seek($subbase, 0);
735 }
736 $fh->print(TTF_Pack("SssCCCCCCCCCCCCCC",
737 $silf->{'maxGlyphID'}, $silf->{'Ascent'}, $silf->{'Descent'},
738 scalar @{$silf->{'PASS'}}, $silf->{'substPass'}, $silf->{'posPass'}, $silf->{'justPass'}, $silf->{'bidiPass'},
739 $silf->{'Flags'}, $silf->{'maxPreContext'}, $silf->{'maxPostContext'}, $silf->{'attrPseudo'},
740 $silf->{'attrBreakWeight'}, $silf->{'attrDirectionality'}, $silf->{'attrMirror'}, $silf->{'passBits'}, $#{$silf->{'JUST'}} + 1));
741 foreach (@{$silf->{'JUST'}})
742 { $fh->print(TTF_Pack("CCCCCCCC", $_->{'attrStretch'}, $_->{'attrShrink'}, $_->{'attrStep'},
743 $_->{'attrWeight'}, $_->{'runto'}, 0, 0, 0)); }
744
745 $fh->print(TTF_Pack("SCCCCCCCC", $silf->{'numLigComp'}, $silf->{'numUserAttr'}, $silf->{'maxCompPerLig'},
746 $silf->{'direction'}, $silf->{'attCollisions'}, 0, 0, 0, $#{$silf->{'CRIT_FEATURE'}} + 1));
747 $fh->print(pack("n*", @{$silf->{'CRIT_FEATURE'}}));
748 $fh->print(TTF_Pack("CC", 0, $#{$silf->{'scripts'}} + 1));
749 foreach (@{$self->{'scripts'}})
750 { $fh->print(pack("a4", $_)); }
751 $fh->print(TTF_Pack("S", $silf->{'lbGID'}));
752 $ooPasses = $fh->tell();
753 if ($silf->{'PASS'}) { $fh->print(pack("N*", (0) x (@{$silf->{'PASS'}} + 1)));}
754 $oPseudo = $fh->tell() - $subbase;
755 my (@pskeys) = keys %{$silf->{'pseudos'}};
756 $fh->print(TTF_Pack("SSSS", TTF_bininfo(scalar @pskeys, 6)));
757 foreach my $k (sort {$a <=> $b} @pskeys)
758 { $fh->print(TTF_Pack("Ls", $k, $silf->{'pseudos'}{$k})); }
759 $numlin = $silf->{'numLinearClasses'};
760 $fh->print(TTF_Pack("SS", scalar @{$silf->{'classes'}}, $numlin));
761 my (@coffsets);
762 # printf "%X, ", $fh->tell() - $base;
763 my ($cbase) = (scalar @{$silf->{'classes'}} + 1) * ($self->{'Version'} >= 4 ? 4 : 2) + 4;
764 for ($i = 0; $i < $numlin; $i++)
765 {
766 push (@coffsets, $cbase);
767 $cbase += 2 * scalar @{$silf->{'classes'}[$i]};
768 }
769 my (@nonlinclasses);
770 for ($i = $numlin; $i < @{$silf->{'classes'}}; $i++)
771 {
772 my (@c, $d, @d);
773 my $c = $silf->{'classes'}[$i];
774 push (@coffsets, $cbase);
775 @c = sort {$c->[$a] <=> $c->[$b]} (0 .. $#{$c});
776 foreach $d (@c)
777 { push (@d, $c->[$d], $d); }
778 push (@nonlinclasses, [@d]);
779 my ($len) = scalar @d;
780 $cbase += 8 + 2 * $len;
781 }
782 push (@coffsets, $cbase);
783 $fh->print(pack(($self->{'Version'} >= 4 ? 'N*' : 'n*'), @coffsets));
784 for ($i = 0; $i < $numlin; $i++)
785 { $fh->print(pack("n*", @{$silf->{'classes'}[$i]})); }
786 # printf "%X, ", $fh->tell() - $base;
787 for ($i = $numlin; $i < @{$silf->{'classes'}}; $i++)
788 {
789
790 my ($num) = scalar @{$nonlinclasses[$i-$numlin]};
791 my (@bin) = TTF_bininfo($num/2, 1);
792 $fh->print(TTF_Pack("SSSS", @bin));
793 $fh->print(pack("n*", @{$nonlinclasses[$i-$numlin]}));
794 }
795 $oPasses = $fh->tell() - $subbase;
796 # printf "original pass = %04X\n", $oPasses;
797 push (@opasses, $oPasses);
798 foreach (@{$silf->{'PASS'}})
799 { push(@opasses, $self->out_pass($fh, $_, $silf, $subbase) - $subbase); }
800 $end = $fh->tell();
801 $fh->seek($ooPasses, 0);
802 $fh->print(pack("N*", @opasses));
803 if ($self->{'Version'} >= 3)
804 {
805 $fh->seek($subbase + 4, 0);
806 $fh->print(TTF_Pack("SS", $ooPasses - $subbase, $oPseudo));
807 }
808 $fh->seek($end, 0);
809 $silfc++;
810 }
811 }
812  
813 sub XML_element
814 {
815 my ($self, $context, $depth, $k, $val, $ind) = @_;
816 my ($fh) = $context->{'fh'};
817 my ($i);
818  
819 return $self if ($k eq 'LOC');
820  
821 if ($k eq 'classes')
822 {
823 $fh->print("$depth<classes>\n");
824 foreach $i (0 .. $#{$val})
825 {
826 $fh->printf("$depth <class num='%d'>\n", $i);
827 $fh->printf("$depth " . join(" ", map{sprintf("%d", $_)} @{$val->[$i]}));
828 $fh->print("\n$depth </class>\n");
829 }
830 $fh->print("$depth</classes>\n");
831 }
832 elsif ($k eq 'fsm')
833 {
834 $fh->print("$depth<fsm>\n");
835 my ($i) = 0;
836 foreach (@{$val})
837 { $fh->print("$depth <row index='$i'>" . join(" ", @{$_}) . "</row>\n"); $i++; }
838 $fh->print("$depth</fsm>\n");
839 }
840 elsif ($k eq 'colmap')
841 {
842 my ($i);
843 $fh->print("$depth<colmap>");
844 foreach my $k (sort {$a <=> $b} keys %{$val})
845 {
846 if ($i++ % 8 == 0)
847 { $fh->print("\n$depth "); }
848 $fh->printf(" %d=%d", $k, $val->{$k});
849 }
850 $fh->print("\n$depth</colmap>\n");
851 }
852 elsif ($k eq 'constraintCode' or $k eq 'actionCode')
853 {
854 $fh->print("$depth<$k>\n");
855 foreach my $i (0 .. $#{$val})
856 {
857 my (@rules) = $self->unpack_code($val->[$i]);
858 next unless (@rules);
859 $fh->print("$depth <elem index='$i' code='" . join(" ", unpack('C*', $val->[$i])) . "'>\n");
860 foreach my $r (@rules)
861 { $fh->print("$depth $r->[0]: ". join(", ", @{$r}[1..$#{$r}]) . "\n"); }
862 $fh->print("$depth </elem>\n");
863 }
864 $fh->print("$depth</$k>\n");
865 }
866 else
867 { return $self->SUPER::XML_element($context, $depth, $k, $val, $ind); }
868  
869 $self;
870 }
871  
872 =head2 $t->minsize()
873  
874 Returns the minimum size this table can be. If it is smaller than this, then the table
875 must be bad and should be deleted or whatever.
876  
877 =cut
878  
879 sub minsize
880 {
881 return 4;
882 }
883  
884 1;
885  
886 =head1 AUTHOR
887  
888 Martin Hosken L<http://scripts.sil.org/FontUtils>.
889  
890  
891 =head1 LICENSING
892  
893 Copyright (c) 1998-2016, SIL International (http://www.sil.org)
894  
895 This module is released under the terms of the Artistic License 2.0.
896 For details, see the full text of the license in the file LICENSE.
897  
898  
899  
900 =cut