]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__gnuplot_print__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __gnuplot_print__.m
1 ## Copyright (C) 1999-2012 Daniel Heiserer
2 ## Copyright (C) 2001 Laurent Mazet
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} __gnuplot_print__ (@var{@dots{}})
22 ## Undocumented internal function.
23 ## @end deftypefn
24
25 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
26 ## Adapted-By: jwe
27
28 function opts = __gnuplot_print__ (opts)
29
30   dos_shell = (ispc () && ! isunix ());
31
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)));
38     endif
39     if (isempty (axesfontsize))
40       opts.fontsize = defaultfontsize;
41     else
42       opts.fontsize = axesfontsize;
43     endif
44   endif
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);
48
49   pipeline = "";
50
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);
55     endif
56     if (opts.tight_flag || ! isempty (opts.preview))
57       tmp_file = strcat (tmpnam (), ".eps");
58       eps_drawnow (opts, tmp_file, gp_opts);
59       if (dos_shell)
60         cleanup = sprintf (" & del %s", strrep (tmp_file, '/', '\'));
61       else
62         cleanup = sprintf (" ; rm %s", tmp_file);
63       endif
64       pipeline = {sprintf("%s %s",
65                           opts.epstool_cmd (opts, tmp_file, opts.name),
66                           cleanup)};
67     else
68       eps_drawnow (opts, opts.name, gp_opts);
69     endif
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);
74     if (! isempty (dot))
75       if (any (strcmpi (opts.name(dot:end), {strcat(".", suffix), ".tex", "."})))
76         name = opts.name(1:dot-1);
77       else
78         error ("print:invalid-suffix", 
79                "invalid suffix `%s' for device `%s'.",
80                opts.name(dot:end), lower (opts.devopt));
81       endif
82     endif
83     if (strfind (opts.devopt, "standalone"))
84       term = sprintf ("%s ",
85                       strrep (opts.devopt, "standalone", " standalone"));
86     else
87       term = sprintf ("%s ", opts.devopt);
88     endif
89     if (__gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix"))
90       suffix = "tex";
91     else
92       %% Gnuplot 4.0 wants a ".eps" suffix.
93       suffix = "eps";
94     endif
95     local_drawnow (sprintf ("%s %s", term, gp_opts),
96                    strcat (name, ".", suffix), opts);
97   case "tikz"
98     if (__gnuplot_has_terminal__ ("tikz"))
99       local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, opts);
100     else
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__ ());
104     endif
105   case "svg"
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);
112     else
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__ ());
116     endif
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);
128     else
129       cmd = sprintf ("%s", cmd_gs);
130     endif
131     if (dos_shell)
132       cmd = sprintf ("%s & del %s", cmd, strrep (opts.ghostscript.source, '/', '\'));
133     else
134       cmd = sprintf ("%s ; rm %s", cmd, opts.ghostscript.source);
135     endif
136     if (! isempty (cmd_cleanup))
137       if (dos_shell)
138         pipeline = {sprintf("%s & %s", cmd, cmd_cleanup)};
139       else
140         pipeline = {sprintf("%s ; %s", cmd, cmd_cleanup)};
141       endif
142     else
143       pipeline = {cmd};
144     endif
145   otherwise
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));
149   endswitch
150
151
152   opts.pipeline = pipeline;
153
154   for n = 1:numel(pipeline)
155     if (opts.debug)
156       fprintf ("gnuplot-pipeline: '%s'\n", pipeline{n});
157     endif
158     [status, output] = system (pipeline{n});
159     if (status)
160       fprintf ("%s\n%s\n%s\n",
161                "---------- output begin ----------",
162                output,
163                "----------- output end -----------");
164       error ("gnuplot:failedpipe", "print: failed to print");
165     endif
166   endfor
167
168 endfunction
169
170 function eps_drawnow (opts, epsfile, gp_opts)
171   [h, fontsize] = get_figure_text_objs (opts);
172   unwind_protect
173     for n = 1:numel(h)
174       set (h(n), "fontsize", 2 * fontsize{n});
175     endfor
176     local_drawnow (sprintf ("postscript eps %s", gp_opts), epsfile, opts);
177   unwind_protect_cleanup
178     for n = 1:numel(h)
179       set (h(n), "fontsize", fontsize{n});
180     endfor
181   end_unwind_protect
182 endfunction
183
184 function local_drawnow (term, file, opts)
185   if (opts.use_color < 0)
186     mono = true;
187   else
188     mono = false;
189   endif
190   figure (opts.figure);
191   if (isempty (opts.debug_file) || ! opts.debug)
192     drawnow (term, file, mono);
193   else
194     drawnow (term, file, mono, opts.debug_file);
195   endif
196 endfunction
197
198 function f = font_spec (opts, varargin)
199   for n = 1:2:numel(varargin)
200     opts.(varargin{n}) = varargin{n+1};
201   endfor
202   f = "";
203   switch (opts.devopt)
204   case "cgm"
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);
211     endif
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);
223     endif
224   case "svg"
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);
233     endif
234   case "pdf"
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);
241     endif
242   case {"pdfcairo", "pngcairo"}
243     if (! isempty (opts.font))
244       f = sprintf ("font ""%s""", opts.font);
245     endif
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);
253     endif
254   case "pslatex"
255     if (! isempty (opts.fontsize))
256       f = sprintf ("%d", opts.fontsize);
257     endif
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);
265     endif
266   case "emf"
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);
273     endif
274   case "canvas"
275     if (! isempty (opts.fontsize))
276       f = sprintf ("fsize %d", opts.fontsize);
277     endif
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);
285     endif
286   case "fig"
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);
293     endif
294   endswitch
295 endfunction
296
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))
301   case 0
302     fontsize = {};
303   case 1
304     fontsize = {fontsize};
305   endswitch
306 endfunction