Debian Codegraph @

mp2pf

raw
1 #!/usr/bin/perl
2
3 #
4 # METAPOST to Type 1 converter
5 #
6 # THIS FILE IS PUBLIC DOMAIN NOTWITHSTANDING THE COPYRIGHT ON THE
7 # OVERALL TSUKURIMASHOU PACKAGE
8 #
9 # This file is based on the file "mp2pf.awk" from the METATYPE1 package,
10 # version 0.55. It is a manual translation by Matthew Skala from the
11 # original Awk into Perl, with a lot of features irrelevant to the
12 # Tsukurimashou project (namely hinting, kerning, ligatures, and AFM
13 # files) removed to improve maintainability.
14 #
15 # The mp2pf.awk file contains no copyright-related notices of its own, but
16 # the README for METATYPE1 version 0.55 contains the following notices (in
17 # English and Polish; the slashes are verbatim from the original and
18 # presumably are some convention for expressing non-ASCII Polish letters in
19 # the ASCII file):
20 #
21 # This is METATYPE1 package -- a tool for creating Type 1 fonts using
22 # METAPOST. The package belongs to public domain (no copyrights,
23 # copylefts, copyups, copydowns, etc.).
24 # Version: 0.55 (16.09.2009; a tentative version, released along with
25 # the sources of the Latin Modern fonts ver. 2.003)
26 # Author: JNS team <[email protected]>
27 #
28 # To jest pakiet METATYPE1 -- narz/edzie do tworzenia font/ow Type 1
29 # za pomoc/a systemu METAPOST. Pakiet stanowi dobro wsp/olne
30 # (/zadnych copyright/ow, copyleft/ow, copyup/ow, copydown/ow, etc.).
31 # Wersja: 0.55 (16.09.2009 -- wersja opublikowana wraz z wersj/a
32 # /xr/od/low/a 2.003 pakietu font/ow Latin Modern)
33 # Autorstwo: JNS team <[email protected]>
34 #
35 # Although I assert my general right to claim copyright on work of my own
36 # that draws from public domain source materials, I nonetheless am releasing
37 # this file to the public domain in an effort to maintain the spirit of the
38 # JNS team's release above.
39 #
40 # The Adobe copyright notice in the Postscript template code below
41 # refers only to certain boilerplate required by the file format; it
42 # applies to no other part of this file and falls under the "scènes
43 # à faire" doctrine in jurisdictions where that is relevant.
44 #
45 # This program is distributed in the hope that it will be useful,
46 # but WITHOUT ANY WARRANTY; without even the implied warranty of
47 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
48 #
49 # Matthew Skala
50 # [email protected]
51 #
52
53 ########################################################################
54
55 # header from the Awk version (much of this is now irrelevant due to
56 # deleted code):
57
58 # THIS FILE BELONGS TO THE METATYPE1 PACKAGE
59 #
60 # It is an AWK script that does the main job of convertion from
61 # METAPOST output to (raw) Type 1 format
62 # History:
63 # 20.04.2006: 0.40, another bug fixed in set_hint (a[5] and a[6]
64 # used to be used instead of a[7] and a[8], respectively).
65 # 24.03.2006: ver. 0.39, a bug fixed in setting vstems using the old scheme
66 # (hsb was not taken into account); local variable `k'
67 # introduced in `find_stem3'
68 # 13-20.11.2005: ver. 0.385 adaptation to the new hinting scheme -- cont.
69 # global hints are not collected (needed by old interpreters);
70 # BJ: hit replacement corrected (param. stemx added in
71 # hint_clash; clear_hint doest not touch triple stem data);
72 # 26.10.2005: adaptation to the new hinting scheme -- cont. (20.5->-20.5)
73 # 15.10.2005: beginnig of the adaptation to the new hinting scheme
74 # 09.07.2005: ver. 0.38, adaptation to the possibility of writing
75 # implicit encodings (like StandardEncoding) and
76 # to the handling of (optional) CharSet by afm2pfm
77 # 15.02.2005: ver. 0.37, doubled kerns and ligatures silently ignored by
78 # default; they can be reported on demand by setting the
79 # parameter NQ (Non-Quiet; newly introduced) to a `true' value;
80 # messages prefixed with `MP2PF:'
81 # 18.09.2004: ver. 0.36, awk script no longer cares for fontdimen names
82 # 29.04.2003: ver. 0.35, a bug in gawk circumvented (in clear_hints)
83 # 18.01.2003: ver. 0.34, normal round function introduced; old one
84 # renamed to fround (forced round); no more mess due
85 # ADL comment in PFB and Ascender and Descender ones in AFM;
86 # again fun. rational: superfluously robust ;-)
87 # 06.01.2003: ver. 0.33, fun. rational: unlikely case included (as a comment)
88 # 03.01.2003: ver. 0.32, fontdimens and headerbytes handled
89 # 31.07.2002: banner added
90 # 14.07.2002: comment (question) added in `pickup_stem'
91 # 22.09.2001: ver. 0.31 (remarks moved to an informal readme.his file)
92 # 26.01.2001: an empty set of KPX pairs admitted
93 # 14.01.2001: functions rational and approx added
94
95 ########################################################################
96
97 # General utility functions
98
99 # Find a string describing a rational approximation of a real number
100 # Awk version was apparently based on "code kindly sent us by
101 # Berthold K. P. Horn"
102 sub rational {
103 my($x,$nlimit,$dlimit)=@_;
104 my($p0,$q0,$p1,$q1,$p2,$q2,$s,$ds);
105
106 # handle zero, negative, and large
107 return '0 1' if $x==0;
108 return '-'.&rational(-$x,$nlimit,$dlimit) if $x<0.0;
109 return &round($x) if $x>$nlimit;
110
111 # loop over guesses until the numerator and denominator are too large
112 $p0=0; $q0=1; $p1=1; $q1=0; $s=$x;
113 while (1) {
114
115 # check for loop termination with best guess
116 if ((int($s)!=0) &&
117 (($p1>($nlimit-$p0)/int($s)) || ($q1>($dlimit-$q0)/int($s)))) {
118 $p2=$p1;
119 $q2=$q1;
120 last;
121 }
122
123 # try a new guess, terminate if we're lucky and it's exact
124 $p2=$p0+int($s)*$p1;
125 $q2=$q0+int($s)*$q1;
126 last if $p2/$q2==$x;
127 $ds=$s-int($s);
128 last if $ds==0;
129 $p0=$p1; $q0=$q1; $p1=$p2; $q1=$q2; $s=1/$ds;
130 }
131
132 return "$p2 $q2";
133 }
134
135 # abs() is built-in in Perl and doesn't need to be defined as a function
136
137 sub min { return $_[0]<$_[1]?$_[0]:$_[1]; }
138 sub max { return $_[0]>$_[1]?$_[0]:$_[1]; }
139
140 sub round { return $_[0]>=0?int($_[0]+0.5):-int(-$_[0]+0.5); }
141
142 # "forced round" - round to nearest int, but complain about it
143 sub fround {
144 my($x)=@_;
145
146 if ($x!=int($x)) {
147 &mess("MP2PF: WRONG ROUNDING IN METAPOST $x IN $name");
148 return $x>=0?int($x+0.5):-int(-$x+0.5);
149 } else {
150 return $x;
151 }
152 }
153
154 # sort space-separated words; we use Perl's instead of the Awk
155 # version's homemade bubble sort
156 sub trivial_sort {
157 return join(' ',sort { $a <=> $b } split(' ',$_[0]));
158 }
159
160 # write an error message - /dev/tty and log file stuff removed
161 sub mess {
162 print STDERR $_[0]."\n";
163 }
164
165 ########################################################################
166
167 # Functions that generate Postscript code
168
169 # Generate Postscript code for a rational approximation
170 sub approx {
171 my($x)=@_;
172 my($r,$a,$b);
173
174 # Awk version says:
175 # "32767 is a limit due to old (DOS?) implementations of t1utils"
176 $r=&rational($x,32767,32767);
177 ($a,$b)=split(' ',$r);
178 return $b==1?$a:"$r div";
179 }
180
181 sub make_lineto {
182 my($dx,$dy)=@_;
183
184 if (($dx!=0) || ($dy!=0)) {
185 if ($dx==0) {&store_pfb("\t".&fround($dy).' vlineto');}
186 elsif ($dy==0) {&store_pfb("\t".&fround($dx).' hlineto');}
187 else {&store_pfb("\t".&fround($dx).' '.&fround($dy).' rlineto');}
188 }
189 }
190
191 sub make_moveto {
192 my($dx,$dy)=@_;
193
194 if (($dx!=0) || ($dy!=0)) {
195 if ($dx==0) {&store_pfb("\t".&fround($dy).' vmoveto');}
196 elsif ($dy==0) {&store_pfb("\t".&fround($dx).' hmoveto');}
197 else {&store_pfb("\t".&fround($dx).' '.&fround($dy).' rmoveto');}
198 }
199 }
200
201 sub make_curveto {
202 my($dx1,$dy1,$dx2,$dy2,$dx3,$dy3)=@_;
203
204 if (($dx1+$dx2+$dx3!=0) || ($dy1+$dy2+$dy3!=0)) {
205 if (($dy1==0) && ($dx3==0)) {
206 &store_pfb("\t".&fround($dx1).' '.&fround($dx2).' '
207 .&fround($dy2).' '.&fround($dy3).' hvcurveto');
208 } elsif (($dx1==0) && ($dy3==0)) {
209 &store_pfb("\t".&fround($dy1).' '.&fround($dx2).' '
210 .&fround($dy2).' '.&fround($dx3).' vhcurveto');
211 } else {
212 &store_pfb("\t".&fround($dx1).' '.&fround($dy1).' '.&fround($dx2).' '
213 .&fround($dy2).' '.&fround($dx3).' '.&fround($dy3).' rrcurveto');
214 }
215 }
216 }
217
218 ########################################################################
219
220 # Other functions
221
222 # extract static data from what was pfcommon.dat
223 sub get_data_files {
224 my($s,$key,$a);
225
226 @PFB_PRO=split(/\n/,<<EOF);
227 %!PS-AdobeFont-1.0: ##FONT_NAME## ##VERSION##
228 %%CreationDate: ##CREATION_DATE##
229 % Generated by MetaType1 (a MetaPost-based engine)
230 % ##AUTHOR##
231 % ADL: ##ADL_ASCENDER## ##ADL_DESCENDER## ##ADL_LINESKIP##
232 %%EndComments
233 FontDirectory/##FONT_NAME## known{/##FONT_NAME## findfont dup/UniqueID known{dup
234 /UniqueID get 0 eq exch/FontType get 1 eq and}{pop false}ifelse
235 {save true}{false}ifelse}{false}ifelse
236 17 dict begin
237 /FontInfo 9 dict dup begin
238 /version(##VERSION##)readonly def
239 /Notice(##AUTHOR##)readonly def
240 /FullName(##FULL_NAME##)readonly def
241 /FamilyName(##FAMILY_NAME##)readonly def
242 /Weight(##WEIGHT##)readonly def
243 /isFixedPitch ##FIXED_PITCH## def
244 /ItalicAngle ##ITALIC_ANGLE## def
245 /UnderlinePosition ##UNDERLINE_POSITION## def
246 /UnderlineThickness ##UNDERLINE_THICKNESS## def
247 end readonly def
248 /FontName /##FONT_NAME## def
249 ##ENCODING_DATA##
250 /PaintType 0 def
251 /FontType 1 def
252 /StrokeWidth 0 def
253 /FontMatrix[0.001 0 0 0.001 0 0]readonly def
254 %/UniqueID 0 def
255 /FontBBox{##FONT_BOUNDING_BOX##}readonly def
256 currentdict end
257 currentfile eexec
258 dup/Private 19 dict dup begin
259 /RD{string currentfile exch readstring pop}executeonly def
260 /ND{noaccess def}executeonly def
261 /NP{noaccess put}executeonly def
262 /MinFeature{16 16}ND
263 %/UniqueID 0 def
264 /StdHW[##STDHW##]def
265 /StdVW[##STDVW##]def
266 /StemSnapH[##STEMSNAPH##]def
267 /StemSnapV[##STEMSNAPV##]def
268 /ForceBold ##FORCE_BOLD## def
269 /password 5839 def
270 % Copyright (c) 1987-1990 Adobe Systems Incorporated
271 % All Rights Reserved.
272 % This code to be used for hint replacement only
273 /OtherSubrs
274 [ { } { } { }
275 {
276 systemdict /internaldict known not
277 { pop 3 }
278 { 1183615869 systemdict /internaldict get exec
279 dup /startlock known
280 { /startlock get exec }
281 { dup /strtlck known
282 { /strtlck get exec }
283 { pop 3 }
284 ifelse }
285 ifelse }
286 ifelse
287 } executeonly
288 ] noaccess def
289 /Subrs ##NUMBER_OF_SUBRS## array
290 dup 0 {
291 3 0 callothersubr
292 pop
293 pop
294 setcurrentpoint
295 return
296 } NP
297 dup 1 {
298 0 1 callothersubr
299 return
300 } NP
301 dup 2 {
302 0 2 callothersubr
303 return
304 } NP
305 dup 3 {
306 return
307 } NP
308 dup 4 {
309 1 3 callothersubr
310 pop
311 callsubr
312 return
313 } NP
314 ##SUBROUTINES##
315 ND
316 2 index
317 /CharStrings ##NUMBER_OF_CHARSTRINGS## dict dup begin
318 /.notdef {
319 0 280 hsbw
320 endchar
321 } ND
322 EOF
323
324 @PFB_TRA=split(/\n/,<<EOF);
325 end end readonly put put
326 dup/FontName get exch definefont pop
327 mark currentfile closefile
328 cleartomark
329 {restore}if
330 EOF
331
332 open(FD,$FD);
333 while (<FD>) {
334 chomp;
335 s/\s+$//;
336 if (/^(.*?) +: *(.*?)$/) {
337 $font_data{$1}=$2;
338 } else {
339 &mess('MP2PF: IMPROPER FONT INFO FILE');
340 }
341 }
342 close(FD);
343 }
344
345 sub store_pfb { push @pfb_text,$_[0]; }
346
347 sub flush_pfb {
348 my($s,$i,$n);
349
350 open(PFB,">$PFB");
351
352 foreach $n (0..$#PFB_PRO) {
353 $_=$PFB_PRO[$n];
354 foreach $i (keys %font_data) {
355 while (/^(.*)##$i##(.*)$/) {
356 if ($font_data{$i} eq '') {
357 $_='';
358 } else {
359 $_=$1.$font_data{$i}.$2;
360 }
361 }
362 }
363
364 $_='' if /##OTHER_BLUES##/;
365
366 if (/##ENCODING_DATA##/) {
367 if ($font_data{'ENCODING_NAME'} ne '') {
368 $_='/Encoding '.$font_data{'ENCODING_NAME'}.' def';
369 } else {
370 $_="/Encoding 256 array\n"
371 ."0 1 255 {1 index exch /.notdef put} for\n";
372 foreach $i (0..255) {
373 if (exists $enc_name[$i]) {
374 $_.="dup $i/$enc_name[$i] put\n";
375 }
376 }
377 $_.='readonly def';
378 }
379 }
380
381 s/##FONT_BOUNDING_BOX##/$mllx $mlly $murx $mury/;
382 if (/##STDHW##/) {
383 if ($stdhw==0) {
384 $_='';
385 } else {
386 s/##STDHW##/$stdhw/;
387 }
388 }
389 if (/##STDVW##/) {
390 if ($stdvw==0) {
391 $_='';
392 } else {
393 s/##STVHW##/$stdvw/;
394 }
395 }
396 if (/##STEMSNAPH##/) {
397 if ($stdhw==0) {
398 $_='';
399 } else {
400 s/##STEMSNAPH##/&tab2str($stemsnaph)/e;
401 }
402 }
403 if (/##STEMSNAPV##/) {
404 if ($stdvw==0) {
405 $_='';
406 } else {
407 s/##STEMSNAPV##/&tab2str($stemsnapv)/e;
408 }
409 }
410 s/##NUMBER_OF_SUBRS##/2+$subrs_base+$#subrs/e;
411 $_='' if /##SUBROUTINES##/;
412 s/##NUMBER_OF_CHARSTRINGS##/$num_enc_chars+$num_oth_chars+1/e;
413
414 if (/(##[^#]+##)/) {
415 mess("MP2PF: EXTRA TAG: $1");
416 }
417
418 print PFB "$_\n" if $_ ne '';
419 }
420
421 foreach (@pfb_text) {
422 print PFB "$_\n" if $_ ne '';
423 }
424
425 foreach (@PFB_TRA) {
426 print PFB "$_\n" if $_ ne '';
427 }
428
429 close(PFB);
430 }
431
432 ########################################################################
433
434 # Main program
435
436 $tsuversion='UNKNOWN';
437 open(MAKEFILE,'Makefile');
438 while (<MAKEFILE>) {
439 $tsuversion=$1 if /^VERSION\s*=\s*(\S+)/;
440 }
441 close(MAKEFILE);
442
443 &mess("This is mp2pf from Tsukurimashou $tsuversion "
444 .'(converts MP EPSes to PS Type 1 fonts)');
445
446 $NAME=shift;
447
448 if ($NAME eq '') {
449 &mess('MP2PF: NAME MUST NOT BE EMPTY');
450 exit(1);
451 }
452
453 $AFM="$NAME.afm";
454 $PFB="$NAME.p";
455 $FD="$NAME.pfi";
456 $KD="$NAME.kpx";
457 $PL='piclist.tex';
458 $PL=$1.$PL if $NAME=~m!^(.*/)!;
459
460 $max_font_dimen=99; $max_header_byte=99;
461 $mllx=10000; $mlly=10000; $murx=-10000; $mury=-10000;
462
463 &get_data_files;
464
465 # dominant stems chosen manually have the highest priority:
466 $vstem_stat{$font_data{'STDVW'}}=10000 if defined $font_data{'STDVW'};
467 $hstem_stat{$font_data{'STDHW'}}=10000 if defined $font_data{'STDHW'};
468
469 # standard empty subr has number 3==subrs_base-1
470 $subrs_base=4; $xsubrs{''}=-1;
471
472 # the following code must be consistent with tsufb.mp
473 open(PL,$PL);
474 while (<PL>) {
475 chomp;
476 if (/EPSNAMEandNumber\{.*?\}\{([0-9]+)\}/i) {
477 push @file,"$NAME.$1";
478 }
479 }
480 close(PL);
481
482 foreach $curr_file (@file) {
483 open(CURR_FILE,$curr_file);
484 while (<CURR_FILE>) {
485 @F=split(/\s+/,$_);
486
487 if (/%%BoundingBox:/) {
488 print STDERR "#" if ($n++)%4==0;
489 $name=''; $code=0; $hsb=0; $width=0; $x0=0; $y0=0; # sentinels
490 $curr_path=0; $acc_path=0;
491 $hsb=$F[1]; $llx=$F[1]; $lly=$F[2]; $urx=$F[3]; $ury=$F[4];
492 $mllx=$llx if $mllx>$llx; $mlly=$lly if $mlly>$lly;
493 $murx=$urx if $murx<$urx; $mury=$ury if $mury<$ury;
494 }
495
496 if (/^%GLYNFO:/) {
497 if ($F[1] eq 'NAME') {$name=$F[2]; $code=$F[3];}
498 if ($F[1] eq 'HSBW') {
499 $width=$F[3];
500 $x0=$hsb; $y0=0;
501 }
502 if ($F[1] eq 'ACC') {$acc_path=$F[2];}
503 if ($F[1] eq 'BEGINCHAR') {
504 &store_pfb("/$name {\n\t".&fround($hsb).' '.&approx($width).' hsbw');
505 if ($code>=0) {
506 $num_enc_chars++;
507 $enc_name[$code]=$name;
508 } else {
509 $num_oth_chars++;
510 }
511 }
512 }
513
514 if (/lineto/) {
515 &make_lineto($F[0]-$x0,$F[1]-$y0);
516 $x0=$F[0]; $y0=$F[1]
517 } elsif (/curveto/) {
518 &make_curveto($F[0]-$x0,$F[1]-$y0,$F[2]-$F[0],$F[3]-$F[1],
519 $F[4]-$F[2],$F[5]-$F[3]);
520 $x0=$F[4]; $y0=$F[5];
521 } elsif (/moveto/) {
522 &make_moveto($F[1]-$x0,$F[2]-$y0);
523 $x0=$F[1]; $y0=$F[2]; $x_path0=$x0; $y_path0=$y0;
524 }
525
526 if (/closepath/) {
527 if (($x0!=$x_path0) || ($y0!=$y_path0)) {
528 # closing with implicit `lineto'
529 &store_pfb("\tclosepath");
530 }
531 }
532
533 if (/showpage/) {
534 &store_pfb("\tendchar\n\t} ND");
535 }
536 }
537
538 close(CURR_FILE);
539 }
540
541 print STDERR "\n";
542
543 &flush_pfb;