Debian Codegraph @

texk/texlive/linked_scripts/accfonts/mkt1font

raw
1 #!/usr/bin/perl
2
3 # Mkt1font: a program to generate accented Type 1 PostScript fonts
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: mkt1font -n fontname -d definition-file -a afm-file -f font-file
24 [-s shrink-factor] [-c candrabindu-adjustment] [-b]
25
26 Mkt1font creates new Type 1 PostScript fonts based on existing fonts
27 (\"input fonts\"). In order to do this it makes use of I. Lee
28 Hetherington's programs t1asm and t1disasm, which must be present on
29 the system. A successful run will generate an AFM file and a PFB
30 file, as well as a DIS file containing a disassembled version of the
31 PFB file (including useful comments). Four options must be specified
32 on the command line, as follows:
33
34 -n should name the font it is intended to generate. Avoid names
35 such as \"myfont\", as both mkt1font and other programs attempt
36 to draw conclusions from the name; better would be something
37 like \"Utopia_French-BoldItalic\". The generated font files
38 will also use this for their basename.
39
40 -d should refer to a font definition file. This file (which could
41 usefully be named, e.g., \"French.def\") should consist of
42 lines of character definitions, in the form
43 \"number\" \"character\"
44 or
45 \"number\" \"character\" \"accent\"
46 Here \"number\" represents the character's position in the new
47 encoding and may be expressed in decimal, octal or hex;
48 \"character\" names the character (e.g. \"comma\", \"eight\",
49 \"A\") or consists of the word \".notdef\" (indicating that
50 the specified number's \"slot\" in the new encoding is to be
51 empty); and \"accent\" optionally names an accent to be placed
52 on the character. In addition to the standard accents available
53 in PostScript fonts, \"underbar\" and \"underdot\" are also
54 available, as are \"under\" versions of all the normal
55 superscript accents (\"underdieresis\", \"underring\", etc.).
56 The Indian accent \"candrabindu\" may also be specified: it
57 is formed by overprinting a breve with a dotaccent. Finally,
58 \"overdot\" may be used as a synonym for \"dotaccent\".
59
60 If the character named in the \"accent\" position is not in fact
61 a valid accent character, the program interprets the definition
62 as a request for a digraph formed from the \"character\" and the
63 \"accent\". A digraph consisting of, say, \"k\" and \"h\" will be
64 indistinguishable from the letters \"k\" and \"h\" printed
65 consecutively, but the digraph \"kh\" can itself receive accents
66 like any other character: see next paragraph.
67
68 A new character (such as \"amacron\" or \"kh\") may be freely
69 used in the \"character\" position of a further definition (such
70 as \"amacron breve\" or \"kh underbar\"). There is no constraint
71 on the ordering of definitions within a definition file. The
72 definition of \"a macron\" does not have to precede that of
73 \"amacron breve\": requests for \"impossible\" characters are
74 deferred until their constituents have had a chance to come into
75 being.
76
77 \"Slots\" for which no new definition is given retain the
78 definition they have in the input font.
79
80 The definition file may also contain blank lines and comments
81 (introduced by \"\#\").
82
83 -a should refer to the AFM (Adobe Font Metrics) file for the input
84 font.
85
86 -f should refer to the binary font file (PFB) for the input font.
87 (In fact the equivalent ASCII file (PFA) is also acceptable for
88 input, but the output font is always in PFB format.)
89
90 -s may optionally give the factor, expressed as a per-thousand
91 value, by which normally superscript accents (such as dieresis,
92 ring) should be shrunk when they are used as subscript accents
93 (such as underdieresis, underring). Values of around 800 may be
94 found useful.
95
96 -c may optionally give two comma-separated numerical values to
97 adjust the x and y coordinates of the dotaccent placed within a
98 breve to form the candrabindu accent.
99
100 -b may optionally be specified to block the use of predefined
101 accented characters, forcing mkt1font to define its own
102 versions. This may be useful to secure a consistent appearance
103 in cases where a font designer does not share mkt1font's views
104 on where accents should be placed.
105
106 The -h option prints this help.
107 ";
108 #-----------------------------------------------------------------------------#
109
110 ########################
111 # Packages and constants
112 ########################
113 #
114 use File::Basename;
115 use Getopt::Std;
116 $cmdline = basename($0) . " " . join " ", @ARGV;
117 getopts('n:d:a:f:c:s:bh');
118 if ($opt_h or !($opt_n && $opt_d && $opt_a && $opt_f) or ($#ARGV >= 0)) {
119 print STDERR $description;
120 exit 1;
121 }
122 ($enc = basename($opt_d)) =~ s/\..*$//;
123 if ($opt_n =~ /\s/) { die "Font name cannot contain whitespace\n" }
124 ($famname = $opt_n) =~ s/^(.*)-.*?$/$1/;
125 if ($opt_s) { $shrink = $opt_s / 1000 } else { $shrink = 1 }
126 @months = ( "January", "February", "March", "April",
127 "May", "June", "July", "August",
128 "September", "October", "November", "December"
129 );
130 @lt = localtime;
131 $date = "$months[$lt[4]] $lt[3], ${ \($lt[5] + 1900) }";
132 if ($opt_c) { ($cbx, $cby) = $opt_c =~ /^(.*),(.*)$/ } # candrabindu
133
134 ###############
135 # Read DEF file
136 ###############
137 #
138 open DEF, $opt_d or die "Cannot open $opt_d: $!\n";
139 while (<DEF>) {
140 next if (/^\s*$/ || /^\#/);
141 s/\s*(\#.*)?$//;
142 push @deflines, $_;
143 }
144 close DEF;
145
146 ###############
147 # Read AFM file
148 ###############
149 #
150 # File header
151 #
152 open AFM, $opt_a or die "Cannot open $opt_a: $!\n";
153 while (<AFM>) {
154 if (/^StartFontMetrics/) {
155 $afmhead = $_;
156 do {
157 $_ = <AFM>;
158 $afmhead .= $_;
159 }
160 until $_ =~ /^StartCharMetrics \d+\r?$/;
161 $afmhead =~ s/\r//gm; # Might be DOS file
162 $afmhead =~ /^FontName (.*)$/m;
163 $ofname = $1;
164 $afmbanner = "\nComment Based on $ofname; modified for $enc encoding";
165 $afmbanner .= "\nComment by John Smiths's program mkt1font ";
166 $afmbanner .= "(v. $version) " if $version;
167 $afmbanner .= "on $date";
168 $afmbanner .= "\nComment Command line: $cmdline";
169 $afmhead =~ s/(\n^FontName )(.*)$/$1$opt_n/m;
170 $afmhead =~ s/(\n^FullName )(.*)$/$1$opt_n/m;
171 $afmhead =~ s/(\n^FamilyName )(.*)$/$1$famname/m;
172 $afmhead =~ s/\n^Comment /$afmbanner$&/m;
173 $afmhead =~ s/(\n^EncodingScheme ).*$/$1FontSpecific/m;
174 if ($afmhead =~ /\n^ItalicAngle (.*)$/m) { $italangle = $1 }
175 if ($afmhead =~ /\n^CapHeight (.*)$/m) { $capheight = $1 }
176 if ($afmhead =~ /\n^XHeight (.*)$/m) { $xheight = $1 }
177 if ($afmhead =~ /\n^FontBBox -?\d+ (-?\d+) (-?\d+) (-?\d+)$/m) {
178 ($maxdp, $maxrs, $maxht) = ($1, $2, $3);
179 }
180 }
181 #
182 # Encoded chars
183 #
184 if (/(^C (\d+) ; WX \d+ ; N ([a-zA-Z_][a-zA-Z0-9_.]*|\.notdef) ; B .* ;( L .* ;)?)\r?$/) {
185 $metrics_enc[$2] = "$1\n";
186 }
187 #
188 # Unencoded chars
189 #
190 elsif (/(^C -1 ; WX \d+ ; N ([a-zA-Z_][a-zA-Z0-9_.]*|\.notdef) ; B .* ;( L .* ;)?)\r?$/) {
191 $metrics_unenc .= "$1\n";
192 }
193 #
194 # Kerns etc.
195 #
196 if (/^EndCharMetrics\r?$/) {
197 $afmtail = $_;
198 do {
199 $_ = <AFM>;
200 $afmtail .= $_;
201 }
202 until eof(AFM);
203 $afmtail =~ s/[\r\cZ]//gm; # Might be DOS file
204 }
205 }
206 if ($afmtail =~ /(\A(?:.|\n)*^StartKernPairs )\d+\n((?:.|\n)*)(^EndKernPairs(?:.|\n)*\Z)/m) {
207 ($kstart, $kdefs, $kend) = ($1, $2, $3);
208 }
209 close AFM;
210
211 ###############################
212 # Read and disassemble PFB file
213 ###############################
214 #
215 open PFB, $opt_f or die "Cannot open $opt_f: $!\n";
216 $_ = <PFB>;
217 unless (/%!/) { die "File $opt_f is not a PostScript font\n" }
218 close PFB;
219 $disfile1 = "mkt1font$$.dis";
220 system "t1disasm $opt_f >$disfile1 2>/dev/null";
221 open DIS, $disfile1 or die "Cannot open disassembled font file: $!\n";
222 while (<DIS>) {
223 #
224 # Locate crucial definitions
225 #
226 if (/^\/(.*?) ?{ ?noaccess def ?} ?executeonly def$/) {
227 $nd = $1;
228 ($rend = $nd) =~ s/\|/\\|/g; # Regexp version
229 next;
230 }
231 if (/^\/(.*?) ?{ ?noaccess put ?} ?executeonly def$/) {
232 $np = $1;
233 ($renp = $np) =~ s/\|/\\|/g; # Regexp version
234 next;
235 }
236 if (/^\/Subrs (\d+) array/) {
237 $subrsnum = $1;
238 do {
239 #
240 # Store subroutine definitions in @subrs
241 #
242 $l = $_ = <DIS>;
243 if (/^dup (\d+) {$/) {
244 $subrnum = $1;
245 $subrdef = $_;
246 do {
247 $_ = <DIS>;
248 $subrdef .= $_;
249 }
250 until /^\t} ?$renp$/;
251 }
252 #
253 # "correct" t1disasm output so t1asm will accept it as input
254 #
255 $subrdef =~ s/^(\t})($rend|$renp)$/$1 $2/m;
256 $subrs[$subrnum] = $subrdef;
257 }
258 until ($l !~ /^dup \d+ {$/);
259 next;
260 }
261 if (/^2 index \/CharStrings (\d+) dict dup begin/) {
262 #
263 # do *not* use "$" to anchor the end of this line, because
264 # it may end with an invisible trailing space!
265 #
266 $chdefsnum = $1;
267 do {
268 #
269 # Store character definitions in %chstrings
270 #
271 $l = $_ = <DIS>;
272 if (/^\/([a-zA-Z_][a-zA-Z0-9_.]*|\.notdef|\.null) {$/) {
273 $chname = $1;
274 $chdef = $_;
275 do {
276 $_ = <DIS>;
277 $chdef .= $_;
278 }
279 until /^\t} ?$rend$/;
280 }
281 #
282 #"correct" t1disasm output so t1asm will accept it as input
283 #
284 $chdef =~ s/^(\t})($rend|$renp)$/$1 $2/m;
285 $chstrings{$chname} = $chdef;
286 }
287 until ($l !~ /^\/([a-zA-Z_][a-zA-Z0-9_.]*|\.notdef|\.null) {$/);
288 #
289 # File tail
290 #
291 if (/^end/) {
292 $distail = $_;
293 do {
294 $_ = <DIS>;
295 $distail .= $_;
296 }
297 until eof(DIS);
298 }
299 }
300 }
301 close DIS;
302 unless ($nd) { die "Cannot find \"noaccess def\" definition\n" }
303 unless ($np) { die "Cannot find \"noaccess put\" definition\n" }
304 unless ($subrsnum) { die "Cannot find size of Subrs array\n" }
305 unless ($chdefsnum) { die "Cannot find size of CharStrings dict\n" }
306
307 ##################
308 # Set up constants
309 ##################
310 #
311 $subacc = "(cedilla|ogonek|commaaccent)";
312 $supacc = "(grave|acute|circumflex|tilde|macron|breve|dotaccent|overdot|dieresis|ring|hungarumlaut|caron|candrabindu)";
313 $underacc = "(underdot|under$supacc)";
314 $accents = "($subacc|$supacc|$underacc|underbar)";
315 $underadp = -230; # depth of "under" accs
316 $underddp = -213; # depth of underdot
317 if ($opt_n =~ /Bold/) { $thk = 72 } else { $thk = 52 } # thickness and
318 $underbdp = -(82 + $thk); # depth of underbar
319 getcharinfo("macron");
320 $accheight = $chht{macron}; # height of macron
321 $accdepth = $chdp{macron}; # "depth" of macron
322 $v1 = $accheight - $xheight; # vertical offset for double accents
323 $v2 = $capheight - $xheight; # vertical offset for accented caps etc
324 #
325 # Italic adjustment: tan(angle)
326 #
327 $itadj = sin($italangle * 3.14159 / 180) / cos($italangle * 3.14159 / 180);
328
329 ###########################
330 # Create uni0237 (dotlessj)
331 ###########################
332 #
333 $chstrings{uni0237} = $chstrings{j} . "\n";
334 $chstrings{uni0237} =~ /\A(.*?)(\t[-0-9 ]+?[rvh]moveto)(\n.+)(\n\t.*?(-?\d+) [rv]moveto)(.*?)(\n\tendchar.*?)\Z/ms;
335 ($one, $two, $three, $four, $five, $six, $seven) = ($1, $2, $3, $4, $5, $6, $7);
336 if ($five > 0) { $chstrings{uni0237} = $one . $two . $three . $seven }
337 else {
338 findend("j1", ($two . $three));
339 $j1end = "\t$hend{j1} $vend{j1} rmoveto";
340 $chstrings{uni0237} = $one . $j1end . $four . $six . $seven
341 }
342 $chstrings{uni0237} =~s/\A\/j /\/uni0237 /m;
343 getcharinfo("j");
344 getcharinfo("dotlessi");
345 $metrics_unenc .= "C -1 ; WX $chwd{j} ; N uni0237 ; B $chls{j} $chdp{j} $chrs{j} $chht{dotlessi} ;\n";
346 #
347 ######################
348 # Build the characters
349 ######################
350 #
351 # First build a list of definitions supplied by user
352 #
353 for (@deflines) {
354 if (/^\s*(\d+|0[0-7]+|0x[0-9a-fA-F]+)\s+([a-zA-Z_][a-zA-Z0-9_.]*?|\.notdef)(?:\s+([a-zA-Z_][a-zA-Z0-9_.]*))?$/) {
355 ($num, $char, $acc) = ($1, $2, $3);
356 $num = oct $num if $num =~ /^0/;
357 if ($num > 255) { die "Bad definition (number out of range): $_\n" }
358 $def = {};
359 $def->{qdef} = $_;
360 $def->{num} = $num;
361 $def->{char} = $char;
362 $def->{acc} = $acc;
363 $def->{nchar} = $char . $acc;
364 push @nchars, $def->{nchar};
365 push @defs, $def;
366 }
367 else { die "Bad definition: $_\n" }
368 }
369 #
370 # Work through the list
371 #
372 while (@defs) {
373 $def = shift @defs;
374 $qdef = $def->{qdef};
375 $num = $def->{num};
376 $char = $def->{char};
377 $acc = $def->{acc};
378 $nchar = $def->{nchar};
379 #
380 # If we can't handle $char/$acc yet, but believe we will be able
381 # to later, send the definition to the back of the queue. In case
382 # it later turns out we were wrong, allow only five loops before
383 # giving up.
384 #
385 if (!grep / ; N $char ; /, @metrics_enc
386 and $metrics_unenc !~ / ; N $char ; /m) {
387 if (grep /^$char$/, @nchars) {
388 unless (++$def->{requeue} > 5) {
389 push @defs, $def;
390 next;
391 }
392 }
393 else { die "Bad definition (no such character): $qdef\n" }
394 }
395 if ($acc
396 and !grep / ; N $acc ; /, @metrics_enc
397 and $metrics_unenc !~ / ; N $acc ; /m
398 and $acc !~ /^$accents$/) {
399 if (grep /^$acc$/, @nchars) {
400 unless (++$def->{requeue} > 5) {
401 push @defs, $def;
402 next;
403 }
404 }
405 else { die "Bad definition (no such accent): $qdef\n" }
406 }
407 #
408 # First deal with .notdef
409 #
410 if ($nchar eq ".notdef") {
411 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
412 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
413 undef $metrics_enc[$num];
414 }
415 #
416 # Now look for new char among existing encoded chars (unless blocked by -b)
417 #
418 else {
419 if (!($acc and $opt_b)
420 and @foundit = grep / ; N $nchar ; /, @metrics_enc) {
421 ($foundit = shift @foundit) =~ s/^C (\d+)/C $num/;
422 $prevnum = $1;
423 unless ($prevnum == $num) {
424 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
425 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
426 $metrics_enc[$num] = $foundit;
427 }
428 fixkerns($char, $acc);
429 }
430 #
431 # Next look among existing unencoded chars (unless blocked by -b)
432 #
433 elsif (!($acc and $opt_b)
434 and $metrics_unenc =~ s/^C -1 ; WX \d+ ; N $nchar ; B .* ;( L .* ;)?\n//m) {
435 ($foundit = $&) =~ s/^C -1/C $num$1/m;
436 $old = $metrics_enc[$num];
437 if ($old) {
438 $old =~ s/^C $num/C -1/;
439 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
440 }
441 $metrics_enc[$num] = $foundit;
442 fixkerns($char, $acc);
443 }
444 #
445 # If it can't be built from sub-elements, issue a warning and move on
446 #
447 elsif (!$acc) { warn "No such character - ignoring definition: $qdef\n" }
448 #
449 # Now build the char
450 #
451 else {
452 #
453 # First get rid of predefined afm entries and synonyms
454 #
455 $metrics_unenc =~ s/^C -1 ; WX \d+ ; N $nchar ; B .* ;( L .* ;)?\n//m;
456 $kdefs =~ s/\nKPX .*$nchar .*$//gm;
457 if ($acc eq "overdot") {
458 $nchar2 = $char . "dotaccent";
459 $metrics_unenc =~ s/^C -1 ; WX \d+ ; N $nchar2 ; B .* ;( L .* ;)?\n//m;
460 $kdefs =~ s/\nKPX .*$nchar2 .*$//gm;
461 delete $chstrings{$nchar2};
462 }
463 #
464 # Go!
465 #
466 if ($acc =~ /^$subacc$/) {
467 subacc($num, $char, $acc, $nchar);
468 fixkerns($char, $acc);
469 }
470 elsif ($acc =~ /^$supacc$/) {
471 supacc($num, $char, $acc, $nchar);
472 fixkerns($char, $acc);
473 }
474 elsif ($acc =~ /^$underacc$/) {
475 underacc($num, $char, $acc, $nchar);
476 fixkerns($char, $acc);
477 }
478 elsif ($acc =~ /^underbar$/) {
479 underb($num, $char, $nchar);
480 fixkerns($char, $acc);
481 }
482 else {
483 digraph($num, $char, $acc, $nchar);
484 fixkerns($char, $acc);
485 }
486 }
487 }
488 }
489
490 ################
491 # Write AFM file
492 ################
493 #
494 open OUTAFM, ">$opt_n.afm";
495 foreach (@metrics_enc) { # Avoid duplicate entries
496 ($chname) = /( ; N [a-zA-Z_][a-zA-Z0-9_.]* ; )/;
497 $metrics_unenc =~ s/^C -1 .*$chname.*\n//gm if $chname;
498 }
499 $chmetricnum = split(/\n/, $metrics_unenc);
500 $chmetricnum += grep /^C /, @metrics_enc;
501 $afmhead =~ s/^(FontBBox -?\d+ )-?\d+ -?\d+ -?\d+$/$1$maxdp $maxrs $maxht/m;
502 $afmhead =~ s/^(StartCharMetrics )\d+$/$1$chmetricnum/m;
503 $encoding = "/Encoding 256 array\n0 1 255 {1 index exch /.notdef put} for\n";
504 print OUTAFM $afmhead;
505 for $i (0 .. 255) {
506 $line = $metrics_enc[$i];
507 if ($line) {
508 print OUTAFM $line;
509 if ($line =~ /^C \d+ ; WX \d+ ; N ([a-zA-Z_][a-zA-Z0-9_.]*) ; /) {
510 $encoding .= "dup $i /$1 put\n";
511 }
512 }
513 }
514 $encoding .= "readonly def\n";
515 print OUTAFM $metrics_unenc;
516 #
517 # Tidy up kerns
518 #
519 if ($kdefs) {
520 $kdefs = join("\n", sort split /\n/, $kdefs); # sort
521 $kdefs .= "\n";
522 $kdefs =~ s/((^KPX .*?)-?\d+$)\n\2.*$/$1/gm; # remove duplicates
523 $kdefs =~ s/^\s*\n//gm; # and blank lines
524 $knum = split(/\n/, $kdefs);
525 $afmtail = $kstart . $knum . "\n" . $kdefs . $kend;
526 }
527 #
528 # Eliminate composite character definitions
529 #
530 $afmtail =~ s/\n(Start|End)Composites.*$//gm;
531 $afmtail =~ s/\nCC .*;$//gm;
532 print OUTAFM $afmtail;
533 close OUTAFM;
534
535 #########################
536 # Write DIS and PFB files
537 #########################
538 #
539 $disfile2 = "$opt_n.dis";
540 open OUTDIS, ">$disfile2";
541 $disbanner = "% Based on $ofname; modified for $enc encoding\n";
542 $disbanner .= "% by John Smiths's program mkt1font ";
543 $disbanner .= "(v. $version) " if $version;
544 $disbanner .= "on $date\n";
545 $disbanner .= "% Command line: $cmdline";
546 open DIS, $disfile1;
547 while (<DIS>) { # get header
548 s/^(%!.*: ).*$/$1$opt_n\n$disbanner/;
549 s/(\/FullName \().*(\).*)$/$1$opt_n$2/;
550 s/(\/FamilyName \().*(\).*)$/$1$famname$2/;
551 s/(\/FontName \/).*?( .*)$/$1$opt_n$2/;
552 s/(\/FontBBox {-?\d+ )-?\d+ -?\d+ -?\d+/$1$maxdp $maxrs $maxht/m;
553 if (/^\/Encoding /) {
554 print OUTDIS $encoding;
555 next;
556 }
557 next if (/\/UniqueID .*$/);
558 s /(\/BlueValues \[.*)(\] def)$/$1 $accheight $accheight$2/;
559 if (/(\/Subrs )\d+( array)$/) {
560 print OUTDIS "$1$subrsnum$2\n";
561 last;
562 }
563 else { print OUTDIS }
564 }
565 close DIS;
566 unlink $disfile1;
567 foreach $subrdef (@subrs) { print OUTDIS $subrdef }
568 $chdefsnum = scalar(keys(%chstrings));
569 print OUTDIS "$nd\n2 index /CharStrings $chdefsnum dict dup begin\n";
570 foreach (sort values %chstrings) { print OUTDIS }
571 print OUTDIS $distail;
572 close OUTDIS;
573 system "t1asm -b $disfile2 >$opt_n.pfb 2>/dev/null";
574
575 #####################
576 # End of main program
577 #####################
578
579 sub subacc {
580 #
581 # Subscript accents
582 #
583 my ($num, $char, $acc, $nchar) = @_;
584 my ($old, $astr, $h, $H, $V, $chdef, $acdef, $pstr);
585 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
586 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
587 getcharinfo($char) unless defined $chwd{$char};
588 getcharinfo($acc) unless defined $chwd{$acc};
589 $astr = "C $num ; WX $chwd{$char} ; N $nchar ; B $chls{$char} ";
590 $astr .= "$chdp{$acc} $chrs{$char} $chht{$char} ;\n";
591 $metrics_enc[$num] = $astr;
592 $h = round(($chwd{$char} - $chwd{$acc}) / 2 + $chls{$acc});
593 unless (defined $hend{$char}) { findend($char, $chstrings{$char}) }
594 $H = $h - $hend{$char} - $chls{$char};
595 $V = -$vend{$char};
596 $chstrings{$char} =~ /^\/$char {\n(\t.+?hsbw\n(.|\n)*?)\tendchar\n\t} $rend\n/m;
597 $chdef = $1;
598 $chstrings{$acc} =~ /^\/$acc {\n\t.+? .+?hsbw\n((.|\n)+?\t} $rend\n)/m;
599 $acdef = $1;
600 $acdef = fixhints($nchar, $acc, $acdef, $h, 0);
601 $pstr = "/$nchar {\n$chdef % $nchar: start of $acc\n";
602 $pstr .= "\t$H $V rmoveto\n$acdef";
603 $chstrings{$nchar} = $pstr;
604 }
605
606 sub supacc {
607 #
608 # Superscript accents
609 #
610 my ($num, $char, $acc, $nchar) = @_;
611 my ($old, $cb, $astr, $h, $v, $H, $V, $it, $chdef, $acdef, $pstr, $newchar);
612 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
613 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
614 if ($char eq "i") { $char = "dotlessi" }
615 if ($char eq "j") { $char = "uni0237" }
616 if ($acc eq "overdot") { $acc = "dotaccent" }
617 if ($acc eq "candrabindu") {
618 $acc = "breve";
619 $chstrings{"dotaccent"} =~ /^\/dotaccent {\n\t.+? .+?hsbw\n((.|\n)+?\t} $rend\n)/m;
620 $cb = $1;
621 }
622 getcharinfo($char) unless defined $chwd{$char};
623 getcharinfo($acc) unless defined $chwd{$acc};
624 $astr = "C $num ; WX $chwd{$char} ; N $nchar ; B ";
625 $astr .= "$chls{$char} $chdp{$char} $chrs{$char} ";
626 $v = $chht{$acc};
627 if ($chht{$char} >= $accheight + $v2) { # double accs
628 $v += ($v1 + $v2); # on caps etc.
629 $it = ($v1 + $v2) * $itadj;
630 }
631 elsif ($chht{$char} > 1.15 * $xheight) {
632 if ($char =~ /$supacc$/ and $char !~ /under$supacc$/) {
633 $v += $v1; # double accents
634 $it = $v1 * $itadj;
635 }
636 else {
637 $v += $v2; # accented caps etc.
638 $it = $v2 * $itadj;
639 }
640 }
641 else { $it = 0 }
642 if ($v > $maxht) { $maxht = $v }
643 $astr .= "$v ;\n";
644 $metrics_enc[$num] = $astr;
645 $v -= $chht{$acc};
646 $h = round(($chwd{$char} - $chwd{$acc}) / 2 + $chls{$acc} - $it);
647 unless (defined $hend{$char}) { findend($char, $chstrings{$char}) }
648 $H = $h - $hend{$char} - $chls{$char};
649 $V = $v - $vend{$char};
650 $chstrings{$char} =~ /^\/$char {\n(\t.+?hsbw\n(.|\n)*?)\tendchar\n\t} $rend\n/m;
651 $chdef = $1;
652 $chstrings{$acc} =~ /^\/$acc {\n\t.+? .+?hsbw\n((.|\n)+?\t} $rend\n)/m;
653 $acdef = $1;
654 $acdef = fixhints($nchar, $acc, $acdef, $h, $v);
655 $pstr = "/$nchar {\n$chdef % $nchar: start of $acc\n";
656 $pstr .= "\t$H $V rmoveto\n$acdef";
657 if ($cb) { # candrabindu
658 getcharinfo("dotaccent") unless defined $chwd{dotaccent};
659 $h = round(($chwd{$char} - $chwd{dotaccent}) / 2 + $chls{dotaccent} - $it);
660 $h += $cbx;
661 $v += $cby;
662 $cb = fixhints($nchar, "dotaccent", $cb, $h, $v);
663 $newchar = $char . $acc;
664 unless (defined $hend{$newchar}) { findend($newchar, $pstr) }
665 $H = $h - $hend{$newchar} - $chls{$char};
666 $V = $v - $vend{$newchar};
667 $pstr =~ s/\tendchar\n\t} $rend\n\Z//m;
668 $pstr .= " % $nchar: start of dotaccent\n\t$H $V rmoveto\n$cb";
669 }
670 $chstrings{$nchar} = $pstr;
671 }
672
673 sub underacc {
674 #
675 # Dropped accents
676 #
677 # The Fontographer hack below is necessary because Fontographer (which
678 # I have had to use to build TrueType and Macintosh versions of fonts)
679 # seems not to respect the depths for characters specified in afm files
680 # it has imported: it sets the depth to the lowest point in the path,
681 # whether or not a mark was made there. So it is not possible to
682 # implement (e.g.) underring by diving down low and then printing a
683 # ring at its normal height.
684 #
685 my ($num, $char, $acc, $nchar) = @_;
686 my ($old, $acname, $astr, $h, $v, $H, $V, $chdef, $acdef, $begin, $line, $end, $pstr);
687 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
688 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
689 $acc =~ s/^under//;
690 $acname = $acc;
691 if ($acc eq "dot") { $acc = "period" }
692 if ($acc eq "candrabindu") { die "Bad definition (no such accent): $qdef\n" }
693 getcharinfo($char) unless defined $chwd{$char};
694 getcharinfo($acc) unless defined $chwd{$acc};
695 if ($acc =~ /^$supacc$/) {
696 $v = round($underadp + ($chdp{$acc} - $accdepth) * $shrink);
697 }
698 else { $v = $underddp + $chdp{$acc} }
699 if ($v < $maxdp) { $maxdp = $v }
700 $astr = "C $num ; WX $chwd{$char} ; N $nchar ; B $chls{$char} ";
701 $astr .= "$v $chrs{$char} $chht{$char} ;\n";
702 $metrics_enc[$num] = $astr;
703 if ($acc =~ /^$supacc$/) {
704 $h = round(($chwd{$char} - ($chwd{$acc} * $shrink)) / 2
705 + $chls{$acc} * $shrink - ($v - $accdepth * $shrink) * $itadj);
706 }
707 else {
708 $h = round(($chwd{$char} - $chwd{$acc}) / 2 + $chls{$acc} - $v * $itadj);
709 }
710 unless (defined $hend{$char}) { findend($char, $chstrings{$char}) }
711 $H = $h - $hend{$char} - $chls{$char};
712 if ($acc =~ /^$supacc$/) { $V = $underadp - $vend{$char} }
713 else { $V = $v - $chdp{$acc} - $vend{$char} }
714 #
715 # Adjustment for smaller, therefore lower, shrunken accent defs
716 #
717 if ($acc =~ /^$supacc$/) {
718 $V += round($chdp{$acc} * (1 - $shrink) / $shrink);
719 }
720 $chstrings{$char} =~ /^\/$char {\n(\t.+?hsbw\n(.|\n)*?)\tendchar\n\t} $rend\n/m;
721 $chdef = $1;
722 $chstrings{$acc} =~ /^\/$acc {\n\t.+? .+?hsbw\n((.|\n)+?\t} $rend\n)/m;
723 $acdef = $1;
724 if ($acc =~ /^$supacc$/ and $opt_s) { $acdef = fixshrink($acc, $acdef) }
725 if ($acc =~ /^$supacc$/) {
726 $acdef = fixhints($nchar, $acc, $acdef, $h, $underadp);
727 }
728 else { $acdef = fixhints($nchar, $acc, $acdef, $h, $underddp) }
729 #
730 # Bloody Fontographer! We can't just say
731 # if ($acc =~ /^$supacc$/) { $V -= $accdepth }
732 # Instead we have to go through the following rigmarole:
733 #
734 if ($acc =~ /^$supacc$/) {
735 if ($acdef =~ /^(.*moveto)$/m) {
736 ($begin, $line, $end) = ($`, $1, $');
737 if ($line =~ /^\t(-?\d+) (-?\d+)( rmoveto)$/) {
738 $acdef = "$begin\t$1 " . ($2 - $accdepth) . $3 . $end;
739 }
740 elsif ($line =~ /^\t(-?\d+)( vmoveto)$/) {
741 $acdef = "$begin\t" . ($1 - $accdepth) . $2 . $end;
742 }
743 elsif ($line =~ /\t(-?\d+) hmoveto$/) {
744 $acdef = "$begin\t$1 " . (-$accdepth) . "rmoveto" . $end;
745 }
746 }
747 else { $acdef =~ s/\A/\t-$accdepth vmoveto\n/m }
748 }
749 $pstr = "/$nchar {\n$chdef % $nchar: start of $acname\n";
750 $pstr .= "\t$H $V rmoveto\n$acdef";
751 $chstrings{$nchar} = $pstr;
752 }
753
754 sub underb {
755 #
756 # Underbar
757 #
758 my ($num, $char, $nchar) = @_;
759 my ($old, $astr, $l, $h, $v, $H, $V, $bdef, $chdef, $acdef, $pstr);
760 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
761 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
762 $v = $underbdp + $thk;
763 getcharinfo($char) unless defined $chwd{$char};
764 $h = round(0.1 * $chwd{$char} - $underbdp * $itadj);
765 $l = round(0.8 * $chwd{$char});
766 $astr = "C $num ; WX $chwd{$char} ; N $nchar ; B $chls{$char} ";
767 $astr .= "$underbdp $chrs{$char} $chht{$char} ;\n";
768 $metrics_enc[$num] = $astr;
769 unless (defined $hend{$char}) { findend($char, $chstrings{$char}) }
770 $H = $h - $hend{$char} - $chls{$char};
771 $V = -$vend{$char};
772 $bdef = "\t$underbdp $thk hstem\n\t$v vmoveto\n\t-$thk vlineto\n\t";
773 $bdef .= "$l hlineto\n\t$thk vlineto\n\tclosepath\n";
774 $chstrings{$char} =~ /^\/$char {\n(\t.+?hsbw\n(.|\n)*?)\tendchar\n\t} $rend\n/m;
775 $chdef = $1;
776 $acdef = fixhints($nchar, "underbar", $bdef, $h, 0);
777 $pstr = "/$nchar {\n$chdef % $nchar: start of $acc\n";
778 $pstr .= "\t$H $V rmoveto\n$acdef\tendchar\n\t} $nd\n";
779 $chstrings{$nchar} = $pstr;
780 }
781
782 sub digraph {
783 #
784 # Make a new character consisting of two existing characters
785 #
786 my ($num, $char, $acc, $nchar) = @_;
787 my ($old, $kern, $w, $newrs, $astr, $H, $V, $chdef, $acdef, $pstr);
788 ($old = $metrics_enc[$num]) =~ s/^C $num/C -1/;
789 $metrics_unenc .= $old unless $metrics_unenc =~ /$old/m;
790 getcharinfo($char) unless defined $chwd{$char};
791 getcharinfo($acc) unless defined $chwd{$acc};
792 if ($kdefs =~ /^KPX $char $acc (-?\d+)$/m) { $kern = $1 };
793 $w = $chwd{$char} + $chwd{$acc} + $kern;
794 $newrs = $chrs{$char} + $chwd{$acc} + $kern;
795 if ($newrs > $maxrs) { $maxrs = $newrs }
796 $astr = "C $num ; WX " . $w . " ; N $nchar ; B $chls{$char} ";
797 $astr .= (min($chdp{$char}, $chdp{$acc})) . " $newrs ";
798 $astr .= (max($chht{$char}, $chht{$acc})) . " ;\n";
799 $metrics_enc[$num] = $astr;
800 unless (defined $hend{$char}) { findend($char, $chstrings{$char}) }
801 $H = $chwd{$char} + $chls{$acc} + $kern - $hend{$char};
802 $V = -$vend{$char};
803 $chstrings{$char} =~ /^\/$char {\n(\t.+?hsbw\n(.|\n)*?)\tendchar\n\t} $rend\n/m;
804 ($chdef = $1) =~ s/-?\d+( hsbw\n)/$w$1/m;
805 $chstrings{$acc} =~ /^\/$acc {\n\t.+? .+?hsbw\n((.|\n)+?\t} $rend\n)/m;
806 $acdef = $1;
807 $acdef = fixhints($nchar, $acc, $acdef, ($chwd{$char} + $kern), 0);
808 $pstr = "/$nchar {\n$chdef % $nchar: start of $acc\n";
809 $pstr .= "\t$H $V rmoveto\n$acdef";
810 $chstrings{$nchar} = $pstr;
811 }
812
813 sub max {
814 #
815 # Return greater of two values
816 #
817 my ($a, $b) = @_;
818 return $a > $b ? $a : $b;
819 }
820
821 sub min {
822 #
823 # Return lesser of two values
824 #
825 my ($a, $b) = @_;
826 return $a > $b ? $b : $a;
827 }
828
829 sub round {
830 #
831 # Return nearest integer value
832 #
833 my ($num) = shift;
834 my $n1, $n2;
835 if ($num < 0) { $n1 = (0 - $num) }
836 else { $n1 = $num }
837 $n2 = int($n1);
838 if (($n1 - $n2) >= 0.5) { $n2++ }
839 if ($num < 0) { return (0 - $n2) }
840 else { return $n2 }
841 }
842
843 sub getcharinfo {
844 #
845 # Store character metric information
846 #
847 my $char = shift;
848 my $foundit;
849 my $metrics_all = join("", @metrics_enc) . $metrics_unenc;
850 if ($metrics_all =~ /^.* ; N $char ; .*$/m) { $foundit = $& }
851 else { die "Bad definition (no such character/accent): $char\n" }
852 $foundit =~ /^C (-?\d+) ; WX (\d+) ; N $char ; B (.+?) (.+?) (.+?) (.+?) ;/;
853 ($chnum{$char}, $chwd{$char}, $chls{$char},
854 $chdp{$char}, $chrs{$char}, $chht{$char}) = ($1, $2, $3, $4, $5, $6);
855 }
856
857 sub fixkerns {
858 #
859 # Generalise the kerning info contained in the afm file by applying
860 # it to new accented chars. Do not kern lower-case chars bearing
861 # superscript accents with capitals, quotes or a preceding "f".
862 #
863 my ($char, $acc) = @_;
864 my ($lchar, $rchar);
865 if ($acc =~ /^$accents$/) { $lchar = $rchar = $char }
866 else {
867 $lchar = $char;
868 $rchar = $acc;
869 }
870 if ($char =~ /^[a-z]/ and $acc =~ /^$supacc$/) {
871 if ($kdefs =~ /^(KPX $char)(( (?:[a-pr-z][a-zA-Z]*|q(?!uote).*?) )-?\d+)$/m) {
872 ($one, $three) = ($1, $3);
873 $kdefs =~ s[^(KPX $char)(( (?:[a-pr-z][a-zA-Z]*|q(?!uote).*?) )-?\d+)$]
874 [$&\n$1$acc$2]gm
875 unless $kdefs =~ /^$one$acc$three/m;
876 }
877 if ($kdefs =~ /^(KPX (?:[a-eg-pr-z][a-zA-Z]*|q(?!uote).*?) $char)( -?\d+)$/m) {
878 $one = $1;
879 $kdefs =~ s[^(KPX (?:[a-eg-pr-z][a-zA-Z]*|q(?!uote).*?) $char)( -?\d+)$]
880 [$&\n$1$acc$2]gm
881 unless $kdefs =~ /^$one$acc /m;
882 }
883 }
884 else {
885 if ($kdefs =~ /^(KPX )$rchar(( [a-zA-Z]+ )-?\d+)$/m) {
886 ($one, $three) = ($1, $3);
887 $kdefs =~ s[^(KPX )$rchar(( [a-zA-Z]+ )-?\d+)$]
888 [$&\n$1$char$acc$2]gm
889 unless $kdefs =~ /^$one$char$acc$three/m;
890 }
891 if ($kdefs =~ /^(KPX [a-zA-Z]+ )$char( -?\d+)$/m) {
892 $one = $1;
893 $kdefs =~ s[^(KPX [a-zA-Z]+ )$char( -?\d+)$]
894 [$&\n$1$char$acc$2]gm
895 unless $kdefs =~ /^$one$char$acc /m;
896 }
897 }
898 }
899
900 sub fixshrink {
901 #
902 # Multiply all coordinates in a character definition by $shrink,
903 # creating new subroutines as necessary.
904 #
905 my ($ch, $chdef) = @_;
906 my (@chdef, $line, $snum, $subrdef);
907 @chdef = split(/\n/, $chdef);
908 $chdef = "";
909 foreach $line (@chdef) {
910 if ($line =~ /^\t(\d+) .*call(other)?subr$/) {
911 $snum = $1;
912 $subrdef = $subrs[$snum];
913 $subrdef = fixshrink($ch, $subrdef);
914 $subrdef =~ s/^(dup )$snum \{$/$1$subrsnum {\n % $nchar: shrink $ch/m;
915 $subrs[$subrsnum] = $subrdef;
916 $line =~ s/^\t$snum/\t$subrsnum/;
917 $subrsnum++;
918 }
919 else { $line =~ s/-?\d+/int($& * $shrink)/ge unless $line =~ /^dup / }
920 $chdef .= "$line\n";
921 }
922 return $chdef;
923 }
924
925 sub fixhints {
926 #
927 # First adjust any Flex "0 callsubr" lines for horizontal and
928 # vertical offset. Then collect the hints from the accent definition,
929 # remove them, adjust for horizontal and vertical offset, and
930 # place them in a numbered subroutine. Have the definition call
931 # the subroutine.
932 #
933 my ($nchar, $acc, $acdef, $h, $v) = @_;
934 my $subrdef;
935 $acdef =~ s[^(\t-?\d+ )(-?\d+) (-?\d+)( 0 callsubr)$]
936 [$1 . ($2 + $h) . " " . ($3 + $v) . $4]gme;
937 if ($subrdef = join("\n", grep /^\t.*[hv]stem$/, split(/\n/, $acdef))) {
938 $acdef = join("\n", grep !/^\t.*[hv]stem$/, split(/\n/, $acdef)) . "\n";
939 }
940 elsif ($acdef =~ s/\A(?:\t(\d+) callsubr\n)((?:.|\n)*)\Z/$2/m) {
941 $subrdef = $subrs[$1];
942 $subrdef = join("\n", grep /^\t.*[hv]stem$/, split(/\n/, $subrdef))
943 }
944 $subrdef =~ s/^(\t)(-?\d+)( .*hstem)$/$1 . ($2 + $v) . $3/gme;
945 $subrdef =~ s/^(\t)(-?\d+)( .*vstem)$/$1 . ($2 + $h) . $3/gme;
946 if ($subrdef) {
947 $subrs[$subrsnum] = "dup $subrsnum {\n % $nchar: hints for $acc\n";
948 $subrs[$subrsnum] .= "$subrdef\n\treturn\n\t} $np\n";
949 $acdef = "\t$subrsnum 1 3 callothersubr\n\tpop\n\tcallsubr\n" . $acdef;
950 $subrsnum++;
951 }
952 return $acdef;
953 }
954
955 sub findend {
956 #
957 # Calculate the current point reached at the end of the character
958 # definition. Place the horizontal offset in $hend{$char}, the
959 # vertical offset in $vend{$char}.
960 #
961 my ($char, $def) = @_;
962 my $line;
963 foreach $line (split /\n/, $def) {
964 SWITCH: {
965 $line =~ /^\t(-?\d+) h(line|move)to$/
966 && do {
967 $hend{$char} += $1;
968 last SWITCH;
969 };
970 $line =~ /^\t(-?\d+) v(line|move)to$/
971 && do {
972 $vend{$char} += $1;
973 last SWITCH;
974 };
975 $line =~ /^\t(-?\d+) (-?\d+) r(line|move)to$/
976 && do {
977 $hend{$char} += $1;
978 $vend{$char} += $2;
979 last SWITCH;
980 };
981 $line =~ /^\t(-?\d+) (-?\d+) (-?\d+) (-?\d+) hvcurveto$/
982 && do {
983 $hend{$char} += ($1 + $2);
984 $vend{$char} += ($3 + $4);
985 last SWITCH;
986 };
987 $line =~ /^\t(-?\d+) (-?\d+) (-?\d+) (-?\d+) vhcurveto$/
988 && do {
989 $hend{$char} += ($2 + $4);
990 $vend{$char} += ($1 + $3);
991 last SWITCH;
992 };
993 $line =~ /^\t(-?\d+) (-?\d+) (-?\d+) (-?\d+) (-?\d+) (-?\d+) rrcurveto$/
994 && do {
995 $hend{$char} += ($1 + $3 + $5);
996 $vend{$char} += ($2 + $4 + $6);
997 last SWITCH;
998 };
999 $line =~ /^\t(\d+) callsubr$/
1000 && do {
1001 findend($char, $subrs[$1]);
1002 last SWITCH;
1003 };
1004 $line =~ /^\t-?\d+ (-?\d+) (-?\d+) 0 callsubr$/ # Flex
1005 && do {
1006 $hend{$char} = $1;
1007 $vend{$char} = $2;
1008 last SWITCH;
1009 };
1010 }
1011 }
1012 }