comparison scripts/plot/print.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 045558999570
children ee9d74048827
comparison
equal deleted inserted replaced
10912:9abc67b4bd4f 10913:dd6b90f44ae5
87 ## @itemx pdflatex 87 ## @itemx pdflatex
88 ## Generate a @LaTeX{} (or @TeX{}) file for labels, and eps/ps/pdf 88 ## Generate a @LaTeX{} (or @TeX{}) file for labels, and eps/ps/pdf
89 ## for graphics. The file produced by @code{epslatexstandalone} can be 89 ## for graphics. The file produced by @code{epslatexstandalone} can be
90 ## processed directly by @LaTeX{}. The other formats are intended to 90 ## processed directly by @LaTeX{}. The other formats are intended to
91 ## be included in a @LaTeX{} (or @TeX{}) document. The @code{tex} device 91 ## be included in a @LaTeX{} (or @TeX{}) document. The @code{tex} device
92 ## is the same as the @code{epslatex} device. 92 ## is the same as the @code{epslatex} device. The @code{pdflatex} device
93 ## is only available for the FLTK backend.
93 ## 94 ##
94 ## @item tikz 95 ## @item tikz
95 ## Generate a @LaTeX{} file using PGF/TikZ. 96 ## Generate a @LaTeX{} file using PGF/TikZ. For the FLTK the result is
97 ## PGF.
96 ## 98 ##
97 ## @item ill 99 ## @item ill
98 ## @itemx aifm 100 ## @itemx aifm
99 ## Adobe Illustrator 101 ## Adobe Illustrator (Obsolete for Gnuplot versions > 4.2)
100 ## 102 ##
101 ## @item cdr 103 ## @item cdr
102 ## @itemx @nospell{corel} 104 ## @itemx @nospell{corel}
103 ## CorelDraw 105 ## CorelDraw
104 ## 106 ##
181 ## by the figure's "papersize" property. When the output 183 ## by the figure's "papersize" property. When the output
182 ## is sent to a file the size is determined by the plot box defined by 184 ## is sent to a file the size is determined by the plot box defined by
183 ## the figure's "paperposition" property. 185 ## the figure's "paperposition" property.
184 ## 186 ##
185 ## @itemx -append 187 ## @itemx -append
186 ## Appends the PS, PDF, or EPS output to a pre-existing file of the 188 ## Appends the PS, or PDF output to a pre-existing file of the
187 ## same type. 189 ## same type.
188 ## 190 ##
189 ## @itemx -r@var{NUM} 191 ## @itemx -r@var{NUM}
190 ## Resolution of bitmaps in pixels per inch. For both metafiles and 192 ## Resolution of bitmaps in pixels per inch. For both metafiles and
191 ## SVG the default is the screen resolution, for other it is 150 dpi. 193 ## SVG the default is the screen resolution, for other it is 150 dpi.
222 error ("print: no figure to print.") 224 error ("print: no figure to print.")
223 endif 225 endif
224 226
225 orig_figure = get (0, "currentfigure"); 227 orig_figure = get (0, "currentfigure");
226 figure (opts.figure) 228 figure (opts.figure)
227 drawnow ();
228 backend = (get (opts.figure, "__backend__"));
229
230 if (strcmp (backend, "gnuplot"))
231 ## FIXME - this can be removed when __gnuplot_print__ has been modified
232 ## to work consistently with __fltk_print__
233 opts.ghostscript_binary = opts.ghostscript.binary;
234 opts.resolution = opts.ghostscript.resolution;
235 opts.canvas_size = opts.canvas_size * opts.resolution / 72;
236 opts.resolution = sprintf ("%d", opts.resolution);
237 opts.fontsize = sprintf ("%d", opts.fontsize);
238 if (strcmp (opts.devopt, "tiff"))
239 error ("print:notiffoutput",
240 "print.m: TIFF output is not available for the Gnuplot backend.")
241 endif
242 __gnuplot_print__ (opts);
243 return
244 else
245 if (strcmp (opts.devopt, "gif"))
246 error ("print:notiffoutput",
247 "print.m: GIF output is not available for the FLTK backend.")
248 endif
249 endif
250 229
251 if (opts.append_to_file && ! (strncmp (opts.devopt, "pdf", 3) 230 if (opts.append_to_file && ! (strncmp (opts.devopt, "pdf", 3)
252 || strncmp (opts.devopt(1:2), "ps", 2))) 231 || strncmp (opts.devopt(1:2), "ps", 2)))
253 warning ("print:cannotappendfile", 232 warning ("print:cannotappendfile",
254 "print.m: Cannot append files of type '%s'.", opts.devopt) 233 "print.m: Cannot append files of type '%s'.", opts.devopt)
255 opts.append_to_file = false; 234 opts.append_to_file = false;
256 endif 235 endif
257 236
258 if (opts.append_to_file)
259 saved_original_file = strcat (tmpnam (), ".", opts.devopt);
260 opts.unlink(end+1) = {saved_original_file};
261 movefile (opts.name, saved_original_file);
262 endif
263
264 ## Modify properties as specified by options
265 ## FIXME - need an unwind_protect block
266 props = [];
267
268 unwind_protect 237 unwind_protect
238
239 if (opts.append_to_file)
240 saved_original_file = strcat (tmpnam (), ".", opts.devopt);
241 opts.unlink(end+1) = {saved_original_file};
242 movefile (opts.name, saved_original_file);
243 endif
244
245 ## Modify properties as specified by options
246 props = [];
269 247
270 ## backend tranlates figure position to eps bbox in points 248 ## backend tranlates figure position to eps bbox in points
271 fpos = get (opts.figure, "position"); 249 fpos = get (opts.figure, "position");
272 props(1).h = opts.figure; 250 props(1).h = opts.figure;
273 props(1).name = "position"; 251 props(1).name = "position";
274 props(1).value = {fpos}; 252 props(1).value = {fpos};
275 fpos(3:4) = opts.canvas_size; 253 fpos(3:4) = opts.canvas_size;
276 set (opts.figure, "position", fpos) 254 set (opts.figure, "position", fpos)
277 255
256 ## Set figure background to none. This is done both for
257 ## consistency with Matlab and to elliminate the visible
258 ## box along the figure's perimeter.
259 props(2).h = opts.figure;
260 props(2).name = "color";
261 props(2).value{1} = get (props(2).h, props(2).name);
262 set (props(2).h, props(2).name, "none");
263
278 if (opts.force_solid != 0) 264 if (opts.force_solid != 0)
279 h = findobj (opts.figure, "-property", "linestyle"); 265 h = findall (opts.figure, "-property", "linestyle");
280 m = numel (props); 266 m = numel (props);
281 for n = 1:numel(h) 267 for n = 1:numel(h)
282 props(m+n).h = h(n); 268 props(m+n).h = h(n);
283 props(m+n).name = "linestyle"; 269 props(m+n).name = "linestyle";
284 props(m+n).value = {get(h(n), "linestyle")}; 270 props(m+n).value = {get(h(n), "linestyle")};
289 linestyle = "--"; 275 linestyle = "--";
290 endif 276 endif
291 set (h, "linestyle", linestyle) 277 set (h, "linestyle", linestyle)
292 endif 278 endif
293 279
294 if (opts.use_color < 0) 280 if (opts.use_color < 0
281 && ! strcmp (get (opts.figure, "__backend__"), "gnuplot"))
295 color_props = {"color", "facecolor", "edgecolor", "colormap"}; 282 color_props = {"color", "facecolor", "edgecolor", "colormap"};
296 for c = 1:numel(color_props) 283 for c = 1:numel(color_props)
297 h = findobj (opts.figure, "-property", color_props{c}); 284 h = findall (opts.figure, "-property", color_props{c});
298 hnone = findobj (opts.figure, color_props{c}, "none"); 285 hnone = findall (opts.figure, color_props{c}, "none");
299 h = setdiff (h, hnone); 286 h = setdiff (h, hnone);
300 m = numel (props); 287 m = numel (props);
301 for n = 1:numel(h) 288 for n = 1:numel(h)
302 rgb = get (h(n), color_props{c}); 289 rgb = get (h(n), color_props{c});
303 props(m+n).h = h(n); 290 props(m+n).h = h(n);
312 endfor 299 endfor
313 endfor 300 endfor
314 endif 301 endif
315 302
316 if (! isempty (opts.font) || ! isempty (opts.fontsize)) 303 if (! isempty (opts.font) || ! isempty (opts.fontsize))
317 h = findobj (opts.figure, "-property", "fontname"); 304 h = findall (opts.figure, "-property", "fontname");
318 m = numel (props); 305 m = numel (props);
319 for n = 1:numel(h) 306 for n = 1:numel(h)
320 if (! isempty (opts.font)) 307 if (! isempty (opts.font))
321 props(end+1).h = h(n); 308 props(end+1).h = h(n);
322 props(end).name = "fontname"; 309 props(end).name = "fontname";
340 set (h, "fontsize", fontsize) 327 set (h, "fontsize", fontsize)
341 endif 328 endif
342 endif 329 endif
343 330
344 ## call the backend print script 331 ## call the backend print script
345 drawnow ("expose") 332 feval (strcat ("__", get (opts.figure, "__backend__") , "_print__"), opts);
346 feval (strcat ("__", backend, "_print__"), opts); 333
334 ## Send to the printer
335 if (opts.send_to_printer)
336 if (isempty (opts.ghostscript.output))
337 prn_datafile = opts.name;
338 else
339 prn_datafile = opts.ghostscript.output;
340 endif
341 if (isempty (opts.printer))
342 prn_cmd = sprintf ("lpr %s '%s' 2>&1", opts.lpr_options, prn_datafile);
343 else
344 prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", opts.lpr_options,
345 opts.printer, prn_datafile);
346 endif
347 if (opts.debug)
348 fprintf ("lpr command: %s\n", prn_cmd)
349 [status, output] = system ("lpq");
350 disp (output)
351 endif
352 [status, output] = system (prn_cmd);
353 if (status != 0)
354 disp (output)
355 warning ("print.m: printing failed.")
356 endif
357 endif
358
359 ## Append to file using GS
360 if (opts.append_to_file)
361 if (strncmp (opts.devopt, "pdf", 3))
362 suffix = "pdf";
363 device = suffix;
364 elseif (strncmp (opts.devopt(1:2), "ps", 2))
365 ## FIXME - For FLTK & Gnuplot the fonts get mangled
366 ## See "How to concatenate several PS files" at the link,
367 ## http://en.wikibooks.org/wiki/PostScript_FAQ
368 suffix = "ps";
369 device = suffix;
370 endif
371 tmp_combined_file = strcat (tmpnam (), ".", suffix);
372 opts.unlink{end+1} = tmp_combined_file;
373 gs_opts = "-dQUIET -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA";
374 gs_cmd = sprintf ("%s %s -sDEVICE=%swrite -sOutputFile=%s %s %s",
375 opts.ghostscript.binary, gs_opts, device, tmp_combined_file,
376 saved_original_file, opts.name);
377 [status, output] = system (gs_cmd);
378 if (opts.debug)
379 fprintf ("Append files: %s\n", gs_cmd);
380 endif
381 if (status != 0)
382 warning ("print:failedtoappendfile",
383 "print.m: failed to append output to file '%s'.", opts.name)
384 copyfile (saved_original_file, opts.name);
385 else
386 copyfile (tmp_combined_file, opts.name);
387 endif
388 endif
347 389
348 unwind_protect_cleanup 390 unwind_protect_cleanup
349
350 ## restore modified properties 391 ## restore modified properties
351 if (isstruct (props)) 392 if (isstruct (props))
352 for n = 1:numel(props) 393 for n = 1:numel(props)
353 set (props(n).h, props(n).name, props(n).value{1}) 394 set (props(n).h, props(n).name, props(n).value{1})
354 endfor 395 endfor
355 endif 396 endif
356 397
398 ## Unlink temporary files
399 for n = 1:numel(opts.unlink)
400 [status, output] = unlink (opts.unlink{n});
401 if (status != 0)
402 warning ("print.m: %s, '%s'.", output, opts.unlink{n})
403 endif
404 endfor
357 end_unwind_protect 405 end_unwind_protect
358
359 ## Send to the printer
360 if (opts.send_to_printer)
361 if (isempty (opts.ghostscript.output))
362 prn_datafile = opts.name;
363 else
364 prn_datafile = opts.ghostscript.output;
365 endif
366 if (isempty (opts.printer))
367 prn_cmd = sprintf ("lpr %s '%s' 2>&1", opts.lpr_options, prn_datafile);
368 else
369 prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", opts.lpr_options,
370 opts.printer, prn_datafile);
371 endif
372 if (opts.debug)
373 fprintf ("lpr command: %s\n", prn_cmd)
374 [status, output] = system ("lpq");
375 disp (output)
376 endif
377 [status, output] = system (prn_cmd);
378 if (status != 0)
379 disp (output)
380 warning ("print.m: printing failed.")
381 endif
382 endif
383
384 ## Append to file using GS
385 if (opts.append_to_file)
386 if (strncmp (opts.devopt, "pdf", 3))
387 suffix = "pdf";
388 device = suffix;
389 elseif (strncmp (opts.devopt(1:2), "ps", 2))
390 ## FIXME - For FLTK the fonts get mangled
391 ## See the seciton "How to concatenate several PS files" at the link,
392 ## http://en.wikibooks.org/wiki/PostScript_FAQ
393 suffix = "ps";
394 device = suffix;
395 endif
396 tmp_combined_file = strcat (tmpnam (), ".", suffix);
397 opts.unlink{end+1} = tmp_combined_file;
398 gs_opts = "-dQUIET -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA";
399 gs_cmd = sprintf ("%s %s -sDEVICE=%swrite -sOutputFile=%s %s %s",
400 opts.ghostscript.binary, gs_opts, device, tmp_combined_file,
401 saved_original_file, opts.name);
402 [status, output] = system (gs_cmd);
403 if (opts.debug)
404 fprintf ("Append files: %s\n", gs_cmd);
405 endif
406 if (status != 0)
407 warning ("print:failedtoappendfile",
408 "print.m: failed to append output to file '%s'.", opts.name)
409 copyfile (saved_original_file, opts.name);
410 else
411 copyfile (tmp_combined_file, opts.name);
412 endif
413 endif
414
415 ## Unlink temporary files
416 for n = 1:numel(opts.unlink)
417 [status, output] = unlink (opts.unlink{n});
418 if (status != 0)
419 disp (output)
420 warning ("print.m: failed to delete temporay file, '%s'.", opts.unlink{n})
421 endif
422 endfor
423 406
424 if (isfigure (orig_figure)) 407 if (isfigure (orig_figure))
425 figure (orig_figure); 408 figure (orig_figure);
426 endif 409 endif
427 410