Debian Codegraph @

texk/texlive/linked_scripts/accfonts/vpl2vpl

raw
1 #!/usr/bin/perl
2
3 # Vpl2vpl: a program to generate accented virtual fonts for TeX
4 # Copyright (C) 1997 John D. Smith
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 $version = 0.25;
21 #------------------------------------------------------------------------#
22 $description =
23 "Syntax: vpl2vpl -d definition-file [-s shrink-factor]
24 [-c candrabindu-adjustment] [-b] vpl-file
25
26 Vpl2vpl creates new TeX virtual fonts based on existing fonts or
27 virtual fonts (\"input fonts\"). A successful run will read a pl
28 (Property List) or vpl (Virtual Property List) file and a definition
29 file, and will generate a new vpl (Virtual Property List) file on
30 standard output. The input font is assumed to adhere to the standard
31 TeX encoding for text fonts unless it was created with either of the
32 programs afm2pl or afm2tfm, in which case it is assumed to conform to
33 (respectively) the Adobe Standard Encoding or the encoding specified
34 in the file dvips.enc. In either case, the name of the input font is
35 assumed to be the name of the input file without its .vpl or .pl
36 extension: it must conform to normal TeX conventions for naming fonts,
37 as vpl2vpl attempts to draw conclusions from it about the kind of font
38 it is dealing with.
39
40 A typical complete sequence of commands to create a new virtual
41 font might therefore be
42 tftopl cmr10.tfm cmr10.pl
43 vpl2vpl -d ISO-Latin1.def cmr10.pl >cmr10_isol1.vpl
44 vptovf cmr10_isol1.vpl cmr10_isol1.vf cmr10_isol1.tfm
45 for a Computer Modern font, or
46 afm2pl Times-Roman.afm rptmr.pl
47 pltotf rptmr.pl rptmr.tfm
48 vpl2vpl -d ISO-Latin1.def rptmr.pl >ptmr-isol1.vpl
49 vptovf ptmr-isol1.vpl ptmr-isol1.vf ptmr-isol1.tfm
50 for a PostScript font.
51
52 Another approach for a PostScript font is to use afm2tfm:
53 afm2tfm Times-Roman.afm -t dvips.enc -v ptmr rptmr
54 vpl2vpl -d ISO-Latin1.def ptmr.vpl >ptmr-isol1.vpl
55 vptovf ptmr-isol1.vpl ptmr-isol1.vf ptmr-isol1.tfm
56 -- but this is now deprecated, as afm2tfm generates incorrect
57 values for the heights of some characters, and this can lead to
58 bad accent placing.
59
60 In order to keep the whole upper half of the character set free for
61 the requirements of the encoding specified in the definition file,
62 certain modifications are made to input fonts following the
63 dvips.enc encoding to bring them into greater conformity with the
64 TeX norm. In particular, the characters dotaccent and hungarumlaut
65 are placed in the positions assigned by TeX (\"5F, \"7D), not those
66 enforced by dvips.enc (\"C7, \"CD). The f-ligatures, double quotes
67 and dashes are also moved from the upper half of the character set
68 to their normal TeX positions. As a result, the following characters
69 are not found in the lower half of the character set: quotesingle,
70 quotedbl, backslash, underscore, braceleft, bar, braceright. These
71 characters can, however, be assigned positions in the output font if
72 they are needed. (Indeed, they could all be explicitly restored to
73 their dvips.enc positions if this were desired.)
74
75 Options:
76
77 -d should refer to a font definition file. This file (which could
78 usefully be named, e.g., \"French.def\") should consist of
79 lines of character definitions, in the form
80 \"number\" \"character\"
81 or
82 \"number\" \"character\" \"accent\"
83 Here \"number\" represents the character's position in the new
84 encoding and may be expressed in decimal, octal or hex;
85 \"character\" names the character (e.g. \"comma\", \"eight\",
86 \"A\") or consists of the word \".notdef\" (indicating that
87 the specified number's \"slot\" in the new encoding is to be
88 empty); and \"accent\" optionally names an accent to be placed
89 on the character. In addition to the standard accents available
90 in PostScript fonts, \"underbar\" and \"underdot\" are also
91 available, as are \"under\" versions of all the normal
92 superscript accents (\"underdieresis\", \"underring\", etc.).
93 The Indian accent \"candrabindu\" may also be specified: it
94 is formed by overprinting a breve with a dotaccent. Finally,
95 \"overdot\" may be used as a synonym for \"dotaccent\".
96
97 If the character named in the \"accent\" position is not in fact
98 a valid accent character, the program interprets the definition
99 as a request for a digraph formed from the \"character\" and the
100 \"accent\". A digraph consisting of, say, \"k\" and \"h\" will be
101 indistinguishable from the letters \"k\" and \"h\" printed
102 consecutively, but the digraph \"kh\" can itself receive accents
103 like any other character: see next paragraph.
104
105 A new character (such as \"amacron\" or \"kh\") may be freely
106 used in the \"character\" position of a further definition (such
107 as \"amacron breve\" or \"kh underbar\"). There is no constraint
108 on the ordering of definitions within a definition file. The
109 definition of \"a macron\" does not have to precede that of
110 \"amacron breve\": requests for \"impossible\" characters are
111 deferred until their constituents have had a chance to come into
112 being.
113
114 \"Slots\" for which no new definition is given retain the
115 definition they have in the input font.
116
117 The definition file may also contain blank lines and comments
118 (introduced by \"\#\").
119
120 -s may optionally give the factor, expressed as a per-thousand
121 value, by which normally superscript accents (such as dieresis,
122 ring) should be shrunk when they are used as subscript accents
123 (such as underdieresis, underring). Values of around 800 may be
124 found useful.
125
126 -c may optionally give two comma-separated numerical values to
127 adjust the x and y coordinates of the dotaccent placed within a
128 breve to form the candrabindu accent. A coordinate scheme using
129 \"DESIGNUNITS R 1000\" is assumed.
130
131 -b may optionally be specified to block the use of predefined
132 accented characters, forcing vpl2vpl to define its own
133 versions. This may be useful to secure a consistent appearance
134 in cases where a font designer does not share vpl2vpl's views
135 on where accents should be placed.
136
137 -h prints this help.
138 ";
139 #------------------------------------------------------------------------#
140
141 ########################
142 # Packages and constants
143 ########################
144 #
145 use File::Basename;
146 use Getopt::Std;
147 $cmdline = basename($0) . " " . join " ", @ARGV;
148 getopts('d:s:c:bh');
149 if ($opt_h or !$opt_d or $#ARGV != 0) {
150 print STDERR $description;
151 exit 1;
152 }
153
154 $filename = $ARGV[0];
155 ($fontname = $filename) =~ s/\..*$//;
156 ($encname = basename($opt_d)) =~ s/\..*$//;
157 $vtitle = "(VTITLE Font $fontname modified for $encname encoding by vpl2vpl";
158 $vtitle .= " v. $version" if $version;
159 $vtitle .= ")\n(COMMENT Command line: $cmdline)";
160
161 #
162 # Flags for bold and small caps. These are probably a bit iffy, but
163 # there's not much that can be done about it.
164 #
165 if ($fontname =~ /(^p.*b[oi]?[c]?$|^[^p].*bx[a-z]*[0-9]+$)/) { $bold = 1 }
166 if ($fontname =~ /(^p.*c$|^[^p].*csc[a-z]*[0-9]+$)/) { $scaps = 1 }
167 if ($opt_s) { $shrink = $opt_s / 1000 } else { $shrink = 1 }
168
169 #
170 # Array to convert from number to vpl representation
171 #
172 foreach $i (0 .. 255) {
173 $nv[$i] = (chr($i) =~ /[0-9A-Za-z]/ ? "C " . chr $i : sprintf("O %lo", $i));
174 }
175
176 #
177 # Now the encoding vectors.
178 #
179 @TeXenc = (
180 "Gamma", "Delta", "Theta", "Lambda",
181 "Xi", "Pi", "Sigma", "Upsilon",
182 "Phi", "Psi", "Omega", "ff",
183 "fi", "fl", "ffi", "ffl",
184 "dotlessi", "dotlessj", "grave", "acute",
185 "caron", "breve", "macron", "ring",
186 "cedilla", "germandbls", "ae", "oe",
187 "oslash", "AE", "OE", "Oslash",
188 "space", "exclam", "quotedblright", "numbersign",
189 "dollar", "percent", "ampersand", "quoteright",
190 "parenleft", "parenright", "asterisk", "plus",
191 "comma", "hyphen", "period", "slash",
192 "zero", "one", "two", "three",
193 "four", "five", "six", "seven",
194 "eight", "nine", "colon", "semicolon",
195 "exclamdown", "equal", "questiondown", "question",
196 "at", "A", "B", "C",
197 "D", "E", "F", "G",
198 "H", "I", "J", "K",
199 "L", "M", "N", "O",
200 "P", "Q", "R", "S",
201 "T", "U", "V", "W",
202 "X", "Y", "Z", "bracketleft",
203 "quotedblleft", "bracketright", "circumflex", "dotaccent",
204 "quoteleft", "a", "b", "c",
205 "d", "e", "f", "g",
206 "h", "i", "j", "k",
207 "l", "m", "n", "o",
208 "p", "q", "r", "s",
209 "t", "u", "v", "w",
210 "x", "y", "z", "endash",
211 "emdash", "hungarumlaut", "tilde", "dieresis"
212 );
213
214 @dvipsenc = (
215 ".notdef", ".notdef", ".notdef", ".notdef",
216 ".notdef", ".notdef", ".notdef", ".notdef",
217 ".notdef", ".notdef", ".notdef", ".notdef",
218 ".notdef", "quotesingle", "exclamdown", "questiondown",
219 "dotlessi", "dotlessj", "grave", "acute",
220 "caron", "breve", "macron", "ring",
221 "cedilla", "germandbls", "ae", "oe",
222 "oslash", "AE", "OE", "Oslash",
223 "space", "exclam", "quotedbl", "numbersign",
224 "dollar", "percent", "ampersand", "quoteright",
225 "parenleft", "parenright", "asterisk", "plus",
226 "comma", "hyphen", "period", "slash",
227 "zero", "one", "two", "three",
228 "four", "five", "six", "seven",
229 "eight", "nine", "colon", "semicolon",
230 "less", "equal", "greater", "question",
231 "at", "A", "B", "C",
232 "D", "E", "F", "G",
233 "H", "I", "J", "K",
234 "L", "M", "N", "O",
235 "P", "Q", "R", "S",
236 "T", "U", "V", "W",
237 "X", "Y", "Z", "bracketleft",
238 "backslash", "bracketright", "circumflex", "underscore",
239 "quoteleft", "a", "b", "c",
240 "d", "e", "f", "g",
241 "h", "i", "j", "k",
242 "l", "m", "n", "o",
243 "p", "q", "r", "s",
244 "t", "u", "v", "w",
245 "x", "y", "z", "braceleft",
246 "bar", "braceright", "tilde", "dieresis",
247 "asciicircum", "asciitilde", "Ccedilla", "Iacute",
248 "Icircumflex", "atilde", "edieresis", "egrave",
249 "scaron", "zcaron", "Eth", "ff",
250 "ffi", "ffl", ".notdef", ".notdef",
251 ".notdef", ".notdef", "Scaron", ".notdef",
252 ".notdef", ".notdef", ".notdef", ".notdef",
253 "Ydieresis", ".notdef", "Zcaron", ".notdef",
254 ".notdef", ".notdef", ".notdef", ".notdef",
255 ".notdef", ".notdef", "cent", "sterling",
256 "fraction", "yen", "florin", "section",
257 "currency", "copyright", "quotedblleft", "guillemotleft",
258 "guilsinglleft", "guilsinglright", "fi", "fl",
259 "degree", "endash", "dagger", "daggerdbl",
260 "periodcentered", ".notdef", "paragraph", "bullet",
261 "quotesinglbase", "quotedblbase", "quotedblright", "guillemotright",
262 "ellipsis", "perthousand", ".notdef", ".notdef",
263 "Agrave", "Aacute", "Acircumflex", "Atilde",
264 "Adieresis", "Aring", ".notdef", "dotaccent",
265 "Egrave", "Eacute", "Ecircumflex", "Edieresis",
266 "Igrave", "hungarumlaut", "ogonek", "Idieresis",
267 "emdash", "Ntilde", "Ograve", "Oacute",
268 "Ocircumflex", "Otilde", "Odieresis", ".notdef",
269 ".notdef", "Ugrave", "Uacute", "Ucircumflex",
270 "Udieresis", "Yacute", "Thorn", ".notdef",
271 "agrave", "aacute", "acircumflex", "ordfeminine",
272 "adieresis", "aring", ".notdef", "ccedilla",
273 "Lslash", "eacute", "ecircumflex", "ordmasculine",
274 "igrave", "iacute", "icircumflex", "idieresis",
275 ".notdef", "ntilde", "ograve", "oacute",
276 "ocircumflex", "otilde", "odieresis", ".notdef",
277 "lslash", "ugrave", "uacute", "ucircumflex",
278 "udieresis", "yacute", "thorn", "ydieresis"
279 );
280
281 @adobeenc=(
282 ".notdef", ".notdef", ".notdef", ".notdef",
283 ".notdef", ".notdef", ".notdef", ".notdef",
284 ".notdef", ".notdef", ".notdef", ".notdef",
285 ".notdef", ".notdef", ".notdef", ".notdef",
286 ".notdef", ".notdef", ".notdef", ".notdef",
287 ".notdef", ".notdef", ".notdef", ".notdef",
288 ".notdef", ".notdef", ".notdef", ".notdef",
289 ".notdef", ".notdef", ".notdef", ".notdef",
290 "space", "exclam", "quotedbl", "numbersign",
291 "dollar", "percent", "ampersand", "quoteright",
292 "parenleft", "parenright", "asterisk", "plus",
293 "comma", "hyphen", "period", "slash",
294 "zero", "one", "two", "three",
295 "four", "five", "six", "seven",
296 "eight", "nine", "colon", "semicolon",
297 "less", "equal", "greater", "question",
298 "at", "A", "B", "C",
299 "D", "E", "F", "G",
300 "H", "I", "J", "K",
301 "L", "M", "N", "O",
302 "P", "Q", "R", "S",
303 "T", "U", "V", "W",
304 "X", "Y", "Z", "bracketleft",
305 "backslash", "bracketright", "asciicircum", "underscore",
306 "quoteleft", "a", "b", "c",
307 "d", "e", "f", "g",
308 "h", "i", "j", "k",
309 "l", "m", "n", "o",
310 "p", "q", "r", "s",
311 "t", "u", "v", "w",
312 "x", "y", "z", "braceleft",
313 "bar", "braceright", "asciitilde", ".notdef",
314 ".notdef", ".notdef", ".notdef", ".notdef",
315 ".notdef", ".notdef", ".notdef", ".notdef",
316 ".notdef", ".notdef", ".notdef", ".notdef",
317 ".notdef", ".notdef", ".notdef", ".notdef",
318 ".notdef", ".notdef", ".notdef", ".notdef",
319 ".notdef", ".notdef", ".notdef", ".notdef",
320 ".notdef", ".notdef", ".notdef", ".notdef",
321 ".notdef", ".notdef", ".notdef", ".notdef",
322 ".notdef", "exclamdown", "cent", "sterling",
323 "fraction", "yen", "florin", "section",
324 "currency", "quotesingle", "quotedblleft", "guillemotleft",
325 "guilsinglleft", "guilsinglright", "fi", "fl",
326 ".notdef", "endash", "dagger", "daggerdbl",
327 "periodcentered", ".notdef", "paragraph", "bullet",
328 "quotesinglbase", "quotedblbase", "quotedblright", "guillemotright",
329 "ellipsis", "perthousand", ".notdef", "questiondown",
330 ".notdef", "grave", "acute", "circumflex",
331 "tilde", "macron", "breve", "dotaccent",
332 "dieresis", ".notdef", "ring", "cedilla",
333 ".notdef", "hungarumlaut", "ogonek", "caron",
334 "emdash", ".notdef", ".notdef", ".notdef",
335 ".notdef", ".notdef", ".notdef", ".notdef",
336 ".notdef", ".notdef", ".notdef", ".notdef",
337 ".notdef", ".notdef", ".notdef", ".notdef",
338 ".notdef", "AE", ".notdef", "ordfeminine",
339 ".notdef", ".notdef", ".notdef", ".notdef",
340 "Lslash", "Oslash", "OE", "ordmasculine",
341 ".notdef", ".notdef", ".notdef", ".notdef",
342 ".notdef", "ae", ".notdef", ".notdef",
343 ".notdef", "dotlessi", ".notdef", ".notdef",
344 "lslash", "oslash", "oe", "germandbls",
345 ".notdef", ".notdef", ".notdef", ".notdef"
346 );
347
348 ###############
349 # Read DEF file
350 ###############
351 #
352 open DEF, $opt_d or die "Cannot open $opt_d: $!\n";
353 while (<DEF>) {
354 next if (/^\s*$/ || /^\#/);
355 s/\s*(\#.*)?$//;
356 push @deflines, $_;
357 }
358 close DEF;
359
360 ###############
361 # Read VPL file
362 ###############
363 #
364 # File header
365 #
366 $vplhead = <> or exit 1;
367 unless ($vplhead =~ /^\((VTITLE|FAMILY) /) {
368 die "$filename is not a vpl file: giving up\n"
369 }
370 do {
371 $_ = <>;
372 $vplhead .= $_;
373 } until ($_ =~ /^\(LIGTABLE$/ or eof);
374 if (eof) {
375 die "$filename does not seem to be a text font (no LIGTABLE): giving up\n";
376 }
377 if ($vplhead =~ s/\A\(VTITLE(.*)$/$vtitle\n(COMMENT Old vtitle:$1/m) {
378 $vplhead =~ s/\n\(COMMENT Please edit that VTITLE .*\)$//m;
379 @enc = @dvipsenc;
380 $dvips = 1;
381 }
382 elsif ($vplhead =~ /\A\(FAMILY.*\)\n\(CODINGSCHEME ADOBESTANDARDENCODING\)$/m) {
383 $vplhead =~ s/\A/$vtitle\n/m;
384 @enc = @adobeenc;
385 $dvips = 0;
386 }
387 else {
388 $vplhead =~ s/\A/$vtitle\n/m;
389 @enc = @TeXenc;
390 $dvips = 0;
391 }
392 if ($vplhead =~ /^\(CODINGSCHEME TEX MATH SYMBOLS/m) {
393 die "$filename is a TeX math font: giving up\n";
394 }
395 unless ($vplhead =~ s/^(\(CODINGSCHEME .*\+\s?)(\S+)\)$/$1$encname)/m) {
396 $vplhead =~ s/^(\(CODINGSCHEME .*)\)$/$1 + $encname)/m;
397 }
398 if ($vplhead =~ /^\(DESIGNUNITS R (.+)\)/m) { $scale = $1 }
399 else {$scale = 1 }
400 if ($vplhead =~ /^ \(SLANT R (.+)\)/m) { $slant = $1 }
401 if ($vplhead =~ /^ \(XHEIGHT [DR] (.+)\)/m) { $xheight = $1 }
402 unless ($vplhead =~ /^\(MAPFONT /m) {
403 if ($vplhead =~ /^\(DESIGNSIZE R (.*)\)$/m) { $dsize = $1 }
404 $mapfont = "\n(MAPFONT D 0\n (FONTNAME $fontname)\n (FONTDSIZE R $dsize)\n )";
405 $vplhead =~ s/\n\(LIGTABLE\Z/$mapfont$&/m;
406 }
407 if ($opt_s) {
408 $vplhead =~ s[^(\(MAPFONT D )0(.*?)( \))]
409 [$&\n${1}1$2 (FONTAT R ${ \($shrink * $scale) })\n$3]ms;
410 }
411
412 #
413 # Ligatures and kerns
414 #
415 do {
416 $_ = <>;
417 s/ \(comment .*$//i;
418 $ligs .= $_;
419 } until $_ =~ /^ \)/;
420 #
421 # Now build a hash to convert from vpl representation to char name
422 # and use it to make ligtable readable
423 #
424 foreach $i (0 .. 255) { $vc{$nv[$i]} = $enc[$i] }
425 $ligs =~ s/^( \((?:LABEL|KRN) )(\S+ \S+)(.*\))$/$1$vc{$2}$3/gm;
426 $ligs =~ s/^( \(LIG )(\S+ \S+) (\S+ \S+)\)$/$1$vc{$2} $vc{$3})/gm;
427
428 #
429 # Character definitions: store "encoded" defs in @chars, store *all*
430 # defs in %allchars
431 #
432 $_ = <>;
433 do {
434 if (/^\(CHARACTER/) {
435 $character = $_;
436 do {
437 $_ = <>;
438 $character .= $_;
439 } until $_ =~ /^ \)/;
440 storeinfo($character);
441 $_ = <>;
442 }
443 } until eof;
444 foreach $i (0 .. $#chars) {
445 if ($chars[$i] and $enc[$i] ne ".notdef") {
446 $allchars{$enc[$i]} = $chars[$i];
447 }
448 }
449
450 ##################
451 # Set up constants
452 ##################
453
454 $subacc = "(cedilla|ogonek|commaaccent)";
455 $supacc = "(grave|acute|circumflex|tilde|macron|breve|dotaccent|overdot|dieresis|ring|hungarumlaut|caron|candrabindu)";
456 $underacc = "(underdot|under$supacc)";
457 $accents = "($subacc|$supacc|$underacc|underbar)";
458 $underadp = 0.230; # depth of "under" accs
459 $underddp = 0.213; # depth of underdot
460 if ($bold) { $thk = 0.072 } else { $thk = 0.052 } # thickness and
461 $underbdp = 0.082 + $thk; # depth of underbar
462 $capheight = $allchars{"X"}{ht};
463 $accheight = $allchars{"macron"}{ht};
464 $accdepth = $accheight - $thk * $scale; # probable approx. "depth" of macron
465 $v1 = $accheight - $xheight; # vertical offset for double accents
466 $v2 = $capheight - $xheight; # vertical offset for accented caps etc
467 if ($scaps) { # accented small caps
468 $scoffset = $allchars{"x"}{ht} - $xheight;
469 $v1 += $scoffset;
470 }
471 if ($opt_c) { # candrabindu
472 ($cbx, $cby) = $opt_c =~ /^(.*),(.*)$/;
473 $cbx += ($allchars{"breve"}{wd} - $allchars{"dotaccent"}{wd}) / 2;
474 $cbx /= 1000;
475 $cby /= 1000;
476 }
477
478 ######################
479 # Build the characters
480 ######################
481 #
482 # First normalise dvips.enc encoding quirks
483 #
484 if ($dvips) {
485 chmove("fi", 014);
486 chmove("fl", 015);
487 chmove("quotedblright", 042);
488 chmove("quotedblleft", 0134);
489 chmove("dotaccent", 0137);
490 chmove("endash", 0173);
491 chmove("emdash", 0174);
492 chmove("hungarumlaut", 0175);
493 }
494 #
495 # Now build a list of definitions supplied by user
496 #
497 for (@deflines) {
498 if (/^\s*(\d+|0[0-7]+|0x[0-9a-fA-F]+)\s+([a-zA-Z]+?|\.notdef)(?:\s+([a-zA-Z]+))?$/) {
499 ($num, $char, $acc) = ($1, $2, $3);
500 $num = oct $num if $num =~ /^0/;
501 if ($num > 255) { die "Bad definition (number out of range): $_\n" }
502 $def = {};
503 $def->{qdef} = $_;
504 $def->{num} = $num;
505 $def->{char} = $char;
506 $def->{acc} = $acc;
507 $def->{nchar} = $char . $acc;
508 push @nchars, $def->{nchar};
509 push @defs, $def;
510 }
511 else { die "Bad definition: $_\n" }
512 }
513 #
514 # Work through the list
515 #
516 while (@defs) {
517 $def = shift @defs;
518 $qdef = $def->{qdef};
519 $num = $def->{num};
520 $char = $def->{char};
521 $acc = $def->{acc};
522 $nchar = $def->{nchar};
523 #
524 # If we can't handle $char/$acc yet, but believe we will be able
525 # to later, send the definition to the back of the queue. In case
526 # it later turns out we were wrong, allow only five loops before
527 # giving up.
528 #
529 if (!$allchars{$char} and $char ne ".notdef") {
530 if (grep /^$char$/, @nchars) {
531 unless (++$def->{requeue} > 5) {
532 push @defs, $def;
533 next;
534 }
535 }
536 else { die "Bad definition (no such character): $qdef\n" }
537 }
538 if ($acc and !$allchars{$acc} and $acc !~ /^$accents$/) {
539 if (grep /^$acc$/, @nchars) {
540 unless (++$def->{requeue} > 5) {
541 push @defs, $def;
542 next;
543 }
544 }
545 else { die "Bad definition (no such accent): $qdef\n" }
546 }
547 #
548 # Remove any existing claims on $num
549 #
550 @{ $allchars{$chars[$num]{id}}{num} } = grep !/$num/,
551 @{ $allchars{$chars[$num]{id}}{num} };
552 #
553 # First deal with .notdef
554 #
555 if ($nchar eq ".notdef") {
556 undef $chars[$num];
557 }
558 #
559 # Next look among existing chars (unless blocked by -b)
560 #
561 elsif (!($acc and $opt_b) and $allchars{$nchar}) {
562 push( @{ $allchars{$nchar}{num} }, $num);
563 $chars[$num] = $allchars{$nchar};
564 }
565 #
566 # If it can't be built from sub-elements, issue a warning and move on
567 #
568 elsif (!$acc) {
569 warn "No such character - ignoring definition: $qdef\n";
570 undef $chars[$num];
571 }
572 #
573 # Now build the char
574 #
575 else {
576 #
577 # First get rid of predefined/duplicated ligtable statements
578 # and character definitions; also synonyms
579 #
580 $ligs =~ s/\n \((LABEL|KRN|LIG) ($nchar .*|.*$nchar)\)$//gm;
581 $allchars{$nchar} = ();
582 if ($acc eq "overdot") {
583 $nchar2 = $char . "dotaccent";
584 $ligs =~ s/\n \((LABEL|KRN|LIG) ($nchar2 .*|.*$nchar2)\)$//gm;
585 delete $allchars{$nchar2};
586 }
587 #
588 # Go!
589 #
590 if ($acc =~ /^$subacc$/) {
591 subacc($num, $char, $acc, $nchar);
592 fixkerns($char, $acc);
593 }
594 elsif ($acc =~ /^$supacc$/) {
595 supacc($num, $char, $acc, $nchar);
596 fixkerns($char, $acc);
597 }
598 elsif ($acc =~ /^$underacc$/) {
599 underacc($num, $char, $acc, $nchar);
600 fixkerns($char, $acc);
601 }
602 elsif ($acc =~ /^underbar$/) {
603 underb($num, $char, $nchar);
604 fixkerns($char, $acc);
605 }
606 else {
607 digraph($num, $char, $acc, $nchar);
608 fixkerns($char, $acc);
609 }
610 }
611 }
612
613 ###################
614 # Sort out ligtable
615 ###################
616 #
617 # Convert to vpl representation, eliminating statements invoking
618 # "unencoded" characters
619 #
620 @liglist = split /\n/, $ligs;
621 $ligs = "";
622 foreach (@liglist) {
623 if (/^( \(LIG \S+ )(\S+)\)$/) {
624 if ($n = ${ $allchars{$2}{num} }[0]) {
625 s/^( \(LIG \S+ )(\S+)\)$/$1$nv[$n])/;
626 }
627 else { next }
628 }
629 if (/^( \((?:LABEL|LIG|KRN) )([^ )]+)(.*)$/) {
630 ($one, $two, $three) = ($1, $2, $3);
631 foreach $n (@{ $allchars{$two}{num} }) {
632 $ligs .= "$one$nv[$n]$three\n";
633 }
634 }
635 else { $ligs .= "$_\n" }
636 }
637 #
638 # Eliminate sequences orphaned by elimination of a LABEL
639 #
640 @liglist = split / \(STOP\)\n/, $ligs;
641 $ligs = "";
642 foreach (@liglist) {
643 if (/^ \(LABEL /m) { $ligs .= "$_ (STOP)\n" }
644 elsif (/^ \)$/m) { $ligs .= $_ }
645 }
646 #
647 # Eliminate empty statements
648 #
649 $ligs =~ s/(^ \(LABEL .*\)\n)+ \(STOP\)\n//gm;
650
651 ####################
652 # Output the results
653 ####################
654 #
655 print $vplhead, $ligs;
656 foreach $i (0 .. 255) { if (defined $chars[$i]{id}) { printchar($i) } }
657
658 #####################
659 # End of main program
660 #####################
661
662 sub storeinfo {
663 #
664 # Extract info from a character definition and store it in @chars
665 #
666 my $char = shift;
667 my $num;
668 if ($char =~ /\A\(CHARACTER O ([0-7]+)/m) { $num = oct $1 }
669 elsif ($char =~ /\A\(CHARACTER C (.)/m) { $num = ord $1 }
670 $chars[$num]{id} = $enc[$num];
671 push( @{ $chars[$num]{num} }, $num);
672 if ($char =~ /^ \(CHARWD R (.*?)\)$/m) { $chars[$num]{wd} = $1 }
673 if ($char =~ /^ \(CHARHT R (.*?)\)$/m) { $chars[$num]{ht} = $1 }
674 if ($char =~ /^ \(CHARDP R (.*?)\)$/m) { $chars[$num]{dp} = $1 }
675 if ($char =~ /^ \(CHARIC R (.*?)\)$/m) { $chars[$num]{ic} = $1 }
676 if ($char =~ /^ \(MAP\n((.|\n)*)^ \)/m) { $chars[$num]{map} = $1 }
677 else { $chars[$num]{map} = " (SETCHAR $nv[$num])\n" }
678 }
679
680 sub printchar {
681 #
682 # Extract info from @chars and build it into a character definition
683 #
684 my $num = shift;
685 print "(CHARACTER ";
686 if (chr($num) =~ /[0-9A-Za-z]/) { print "C " . chr($num) }
687 else {
688 printf "O %lo", $num;
689 print " (COMMENT " . $chars[$num]{id} . ")";
690 }
691 print "\n";
692 print " (CHARWD R " . $chars[$num]{wd} . ")\n" if $chars[$num]{wd};
693 print " (CHARHT R " . $chars[$num]{ht} . ")\n" if $chars[$num]{ht};
694 print " (CHARDP R " . $chars[$num]{dp} . ")\n" if $chars[$num]{dp};
695 print " (CHARIC R " . $chars[$num]{ic} . ")\n" if $chars[$num]{ic};
696 print " (MAP\n";
697 print $chars[$num]{map};
698 print " )\n";
699 print " )\n";
700 }
701
702 sub chmove {
703 #
704 # Move a character
705 #
706 my ($char, $num) = @_;
707 my $i;
708 foreach $i (@{ $allchars{$char}{num} }) { undef $chars[$i] }
709 @{ $allchars{$char}{num} } = ();
710 @{ $allchars{$chars[$num]{id}}{num} } = grep !/$num/,
711 @{ $allchars{$chars[$num]{id}}{num} };
712 push( @{ $allchars{$char}{num} }, $num);
713 $chars[$num] = $allchars{$char};
714 }
715
716 sub max {
717 #
718 # Return greater of two values
719 #
720 my ($a, $b) = @_;
721 return $a > $b ? $a : $b;
722 }
723
724 sub subacc {
725 #
726 # Subscript accents
727 #
728 my ($num, $char, $acc, $id) = @_;
729 my ($h, $s1, $s2, $s3);
730 $allchars{$id}{wd} = $allchars{$char}{wd};
731 $allchars{$id}{ht} = $allchars{$char}{ht};
732 $allchars{$id}{dp} = $allchars{$acc}{dp};
733 $allchars{$id}{ic} = $allchars{$char}{ic};
734 $allchars{$id}{id} = $id;
735 push( @{ $allchars{$id}{num} }, $num);
736 $s1 = $allchars{$char}{map};
737 $s1 =~ s/\A (.*)\n\Z/(PUSH) $1 (POP)/s;
738 $h = sprintf("%.3f", ($allchars{$char}{wd} - $allchars{$acc}{wd}) / 2);
739 if ($h > 0) { $s2 = " (MOVERIGHT R $h) " }
740 elsif ($h < 0) {
741 $h = -$h;
742 $s2 = " (MOVELEFT R $h) ";
743 }
744 else { $s2 = " " }
745 $s3 = $allchars{$acc}{map};
746 $s3 =~ s/\A.*(\(SETCHAR . .+?\)).*\Z/$1/s;
747 $allchars{$id}{map} = " $s1\n$s2$s3\n";
748 $chars[$num] = $allchars{$id};
749 }
750
751 sub supacc {
752 #
753 # Superscript accents
754 #
755 my ($num, $char, $acc, $id) = @_;
756 my ($cb, $h, $hadj, $tallchar, $ic, $s1, $s2, $s3);
757 if ($char eq "i" and $allchars{"dotlessi"}) { $char = "dotlessi" }
758 if ($char eq "j" and $allchars{"dotlessj"}) { $char = "dotlessj" }
759 if ($acc eq "overdot") { $acc = "dotaccent" }
760 if ($acc eq "candrabindu") {
761 $acc = "breve";
762 ($cb = $allchars{"dotaccent"}{map}) =~ s/\A.*(\(SETCHAR . .+?\)).*\Z/$1/s;
763 }
764 $allchars{$id}{wd} = $allchars{$char}{wd};
765 $allchars{$id}{ht} = $allchars{$acc}{ht};
766 $allchars{$id}{dp} = $allchars{$char}{dp};
767 $allchars{$id}{id} = $id;
768 push( @{ $allchars{$id}{num} }, $num);
769 $s1 = $allchars{$char}{map};
770 $s1 =~ s/\A (.*)\n\Z/(PUSH) $1 (POP)/s;
771 if ($scaps and $char =~ /^[a-z]/) { # accented small caps
772 $tallchar = 1;
773 if ($char =~ /$supacc$/
774 and $char !~ /under$supacc$/) { # double accs
775 $s2 = " (MOVEUP R $v1)";
776 $allchars{$id}{ht} += $v1;
777 $hadj = sprintf("%.3f", $v1 * $slant);
778 $ic = $allchars{$char}{ic};
779 }
780 else { # single accs
781 $s2 = " (MOVEUP R $scoffset)";
782 $allchars{$id}{ht} += $scoffset;
783 $hadj = sprintf("%.3f", $scoffset * $slant);
784 $ic = $allchars{$acc}{ic} + $hadj;
785 }
786 }
787 elsif ($allchars{$char}{ht} >= ($accheight + $v2)) { # double accs
788 $tallchar = 1; # on caps etc.
789 $s2 = " (MOVEUP R ${ \($v1 + $v2) })";
790 $allchars{$id}{ht} += ($v1 + $v2);
791 $hadj = sprintf("%.3f", ($v1 + $v2) * $slant);
792 $ic = $allchars{$char}{ic};
793 }
794 elsif ($allchars{$char}{ht} > 1.15 * $xheight) {
795 $tallchar = 1;
796 if ($char =~ /$supacc$/
797 and $char !~ /under$supacc$/) { # double accs
798 $s2 = " (MOVEUP R $v1)";
799 $allchars{$id}{ht} += $v1;
800 $hadj = sprintf("%.3f", $v1 * $slant);
801 $ic = $allchars{$char}{ic};
802 }
803 else { # caps etc.
804 $s2 = " (MOVEUP R $v2)";
805 $allchars{$id}{ht} += $v2;
806 $hadj = sprintf("%.3f", $v2 * $slant);
807 $ic = $allchars{$char}{ic};
808 }
809 }
810 else { # single accs
811 $s2 = " ";
812 $ic = $allchars{$acc}{ic};
813 }
814 $h = sprintf("%.3f", ($allchars{$char}{wd} - $allchars{$acc}{wd}) / 2);
815 unless ($tallchar) { $ic -= $h }
816 $allchars{$id}{ic} = $ic unless $ic < 0;
817 $h += $hadj;
818 if ($h > 0) { $s2 .= " (MOVERIGHT R $h) " }
819 elsif ($h < 0) {
820 $h = -$h;
821 $s2 .= " (MOVELEFT R $h) ";
822 }
823 else { $s2 .= " " }
824 $s3 = $allchars{$acc}{map};
825 $s3 =~ s/\A.*(\(SETCHAR . .+?\)).*\Z/$1/s;
826 if ($cb) { # candrabindu
827 $cb = $s2 . $cb;
828 if ($cbx) {
829 unless (($cb =~ s/(MOVERIGHT R )([0-9.]+)/$1 . ($2 + $cbx * $scale)/e)
830 or ($cb =~ s/(MOVELEFT R )([0-9.]+)/$1 . ($2 - $cbx * $scale)/e)) {
831 $cb =~ s/^( +)/"$1(MOVERIGHT R " . ($cbx * $scale) . ") "/e;
832 }
833 }
834 if ($cby) {
835 unless ($cb =~ s/(MOVEUP R )([0-9.]+)/$1 . ($2 + $cby * $scale)/e) {
836 $cb =~ s/^( +)/"$1(MOVEUP R " . ($cby * $scale) . ") "/e;
837 }
838 }
839 $cb .= "\n";
840 $s1 = "(PUSH) " . $s1;
841 $s3 .= " (POP)";
842 }
843 $allchars{$id}{map} = " $s1\n$s2$s3\n$cb";
844 $chars[$num] = $allchars{$id};
845 }
846
847 sub underacc {
848 #
849 # Dropped accents
850 #
851 my ($num, $char, $acc, $id) = @_;
852 my ($h, $v, $s1, $s2, $s3);
853 $acc =~ s/^under//;
854 if ($acc eq "dot") { $acc = "period" }
855 if ($acc eq "candrabindu") { die "Bad definition (no such accent): $qdef\n" }
856 $allchars{$id}{wd} = $allchars{$char}{wd};
857 $allchars{$id}{ht} = $allchars{$char}{ht};
858 $allchars{$id}{ic} = $allchars{$char}{ic};
859 $allchars{$id}{id} = $id;
860 push( @{ $allchars{$id}{num} }, $num);
861 if ($acc =~ /^$supacc$/) {
862 $v = $allchars{$id}{dp} = $underadp * $scale * $shrink;
863 $v += ($accdepth * $shrink);
864 }
865 else {
866 $v = $allchars{$id}{dp} = $underddp * $scale + $allchars{$acc}{dp};
867 }
868 $s1 = $allchars{$char}{map};
869 $s1 =~ s/\A (.*)\n\Z/(PUSH) $1 (POP)/s;
870 if ($acc =~ /^$supacc$/) {
871 $h = ($allchars{$char}{wd} - ($allchars{$acc}{wd} * $shrink)) / 2 - $v * $slant;
872 if ($opt_s) { $s2 = " (SELECTFONT D 1)\n" }
873 }
874 else {
875 $h = ($allchars{$char}{wd} - $allchars{$acc}{wd}) / 2 - $v * $slant;
876 }
877 $h = sprintf("%.3f", $h);
878 if ($h > 0) {
879 $s2 .= " (MOVEDOWN R $v) (MOVERIGHT R $h) ";
880 }
881 elsif ($h < 0) {
882 $h = -$h;
883 $s2 .= " (MOVEDOWN R $v) (MOVELEFT R $h) ";
884 }
885 else { $s2 = " (MOVEDOWN R $v) " }
886 $s3 = $allchars{$acc}{map};
887 $s3 =~ s/\A.*(\(SETCHAR . .+?\)).*\Z/$1/s;
888 if ($opt_s and $acc =~ /^$supacc$/) { $s3 .= "\n (SELECTFONT D 0)" }
889 $allchars{$id}{map} = " $s1\n$s2$s3\n";
890 $chars[$num] = $allchars{$id};
891 }
892
893 sub underb {
894 #
895 # Underbar
896 #
897 my ($num, $char, $id) = @_;
898 my ($h, $w, $dp, $s1, $s2, $s3);
899 $allchars{$id}{wd} = $allchars{$char}{wd};
900 $allchars{$id}{ht} = $allchars{$char}{ht};
901 $allchars{$id}{dp} = $dp = $underbdp * $scale;
902 $allchars{$id}{ic} = $allchars{$char}{ic};
903 $allchars{$id}{id} = $id;
904 push( @{ $allchars{$id}{num} }, $num);
905 $s1 = $allchars{$char}{map};
906 $s1 =~ s/\A (.*)\n\Z/(PUSH) $1 (POP)/s;
907 $h = sprintf("%.3f", ($allchars{$id}{wd} / 10 - $dp * $slant));
908 $w = sprintf("%.3f", ($allchars{$id}{wd} * 8 / 10));
909 if ($h > 0) {
910 $s2 = " (MOVEDOWN R $dp) (MOVERIGHT R $h) ";
911 }
912 elsif ($h < 0) {
913 $h = -$h;
914 $s2 = " (MOVEDOWN R $dp) (MOVELEFT R $h) ";
915 }
916 else { $s2 .= " (MOVEDOWN R $dp) " }
917 $s3 = "(SETRULE R ${ \($thk * $scale) } R $w)";
918 $allchars{$id}{map} = " $s1\n$s2$s3\n";
919 $chars[$num] = $allchars{$id};
920 }
921
922 sub digraph {
923 #
924 # Make a new character consisting of two existing characters
925 #
926 my ($num, $char, $acc, $id) = @_;
927 my ($one, $two, $kern, $s1, $s2);
928 if ($ligs =~ /^ \(LABEL $char\)\n(.*?)\n \(KRN $acc R (-?[0-9.]+)\)\n/ms) {
929 ($one, $two) = ($1, $2);
930 }
931 $kern = $two if $one !~ /^ \(STOP\)$/m;
932 $allchars{$id}{wd} = $allchars{$char}{wd} + $allchars{$acc}{wd};
933 $allchars{$id}{wd} += $kern;
934 $allchars{$id}{ht} = max($allchars{$char}{ht}, $allchars{$acc}{ht});
935 $allchars{$id}{dp} = max($allchars{$char}{dp}, $allchars{$acc}{dp});
936 $allchars{$id}{ic} = $allchars{$acc}{ic};
937 $allchars{$id}{id} = $id;
938 push( @{ $allchars{$id}{num} }, $num);
939 $s1 = $allchars{$char}{map};
940 chomp ($s2 = $allchars{$acc}{map});
941 $s1 =~ s/(\(SETCHAR .*?\))/$1\n$s2/;
942 if ($kern) {
943 if ($kern < 0) {
944 $kern = -$kern;
945 $s1 =~ s/(\(SETCHAR .*?\))/$1 (MOVELEFT R $kern)/;
946 }
947 else { $s1 =~ s/(\(SETCHAR .*?\))/$1 (MOVERIGHT R $kern)/ }
948 }
949 $allchars{$id}{map} = $s1;
950 $chars[$num] = $allchars{$id};
951 }
952
953 sub fixkerns {
954 #
955 # Generalise the kerning info contained in the vpl file by applying
956 # it to new accented chars. Do not kern lower-case chars bearing
957 # superscript accents with capitals, quotes or a preceding "f".
958 #
959 my ($char, $acc) = @_;
960 my ($olabel, $nlabel, @liglist, $lchar, $rchar);
961 if ($acc =~ /^$accents$/) { $lchar = $rchar = $char }
962 else {
963 $lchar = $char;
964 $rchar = $acc;
965 }
966 unless ($char =~ /^[a-z]/ and $acc =~ /^$supacc$/) {
967 $ligs =~ s[(\n \(LABEL )$rchar\)(?!\n \(LIG.*$)]
968 [$&$1$char$acc)]gm
969 unless $ligs =~ /\n \(LABEL $char$acc\)$/m;
970 $ligs =~ s[(\n \(LABEL )$rchar\)(\n \(LIG.*$)+(?!\n \(STOP\))]
971 [$&$1$char$acc)]gm
972 unless $ligs =~ /\n \(LABEL $char$acc\)$/m;
973 $ligs =~ s[(\n \(KRN )$lchar( .*)$]
974 [$&$1$char$acc$2]gm;
975 }
976 else {
977 if ($ligs =~ /\n \(LABEL $char\).*?\(STOP\)/s) {
978 $nlabel = $olabel = $&;
979 $nlabel =~ s/(\n \(LABEL $char)\)/$1$acc)/
980 unless $ligs =~ /\n \(LABEL $char$acc\)/m;
981 $nlabel =~ s/\n \(LIG .*\)$//gm;
982 $nlabel =~ s/\n \(LABEL (?!$char$acc).*\)$//gm;
983 $nlabel =~ s/\n \(KRN ([A-Z]|quote).*\)$//gm;
984 $ligs =~ s/(\n \(LABEL )$char\).*?\(STOP\)/$olabel$nlabel/s;
985 }
986 @liglist = split /\n \(STOP\)/, $ligs;
987 foreach (@liglist) {
988 unless (/\n \(LABEL ([A-Zf]|quote).*\)$/m) {
989 s/(\n \(KRN $char)( .*\))$/$&$1$acc$2/gm;
990 }
991 }
992 $ligs = join("\n (STOP)", @liglist);
993 }
994 }