comparison scripts/plot/gnuplot_drawnow.m @ 10913:dd6b90f44ae5

Unify gnuplot printing with the fltk backend.
author Ben Abbott <bpabbott@mac.com>
date Fri, 27 Aug 2010 06:39:36 -0400
parents d1978e7364ad
children f5c863803e29
comparison
equal deleted inserted replaced
10912:9abc67b4bd4f 10913:dd6b90f44ae5
36 if (nargin >= 3 && nargin <= 5) 36 if (nargin >= 3 && nargin <= 5)
37 ## Produce various output formats, or redirect gnuplot stream to a 37 ## Produce various output formats, or redirect gnuplot stream to a
38 ## debug file. 38 ## debug file.
39 plot_stream = []; 39 plot_stream = [];
40 fid = []; 40 fid = [];
41 printing = ! output_to_screen (gnuplot_trim_term (term));
42 default_plot_stream = get (h, "__plot_stream__"); 41 default_plot_stream = get (h, "__plot_stream__");
43 unwind_protect 42 unwind_protect
44 plot_stream = __gnuplot_open_stream__ (2, h); 43 plot_stream = __gnuplot_open_stream__ (2, h);
45 if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS")) 44 if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS"))
46 available_terminals = __gnuplot_get_var__ (plot_stream, "GPVAL_TERMINALS"); 45 available_terminals = __gnuplot_get_var__ (plot_stream, "GPVAL_TERMINALS");
49 gnuplot_trim_term (term))); 48 gnuplot_trim_term (term)));
50 else 49 else
51 gnuplot_supports_term = true; 50 gnuplot_supports_term = true;
52 endif 51 endif
53 if (gnuplot_supports_term) 52 if (gnuplot_supports_term)
54 [enhanced, implicit_margin] = gnuplot_set_term (plot_stream (1), true, 53 enhanced = gnuplot_set_term (plot_stream (1), true, h, term, file);
55 h, term, file); 54 __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
56 __go_draw_figure__ (h, plot_stream(1), enhanced, mono, printing, implicit_margin);
57 if (nargin == 5) 55 if (nargin == 5)
58 fid = fopen (debug_file, "wb"); 56 fid = fopen (debug_file, "wb");
59 [enhanced, implicit_margin] = gnuplot_set_term (fid, true, h, term, file); 57 enhanced = gnuplot_set_term (fid, true, h, term, file);
60 __go_draw_figure__ (h, fid, enhanced, mono, printing, implicit_margin); 58 __go_draw_figure__ (h, fid, enhanced, mono);
61 endif 59 endif
62 else 60 else
63 error ("gnuplot_drawnow: the gnuplot terminal, \"%s\", is not available.", 61 error ("gnuplot_drawnow: the gnuplot terminal, \"%s\", is not available.",
64 gnuplot_trim_term (term)) 62 gnuplot_trim_term (term))
65 endif 63 endif
94 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, ... 92 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, ...
95 term, dumb_tmp_file); 93 term, dumb_tmp_file);
96 else 94 else
97 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, term); 95 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, term);
98 end 96 end
99 __go_draw_figure__ (h, plot_stream (1), enhanced, mono, 0); 97 __go_draw_figure__ (h, plot_stream (1), enhanced, mono);
100 fflush (plot_stream (1)); 98 fflush (plot_stream (1));
101 if (strcmp (term, "dumb")) 99 if (strcmp (term, "dumb"))
102 fid = -1; 100 fid = -1;
103 while (fid < 0) 101 while (fid < 0)
104 pause (0.1); 102 pause (0.1);
120 print_usage (); 118 print_usage ();
121 endif 119 endif
122 120
123 endfunction 121 endfunction
124 122
125 function implicit_margin = gnuplot_implicit_margin (term, opts_str) 123 function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file)
126 ## gnuplot has an implicit margin of 50pts for PS output.
127 if (strcmpi (term, "postscript"))
128 if (isempty (strfind (opts_str, " eps"))
129 && isempty (strfind (opts_str, "eps ")))
130 implicit_margin = 50/72;
131 else
132 ## When zero, the behavior of gnuplot changes.
133 implicit_margin = 1/72;
134 endif
135 else
136 implicit_margin = 0.0;
137 endif
138 endfunction
139
140 function [enhanced, implicit_margin] = gnuplot_set_term (plot_stream, new_stream, h, term, file)
141 ## Generate the gnuplot "set terminal <term> ..." command. 124 ## Generate the gnuplot "set terminal <term> ..." command.
142 ## When "term" originates from print.m, it may include other options. 125 ## When "term" originates from print.m, it may include other options.
143 if (nargin < 4) 126 if (nargin < 4)
144 ## This supports the gnuplot backend. 127 ## This supports the gnuplot backend.
145 term = gnuplot_default_term (); 128 term = gnuplot_default_term ();
146 opts_str = ""; 129 opts_str = "";
147 else 130 else
148 ## Get the one word terminal id and save the remaining as options to 131 ## Get the one word terminal id and save the remaining as options to
149 ## be passed on to gnuplot. The terminal may respect the backend. 132 ## be passed on to gnuplot. The terminal may respect the backend.
150 [term, opts_str] = gnuplot_trim_term (term); 133 [term, opts_str] = gnuplot_trim_term (term);
151 if (strcmpi (term, "pdf") && strcmpi (opts_str, "color")) 134 term = lower (term);
152 ## FIXME -- "color" for the pdf terminal produces a gnuplot error. 135 if (strcmpi (term, "lua"))
153 opts_str = ""; 136 ## Replace "lau tikz" with
154 endif 137 term = "tikz";
155 endif 138 opts_str = strrep (opts_str, "tikz", "");
156 139 endif
157 implicit_margin = gnuplot_implicit_margin (term, opts_str); 140 endif
158 141
159 enhanced = gnuplot_is_enhanced_term (term); 142 if (strfind (opts_str, "noenhanced"))
143 enhanced = false;
144 else
145 enhanced = gnuplot_is_enhanced_term (term);
146 endif
160 147
161 ## Set the terminal. 148 ## Set the terminal.
162 if (! isempty (term)) 149 if (! isempty (term))
163 150
164 if (enhanced) 151 if (enhanced)
184 title_str = fig.name; 171 title_str = fig.name;
185 endif 172 endif
186 if (! isempty (title_str)) 173 if (! isempty (title_str))
187 title_str = sprintf ("title \"%s\"", title_str); 174 title_str = sprintf ("title \"%s\"", title_str);
188 endif 175 endif
176 if (strcmp (term, "aqua"))
177 ## Adjust axes-label and tick-label spacing.
178 opts_str = sprintf ("%s font \"%s,%d\"", opts_str,
179 get (0, "defaultaxesfontname"),
180 get (0, "defaultaxesfontsize") / 1.5);
181 endif
189 else 182 else
190 title_str = ""; 183 title_str = "";
191 endif 184 endif
185
192 if (! (any (strfind (opts_str, " size ") > 0) 186 if (! (any (strfind (opts_str, " size ") > 0)
193 || any (strfind (opts_str, "size ") == 1))) 187 || any (strfind (opts_str, "size ") == 1)))
194 ## Convert position to units used by gnuplot. 188 ## Get figure size in pixels. Rely on listener to handle coversion.
195 if (output_to_screen (term)) 189 units = get (h, "units");
196 ## Get figure size in pixels. Rely on listener 190 unwind_protect
197 ## to handle coversion of position property. 191 set (h, "units", "pixels");
198 units = get (h, "units"); 192 position_in_pixels = get (h, "position");
199 unwind_protect 193 unwind_protect_cleanup
200 set (h, "units", "pixels"); 194 set (h, "units", units);
201 position_in_pixesl = get (h, "position"); 195 end_unwind_protect
202 unwind_protect_cleanup 196 gnuplot_pos = position_in_pixels(1:2);
203 set (h, "units", units); 197 gnuplot_size = position_in_pixels(3:4);
204 end_unwind_protect 198 if (! (output_to_screen (term)
205 gnuplot_pos = position_in_pixesl(1:2); 199 || any (strcmp (term, {"emf", "gif", "jpeg", "pbm", "png", ...
206 gnuplot_size = position_in_pixesl(3:4); 200 "pngcairo", "svg"}))))
207 else 201 ## Convert to inches
208 ## Get size of the printed plot in inches. Rely on listener 202 gnuplot_pos = gnuplot_pos / 72;
209 ## to handle coversion of papersize property. 203 gnuplot_size = gnuplot_size / 72;
210 paperunits = get (h, "paperunits");
211 unwind_protect
212 set (h, "paperunits", "inches");
213 gnuplot_size = get (h, "papersize");
214 unwind_protect_cleanup
215 set (h, "paperunits", paperunits);
216 end_unwind_protect
217 if (term_units_are_pixels (term))
218 ## Convert to inches using the property set by print().
219 gnuplot_size = gnuplot_size * get (h, "__pixels_per_inch__");
220 else
221 ## Implicit margins are in units of "inches"
222 gnuplot_size = gnuplot_size - implicit_margin;
223 endif
224 endif
225 [begin_match, end_match, te, match] = regexp (opts_str, "(\\s-r\\d+)|(^-r\\d+)");
226 if (! isempty (begin_match))
227 error ("gnuplot_drawnow: specifying resultion, '%s', not supported for terminal '%s'",
228 strtrim (match{1}), term)
229 endif 204 endif
230 if (all (gnuplot_size > 0)) 205 if (all (gnuplot_size > 0))
231 ## Set terminal size. 206 terminals_with_size = {"canvas", "emf", "epslatex", "fig", ...
232 terminals_with_size = {"emf", "gif", "jpeg", "latex", "pbm", ... 207 "gif", "jpeg", "latex", "pbm", "pdf", ...
233 "pdf", "png", "postscript", "svg", ... 208 "pdfcairo", "postscript", "png", "pngcairo", ...
234 "epslatex", "pstex", "pslatex", "tikz"}; 209 "pstex", "pslatex", "svg", "tikz"};
235 if (__gnuplot_has_feature__ ("x11_figure_position")) 210 if (__gnuplot_has_feature__ ("x11_figure_position"))
236 terminals_with_size{end+1} = "x11"; 211 terminals_with_size{end+1} = "x11";
237 endif 212 endif
238 if (__gnuplot_has_feature__ ("wxt_figure_size")) 213 if (__gnuplot_has_feature__ ("wxt_figure_size"))
239 terminals_with_size{end+1} = "wxt"; 214 terminals_with_size{end+1} = "wxt";
240 endif 215 endif
241 if (any (strncmpi (term, terminals_with_size, 3))) 216 switch term
242 if (term_units_are_pixels (term)) 217 case terminals_with_size
243 size_str = sprintf ("size %d,%d", gnuplot_size); 218 size_str = sprintf ("size %g,%g", gnuplot_size);
244 elseif (strcmp (term, "tikz")) 219 case "tikz"
245 size_str = sprintf ("size %.15gin,%.15gin", gnuplot_size); 220 size_str = sprintf ("size %gin,%gin", gnuplot_size);
246 else 221 case "dumb"
247 size_str = sprintf ("size %.15g,%.15g", gnuplot_size);
248 endif
249 if (strncmpi (term, "X11", 3) && __gnuplot_has_feature__ ("x11_figure_position"))
250 ## X11 allows the window to be positioned as well.
251 units = get (0, "units");
252 unwind_protect
253 set (0, "units", "pixels");
254 screen_size = get (0, "screensize")(3:4);
255 unwind_protect_cleanup
256 set (0, "units", units);
257 end_unwind_protect
258 if (all (screen_size > 0))
259 ## For X11, set the figure positon as well as the size
260 ## gnuplot position is UL, Octave's is LL (same for screen/window)
261 gnuplot_pos(2) = screen_size(2) - gnuplot_pos(2) - gnuplot_size(2);
262 gnuplot_pos = max (gnuplot_pos, 1);
263 size_str = sprintf ("%s position %d,%d", size_str,
264 gnuplot_pos(1), gnuplot_pos(2));
265 endif
266 endif
267 elseif (strncmpi (term, "aqua", 3))
268 ## Aqua has size, but the format is different.
269 size_str = sprintf ("size %d %d", gnuplot_size);
270 elseif (strncmpi (term, "dumb", 3))
271 new_stream = 1; 222 new_stream = 1;
272 if (~isempty (getenv ("COLUMNS")) && ~isempty (getenv ("LINES"))) 223 if (~isempty (getenv ("COLUMNS")) && ~isempty (getenv ("LINES")))
273 ## Let dumb use full text screen size (minus prompt lines). 224 ## Let dumb use full text screen size (minus prompt lines).
274 n = sprintf ("%i", -2 - length (find (sprintf ("%s", PS1) == "\n"))); 225 n = sprintf ("%i", -2 - length (find (sprintf ("%s", PS1) == "\n")));
275 ## n = the number of times \n appears in PS1 226 ## n = the number of times \n appears in PS1
276 size_str = ["size ", getenv("COLUMNS"), ",", getenv("LINES"), n]; 227 size_str = ["size ", getenv("COLUMNS"), ",", getenv("LINES"), n];
277 else 228 else
278 ## Use the gnuplot default. 229 ## Use the gnuplot default.
279 size_str = ""; 230 size_str = "";
280 end 231 end
281 elseif (strncmpi (term, "fig", 3)) 232 case {"aqua", "fig", "corel"}
282 ## Fig also has size, but the format is different. 233 size_str = sprintf ("size %g %g", gnuplot_size);
283 size_str = sprintf ("size %.15g %.15g", gnuplot_size); 234 case "dxf"
284 elseif (any (strncmpi (term, {"corel", "hpgl"}, 3)))
285 ## The size for corel and hpgl are goes at the end (implicit).
286 size_str = sprintf ("%.15g %.15g", gnuplot_size);
287 elseif (any (strncmpi (term, {"dxf"}, 3)))
288 ## DXF uses autocad units.
289 size_str = ""; 235 size_str = "";
290 else 236 otherwise
291 size_str = ""; 237 size_str = "";
238 endswitch
239 if (strncmpi (term, "x11", 3)
240 && __gnuplot_has_feature__ ("x11_figure_position"))
241 ## X11 allows the window to be positioned as well.
242 units = get (0, "units");
243 unwind_protect
244 set (0, "units", "pixels");
245 screen_size = get (0, "screensize")(3:4);
246 unwind_protect_cleanup
247 set (0, "units", units);
248 end_unwind_protect
249 if (all (screen_size > 0))
250 ## For X11, set the figure positon as well as the size
251 ## gnuplot position is UL, Octave's is LL (same for screen/window)
252 gnuplot_pos(2) = screen_size(2) - gnuplot_pos(2) - gnuplot_size(2);
253 gnuplot_pos = max (gnuplot_pos, 1);
254 size_str = sprintf ("%s position %d,%d", size_str,
255 gnuplot_pos(1), gnuplot_pos(2));
256 endif
292 endif 257 endif
293 else 258 else
294 size_str = ""; 259 size_str = "";
295 warning ("gnuplot_set_term: size is zero") 260 warning ("gnuplot_set_term: size is zero")
296 endif 261 endif
314 term_str = sprintf ("%s %s", term_str, enh_str); 279 term_str = sprintf ("%s %s", term_str, enh_str);
315 endif 280 endif
316 if (! isempty (title_str)) 281 if (! isempty (title_str))
317 term_str = sprintf ("%s %s", term_str, title_str); 282 term_str = sprintf ("%s %s", term_str, title_str);
318 endif 283 endif
319 if (nargin > 3 && ischar (opts_str)) 284 if (isempty (strfind (term, "corel")))
320 ## Options must go last. 285 if (! isempty (size_str) && new_stream)
321 term_str = sprintf ("%s %s", term_str, opts_str); 286 ## size_str comes after other options to permit specification of
322 endif 287 ## the canvas size for terminals cdr/corel.
323 if (! isempty (size_str) && new_stream) 288 term_str = sprintf ("%s %s", term_str, size_str);
324 ## size_str comes after other options to permit specification of 289 endif
325 ## the canvas size for terminals cdr/corel. 290 if (nargin > 3 && ischar (opts_str))
326 term_str = sprintf ("%s %s", term_str, size_str); 291 ## Options must go last.
327 endif 292 term_str = sprintf ("%s %s", term_str, opts_str);
293 endif
294 else
295 if (nargin > 3 && ischar (opts_str))
296 ## Options must go last.
297 term_str = sprintf ("%s %s", term_str, opts_str);
298 endif
299 if (! isempty (size_str) && new_stream)
300 ## size_str comes after other options to permit specification of
301 ## the canvas size for terminals cdr/corel.
302 term_str = sprintf ("%s %s", term_str, size_str);
303 endif
304 endif
305
328 ## Work around the gnuplot feature of growing the x11 window and 306 ## Work around the gnuplot feature of growing the x11 window and
329 ## flickering window (x11, windows, & wxt) when the mouse and 307 ## flickering window (x11, windows, & wxt) when the mouse and
330 ## multiplot are set in gnuplot. 308 ## multiplot are set in gnuplot.
331 fputs (plot_stream, "unset multiplot;\n"); 309 fputs (plot_stream, "unset multiplot;\n");
332 flickering_terms = {"x11", "windows", "wxt", "dumb"}; 310 flickering_terms = {"x11", "windows", "wxt", "dumb"};
388 function have_enhanced = gnuplot_is_enhanced_term (term) 366 function have_enhanced = gnuplot_is_enhanced_term (term)
389 persistent enhanced_terminals; 367 persistent enhanced_terminals;
390 if (isempty (enhanced_terminals)) 368 if (isempty (enhanced_terminals))
391 ## Don't include pstex, pslatex or epslatex here as the TeX commands 369 ## Don't include pstex, pslatex or epslatex here as the TeX commands
392 ## should not be interpreted in that case. 370 ## should not be interpreted in that case.
393 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ... 371 enhanced_terminals = {"aqua", "canvas", "dumb", "emf", "gif", "jpeg", ...
394 "windows", "wxt", "svg", "postscript", "x11", ... 372 "pdf", "pdfcairo", "pm", "png", "pngcairo", ...
395 "pdf", "emf"}; 373 "postscript", "svg", "windows", "wxt", "x11"};
396 endif 374 endif
397 if (nargin < 1) 375 if (nargin < 1)
398 ## Determine the default gnuplot terminal. 376 ## Determine the default gnuplot terminal.
399 term = gnuplot_default_term (); 377 term = gnuplot_default_term ();
400 endif 378 endif
403 381
404 function ret = output_to_screen (term) 382 function ret = output_to_screen (term)
405 ret = any (strcmpi ({"aqua", "dumb", "wxt", "x11", "windows", "pm"}, term)); 383 ret = any (strcmpi ({"aqua", "dumb", "wxt", "x11", "windows", "pm"}, term));
406 endfunction 384 endfunction
407 385
408 function ret = term_units_are_pixels (term) 386
409 ret = any (strncmpi ({"emf", "gif", "jpeg", "pbm", "png", "svg"}, term, 3));
410 endfunction
411