comparison scripts/plot/print.m @ 10834:05ba991794d4

Improvements for fltk printing.
author Ben Abbott <bpabbott@mac.com>
date Thu, 29 Jul 2010 19:44:07 -0400
parents 693e22af08ae
children 1577accf2ed8
comparison
equal deleted inserted replaced
10833:e5c752231985 10834:05ba991794d4
30 ## 30 ##
31 ## @var{h} specifies the figure handle. If no handle is specified 31 ## @var{h} specifies the figure handle. If no handle is specified
32 ## the handle for the current figure is used. 32 ## the handle for the current figure is used.
33 ## 33 ##
34 ## @var{options}: 34 ## @var{options}:
35 ##
35 ## @table @code 36 ## @table @code
37 ## @item -f@var{h}
38 ## Specify the handle, @var{h}, of the figure to be printed. The
39 ## default is the current figure.
40 ##
36 ## @item -P@var{printer} 41 ## @item -P@var{printer}
37 ## Set the @var{printer} name to which the graph is sent if no 42 ## Set the @var{printer} name to which the graph is sent if no
38 ## @var{filename} is specified. 43 ## @var{filename} is specified.
39 ## 44 ##
40 ## @item -G@var{ghostscript_command} 45 ## @item -G@var{ghostscript_command}
154 ## @item pcx24b 159 ## @item pcx24b
155 ## 24-bit color PCX file format 160 ## 24-bit color PCX file format
156 ## 161 ##
157 ## @item ppm 162 ## @item ppm
158 ## Portable Pixel Map file format 163 ## Portable Pixel Map file format
164 ##
165 ## @item pdfwrite
166 ## Converts ps or eps to pdf
159 ## @end table 167 ## @end table
160 ## 168 ##
161 ## For a complete list, type `system ("gs -h")' to see what formats 169 ## For a complete list, type `system ("gs -h")' to see what formats
162 ## and devices are available. 170 ## and devices are available.
163 ## 171 ##
198 ## @end table 206 ## @end table
199 ## 207 ##
200 ## The filename and options can be given in any order. 208 ## The filename and options can be given in any order.
201 ## @end deftypefn 209 ## @end deftypefn
202 210
203 function varargout = print (varargin) 211 function print (varargin)
204 212
205 f = gcf (); 213 opts = __print_parse_opts__ (varargin{:});
214
215 if (! isfigure (opts.figure))
216 error ("print: no figure to print.")
217 endif
218
219 orig_figure = get (0, "currentfigure");
220 figure (opts.figure)
206 drawnow (); 221 drawnow ();
207 backend = (get (f, "__backend__")); 222 backend = (get (opts.figure, "__backend__"));
208 223
209 varargout = cell (1, nargout); 224 if (strcmp (backend, "gnuplot"))
210 [varargout{:}] = feval (strcat ("__", backend, "_print__"), varargin{:}); 225 ## FIXME - this can be removed when __gnuplot_print__ has been modified
226 ## to work consistently with __fltk_print__
227 __gnuplot_print__ (varargin{:});
228 return
229 endif
230
231 ## FIXME - this can be moved to __print_parse_opts__ when __gnuplot_print__
232 ## has been modified to work consistently with __fltk_print__
233 if (! isempty (opts.canvas_size) && ischar (opts.resolution))
234 opts.canvas_size = str2num (strrep (strrep (opts.canvas_size, "X", ""), "Y", ""));
235 endif
236 if (! isempty (opts.resolution) && ischar (opts.resolution))
237 opts.resolution = str2num (opts.resolution);
238 endif
239 if (! isempty (opts.fontsize) && ischar (opts.fontsize))
240 opts.fontsize = str2num (opts.fontsize);
241 endif
242
243 if (opts.append_to_file)
244 saved_original_file = strcat (tmpnam (), ".", opts.devopt);
245 opts.unlink(end+1) = {save_original_file};
246 movefile (opts.name, saved_original_file);
247 endif
248
249 ## Modify properties as specified by options
250 ## FIXME - need an unwind_protect block
251 props = [];
252
253 if ((! isempty (opts.canvas_size))
254 || (! strcmpi (get (opts.figure, "paperorientation"), opts.orientation)))
255 m = numel (props);
256 props(m+1).h = opts.figure;
257 props(m+1).name = "paperposition";
258 props(m+1).value = {get(opts.figure, "paperposition")};
259 props(m+2).h = opts.figure;
260 props(m+2).name = "paperunits";
261 props(m+2).value = {get(opts.figure, "paperunits")};
262 props(m+3).h = opts.figure;
263 props(m+3).name = "papersize";
264 props(m+3).value = {get(opts.figure, "papersize")};
265 props(m+4).h = opts.figure;
266 props(m+4).name = "paperorientation";
267 props(m+4).value = {get(opts.figure, "paperorientation")};
268 props(m+5).h = opts.figure;
269 props(m+5).name = "papertype";
270 props(m+5).value = {get(opts.figure, "papertype")};
271 if (! isempty (opts.canvas_size))
272 ## canvas_size is in pixels/points
273 set (opts.figure, "paperorientation", "portrait");
274 set (opts.figure, "paperposition", [0, 0, opts.canvas_size]);
275 set (opts.figure, "paperunits", "points");
276 set (opts.figure, "papersize", opts.canvas_size);
277 fpos = get (opts.figure, "position");
278 props(m+6).h = opts.figure;
279 props(m+6).name = "position";
280 props(m+6).value = {fpos};
281 fpos(3:4) = opts.canvas_size;
282 set (opts.figure, "position", fpos);
283 elseif (opts.paperoutput)
284 ## FIXME - should the backend handle this?
285 orient (opts.orientation)
286 endif
287 endif
288
289 if (opts.force_solid != 0)
290 h = findobj (opts.figure, "-property", "linestyle");
291 m = numel (props);
292 for n = 1:numel(h)
293 props(m+n).h = h(n);
294 props(m+n).name = "linestyle";
295 props(m+n).value = {get(h(n), "linestyle")};
296 endfor
297 if (opts.force_solid > 0)
298 linestyle = "-";
299 else
300 linestyle = "--";
301 endif
302 set (h, "linestyle", linestyle)
303 endif
304
305 if (opts.use_color < 0)
306 color_props = {"color", "facecolor", "edgecolor"};
307 for c = 1:numel(color_props)
308 h = findobj (opts.figure, "-property", color_props{c});
309 hnone = findobj (opts.figure, color_props{c}, "none");
310 h = setdiff (h, hnone);
311 m = numel (props);
312 for n = 1:numel(h)
313 rgb = get (h(n), color_props{c});
314 props(m+n).h = h(n);
315 props(m+n).name = color_props{c};
316 props(m+n).value = {get(h(n), color_props{c})};
317 xfer = repmat ([0.30, 0.59, 0.11], size (rgb, 1), 1);
318 ## convert RGB color to RGB gray scale
319 ggg = repmat (sum (xfer .* rgb, 2), 1, 3);
320 set (h(n), color_props{c}, ggg)
321 endfor
322 endfor
323 endif
324
325 if (! isempty (opts.font) || ! isempty (opts.fontsize))
326 h = findobj (opts.figure, "-property", "fontname");
327 m = numel (props);
328 for n = 1:numel(h)
329 if (! isempty (opts.font))
330 props(end+1).h = h(n);
331 props(end).name = "fontname";
332 props(end).value = {get(h(n), "fontname")};
333 endif
334 if (! isempty (opts.fontsize))
335 props(end+1).h = h(n);
336 props(end).name = "fontsize";
337 props(end).value = {get(h(n), "fontsize")};
338 endif
339 endfor
340 if (! isempty (opts.font))
341 set (h, "fontname", opts.font)
342 endif
343 if (! isempty (opts.fontsize))
344 if (ischar (opts.fontsize))
345 fontsize = str2double (opts.fontsize);
346 else
347 fontsize = opts.fontsize;
348 endif
349 set (h, "fontsize", fontsize)
350 endif
351 endif
352
353 ## call the backend print script
354 drawnow ("expose")
355 feval (strcat ("__", backend, "_print__"), opts);
356
357 ## restore modified properties
358 if (isstruct (props))
359 for n = 1:numel(props)
360 set (props(n).h, props(n).name, props(n).value{1})
361 endfor
362 endif
363
364 ## Send to the printer
365 if (opts.send_to_printer)
366 if (isempty (opts.ghostscript_output))
367 prn_datafile = opts.name;
368 else
369 prn_datafile = opts.ghostscript_output;
370 endif
371 if (isempty (opts.printer))
372 prn_cmd = sprintf ("lpr %s '%s' 2>&1", opts.lpr_options, prn_datafile);
373 else
374 prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", opts.lpr_options,
375 opts.printer, prn_datafile);
376 endif
377 if (opts.debug)
378 fprintf ("lpr command: %s\n", prn_cmd)
379 endif
380 [status, output] = system (prn_cmd);
381 if (status != 0)
382 disp (output)
383 warning ("print.m: printing failed.")
384 endif
385 endif
386
387 ## Append to file using GS
388 if (opts.append_to_file)
389 if (strcmp (opts.devopt, "pdf"))
390 suffix = "pdf";
391 elseif (strcmp (opts.devopt(1:2), "ps"))
392 suffix = "ps";
393 endif
394 tmp_combined_file = strcat (tmpnam (), ".", suffix);
395 opts.unlink{end+1} = tmp_combined_file;
396 gs_opts = "-q -dNOPAUSE -dBATCH";
397 gs_cmd = sprintf ("%s %s -sDEVICE=%swrite -sOutputFile=%s %s %s",
398 opts.ghostscript_binary, gs_opts, suffix, tmp_combined_file,
399 saved_original_file, opts.name);
400 [status, output] = system (gs_cmd);
401 if (opts.debug)
402 fprintf ("Append files: %s\n", gs_cmd);
403 endif
404 if (status != 0)
405 warning ("print:failedtoappendfile",
406 "print.m: failed to append output to file '%s'.", opts.name)
407 movefile (saved_original_file, opts.name);
408 else
409 movefile (tmp_combined_file, opts.name);
410 endif
411 endif
412
413 ## Unlink temporary files
414 for n = 1:numel(opts.unlink)
415 [status, output] = unlink (opts.unlink{n});
416 if (status != 0)
417 disp (output)
418 warning ("print.m: failed to delete temporay file, '%s'.", opts.name)
419 endif
420 endfor
421
422 if (isfigure (orig_figure))
423 figure (orig_figure);
424 endif
211 425
212 endfunction 426 endfunction