# autolatex - svg2pdf+layers+tex_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 = .svglt .svg_lt .svglayerstex .svgtexlayers .svg+layers+tex .svg+tex+layers .layers.tex.svg .tex.layers.svg +layers+tex.svg +tex+layers.svg .svgzlt .svgz_lt .svgzlayerstex .svgztexlayers .svgz+layers+tex .svgz+tex+layers .layers.tex.svgz .tex.layers.svgz +layers+tex.svgz +tex+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, $ext3, $opt);
	if ($ispdfmode) {
		$ext1 = '.pdftex_t';
		$ext2 = '.pdf';
		$ext3 = '.pdf_tex';
		$opt = '--export-pdf';
	}
	else {
		$ext1 = '.pstex_t';
		$ext2 = '.eps';
		$ext3 = '.ps_tex';
		$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'}) {
			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'};
					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");

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

					my $texFile = File::Spec->catfile(
								dirname($out),
								"$outputbasename$ext3");

					$imageinclusions .= "\\node<$overlay_spec> (X) {\\input{$texFile}};%\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_tex $out_*.ps_tex $out_*.pdf $out_*.eps

