Debian Codegraph @

texk/texlive/linked_scripts/jfmutil/jfmutil.pl

raw
1 #!/usr/bin/env perl
2 #
3 # This is file 'jfmutil.pl'.
4 #
5 # Copyright (c) 2008-2020 Takayuki YATO (aka. "ZR")
6 # GitHub: https://github.com/zr-tex8r
7 # Twitter: @zr_tex8r
8 #
9 # This software is distributed under the MIT License.
10 #
11 use strict;
12
13 #------------------------------------------------- ZRTeXtor module
14 package ZRTeXtor;
15 our $VERSION = 1.008_01;
16 our $mod_date = "2021/10/09";
17 use Encode qw(encode decode);
18
19 # Here follows excerpt from ZRTeXtor.pm
20 #================================================= BEGIN
21 ######## general ########
22
23 our $binmode = 0; # always read/write in binary mode
24 our $errmsg; # last error message
25 use constant { HUGE => 1e20, EPS => 1e-7 };
26 # TU = TFM factor, DU = DVI factor
27 use constant { TU => 1 << 20, DU => 1 << 16, B31 => 1 << 31 };
28 use constant { M32 => 0xFFFFFFFF, M31 => 0x7FFFFFFF };
29 use constant { # kanji encoding names
30 K_JIS => 'iso-2022-jp', K_EUC => 'euc-jp',
31 K_SJIS => 'shiftjis', K_UTF8 => 'UTF-8',
32 KI_JIS => 'jis0208-raw', KI_UNI => 'UTF-16BE',
33 KI_XJIS => '*xjis*', K_XJIS => '*xjis*',
34 };
35 # for get_temp_name
36 our $get_temp_base = '__zrtx';
37 our (@get_temp_id);
38
39 ## error($msg, ...)
40 # Sets the error message and returns nothing (undef in scalar).
41 # Usually a defined value is returned in success.
42 sub error
43 {
44 $errmsg = join('', map { (ref $_) ? '[obj]' : $_ } (@_));
45 return;
46 }
47
48 ## fatal($msg, ...)
49 # Intended for internal errors....
50 sub fatal
51 {
52 return error("!!FATAL(", @_, ")");
53 }
54
55 ##<*> textool_error()
56 # Obtains the error message of the last error. This string
57 # can be used in error handling of your own style.
58 sub textool_error
59 {
60 return $errmsg;
61 }
62
63 ##<*> textool_version()
64 # Returns the version information on this library.
65 sub textool_version
66 {
67 my ($t, @fs);
68 $t = sprintf("%.5f", $VERSION);
69 (@fs = $t =~ m|^(\d+)\.(\d\d\d)(\d\d)$|)
70 or return fatal("textool_version");
71 $t = sprintf("%d.%d.%d", @fs);
72 return ("$t", $mod_date);
73 }
74
75 ## get_temp_name()
76 # Returns a temporary file name which is unique in the process.
77 # Note: Trailing digits in font names can have special meaning
78 # in some TeX tools.
79 sub get_temp_name
80 {
81 ++$get_temp_id[0];
82 return join('', $get_temp_base, $$, @get_temp_id[1, 0, 2]);
83 }
84
85 ## get_temp_anme_init()
86 # Initializer for get_temp_name.
87 sub get_temp_name_init
88 {
89 my ($t); $get_temp_id[0] = 0;
90 $t = join('', map { ('a' .. 'z')[int(rand() * 26)] } (0 .. 5));
91 @get_temp_id[1, 2] = map { substr($t, $_, 3) } (0, 3);
92 }
93
94 ##<*> read_whole_file($fnam, $swbin)
95 # Reads in file $fnam and returns its content as a string.
96 # If $swbin or the global flag $binmode is true the file is read
97 # in binary mode.
98 sub read_whole_file
99 {
100 my ($fnam, $swbin) = @_; my ($hf, $txt); local ($/);
101 (defined $fnam) or return;
102 open($hf, '<', $fnam)
103 or return error("cannot open for read: $fnam");
104 if ($binmode || $swbin) { binmode($hf); }
105 $txt = readline($hf);
106 close($hf);
107 return $txt;
108 }
109
110 ##<*> write_whole_file($fnam, $txt, $swbin)
111 # Creates (or crobbers) the file $fnam and write $txt to it.
112 # If $swbin or the global flag $binmode is true it writes in
113 # binary mode.
114 sub write_whole_file
115 {
116 my ($fnam, $txt, $swbin) = @_; my ($hf);
117 open($hf, '>', $fnam)
118 or return error("cannot open for write: $fnam");
119 if ($binmode || $swbin) { binmode($hf); }
120 print $hf ($txt);
121 close($hf);
122 return 1;
123 }
124
125 ## unpack_num($s)
126 # Decodes a unsigned number in big-endian string.
127 sub unpack_num
128 {
129 my ($s) = @_;
130 return unpack("N", substr("\0\0\0\0$s", length($s)));
131 }
132
133 ## unpack_snum($s)
134 # Decodes a signed number in big-endian string.
135 my @snum_bound = (0, 0x80, 0x8000, 0x800000, B31);
136 sub unpack_snum
137 {
138 my ($s) = @_; my ($b, $v);
139 $b = $snum_bound[length($s)];
140 $v = unpack("N", substr("\0\0\0\0$s", length($s)));
141 return ($v >= $b) ? ($v - $b - $b) : $v;
142 }
143
144 ## pack_num($v)
145 # Encodes a unsigned number in big-endian string, with the length
146 # prefix. In scalar context the returned values are concatenated.
147 sub pack_num
148 {
149 my ($v) = @_; my ($t, $l);
150 ($t = pack('N', $v)) =~ s/^\0{1,3}//; $l = length($t);
151 return (wantarray) ? ($l, $t) : (chr($l) . $t);
152 }
153
154 ## pack_snum($v)
155 # Signed version of pack_num.
156 sub pack_snum
157 {
158 my ($v) = @_; my ($t, $l);
159 ($t = pack('N', $v)) =~
160 s/^\0{1,3}([\x00-\x7f])|^\xff{1,3}([\x80-\xff])/$+/;
161 $l = length($t);
162 return (wantarray) ? ($l, $t) : (chr($l) . $t);
163 }
164
165 ## signed($v)
166 # Converts 'unsigned long' to 'signed long'.
167 sub signed
168 {
169 my ($v) = @_; $v &= M32;
170 return ($v >= B31) ? ($v - B31 - B31) : $v;
171 }
172
173 ## round($v)
174 # Rounds a real value to an integer.
175 sub round
176 {
177 my ($v) = @_;
178 return int($v + (($v < 0) ? -0.5 : +0.5));
179 }
180
181 ##<*> arraymap($map, $swmm)
182 # Converts a code map in hash form into array form.
183 sub arraymap
184 {
185 my ($map, $swmm) = @_; my ($sc, $dc, @u, @amap);
186 if (ref $map eq 'HASH') {
187 @u = sort { $a <=> $b } (keys %$map);
188 foreach $sc (@u) {
189 $dc = $map->{$sc};
190 if (ref $dc eq 'ARRAY') {
191 if ($swmm) {
192 foreach (@$dc) { push(@amap, [$sc, $_]); }
193 } else { push(@amap, [$sc, $dc->[0]]); }
194 } elsif (defined $dc) { push(@amap, [$sc, $dc]); }
195 }
196 return \@amap;
197 } elsif (ref $map eq 'ARRAY') { return $map; }
198 else { return; }
199 }
200
201 ## squote($str)
202 # S-quotes a string.
203 sub squote
204 {
205 my ($str) = @_; $str =~ s/([\\\'])/\\$1/g;
206 return "'$str'";
207 }
208
209 ## zdquote($str)
210 # ZD-quotes a string.
211 sub zdquote
212 {
213 my ($str) = @_; $str =~ s/([\\\"])/\\$1/g;
214 $str =~ s/([^\x20-\x7e])/sprintf("\\%02X",$1)/ge;
215 return "\"$str\"";
216 }
217
218 ######## 'x' section ########
219
220 use IPC::Open3; # for open3()
221 our %cmd_name = (
222 kpsewhich => 'kpsewhich',
223 tftopl => 'ptftopl',
224 pltotf => 'ppltotf',
225 uptftopl => 'uptftopl',
226 uppltotf => 'uppltotf',
227 vftovp => 'vftovp',
228 vptovf => 'vptovf',
229 opl2ofm => 'opl2ofm',
230 );
231
232 ##<*> x_captured_exec(@cmd);
233 # Spawns the command @cmd, captures its stdout and stderr into
234 # strings, and returns them.
235 # --- Am I doing right?
236 sub x_captured_exec
237 {
238 my (@cmd) = @_; my ($pid, @fs, @ds);
239 local(*CHIN, *CHOUT, *CHERR, $/);
240 L1:{
241 @fs = (get_temp_name(), get_temp_name());
242 open(CHOUT, '+>', $fs[0]) or last;
243 open(CHERR, '+>', $fs[1]) or last;
244 ($pid = open3(\*CHIN, '>&CHOUT', '>&CHERR', @cmd)) or last;
245 waitpid($pid, 0);
246 seek(CHOUT, 0, 0); $ds[0] = <CHOUT>;
247 seek(CHERR, 0, 0); $ds[1] = <CHERR>;
248 }
249 close(CHIN); close(CHOUT); close(CHERR);
250 unlink(@fs);
251 return (@ds);
252 }
253
254 ##<*> x_tftopl
255 # Wrapper for 'tftopl' command.
256 sub x_tftopl
257 {
258 my ($tfm, $cmd) = @_; my ($ftmp, $ftfm, $fpl, $cout, $cerr);
259 if (!defined $cmd) { $cmd = $cmd_name{tftopl}; }
260 if ($tfm =~ m/\.tfm$/i && $tfm !~ /\0/) { $ftfm = $tfm; }
261 else {
262 $ftfm = $ftmp = get_temp_name() . ".tfm";
263 (write_whole_file($ftmp, $tfm, 1)) or return;
264 }
265 $fpl = get_temp_name() . ".pl";
266 ($cout, $cerr) = x_captured_exec("$cmd $ftfm $fpl");
267 if (defined $ftmp) { unlink($ftmp); }
268 $cout = read_whole_file($fpl); unlink($fpl);
269 if ($cout eq '' || $cout =~ /CHANGED!\)\s*$/) {
270 return error("tftopl failed: $ftfm");
271 }
272 return pl_parse($cout);
273 }
274
275 ##<*> x_uptftopl
276 # Wrapper for 'uptftopl' command.
277 # Use of x_tftopl($tfm, "uptftopl") does not seem to work right.
278 # Perhaps getting uptftopl to 'output to terminal' confuses
279 # uptftopl as to handling UTF-8 strings....
280 sub x_uptftopl
281 {
282 my ($tfm, $cmd) = @_; my ($ftmp, $ftfm, $fpl, $cout, $cerr);
283 if (!defined $cmd) { $cmd = $cmd_name{uptftopl}; }
284 if ($tfm =~ m/\.tfm$/i && $tfm !~ /\0/) { $ftfm = $tfm; }
285 else {
286 $ftfm = $ftmp = get_temp_name() . ".tfm";
287 (write_whole_file($ftmp, $tfm, 1)) or return;
288 }
289 $fpl = get_temp_name() . ".pl";
290 ($cout, $cerr) = x_captured_exec("$cmd $ftfm $fpl");
291 if (defined $ftmp) { unlink($ftmp); }
292 $cout = read_whole_file($fpl); unlink($fpl);
293 if ($cout eq '' || $cout =~ /CHANGED!\)\s*$/) {
294 return error("tftopl failed: $ftfm");
295 }
296 if ($cout =~ m/CHARSINTYPE/) { $cout = pl_adjust_lit_paren($cout); }
297 return pl_parse($cout);
298 }
299
300 ##<*> x_pltotf
301 # Wrapper for 'pltotf' command.
302 sub x_pltotf
303 {
304 my ($pl, $cmd) = @_;
305 my ($ftmp, $fpl, $cout, $ftfm, $tfm, $tn);
306 if (!defined $cmd) { $cmd = $cmd_name{pltotf}; }
307 $tn = get_temp_name(); $ftfm = "$tn.tfm";
308 if (ref $pl eq 'ARRAY') { $pl = pl_form($pl); }
309 if ($pl =~ m/\.pl$/i) { $fpl = $pl; }
310 else {
311 $fpl = $ftmp = "$tn.pl";
312 (write_whole_file($ftmp, $pl)) or return;
313 }
314 $cout = `$cmd $fpl $ftfm`; # no output to stderr
315 if (defined $ftmp) { unlink($ftmp); }
316 (-f $ftfm) or return error("pltotf failed: $fpl");
317 $tfm = read_whole_file($ftfm, 1); unlink($ftfm);
318 foreach (split(/\n/, $cout)) {
319 if (m/^\s*$/ || m/^I had to round some / ||
320 m/Input file is in kanji /) { next; }
321 return error("pltotf failed: $fpl");
322 }
323 return $tfm;
324 }
325
326 ##<*> x_vptovf
327 # Wrapper for 'vptovf' command.
328 sub x_vptovf
329 {
330 my ($pl, $cmd) = @_;
331 my ($ftmp, $fpl, $cout, $ftfm, $tfm, $fvf, $vf, $tn);
332 if (!defined $cmd) { $cmd = $cmd_name{vptovf}; }
333 $tn = get_temp_name(); $ftfm = "$tn.tfm"; $fvf = "$tn.vf";
334 if (ref $pl eq 'ARRAY') { $pl = pl_form($pl); }
335 if ($pl =~ m/\.vpl$/i) { $fpl = $pl; }
336 else {
337 $fpl = $ftmp = "$tn.vpl";
338 (write_whole_file($ftmp, $pl)) or return;
339 }
340 $cout = `$cmd $fpl $fvf $ftfm`; # no output to stderr
341 if (defined $ftmp) { unlink($ftmp); }
342 (-f $ftfm && -f $fvf) or return error("vptovf failed: $fpl");
343 $tfm = read_whole_file($ftfm, 1); unlink($ftfm);
344 $vf = read_whole_file($fvf, 1); unlink($fvf);
345 foreach (split(/\n/, $cout)) {
346 if (m/^\s*$/ || m/^I had to round some / ||
347 m/Input file is in kanji / || m/LIG/) { next; }
348 return error("vptovf failed: $fpl");
349 }
350 return ($vf, $tfm);
351 }
352
353 ##<*> x_opl2ofm
354 # Wrapper for 'opl2ofm' command.
355 sub x_opl2ofm
356 {
357 my ($pl) = @_; my ($cmd, $ftmp, $fpl, $cout, $ftfm, $tfm, $tn);
358 $tn = get_temp_name(); $ftfm = "$tn.ofm";
359 if (ref $pl eq 'ARRAY') { $pl = pl_form($pl); }
360 if ($pl =~ m/\.opl$/i) { $fpl = $pl; }
361 else {
362 $fpl = $ftmp = "$tn.opl";
363 (write_whole_file($ftmp, $pl)) or return;
364 }
365 $cmd = $cmd_name{opl2ofm};
366 $cout = `$cmd $fpl $ftfm`; # no output to stderr
367 if (defined $ftmp) { unlink($ftmp); }
368 (-f $ftfm) or return error("opl2ofm failed: $fpl");
369 $tfm = read_whole_file($ftfm, 1); unlink($ftfm);
370 foreach (split(m/\n/, $cout)) {
371 if (m/^\s*$/ || m/^I had to round some / ||
372 m/Input file is in kanji /) { next; }
373 return error("opl2ofm failed: $fpl");
374 }
375 return $tfm;
376 }
377
378
379 ######## 'pl' section ########
380
381 ####---- Handling General Property Lists
382
383 # * Data structure: pl-list, pl-struct
384 # A PL Text consists of a sequence of Lispish lists (without the
385 # outermost parentheses). In 'raw' structures, Lispish lists are
386 # represented by Perl arrays with straightforward conversion
387 # (all atomic elements are strings). Such an array is called
388 # 'pl-list' here. The entire PL is represented as an array of
389 # pl-lists, called 'pl-struct' here.
390 # However, in 'cooked' structures, some numerical data are
391 # interpreted. For example, raw data
392 # 'D', '0100' (a part of a pl-list)
393 # are cooked they change to
394 # [ CNUM, 100, 'D' ], '0100'
395 # and moreover the assignment of 500 to this part results in
396 # [ CNUM, 500, 'D' ], undef
397 # And its conversion back into text is 'O 764', because 500 cannot
398 # be expressed in 'D' notation.
399
400 use constant CNUM => ' '; # flag signifying a cooked number
401 our $freenum = 0;
402
403 my %pl_facecode = ( # 'F' notation
404 MRR => 0, MIR => 1, BRR => 2, BIR => 3, LRR => 4, LIR => 5,
405 MRC => 6, MIC => 7, BRC => 8, BIC => 9, LRC => 10, LIC => 11,
406 MRE => 12, MIE => 13, BRE => 14, BIE => 15, LRE => 16, LIE => 17
407 );
408 my %pl_facecode_rev = (reverse %pl_facecode);
409
410 ##<*> pl_parse($txt)
411 # Converts a PL text $txt to a 'cooked' pl-struct.
412 sub pl_parse
413 {
414 my ($txt) = @_; my ($pl, $ent);
415 (defined($pl = pl_parse_list("($txt)"))) or return;
416 (pl_cook_list($pl)) or return;
417 foreach $ent (@$pl) {
418 if (!ref $ent) { return error("bareword found: ", $ent); }
419 }
420 return $pl;
421 }
422
423 ##<*> pl_parse_list($txt)
424 # Converts a text $txt of a Lispish list to a 'raw' pl-list.
425 sub pl_parse_list
426 {
427 my ($txt) = @_; my (@toks, $pp, $t, $swjis);
428 if (($swjis = $txt =~ m/\x1B\x24/)) { $txt = pl_conv_jis_out($txt); }
429 @toks = grep { $_ ne "" } (split(/(\()|(\))|\s+/, $txt));
430 if ($swjis) {
431 foreach (@toks) {
432 if (m/[\x80-\xff]/) { $_ = pl_conv_jis_in($_); }
433 }
434 }
435 if ($toks[0] ne '(') { return error("missing paren at top"); }
436 $pp = pl_corres_paren(\@toks, 0);
437 if ($pp == $#toks) {
438 return pl_parse_sub(\@toks, 1, $pp - 1);
439 } elsif ($pp < 0) {
440 return error("unmatched parens (end at level ", -$pp, ")");
441 } else {
442 return error("unmatched parens (extra tokens)");
443 }
444 }
445 sub pl_conv_jis_out {
446 my ($txt) = @_; my ($t, $pos, @cnks);
447 $pos = pos($txt) = 0;
448 while ($txt =~
449 m/(\x1B\x24[\x42\x40]([\x21-\x7E]+)\x1B\x28[\x42\x4A])/g) {
450 ($t = $2) =~ tr/\x21-\x7E/\xA1-\xFE/;
451 push(@cnks, substr($txt, $pos, pos($txt) - length($1) - $pos), $t);
452 $pos = pos($txt);
453 }
454 return join('', @cnks, substr($txt, $pos));
455 }
456 sub pl_conv_jis_in {
457 my ($txt) = @_; my ($t, $pos, @cnks);
458 $pos = pos($txt) = 0;
459 while ($txt =~ m/([\xA1-\xFE]+)/g) {
460 ($t = $1) =~ tr/\xA1-\xFE/\x21-\x7E/;
461 push(@cnks, substr($txt, $pos, pos($txt) - length($1) - $pos),
462 "\x1B\x24\x42$t\x1B\x28\x42");
463 $pos = pos($txt);
464 }
465 return join('', @cnks, substr($txt, $pos));
466 }
467
468 ## pl_adjust_lit_paren($pl)
469 sub pl_adjust_lit_paren
470 {
471 my ($pl) = @_; my ($mod, $repl, $lin, @lins);
472 @lins = split(m/\n/, $pl);
473 foreach (0 .. $#lins) {
474 $lin = $lins[$_];
475 if ($mod == 2) {
476 if ($lin =~ m/^\s*\)\s*$/) {
477 $lins[$_ - 1] =~ s/\)/X0029/; $repl = 1;
478 }
479 $mod = 0;
480 } elsif ($mod == 1) {
481 if ($lin =~ m/^\s*\)\s*$/) { $mod = 2; }
482 else {
483 if ($lin =~ m/\(/) { $lins[$_] =~ s/\(/X0028/g; $repl = 1; }
484 if ($lin =~ m/\)/) { $lins[$_] =~ s/\)/X0029/g; $repl = 1; }
485 if ($lin =~ m/\bU [\dA-F]{4}/) {
486 $lins[$_] =~ s/\bU ([\dA-F]{4})/U$1/g; $repl = 1;
487 }
488 }
489 }
490 if ($lin =~ m/^\(CHARSINTYPE /) { $mod = 1; }
491 }
492 return ($repl) ? join("\n", @lins) : $pl;
493 }
494
495 ## pl_parse_sub(...)
496 # Subcontractor of pl_parse_list.
497 sub pl_parse_sub
498 {
499 my ($toks, $sp, $ep) = @_; my (@pl, $tok, $p, $pp, $pl2);
500 for ($p = $sp; $p <= $ep; $p++) {
501 $tok = $toks->[$p];
502 if ($tok eq '(') {
503 $pp = pl_corres_paren($toks, $p);
504 ($p < $pp && $pp <= $ep) or return fatal(0);
505 (defined($pl2 = pl_parse_sub($toks, $p + 1, $pp - 1))) or return;
506 push(@pl, $pl2); $p = $pp;
507 } else {
508 push(@pl, $tok);
509 }
510 }
511 return \@pl;
512 }
513
514 ## pl_corres_paren($toks, $p)
515 # Returns the index of the ')' token which corresponds with the
516 # '(' token at index $p in an array $toks.
517 sub pl_corres_paren
518 {
519 my ($toks, $p) = @_; my ($tok, $lv);
520 for ($lv = 1, ++$p; $p <= $#$toks; $p++) {
521 $tok = $toks->[$p];
522 if ($tok eq '(') { ++$lv; }
523 elsif ($tok eq ')') { --$lv; }
524 if ($lv == 0) { last; }
525 }
526 return ($lv > 0) ? -$lv : $p;
527 }
528
529 ##<*> pl_cook_list($pl)
530 # Makes a raw pl-list $pl cooked.
531 sub pl_cook_list
532 {
533 my ($pl) = @_; my ($k, $ent, $res);
534 for ($k = 0; $k <= $#$pl; $k++) {
535 $ent = $pl->[$k];
536 if (ref $ent) {
537 if ($ent->[0] eq 'COMMENT') {
538 splice(@$pl, $k, 1); redo;
539 } elsif ($ent->[0] ne CNUM) {
540 (pl_cook_list($ent)) or return;
541 }
542 } elsif ($ent =~ /^[CKDFOHR]$/ && $k < $#$pl) {
543 (defined pl_proc_num($pl, $k)) or return;
544 ++$k;
545 }
546 }
547 return $pl;
548 }
549
550 ##<*> pl_form($pl, $ind)
551 # Converts a pl-struct $pl into a PL text. Here $ind is the amount
552 # of indent: if negative the result is in in-line form.
553 sub pl_form
554 {
555 my ($pl, $ind) = @_; my (@cnks, $ent, $txt);
556 foreach $ent (@$pl) {
557 (defined($txt = pl_form_list($ent, $ind))) or return;
558 push(@cnks, $txt);
559 }
560 if ($ind >= 0) { return join("\n", @cnks, ''); }
561 else { return join(' ', @cnks); }
562 }
563
564 # for pl_form_list
565 my $pl_rx_kent = qr/^[^\x20-\x7e]|^[JUX][0-9A-Fa-f]{4,6}$/;
566
567 ##<*> pl_form_list($pl, $ind)
568 # Converts a pl-list $pl into a Lispish list.
569 sub pl_form_list
570 {
571 my ($pl, $ind) = @_; my (@cnks, @lins, @toks);
572 my ($k, $t, $lsepp, $lsep, $ent, $tok, $txt);
573 if ($ind >= 0) {
574 push(@cnks, '('); $ind += 3;
575 $lsepp = $lsep = "\n" . ' ' x $ind;
576 } else { push(@cnks, '('); $lsepp = $lsep = ' '; }
577 for ($k = 0; $k <= $#$pl; $k++) {
578 $ent = $pl->[$k];
579 if (ref $ent) {
580 if ($ent->[0] eq CNUM) {
581 $tok = $pl->[$k + 1]; ++$k;
582 if (defined $tok) { push(@lins, $ent->[2], $tok); }
583 else {
584 (@toks = pl_form_num($ent->[2], $ent->[1])) or return;
585 push(@lins, @toks);
586 }
587 } else {
588 if (@lins) { push(@cnks, join(' ', @lins), $lsepp); }
589 (defined($txt = pl_form_list($ent, $ind))) or return;
590 @lins = (); push(@cnks, $txt, $lsep);
591 }
592 } elsif ($k > 0 && $ind >= 0 && $ent =~ m/$pl_rx_kent/) {
593 if (@lins) { push(@cnks, join(' ', @lins), $lsepp); }
594 $t = '';
595 while ($pl->[$k] =~ m/$pl_rx_kent/) {
596 if (length($t) + length($pl->[$k]) + $ind > 72) { last; }
597 $t .= $pl->[$k] . ' '; ++$k;
598 }
599 --$k; chop($t); @lins = (); push(@cnks, $t, $lsep);
600 } else { push(@lins, $ent); }
601 }
602 push(@cnks, join(' ', @lins), ')');
603 if ($ind < 0 && $cnks[-3] eq $lsep) { $cnks[-3] = ''; }
604 return join('', @cnks);
605 }
606
607 ##<*> pl_value($pl, $k, $sw)
608 # Reads the number at position $k in pl-list $pl. Note that
609 # $k is the position of form prefix and not the string represeting
610 # the nubmer itself.
611 # The value will be rounded to integers unless $sw is true.
612 sub pl_value
613 {
614 my ($pl, $k, $sw) = @_; my ($ent);
615 $ent = $pl->[$k];
616 if (ref $ent && $ent->[0] eq CNUM) {
617 return ($sw) ? $ent->[1] : round($ent->[1]);
618 }
619 return pl_proc_num($pl, $k);
620 }
621
622 ## pl_proc_num($pl, $k)
623 # Converts the number token at position $k in pl-list $pl to
624 # cooked form.
625 sub pl_proc_num
626 {
627 my ($pl, $k) = @_; my ($v, $fl, $tok);
628 ($fl, $tok) = ($pl->[$k], $pl->[$k + 1]);
629 if (defined($v = pl_parse_num($fl, $tok))) {
630 $pl->[$k] = [ CNUM, $v, $fl ]; return $v;
631 } else {
632 return error("malformed number (", $fl, " ", $tok, ")");
633 }
634 }
635
636 ##<*> pl_set_value($pl, $k, $v)
637 # Changes the number at position $k in pl-list $pl to $v.
638 sub pl_set_value
639 {
640 my ($pl, $k, $v) = @_; my ($ent);
641 $ent = $pl->[$k];
642 (ref $ent && $ent->[0] eq CNUM)
643 or return error("illegal assignment", $ent);
644 $pl->[$k + 1] = undef; $ent->[1] = $v;
645 return 1;
646 }
647
648 ##<*> pl_set_real($pl, $k)
649 # Changes the 'R'-form number at position $k in pl-list $pl to
650 # $v, which is a non-scaled value.
651 sub pl_set_real
652 {
653 my ($pl, $k, $v) = @_; my ($ent);
654 $ent = $pl->[$k];
655 (ref $ent && $ent->[0] eq CNUM && $ent->[2] eq 'R')
656 or return error("illegal assignment", $ent);
657 $pl->[$k + 1] = undef; $ent->[1] = $v * TU;
658 return 1;
659 }
660
661 ##<*> pl_set_value($pl, $k, $v)
662 # Changes the form prefix of the number at position $k in
663 # pl-list $pl to $v.
664 sub pl_set_numtype
665 {
666 my ($pl, $k, $v) = @_; my ($ent);
667 $ent = $pl->[$k];
668 (ref $ent && $ent->[0] eq CNUM)
669 or return error("illegal assignment", $ent);
670 $pl->[$k + 1] = undef; $ent->[2] = $v;
671 return 1;
672 }
673
674
675 ##<*> pl_prefer_hex($sw)
676 # Sets the value of the global flag $pl_prefer_hex. If its value is
677 # true, then 'H' (instead of 'O') is used as a fallback of 'D'/'C'.
678 our $pl_prefer_hex = 0;
679 sub pl_prefer_hex { $pl_prefer_hex = $_[0]; }
680
681 ## pl_parse_num($fl, $tok)
682 # Converts a number token to the number it expresses. Here $fl is
683 # a form prefix and $tok is a token,
684 sub pl_parse_num
685 {
686 my ($fl, $tok) = @_; my ($ll, $ul, $v);
687 $ll = 0; $ul = M32;
688 if (ref $tok) { return; }
689 if ($fl eq 'C') {
690 $v = ($tok =~ /^[\x21-\x7e]$/ && ord($tok));
691 } elsif ($fl eq 'K') {
692 $v = jcode_ord($tok);
693 } elsif ($fl eq 'D') {
694 $v = ($tok =~ /^-?\d+$/ && $tok); $ul = 255;
695 } elsif ($fl eq 'F') {
696 $v = (exists $pl_facecode{$tok} && $pl_facecode{$tok});
697 } elsif ($fl eq 'O') {
698 $v = ($tok =~ /^[0-7]+$/ && oct($tok)); $ul = M32;
699 } elsif ($fl eq 'H' || $fl eq 'I') {
700 $v = ($tok =~ /^[\da-f]+$/i && hex($tok)); $ul = M32;
701 } elsif ($fl eq 'R') {
702 $v = ($tok =~ /^[\+\-]?(\d*\.)?\d+$/ && pl_scale_real($tok));
703 $ll = - B31; $ul = M31;
704 }
705 if ($freenum && $fl ne 'R') { $ul = M32; $ll = -$ul; }
706 if (!($ll <= $v && $v <= $ul)) { $v = undef; }
707 return $v;
708 }
709
710 ## pl_form_num($fl, $v)
711 # Expresses the number $v in form $fl. If the number cannot (or
712 # should not) be expressed in form $fl, then $fl is fallen back
713 # to another suitable value. It returns ($fl, $tok) where $tok
714 # is the resulted expression and $fl is the possibly adapted
715 # value of form prefix.
716 sub pl_form_num
717 {
718 my ($fl, $v) = @_; my ($tok);
719 $tok = $fl;
720 if ($fl eq 'F' && $v > 17) { $fl = 'D'; }
721 if ($fl eq 'C' && !pl_isprint($v)) { $fl = 'I'; }
722 if ($fl eq 'K' && (!pl_isjprint($v))) { $fl = 'H'; }
723 if ($fl eq 'D' && $v > 255) { $fl = 'I'; }
724 if ($fl eq 'I') { $fl = ($pl_prefer_hex) ? 'H' : 'O'; }
725 if (($fl eq 'R' && !(- B31 <= $v && $v <= M31))
726 || ($fl ne 'R' && !(0 <= $v && $v <= M32))) {
727 return error("number is out of range ($v for $tok)");
728 }
729 if ($fl eq 'C') { $tok = chr($v); }
730 elsif ($fl eq 'K') { $tok = jcode_chr($v); }
731 elsif ($fl eq 'D') { $tok = $v; }
732 elsif ($fl eq 'F') { $tok = $pl_facecode_rev{$v}; }
733 elsif ($fl eq 'O') { $tok = sprintf("%o", $v); }
734 elsif ($fl eq 'H') { $tok = sprintf("%X", $v); }
735 elsif ($fl eq 'R') { $tok = pl_form_real($v / TU); }
736 return ($fl, $tok);
737 }
738
739 # Note: In PL, big numbers (>255) in decimal are not allowed,
740 # whereas they are allowed in VPL.
741
742 ## pl_form_real($a)
743 # Expresses a real number in the same way as in PL.
744 sub pl_form_real
745 {
746 my ($a) = @_; my ($d, @t);
747 if ($a < -0.5 / TU) { $a = -$a; @t = ("-"); }
748 $a = int($a * TU + 0.5);
749 { use integer;
750 push(@t, $a / TU, "."); $a %= TU;
751 $a = $a * 10 + 5; $d = 10;
752 do {
753 if ($d > 0x100000) { $a = $a + 0x80000 - $d / 2; }
754 push(@t, $a / 0x100000); $a = $a % 0x100000 * 10; $d *= 10;
755 } until ($a <= $d);
756 }
757 return join('', @t);
758 }
759
760 ## pl_scale_real($a)
761 # Returns a real value scaled to tfm-unit, rounded to integer.
762 sub pl_scale_real
763 {
764 my ($a) = @_;
765 return int($a * TU + (($a < 0) ? -0.5 : +0.5));
766 }
767
768
769 ## pl_isprint($c)
770 # Tests if the number $c is to output really in 'C' form.
771 sub pl_isprint
772 {
773 my ($c) = @_;
774 return (0 <= $c && $c <= 255 && chr($c) =~ /^\w/);
775 }
776
777 ## pl_isjprint($c)
778 # Tests if the number $c is to output in 'K' form.
779 sub pl_isjprint
780 {
781 my ($c) = @_;
782 return defined(jcode_chr($c));
783 }
784
785 ####---- Rearranging pl-structs
786
787 ##<*> pl_rearrange($pl)
788 # Sorts the pl-lists in a pl-struct $pl so that they are in
789 # 'usual' order.
790 sub pl_rearrange
791 {
792 my ($pl) = @_;
793 @$pl = sort { pl_prop_pos($a) <=> pl_prop_pos($b) } (@$pl);
794 return $pl;
795 }
796
797 # for pl_prop_pos
798 my %pl_prop_pos_base = (
799 # 0xZ0001 --> 0xZXXXXXX
800 # 0xZ0002 --> 0xZXX0000
801 # 0xZ0003 --> 0xZXXYYYY
802 DIRECTION => 0x0000010,
803 VTITLE => 0x0000011,
804 FAMILY => 0x0000012,
805 FACE => 0x0000013,
806 HEADER => 0x1000001,
807 CODINGSCHEME => 0x2000010,
808 DESIGNUNITS => 0x2000011,
809 DESIGNSIZE => 0x2000012,
810 CHECKSUM => 0x2000013,
811 SEVENBITSAFEFLAG => 0x2000014,
812 FONTDIMEN => 0x2000015,
813 BOUNDARYCHAR => 0x2000016,
814 MAPFONT => 0x3000001,
815 LIGTABLE => 0x4000010,
816 GLUEKERN => 0x4000010,
817 CODESPACE => 0x5000000,
818 CHARSINTYPE => 0x5000002,
819 CHARSINSUBTYPE => 0x5000003,
820 TYPE => 0x6000002,
821 SUBTYPE => 0x6000003,
822 CHARACTER => 0x7000001,
823 );
824
825 ## pl_prop_pos($pl)
826 # Subcontractor for pl_rearrange.
827 sub pl_prop_pos
828 {
829 my ($pl) = @_; my ($v, $u);
830 $v = $pl_prop_pos_base{$pl->[0]};
831 if (!defined $v) { return 0xFFFFFFF; }
832 $u = ($v & 0xffffff);
833 if ($u == 1) {
834 return ($v & 0xf000000) | pl_value($pl, 1);
835 } elsif ($u == 2) {
836 return ($v & 0xf000000) | (pl_value($pl, 1) << 16);
837 } elsif ($u == 3) {
838 return ($v & 0xf000000) | (pl_value($pl, 1) << 16) | pl_value($pl, 3);
839 } else { return $v; }
840 }
841
842 ####---- Rearranging pl-lists
843
844 ##<*> pl_clone($pl)
845 # Returns a deep clone of a pl-list, where the original and the
846 # clone share no reference.
847 sub pl_clone
848 {
849 my ($pl) = @_;
850 if (ref $pl eq "ARRAY") {
851 return [ map { pl_clone($_) } (@$pl) ];
852 } else { return $pl; }
853 }
854
855 ##<*> pl_sclone($pl)
856 # Returns a one-level clone of a pl-list, considering cooked
857 # number forms which should be uniquified.
858 sub pl_sclone
859 {
860 my ($pl) = @_;;
861 if (ref $pl eq "ARRAY") {
862 return [ map {
863 (ref $_ eq "ARRAY" && $_->[0] eq CNUM) ? [ @$_ ] : $_
864 } (@$pl) ];
865 } else { return $pl; }
866 }
867
868
869 ####---- Handling PL/JPL/OPL/VPL Structs
870
871 # The three functions below generate a header part (stuffs before
872 # FONTDIMEN and optionally FONTDIMEN). Here $in is a hash ref with
873 # the following effective keys:
874 # direction, family, vtitle, face, codingscheme, designunits,
875 # designsize, checksum, sevenbitsafeflag, boundarychar
876 # they each correspond with the property of the same name. Of them
877 # 'designsize' has the default value 10, but it can be cancelled
878 # by 'designsize' key with the explicit undef value.
879 # If $fd is not undef, it specifies FONTDIMEN list: if $fd is an
880 # array ref it is seen as pl-list of FONTDIMEN and placed inside
881 # the output pl-list; if $fd is a hash ref then pl_fontdimen($fd)
882 # is placed instead.
883 # Note: currently these three functions give the same result.
884
885 ##<*> pl_header($in, $fd)
886 sub pl_header
887 { return pl_header_gen($_[0], $_[1], 0); }
888
889 ##<*> pl_header_vpl($in, $fd)
890 sub pl_header_vpl
891 { return pl_header_gen($_[0], $_[1], 8); }
892
893 ##<*> pl_header_opl($in, $fd, $swl1)
894 sub pl_header_opl
895 { return pl_header_gen($_[0], $_[1], ($_[2]) ? 2 : 1); }
896
897 ## pl_header_gen($in, $fd, $sw)
898 # Subcontractor for the above three pl_header_* functions.
899 sub pl_header_gen
900 {
901 my ($in, $fd, $sw) = @_; my ($t, $pe, $dsiz, $ol, @pl);
902 if (exists $in->{ofmlevel}) { $ol = $in->{ofmlevel}; }
903 elsif ($sw == 1 || $sw == 2) { $ol = $sw - 1; }
904 $dsiz = (exists $in->{designsize}) ? $in->{designsize} : 10;
905 if (defined $ol) {
906 $pe = pl_cook(['OFMLEVEL', 'H', $ol]);
907 pl_set_value($pe, 1, $ol); push(@pl, $pe);
908 }
909 if (defined $in->{direction}) {
910 push(@pl, ['DIRECTION', $in->{direction}]);
911 }
912 if (defined $in->{family}) {
913 push(@pl, ['FAMILY', $in->{family}]);
914 }
915 if (defined $in->{vtitle}) {
916 push(@pl, ['VTITLE', $in->{vtitle}]);
917 }
918 if (defined $in->{face}) {
919 $pe = pl_cook(['FACE', 'F', 0]);
920 pl_set_value($pe, 1, $in->{face}); push(@pl, $pe);
921 }
922 if (defined $in->{codingscheme}) {
923 push(@pl, ['CODINGSCHEME', $in->{codingscheme}]);
924 }
925 if (defined $in->{designunits}) {
926 $pe = pl_cook(['DESIGNUNITS', 'R', 0]);
927 pl_set_real($pe, 1, $in->{designunits}); push(@pl, $pe);
928 }
929 if (defined $dsiz) {
930 $pe = pl_cook(['DESIGNSIZE', 'R', 0]);
931 pl_set_real($pe, 1, $dsiz); push(@pl, $pe);
932 }
933 if (defined $in->{checksum}) {
934 $pe = pl_cook(['CHECKSUM', 'O', 0]);
935 pl_set_value($pe, 1, $in->{checksum}); push(@pl, $pe);
936 }
937 if (defined $in->{sevenbitsafeflag}) {
938 push(@pl, ['SEVENBITSAFEFLAG', $in->{sevenbitsafeflag}]);
939 }
940 if (ref $fd eq 'ARRAY') {
941 push(@pl, $fd);
942 }
943 if (ref $fd eq 'HASH') {
944 push(@pl, pl_fontdimen($fd));
945 }
946 if (defined $in->{boundarychar}) {
947 $pe = pl_cook(['BOUNDARYCHAR', 'C', 0]);
948 pl_set_value($pe, 1, $in->{boundarychar}); push(@pl, $pe);
949 }
950 return \@pl;
951 }
952
953 ##<*> pl_fontdimen($in)
954 # Generates a FONTDIMEN list. $in is a hash ref with the following
955 # effective keys:
956 # slant, space, stretch, shrink, xheight, quad, extraspace;
957 # they each correspond with the property of the same name. All
958 # of them have a default value.
959 sub pl_fontdimen
960 {
961 my ($in) = @_; my ($q, $t, $pl);
962 (defined $in) or $in = { };
963 $pl = pl_cook(['FONTDIMEN',
964 ['SLANT', 'R', 0], ['SPACE', 'R', 0],
965 ['STRETCH', 'R', 0], ['SHRINK', 'R', 0],
966 ['XHEIGHT', 'R', 0], ['QUAD', 'R', 0],
967 ['EXTRASPACE', 'R', 0]]);
968 $q = $in->{quad}; (defined $q) or $q = 1;
969 pl_set_real($pl->[6], 1, $q);
970 $t = $in->{slant}; (defined $t) or $t = 0;
971 pl_set_real($pl->[1], 1, $t);
972 $t = $in->{space}; (defined $t) or $t = $q / 3;
973 pl_set_real($pl->[2], 1, $t);
974 $t = $in->{stretch}; (defined $t) or $t = $q / 6;
975 pl_set_real($pl->[3], 1, $t);
976 $t = $in->{shrink}; (defined $t) or $t = $q / 9;
977 pl_set_real($pl->[4], 1, $t);
978 $t = $in->{xheight}; (defined $t) or $t = $q / 2;
979 pl_set_real($pl->[5], 1, $t);
980 $t = $in->{extraspace}; (defined $t) or $t = $q / 9;
981 pl_set_real($pl->[7], 1, $t);
982 return $pl;
983 }
984
985 ##<*> pl_fontdimen_jpl($in)
986 # Generates a FONTDIMEN list of JPL. Here $in is the same as in
987 # pl_fontdimen, except that two additional keys 'extrastretch'
988 # and 'extrashrink' are used and default setting is different.
989 sub pl_fontdimen_jpl
990 {
991 my ($in) = @_; my ($q, $t, $pl);
992 (defined $in) or $in = { };
993 $pl = pl_cook(['FONTDIMEN',
994 ['SLANT', 'R', 0], ['SPACE', 'R', 0],
995 ['STRETCH', 'R', 0], ['SHRINK', 'R', 0],
996 ['XHEIGHT', 'R', 0], ['QUAD', 'R', 0],
997 ['EXTRASPACE', 'R', 0], ['EXTRASTRETCH', 'R', 0],
998 ['EXTRASHRINK', 'R', 0]]);
999 $q = $in->{quad}; (defined $q) or $q = 1;
1000 pl_set_real($pl->[6], 1, $q);
1001 $t = $in->{slant}; (defined $t) or $t = 0;
1002 pl_set_real($pl->[1], 1, $t);
1003 $t = $in->{space}; (defined $t) or $t = 0;
1004 pl_set_real($pl->[2], 1, $t);
1005 $t = $in->{stretch}; (defined $t) or $t = $q / 10;
1006 pl_set_real($pl->[3], 1, $t);
1007 $t = $in->{shrink}; (defined $t) or $t = 0;
1008 pl_set_real($pl->[4], 1, $t);
1009 $t = $in->{xheight}; (defined $t) or $t = $q;
1010 pl_set_real($pl->[5], 1, $t);
1011 $t = $in->{extraspace}; (defined $t) or $t = $q / 4;
1012 pl_set_real($pl->[7], 1, $t);
1013 $t = $in->{extrastretch}; (defined $t) or $t = $q / 5;
1014 pl_set_real($pl->[8], 1, $t);
1015 $t = $in->{extrashrink}; (defined $t) or $t = $q / 8;
1016 pl_set_real($pl->[9], 1, $t);
1017 return $pl;
1018 }
1019
1020 # for pl_fonrdimen_?pl_rmt
1021 our @pl_keys_quad_u_jpl = (
1022 0x4E00, 0x3042, 0x306E, 0xFF2D, 0x004D, 0x2014
1023 );
1024 our @pl_keys_quad_u_opl = (
1025 0x2001, 0x2003, 0x004D, 0x2014
1026 );
1027 our @pl_keys_space_u_opl = (
1028 0x0020, 0x00A0
1029 );
1030
1031 ##<*> pl_fontdimen_opl_rmt($rmt)
1032 # Generates a FONTDIMEN list of OPL with values estimated from
1033 # the glyph metric $rmt.
1034 sub pl_fontdimen_opl_rmt
1035 {
1036 my ($rmt) = @_; my ($t, $in);
1037 $in = { };
1038 (defined($t = pl_key_width($rmt, \@pl_keys_quad_u_opl)))
1039 and $in->{quad} = $t;
1040 (defined($t = pl_key_width($rmt, \@pl_keys_space_u_opl)))
1041 and $in->{space} = $t;
1042 return pl_fontdimen($in);
1043 }
1044
1045 ##<*> pl_fontdimen_jpl_rmt($rmt)
1046 # Generates a FONTDIMEN list of JPL with values estimated from
1047 # the glyph metric $rmt.
1048 sub pl_fontdimen_jpl_rmt
1049 {
1050 my ($rmt) = @_; my ($t, $in);
1051 $in = { };
1052 (defined($t = pl_key_width($rmt, \@pl_keys_quad_u_jpl)))
1053 and $in->{quad} = $t;
1054 return pl_fontdimen($in);
1055 }
1056
1057 ## pl_key_width($rmt)
1058 # Subcontractor of pl_fontdimen_?pl_rmt.
1059 sub pl_key_width
1060 {
1061 my ($rmt, $keys) = @_; my ($t, %hsh);
1062 foreach (@$rmt) { $hsh{$_->[0]} = $_; }
1063 foreach (@$keys) {
1064 (defined($t = $hsh{$_})) or next;
1065 return $t->[1];
1066 }
1067 return;
1068 }
1069
1070 ## pl_cook($pl)
1071 # Cooks a pl-list $pl and returns it. (Sometimes more convenient
1072 # than pl_cook_list.)
1073 *pl_cook = \&pl_cook_list;
1074
1075 # for pl_get_metric
1076 my %pl_char_part_pos = (
1077 CHARWD => 1, CHARHT => 2, CHARDP => 3, CHARIC => 4
1078 );
1079
1080 ##<*> pl_get_metric($pl)
1081 # Reads the metric data from the CHARACTER set of pl-struct $pl
1082 # and converts them to a raw metric array.
1083 sub pl_get_metric
1084 {
1085 my ($pl) = @_; my ($t, $p, $pe, $pe2, $ent, @rmt);
1086 foreach $pe (@$pl) {
1087 if ($pe->[0] eq 'CHARACTER') {
1088 (defined($t = pl_value($pe, 1))) or return;
1089 $ent = [ $t ];
1090 foreach $pe2 (@{$pe}[3 .. $#$pe]) {
1091 if (defined($p = $pl_char_part_pos{$pe2->[0]})) {
1092 (defined($t = pl_value($pe2, 1))) or return;
1093 $ent->[$p] = $t / TU;
1094 }
1095 }
1096 push(@rmt, $ent);
1097 }
1098 }
1099 return \@rmt;
1100 }
1101
1102 # for pl_char_part
1103 my @pl_char_part_name = qw( * CHARWD CHARHT CHARDP CHARIC );
1104
1105 ##<*> pl_char_part
1106 # Converts a raw metric array to an array of CHARACTER lists.
1107 # Partial inverse of pl_get_metric
1108 sub pl_char_part
1109 {
1110 my ($rmt) = @_; my ($ent, $pe, $pe2, $pl);
1111 $pl = [ ];
1112 foreach $ent (@$rmt) {
1113 $pe = pl_cook(['CHARACTER', 'C', 0]);
1114 pl_set_value($pe, 1, $ent->[0]);
1115 foreach (1 .. 4) {
1116 if (defined($ent->[$_])) {
1117 $pe2 = pl_cook([$pl_char_part_name[$_], 'R', 0]);
1118 pl_set_real($pe2, 1, $ent->[$_]); push(@$pe, $pe2);
1119 }
1120 }
1121 push(@$pl, $pe);
1122 }
1123 return $pl;
1124 }
1125
1126 ##<*> pl_char_part_jpl($imt, $vmt, $swu)
1127 # ...
1128 sub pl_char_part_jpl
1129 {
1130 my ($imt, $vmt, $swu) = @_; my ($t, $cc, $ti, $jp);
1131 my (@cit, $pl, $pe, @u);
1132 $jp = ($swu) ? 'U' : 'J';
1133 foreach $cc (keys %$imt) {
1134 push(@{$cit[$imt->{$cc}]}, $cc);
1135 }
1136 foreach $ti (1 .. $#cit) {
1137 @u = map { sprintf("%s%04X", $jp, $_) }
1138 (sort { $a <=> $b } (@{$cit[$ti]}));
1139 $pe = pl_cook(['CHARSINTYPE', 'D', 0, @u]);
1140 pl_set_value($pe, 1, $ti); push(@$pl, $pe);
1141 }
1142 foreach $ti (0 .. $#$vmt) {
1143 $pe = pl_cook(['TYPE', 'D', 0, ['CHARWD', 'R', 0],
1144 ['CHARHT', 'R', 0], ['CHARDP', 'R', 0]]);
1145 pl_set_value($pe, 1, $ti);
1146 pl_set_real($pe->[3], 1, $vmt->[$ti][0]);
1147 pl_set_real($pe->[4], 1, $vmt->[$ti][1]);
1148 pl_set_real($pe->[5], 1, $vmt->[$ti][2]);
1149 push(@$pl, $pe);
1150 }
1151 return $pl;
1152 }
1153
1154
1155 ######## 'jcode' section ########
1156
1157 our $jcode_ex = K_JIS;
1158 our $jcode_in = KI_JIS;
1159 our $jfm_forced_prefix;
1160
1161 my %jcode_ex_sym = ( jis => K_JIS, euc => K_EUC,
1162 sjis => K_SJIS, utf8 => K_UTF8,
1163 none => undef, xjis => K_XJIS );
1164 my %jcode_in_sym = ( jis => KI_JIS, unicode => KI_UNI,
1165 none => undef, xjis => KI_XJIS );
1166 # 'xjis' is for internal use
1167
1168 ##<*> jcode_set($xjc, $ijc)
1169 # Declares external [internal] Japanese code to be $xjc [$ijc],
1170 # which is a key of %jcode_ex_sym [%jcode_in_sym].
1171 sub jcode_set
1172 {
1173 my ($xjc, $ijc) = @_; my ($t);
1174 if (defined $xjc) {
1175 (exists $jcode_ex_sym{$xjc}) or return;
1176 $jcode_ex = $jcode_ex_sym{$xjc};
1177 }
1178 if (defined $ijc) {
1179 (exists $jcode_in_sym{$ijc}) or return;
1180 $jcode_in = $jcode_in_sym{$ijc};
1181 }
1182 #if (!defined $jcode_ex || !defined $jcode_in) {
1183 # $jcode_ex = $jcode_in = undef;
1184 #}
1185 return 1;
1186 }
1187
1188 ##<*> jcode_chr($cod)
1189 # Converts a code value of the internal code to a string containing
1190 # the character encoded in the external code.
1191 sub jcode_chr
1192 {
1193 my ($cod) = @_; my ($xs, $is);
1194 (defined $jcode_in && defined $jcode_ex) or return;
1195 (0 <= $cod && $cod <= 0xFFFF) or return;
1196 $xs = chr($cod >> 8) . chr($cod & 0xFF);
1197 eval {
1198 $is = decode($jcode_in, $xs, Encode::FB_CROAK);
1199 $xs = encode($jcode_ex, $is, Encode::FB_CROAK);
1200 };
1201 ($@ eq '' && length($is) == 1) or return;
1202 return $xs;
1203 }
1204
1205 ##<*> jcode_ord($xs)
1206 # Inverse of jcode_chr.
1207 sub jcode_ord
1208 {
1209 my ($xs) = @_; my ($is, $cod, $f);
1210 (defined $jcode_in && defined $jcode_ex) or return;
1211 if ($jcode_in eq KI_XJIS && $jcode_ex eq K_XJIS) {
1212 $xs =~ m/\x1B\x24\x42(..)\x1B\x28\x42/ or return;
1213 return unpack('n', $1);
1214 }
1215 eval {
1216 $is = decode($jcode_ex, $xs, Encode::FB_CROAK);
1217 (length($is) == 1) or die;
1218 };
1219 ($@ eq '') or return;
1220 if ($jcode_in eq KI_UNI) { return ord($is); }
1221 eval {
1222 $xs = encode($jcode_in, $is, Encode::FB_CROAK);
1223 };
1224 ($@ eq '' && $xs =~ m/^(.)(.)$/s) or return;
1225 return (ord($1) << 8 | ord($2));
1226 }
1227
1228 ######## 'kpse' section ########
1229
1230 our $kpse_init_done;
1231 our $kpse_delim;
1232 our %kpse_format_alias = (
1233 cmap => 'cmap files',
1234 );
1235
1236 ##<*> kpse($fnam, $opt)
1237 # Executes 'kpsewhich' for filename $fnam with option $opt.
1238 # If $opt is a scalar, it means the value for 'format' option.
1239 # If $opt is a hash ref, then the value for keys 'dpi', 'engine',
1240 # 'mode', 'progname' and 'format' corresponds with the value of the
1241 # option with same name and the boolean value for key 'mustexist'
1242 # corresponds with existence of 'must-exist' option. For 'option'
1243 # value, aliasing specified with %kpse_format_alias is done.
1244 sub kpse
1245 {
1246 my ($fnam, $opt) = @_; my ($cmd, $res);
1247 ($kpse_init_done || kpse_init()) or return undef;
1248 $opt = kpse_parse_option($opt, $fnam); $cmd = $cmd_name{kpsewhich};
1249 if (ref $opt eq 'ARRAY') { return kpse_manual($fnam, $opt); }
1250 $res = `$cmd $opt "$fnam"`; chomp($res);
1251 if (-f $res) { return $res; }
1252 else { # returns undef, not nothing
1253 error("kpse failed to find a file: $fnam"); return undef;
1254 }
1255 }
1256
1257 ##<*> kpse_init()
1258 # Initializes the kpse section of this module.
1259 sub kpse_init
1260 {
1261 my ($res, $cmd);
1262 (!defined $kpse_init_done)
1263 or return error("kpsewhich failure");
1264 $cmd = $cmd_name{kpsewhich};
1265 if (($res = `$cmd -show-path=tex`) eq '') {
1266 $kpse_init_done = 0;
1267 return error("kpsewhich failure");
1268 }
1269 if ($res =~ m/^\.\:/) { $kpse_delim = ':'; }
1270 elsif ($res =~ m/;/) { $kpse_delim = ';'; }
1271 else { $kpse_delim = ':'; }
1272 $kpse_init_done = 1;
1273 return 1;
1274 }
1275
1276 ## kpse_parse_option($opt)
1277 # Subcontractor of kpse.
1278 sub kpse_parse_option
1279 {
1280 my ($opt, $fnam) = @_; my ($o, $t, @copts);
1281 if (ref $opt eq 'ARRAY') { return $opt; } # for future extension
1282 elsif (ref $opt eq 'HASH') {
1283 foreach $o (qw(dpi engine mode progname)) {
1284 if (exists $opt->{$o}) {
1285 push(@copts, "-$o=" . $opt->{$o});
1286 }
1287 }
1288 if ($opt->{mustexist}) { push(@copts, '-must-exist'); }
1289 $opt = $opt->{format};
1290 }
1291 if ($opt eq '' && $fnam =~ m/\.vf$/i) { $opt = "vf"; }
1292 if ($opt ne '') {
1293 if (defined($t = $kpse_format_alias{$opt})) { $opt = $t; }
1294 push(@copts, "-format=\"$opt\"");
1295 }
1296 return join(' ', @copts);
1297 }
1298
1299 ######## 'vf' section ########
1300
1301 ##<*> vf_strict($sw)
1302 # Sets strict mode in parsing or forming VF.
1303 our $vf_strict = 1;
1304 sub vf_strict { $vf_strict = $_[0]; }
1305
1306 ## vf_simple_move_code($sw)
1307 # Sets the value of $vf_simple_move_code. If it is true, then
1308 # vf_form does not exploit w, x, y, z registers in compiling
1309 # move operations in DVI code.
1310 our $vf_simple_move_code = 0;
1311 sub vf_simple_move_code { $vf_simple_move_code = $_[0]; }
1312
1313 ##-------- Procedures on ZVP0 format
1314
1315 ##<*> vf_parse($dat, $swdh)
1316 # Converts a (binary) VF data $dat to a pl-struct describing
1317 # ZPL0 data. If something invalid is found in DVI code and $swdh
1318 # is true, then DVI is written with a DIRECTHEX entry.
1319 sub vf_parse
1320 {
1321 my ($dat, $swdh) = @_;
1322 my ($t, $u, @fs, $pos, $pl, $pe, $stg);
1323 (defined $swdh) or $swdh = !$vf_strict;
1324 (length($dat) >= 3) or return vf_synerror("in preamble");
1325 @fs = unpack("CCC/a*NN", $dat); $pos = length($fs[2]) + 11;
1326 ($#fs == 4 && $fs[0] == 247 && $fs[1] == 202)
1327 or return vf_synerror("in preamble");
1328 $pl = pl_header_vpl({ vtitle => $fs[2], checksum => $fs[3],
1329 designsize => $fs[4] / TU });
1330 for (;;) {
1331 $t = ord(substr($dat, $pos, 1));
1332 if ($stg <= 2 && 0 <= $t && $t <= 241) { # short_charN
1333 @fs = unpack("CCa3a$t", substr($dat, $pos, $t + 5)); $pos += $t + 5;
1334 ($#fs == 3 && length($fs[3]) == $t)
1335 or return vf_synerror("premature end");
1336 $pe = pl_cook(['CHARACTER', 'C', 0,
1337 ['CHARWD', 'R', 0], undef]);
1338 if (defined($t = vf_dvi_parse($fs[3]))) { $pe->[4] = $t; }
1339 elsif (!$swdh) {
1340 return vf_synerror("illegal dvi code (char $fs[1])");
1341 } else { $pe->[4] = vf_dvi_dumb_parse($u); }
1342 pl_set_value($pe->[3], 1, unpack_num($fs[2])); # (unsigned)
1343 pl_set_value($pe, 1, $fs[1]);
1344 $stg = 2; push(@$pl, $pe);
1345 } elsif ($stg <= 2 && $t == 242) { # long_char
1346 @fs = unpack("CNNN", substr($dat, $pos, 13)); $pos += 13;
1347 $u = substr($dat, $pos, $fs[1]); $pos += $fs[1];
1348 #-- give a cooked list for efficiency
1349 #$pe = pl_cook(['CHARACTER', 'C', 0,
1350 # ['CHARWD', 'R', 0], undef]);
1351 $pe = (['CHARACTER', [CNUM, 0, 'C'], 0,
1352 ['CHARWD', [CNUM, 0, 'R'], 0], undef]);
1353 if (defined($t = vf_dvi_parse($u))) { $pe->[4] = $t; }
1354 elsif (!$swdh) {
1355 return vf_synerror("illegal dvi code (char $fs[2])");
1356 } else { $pe->[4] = vf_dvi_dumb_parse($u); }
1357 pl_set_value($pe->[3], 1, signed($fs[3]));
1358 pl_set_value($pe, 1, $fs[2]);
1359 $stg = 2; push(@$pl, $pe);
1360 } elsif ($stg <= 1 && 243 <= $t && $t <= 246) { # fnt_defN
1361 $t -= 242;
1362 @fs = unpack("Ca${t}NNNCC", substr($dat, $pos, $t + 15)); $pos += $t + 15;
1363 ($#fs == 6) or return vf_synerror("premature end");;
1364 $t = $fs[5] + $fs[6]; $u = substr($dat, $pos, $t); $pos += $t;
1365 (length($u) == $t) or return vf_synerror("premature end");;
1366 $fs[6] = substr($u, $fs[5]); $fs[5] = substr($u, 0, $fs[5]);
1367 $pe = pl_cook(['MAPFONT', 'D', 0, ['FONTAREA', 0],
1368 ['FONTNAME', 0],, ['FONTCHECKSUM', 'O', 0],
1369 ['FONTAT', 'R', 0], ['FONTDSIZE', 'R', 0]]);
1370 $pe->[3][1] = $fs[5]; $pe->[4][1] = $fs[6];
1371 pl_set_value($pe->[5], 1, $fs[2]);
1372 pl_set_value($pe->[6], 1, $fs[3]);
1373 pl_set_value($pe->[7], 1, $fs[4]);
1374 pl_set_value($pe, 1, unpack_num($fs[1]));
1375 if ($fs[5] eq '') { splice(@$pe, 3, 1); }
1376 $stg = 1; push(@$pl, $pe);
1377 } elsif ($stg <= 2 && $t == 248) { # post
1378 (($u = substr($dat, $pos, $t)) =~ /^\xf8+$/)
1379 or return vf_synerror("in postamble");
1380 $stg = 3;
1381 last;
1382 } else { return vf_synerror("unexpected byte $t"); }
1383 }
1384 ($swdh || $stg == 3) or return vf_synerror("premature end");
1385 return $pl;
1386 }
1387
1388 ## vf_dvi_parse($dat)
1389 # Subcontractor of vf_parse.
1390 sub vf_dvi_parse
1391 {
1392 my ($dat) = @_;
1393 my ($t, $u, @fs, $pos, $pl, $pe, $stk, $stg);
1394 $pl = ['MAP']; $stk = [{}];
1395 for ($pos = 0; $pos < length($dat); ) {
1396 $t = ord(substr($dat, $pos, 1));
1397 if (0 <= $t && $t <= 127) { # set_charN
1398 $pe = pl_cook(['SETCHAR', 'C', 0]); $pos += 1;
1399 pl_set_value($pe, 1, $t); push(@$pl, $pe);
1400 } elsif (128 <= $t && $t <= 131) { # setN
1401 $t -= 127; @fs = unpack("Ca$t", substr($dat, $pos));
1402 $pos += $t + 1; ($#fs == 1) or return;
1403 $pe = pl_cook(['SETCHAR', 'C', 0]);
1404 pl_set_value($pe, 1, unpack_num($fs[1])); push(@$pl, $pe);
1405 } elsif ($t == 132) { # setrule
1406 @fs = unpack("CNN", substr($dat, $pos));
1407 $pos += 9; ($#fs == 2) or return;
1408 $pe = pl_cook(['SETRULE', 'R', 0, 'R', 0]);
1409 pl_set_value($pe, 1, signed($fs[1]));
1410 pl_set_value($pe, 3, signed($fs[2])); push(@$pl, $pe);
1411 } elsif ($t == 141) { # push
1412 $pos += 1; push(@$pl, ['PUSH']); push(@$stk, {});
1413 } elsif ($t == 142) { # pop
1414 $pos += 1; push(@$pl, ['POP']); pop(@$stk);
1415 (@$stk) or return;
1416 } elsif (143 <= $t && $t <= 146) { # rightN
1417 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'r', $t - 142);
1418 (defined $pe) or return; push(@$pl, $pe);
1419 } elsif ($t == 147) { # w0
1420 ($pe, $pos) = vf_dvi_move0($dat, $pos, $stk, 'w');
1421 (defined $pe) or return; push(@$pl, $pe);
1422 } elsif (148 <= $t && $t <= 151) { # wN
1423 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'w', $t - 147);
1424 (defined $pe) or return; push(@$pl, $pe);
1425 } elsif ($t == 152) { # x0
1426 ($pe, $pos) = vf_dvi_move0($dat, $pos, $stk, 'x');
1427 (defined $pe) or return; push(@$pl, $pe);
1428 } elsif (153 <= $t && $t <= 156) { # xN
1429 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'x', $t - 152);
1430 (defined $pe) or return; push(@$pl, $pe);
1431 } elsif (157 <= $t && $t <= 160) { # downN
1432 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'd', $t - 156);
1433 (defined $pe) or return; push(@$pl, $pe);
1434 } elsif ($t == 161) { # y0
1435 ($pe, $pos) = vf_dvi_move0($dat, $pos, $stk, 'y');
1436 (defined $pe) or return; push(@$pl, $pe);
1437 } elsif (162 <= $t && $t <= 165) { # yN
1438 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'y', $t - 161);
1439 (defined $pe) or return; push(@$pl, $pe);
1440 } elsif ($t == 166) { # z0
1441 ($pe, $pos) = vf_dvi_move0($dat, $pos, $stk, 'z');
1442 (defined $pe) or return; push(@$pl, $pe);
1443 } elsif (167 <= $t && $t <= 170) { # zN
1444 ($pe, $pos) = vf_dvi_move1($dat, $pos, $stk, 'z', $t - 166);
1445 (defined $pe) or return; push(@$pl, $pe);
1446 } elsif (171 <= $t && $t <= 234) { # fnt_numN
1447 $t -= 171; $pe = pl_cook(['SELECTFONT', 'D', 0]); $pos += 1;
1448 pl_set_value($pe, 1, $t); push(@$pl, $pe);
1449 } elsif (235 <= $t && $t <= 238) { # fntN
1450 $t -= 234; @fs = unpack("Ca$t", substr($dat, $pos));
1451 $pos += $t + 1; ($#fs == 1) or return;
1452 $pe = pl_cook(['SELECTFONT', 'D', 0]);
1453 pl_set_value($pe, 1, unpack_num($fs[1])); push(@$pl, $pe);
1454 } elsif (239 <= $t && $t <= 242) { # xxxN
1455 $t -= 238; @fs = unpack("Ca$t", substr($dat, $pos));
1456 $pos += $t + 1; ($#fs == 1) or return;
1457 $t = unpack_num($fs[1]); $u = substr($dat, $pos, $t);
1458 $pos += $t; ($t == length($u)) or return;
1459 $pe = vf_dvi_special($u); push(@$pl, $pe);
1460 } elsif ($t == 255) { # dir (JVF)
1461 @fs = unpack("CC", substr($dat, $pos));
1462 $pos += 2; ($#fs == 1) or return;
1463 $pe = pl_cook(['DIR', 'D', 0]);
1464 pl_set_value($pe, 1, $fs[1]); push(@$pl, $pe);
1465 } else { return; }
1466 }
1467 return $pl;
1468 }
1469
1470 ## vf_synerror($msg)
1471 # Error messages in vf_parse.
1472 sub vf_synerror
1473 {
1474 return error("VF syntax error: $_[0]");
1475 }
1476
1477 # for vf_dvi_move1 / vf_dvi_move0
1478 my %vf_dvi_move = (
1479 r => 'MOVERIGHT', w => 'MOVERIGHT', x => 'MOVERIGHT',
1480 d => 'MOVEDOWN', y => 'MOVEDOWN', z => 'MOVEDOWN',
1481 );
1482
1483 ## vf_dvi_move1(...)
1484 # Subcontractor of vf_dvi_parse.
1485 sub vf_dvi_move1
1486 {
1487 my ($dat, $pos, $stk, $r, $l) = @_; my ($t, $pe, @fs);
1488 @fs = unpack("Ca$l", substr($dat, $pos));
1489 $pos += $l + 1; ($#fs == 1) or return;
1490 $stk->[-1]{$r} = $t = unpack_snum($fs[1]);
1491 $pe = pl_cook([$vf_dvi_move{$r}, 'R', 0]);
1492 pl_set_value($pe, 1, $t);
1493 return ($pe, $pos);
1494 }
1495
1496 ## vf_dvi_move0(...)
1497 # Subcontractor of vf_dvi_parse.
1498 sub vf_dvi_move0
1499 {
1500 my ($dat, $pos, $stk, $r) = @_; my ($t, $pe);
1501 (defined($t = $stk->[-1]{$r})) or return;
1502 $pe = pl_cook([$vf_dvi_move{$r}, 'R', 0]);
1503 pl_set_value($pe, 1, $t);
1504 return ($pe, $pos + 1);
1505 }
1506
1507 ## vf_dvi_special($dat)
1508 # Subcontractor of vf_dvi_parse.
1509 sub vf_dvi_special
1510 {
1511 my ($dat) = @_; my ($t, $u, $pl); local ($errmsg);
1512 L1:{
1513 $t = "($dat)"; ($t !~ m/[^\x20-\x7e]/) or last;
1514 (defined($pl = pl_parse_list($t))) or last;
1515 (vf_issafe_list($pl)) or last;
1516 $u = pl_form_list($pl, -1);
1517 return ['SPECIAL', $dat];
1518 }
1519 return ['SPEICALHEX', uc(unpack('H*', $dat))];
1520 }
1521
1522 ## vf_issafe_list($pl)
1523 # Subcontractor of vf_dvi_special.
1524 sub vf_issafe_list
1525 {
1526 my ($pl) = @_; my ($ent);
1527 foreach $ent (@$pl) {
1528 if (ref $ent) {
1529 (vf_issafe_list($ent)) or return 0;
1530 } elsif ($ent =~ /^[CKDFOHR]$/ || $ent eq 'COMMENT') {
1531 return 0;
1532 }
1533 }
1534 return 1;
1535 }
1536
1537 ## vf_dvi_dumb_parse($dat)
1538 # Subcontractor of dvi_parse.
1539 sub vf_dvi_dumb_parse
1540 {
1541 my ($dat) = @_;
1542 my ($t);
1543 $t = uc(unpack("H*", $dat));
1544 return ['MAP', ['DIRECTHEX', $t]];
1545 }
1546
1547 ##<*> vf_form($pl)
1548 # Inverse of vf_parse.
1549 sub vf_form
1550 {
1551 my ($pl) = @_;
1552 my ($t, $u, $v, $pe, @fs, @chds, @cfds, @ccps);
1553 @chds = (247, 202, "", 0, 10 * TU);
1554 foreach $pe (@$pl) {
1555 if ($pe->[0] eq 'VTITLE') {
1556 ($#$pe <= 1) or return vf_fsynerror("bad argument", $pe);
1557 (length($pe->[1]) <= 255)
1558 or return vf_fsynerror("VTITLE string too long", $pe->[1]);
1559 $chds[2] = $pe->[1];
1560 } elsif ($pe->[0] eq 'CHECKSUM') {
1561 ($#$pe == 2 && defined($t = pl_value($pe, 1)))
1562 or return vf_fsynerror("bad argument", $pe);
1563 $chds[3] = $t;
1564 } elsif ($pe->[0] eq 'DESIGNSIZE') {
1565 ($#$pe == 2 && defined($t = pl_value($pe, 1)))
1566 or return vf_fsynerror("bad argument", $pe);
1567 $chds[4] = $t;
1568 } elsif ($pe->[0] eq 'MAPFONT') {
1569 ($#$pe >= 2 && defined($t = pl_value($pe, 1)))
1570 or return vf_fsynerror("bad argument", $pe);
1571 ($u, $t) = pack_num($t);
1572 @fs = ($u + 242, $t, 0, TU, 10 * TU, 0, 0, '', '');
1573 foreach $pe (@{$pe}[3 .. $#$pe]) {
1574 if (!ref $pe) {
1575 return vf_fsynerror("unexpected bareword", $pe);
1576 } elsif ($pe->[0] eq 'FONTCHECKSUM') {
1577 ($#$pe == 2 && defined($fs[2] = pl_value($pe, 1)))
1578 or return vf_fsynerror("bad argument", $pe);
1579 } elsif ($pe->[0] eq 'FONTAT') {
1580 ($#$pe == 2 && defined($fs[3] = pl_value($pe, 1)))
1581 or return vf_fsynerror("bad argument", $pe);
1582 } elsif ($pe->[0] eq 'FONTDSIZE') {
1583 ($#$pe == 2 && defined($fs[4] = pl_value($pe, 1)))
1584 or return vf_fsynerror("bad argument", $pe);
1585 } elsif ($pe->[0] eq 'FONTAREA') {
1586 ($#$pe == 1 && length($pe->[1]) <= 255)
1587 or return vf_fsynerror("bad argument", $pe);
1588 $fs[7] = $pe->[1]; $fs[5] = length($pe->[1]);
1589 } elsif ($pe->[0] eq 'FONTNAME') {
1590 ($#$pe == 1 && length($pe->[1]) <= 255)
1591 or return vf_fsynerror("bad argument", $pe);
1592 $fs[8] = $pe->[1]; $fs[6] = length($pe->[1]);
1593 } elsif (!$vf_strict) {
1594 return vf_fsynerror("unknown property", $pe);
1595 }
1596 }
1597 push(@cfds, pack("Ca*NNNCCa*a*", @fs));
1598 } elsif ($pe->[0] eq 'CHARACTER') {
1599 ($#$pe >= 2 && defined($t = pl_value($pe, 1)))
1600 or return vf_fsynerror("bad argument", $pe);
1601 $v = 0; $u = undef;
1602 foreach $pe (@{$pe}[3 .. $#$pe]) {
1603 if (!ref $pe) {
1604 return vf_fsynerror("unexpected bareword", $pe);
1605 } elsif ($pe->[0] eq 'CHARWD') {
1606 ($#$pe == 2 && defined($v = pl_value($pe, 1)))
1607 or return vf_fsynerror("bad argument", $pe);
1608 } elsif ($pe->[0] eq 'MAP') {
1609 (defined($u = vf_dvi_form($pe, $t))) or return;
1610 } elsif (!$vf_strict) {
1611 return vf_fsynerror("unknown property", $pe);
1612 }
1613 }
1614 if (!defined $u) {
1615 $u = pl_cook(['MAP', ['SETCHAR']]);
1616 (defined($u = vf_dvi_form($u, $t))) or return;
1617 }
1618 if (0 <= $t && $t <= 255 && 0 <= $v && $v <= 0xFFFFFF &&
1619 length($u) <= 241) { # short form
1620 @fs = (length($u), $t, substr(pack('N', $v), 1), $u);
1621 push(@ccps, pack("CCa3a*", @fs));
1622 } else {
1623 @fs = (242, length($u), $t, $v, $u);
1624 push(@ccps, pack("CNNNa*", @fs));
1625 }
1626 } elsif ($vf_strict) {
1627 return vf_fsynerror("unknown property", $pe);
1628 }
1629 }
1630 $t = pack("CCC/a*NN", @chds);
1631 $t = join('', $t, @cfds, @ccps);
1632 $t .= "\xf8" x (4 - length($t) % 4);
1633 return $t;
1634 }
1635
1636 ## vf_dvi_form($pl, $cc)
1637 # Subcontractor of vf_form
1638 sub vf_dvi_form
1639 {
1640 my ($pl, $cc) = @_;
1641 my ($t, $u, $v, $l, $pe, $stk, @cnks);
1642 $stk = [{}];
1643 foreach $pe (@{$pl}[1 .. $#$pl]) {
1644 if ($pe->[0] eq 'SETCHAR') {
1645 if ($#$pe == 0) { $t = $cc; }
1646 elsif ($#$pe == 2 && defined($t = pl_value($pe, 1))) {
1647 } else { return vf_fsynerror("bad argument", $pe); }
1648 if (0 <= $t && $t <= 127) {
1649 push(@cnks, chr($t));
1650 } else {
1651 ($l, $t) = pack_num($t);
1652 push(@cnks, pack("Ca*", $l + 127, $t));
1653 }
1654 } elsif ($pe->[0] eq 'SETRULE') {
1655 ($#$pe == 4 && defined($t = pl_value($pe, 1)) &&
1656 defined($u = pl_value($pe, 3)))
1657 or return vf_fsynerror("bad argument", $pe);
1658 push(@cnks, pack("CNN", 132, $t, $u));
1659 } elsif ($pe->[0] eq 'PUSH') {
1660 push(@$stk, {}); push(@cnks, chr(141));
1661 } elsif ($pe->[0] eq 'POP') {
1662 pop(@$stk); (@$stk) or vf_fsynerror("cannot POP (char $cc)");
1663 push(@cnks, chr(142));
1664 } elsif ($pe->[0] eq 'MOVERIGHT') {
1665 (defined($t = vf_dvi_f_move($pe, $stk->[-1], 'r',+1))) or return;
1666 push(@cnks, $t);
1667 } elsif ($pe->[0] eq 'MOVELEFT') {
1668 (defined($t = vf_dvi_f_move($pe, $stk->[-1], 'r',-1))) or return;
1669 push(@cnks, $t);
1670 } elsif ($pe->[0] eq 'MOVEDOWN') {
1671 (defined($t = vf_dvi_f_move($pe, $stk->[-1], 'd',+1))) or return;
1672 push(@cnks, $t);
1673 } elsif ($pe->[0] eq 'MOVEUP') {
1674 (defined($t = vf_dvi_f_move($pe, $stk->[-1], 'd',-1))) or return;
1675 push(@cnks, $t);
1676 } elsif ($pe->[0] eq 'SELECTFONT') {
1677 ($#$pe == 2 && defined($t = pl_value($pe, 1)))
1678 or return vf_fsynerror("bad argument", $pe);
1679 if (0 <= $t && $t <= 63) {
1680 push(@cnks, chr($t + 171));
1681 } else {
1682 ($l, $t) = pack_num($t);
1683 push(@cnks, pack("Ca*", $l + 234, $t));
1684 }
1685 } elsif ($pe->[0] eq 'SPECIAL') {
1686 $t = pl_form_list($pe, -1);
1687 ($t =~ m|^\(SPECIAL\s?(.*)\)$|) or return fatal("vf_dvi_form");
1688 $u = $1; ($l, $t) = pack_num(length($u));
1689 push(@cnks, pack("Ca*a*", 238 + $l, $t, $u));
1690 } elsif ($pe->[0] eq 'SPECIALHEX') {
1691 ($u = join('', @{$pe}[1 .. $#$pl])) =~ s/\s+//g;
1692 ($u =~ m/^([0-9A-Fa-f]{2})+$/)
1693 or return vf_fsynerror("bad arguments", $pe);
1694 $u = pack("H*", $u); ($l, $t) = pack_num(length($u));
1695 push(@cnks, pack("Ca*a*", 238 + $l, $t, $u));
1696 } elsif ($pe->[0] eq 'DIR') {
1697 ($#$pe == 2 && defined($t = pl_value($pe, 1)) && $t <= 1)
1698 or return vf_fsynerror("bad argument", $pe);
1699 push(@cnks, pack("CC", 255, $t));
1700 } elsif ($pe->[0] eq 'DIRECTHEX') {
1701 ($u = join('', @{$pe}[1 .. $#$pl])) =~ s/\s+//g;
1702 ($u =~ m/^([0-9A-Fa-f]{2})+$/)
1703 or return vf_fsynerror("bad arguments", $pe);
1704 $u = pack("H*", $u); push(@cnks, $u);
1705 } else {
1706 return vf_fsynerror("unknown DVI operator: ", $pe);
1707 }
1708 }
1709 return join('', @cnks);
1710 }
1711
1712 ## vf_dvi_f_move($pe, $stk, $r, $sgn)
1713 # Subcontractor of vf_dvi_form.
1714 sub vf_dvi_f_move
1715 {
1716 my ($pe, $stk, $r, $sgn) = @_; my ($v, $l, $t, $w, $x, $b);
1717 ($#$pe == 2 && defined($v = pl_value($pe, 1)))
1718 or return vf_fsynerror("bad argument", $pe);
1719 if ($sgn < 0) { $v = -$v; }
1720 ($l, $t) = pack_snum($v);
1721 ($w, $x, $b) = ($r eq 'r') ? ('w', 'x', 142) : ('y', 'z', 156);
1722 if ($vf_simple_move_code) { $t = pack("Ca*", $b + $l, $t); }
1723 elsif (!exists $stk->{$w}) {
1724 $stk->{$w} = $v; $t = pack("Ca*", $b + $l + 5, $t);
1725 } elsif ($stk->{$w} == $v) { $t = chr($b + 5); }
1726 elsif (!exists $stk->{$x}) {
1727 $stk->{$x} = $v; $t = pack("Ca*", $b + $l + 10, $t);
1728 } elsif ($stk->{$x} == $v) { $t = chr($b + 10); }
1729 else { $t = pack("Ca*", $b + $l, $t); }
1730 return $t;
1731 }
1732
1733 ## vf_fsynerror($msg)
1734 # Error messages in vf_form.
1735 sub vf_fsynerror
1736 {
1737 my ($msg, $pl) = @_;
1738 if (ref $pl) { $pl = pl_form_list($pl, -1); }
1739 return error("VPL syntax error: $msg: $pl");
1740 }
1741
1742 ##<*> vf_for_mapping($map, $fn, $rmt)
1743 #
1744 sub vf_for_mapping
1745 {
1746 my ($map, $fn, $rmt) = @_; my ($e, $pe, $pe2, $pl, %hrmt);
1747 if (defined $rmt) {
1748 foreach (@$rmt) { $hrmt{$_->[0]} = $_; }
1749 }
1750 (defined($map = arraymap($map))) or return;
1751 $pl = pl_header_vpl({});
1752 push(@$pl, pl_cook(['MAPFONT', 'D', 0, ['FONTNAME', $fn]]));
1753 foreach $e (@$map) {
1754 if (defined $rmt && !defined $hrmt{$e->[0]}) { next; }
1755 $pe = pl_cook(['CHARACTER', 'C', 0,
1756 ['MAP', ['SETCHAR', 'C', 0]]]);
1757 pl_set_value($pe, 1, $e->[0]);
1758 pl_set_value($pe->[3][1], 1, $e->[1]);
1759 if (defined $rmt) {
1760 $pe2 = pl_cook(['CHARWD', 'R', 0]);
1761 pl_set_real($pe2, 1, $hrmt{$e->[0]}[1]);
1762 splice(@$pe, 3, 0, $pe2);
1763 }
1764 push(@$pl, $pe);
1765 }
1766 return $pl;
1767 }
1768
1769 ##<*> vf_mapfont($pl, $fn)
1770 # Returns the FONTNAME value of MAPFONT id $fn in VPL $pl.
1771 # If $fn is undef then it returns ref to the hash that maps
1772 # id to fontmame.
1773 sub vf_mapfont
1774 {
1775 my ($pl, $fn) = @_; my ($t, $pe, $pe2, %hsh);
1776 foreach $pe (@$pl) {
1777 if ($pe->[0] eq 'MAPFONT') {
1778 ($pe2) = grep { $_->[0] eq 'FONTNAME' } (@{$pe}[3 .. $#$pe]);
1779 (defined $pe2) or next;
1780 if (!defined $fn) {
1781 $hsh{pl_value($pe, 1)} = $pe2->[1];
1782 } elsif (defined($t = pl_value($pe, 1)) && $t == $fn) {
1783 return $pe2->[1];
1784 }
1785 }
1786 }
1787 return (defined $fn) ? () : \%hsh;
1788 }
1789
1790 ##<*> vf_set_mapfont($pl, $fn, $fnam)
1791 # Sets the FONTNAME value of MAPFONT id $fn to $fname
1792 # in VPL $pl.
1793 sub vf_set_mapfont
1794 {
1795 my ($pl, $fn, $fnam) = @_; my ($t, $pe, $pe2, %hsh);
1796 foreach $pe (@$pl) {
1797 if ($pe->[0] eq 'MAPFONT' &&
1798 defined($t = pl_value($pe, 1)) && $t == $fn) {
1799 ($pe2) = grep { $_->[0] eq 'FONTNAME' } (@{$pe}[3 .. $#$pe]);
1800 (defined $pe2) or return 0;
1801 $pe2->[1] = $fnam; return 1;
1802 }
1803 }
1804 return 0;
1805 }
1806
1807 ##-------- Procedures on ZVP format
1808
1809 my %vf_zvp_category = (
1810 # 1: to JPL, 2: to ZVP0, 3: both, 0: special
1811 DIRECTION => 1,
1812 VTITLE => 2,
1813 FAMILY => 1,
1814 FACE => 1,
1815 HEADER => 1,
1816 CODINGSCHEME => 1,
1817 DESIGNUNITS => 1,
1818 DESIGNSIZE => 3,
1819 CHECKSUM => 3,
1820 SEVENBITSAFEFLAG => 1,
1821 FONTDIMEN => 1,
1822 BOUNDARYCHAR => 1,
1823 MAPFONT => 2,
1824 LIGTABLE => 1,
1825 GLUEKERN => 0,
1826 CODESPACE => 0,
1827 CHARSINTYPE => 0,
1828 CHARSINSUBTYPE => 0,
1829 TYPE => 0,
1830 SUBTYPE => 0,
1831 CHARACTER => 0,
1832 );
1833 my %vf_zvp_category_char = (
1834 CHARWD => 0,
1835 CHARHT => 2,
1836 CHARDP => 3,
1837 CHARIC => 4,
1838 );
1839
1840 ##<*> debug_vf_form($val)
1841 our ($debug_vf_form);
1842 sub debug_vf_form { $debug_vf_form = $_[0]; }
1843
1844 ##<*> vf_form_ex($pl)
1845 # Converts ZPL $pl to VF $vf and TFM $tfm and returns pair
1846 # ($vf, $tfm).
1847 sub vf_form_ex
1848 {
1849 my ($pl) = @_; my ($plv, $plt, $vf, $tfm);
1850 (($plv, $plt) = vf_divide_zvp($pl)) or return;
1851 if ($debug_vf_form) {
1852 return (pl_form($plv), pl_form($plt));
1853 }
1854 (defined($vf = vf_form($plv))) or return;
1855 (defined($tfm = jfm_form($plt))) or return;
1856 return ($vf, $tfm);
1857 }
1858
1859
1860 ## vf_divide_zvp($pl)
1861 # Subcontractor of vf_form_ex. Divides $pl into ZVP0 part
1862 # $plv and ZPL part $plt and returns ($plv, $plt).
1863 sub vf_divide_zvp
1864 {
1865 my ($pl) = @_; my ($t, $u, $k, $pe, $pe2, @v);
1866 my ($tyd1, $zcat, $rpe, $cspc, @cit, @cist, $glkrn);
1867 my (@plv, @plt, @tydsc, @stydsc, %char, %type, %stype);
1868 # First I classify each enry in $pl into @plv (ZVP0 part)
1869 # and @plt (ZPL part) and extract necessary information
1870 # to @cit, @tydsc, etc.
1871 # $cspc is charlist describing codespace
1872 # $cit[$t] is charlist of type $t
1873 # $cist[$t][$u] is charlist of subtype $t $u
1874 # $tydsc[$t] is 'description' of type $t
1875 # $stydsc[$t][$u] is 'description' of subtype $t $u
1876 # $char{$cc} is 'description' of char $cc
1877 # Here 'description' is the pair of CHARWD and MAP.
1878 # Currenetly CHARWD specified for subtypes and characters
1879 # are ignored (values set for corresponding types are
1880 # used), thus CHARWD entry of descriptions of subtypes
1881 # and chars are unused.
1882 foreach $pe (@$pl) {
1883 (defined($zcat = $vf_zvp_category{$pe->[0]})) or next;
1884 if ($zcat & 2) { push(@plv, $pe); }
1885 if ($zcat & 1) { push(@plt, $pe); }
1886 if ($zcat == 0) {
1887 if ($pe->[0] eq 'GLUEKERN') {
1888 $glkrn = $pe;
1889 } elsif ($pe->[0] eq 'CODESPACE') {
1890 if ($#$pe == 1 && !ref $pe->[1]
1891 && $pe->[1] =~ m/^[\w\-]{6,}$/) {
1892 $t = uc($pe->[1]);
1893 (defined($cspc = jfm_charlist($t)))
1894 or return error("unknown charlist name '$t'");
1895 } else {
1896 $cspc = jfm_grab_charlist($pe, 1);
1897 }
1898 } elsif ($pe->[0] eq 'CHARSINTYPE') {
1899 (defined($t = pl_value($pe ,1))) or return;
1900 (0 < $t && $t < 256)
1901 or return error("CIT with invalid type number ($t)");
1902 $cit[$t] = jfm_grab_charlist($pe, 3);
1903 } elsif ($pe->[0] eq 'CHARSINSUBTYPE') {
1904 (defined($t = pl_value($pe, 1))) or return;
1905 (0 <= $t && $t < 256)
1906 or return error("CIST with invalid type number ($t)");
1907 (defined($u = pl_value($pe, 3))) or return;
1908 (0 < $u && $u < 0x10000)
1909 or return error("CIST with invalid subtype number ($u)");
1910 $cist[$t][$u] = jfm_grab_charlist($pe, 5);
1911 } elsif ($pe->[0] eq 'TYPE') {
1912 (defined($t = pl_value($pe, 1))) or return;
1913 (0 <= $t && $t < 256)
1914 or return error("TYPE with invalid type number ($t)");
1915 $tyd1 = $tydsc[$t] = [ ];
1916 for ($k = 3; $k <= $#$pe; $k++) {
1917 $pe2 = $pe->[$k];
1918 if (defined($u = $vf_zvp_category_char{$pe2->[0]})) {
1919 $tyd1->[$u] = $pe2;
1920 } elsif ($pe2->[0] eq 'MAP') { $tyd1->[1] = $pe2; }
1921 }
1922 } elsif ($pe->[0] eq 'SUBTYPE') {
1923 (defined($t = pl_value($pe, 1))) or return;
1924 (0 <= $t && $t < 256)
1925 or return error("SUBTYPE with invalid type number ($t)");
1926 (defined($u = pl_value($pe, 3))) or return;
1927 (0 < $u && $u < 0x10000)
1928 or return error("SUBTYPE with invalid subtype number ($u)");
1929 $tyd1 = $stydsc[$t][$u] = [ ];
1930 for ($k = 5; $k <= $#$pe; $k++) {
1931 $pe2 = $pe->[$k];
1932 if (defined($u = $vf_zvp_category_char{$pe2->[0]})) {
1933 $tyd1->[$u] = $pe2;
1934 } elsif ($pe2->[0] eq 'MAP') { $tyd1->[1] = $pe2; }
1935 }
1936 } elsif ($pe->[0] eq 'CHARACTER') {
1937 (defined($t = pl_value($pe ,1))) or return;
1938 (0 <= $t && $t <= 0xFFFFFF)
1939 or return error("char code out of range ($t)");
1940 $tyd1 = $char{$t} = [ ];
1941 for ($k = 3; $k <= $#$pe; $k++) {
1942 $pe2 = $pe->[$k];
1943 # only MAP is significant
1944 if ($pe2->[0] eq 'MAP') { $tyd1->[1] = $pe2; }
1945 # tyd1->[0] is currently unused
1946 }
1947 }
1948 }
1949 }
1950 # default codespace is GL94DB
1951 (defined $cspc) or $cspc = jfm_charlist('GL94DB');
1952 # Next I check consistency about existence of entries
1953 # (e.g. CIT 4 should exist iff TYPE 4 exists).
1954 ($#cist >= 0 && $#cit < 0) and $#cit = 0;
1955 (defined $tydsc[0])
1956 or return error("no TYPE for type 0");
1957 (vf_check_match("TYPE", \@tydsc, "CIT", \@cit, 1, "type"))
1958 or return;
1959 foreach (0 .. $#tydsc) {
1960 (vf_check_match("SUBTYPE", $stydsc[$_], "CIST", $cist[$_],
1961 1, "subtype $_")) or return;
1962 }
1963 # Next I recompile JFM properties for support of subtypes with different
1964 # CHARWD values; then I output the resulted GLUEKERN, CHARSIN... lists,
1965 # and TYPE lists.
1966 vf_recompile_gluekern($glkrn, \@cit, \@cist, \@tydsc, \@stydsc);
1967 if (defined $glkrn) { push(@plt, $glkrn); }
1968 foreach $t (0 .. $#cit) {
1969 (defined $cit[$t]) or next;
1970 local $jfm_forced_prefix = 'X';
1971 push(@plt, jfm_form_cit($t, $cit[$t]));
1972 }
1973 @v = sort { $a <=> $b } (values %vf_zvp_category_char);
1974 foreach $t (0 .. $#tydsc) {
1975 (defined($tyd1 = $tydsc[$t])) or next;
1976 $rpe = pl_cook(['TYPE', 'D', 0]); pl_set_value($rpe, 1, $t);
1977 push(@$rpe, grep { defined $_ } (@{$tyd1}[@v]));
1978 push(@plt, $rpe); #qq
1979 }
1980 # Next I make hash %type from charcode to type and %stype
1981 # from charcode to subtype, converting from $cspc, @cit,
1982 # and @cist. The key set of %type is equal to codespace.
1983 # $type{$cc} is type of char $cc (can be 0)
1984 # $stype{$cc} is subtype of char $cc (cannot be 0)
1985 (vf_assign_type($cspc, \%type, \%stype, 0)) or return;
1986 foreach $t (1 .. $#cit) {
1987 (vf_assign_type($cit[$t], \%type, \%stype, $t)) or return;
1988 }
1989 foreach $t (0 .. $#cit) {
1990 foreach $u (1 .. $#{$cist[$t]}) {
1991 (vf_assign_type($cist[$t][$u], \%type, \%stype, $t, $u))
1992 or return;
1993 }
1994 }
1995 # Last I generate the char packet part of ZVP0, using
1996 # information gathered so far.
1997 $t = vf_generate_char_packet(\@tydsc, \@stydsc,
1998 \%char, \%type, \%stype);
1999 push(@plv, @$t);
2000 #print(pl_form(\@plv), ('-') x 60, "\n", pl_form(\@plt));exit;
2001 return (\@plv, \@plt);
2002 }
2003
2004 ## vf_generate_char_packet(...)
2005 # Subcontractor of vf_divide_zvp. Generates the char packet
2006 # part of ZVP0.
2007 sub vf_generate_char_packet
2008 {
2009 my ($tydsc, $stydsc, $char, $type, $stype) = @_;
2010 my ($t, $pe, $ty, $cc, @ccs, @pl);
2011 @ccs = sort { $a <=> $b } (keys %$type);
2012 foreach $cc (@ccs) {
2013 $pe = pl_cook(['CHARACTER', 'C', 0]);
2014 pl_set_value($pe, 1, $cc);
2015 $ty = $type->{$cc};
2016 push(@$pe, $tydsc->[$ty][0]); # CHARWD entry
2017 # add MAP entry
2018 if (defined($t = $char->{$cc})) {
2019 push(@$pe, $t->[1]);
2020 } elsif (defined($t = $stype->{$cc})) {
2021 push(@$pe, $stydsc->[$ty][$t][1]);
2022 } else {
2023 push(@$pe, $tydsc->[$ty][1]);
2024 }
2025 push(@pl, $pe);
2026 }
2027 return \@pl;
2028 }
2029
2030 sub vf_recompile_gluekern
2031 {
2032 my ($glkrn, $cit, $cist, $tydsc, $stydsc) = @_;
2033 my ($t, $u, $tyd0, $tyd, $ty, $nty, $sty, $nsty);
2034 my (@tyrel, @tygrp, $orgnty, @glkrn1);
2035 $orgnty = $#$tydsc;
2036 # type migration
2037 for ($ty = 0; $ty <= $#$tydsc; $ty++) {
2038 (defined $tydsc->[$ty]) or next;
2039 undef $nty; $tyd0 = $tydsc->[$ty];
2040 foreach $sty (1 .. $#{$stydsc->[$ty]}) {
2041 (defined $stydsc->[$ty][$sty]) or next;
2042 $tyd = $stydsc->[$ty][$sty];
2043 (defined $tyd->[1]) or $tyd->[1] = pl_clone($tyd0->[1]);
2044 if (vf_resolve_metric($tyd0, $tyd)) { next; }
2045 if (defined $nty) {
2046 $nsty = ($#{$stydsc->[$nty]} + 1 || 1);
2047 $stydsc->[$nty][$nsty] = $stydsc->[$ty][$sty];
2048 #print("move $ty/$sty to $nty/$nsty\n");
2049 #print("CIT/$nty ", dumpp($cit->[$nty]), " -> ");
2050 vf_clist_add($cit->[$nty], $cist->[$ty][$sty]);
2051 $cist->[$nty][$nsty] = $cist->[$ty][$sty];
2052 #print(dumpp($cit->[$nty]), "\n");
2053 } else {
2054 $nty = $#$tydsc + 1; $tyrel[$ty] = $nty;
2055 $tydsc->[$nty] = $stydsc->[$ty][$sty];
2056 #print("move $ty/$sty to $nty\n");
2057 $cit->[$nty] = $cist->[$ty][$sty];
2058 }
2059 #print("CIT/$ty ", dumpp($cit->[$ty]), " -> ");
2060 vf_clist_remove($cit->[$ty], $cist->[$ty][$sty]);
2061 #print(dumpp($cit->[$ty]), "\n");
2062 undef $stydsc->[$ty][$sty]; undef $cist->[$ty][$sty];
2063 }
2064 }
2065 # compile @tygrp from @tyrel
2066 foreach $ty (0 .. $orgnty) {
2067 $tygrp[$ty] = $u = [$ty]; $t = $ty;
2068 while (defined $tyrel[$t]) { $t = $tyrel[$t]; push(@$u, $t); }
2069 }
2070 # create new GLUEKERN list
2071 foreach $u (@$glkrn) {
2072 if (ref $u &&
2073 ($u->[0] eq 'KRN' || $u->[0] eq 'GLUE' || $u->[0] eq 'LABEL')) {
2074 foreach $ty (@{$tygrp[pl_value($u, 1)]}) {
2075 $t = pl_sclone($u); pl_set_value($t, 1, $ty);
2076 push(@glkrn1, $t);
2077 }
2078 } else { push(@glkrn1, $u); }
2079 }
2080 @$glkrn = @glkrn1;
2081 }
2082 ## vf_clise_remove(...)
2083 # Subcontractor of vf_recompile_gluekern.
2084 sub vf_clist_remove {
2085 my ($lst1, $lst2) = @_; my (%hs);
2086 vf_clist_check(\%hs, $lst1, 1); vf_clist_check(\%hs, $lst2);
2087 @$lst1 = sort { $a <=> $b } (keys %hs);
2088 }
2089
2090 ## vf_clise_add(...)
2091 # Subcontractor of vf_recompile_gluekern.
2092 sub vf_clist_add {
2093 my ($lst1, $lst2) = @_; my (%hs);
2094 vf_clist_check(\%hs, $lst1, 1); vf_clist_check(\%hs, $lst2, 1);
2095 @$lst1 = sort { $a <=> $b } (keys %hs);
2096 }
2097
2098 ## vf_clist_check(...)
2099 # Subcontractor of vf_recompile_gluekern.
2100 sub vf_clist_check {
2101 my ($hs, $clst, $val) = @_; my ($e, $sc, $ec);
2102 foreach $e (@$clst) {
2103 ($sc, $ec) = (ref $e) ? @$e : ($e, $e);
2104 foreach ($sc .. $ec) {
2105 if (defined $val) { $hs->{$_} = $val; } else { delete $hs->{$_}; }
2106 }
2107 }
2108 }
2109
2110 ## vf_resolve_metric(...)
2111 # Subcontractor of vf_recompile_gluekern.
2112 sub vf_resolve_metric {
2113 my ($tyd1, $tyd2) = @_; my ($wd1, $nam, $idx, $same);
2114 $same = 1;
2115 foreach $nam (keys %vf_zvp_category_char) {
2116 $idx = $vf_zvp_category_char{$nam};
2117 $wd1 = (defined $tyd1->[$idx]) ? pl_value($tyd1->[$idx], 1) : 0;
2118 if (defined $tyd2->[$idx]) {
2119 if ($wd1 != pl_value($tyd2->[$idx], 1)) { undef $same; }
2120 } else {
2121 $tyd2->[$idx] = pl_cook([$nam, 'R', 0]);
2122 pl_set_value($tyd2->[$idx], 1, $wd1);
2123 }
2124 }
2125 return $same;
2126 }
2127
2128 ## vf_assign_type($cl, $type, $stype, $ty, $sty)
2129 # Subcontractor of vf_divide_zvp. If $sty is defined, it maps
2130 # chars in $cl to $sty in hash $stype->[$ty]; otherwise it maps
2131 # chars in$cl to $ty in hash $type.
2132 sub vf_assign_type
2133 {
2134 my ($cl, $type, $stype, $ty, $sty) = @_;
2135 my ($t, $c, $s, $e);
2136 foreach $c (@$cl) {
2137 ($s, $e) = (ref $c) ? @$c : ($c, $c);
2138 foreach ($s .. $e) {
2139 if (defined $sty) { # set subtype
2140 ($type->{$_} == $ty) or return error(
2141 sprintf("inconsistent subtype assignment to char %04X" .
2142 " (subtype %s %s vs type %s)",
2143 $_, $ty, $sty, $type->{$_}));
2144 (!defined $stype->{$_}) or return error(
2145 sprintf("subtype reassignment to char %04X" .
2146 " (subtype %s vs %s)",
2147 $_, $sty, $stype->{$_}));
2148 $stype->{$_} = $sty;
2149 } elsif ($ty != 0) { # set type >0
2150 (defined $type->{$_}) or return error(
2151 sprintf("type assignment (%s) to char out of codespace:" .
2152 "char %04X",
2153 $ty, $_));
2154 ($type->{$_} == 0) or return error(
2155 sprintf("type reassignment to char %04X" .
2156 " (type %s vs %s)",
2157 $_, $ty, $type->{$_}));
2158 $type->{$_} = $ty;
2159 } else { # set type 0
2160 $type->{$_} = $ty;
2161 }
2162 }
2163 }
2164 return 1;
2165 }
2166
2167 ## vf_check_match($lbla, $lsta, $lblb, $lstb, $pos, $srt)
2168 # Subcontractor of vf_divide_zvp.
2169 sub vf_check_match
2170 {
2171 my ($lbla, $lsta, $lblb, $lstb, $pos, $srt) = @_; my ($mpos);
2172 $mpos = ($#$lsta > $#$lstb) ? $#$lsta : $#$lstb;
2173 foreach ($pos .. $mpos) {
2174 if (defined $lsta->[$_] && !defined $lstb->[$_]) {
2175 return error("$lbla entry without matching $lblb ($srt $_)");
2176 } elsif (defined $lstb->[$_] && !defined $lsta->[$_]) {
2177 return error("$lblb entry without matching $lbla ($srt $_)");
2178 }
2179 }
2180 return 1;
2181 }
2182
2183
2184 ##<*> vf_parse_ex($vf, $jfm)
2185 sub vf_parse_ex
2186 {
2187 my ($vf, $jfm) = @_; my ($plv, $plt, $cit, $typ);
2188 my ($pl, $tydsc, $chdsc, $cspc, $cist, $stydsc, $chdsc);
2189 $plv = vf_parse($vf) or return;
2190 ($plt, $cit, $typ) = jfm_half_parse($jfm) or return;
2191 ($pl, $tydsc, $chdsc) = vf_restructure($plv, $plt) or return;
2192 ($cspc, $cist, $stydsc, $chdsc) =
2193 vf_analyze_dimap($chdsc, $tydsc, $typ, $cit) or return;
2194 $pl = vf_compose_zvp($pl, $cspc, $cist, $stydsc, $chdsc);
2195 return $pl;
2196 }
2197
2198 sub vf_restructure
2199 {
2200 my ($plv, $plt) = @_; my ($t, $u, $pe, $zcat, %chk);
2201 my (@pl, @tydsc, %chdsc);
2202 # ZPL
2203 foreach $pe (@$plt) {
2204 $zcat = $vf_zvp_category{$pe->[0]};
2205 if ($zcat == 1) {
2206 push(@pl, $pe);
2207 } elsif ($zcat == 3) {
2208 push(@pl, $pe); $chk{$pe->[0]} = pl_value($pe, 1);
2209 } elsif ($zcat == 0) {
2210 if ($pe->[0] eq 'CHARSINTYPE' || $pe->[0] eq 'GLUEKERN') {
2211 push(@pl, $pe);
2212 } elsif ($pe->[0] eq 'TYPE') {
2213 push(@pl, $pe); $t = pl_value($pe, 1);
2214 $u = pl_cook(['MAP']); push(@$pe, $u);
2215 $tydsc[$t] = [$pe->[3], $u];
2216 }
2217 } elsif (!defined $zcat) {
2218 return fatal("vf_restructure");
2219 }
2220 }
2221 # ZVP0
2222 foreach $pe (@$plv) {
2223 $zcat = $vf_zvp_category{$pe->[0]};
2224 if ($zcat == 2) {
2225 push(@pl, $pe);
2226 } elsif ($zcat == 3 && $vf_strict) {
2227 $t = pl_value($pe, 1); $u = $chk{$pe->[0]};
2228 ($t == $u ||
2229 ($pe->[0] eq 'CHECKSUM' && ($t == 0 || $u == 0)))
2230 or return error("inconsistent value: ", $pe->[0]);
2231 } elsif ($zcat == 0) {
2232 if ($pe->[0] eq 'CHARACTER') {
2233 $t = pl_value($pe, 1);
2234 $chdsc{$t} = [$pe->[3], $pe->[4]];
2235 }
2236 } elsif (!defined $zcat) {
2237 return fatal("vf_restructure");
2238 }
2239 }
2240 return (\@pl, \@tydsc, \%chdsc);
2241 }
2242
2243 ## vf_analyze_dimap
2244 sub vf_analyze_dimap
2245 {
2246 my ($chdsc, $tydsc, $typ, $citpe) = @_;
2247 my ($t, $u, $k, $cc, @ccs, $pe, @fs, $ty, $chd, @dmaps, %cnt);
2248 my ($cspc, @cit, @cist, @stydsc, %chdsc2);
2249 #@dmaps = ({}) x scalar(@$tydsc);
2250 # coderange consistency
2251 @fs = sort { $a <=> $b } (keys %$typ);
2252 foreach $cc (@fs) {
2253 (defined $chdsc->{$cc}) and next;
2254 if ($vf_strict) {
2255 return error(sprintf("charpacket missing in VF: code %04X", $cc));
2256 }
2257 delete $typ->{$cc};
2258 }
2259 #
2260 @ccs = sort { $a <=> $b } (keys %$chdsc);
2261 foreach $cc (@ccs) {
2262 $ty = $typ->{$cc}; $chd = $chdsc->{$cc};
2263 push(@{$cit[$ty]}, $cc);
2264 if ($vf_strict) {
2265 (pl_value($tydsc->[$ty][0], 1) == pl_value($chd->[0], 1))
2266 or return error(
2267 sprintf("CHARWD value mismatch: code %04X", $cc));
2268 }
2269 $pe = vf_contract_selfcode($chd->[1], $cc);
2270 $t = pl_form_list($pe, -1);
2271 push(@{$dmaps[$ty]{$t}}, $cc);
2272 }
2273 #
2274 if (defined($t = jfm_charlist_name(\@ccs))) {
2275 $cspc = [ $t ];
2276 } else { $cspc = jfm_form_charlist(\@ccs); }
2277 foreach $ty (0 .. $#dmaps) {
2278 (defined($u = $dmaps[$ty])) or next;
2279 foreach (keys %$u) { $cnt{$_} = scalar(@{$u->{$_}}); }
2280 @fs = sort {
2281 $cnt{$b} <=> $cnt{$a} || $u->{$a}[0] <=> $u->{$b}[0]
2282 } (keys %$u);
2283 foreach $k (0 .. $#fs) {
2284 $u = $dmaps[$ty]{$fs[$k]};
2285 if ($k == 0 || ($cnt{$fs[$k]} > 1 && $k < 256)) {
2286 $cist[$ty][$k] = jfm_form_charlist($u);
2287 $stydsc[$ty][$k][1] =
2288 vf_contract_selfcode($chdsc->{$u->[0]}[1], $u->[0]);
2289 } else {
2290 foreach (@$u) {
2291 $chdsc2{$_}[1] = $chdsc->{$_}[1];
2292 }
2293 }
2294 }
2295 @{$tydsc->[$ty][1]} = @{$stydsc[$ty][0][1]};
2296 undef $cist[$ty][0]; undef $stydsc[$ty][0];
2297 if ($ty > 0) {
2298 $t = jfm_form_charlist($cit[$ty]);
2299 push(@{$citpe->[$ty]}, @$t);
2300 }
2301 }
2302 return ($cspc, \@cist, \@stydsc, \%chdsc2);
2303 }
2304
2305 sub vf_compose_zvp
2306 {
2307 my ($pl, $cspc, $cist, $stydsc, $chdsc) = @_;
2308 my ($t, $u, $ty, $sty, $cc, $pe);
2309 $pe = pl_cook(['CODESPACE']); push(@$pl, $pe);
2310 push(@$pe, @$cspc);
2311 foreach $ty (0 .. $#$stydsc) {
2312 foreach $sty (0 .. $#{$stydsc->[$ty]}) {
2313 if (defined($t = $cist->[$ty][$sty])) {
2314 $pe = pl_cook(['CHARSINSUBTYPE', 'D', 0, 'D', 0]);
2315 pl_set_value($pe, 1, $ty); pl_set_value($pe, 3, $sty);
2316 push(@$pe, @$t); push(@$pl, $pe);
2317 }
2318 if (defined($t = $stydsc->[$ty][$sty])) {
2319 $pe = pl_cook(['SUBTYPE', 'D', 0, 'D', 0]);
2320 pl_set_value($pe, 1, $ty); pl_set_value($pe, 3, $sty);
2321 push(@$pe, $t->[1]); push(@$pl, $pe);
2322 }
2323 }
2324 }
2325 foreach $cc (keys %$chdsc) {
2326 $pe = pl_cook(['CHARACTER', 'C', 0]);
2327 pl_set_value($pe, 1, $cc);
2328 push(@$pe, $chdsc->{$cc}[1]); push(@$pl, $pe);
2329 }
2330 pl_rearrange($pl);
2331 return $pl;
2332 }
2333
2334 ## vf_contract_selfcode
2335 sub vf_contract_selfcode
2336 {
2337 my ($pl, $cc) = @_; my ($k, $pe, $pl2);
2338 $pl2 = pl_sclone($pl);
2339 foreach $k (1 .. $#$pl2) {
2340 $pe = $pl2->[$k];
2341 if ($pe->[0] eq 'SETCHAR' &&
2342 $#$pe > 0 && pl_value($pe, 1) == $cc) {
2343 $pl2->[$k] = pl_cook(['SETCHAR']);
2344 }
2345 }
2346 return $pl2;
2347 }
2348
2349 ##<*> vf_expand_named_charlist
2350 #
2351 sub vf_expand_named_charlist {
2352 my ($pl) = @_; my ($t);
2353 ($pl->[0] eq 'CODESPACE') or return;
2354 (defined($t = jfm_charlist($pl->[1]))) or return;
2355 $t = jfm_form_charlist($t, 0);
2356 splice(@$pl, 1, 1, @$t);
2357 }
2358
2359
2360 ######## 'jfm' section ########
2361
2362 # jfm_form (ZPL -> JFM)
2363 # Jx --[jc(x)=Ic(i)]-> i (!uptex?)--> Ji --[ppltotf]--> (JFM)
2364 # Ux --[uc(x)=Ic(i)]-> (uptex?)---> Ui --[uppltotf]->
2365 # Xx --[x=i]--------->
2366 # K $ --[Ee($)=Ic(i)]->
2367 # jfm_parse (JFM -> ZPL)
2368 # (JFM) (!uptex?)--[ptftopl]-> $ --[je($)=Ic(x)]-> Jx/Ux/Xx
2369 # (uptex?)--[uptftopl]-> $ --[ue($)=Ic(x)]->
2370
2371 # for jfm_injcode
2372 my %jfm_pfx_ijc = ( J => KI_JIS, U => KI_UNI, X => undef );
2373 my %jfm_ijc_pfx = ( reverse %jfm_pfx_ijc );
2374
2375 ##<*> jfm_use_uptex_tool($sw)
2376 # Decides if upTeX tools are used to do jfm_parse. Here truth
2377 # value of $sw means upTeX-pltotf should/shouldn't be used.
2378 our $jfm_use_uptex_tool = 0;
2379 sub jfm_use_uptex_tool
2380 {
2381 my ($sw) = @_; my ($t);
2382 $t = ($cmd_name{uptftopl} eq '' || $cmd_name{uppltotf} eq '');
2383 if ($sw && $t) { return error("upTeX tools disabled"); }
2384 $jfm_use_uptex_tool = $sw;
2385 return 1;
2386 }
2387
2388 ##<*> jfm_parse($jfm)
2389 # Converts JFM data $jfm to a pl-struct describing ZPL.
2390 sub jfm_parse
2391 {
2392 my ($jfm) = @_; my ($pl, $cit, $typ);
2393 ($pl, $cit, $typ) = jfm_half_parse($jfm) or return;
2394 return jfm_record_cit($pl, $cit, $typ);
2395 }
2396
2397 ## jfm_half_parse($jfm)
2398 # Converts JFM data $jfm to a 'half-parsed' form.
2399 sub jfm_half_parse
2400 {
2401 my ($jfm) = @_; my ($cmd, $pl, $map, $cit, $typ);
2402 if ($jfm_use_uptex_tool) {
2403 $pl = x_uptftopl($jfm) or return;
2404 jfm_interprocess($pl) or return;
2405 return jfm_grab_cit($pl, 'utf8', 'unicode');
2406 } else {
2407 $cmd = $cmd_name{tftopl} . ' -kanji=jis';
2408 (($jfm, $map) = jfm_parse_preprocess($jfm)) or return;
2409 $pl = x_tftopl($jfm, $cmd) or return;
2410 jfm_interprocess($pl) or return;
2411 return jfm_parse_postprocess($pl, $map);
2412 }
2413 }
2414
2415 ##<*> jfm_form($pl)
2416 # Converts a pl-struct $pl describing a JPLZ to JFM data.
2417 sub jfm_form
2418 {
2419 my ($pl) = @_; my ($cmd, $map, $jfm);
2420 if ($jfm_use_uptex_tool) {
2421 local $jfm_forced_prefix = 'U';
2422 (defined($pl = jfm_normalize($pl))) or return;
2423 return x_pltotf($pl, $cmd_name{uppltotf});
2424 } else {
2425 $cmd = $cmd_name{pltotf} . ' -kanji=jis';
2426 ((($pl, $map) = jfm_form_preprocess($pl))) or return;
2427 $jfm = x_pltotf($pl, $cmd) or return;
2428 return jfm_form_postprocess($jfm, $map);
2429 }
2430 }
2431
2432 ## jfm_grab_cit($pl)
2433 sub jfm_grab_cit
2434 {
2435 my ($pl, $xjc, $ijc) = @_; my ($t, $cl, $ty, $pe);
2436 my (@pl2, %typ, @cit, @ccs);
2437 local ($jcode_ex) =
2438 (defined $xjc) ? $jcode_ex_sym{$xjc} : $jcode_ex;
2439 local ($jcode_in) =
2440 (defined $ijc) ? $jcode_in_sym{$ijc} : $jcode_in;
2441 foreach $pe (@$pl) {
2442 if ($pe->[0] eq 'CHARSINTYPE') {
2443 pl_cook_list($pe);
2444 (defined($ty = pl_value($pe, 1))) or return;
2445 $cl = jfm_grab_charlist($pe, 3) or return;
2446 foreach $t (@$cl) {
2447 if (ref $t) {
2448 foreach ($t->[0] .. $t->[1]) { $typ{$_} = $ty; }
2449 } else { $typ{$t} = $ty; }
2450 }
2451 $cit[$ty] = $t = pl_cook(['CHARSINTYPE', 'D', 0]);
2452 pl_set_value($t, 1, $ty); push(@pl2, $t);
2453 } else { push(@pl2, $pe); }
2454 }
2455 return (\@pl2, \@cit, \%typ);
2456 }
2457
2458 ## jfm_normalize($pl, $xjc, $ijc)
2459 # Normalizes pl-struct $pl, i.e., converts ZPL to ordinary PL.
2460 # Here $xjc and $ijc mean source and internal kanji codes that
2461 # are effective in this process (unchanged if undef).
2462 sub jfm_normalize
2463 {
2464 my ($pl, $xjc, $ijc) = @_; my ($citpe, $typ);
2465 (($pl, $citpe, $typ) = jfm_grab_cit($pl, $xjc, $ijc)) or return;
2466 (defined($pl = jfm_record_cit($pl, $citpe, $typ))) or return;
2467 return $pl;
2468 }
2469
2470 ## jfm_record_cit($pl, $citpe, $typ)
2471 # Assembles a half-parsed form to get a complete ZPL.
2472 sub jfm_record_cit
2473 {
2474 my ($pl, $citpe, $typ) = @_; my ($t, $u, $cc, @ccs, @cit);
2475 @ccs = sort { $a <=> $b } (keys %$typ);
2476 foreach $cc (@ccs) {
2477 push(@{$cit[$typ->{$cc}]}, $cc);
2478 }
2479 foreach $t (1 .. $#cit) {
2480 (defined $cit[$t]) or next;
2481 $u = jfm_form_charlist($cit[$t]);
2482 push(@{$citpe->[$t]}, @$u);
2483 }
2484 return $pl;
2485 }
2486
2487 ## jfm_form_preprocess
2488 # Subcontactor of jfm_form.
2489 sub jfm_form_preprocess
2490 {
2491 my ($pl) = @_; my ($pl2, $cit, $typ, $jc, $cc, @ccs, %map);
2492 ((($pl2, $cit, $typ) = jfm_grab_cit($pl))) or return;
2493 @ccs = sort { $a <=> $b } (keys %$typ);
2494 $jc = 0x2121;
2495 foreach $cc (@ccs) {
2496 push(@{$cit->[$typ->{$cc}]}, sprintf("J%04X", $jc));
2497 $map{$jc} = $cc; $jc = jfm_nextcode($jc) or return;
2498 }
2499 return ($pl2, \%map);
2500 }
2501 # Valid codespace in pltotf: [21-28|30-74][21-7F] (7238 chars)
2502
2503 ## jfm_form_postprocess
2504 # Subcontactor of jfm_form.
2505 sub jfm_form_postprocess
2506 {
2507 my ($jfm, $map) = @_; my ($k, $pct, $lct, $ct, @fs);
2508 @fs = unpack('nnnn', $jfm);
2509 ($#fs == 3 && $fs[2] * 4 == length($jfm) &&
2510 ($fs[0] == 9 || $fs[0] == 11)) or return;
2511 $pct = $fs[3] * 4 + 28; $lct = $fs[1] * 4;
2512 $ct = substr($jfm, $pct, $lct); @fs = unpack('n*', $ct);
2513 for ($k = 2; $k <= $#fs; $k += 2) {
2514 my $cc = $map->{$fs[$k]};
2515 $fs[$k] = ($cc & 0xFFFF); $fs[$k+1] |= ($cc >> 16 << 8);
2516 }
2517 $ct = pack('n*', @fs);
2518 return substr($jfm, 0, $pct) . $ct . substr($jfm, $pct + $lct);
2519 }
2520
2521 ## jfm_parse_preprocess
2522 # Subcontactor of jfm_half_parse.
2523 sub jfm_parse_preprocess
2524 {
2525 my ($jfm) = @_; my ($k, $pct, $lct, $ct, @fs, $jc, %map);
2526 @fs = unpack('nnnn', $jfm);
2527 ($#fs == 3 && $fs[2] * 4 == length($jfm) &&
2528 ($fs[0] == 9 || $fs[0] == 11)) or return;
2529 $pct = $fs[3] * 4 + 28; $lct = $fs[1] * 4;
2530 $ct = substr($jfm, $pct, $lct); @fs = unpack('n*', $ct);
2531 for ($jc = 0x2121, $k = 2; $k <= $#fs; $k += 2) {
2532 $map{$jc} = ($fs[$k] | $fs[$k+1] >> 8 << 16);
2533 $fs[$k] = $jc; $fs[$k+1] &= 0xFF;
2534 $jc = jfm_nextcode($jc) or return;
2535 }
2536 $ct = pack('n*', @fs);
2537 $jfm = substr($jfm, 0, $pct) . $ct . substr($jfm, $pct + $lct);
2538 return ($jfm, \%map);
2539 }
2540
2541 ## jfm_parse_postprocess
2542 # Subcontactor of jfm_half_parse.
2543 sub jfm_parse_postprocess
2544 {
2545 my ($pl, $map, $pfx) = @_; my ($cit, $typ, %typ2, $cc);
2546 ($pl, $cit, $typ) = jfm_grab_cit($pl, 'xjis', 'xjis') or return;
2547 foreach $cc (keys %$typ) {
2548 $typ2{$map->{$cc}} = $typ->{$cc};
2549 }
2550 return ($pl, $cit, \%typ2);
2551 }
2552
2553 ## jfm_nextcode($jc)
2554 # Subcontactor of jfm_form_preprocess and jfm_parse_preprocess.
2555 sub jfm_nextcode
2556 {
2557 my ($jc) = @_;
2558 if ((++$jc & 0xFF) < 0x7F) { return $jc; }
2559 if ($jc == 0x747F) {
2560 return error("too many chars have non-zero type");
2561 } elsif ($jc == 0x287F) { $jc = 0x3021; }
2562 else { $jc += 162; }
2563 return $jc;
2564 }
2565
2566 ## jfm_injcode($f, $xc)
2567 # Subcontactor of jfm_form_preprocess.
2568 sub jfm_injcode
2569 {
2570 my ($pfx, $xc) = @_; local ($jcode_ex);
2571 # Note: here encodings meant for 'internal' use are
2572 # used as 'external' excoding.
2573 (defined($jcode_ex = $jfm_pfx_ijc{$pfx})) or return;
2574 if ($jcode_ex eq $jcode_in) { return $xc; }
2575 return jcode_ord(chr($xc >> 8) . chr($xc & 0xff));
2576 }
2577
2578
2579 ## jfm_interprocess($pl)
2580 sub jfm_interprocess
2581 {
2582 my ($pl) = @_; my ($pe, $pe2, $ok);
2583 foreach $pe (@$pl) {
2584 if ($pe->[0] eq 'TYPE') {
2585 $ok = 1; pl_set_numtype($pe, 1, 'D') or return;
2586 } elsif ($pe->[0] eq 'CHARSINTYPE') {
2587 $ok = 1; pl_set_numtype($pe, 1, 'D') or return;
2588 } elsif ($pe->[0] eq 'GLUEKERN') {
2589 foreach $pe2 (@$pe) {
2590 (ref $pe2) or next;
2591 ($pe2->[0] eq 'LABEL' ||
2592 $pe2->[0] eq 'GLUE' || $pe2->[0] eq 'KRN') or next;
2593 pl_set_numtype($pe2, 1, 'D') or return;
2594 }
2595 }
2596 }
2597 ($ok) or return error("input TFM is not JFM");
2598 return 1;
2599 }
2600
2601 ##-------- Procedures on charlist
2602
2603 # A charlist is an array, each entry of which is either a code
2604 # value or a array-ref consisting of two values, which means
2605 # a code range. For example,
2606 # [ 0x50, [ 0x100, 0x1FF ], 0x234 ]
2607 # is a charlist consisting of 258 code values.
2608
2609 our %jfm_charlist_registry = (
2610 'UNICODE-BMP' => [ [0x0000, 0xFFFF] ],
2611 'GL94DB' => [ map { [ ($_ << 8) | 0x21, ($_ << 8) | 0x7E ] }
2612 (0x21 .. 0x7E) ],
2613 );
2614
2615 ##<*> jfm_use_charlist_name()
2616 our $jfm_use_charlist_name = 1;
2617 sub jfm_use_charlist_name { $jfm_use_charlist_name = $_[0]; }
2618
2619 ## jfm_charlist($name)
2620 sub jfm_charlist
2621 {
2622 return $jfm_charlist_registry{$_[0]};
2623 }
2624
2625 ## jfm_charlist_name($cl)
2626 sub jfm_charlist_name
2627 {
2628 my ($cl) = @_;
2629 my ($k, $f1, $f2, $l1, $l2, $cl2, $nam, @nams, $res);
2630 (@$cl && $jfm_use_charlist_name) or return;
2631 $f1 = (ref $cl->[0]) ? $cl->[0][0] : $cl->[0];
2632 @nams = sort { $a cmp $b } (keys %jfm_charlist_registry);
2633 L1:foreach $nam (@nams) {
2634 $cl2 = $jfm_charlist_registry{$nam};
2635 $f2 = (ref $cl2->[0]) ? $cl2->[0][0] : $cl2->[0];
2636 ($f1 == $f2) or next;
2637 if (!defined $l1) { $l1 = jfm_length_charlist($cl); }
2638 $l2 = jfm_length_charlist($cl2);
2639 ($l1 == $l2) or next;
2640 $cl = jfm_rangify_charlist($cl);
2641 ($#$cl == $#$cl2) or next;
2642 foreach $k (0 .. $#$cl) {
2643 if (ref $cl->[$k] && ref $cl2->[$k]) {
2644 ($cl->[$k][0] == $cl2->[$k][0] &&
2645 $cl->[$k][1] == $cl2->[$k][1]) or next L1;
2646 } elsif (!ref $cl->[$k] && !ref $cl2->[$k]) {
2647 ($cl->[$k] == $cl2->[$k]) or next L1;
2648 } else { next L1; }
2649 }
2650 $res = $nam; last L1;
2651 }
2652 return $res;
2653 }
2654
2655 sub jfm_length_charlist
2656 {
2657 my ($cl) = @_; my ($s);
2658 $s = 0;
2659 foreach (@$cl) {
2660 if (ref $_) { $s += $_->[1] - $_->[0] + 1; }
2661 else { $s += 1; }
2662 }
2663 return $s;
2664 }
2665
2666 ## for jfm_rangify_charlist()
2667 our $jfm_rangify_threshold = 8;
2668
2669 ## jfm_rangify_charlist($cl)
2670 sub jfm_rangify_charlist
2671 {
2672 my ($cl) = @_; my ($cc, $scc, $ecc,@cl2);
2673 foreach $cc (@$cl, []) {
2674 if (defined $scc && !ref $cc && $cc == $ecc + 1) {
2675 $ecc = $cc;
2676 } else {
2677 if (!defined $scc) { # do nothing
2678 } elsif ($ecc - $scc < $jfm_rangify_threshold) {
2679 push(@cl2, $scc .. $ecc);
2680 } else {
2681 push(@cl2, [$scc, $ecc]);
2682 }
2683 if (ref $cc) {
2684 push(@cl2, $cc); $scc = $ecc = undef;
2685 } else {
2686 $scc = $ecc = $cc;
2687 }
2688 }
2689 }
2690 pop(@cl2);
2691 return \@cl2;
2692 }
2693
2694 ## jfm_form_cit($ty, $cl, $sym)
2695 sub jfm_form_cit
2696 {
2697 my ($ty, $cl) = @_; my ($t, $pe);
2698 $pe = pl_cook(['CHARSINTYPE', 'D', 0]);
2699 pl_set_value($pe, 1, $ty);
2700 $t = jfm_form_charlist($cl, 0); push(@$pe, @$t);
2701 return $pe;
2702 }
2703
2704 ## jfm_form_charlist($cl, $swrng)
2705 sub jfm_form_charlist
2706 {
2707 my ($cl, $swrng) = @_; my ($cc, $pe, @cl2, $nf, $pfx);
2708 $pfx = $jfm_ijc_pfx{$jcode_in};
2709 $nf = ($pl_prefer_hex) ? 'H' : 'O';
2710 if (defined $jfm_forced_prefix) { $pfx = $jfm_forced_prefix; }
2711 (defined $swrng)
2712 or $swrng = (!defined $jcode_in && !defined $jfm_forced_prefix);
2713 if ($swrng) { $cl = jfm_rangify_charlist($cl); }
2714 foreach $cc (@$cl) {
2715 if (ref $cc) {
2716 $pe = pl_cook(['CTRANGE', $nf, 0, $nf, 0]);
2717 pl_set_value($pe, 1, $cc->[0]);
2718 pl_set_value($pe, 3, $cc->[1]);
2719 push(@cl2, $pe);
2720 } else {
2721 push(@cl2, sprintf("%s%04X", $pfx, $cc));
2722 }
2723 }
2724 return \@cl2;
2725 }
2726
2727 ## jfm_grab_charlist($pe, $pos)
2728 sub jfm_grab_charlist
2729 {
2730 my ($pe, $pos) = @_; my ($k, $e, $t, $u, $cc, @cl);
2731 for ($k = $pos; $k <= $#$pe; $k++) {
2732 $e = $pe->[$k];
2733 if (ref $e && $e->[0] eq CNUM) {
2734 (defined($cc = pl_value($pe, $k))) or return;
2735 push(@cl, $cc); ++$k;
2736 } elsif ($e =~ m/^([JUX])([0-9A-Fa-f]{1,6})$/) {
2737 if (!defined $jcode_in || $1 eq 'X') {
2738 push(@cl, hex($2));
2739 } else {
2740 (defined($cc = jfm_injcode($1, hex($2)))) or return;
2741 push(@cl, $cc);
2742 }
2743 } elsif ($e =~ m/^[^\x21-\x7e]/) {
2744 (defined($cc = jcode_ord($e)))
2745 or return error("malformed $cc kanji character: ",
2746 unpack('H*', $e));
2747 push(@cl, $cc);
2748 } elsif (ref $e && $e->[0] eq 'CTRANGE') {
2749 (defined($t = pl_value($e, 1)) &&
2750 defined($u = pl_value($e, 3))) or return;
2751 push(@cl, [$t, $u]);
2752 } else {
2753 return error("illegal element in CHARSINTYPE: ", $e);
2754 }
2755 }
2756 return \@cl;
2757 }
2758
2759 #================================================= END
2760 ($jcode_in, $jcode_ex) = (undef, undef);
2761 get_temp_name_init();
2762 if (defined $errmsg) { error("initialization failed"); }
2763
2764 #------------------------------------------------- dumb importer
2765 package main;
2766 {
2767 no strict;
2768 foreach (qw(
2769 textool_error textool_version
2770 read_whole_file write_whole_file
2771 pl_parse pl_form pl_prefer_hex pl_value
2772 jcode_set
2773 kpse
2774 vf_parse vf_form vf_parse_ex vf_form_ex vf_strict
2775 jfm_use_uptex_tool jfm_parse jfm_form
2776 )) {
2777 *{$_} = *{"ZRTeXtor::".$_};
2778 }
2779 }
2780
2781 #------------------------------------------------- pxutil stuffs
2782 # Here follows excerpt from pxutil.pl
2783 #================================================= BEGIN
2784 use Encode qw(encode decode);
2785 my $prog_name = 'jfmutil';
2786 my $version = '1.3.3';
2787 my $mod_date = '2021/10/09';
2788 #use Data::Dump 'dump';
2789 #
2790 my ($sw_hex, $sw_uptool, $sw_noencout, $inenc, $exenc, $sw_lenient);
2791 my ($sw_compact);
2792 my ($proc_name, $infile, $in2file ,$outfile, $out2file);
2793
2794 #### main procedure
2795
2796 my %procs = (
2797 vf2zvp0 => \&main_vf2zvp0,
2798 zvp02vf => \&main_zvp02vf,
2799 vf2zvp => \&main_vf2zvp,
2800 zvp2vf => \&main_zvp2vf,
2801 tfm2zpl => \&main_tfm2zpl,
2802 zpl2tfm => \&main_zpl2tfm,
2803 );
2804
2805 sub main {
2806 if (defined textool_error()) { error(); }
2807 local $_ = shift(@ARGV);
2808 if (!defined $_) {
2809 show_usage();
2810 } elsif (($proc_name) = m/^:?(\w+)$/) {
2811 my $proc = $procs{$proc_name};
2812 (defined $proc) or error("unknown subcommand name", $proc_name);
2813 $proc->();
2814 } elsif (m/^-/) {
2815 if (m/^--?h(?:elp)?$/) {
2816 show_usage();
2817 } elsif (m/^-(?:V|-version)$/) {
2818 show_version();
2819 } else { error("unknown (or invalid usage of) option", $_); }
2820 } else { error("invalid argument", $_); }
2821 }
2822
2823 sub main_vf2zvp0 {
2824 my ($t);
2825 read_option();
2826 $t = read_whole_file(kpse($infile), 1) or error();
2827 $t = vf_parse($t) or error();
2828 $t = pl_form($t) or error();
2829 write_whole_file($outfile, $t) or error();
2830 }
2831
2832 sub main_zvp02vf {
2833 my ($t);
2834 read_option();
2835 $t = read_whole_file(kpse($infile)) or error();
2836 $t = pl_parse($t) or error();
2837 ($sw_compact) and $t = do_compact_vf($t);
2838 $t = vf_form($t) or error();
2839 write_whole_file($outfile, $t, 1) or error();
2840 }
2841
2842 sub main_zvp2vf {
2843 my ($t, $u);
2844 read_option();
2845 if ($sw_uptool) { jfm_use_uptex_tool(1); }
2846 $t = read_whole_file(kpse($infile)) or error();
2847 $t = pl_parse($t) or error();
2848 ($t, $u) = vf_form_ex($t) or error();
2849 write_whole_file($outfile, $t, 1) or error();
2850 write_whole_file($out2file, $u, 1) or error();
2851 }
2852 sub main_vf2zvp {
2853 my ($t, $vf, $tfm);
2854 read_option();
2855 if ($sw_uptool) { jfm_use_uptex_tool(1); }
2856 $vf = read_whole_file(kpse($infile), 1) or error();
2857 $tfm = read_whole_file(kpse($in2file), 1) or error();
2858 $t = vf_parse_ex($vf, $tfm) or error();
2859 $t = pl_form($t) or error();
2860 write_whole_file($outfile, $t) or error();
2861 }
2862
2863 sub main_tfm2zpl {
2864 my ($t);
2865 read_option();
2866 if ($sw_uptool) { jfm_use_uptex_tool(1); }
2867 $t = read_whole_file(kpse($infile), 1) or error();
2868 $t = jfm_parse($t) or error();
2869 $t = pl_form($t) or error();
2870 write_whole_file($outfile, $t) or error();
2871 }
2872
2873 sub main_zpl2tfm {
2874 my ($t);
2875 read_option();
2876 if ($sw_uptool) { jfm_use_uptex_tool(1); }
2877 $t = read_whole_file(kpse($infile)) or error();
2878 $t = pl_parse($t) or error();
2879 $t = jfm_form($t) or error();
2880 write_whole_file($outfile, $t, 1) or error();
2881 }
2882
2883 sub is_simple_char {
2884 local ($_) = @_;
2885 ($#$_ == 4 &&
2886 $_->[0] eq 'CHARACTER' &&
2887 $_->[3][0] eq 'CHARWD' &&
2888 $_->[4][0] eq 'MAP'
2889 ) or return;
2890 my $cc = ::pl_value($_, 1);
2891 $_ = $_->[4];
2892 ($#$_ == 1 &&
2893 $_->[1][0] eq 'SETCHAR' &&
2894 ::pl_value($_->[1], 1) == $cc
2895 ) or return;
2896 return 1;
2897 }
2898
2899 sub do_compact_vf {
2900 my ($t) = @_;
2901 $t = [ grep { !is_simple_char($_) } (@$t) ];
2902 return $t;
2903 }
2904
2905 sub show_usage {
2906 print(usage_message());
2907 exit;
2908 }
2909 sub show_version {
2910 print("$prog_name version $version\n");
2911 exit;
2912 }
2913 sub usage_message {
2914 my ($v, $m);
2915 ($v, $m) = textool_version() or error();
2916 return <<"EOT1", <<"EOT2";
2917 This is $prog_name v$version <$mod_date> by 'ZR'.
2918 [ZRTeXtor library v$v <$m> by 'ZR']
2919 Usage: $prog_name vf2zvp0 [<options>] <in.vf> [<out.zvp0>]
2920 $prog_name zvp02vf [<options>] <in.zvp0> [<out.vf>]
2921 $prog_name vf2zvp [<options>] <in.vf> [<in.tfm> <out.zvp>]
2922 $prog_name zvp2vf [<options>] <in.zvp> [<out.vf> <out.tfm>]
2923 $prog_name zpl2tfm [<options>] <in.zvp0> [<out.vf>]
2924 $prog_name tfm2zpl [<options>] <in.zvp0> [<out.vf>]
2925 Arguments:
2926 <in.xxx> input files
2927 N.B. Input TFM/VF files are searched by Kpathsea. (ZVP/ZVP9 are not.)
2928 <out.xxx> output files
2929 Options:
2930 --hex output charcode in 'H' form [default]
2931 -o / --octal output charcode in 'O' form
2932 --uptool use upTeX tools (uppltotf etc.)
2933 --lenient ignore non-fatal error on VFs
2934 --compact output VF in compact form
2935 The following options affect interpretation of 'K' form.
2936 --kanji=ENC set source encoding: ENC=jis/sjis/euc/utf8/none
2937 --kanji-internal=ENC set internal encoding: ENC=jis/unicode/none
2938 -j / --jis == --kanji=jis --kanji-internal=jis
2939 -u / --unicode == --kanji=utf8 --kanji-internal=unicode
2940 -E / --no-encoding == --kanji=none --kanji-internal=none
2941 EOT1
2942 -h / --help show this help message and exit
2943 -V / --version show version
2944 EOT2
2945 }
2946
2947 #### command-line options
2948
2949 sub read_option {
2950 my ($opt, $arg);
2951 $sw_hex = 1; $sw_uptool = 0;
2952 while ($ARGV[0] =~ m/^-/) {
2953 $opt = shift(@ARGV);
2954 if ($opt =~ m/^--?h(elp)?$/) {
2955 show_usage();
2956 } elsif ($opt =~ m/^-(?:V|-version)?$/) {
2957 show_version();
2958 } elsif ($opt eq '--hex') {
2959 $sw_hex = 1;
2960 } elsif ($opt eq '--octal' || $opt eq '-o') {
2961 $sw_hex = 0;
2962 } elsif ($opt eq '--uptool') {
2963 $sw_uptool = 1;
2964 } elsif ($opt eq '--lenient') {
2965 $sw_lenient = 1;
2966 } elsif ($opt eq '--no-encoding' || $opt eq '-E') {
2967 ($exenc, $inenc) = ('none', 'none');
2968 } elsif ($opt eq '--jis' || $opt eq '-j') {
2969 ($exenc, $inenc) = ('jis', 'jis');
2970 } elsif ($opt eq '--unicode' || $opt eq '-u') {
2971 ($exenc, $inenc) = ('utf8', 'unicode');
2972 } elsif (($arg) = $opt =~ m/^--kanji[=:](.*)$/) {
2973 $exenc = $arg;
2974 } elsif (($arg) = $opt =~ m/^--kanji-internal[=:](.*)$/) {
2975 $inenc = $arg;
2976 } elsif ($opt eq '--compact') {
2977 $sw_compact = 1;
2978 } else {
2979 error("invalid option", $opt);
2980 }
2981 }
2982 jcode_set($exenc)
2983 or error("unknown source kanji code: $exenc");
2984 jcode_set(undef, $inenc)
2985 or error("unknown internal kanji code: $inenc");
2986 #if ($inenc eq 'unicode') { $sw_uptool = 1; }
2987 if ($sw_hex) { pl_prefer_hex(1); }
2988 if ($sw_lenient) { vf_strict(0); }
2989 if ($proc_name eq 'vf2zvp0') {
2990 ($infile, $outfile) = fix_pathname(".vf", ".zvp0");
2991 } elsif ($proc_name eq 'zvp02vf') {
2992 ($infile, $outfile) = fix_pathname(".zvp0", ".vf");
2993 } elsif ($proc_name eq 'vf2zvp') {
2994 ($infile, $in2file, $outfile) =
2995 fix_pathname(".vf", ".tfm", ".zvp");
2996 } elsif ($proc_name eq 'zvp2vf') {
2997 ($infile, $outfile, $out2file) =
2998 fix_pathname(".zvp", ".vf", ".tfm");
2999 } elsif ($proc_name eq 'tfm2zpl') {
3000 ($infile, $outfile) = fix_pathname(".tfm", ".zpl");
3001 } elsif ($proc_name eq 'zpl2tfm') {
3002 ($infile, $outfile) = fix_pathname(".zpl", ".tfm");
3003 }
3004 if ($sw_compact && $proc_name ne 'zvp02vf') {
3005 alert("option unsupported for '$proc_name'", "--compact");
3006 }
3007 ($infile ne $outfile)
3008 or error("input and output file have same name", $infile);
3009 }
3010
3011 sub fix_pathname {
3012 my (@ext) = @_; my (@path);
3013 (0 <= $#ARGV && $#ARGV <= $#ext)
3014 or error("wrong number of arguments");
3015 @{$path[0]} = split_path($ARGV[0]);
3016 (defined $path[0][2]) or $path[0][2] = $ext[0];
3017 foreach (1 .. $#ext) {
3018 if (defined $ARGV[$_]) {
3019 @{$path[$_]} = split_path($ARGV[$_]);
3020 (defined $path[$_][2]) or $path[$_][2] = $ext[$_];
3021 } else {
3022 @{$path[$_]} = (undef, $path[0][1], $ext[$_]);
3023 }
3024 }
3025 return map { join('', @{$path[$_]}) } (0 .. $#_);
3026 }
3027
3028 sub split_path {
3029 my ($pnam) = @_; my ($dnam, $fbas, $ext);
3030 ($dnam, $fbas) = ($pnam =~ m|^(.*/)(.*)$|) ? ($1, $2) :
3031 (undef, $pnam);
3032 ($fbas, $ext) = ($fbas =~ m|^(.+)(\..*)$|) ? ($1, $2) :
3033 ($fbas, undef);
3034 return ($dnam, $fbas, $ext);
3035 }
3036
3037 #### user interface
3038
3039 sub show_info {
3040 print STDERR (join(": ", $prog_name, @_), "\n");
3041 }
3042
3043 sub alert {
3044 show_info("warning", @_);
3045 }
3046
3047 sub error {
3048 show_info((@_) ? (@_) : textool_error());
3049 exit(-1);
3050 }
3051
3052 #================================================= END
3053
3054 #------------------------------------------------- extra interfaces
3055
3056 *usage_message_org = \&usage_message;
3057
3058 *usage_message = sub {
3059 local ($_) = usage_message_org();
3060 my ($part1, $part2) = (<<"EOT1", <<"EOT2");
3061
3062 * ZVP Conversion
3063 EOT1
3064
3065 * VF Replication
3066 Usage: $prog_name vfcopy [<options>] <in.vf> <out.vf> <out_base.tfm>...
3067 $prog_name vfinfo [<options>] <in.vf>
3068 $prog_name jodel [<options>] <in.vf> <prefix>
3069 Arguments:
3070 <in.vf> input virtual font name
3071 N.B. Input TFM/VF files are searched by Kpathsea.
3072 <out.vf> output virtual font name
3073 <out_base.tfm> names of raw TFMs referred by the output virtual font;
3074 each entry replaces a font mapping in the input font in
3075 the given order, so the exactly same number of entries
3076 must be given as font mappings
3077 <prefix> prefix of output font names (only for jodel)
3078 Options:
3079 -z / --zero change first fontmap id in vf to zero
3080 --uptex assume input font to be for upTeX (only for jodel)
3081 --unicode generate VF for 'direct-unicode' mode imposed by pxufont
3082 package; this option is supported only for upTeX fonts and
3083 thus implies '--uptex' (only for jodel)
3084 --compact output VF in compact form
3085
3086 * VF Compaction
3087 Usage: $prog_name compact <in.vf> <out.vf>
3088
3089 * Common Options
3090 -h / --help show this help message and exit
3091 -V / --version show version
3092 EOT2
3093 s/(Usage:)/$part1$1/; s/\z/$part2/;
3094 return $_;
3095 };
3096
3097 %procs = (%procs,
3098 vfinfo => \&main_vfinfo,
3099 vfcopy => \&main_vfcopy,
3100 jodel => \&main_jodel,
3101 compact => \&main_compact,
3102 );
3103
3104 sub main_vfinfo {
3105 PXCopyFont::read_option('vfinfo');
3106 PXCopyFont::info_vf();
3107 }
3108
3109 sub main_vfcopy {
3110 PXCopyFont::read_option('vfcopy');
3111 PXCopyFont::copy_vf();
3112 }
3113
3114 sub main_jodel {
3115 PXCopyFont::read_option('jodel');
3116 PXCopyFont::jodel();
3117 }
3118
3119 sub main_compact {
3120 PXCompact::read_option('compact');
3121 PXCompact::compact();
3122 }
3123
3124 #------------------------------------------------- pxcopyfont stuffs
3125 package PXCopyFont;
3126
3127 *error = *main::error;
3128
3129 our ($src_main, $dst_main, @dst_base, $op_zero, $op_uptex, $op_quiet);
3130 our ($op_compact, $op_dbgone);
3131
3132 sub info {
3133 ($op_quiet) or ::show_info(@_);
3134 }
3135
3136 sub copy_vf {
3137 local $_ = ::read_whole_file(::kpse("$src_main.vf"), 1) or error();
3138 ($op_compact) and $_ = compact_vf($_);
3139 my $vfc = parse_vf($_);
3140 my ($nb, $nb1) = (scalar(@{$vfc->[0]}), scalar(@dst_base));
3141 info("number of base TFMs in '$src_main'", $nb);
3142 if ($dst_base[-1] eq '...' && $nb1 <= $nb) {
3143 foreach ($nb1-1 .. $nb-1) { $dst_base[$_] = $vfc->[0][$_][1]; }
3144 } elsif ($nb != $nb1) {
3145 error("wrong number of base TFMs given", $nb1);
3146 }
3147 ::write_whole_file("$dst_main.vf", form_vf($vfc), 1) or error();
3148 ::write_whole_file("$dst_main.tfm",
3149 ::read_whole_file(::kpse("$src_main.tfm"), 1), 1) or error();
3150 foreach my $k (0 .. $#dst_base) {
3151 my $sfn = $vfc->[0][$k][1]; my $dfn = $dst_base[$k];
3152 ($sfn ne $dfn) or next;
3153 ::write_whole_file("$dfn.tfm",
3154 ::read_whole_file(::kpse("$sfn.tfm"), 1), 1) or error();
3155 }
3156 }
3157
3158 sub parse_vf {
3159 my ($vf) = @_; my (@fs, @lst, $pos);
3160 @fs = unpack("CCC", $vf);
3161 ($fs[0] == 0xf7 && $fs[1] == 0xca) or return;
3162 $pos = $fs[2] + 11; my $hd = substr($vf, 0, $pos);
3163 while (1) {
3164 @fs = unpack("CC", substr($vf, $pos, 2));
3165 (243 <= $fs[0] && $fs[0] <= 246) or last;
3166 my $fid = ($fs[0] == 243) ? $fs[1] : 999;
3167 my $t = $fs[0] - 242 + 13;
3168 @fs = unpack("a${t}CC", substr($vf, $pos, 260));
3169 my $l = $fs[1] + $fs[2]; my $n = substr($vf, $pos + $t + 2, $l);
3170 $pos += $t + 2 + $l; push(@lst, [ $fs[0], $n, $fid ]);
3171 if ($n !~ m/^[\x21-\x7e]+$/) {
3172 $n =~ s/([^\x21-\x5b\x5d-\x7e])/sprintf("\\x%02x", ord($1))/g;
3173 error("bad tfm name recorded in VF", $n);
3174 }
3175 }
3176 my $ft = substr($vf, $pos); $ft =~ s/\xf8+\z//g;
3177 return [ \@lst, $hd, $ft ];
3178 }
3179
3180 sub info_vf {
3181 local $_ = ::read_whole_file(::kpse("$src_main.vf"), 1) or error();
3182 my $vfc = parse_vf($_);
3183 foreach (@{$vfc->[0]}) {
3184 printf("%d=%s\n", $_->[2], $_->[1]);
3185 }
3186 }
3187
3188 sub form_vf {
3189 my ($vfc) = @_; my (@lst);
3190 if ($op_zero) {{
3191 my $t = $vfc->[0][0] or last;
3192 ($t->[2] == 0) and last; # already zero
3193 info("change first fontmap id to zero (from " . $t->[2] . ")");
3194 substr($t->[0], 1, 1) = "\0"; $t->[2] = 0;
3195 }}
3196 foreach my $k (0 .. $#{$vfc->[0]}) {
3197 my $t = $vfc->[0][$k]; my $sfn = $t->[1];
3198 my $dfn = $dst_base[$k];
3199 (length($dfn) < 256) or error("TFM name too long", $dfn);
3200 info("id=".$t->[2], $sfn, $dfn);
3201 push(@lst, $t->[0], "\0" . chr(length($dfn)), $dfn);
3202 }
3203 my $tfm = join('', $vfc->[1], @lst, $vfc->[2]);
3204 return $tfm . ("\xf8" x (4 - length($tfm) % 4));
3205 }
3206
3207 sub compact_vf {
3208 my ($vf) = @_;
3209 my $pl = ::vf_parse($vf) or error();
3210 $pl = [ grep { !::is_simple_char($_) } (@$pl) ];
3211 $vf = ::vf_form($pl) or error();
3212 return $vf;
3213 }
3214
3215 sub read_option {
3216 my ($proc) = @_;
3217 $op_zero = 0; $op_uptex = 0; $op_quiet = 0;
3218 $op_compact = 0; $op_dbgone = 0;
3219 while ($ARGV[0] =~ m/^-/) {
3220 my $opt = shift(@ARGV);
3221 if ($opt =~ m/^--?h(elp)?$/) {
3222 ::show_usage();
3223 } elsif ($opt =~ m/^-(?:V|-version)?$/) {
3224 ::show_version();
3225 } elsif ($opt eq '-z' || $opt eq '--zero') {
3226 $op_zero = 1;
3227 } elsif ($opt eq '--uptex') {
3228 $op_uptex = 1;
3229 } elsif ($opt eq '--unicode') {
3230 $op_uptex = 2;
3231 } elsif ($opt eq '--compact') {
3232 $op_compact = 1;
3233 } elsif ($opt eq '--debug-one') { # undocumented
3234 $op_dbgone = 1;
3235 } elsif ($opt eq '--quiet') { # undocumented
3236 $op_quiet = 2;
3237 } else {
3238 error("invalid option", $opt);
3239 }
3240 }
3241 ($src_main, $dst_main, @dst_base) = @ARGV;
3242 $src_main =~ s/\.vf$//;
3243 (defined $src_main) or error("no argument given");
3244 (($proc eq 'vfinfo') ? (!defined $dst_main) :
3245 ($proc eq 'vfcopy') ? (defined $dst_main) :
3246 ($proc eq 'jodel') ? (defined $dst_main && $#dst_base == -1) : 1)
3247 or error("wrong number of arguments");
3248 if ($proc eq 'vfcopy') {
3249 $dst_main =~ s/\.vf$//;
3250 foreach (@dst_base) { s/\.tfm$//; }
3251 ($src_main ne $dst_main)
3252 or error("output vf name is same as input");
3253 (@dst_base) or error("no base tfm name given");
3254 }
3255 if ($proc eq 'jodel') {
3256 (!$op_zero) or error("invalid in jodel command", "-z/--zero");
3257 ($dst_main =~ m/^\w+$/)
3258 or error("bad characters in prefix", $dst_main);
3259 (length($dst_main) <= 100) or error("prefix too long", $dst_main);
3260 } else {
3261 (!$op_uptex) or error("invalid except in jodel command", "--uptex");
3262 }
3263 }
3264
3265 #------------------------------- jodel
3266
3267 our %standard_vf = (
3268 'rml' => [1, 'JhXXXN-h'],
3269 'rmlv' => [1, 'JhXXXN-v'],
3270 'uprml-h' => [2, 'uphXXXN-h'],
3271 'uprml-hq' => [2, 'jodhXXX-hq'],
3272 'uprml-v' => [2, 'uphXXXN-v'],
3273 'gbm' => [1, 'JhXXXN-h'],
3274 'gbmv' => [1, 'JhXXXN-v'],
3275 'upgbm-h' => [2, 'uphXXXN-h'],
3276 'upgbm-hq' => [2, 'jodhXXX-hq'],
3277 'upgbm-v' => [2, 'uphXXXN-v'],
3278 );
3279 our @shape = (
3280 'minl', 'minr', 'minb', 'gothr', 'gothb', 'gotheb', 'mgothr'
3281 );
3282
3283 our ($jengine, $jtate, @jvfname, %jvfidx, %jvfparsed);
3284
3285 sub jodel {
3286 ($op_dbgone) and @shape = @shape[1];
3287 jodel_analyze();
3288 if ($op_uptex == 2) {
3289 ($jengine == 2)
3290 or error("direct-unicode mode is only supported for pure upTeX fonts");
3291 foreach (values %standard_vf) {
3292 ($_->[1] =~ m/^jod/) and $_->[1] =~ s/jod/zu-jod/;
3293 }
3294 }
3295 foreach (@shape) {
3296 jodel_generate($_, '');
3297 jodel_generate($_, 'n');
3298 }
3299 }
3300
3301 sub jodel_vf_name {
3302 my ($shp, $nn, $idx) = @_;
3303 my $zu = ($op_uptex == 2) ? 'zu-' : '';
3304 my $i = ($idx > 0) ? "$idx" : '';
3305 my $up = (jodel_for_uptex()) ? 'up' : '';
3306 my $hv = ($jtate) ? 'v' : 'h';
3307 return "$zu$dst_main-$i-${up}nml$shp$nn-$hv";
3308 }
3309 sub jodel_tfm_name {
3310 my ($shp, $nn, $nam) = @_;
3311 local $_ = $nam; my $jod = ($nn eq 'n') ? 'jod' : '';
3312 s/XXX/\Q$shp\E/; s/N/\Q$nn\E/; s/J/\Q$jod\E/;
3313 return $_;
3314 }
3315 sub jodel_for_uptex {
3316 return ($jengine == 2 || ($jengine == 3 && $op_uptex));
3317 }
3318
3319 {
3320 my (%jkpse);
3321 sub jodel_kpse {
3322 my ($in) = @_;
3323 if (exists $jkpse{$in}) { return $jkpse{$in}; }
3324 my $out = ::kpse($in); $jkpse{$in} = $out;
3325 return $out;
3326 }
3327 }
3328
3329 sub jodel_clone {
3330 my ($val) = @_;
3331 if (ref($val) eq '') {
3332 return $val;
3333 } elsif (ref($val) eq 'ARRAY') {
3334 return [ map { jodel_clone($_) } (@$val) ];
3335 } else { error("OOPS", 98, ref($val)); }
3336 }
3337
3338 sub jodel_analyze {
3339 local ($_);
3340 info("**** Analyze VF '$src_main'");
3341 $_ = ::read_whole_file(jodel_kpse("$src_main.tfm"), 1) or error();
3342 $jtate = (unpack('n', $_) == 9);
3343 info("direction", ($jtate) ? 'tate' : 'yoko');
3344 @jvfname = ($src_main); $jengine = 0;
3345 info("base TFMs", "");
3346 for (my $i = 0; $i <= $#jvfname; $i++) {
3347 my $nvf = $jvfname[$i];
3348 $_ = ::read_whole_file(jodel_kpse("$nvf.vf"), 1)
3349 or error(($i > 0) ? ("non-standard raw TFM", $nvf) : ());
3350 ($op_compact) and $_ = compact_vf($_);
3351 $_ = parse_vf($_) or error();
3352 $jvfidx{$nvf} = $i; $jvfparsed{$nvf} = $_;
3353 my @lst = map { $_->[1] } @{$_->[0]};
3354 info(" $nvf -> @lst");
3355 foreach (@lst) {
3356 if (exists $standard_vf{$_}) {
3357 $jengine |= $standard_vf{$_}[0];
3358 next;
3359 }
3360 (exists $jvfidx{$_}) and next;
3361 push(@jvfname, $_);
3362 }
3363 }
3364 my $eng = (jodel_for_uptex()) ? 'upTeX' : 'pTeX';
3365 ($jengine == 3) and $eng .= ' (mixed)';
3366 info("engine", $eng);
3367 }
3368
3369 sub jodel_generate {
3370 my ($shp, $nn) = @_; local ($_);
3371 my $dnvf0 = jodel_vf_name($shp, $nn, 0);
3372 info("*** Generate VF '$dnvf0'");
3373 foreach my $i (0 .. $#jvfname) {
3374 my $snvf = $jvfname[$i];
3375 my $dnvf = jodel_vf_name($shp, $nn, $i);
3376 my $vfc = jodel_clone($jvfparsed{$snvf});
3377 my (@slst, @dlst);
3378 foreach my $e (@{$vfc->[0]}) {
3379 my $sbas = $e->[1]; my $dbas;
3380 if (exists $standard_vf{$sbas}) {
3381 $dbas = jodel_tfm_name($shp, $nn, $standard_vf{$sbas}[1]);
3382 } elsif (exists $jvfidx{$sbas}) {
3383 $dbas = jodel_vf_name($shp, $nn, $jvfidx{$sbas});
3384 } else { error("OOPS", 95, "$sbas"); }
3385 push(@slst, $sbas); push(@dlst, $dbas);
3386 $e->[1] = $dbas;
3387 }
3388 info("from", "$snvf -> @slst");
3389 info(" to", "$dnvf -> @dlst");
3390 ::write_whole_file("$dnvf.vf", jodel_form_vf($vfc), 1) or error();
3391 ::write_whole_file("$dnvf.tfm",
3392 ::read_whole_file(jodel_kpse("$snvf.tfm"), 1), 1) or error();
3393 }
3394 }
3395
3396 sub jodel_form_vf {
3397 my ($vfc) = @_; my (@lst);
3398 foreach my $k (0 .. $#{$vfc->[0]}) {
3399 my $t = $vfc->[0][$k]; my $dfn = $t->[1];
3400 push(@lst, $t->[0], "\0" . chr(length($dfn)), $dfn);
3401 }
3402 my $tfm = join('', $vfc->[1], @lst, $vfc->[2]);
3403 return $tfm . ("\xf8" x (4 - length($tfm) % 4));
3404 }
3405
3406 #------------------------------------------------- 'compact' stuffs
3407 package PXCompact;
3408
3409 *error = *main::error;
3410
3411 our ($src_name, $dst_name, $op_quiet);
3412
3413 sub info {
3414 ($op_quiet) or ::show_info(@_);
3415 }
3416
3417 sub num_chars {
3418 my ($pl) = @_; my $c = 0;
3419 foreach (@$pl) { $c += 1 if ($_->[0] eq 'CHARACTER'); }
3420 return $c;
3421 }
3422
3423 sub compact {
3424 local $_ = ::read_whole_file(::kpse("$src_name.vf"), 1) or error();
3425 my $pl = ::vf_parse($_) or error();
3426 my ($siz, $nc) = (length($_), num_chars($pl));
3427 info("from", "$siz bytes, $nc chars", "$src_name.vf");
3428 $pl = [ grep { !::is_simple_char($_) } (@$pl) ];
3429 $_ = ::vf_form($pl) or error();
3430 ($siz, $nc) = (length($_), num_chars($pl));
3431 ::write_whole_file("$dst_name.vf", $_, 1) or error();
3432 info(" to", "$siz bytes, $nc chars", "$dst_name.vf");
3433 }
3434
3435 sub read_option {
3436 my ($proc) = @_;
3437 $op_quiet = 0;
3438 while ($ARGV[0] =~ m/^-/) {
3439 my $opt = shift(@ARGV);
3440 if ($opt =~ m/^--?h(elp)?$/) {
3441 ::show_usage();
3442 } elsif ($opt =~ m/^-(?:V|-version)?$/) {
3443 ::show_version();
3444 } elsif ($opt eq '--quiet') { # undocumented
3445 $op_quiet = 2;
3446 } else {
3447 error("invalid option", $opt);
3448 }
3449 }
3450 ($#ARGV == 1) or error("wrong number of arguments");
3451 ($src_name, $dst_name) = @ARGV;
3452 $src_name =~ s/\.vf$//; $dst_name =~ s/\.vf$//;
3453 }
3454
3455 #------------------------------------------------- go to main
3456 package main;
3457 main();
3458 ## EOF