summaryrefslogtreecommitdiff
path: root/graphics/figfrag/figfrag
blob: f8d4ca6dc0188aa7c87cee22a1c040f2e61a4947 (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
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
#!/usr/bin/perl

# figfrag -- generate EPS from FIG files (generated with xfig)
# Copyright (C) 1999,  Maarten Ditzel
#
# 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.

use strict;

use Getopt::Std;
use File::Path;
use Cwd;

my $program = "figfrag";
my $version = "2.5";

my $tmpdir = "/tmp/.$program-$$";
my $calldir = cwd();

my $fig = "$program.fig";
my $tex = "$program.tex";
my $dvi = "$program.dvi";
my $eps1 = "$program.eps1";
my $eps2 = "$program.eps2";

my $quiet = 0;
my $keep = 0;
my $defaultcolor = "black";
my $font;

my ($include, $infile, $outfile);

my $texheader = <<EOF;
%% AUTOMATICALLY GENERATED WITH $program $version

\\documentclass[a4paper,10pt]{article}

\\usepackage{epsfig}
\\usepackage{psfrag}
\\usepackage{color}
\\usepackage{calc}
\\usepackage{ifthen}

\\pagestyle{empty}

\\begin{document}

\\newbox{\\fig}
\\newlength{\\figwidth}
\\newlength{\\figheight}
\\newlength{\\widthratio}
\\newlength{\\heightratio}
EOF

my $texfooter = <<EOF;
\\settowidth{\\figwidth}{\\usebox{\\fig}}
\\settoheight{\\figheight}{\\usebox{\\fig}}
\\setlength{\\widthratio}{1pt*\\ratio{\\textwidth}{\\figwidth}}
\\setlength{\\heightratio}{1pt*\\ratio{\\textheight}{\\figheight}}

\\ifthenelse{\\lengthtest{\\widthratio < \\heightratio}}{%%
\\resizebox{.95\\textwidth}{!}{\\usebox{\\fig}}}{%%
\\resizebox{!}{.95\\textheight}{\\usebox{\\fig}}}

\\end{document}
EOF

sub debug {
  my ($msg) = @_;
  print STDERR "$msg\n" if (not $quiet);
}

sub print_info {
  print STDERR "$program $version\n";
  print STDERR "Maarten Ditzel, July 1999, revised February 2002\n";
  print STDERR "usage: $program [options] <file>\n";
  print STDERR 
      "options:\n",
      "  -h : this help\n",
      "  -V : print version\n",
      "  -k : keep intermediate files (debug purposes)\n",
      "  -q : quite (non-verbose)\n",
      "  -i <include_file> : include file with LaTeX commands\n",
      "  -o <output_file>  : output to file\n",
      "  -f <fontfamily>   : set default font\n",
      "  -c <color>        : set default color (default black)\n";
}

sub print_version {
  print STDERR "$program $version\n";
}

sub create_tmpdir {
  mkdir($tmpdir, 0775)  ||
    die "ERROR: could not make temporary directory.\n";
  chdir($tmpdir) ||
    die "ERROR: could not change to working directory.\n";
}

sub destroy_tmpdir {
  chdir $calldir;
  rmtree($tmpdir) || 
    die "ERROR: could not remove temporary directory.\n";
}

sub process_options {

  my %options;

  getopts('hVkqc:f:i:o:', \%options) ||
    die "ERROR: invalid arguments.\n";

  if (defined $options{h}) {
    &print_info;
    exit;
  }

  if (defined $options{V}) {
    &print_version;
    exit;
  }

  if (defined $options{q}) {
    $quiet = 1;
  }

  if (defined $options{f}) {
    $font = "$options{f}";
  }

  if (defined $options{c}) {
    $defaultcolor = "$options{c}";
  }

  if (defined $options{i}) {
    $include = "$options{i}";
  }
  
  if (defined $options{k}) {
    $keep = 1;
  }

  $infile = shift @ARGV ||
    die "ERROR: no input file specified\n";
  $infile = "$calldir/$infile";

  if (defined $options{o}) {
    $outfile = $options{o};
  }
  else {
    $outfile = $infile;
    $outfile =~ s/\.fig$/\.eps/;
  }
}

sub read_fig {
  # create fig and tex files
  open(FIG, ">$fig") || 
    die "ERROR: could not create $fig\n";
  open(TEX, ">$tex") || 
    die "ERROR: could not create $tex\n";

  debug("Generating LaTeX file...");

  print TEX $texheader;

  # set custom font
  if ($font) {
    print TEX "\\renewcommand{\\familydefault}{$font}\n";
    print TEX "\\normalfont\n";
  }

  # include file
  open(INC, "<$calldir/$include") ||
      die "ERROR: could not open $include\n";
  while(<INC>) {
    print TEX;
  }
  close(INC);

  # start figure box
  print TEX "\\savebox{\\fig}{\n";

  # variables
  my ($mark, $ltx, $marker, $line, $opt, 
      $fontscale, $fontcolor, $fontstyle);

  # alignment strings
  my @align = ( "[Bl][Bl]", "[Bc][Bc]", "[Br][Br]" );
  my @color = ( $defaultcolor, 
		"black", 
		"blue", 
		"green", 
		"cyan",
		"red",
		"magenta",
		"yellow",
		"white" );
  my @style = ( "",              # default font (no change)
		"\\rmfamily",
		"\\bfseries",
		"\\itshape",
		"\\sffamily",
		"\\ttfamily" );

  $mark = 0;

  open(INFILE, "<$infile") || 
      die "ERROR: could not open $infile\n";
  
  while ($line = <INFILE>) {
    
    my @fields = split / /, $line, 14;

    # process only text fields with non Postscript fonts
    if ( $fields[0] == 4 && ! ( $fields[8] & 0x04 ) ) { 

      # reset special flag
      $fields[8] = 0;

      # create unique marker
      $marker = sprintf "mark-%04d", $mark++;

      # get fonts scale factor
      $fontscale = $fields[6]/10.0;

      # get the font color
      if ( $fields[2] > 7 ) {
	print STDERR "color $fields[2] not supported (>7)";
	$fields[2] = -1; # default color
      }

      $fontcolor = "\\color\{$color[$fields[2]+1]\}";

      # get the font style
      $fontstyle = $style[$fields[5]];

      # get the latex command
      $ltx = $fields[13];
      $ltx =~ s/\\001\n$//;

      # replace double backslashes by single
      $ltx =~ s/\\\\/\\/g;

      # replace special characters
      $ltx =~ s/&/\\&/g;

      # change math mode from textstyle to displaystyle
      $ltx =~ s/\$(.*)\$/\$\\displaystyle $1\$/g;

      # replace \int and \sum by \int\limits and \sum\limits
      $ltx =~ s/\\int/\\int\\limits/g;
      $ltx =~ s/\\sum/\\sum\\limits/g;

      $ltx = "\\resizebox{!}{$fontscale\\height}{$fontstyle$fontcolor $ltx}";

      # adjust alignment
      $opt = $align[$fields[1]];

      # generate psfrag command
      print TEX "  \\psfrag{$marker}$opt\{$ltx}\n";

      $fields[13] = $marker;
      print FIG "@fields\\001\n";
    }
    else {
      print FIG $line; # @fields;
    }
  }

  close INFILE;

  # close figure box
  print TEX "  \\epsfig{file=$eps1}\n";
  print TEX "}\n";

  print TEX $texfooter;

  close FIG;
  close TEX;

}

sub convert_fig {

  debug("Converting figure to EPS...");
  system "fig2dev -L eps $fig $eps1 > /dev/null" ||
    die "ERROR: could not execute fig2dev\n";

  debug("Replacing tags with LaTeX commands...");
  system "latex --interaction nonstopmode $tex > /dev/null" ||
    die "ERROR: could not execute latex\n";

  debug("Converting DVI to EPS...");
  system "\\dvips -E -o $eps2 $dvi 2> /dev/null" ||
    die "ERROR: could not execute dvips\n";

  debug("Adjusting bounding box...");
  system "epscrop $eps2 > $outfile" ||
    die "ERROR: could not execute epscrop\n";

}


&process_options;
&create_tmpdir;
&read_fig;
&convert_fig;
if ( $keep ) {
  debug("keeping intermediate files in $tmpdir\n");
}
else {
  &destroy_tmpdir;
}