1 ## Copyright (C) 1999-2012 Daniel Heiserer
2 ## Copyright (C) 2001 Laurent Mazet
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} __gnuplot_print__ (@var{@dots{}})
22 ## Undocumented internal function.
25 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
28 function opts = __gnuplot_print__ (opts)
30 dos_shell = (ispc () && ! isunix ());
32 if (isempty (opts.fontsize))
33 ## If no fontsize, determine the nominal axes fontsize.
34 defaultfontsize = get (0, "defaultaxesfontsize");
35 axesfontsize = get (findobj (opts.figure, "type", "axes"), "fontsize");
36 if (iscell (axesfontsize))
37 axesfontsize = round (median (cell2mat (axesfontsize)));
39 if (isempty (axesfontsize))
40 opts.fontsize = defaultfontsize;
42 opts.fontsize = axesfontsize;
45 ## The axes-label and tick-label spacing is determined by
46 ## the font spec given in "set terminal ..."
47 gp_opts = font_spec (opts);
51 switch (lower (opts.devopt))
52 case {"eps", "eps2", "epsc", "epsc2"}
53 if (any (strcmp (opts.devopt, {"eps", "epsc"})))
54 gp_opts = sprintf ("%s level1", gp_opts);
56 if (opts.tight_flag || ! isempty (opts.preview))
57 tmp_file = strcat (tmpnam (), ".eps");
58 eps_drawnow (opts, tmp_file, gp_opts);
60 cleanup = sprintf (" & del %s", strrep (tmp_file, '/', '\'));
62 cleanup = sprintf (" ; rm %s", tmp_file);
64 pipeline = {sprintf("%s %s",
65 opts.epstool_cmd (opts, tmp_file, opts.name),
68 eps_drawnow (opts, opts.name, gp_opts);
70 case {"epslatex", "pslatex", "pstex", "epslatexstandalone"}
71 dot = find (opts.name == ".", 1, "last");
72 n = find (opts.devopt == "l", 1);
73 suffix = opts.devopt(1:n-1);
75 if (any (strcmpi (opts.name(dot:end), {strcat(".", suffix), ".tex", "."})))
76 name = opts.name(1:dot-1);
78 error ("print:invalid-suffix",
79 "invalid suffix `%s' for device `%s'.",
80 opts.name(dot:end), lower (opts.devopt));
83 if (strfind (opts.devopt, "standalone"))
84 term = sprintf ("%s ",
85 strrep (opts.devopt, "standalone", " standalone"));
87 term = sprintf ("%s ", opts.devopt);
89 if (__gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix"))
92 %% Gnuplot 4.0 wants a ".eps" suffix.
95 local_drawnow (sprintf ("%s %s", term, gp_opts),
96 strcat (name, ".", suffix), opts);
98 if (__gnuplot_has_terminal__ ("tikz"))
99 local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, opts);
101 error (sprintf ("print:no%soutput", opts.devopt),
102 "print.m: '%s' output is not available for gnuplot-%s",
103 upper (opts.devopt), __gnuplot_version__ ());
106 local_drawnow (sprintf ("svg dynamic %s", gp_opts), opts.name, opts);
107 case {"aifm", "corel", "eepic", "emf", "fig"}
108 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts);
109 case {"pdfcairo", "pngcairo"}
110 if (__gnuplot_has_terminal__ (opts.devopt))
111 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts);
113 error (sprintf ("print:no%soutput", opts.devopt),
114 "print.m: '%s' output is not available for gnuplot-%s",
115 upper (opts.devopt), __gnuplot_version__ ());
117 case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"}
118 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts);
119 case opts.ghostscript.device
120 gp_opts = font_spec (opts, "devopt", "eps");
121 opts.ghostscript.output = opts.name;
122 opts.ghostscript.source = strcat (tmpnam (), ".eps");
123 eps_drawnow (opts, opts.ghostscript.source, gp_opts);
124 [cmd_gs, cmd_cleanup] = __ghostscript__ (opts.ghostscript);
125 if (opts.send_to_printer || isempty (opts.name))
126 cmd_lpr = opts.lpr_cmd (opts);
127 cmd = sprintf ("%s | %s", cmd_gs, cmd_lpr);
129 cmd = sprintf ("%s", cmd_gs);
132 cmd = sprintf ("%s & del %s", cmd, strrep (opts.ghostscript.source, '/', '\'));
134 cmd = sprintf ("%s ; rm %s", cmd, opts.ghostscript.source);
136 if (! isempty (cmd_cleanup))
138 pipeline = {sprintf("%s & %s", cmd, cmd_cleanup)};
140 pipeline = {sprintf("%s ; %s", cmd, cmd_cleanup)};
146 error (sprintf ("print:no%soutput", opts.devopt),
147 "print.m: %s output is not available for the Gnuplot graphics toolkit",
148 upper (opts.devopt));
152 opts.pipeline = pipeline;
154 for n = 1:numel(pipeline)
156 fprintf ("gnuplot-pipeline: '%s'\n", pipeline{n});
158 [status, output] = system (pipeline{n});
160 fprintf ("%s\n%s\n%s\n",
161 "---------- output begin ----------",
163 "----------- output end -----------");
164 error ("gnuplot:failedpipe", "print: failed to print");
170 function eps_drawnow (opts, epsfile, gp_opts)
171 [h, fontsize] = get_figure_text_objs (opts);
174 set (h(n), "fontsize", 2 * fontsize{n});
176 local_drawnow (sprintf ("postscript eps %s", gp_opts), epsfile, opts);
177 unwind_protect_cleanup
179 set (h(n), "fontsize", fontsize{n});
184 function local_drawnow (term, file, opts)
185 if (opts.use_color < 0)
190 figure (opts.figure);
191 if (isempty (opts.debug_file) || ! opts.debug)
192 drawnow (term, file, mono);
194 drawnow (term, file, mono, opts.debug_file);
198 function f = font_spec (opts, varargin)
199 for n = 1:2:numel(varargin)
200 opts.(varargin{n}) = varargin{n+1};
205 if (! isempty (opts.font) && ! isempty (opts.fontsize))
206 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
207 elseif (! isempty (opts.font))
208 f = sprintf ("font ""%s""", opts.font);
209 elseif (! isempty (opts.fontsize))
210 f = sprintf ("%d", opts.fontsize);
212 case {"eps", "eps2", "epsc", "epsc2"}
213 ## Gnuplot renders fonts as half their specification, which
214 ## results in a tight spacing for the axes-labels and tick-labels.
215 ## Compensate for the half scale. This will produce the proper
216 ## spacing for the requested fontsize.
217 if (! isempty (opts.font) && ! isempty (opts.fontsize))
218 f = sprintf ("font ""%s,%d""", opts.font, 2 * opts.fontsize);
219 elseif (! isempty (opts.font))
220 f = sprintf ("font ""%s""", opts.font);
221 elseif (! isempty (opts.fontsize))
222 f = sprintf ("%d", 2 * opts.fontsize);
225 if (! isempty (opts.font) && ! isempty (opts.fontsize))
226 fontsize = round (opts.fontsize * 0.75);
227 f = sprintf ("fname ""%s"" fsize %d", opts.font, fontsize);
228 elseif (! isempty (opts.font))
229 f = sprintf ("fname ""%s""", opts.font);
230 elseif (! isempty (opts.fontsize))
231 fontsize = round (opts.fontsize * 0.75);
232 f = sprintf ("%s fsize %d", f, fontsize);
235 if (! isempty (opts.font) && ! isempty (opts.fontsize))
236 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
237 elseif (! isempty (opts.font))
238 f = sprintf ("font ""%s""", opts.font);
239 elseif (! isempty (opts.fontsize))
240 f = sprintf ("fsize %d", f, opts.fontsize);
242 case {"pdfcairo", "pngcairo"}
243 if (! isempty (opts.font))
244 f = sprintf ("font ""%s""", opts.font);
246 case {"epslatex", "epslatexstandalone"}
247 if (! isempty (opts.font) && ! isempty (opts.fontsize))
248 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
249 elseif (! isempty (opts.font))
250 f = sprintf ("font ""%s""", opts.font);
251 elseif (! isempty (opts.fontsize))
252 f = sprintf ("%d", opts.fontsize);
255 if (! isempty (opts.fontsize))
256 f = sprintf ("%d", opts.fontsize);
258 case {"gif", "jpeg", "png"}
259 if (! isempty (opts.font) && ! isempty (opts.fontsize))
260 f = sprintf ("font ""%s ,%d""", opts.font, opts.fontsize);
261 elseif (! isempty (opts.font))
262 f = sprintf ("font ""%s""", opts.font);
263 elseif (! isempty (opts.fontsize))
264 f = sprintf ("font ""%d""", opts.fontsize);
267 if (! isempty (opts.font) && ! isempty (opts.fontsize))
268 f = sprintf ("""%s"" %d", opts.font, opts.fontsize);
269 elseif (! isempty (opts.font))
270 f = sprintf ("""%s""", opts.font);
271 elseif (! isempty (opts.fontsize))
272 f = sprintf ("%d", opts.fontsize);
275 if (! isempty (opts.fontsize))
276 f = sprintf ("fsize %d", opts.fontsize);
278 case {"aifm", "corel"}
279 if (! isempty (opts.font) && ! isempty (opts.fontsize))
280 f = sprintf ("%s %d", opts.font, opts.fontsize);
281 elseif (! isempty (opts.font))
282 f = sprintf ("%s", opts.font);
283 elseif (! isempty (opts.fontsize))
284 f = sprintf ("%d", opts.fontsize);
287 if (! isempty (opts.font) && ! isempty (opts.fontsize))
288 f = sprintf ("font %s fontsize %d", opts.font, opts.fontsize);
289 elseif (! isempty (opts.font))
290 f = sprintf ("font %s", opts.font);
291 elseif (! isempty (opts.fontsize))
292 f = sprintf ("fontsize %d", opts.fontsize);
297 function [h, fontsize] = get_figure_text_objs (opts)
298 h = findall (opts.figure, "-property", "fontsize");
299 fontsize = get (h, "fontsize");
300 switch (numel (fontsize))
304 fontsize = {fontsize};