summaryrefslogtreecommitdiff
path: root/support/autolatex/translators/asml2pdf.transdef
blob: c8b1f68b5e0dd2f715c7e5c942b1bd777a1e3be3 (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
# autolatex - asml2pdf.transdef
# Copyright (C) 2017  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 = .asml

OUTPUT_EXTENSIONS for pdf = .pdf
OUTPUT_EXTENSIONS for eps = .eps

TRANSLATOR_FUNCTION =<<EOL {
	my $INITIAL_HEAP_SIZE = "64m";
	my $MAXIMUM_HEAP_SIZE = "1024m";
	my @JAVA_OPTS = ("-Xms$INITIAL_HEAP_SIZE", "-Xmx$MAXIMUM_HEAP_SIZE");

	my @libraries = (
		'astah-sys.jar',
	);

	# Detect the Astah installation
	if ( ! $ENV{"ASTAH_SYSML_HOME"} ) {
		my @tmp = ('astah-sysml');
		my $tmp = undef;
		for(my $i=0; $i<@tmp && !$tmp; $i++) {
			my $bin = $tmp[$i];
			$bin = which("$bin");
			if ( $bin && -x "$bin" ) {
				$bin = readlink_osindep("$bin");
				$tmp = dirname("$bin");
			}
		}
		if ( ! $tmp ) {
			@tmp = ('/usr/lib/astah_sysml',
				);
			for(my $i=0; $i<@tmp && !$tmp; $i++) {
				my $dir = $tmp[$i];
				if ( -d "$dir" ) {
					$tmp = $dir;
				}
			}
			if ( ! $tmp ) {
				printErr("Unable to find the installation directory of Astah SysML. Please define the ASTAH_SYSML_HOME environment variable.");
			}
		}
		$ENV{"ASTAH_SYSML_HOME"} = $tmp;
	}

	# Detect the Java library of astah
	my $jarfile;
	for(my $i=0; $i<@libraries && !$jarfile; $i++) {
		my $library = $libraries[$i];
		if ( -f File::Spec->catfile("$ENV{ASTAH_SYSML_HOME}", "$library" ) ) {
			$jarfile = $library;
		}
	}
	if ( ! $jarfile ) {
		printErr("Unable to find the java library of Astah SysML. Please define the ASTAH_SYSML_HOME environment variable.");
	}

	# Prepare the generation from Astah
	my $outputDir = File::Spec->rel2abs(dirname("$in"));
	my $shortBasename = basename("$in",@inexts);
	my $astahOutputDir = File::Spec->catfile("$outputDir","$shortBasename");
	if ( -d "$astahOutputDir" ) {
		remove_tree("$astahOutputDir") or printErr("$astahOutputDir: $!");
	}

	# Generation of the SVG from Astah
	runCommandOrFail(
		'java',
		@JAVA_OPTS,
		'-cp',
		File::Spec->catfile("$ENV{ASTAH_SYSML_HOME}","$jarfile"),
		'com.change_vision.jude.cmdline.JudeCommandRunner',
		'-image', 'all',
		'-f', "$in",
		'-t', 'svg',
		'-o', "$outputDir");

	# Generation of the PDF/PS from the SVG
	my @generatedFiles = ();
	local *OUTDIR;
	opendir(*OUTDIR, "$astahOutputDir") or printErr("$astahOutputDir: $!");
	while (my $fn = readdir(*OUTDIR)) {
		if ($fn ne File::Spec->updir() && $fn ne File::Spec->curdir()
				&& $fn =~ /\.svg$/s) {
			my $svgFile = File::Spec->catfile("$astahOutputDir", "$fn");
			push @generatedFiles, "$svgFile";

			# Remove the background
			my $svgcontent = readFileLines("$svgFile");
			$svgcontent =~ s/\Q<rect\E\s+.*?\/?>//s;
			writeFileLines("$svgFile", "$svgcontent");
		}
	}
	closedir(*OUTDIR);

	my $transresult = 0;

	# Move the generated files
	if (@generatedFiles>1) {
		my $template = File::Spec->catfile(
					dirname($out),
					basename($out,$outext)).'_';
		my $transresult = 0;
		foreach my $file (@generatedFiles) {
			my $bn = basename($file,'.svg');
			$bn =~ s/\s+/_/sg;
			$bn = "$template$bn$outext";
			my $result = runTranslator( 'svg2pdf', "$file", "$bn", 0);
			if ($result) {
				$transresult = 1;
			}
		}
		if (!$transresult) {
			printErr("No file generated.");
		} else {
			touch("$out");
		}
	}
	elsif (@generatedFiles) {
		my $file = shift @generatedFiles;
		runTranslator('svg2pdf', "$file", "$out");
	}
	else {
		printErr("No file generated.");
	}

	if ( -d "$astahOutputDir" ) {
		remove_tree("$astahOutputDir") or printErr("$astahOutputDir: $!");
	}

	1;
}
EOL

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