summaryrefslogtreecommitdiff
path: root/support/autolatex/translators/svg2pdf+layers_inkscape.transdef
blob: b18d19d7616bfaeeb40ef3a1dc4f222744b9b942 (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
# autolatex - svg2pdf+layers_inkscape.transdef
# Copyright (C) 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 = .svgl .svg_l .svglayers .svg+layers .layers.svg +layers.svg .svgzl .svgz_l .svgzlayers .svgz+layers .layers.svgz +layers.svgz

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

TRANSLATOR_PERL_DEPENDENCIES = XML::Parser Scalar::Util qw(blessed)

TRANSLATOR_FUNCTION =<<EOL {
	my ($ext1, $ext2, $opt);
	if ($ispdfmode) {
		$ext1 = '.pdftex_t';
		$ext2 = '.pdf';
		$opt = '--export-pdf';
	}
	else {
		$ext1 = '.pstex_t';
		$ext2 = '.eps';
		$opt = '--export-eps';
	}

	# Parse the SVG
	my $parser = XML::Parser->new(Style=>'Objects', Pkg=>'autolatex');
	my $tree = $parser->parsefile("$in");

	my $imageinclusions = '';
	my $layerIndex = 1;

	foreach my $rootElement (@$tree) {
		if ($rootElement->{'Kids'}) {
			my $width = $rootElement->{'width'} || 0;
			# Clamp the value to avoid LaTex failures
			if ($width<=0) {
				$width = 100;
			}
			elsif ($width>1000) {
				$width = 1000;
			}
			foreach my $kidElement (@{$rootElement->{'Kids'}}) {
				my $className = blessed($kidElement);
				if ($className eq 'autolatex::g'
				    && ($kidElement->{'style'}||'') ne "display:none") {
					my $id = $kidElement->{'id'};
					my $label = $kidElement->{'inkscape:label'};
					printDbgFor(3, formatText(_T("Found layer: id={}, label={}"), @{toUTF8($id, $label)}));
					my $overlay_spec = $layerIndex;
					if ($label && $label =~ /<\s*(.+)\s*>/s) {
						$overlay_spec = $1;
					}
					my $outputbasename = basename($out, $ext1)."_$id";
					my $figureFile = File::Spec->catfile(
								dirname($out),
								"$outputbasename$ext2");
					printDbgFor(4, formatText(_T("Overlay spec: <{}>"), @{toUTF8($overlay_spec)}));
					printDbgFor(4, formatText(_T("Output: {}"), $figureFile));

					runCommandOrFail(
						'inkscape',
						'--without-gui',
						'--export-id', "$id",
						'--export-id-only',
						'--export-area-page',
						$opt,
						"$figureFile",
						"--file=$in");

					$imageinclusions .= "\\node<$overlay_spec> (X) {\\includegraphics[width=${width}em]{$outputbasename}};%\n";
					$layerIndex++;
				}
			}
		}
	}

	if (!$imageinclusions) {
		printError(_T("No layer in the SVG file: {}"), $in);
	}

	local *TEXOUT;
	open(*TEXOUT, "> $out") or printErr("$out: $!");
	print TEXOUT "\%Overlays\n\\bgroup%\n\\begin{tikzpicture}%\n$imageinclusions\\end{tikzpicture}%\n\\egroup%";
	close(*TEXOUT);
	1;
}
EOL

FILES_TO_CLEAN = $out_*.pdf $out_*.eps