1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
#!/usr/bin/perl
#
# BuildFoundries: Given a Foundry file, generate groff font description
# files and a "download" file so gropdf can embed fonts in PDF output.
#
# Copyright (C) 2011-2020 Free Software Foundation, Inc.
# Written by Deri James <deri@chuzzlewit.myzen.co.uk>
#
# This file is part of groff.
#
# groff is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# groff is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use Getopt::Long;
use warnings;
my $pathsep='@PATH_SEPARATOR@';
my $check=0;
my $dirURW='';
my $beStrict=0;
GetOptions("check" => \$check, "dirURW=s" => \$dirURW,
"strict" => \$beStrict);
(my $progname = $0) =~s @.*/@@;
my $where=shift||'';
my @d=(split(':',shift||'../devps'));
my $devps=\@d;
chdir $where if $where ne '';
my (%flg,@downloadpreamble,%download);
my $GSpath=FindGSpath();
my $lct=0;
my $foundry=''; # the default foundry
my $notFoundFont=0;
if ($check)
{
CheckFoundry("Foundry.in");
exit $notFoundFont;
}
else
{
LoadDownload("download"); # not required
LoadFoundry("Foundry");
WriteDownload();
}
exit 0;
sub LoadFoundry
{
my $fn=shift;
my $foundrypath;
$notFoundFont=0;
open(F,"<$fn") or Die("file '$fn' not found or not readable");
while (<F>)
{
chomp;
$lct++;
s/\r$//; # in case edited in windows
s/\s*#.*?$//; # remove comments
next if $_ eq '';
if (m/^[A-Za-z]=/)
{
my (@f)=split('=');
$flg{$f[0]}=$f[1];
next;
}
my (@r)=split('\|');
if (lc($r[0]) eq 'foundry')
{
Warn("\nThe path(s) used for searching:\n".join(':',@{$foundrypath})."\n") if $notFoundFont;
$foundry=uc($r[1]);
$foundrypath=[];
push(@{$foundrypath},$dirURW) if $dirURW;
push(@{$foundrypath},(split(':',$r[2])),@{$devps});
foreach my $j (0..$#{$foundrypath})
{
if (defined($foundrypath->[$j])
&& $foundrypath->[$j]=~m'\s*\(gs\)')
{
splice(@{$foundrypath},$j,1,@{$GSpath});
}
}
$notFoundFont=0;
}
else
{
# 0=groff font name
# 1=IsBase Y/N (one of PDFs 14 base fonts)
# 2=afmtodit flag
# 3=map file
# 4=encoding file
# 5=font file
my $gfont=($foundry eq '')?$r[0]:"$foundry-$r[0]";
if ($r[2] eq '')
{
# Don't run afmtodit; just copy the groff font
# description file for grops.
my $gotf=1;
my $gropsfnt=LocateFile($devps,$r[0],0);
if ($gropsfnt ne '' and -r "$gropsfnt")
{
my $psfont=UseGropsVersion($gropsfnt);
# To be embeddable in PDF, the font file name itself
# needs to be located and written to "download".
if (!PutDownload($psfont,
LocatePF($foundrypath,$r[5]),
uc($r[1])))
{
if (uc($r[1]) ne 'Y')
{
$gotf=0;
my $fns=join(', ',split('!',$r[5]));
Warn("groff font '$gfont' will not be"
. " available for PDF output; unable"
. " to locate font file(s): $fns");
$notFoundFont=1;
unlink $gfont;
}
}
Notice("copied grops font $gfont") if $gotf;
}
else
{
Warn("Can't read grops font '$r[0]' for Foundry '$foundry'");
}
}
else
{
# Use afmtodit to create a groff font description file.
my $afmfile=LocateAF($foundrypath,$r[5]);
if (!$afmfile) {
my $sub=\&Warn;
$sub=\&Die if ($beStrict);
&$sub("cannot locate AFM file for font '$gfont'");
next;
}
my $psfont=RunAfmtodit($gfont,$afmfile,$r[2],$r[3],$r[4]);
if ($psfont)
{
if (!PutDownload($psfont,LocatePF($foundrypath,$r[5]),uc($r[1])))
{
unlink $gfont; # Unable to find the postscript file for the font just created by afmtodit
}
else
{
Notice("generated $gfont");
}
}
else
{
Warn("Failed to create groff font '$gfont' by running afmtodit");
$notFoundFont=1;
}
}
}
}
close(F);
Warn("\nThe path(s) used for searching:\n".join(':',@{$foundrypath})."\n") if $notFoundFont;
}
sub RunAfmtodit
{
my $gfont=shift;
my $afmfile=shift;
my $flags=shift;
my $map=shift||'';
my $enc=shift||'';
my $psfont='';
$enc="-e 'enc/$enc'" if $enc;
$map="'map/$map'" if $map;
my $cmd='afmtodit -c -dDESC';
foreach my $f (split('',$flags))
{
if (!exists($flg{$f}))
{
Warn("Can't use undefined flag '$f' in calling afmtodit for groff font '$gfont'");
return('');
}
$cmd.=" $flg{$f}";
}
system("$cmd $enc '$afmfile' $map $gfont");
if ($?)
{
unlink $gfont;
return('');
}
if (open(GF,"<$gfont"))
{
my (@gf)=(<GF>);
my @ps=grep(/^internalname /,@gf);
if ($#ps == 0) # Just 1 match
{
(undef,$psfont)=split(' ',$ps[0],2);
chomp($psfont);
}
else
{
Warn("Unexpected format for grops font '$gfont' for Foundry '$foundry' - ignoring");
}
close(GF);
}
return($psfont);
}
sub LocateAF
{
my $path=shift;
my $file=shift;
return(LocateFile($path,$file,1));
}
sub LocatePF
{
my $path=shift;
my $file=shift;
return(LocateFile($path,$file,0));
}
sub LocateFile
{
my $path=shift;
my $files=shift;
my $tryafm=shift;
return(substr($files,1)) if substr($files,0,1) eq '*';
foreach my $p (@{$path})
{
next if !defined($p) or $p eq ';' or $p eq ':';
$p=~s/^\s+//;
$p=~s/\s+$//;
$p=~s@/+$@@;
next if $p=~m/^\%rom\%/; # exclude %rom% paths (from (gs))
foreach my $file (reverse(split('!',$files)))
{
if ($tryafm)
{
if (!($file=~s/\..+$/.afm/))
{
# no extenaion
$file.='.afm';
}
}
if ($file=~m'/')
{
# path given with file name so no need to search the paths
if (-r $file)
{
return($file);
}
if ($tryafm and $file=~s'type1/'afm/'i)
{
if (-r "$file")
{
return($file);
}
}
return('');
}
if ($path eq '(tex)')
{
my $res=`kpsewhich $file`;
return '' if $?;
chomp($res);
return($res);
}
if (-r "$p/$file")
{
return("$p/$file");
}
my $ap=$p;
if ($tryafm and $ap=~s'type1/'afm/'i)
{
if (-r "$ap/$file")
{
return("$ap/$file");
}
}
}
}
return('');
}
sub FindGSpath
{
my (@res)=`@GHOSTSCRIPT@ -h 2>/dev/null`;
return [] if $?;
my $buildpath=[];
my $stg=1;
foreach my $l (@res)
{
chomp($l);
if ($stg==1 and $l=~m/^Search path:/)
{
$stg=2;
}
elsif ($stg == 2)
{
if (substr($l,0,1) ne ' ')
{
$stg=3;
}
else
{
$l=~s/^\s+//;
$pathsep=';' if substr($l,-1) eq ';';
push(@{$buildpath},(split("$pathsep",$l)));
}
}
}
return($buildpath);
}
sub UseGropsVersion
{
my $gfont=shift;
my $psfont='';
my (@gfa)=split('/',$gfont);
my $gfontbase=pop(@gfa);
if (open(GF,"<$gfont"))
{
my (@gf)=(<GF>);
my @ps=grep(/^internalname /,@gf);
if ($#ps == 0) # Just 1 match
{
(undef,$psfont)=split(' ',$ps[0],2);
chomp($psfont);
}
else
{
Warn("Unexpected format for grops font '$gfont' for Foundry '$foundry' - ignoring");
}
close(GF);
if ($psfont)
{
if (open(GF,">$gfontbase"))
{
local $"='';
print GF "@gf";
close(GF);
}
else
{
$psfont='';
Warn("Failed to create new font '$gfont' for Foundry '$foundry'");
}
}
else
{
Warn("Failed to locate postscript internalname in grops font '$gfont' for Foundry '$foundry'");
}
close(GF);
}
else
{
Warn("Failed to open grops font '$gfont' for Foundry '$foundry'");
}
return($psfont);
}
sub PutDownload
{
my $psfont=shift;
my $pffile=shift;
my $IsBase14=shift;
my $key="$foundry $psfont";
delete($download{$key}), return 0 if ($pffile eq '');
$pffile='*'.$pffile if $IsBase14 eq 'Y'; # This signals to gropdf to only edmbed if -e given
$download{$key}=$pffile;
return 1;
}
sub LoadDownload
{
my $fn=shift;
return if !open(F,"<$fn");
while (<F>)
{
chomp;
s/\r$//; # in case edited in windows
if (substr($_,0,1) eq '#' or $_ eq '')
{
# Preserve comments at top of download file
push(@downloadpreamble,$_);
next;
}
s/\s*#.*?$//; # remove comments
next if $_ eq '';
my (@r)=split(/\t+/);
my $key=$r[1];
$key="$r[0] $r[1]";
$download{$key}=$r[2];
}
close(F);
}
sub WriteDownload
{
print join("\n",@downloadpreamble),"\n";
foreach my $k (sort keys %download)
{
my ($f,$ps)=split(/ /,$k);
print "$f\t$ps\t$download{$k}\n";
}
}
sub Notice {
my $msg=shift;
Msg("notice: $msg");
}
sub Warn {
my $msg=shift;
Msg("warning: line $lct: $msg");
}
sub Die {
my $msg=shift;
Msg("error: $msg");
exit 2;
}
sub Msg {
my $msg=shift;
print STDERR "$progname: $msg\n";
}
sub CheckFoundry
{
my $fn=shift;
my $foundrypath=[];
$notFoundFont=0;
open(F,"<$fn") or Die("file '$fn' not found or not readable");
while (<F>)
{
chomp;
s/\r$//; # in case edited in windows
s/\s*#.*?$//; # remove comments
next if $_ eq '';
if (m/^[A-Za-z]=/)
{
next;
}
my (@r)=split('\|');
if (lc($r[0]) eq 'foundry')
{
$foundry=uc($r[1]);
$foundrypath=[];
push(@{$foundrypath},$dirURW) if $dirURW;
push(@{$foundrypath},(split(':',$r[2])),$devps);
foreach my $j (0..$#{$foundrypath})
{
if ($foundrypath->[$j]=~m'\s*\(gs\)')
{
splice(@{$foundrypath},$j,1,@{$GSpath});
}
}
$notFoundFont=0;
}
else
{
# 0=groff font name
# 1=IsBase Y/N (one of PDFs 14 base fonts)
# 2=afmtodit flag
# 3=map file
# 4=encoding file
# 5=font file
my $gfont=($foundry eq '')?$r[0]:"$foundry-$r[0]";
if ($r[2] eq '')
{
# Don't run afmtodit, just copy the grops font file
my $gotf=1;
my $gropsfnt=LocateFile([$devps],$r[0],0);
if ($gropsfnt ne '' and -r "$gropsfnt")
{
}
else
{
$notFoundFont|=1;
}
}
else
{
# We need to run afmtodit to create this groff font
$notFoundFont|=2 if !LocateAF($foundrypath,$r[5]);
$notFoundFont|=1 if !LocatePF($foundrypath,$r[5]);
}
}
}
close(F);
}
# Local Variables:
# fill-column: 72
# mode: CPerl
# End:
# vim: set cindent noexpandtab shiftwidth=4 softtabstop=4 textwidth=72:
|