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
|
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
if 0;
#D \module
#D [ file=makempy.pl,
#D version=2000.12.14,
#D title=\METAFUN,
#D subtitle=\METAPOST\ Text Graphics,
#D author=Hans Hagen,
#D date=\currentdate,
#D copyright={PRAGMA / Hans Hagen \& Ton Otten}]
#C
#C This module is part of the \CONTEXT\ macro||package and is
#C therefore copyrighted by \PRAGMA. See licen-en.pdf for
#C details.
# Tobias Burnus provided the code needed to proper testing
# of binaries on UNIX as well as did some usefull suggestions
# to improve the functionality.
# This script uses GhostScript and PStoEdit as well as
# pdfTeX, and if requested TeXEdit and ConTeXt.
# todo: we can nowadays do without the intermediate step, because GS
# can now handle PDF quite good
use Getopt::Long ;
use Config ;
use strict ;
$Getopt::Long::passthrough = 1 ; # no error message
$Getopt::Long::autoabbrev = 1 ; # partial switch accepted
my $help = 0 ;
my $silent = 0 ;
my $force = 0 ;
my $noclean = 0 ;
my $amethod = my $pmethod = my $gmethod = 0 ;
my $format = "plain" ; # can be "context" for plain users too
&GetOptions
( "help" => \$help ,
"silent" => \$silent ,
"force" => \$force ,
"pdftops" => \$pmethod ,
"xpdf" => \$pmethod ,
"gs" => \$gmethod ,
"ghostscript" => \$gmethod ,
"noclean" => \$noclean ) ;
my $mpochecksum = 0 ;
my %tex ; my %start ; my %stop ;
$tex{plain} = "pdftex" ;
$tex{latex} = "pdflatex" ;
$tex{context} = "texexec --batch --once --interface=en --pdf" ;
$start{plain} = '' ;
$stop{plain} = '\end' ;
$start{latex} = '\begin{document}' ;
$stop{latex} = '\end{document}' ;
$start{context} = '\starttext' ;
$stop{context} = '\stoptext' ;
my $ghostscript = "" ;
my $pstoedit = "" ;
my $pdftops = "" ;
my $acroread = "" ;
my $wereondos = ($Config{'osname'} =~ /dos|mswin/io) ;
# Unix only: assume that "gs" in the path. We could also
# use $ghostscipt = system "which gs" but this would require
# that which is installedd on the system.
sub checkenv
{ my ($var, $env) = @_ ;
if ($var)
{ return $var }
elsif ($ENV{$env})
{ return $ENV{$env} }
else
{ return $var } }
$ghostscript = checkenv ($ghostscript, "GS_PROG" ) ;
$ghostscript = checkenv ($ghostscript, "GS" ) ;
$pstoedit = checkenv ($pstoedit , "PSTOEDIT") ;
$pdftops = checkenv ($pdftops , "PDFTOPS" ) ;
$acroread = checkenv ($acroread , "ACROREAD") ;
sub setenv
{ my ($var, $unix, $win) = @_ ;
if ($var)
{ return $var }
elsif ($wereondos)
{ return $win }
else
{ return $unix } }
$ghostscript = setenv($ghostscript, "gs" , "gswin32c") ;
$pstoedit = setenv($pstoedit , "pstoedit", "pstoedit") ;
$pdftops = setenv($pdftops , "pdftops" , "pdftops" ) ;
$acroread = setenv($acroread , "acroread", "" ) ;
# Force a method if unknown.
unless ($pmethod||$amethod||$gmethod)
{ if ($wereondos) { $pmethod = 1 } else { $amethod = 1 } }
# Set the error redirection used under Unix:
# stderr -> stdout
my $logredirection = '>>' ;
# This unfortunally doesn't work with the ksh and simple sh
#
# if (!$wereondos)
# { $logredirection = '2>&1 >>' ; # Bash
# $logredirection = '>>&' ; # tcsh, Bash
# default $logredirection. }
# Some TeX Code Snippets.
my $macros = '
% auxiliary macros
\input supp-mis.tex
\def\startTEXpage[scale=#1]%
{\output{}
\batchmode
\pdfoutput=1
\pdfcompresslevel=9
\hoffset=-1in
\voffset=\hoffset
\scratchcounter=#1
\divide\scratchcounter1000
\edef\TEXscale{\the\scratchcounter\space}
\forgetall
\setbox0=\vbox\bgroup}
\def\stopTEXpage
{\egroup
\dimen0=\ht0 \advance\dimen0 \dp0
\setbox2=\vbox to 10\dimen0
{\pdfliteral{\TEXscale 0 0 \TEXscale 0 0 cm}
\copy0
\pdfliteral{1 0 0 1 0 0 cm}
\vfill}
\wd2=10\wd0
\pdfpageheight=\ht2
\pdfpagewidth=\wd2
\ScaledPointsToBigPoints{\number\pdfpageheight}\pdfcropheight
\ScaledPointsToBigPoints{\number\pdfpagewidth }\pdfcropwidth
\expanded{\pdfpageattr{/CropBox [0 0 \pdfcropwidth \space \pdfcropheight]}}
\shipout\hbox{\box2}}
% end of auxiliary macros' ;
sub report
{ return if $silent ;
my $str = shift ;
if ($str =~ /(.*?)\s+([\:\/])\s+(.*)/o)
{ if ($1 eq "") { $str = " " } else { $str = $2 }
print sprintf("%22s $str %s\n",$1,$3) } }
sub error
{ report("processing aborted : " . shift) ;
exit }
sub process
{ report("generating : " . shift) }
sub banner
{ return if $silent ;
print "\n" ;
report ("MakeMPY 1.1 - MetaFun / PRAGMA ADE 2000-2004") ;
print "\n" }
my $metfile = "" ; # main metapost file
my $mpofile = "" ; # metapost text specifiation file (provided)
my $mpyfile = "" ; # metapost text picture file (generated)
my $texfile = "" ; # temporary tex file
my $pdffile = "" ; # temporary pdf file
my $tmpfile = "" ; # temporary metapost file
my $posfile = "" ; # temporary postscript file
my $logfile = "" ; # temporary log file
my $errfile = "" ; # final log file (with suffix log)
sub show_help_info
{ banner ;
report ("--help : this message" ) ;
report ("--noclean : don't remove temporary files" ) ;
report ("--force : force processing (ignore checksum)" ) ;
report ("--silent : don't show messages" ) ;
print "\n" ;
report ("--pdftops : use pdftops (xpdf) pdf->ps") ;
report ("--ghostscript : use ghostscript (gs) for pdf->ps") ;
print "\n" ;
report ("input file : metapost file with graphics") ;
report ("programs needed : texexec and english context") ;
report (" : pdftops from the xpdf suite, or") ; # page size buggy
report (" : pdf2ps and ghostscript, and") ;
report (" : pstoedit and ghostscript") ;
report ("output file : metapost file with pictures") ;
exit }
sub check_input_file
{ my $file = $ARGV[0] ;
if ((!defined($file))||($file eq ""))
{ banner ; error("no filename given") }
else
{ $file =~ s/\.mp.*$//o ;
$metfile = "$file.mp" ;
$mpofile = "$file.mpo" ;
$mpyfile = "$file.mpy" ;
$logfile = "$file.log" ;
$texfile = "mpy-$file.tex" ;
$pdffile = "mpy-$file.pdf" ;
$posfile = "mpy-$file.pos" ;
$tmpfile = "mpy-$file.tmp" ;
$errfile = "mpy-$file.log" ;
if (! -f $metfile)
{ banner ; error("$metfile is empty") }
elsif (-s $mpofile < 32)
{ unlink $mpofile ; # may exist with zero length
unlink $mpyfile ; # get rid of left overs
exit }
else
{ banner ; report("processing file : $mpofile") } } }
sub verify_check_sum # checksum calculation from perl documentation
{ return unless (open (MPO,"$mpofile")) ;
$mpochecksum = do { local $/ ; unpack("%32C*",<MPO>) % 65535 } ;
close (MPO) ;
return unless open (MPY,"$mpyfile") ;
my $str = <MPY> ; chomp $str ;
close (MPY) ;
if ($str =~ /^\%\s*mpochecksum\s*\:\s*(\d+)/o)
{ if ($mpochecksum eq $1)
{ report("mpo checksum : $mpochecksum / unchanged") ;
exit unless $force }
else
{ report("mpo checksum : $mpochecksum / changed") } } }
sub cleanup_files
{ my @files = <mpy-*.*> ;
foreach (@files) { unless (/\.log/o) { unlink $_ } } }
sub construct_tex_file
{ my $n = 0 ;
unless (open (MPO, "<$mpofile"))
{ error("unable to open $mpofile") }
unless (open (TEX, ">$texfile"))
{ error("unable to open $texfile") }
my $textext = "" ;
while (<MPO>)
{ s/\s*$//mois ;
if (/\%\s*format=(\w+)/)
{ $format = $1 }
else # if (!/^\%/)
{ if (/startTEXpage/o)
{ ++$n ;
$textext .= "$start{$format}\n" ;
$start{$format} = "" }
$textext .= "$_\n" } }
unless (defined($tex{$format})) { $format = "plain" }
if ($format eq "context") { $macros = "" }
# print TEX "$start{$format}\n$macros\n$textext\n$stop{$format}\n" ;
print TEX "$start{$format}\n\n" if $start{$format} ;
print TEX "$macros\n" if $macros ;
print TEX "$textext\n" if $textext ;
print TEX "$stop{$format}\n" if $stop{$format} ;
close (MPO) ;
close (TEX) ;
report("tex format : $format") ;
report("requested texts : $n") }
sub construct_mpy_file
{ unless (open (TMP, "<$tmpfile"))
{ error("unable to open $tmpfile file") }
unless (open (MPY, ">$mpyfile"))
{ error("unable to open $mpyfile file") }
print MPY "% mpochecksum : $mpochecksum\n" ;
my $copying = my $n = 0 ;
while (<TMP>) # a simple sub is faster
{ if (s/beginfig/begingraphictextfig/o)
{ print MPY $_ ; $copying = 1 ; ++$n }
elsif (s/endfig/endgraphictextfig/o)
{ print MPY $_ ; $copying = 0 }
elsif ($copying)
{ print MPY $_ } }
close (TMP) ;
close (MPY) ;
report("processed texts : $n") ;
report("produced file : $mpyfile") }
sub run
{ my ($resultfile, $program,$arguments) = @_ ;
my $result = system("$program $arguments $logredirection $logfile") ;
unless (-f $resultfile) { error("invalid `$program' run") } }
sub make_pdf_pages
{ process ("pdf file") ;
run ($pdffile, "$tex{$format}", "$texfile") }
sub make_mp_figures
{ process ("postscript file") ;
if ($pmethod) { run($posfile, "$pdftops",
"-paper match $pdffile $posfile") }
if ($gmethod) { run($posfile, "$ghostscript",
"-q -sOutputFile=$posfile -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pswrite $pdffile") }
if ($amethod) { run($posfile, "$acroread",
"-toPostScript -pairs $pdffile $posfile") } }
sub make_mp_pictures_ps
{ process ("metapost file") ;
run ($tmpfile, "$pstoedit", "-ssp -dt -f mpost $posfile $tmpfile") }
sub make_mp_pictures_pdf
{ process ("metapost file") ;
run ($tmpfile, "$pstoedit", "-ssp -dt -f mpost $pdffile $tmpfile") }
if ($help) { show_help_info }
check_input_file ;
verify_check_sum ;
cleanup_files ;
construct_tex_file ;
make_pdf_pages ;
if (1)
{ make_mp_pictures_pdf ; }
else
{ make_mp_figures ;
make_mp_pictures_ps ; }
construct_mpy_file ; # less save : rename $tmpfile, $mpyfile ;
unless ($noclean) { cleanup_files }
# a simple test file (needs context)
#
# % output=pdftex
#
# \starttext
#
# \startMPpage
# graphictext
# "\bf MAKE"
# scaled 8
# zscaled (1,2)
# withdrawcolor \MPcolor{blue}
# withfillcolor \MPcolor{gray}
# withpen pencircle scaled 5pt ;
# \stopMPpage
#
# \stoptext
|