comparison scripts/plot/print.m @ 9107:d0d507cbd123

Improvements to printing functionality.
author Ben Abbott <bpabbott@mac.com>
date Fri, 10 Apr 2009 12:11:14 -0400
parents b06dc393ac42
children 4c9aff0c9a61
comparison
equal deleted inserted replaced
9106:1eb5b24186b6 9107:d0d507cbd123
59 ## @itemx epslatex 59 ## @itemx epslatex
60 ## @itemx epslatexstandalone 60 ## @itemx epslatexstandalone
61 ## @itemx pstex 61 ## @itemx pstex
62 ## @itemx pslatex 62 ## @itemx pslatex
63 ## Generate a LaTeX (or TeX) file for labels, and eps/ps for 63 ## Generate a LaTeX (or TeX) file for labels, and eps/ps for
64 ## graphics. The file produced by @code{epslatexstandalone} can be 64 ## graphics. The file produced by @code{epslatexstandalone} can be
65 ## processed directly by LaTeX. The other formats are intended to 65 ## processed directly by LaTeX. The other formats are intended to
66 ## be included in a LaTeX (or TeX) document. The @code{tex} device 66 ## be included in a LaTeX (or TeX) document. The @code{tex} device
67 ## is the same as the @code{epslatex} device. 67 ## is the same as the @code{epslatex} device.
68 ## @item ill 68 ## @item ill
69 ## @itemx aifm 69 ## @itemx aifm
70 ## Adobe Illustrator 70 ## Adobe Illustrator
71 ## @item cdr 71 ## @item cdr
72 ## @itemx corel 72 ## @itemx corel
73 ## CorelDraw 73 ## CorelDraw
74 ## @item dxf 74 ## @item dxf
75 ## AutoCAD 75 ## AutoCAD
76 ## @item emf 76 ## @item emf
77 ## @itemx meta
77 ## Microsoft Enhanced Metafile 78 ## Microsoft Enhanced Metafile
78 ## @item fig 79 ## @item fig
79 ## XFig. If this format is selected the additional options 80 ## XFig. If this format is selected the additional options
80 ## @code{-textspecial} or @code{-textnormal} can be used to control 81 ## @code{-textspecial} or @code{-textnormal} can be used to control
81 ## whether the special flag should be set for the text in the figure 82 ## whether the special flag should be set for the text in
82 ## (default is @code{-textnormal}). 83 ## the figure (default is @code{-textnormal}).
83 ## @item hpgl 84 ## @item hpgl
84 ## HP plotter language 85 ## HP plotter language
85 ## @item mf 86 ## @item mf
86 ## Metafont 87 ## Metafont
87 ## @item png 88 ## @item png
98 ## @item pdf 99 ## @item pdf
99 ## Portable document format 100 ## Portable document format
100 ## @end table 101 ## @end table
101 ## 102 ##
102 ## Other devices are supported by "convert" from ImageMagick. Type 103 ## Other devices are supported by "convert" from ImageMagick. Type
103 ## system("convert") to see what formats are available. 104 ## system("convert") to see what formats are available.
104 ## 105 ##
105 ## If the device is omitted, it is inferred from the file extension, 106 ## If the device is omitted, it is inferred from the file extension,
106 ## or if there is no filename it is sent to the printer as postscript. 107 ## or if there is no filename it is sent to the printer as postscript.
107 ## 108 ##
108 ## @itemx -r@var{NUM} 109 ## @itemx -r@var{NUM}
109 ## Resolution of bitmaps in pixels per inch. 110 ## Resolution of bitmaps in pixels per inch. For both metafiles and
111 ## SVG the default is the screen resolution, for other it is 150 dpi.
112 ## To specify screen resolution, use "-r0".
110 ## 113 ##
111 ## @itemx -S@var{xsize},@var{ysize} 114 ## @itemx -S@var{xsize},@var{ysize}
112 ## Plot size in pixels for PNG and SVG. If using the command form of 115 ## Plot size in pixels for EMF, GIF, JPEG, PBM, PNG and SVG. If
113 ## the print function, you must quote the @var{xsize},@var{ysize} 116 ## using the command form of the print function, you must quote the
114 ## option. For example, by writing @code{"-S640,480"}. 117 ## @var{xsize},@var{ysize} option. For example, by writing
118 ## @code{"-S640,480"}. The size defaults to that specified by the
119 ## figure's paperposition property.
115 ## 120 ##
116 ## @item -F@var{fontname} 121 ## @item -F@var{fontname}
117 ## @itemx -F@var{fontname}:@var{size} 122 ## @itemx -F@var{fontname}:@var{size}
118 ## @itemx -F:@var{size} 123 ## @itemx -F:@var{size}
119 ## @var{fontname} set the postscript font (for use with postscript, 124 ## @var{fontname} set the postscript font (for use with postscript,
120 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm, 125 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm,
121 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'. 126 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'.
122 ## @var{size} is given in points. @var{fontname} is ignored for the 127 ## @var{size} is given in points. @var{fontname} is ignored for the
123 ## fig device. 128 ## fig device.
124 ## @end table 129 ## @end table
125 ## 130 ##
126 ## The filename and options can be given in any order. 131 ## The filename and options can be given in any order.
127 ## @end deftypefn 132 ## @end deftypefn
128 133
134 orientation = orient (); 139 orientation = orient ();
135 use_color = 0; # 0=default, -1=mono, +1=color 140 use_color = 0; # 0=default, -1=mono, +1=color
136 force_solid = 0; # 0=default, -1=dashed, +1=solid 141 force_solid = 0; # 0=default, -1=dashed, +1=solid
137 fontsize = ""; 142 fontsize = "";
138 font = ""; 143 font = "";
139 size = ""; 144 canvas_size = "";
140 name = ""; 145 name = "";
141 devopt = ""; 146 devopt = "";
142 printer = ""; 147 printer = "";
143 debug = false; 148 debug = false;
144 debug_file = "octave-print-commands.log"; 149 debug_file = "octave-print-commands.log";
153 158
154 for i = 1:nargin 159 for i = 1:nargin
155 arg = varargin{i}; 160 arg = varargin{i};
156 if (ischar (arg)) 161 if (ischar (arg))
157 if (strcmp (arg, "-color")) 162 if (strcmp (arg, "-color"))
158 use_color = 1; 163 use_color = 1;
159 elseif (strcmp (arg, "-mono")) 164 elseif (strcmp (arg, "-mono"))
160 use_color = -1; 165 use_color = -1;
161 elseif (strcmp (arg, "-solid")) 166 elseif (strcmp (arg, "-solid"))
162 force_solid = 1; 167 force_solid = 1;
163 elseif (strcmp (arg, "-dashed")) 168 elseif (strcmp (arg, "-dashed"))
172 debug = true; 177 debug = true;
173 if (length (arg) > 7) 178 if (length (arg) > 7)
174 debug_file = arg(8:end); 179 debug_file = arg(8:end);
175 endif 180 endif
176 elseif (length (arg) > 2 && arg(1:2) == "-d") 181 elseif (length (arg) > 2 && arg(1:2) == "-d")
177 devopt = arg(3:end); 182 devopt = tolower(arg(3:end));
178 elseif (length (arg) > 2 && arg(1:2) == "-P") 183 elseif (length (arg) > 2 && arg(1:2) == "-P")
179 printer = arg; 184 printer = arg;
180 elseif (length (arg) > 2 && arg(1:2) == "-F") 185 elseif (length (arg) > 2 && arg(1:2) == "-F")
181 idx = rindex (arg, ":"); 186 idx = rindex (arg, ":");
182 if (idx) 187 if (idx)
184 fontsize = arg(idx+1:length(arg)); 189 fontsize = arg(idx+1:length(arg));
185 else 190 else
186 font = arg(3:length(arg)); 191 font = arg(3:length(arg));
187 endif 192 endif
188 elseif (length (arg) > 2 && arg(1:2) == "-S") 193 elseif (length (arg) > 2 && arg(1:2) == "-S")
189 size = arg(3:length(arg)); 194 canvas_size = arg(3:length(arg));
190 elseif (length (arg) > 2 && arg(1:2) == "-r") 195 elseif (length (arg) > 2 && arg(1:2) == "-r")
191 resolution = arg(3:length(arg)); 196 resolution = arg(3:length(arg));
192 elseif (length (arg) >= 1 && arg(1) == "-") 197 elseif (length (arg) >= 1 && arg(1) == "-")
193 error ("print: unknown option `%s'", arg); 198 error ("print: unknown option `%s'", arg);
194 elseif (length (arg) > 0) 199 elseif (length (arg) > 0)
230 endif 235 endif
231 elseif (strcmp (dev, "ill")) 236 elseif (strcmp (dev, "ill"))
232 dev = "aifm"; 237 dev = "aifm";
233 elseif (strcmp (dev, "cdr")) 238 elseif (strcmp (dev, "cdr"))
234 dev = "corel"; 239 dev = "corel";
235 endif 240 elseif (strcmp (dev, "meta"))
236 241 dev = "emf";
237 ## check if we have to use convert 242 elseif (strcmp (dev, "jpg"))
238 dev_list = {"aifm", "corel", "fig", "png", "jpg", "jpeg", ... 243 dev = "jpeg";
239 "gif", "pbm", "dxf", "mf", "svg", "hpgl", ... 244 endif
240 "ps", "ps2", "psc", "psc2", "eps", "eps2", ... 245
241 "epsc", "epsc2", "emf", "pdf", "pslatex", ... 246 ## Check if the specified device is one that is supported by
242 "epslatex", "epslatexstandalone", "pstex"}; 247 ## gnuplot. If not assume it is one supported by ImageMagick's
243 convertname = ""; 248 ## "convert" utility.
244 [idx, errmsg] = cellidx (dev_list, dev); 249 dev_list = {"aifm", "corel", "fig", "png", "jpeg", ...
245 if (! idx) 250 "gif", "pbm", "dxf", "mf", "svg", "hpgl", ...
251 "ps", "ps2", "psc", "psc2", "eps", "eps2", ...
252 "epsc", "epsc2", "emf", "pdf", "pslatex", ...
253 "epslatex", "epslatexstandalone", "pstex"};
254 if (! any (strcmp (dev, dev_list)))
246 if (! isempty (devopt)) 255 if (! isempty (devopt))
247 convertname = cstrcat (devopt, ":", name); 256 ## Device has been specified but is not supported by gnuplot.
248 else 257 convert_name = cstrcat (devopt, ":", name);
249 convertname = name; 258 else
259 ## Device has *not* been specified and is not supported by gnuplot.
260 convert_name = name;
250 endif 261 endif
251 dev = "epsc"; 262 dev = "epsc";
252 name = cstrcat (tmpnam, ".eps"); 263 name = cstrcat (tmpnam, ".eps");
253 endif 264 else
254 265 convert_name = "";
255 if (strcmp (dev, "ps") || strcmp (dev, "ps2") 266 endif
256 || strcmp (dev, "psc") || strcmp (dev, "psc2") 267
257 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") 268 termn = dev;
258 || strcmp (dev, "eps") || strcmp (dev, "eps2") 269
259 || strcmp (dev, "pstex")|| strcmp (dev, "pslatex") 270 ## SVG isn't actually a bitmap, but gnuplot treats its size option as it
260 || strcmp (dev, "epslatex") || strcmp (dev, "epslatexstandalone")) 271 ## does the bitmap terminals.
272 bitmap_devices = {"emf", "gif", "jpeg", "pbm", "png", "svg"};
273
274 if (any (strcmp (dev, {"ps", "ps2", "psc", "psc2", "epsc", "epsc2", ...
275 "eps", "eps2", "pstex", "pslatex", "epslatex", ...
276 "epslatexstandalone"})))
261 277
262 ## Various postscript options 278 ## Various postscript options
263 if (strcmp (dev, "pstex")|| strcmp (dev, "pslatex") 279 if (any (strcmp (dev, {"pstex", "pslatex", "epslatex"})))
264 || strcmp (dev, "epslatex"))
265 termn = dev;
266 options = ""; 280 options = "";
267 elseif (strcmp (dev, "epslatexstandalone")) 281 elseif (strcmp (dev, "epslatexstandalone"))
268 if (__gnuplot_has_feature__ ("epslatexstandalone_terminal")) 282 if (__gnuplot_has_feature__ ("epslatexstandalone_terminal"))
269 termn = "epslatex"; 283 termn = "epslatex";
270 options = "standalone "; 284 options = "standalone ";
271 else 285 else
272 error ("print: epslatexstandalone needs gnuplot 4.2 or higher"); 286 error ("print: epslatexstandalone needs gnuplot 4.2 or higher");
273 endif 287 endif
274 else 288 else
275 if (dev(1) == "e") 289 if (dev(1) == "e")
276 options = "eps "; 290 options = "eps ";
277 else 291 else
278 options = cstrcat (orientation, " "); 292 options = cstrcat (orientation, " ");
279 endif 293 endif
280 termn = "postscript"; 294 termn = "postscript";
281 endif 295 endif
282 296
283 if (any (dev == "c") || use_color > 0) 297 if (any (dev == "c") || use_color > 0)
315 endif 329 endif
316 if (! isempty (fontsize)) 330 if (! isempty (fontsize))
317 options = cstrcat (options, " ", fontsize); 331 options = cstrcat (options, " ", fontsize);
318 endif 332 endif
319 333
320 new_terminal = cstrcat (dev, " ", options);
321
322 elseif (strcmp (dev, "fig")) 334 elseif (strcmp (dev, "fig"))
323 ## XFig 335 ## XFig
324 options = orientation; 336 options = orientation;
325 if (use_color >= 0) 337 if (use_color >= 0)
326 options = " color"; 338 options = " color";
330 options = cstrcat (options, " ", special_flag); 342 options = cstrcat (options, " ", special_flag);
331 if (! isempty (fontsize)) 343 if (! isempty (fontsize))
332 options = cstrcat (options, " fontsize ", fontsize); 344 options = cstrcat (options, " fontsize ", fontsize);
333 endif 345 endif
334 346
335 new_terminal = cstrcat ("fig ", options);
336
337 elseif (strcmp (dev, "emf")) 347 elseif (strcmp (dev, "emf"))
338 ## Enhanced Metafile format 348 ## Enhanced Metafile format
339 options = " "; 349 options = " ";
340 if (use_color >= 0) 350 if (use_color >= 0)
341 options = " color"; 351 options = " color";
350 endif 360 endif
351 if (! isempty (fontsize)) 361 if (! isempty (fontsize))
352 options = cstrcat (options, " ", fontsize); 362 options = cstrcat (options, " ", fontsize);
353 endif 363 endif
354 364
355 new_terminal = cstrcat ("emf ", options); 365 elseif (any (strcmp (dev, bitmap_devices)))
356 366
357 elseif (strcmp (dev, "png") || strcmp (dev, "gif") 367 if (isempty (canvas_size) && isempty (resolution)
358 || strcmp (dev, "jpg") || strcmp (dev, "jpeg") 368 && any (strcmp (dev, {"pbm", "gif", "jpeg", "png"})))
359 || strcmp (dev, "pbm")) 369 options = "large";
360 if (strcmp (dev, "jpg")) 370 elseif (strcmp (dev, "svg"))
361 dev = "jpeg"; 371 ## Referring to size, either "dynamic" or "fixed"
362 endif 372 options = "fixed";
363 ## Portable network graphics, PBMplus
364
365 ## FIXME -- New PNG interface takes color as "xRRGGBB"
366 ## where x is the literal character 'x' and 'RRGGBB' are the red,
367 ## green and blue components in hex. For now we just ignore it
368 ## and use default. The png terminal now is so rich with options,
369 ## that one perhaps has to write a separate printpng.m function.
370 ## DAS
371
372 if (isempty (size) && isempty (resolution))
373 options = " large";
374 elseif (! isempty (size))
375 options = cstrcat (" size ", size);
376 else 373 else
377 options = ""; 374 options = "";
378 endif 375 end
379 new_terminal = cstrcat (dev, options); 376 if (! isempty (canvas_size))
380 377 options = cstrcat (options, " size ", canvas_size);
381 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) 378 endif
379
380 elseif (any (strcmp (dev, {"dxf", "mf", "hpgl"})))
382 ## AutoCad DXF, METAFONT, HPGL 381 ## AutoCad DXF, METAFONT, HPGL
383 new_terminal = dev;
384
385 elseif (strcmp (dev, "svg"))
386 ## SVG
387 options = ""; 382 options = "";
388 if (! isempty (size)) 383
389 options = cstrcat (" size ", size);
390 endif
391 new_terminal = cstrcat ("svg", options);
392
393 elseif (strcmp (dev, "pdf")) 384 elseif (strcmp (dev, "pdf"))
394 ## Portable Document format 385 ## Portable Document format
395 options = " "; 386 options = " ";
396 if (use_color >= 0) 387 if (use_color >= 0)
397 options = "color"; 388 options = "color";
398 else 389 else
399 options = "mono"; 390 options = "mono";
400 endif 391 endif
401 if (force_solid > 0) 392 if (force_solid >= 0)
402 options = cstrcat (options, " solid"); 393 options = cstrcat (options, " solid");
403 elseif (force_solid < 0) 394 elseif (force_solid < 0)
404 options = cstrcat (options, " dashed"); 395 options = cstrcat (options, " dashed");
405 endif 396 endif
406 if (! isempty (font)) 397 if (! isempty (font))
407 options = cstrcat (options, "\"", font, "\" "); 398 options = cstrcat (options, "\"", font, "\" ");
408 endif 399 endif
409 if (! isempty (fontsize)) 400 if (! isempty (fontsize))
410 options = cstrcat (options, " ", fontsize); 401 options = cstrcat (options, " ", fontsize);
411 endif 402 endif
412 403
413 new_terminal = cstrcat ("pdf ", options); 404 endif
414 405
415 endif 406 if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS"))
407 available_terminals = __gnuplot_get_var__ (gcf, "GPVAL_TERMINALS");
408 available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match");
409 gnuplot_supports_term = any (strcmp (available_terminals, termn));
410 elseif (isunix () && strcmp (termn, "pdf"))
411 ## Some Linux variants do not include a "pdf" capable gnuplot.
412 ## To be safe, use "convert".
413 [status, output] = system ("which convert");
414 have_convert = (status == 0);
415 if (have_convert)
416 gnuplot_supports_term = false;
417 else
418 gnuplot_supports_term = true;
419 endif
420 else
421 gnuplot_supports_term = true;
422 endif
423
424 if (! gnuplot_supports_term)
425 ## If the terminal is not supported by the local gnuplot, try "convert".
426 if (strcmp (termn, "pdf"))
427 if (strfind (name, ".pdf") == numel (name) - 3)
428 convert_name = name;
429 else
430 convert_name = strcat (name, ".pdf");
431 endif
432 name = cstrcat (tmpnam, ".ps");
433 termn = "postscript";
434 options = cstrcat (options, " portrait");
435 ## All "options" for pdf work for postscript as well.
436 else
437 error ("print: the gnuplot terminal, \"%s\", is not available.", termn)
438 endif
439 endif
440
441 new_terminal = cstrcat (termn, " ", options);
416 442
417 mono = use_color < 0; 443 mono = use_color < 0;
418 444
419 if (isempty (resolution)) 445 if (isempty (resolution))
420 resolution = get (0, "screenpixelsperinch"); 446 if (any (strcmp (termn, {"emf", "svg"})))
447 resolution = get (0, "screenpixelsperinch");
448 else
449 resolution = 150;
450 endif
421 else 451 else
422 resolution = str2num (resolution); 452 resolution = str2num (resolution);
453 if (resolution == 0)
454 resolution = get (0, "screenpixelsperinch");
455 endif
423 endif 456 endif
424 figure_properties = get (gcf); 457 figure_properties = get (gcf);
425 if (! isfield (figure_properties, "__pixels_per_inch__")) 458 if (! isfield (figure_properties, "__pixels_per_inch__"))
426 addproperty ("__pixels_per_inch__", gcf, "double", resolution); 459 addproperty ("__pixels_per_inch__", gcf, "double", resolution);
427 endif 460 endif
428 set (gcf, "__pixels_per_inch__", resolution) 461 set (gcf, "__pixels_per_inch__", resolution)
429 462
430 unwind_protect 463 unwind_protect
431 if (! isempty (size) || any (strfind (dev, "eps") == 1)) 464 paper_position_mode = get (gcf, "paperpositionmode");
465 term_for_prn = {"postscript", "pdf"};
466 restore_properties = false;
467 if (! any (strncmp (termn, term_for_prn, numel(termn)))
468 || strncmp (dev, "eps", 3))
469 ## If not pdf and not plain postscript, render an image the size of
470 ## the paperposition box.
471 restore_properties = true;
432 p.paperunits = get (gcf, "paperunits"); 472 p.paperunits = get (gcf, "paperunits");
433 p.papertype = get (gcf, "papertype"); 473 p.papertype = get (gcf, "papertype");
434 p.papersize = get (gcf, "papersize"); 474 p.papersize = get (gcf, "papersize");
435 p.paperposition = get (gcf, "paperposition"); 475 p.paperposition = get (gcf, "paperposition");
436 p.paperpositionmode = get (gcf, "paperpositionmode"); 476 p.paperpositionmode = get (gcf, "paperpositionmode");
437 set (gcf, "paperunits", "inches"); 477 set (gcf, "paperunits", "inches");
438 if (any (strfind (dev, "eps") == 1)) 478 if (! isempty (canvas_size))
439 paperposition_in_inches = get (gcf, "paperposition") + 1/72; 479 size_in_pixels = sscanf (canvas_size ,"%d, %d");
440 paperposition_in_inches(1:2) = 0;
441 papersize_in_inches = paperposition_in_inches(3:4);
442 else
443 size_in_pixels = sscanf (size ,"%d, %d");
444 size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]); 480 size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]);
445 papersize_in_inches = size_in_pixels ./ resolution; 481 papersize_in_inches = size_in_pixels ./ resolution;
446 paperposition_in_inches = [0, 0, papersize_in_inches]; 482 paperposition_in_inches = [0, 0, papersize_in_inches];
483 else
484 paperposition_in_inches = get (gcf, "paperposition");
485 paperposition_in_inches(1:2) = 0;
486 papersize_in_inches = paperposition_in_inches(3:4);
447 endif 487 endif
448 set (gcf, "papertype", "<custom>"); 488 set (gcf, "papertype", "<custom>");
449 set (gcf, "papersize", papersize_in_inches); 489 set (gcf, "papersize", papersize_in_inches);
450 set (gcf, "paperposition", paperposition_in_inches); 490 set (gcf, "paperposition", paperposition_in_inches);
451 set (gcf, "paperpositionmode", "manual"); 491 set (gcf, "paperpositionmode", "manual");
455 else 495 else
456 drawnow (new_terminal, name, mono); 496 drawnow (new_terminal, name, mono);
457 endif 497 endif
458 unwind_protect_cleanup 498 unwind_protect_cleanup
459 ## FIXME - it would be nice to delete "__pixels_per_inch__" property here. 499 ## FIXME - it would be nice to delete "__pixels_per_inch__" property here.
460 if (! isempty (size) || any (strfind (dev, "eps") == 1)) 500 if (restore_properties)
461 props = fieldnames (p); 501 props = fieldnames (p);
462 for n = 1:numel(props) 502 for n = 1:numel(props)
463 set (gcf, props{n}, p.(props{n})) 503 set (gcf, props{n}, p.(props{n}))
464 endfor 504 endfor
465 endif 505 endif
466 end_unwind_protect 506 end_unwind_protect
467 507
468 if (! isempty (convertname)) 508 if (! isempty (convert_name))
469 command = sprintf ("convert '%s' '%s'", name, convertname); 509 command = sprintf ("convert '%s' '%s'", name, convert_name);
470 [errcode, output] = system (command); 510 [errcode, output] = system (command);
471 unlink (name); 511 unlink (name);
472 if (errcode) 512 if (errcode)
473 error ("print: could not convert"); 513 error ("print: could not convert; %s -> %s.", name, convert_name);
474 endif 514 endif
475 endif 515 endif
476 516
477 ## FIXME -- This looks like a dirty, Unix-specific hack. 517 ## FIXME -- This looks like a dirty, Unix-specific hack.
478 ## DAS 518 ## DAS