summaryrefslogtreecommitdiff
path: root/support/autolatex/translators/plot2pdf+tex.transdef
blob: 8b464e57b816b98ede80313b9d1d46f0b1d4fca7 (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
# autolatex - plot2pdf+tex.transdef
# Copyright (C) 1998-2013  Stephane Galland <galland@arakhne.org>
#
# 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; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#--------------------------------------------
# See template.txt for details on the fields
#--------------------------------------------

INPUT_EXTENSIONS = .plott .plot_tex .plottex .plot+tex .tex.plot +tex.plot .gnut .gnu_tex .gnutex .gnu+tex .tex.gnu +tex.gnu

OUTPUT_EXTENSIONS for pdf = .pdf .pdftex_t
OUTPUT_EXTENSIONS for eps = .eps .pstex_t

TRANSLATOR_PERL_DEPENDENCIES = Cwd AutoLaTeX::TeX::TeXParser

TRANSLATOR_FUNCTION =<<EOL {
	my $curdir = getcwd();
	my $plotdir = dirname("$in");

	# Filenames
	my $plotFile = File::Spec->catfile("$plotdir", basename("$in", @inexts).'__xxxx_autolatex.plot');
	my $texFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).'.tex');
	my $psFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).'.ps');
	my $ptextFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).
				($ispdfmode ? '.pdftex_t' : '.pstex_t'));

	# Generate the PLOT file that is able to generate the (PS/PDF)TEX
	local *IN;
	local *OUT;
	open(*IN, "<$in") or printErr("$in: $!");
	open(*OUT, ">$plotFile") or printErr("$plotFile: $!");
	print OUT "set terminal pstex color auxfile;\n";
	print OUT "set output \"".addSlashes($texFile)."\";\n\n";
	while (my $line = <IN>) {
		$line =~ s/set\s+term(?:inal)?.*?[\n;]//gs;
		print OUT $line;
	}
	print OUT "unset output;\n";
	print OUT "quit;\n";
	close(*OUT);
	close(*IN);

	# Invoke Gnuplot
	chdir($plotdir) or printErr("$plotdir: $!");
	runCommandOrFail('gnuplot', "$plotFile");
	chdir($curdir) or printErr("$curdir: $!");
	unlink($plotFile);

	if ($ispdfmode) {
		# Clean the PDFTEX_T file by removing the special macros, and
		# adding the graphic inclusion at the right place
		my $content = readFileLines("$texFile");
		my $listener = {
			'content' => '',
			'expandMacro' => sub {
				my $listener = shift;
				my $parser = shift;
				my $macro = shift;
				if ($macro eq '\\special' ) {
					if ($_[0] && $_[0]->{'text'} && $_[0]->{'text'} =~ /\Qpsfile=\E/s) {
						return "\\put(0,0){\\includegraphics{$out}}";
					}
		# The first character after \special is consumed, reinject it if it is a brace.
					if ($_[0] && $_[0]->{'text'} && $_[0]->{'text'} eq '}') {
						return "$macro}";
					}
					return '';
				}
				return undef;
			},
			'outputString' => sub {
				my $listener = shift;
				my $parser = shift;
				my $str = shift;
				$listener->{'content'} .= $str;			
			},
		};

		my $parser = AutoLaTeX::TeX::TeXParser->new("$texFile", $listener);
		$parser->addTextModeMacro('special','!{}');
		$parser->addMathModeMacro('special','!{}');
		$parser->parse( $content );
		open(*OUT, ">$ptextFile") or printErr("$ptextFile: $!");
		print OUT $listener->{'content'};
		close(*OUT);
		unlink("$texFile");
	}
	else {
		move("$texFile", "$ptextFile") or printErr("$texFile -> $ptextFile: $!");
	}

	# Generate the proper figure
	if ($isepsmode) {
		move("$psFile", "$out") or printErr("$psFile -> $out: $!");
	}
	else {
		my $transresult = runTranslator('eps2pdf', "$psFile", "$out");
		unlink($psFile);
		$transresult;
	}
}
EOL

FILES_TO_CLEAN = $out.pdftex_t $out.pstex_t *__xxxx_autolatex.plot $out/