summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/fragmaster/fragmaster.pl
blob: efb4c9aa8921b55543aa572b40d7f07f007f3268 (plain)
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
#!/usr/bin/perl -w

######################################################################
# $Id: fragmaster.pl,v 1.3 2006/09/26 08:59:30 tvogel Exp $
#
# fragmaster.pl
# creates EPS and PDF graphics from source EPS and control files
# with \psfrag commands
#
# Copyright (C) 2004 Tilman Vogel <tilman vogel web de> (dot at dot)
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# IMPORTANT: ALLOW DVIPS TO MAKE _PORTRAIT_ PS WITH WIDTH > HEIGHT
# BY ADDING
#
# @ custom 0pt 0pt
#
# TO YOUR /usr/share/texmf/dvips/config/config.pdf
# IF THIS ENTRY IS MISSING, DVIPS WILL GUESS ORIENTATION FROM
# WIDTH / HEIGHT RATIO. THIS STILL CAN HAPPEN IN CASE YOUR INPUT EPS
# MATCHES A STANDARD PAPER SIZE!
#
# Source files:
#    <graphics>_fm.eps
#       a source EPS file
#    <graphics>_fm
#       a control file containing \psfrag commands and optionally
#       special comments:
#         % fmclass: <class>
#           use <class> instead of "article"
#         % fmclassopt: <options>
#           use <options> as class options instead of "12pt"
#         % head:
#         % <texcode>
#         % end head
#           causes <texcode> to be put into the preamble
#         % fmopt: <option>
#           causes <option> to be given to \includegraphics as
#           optional parameter
#
# fragmaster.pl scans the current directory for files matching the
# pattern "*_fm" and "*_fm.eps" and converts them to the respective
# ".eps"- and ".pdf"-files if they are outdated.
#
# Credits:
#
# This script was inspired by a posting from
#   Karsten Roemke <k roemke gmx de> (dot at dot)
# with subject
#   "psfrag pdflatex, lange her"
# in de.comp.text.tex on 2003-11-11 05:25:44 PST.
#
# Karsten Roemke was inspired for his solution by postings from
# Thomas Wimmer.

chomp($cwd = `pwd`);

die "Current path contains whitespace. I am sorry, but LaTeX cannot handle this correctly, move somewhere else. Stopped"
    if $cwd =~ /\s/;

foreach $fm_file (<*_fm>) {
  ($base = $fm_file) =~ s/_fm$//;
  $source = "$fm_file.eps";

  if(! -f $source) {
    print "Cannot find EPS file '$source' for fragmaster file '$fm_file'! Skipped.\n";
    next;
  }

  
  $dest_eps = "$base.eps";
  $dest_pdf = "$base.pdf";
  
  $do_it = 0;

  $do_it = 1
    if ! -f $dest_eps;
  $do_it = 1
    if ! -f $dest_pdf;

  if(! $do_it) {
    $oldest_dest = -M $dest_eps;
    $oldest_dest = -M $dest_pdf
      if -M $dest_pdf > $oldest_dest;
  
    $youngest_source = -M $fm_file;
    $youngest_source = -M $source
      if -M $source < $youngest_source;
    $do_it = 1
      if $oldest_dest > $youngest_source;
  }
  
  if( $do_it ) {
    print "$fm_file, $source -> $dest_eps, $dest_pdf\n";

    open FMFILE, "<$fm_file"
      or die "Cannot read $fm_file!";

    $tempdir = `mktemp -d fm-$base.XXXXXX`
      or die "Cannot make temporary directory!";

    chomp($tempdir);

    print "using $tempdir/\n";

    open TEXFILE, ">$tempdir/fm.tex"
      or die "Cannot write LaTeX file!";

    $fmopt = "";
    @fmfile = ();
    @fmhead = ();
    $fmclass = "article";
    $fmclassopt = "12pt";
    while (<FMFILE>) {
      chomp;
      $fmopt = $1 if /fmopt:(.*)/;
      $fmclass = $1 if /fmclass:(.*)/;
      $fmclassopt = $1 if /fmclassopt:(.*)/;
      if (/head:/) {
	push @fmfile, "  $_%\n";
	while(<FMFILE>) {
	  chomp;
	  last if /end head/;
	  push @fmfile, "  $_%\n";
	  # Remove comment prefix
	  s/^[\s%]*//;
	  push @fmhead, "$_%\n";
	}
      }
            
      push @fmfile, "  $_%\n";
    }

    print TEXFILE <<"EOF";
\\documentclass[$fmclassopt]{$fmclass}
\\usepackage{graphicx,psfrag,color}
\\usepackage{german}
EOF
  print TEXFILE
    foreach(@fmhead);
  print TEXFILE <<'EOF';
\setlength{\topmargin}{-1in}
\setlength{\headheight}{0pt}
\setlength{\headsep}{0pt}
\setlength{\topskip}{0pt}
\setlength{\textheight}{\paperheight}
\setlength{\oddsidemargin}{-1in}
\setlength{\evensidemargin}{-1in}
\setlength{\textwidth}{\paperwidth}
\setlength{\parindent}{0pt}
\special{! TeXDict begin /landplus90{true}store end }
%\special{! statusdict /setpage undef }
%\special{! statusdict /setpageparams undef }
\pagestyle{empty}
\newsavebox{\pict}
EOF
  print TEXFILE "\\graphicspath{{../}}\n";
  print TEXFILE <<'EOF';
\begin{document}
  \begin{lrbox}{\pict}%
EOF

  print TEXFILE
    foreach (@fmfile);

  print TEXFILE "  \\includegraphics[$fmopt]{$source}%\n";
  print TEXFILE <<'EOF';
  \end{lrbox}
  \special{papersize=\the\wd\pict,\the\ht\pict}
  \usebox{\pict}
\end{document}
EOF
    
    close TEXFILE;

    chdir($tempdir)
      or die "Cannot chdir to $tempdir!";

    system("latex fm.tex") / 256 == 0
      or die "Cannot latex fm.tex!";

    # Using -E here, causes dvips to detect
    # the psfrag phantom stuff and to set the BoundingBox wrong
    system("dvips -E -P pdf fm.dvi -o fm.ps") / 256 == 0
      or die "Cannot dvips!";
    
    chdir("..")
      or die "Cannot chdir back up!";

    open PS, "<$tempdir/fm.ps"
      or die "Cannot read fm.ps!";

    open EPS, ">$dest_eps"
      or die "Cannot write $dest_eps!";

    # Correct the bounding box by setting the left margin to 0
    # top margin to top of letterpaper!
    # (I hope that is general enough...)
    $saw_bounding_box = 0;
    while(<PS>) {
      if(! $saw_bounding_box) {
	# if(s/^\%\%BoundingBox:\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/\%\%BoundingBox: 0 $2 $3 $4/) {
	if(s/^\%\%BoundingBox:\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/\%\%BoundingBox: 0 $2 $3 792/) {
	  $saw_bounding_box = 1;
	}
      }
      print EPS;
    }
    
    # Not using -E above causes
    # papersizes to be included into the PS
    # Strip off the specifications.
    # Otherwise gv doesn't show the BBox
    # and epstopdf won't detect the correct
    # PDF media size!
    
    #     while(<PS>) {
    #       s/^%!PS-Adobe.*/%!PS-Adobe-3.0 EPSF-3.0/;
    
    #       next if /^\%\%DocumentPaperSizes:/;
    #       if(/^\%\%BeginPaperSize:/) {
    # 	while(<PS>) {
    # 	  last if /^\%\%EndPaperSize/;
    # 	}
    # 	next;
    #       }
    #       s/statusdict \/setpage known/false/;
    #       s/statusdict \/setpageparams known/false/;
    #       print EPS;
    #     }

    close EPS;
    close PS;

    system("epstopdf $dest_eps --outfile=$dest_pdf") / 256 == 0
      or die "Cannot epstopdf!";

    system("rm -rf $tempdir") / 256 == 0
      or die "Cannot remove $tempdir!";

    close FMFILE;

    
    
  }
}