comparison scripts/plot/print.m @ 9393:d6c99b2ee941

print.m: reimplement options -landscape and -portrait.
author Ben Abbott <bpabbott@mac.com>
date Wed, 24 Jun 2009 19:04:30 -0400
parents 878df7c11e3d
children 5af462716bff 69828adba966
comparison
equal deleted inserted replaced
9392:26c72e899228 9393:d6c99b2ee941
16 ## You should have received a copy of the GNU General Public License 16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see 17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>. 18 ## <http://www.gnu.org/licenses/>.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} print (@var{filename}, @var{options}) 21 ## @deftypefn {Function File} {} print ()
22 ## @deftypefnx {Function File} {} print (@var{options})
23 ## @deftypefnx {Function File} {} print (@var{filename}, @var{options})
22 ## @deftypefnx {Function File} {} print (@var{h}, @var{filename}, @var{options}) 24 ## @deftypefnx {Function File} {} print (@var{h}, @var{filename}, @var{options})
23 ## Print a graph, or save it to a file 25 ## Print a graph, or save it to a file
24 ## 26 ##
25 ## @var{filename} defines the file name of the output file. If no 27 ## @var{filename} defines the file name of the output file. If no
26 ## filename is specified, output is sent to the printer. 28 ## filename is specified, the output is sent to the printer.
27 ## 29 ##
28 ## @var{h} specifies the figure handle. If no handle is specified 30 ## @var{h} specifies the figure handle. If no handle is specified
29 ## the handle for the current figure is used. 31 ## the handle for the current figure is used.
30 ## 32 ##
31 ## @var{options}: 33 ## @var{options}:
42 ## @item -solid 44 ## @item -solid
43 ## @itemx -dashed 45 ## @itemx -dashed
44 ## Solid or dashed lines. 46 ## Solid or dashed lines.
45 ## @item -portrait 47 ## @item -portrait
46 ## @itemx -landscape 48 ## @itemx -landscape
47 ## Plot orientation, as returned by "orient". 49 ## Specify the orientation of the plot for printed output.
48 ## @item -d@var{device} 50 ## @item -d@var{device}
49 ## Output device, where @var{device} is one of: 51 ## Output device, where @var{device} is one of:
50 ## @table @code 52 ## @table @code
51 ## @item ps 53 ## @item ps
52 ## @itemx ps2 54 ## @itemx ps2
128 ## @end table 130 ## @end table
129 ## 131 ##
130 ## For a complete list, type `system ("gs -h")' to see what formats 132 ## For a complete list, type `system ("gs -h")' to see what formats
131 ## and devices are available. 133 ## and devices are available.
132 ## 134 ##
133 ## For output sent to a printer, the size is determined by the 135 ## When the ghostscript is sent to a printer the size is determined
134 ## figure's "papersize" property. For output to a file the, size 136 ## by the figure's "papersize" property. When the ghostscript output
135 ## is determined by the "paperposition" property. 137 ## is sent to a file the size is determined by the figure's
138 ## "paperposition" property.
136 ## 139 ##
137 ## @itemx -r@var{NUM} 140 ## @itemx -r@var{NUM}
138 ## Resolution of bitmaps in pixels per inch. For both metafiles and 141 ## Resolution of bitmaps in pixels per inch. For both metafiles and
139 ## SVG the default is the screen resolution, for other it is 150 dpi. 142 ## SVG the default is the screen resolution, for other it is 150 dpi.
140 ## To specify screen resolution, use "-r0". 143 ## To specify screen resolution, use "-r0".
167 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> 170 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
168 ## Adapted-By: jwe 171 ## Adapted-By: jwe
169 172
170 function print (varargin) 173 function print (varargin)
171 174
172 orientation = orient (); 175 persistent warn_on_inconsistent_orientation = true
176 orientation = "";
173 use_color = 0; # 0=default, -1=mono, +1=color 177 use_color = 0; # 0=default, -1=mono, +1=color
174 force_solid = 0; # 0=default, -1=dashed, +1=solid 178 force_solid = 0; # 0=default, -1=dashed, +1=solid
175 fontsize = ""; 179 fontsize = "";
176 font = ""; 180 font = "";
177 canvas_size = ""; 181 canvas_size = "";
312 "epsc", "epsc2", "emf", "pdf", "pslatex", ... 316 "epsc", "epsc2", "emf", "pdf", "pslatex", ...
313 "epslatex", "epslatexstandalone", "pstex"}; 317 "epslatex", "epslatexstandalone", "pstex"};
314 if (! any (strcmp (dev, dev_list)) && have_ghostscript) 318 if (! any (strcmp (dev, dev_list)) && have_ghostscript)
315 ghostscript_output = name; 319 ghostscript_output = name;
316 ghostscript_device = dev; 320 ghostscript_device = dev;
317 dev = "epsc"; 321 if (doprint)
318 name = cstrcat (tmpnam, ".eps"); 322 ## If printing, use color postscript.
323 dev = "psc";
324 name = cstrcat (tmpnam, ".ps");
325 else
326 ## If saving to a file, use color encapsulated postscript.
327 dev = "epsc";
328 name = cstrcat (tmpnam, ".eps");
329 endif
319 else 330 else
320 ghostscript_output = ""; 331 ghostscript_output = "";
321 endif 332 endif
322 333
323 termn = dev; 334 termn = dev;
342 endif 353 endif
343 else 354 else
344 if (dev(1) == "e") 355 if (dev(1) == "e")
345 options = "eps "; 356 options = "eps ";
346 else 357 else
347 options = cstrcat (orientation, " "); 358 options = "";
348 endif 359 endif
349 termn = "postscript"; 360 termn = "postscript";
350 endif 361 endif
351 362
352 if (any (dev == "c") || use_color > 0 363 if (any (dev == "c") || use_color > 0
424 435
425 elseif (any (strcmp (dev, bitmap_devices))) 436 elseif (any (strcmp (dev, bitmap_devices)))
426 437
427 if (isempty (canvas_size) && isempty (resolution) 438 if (isempty (canvas_size) && isempty (resolution)
428 && any (strcmp (dev, {"pbm", "gif", "jpeg", "png"}))) 439 && any (strcmp (dev, {"pbm", "gif", "jpeg", "png"})))
429 ##options = "large";
430 options = ""; 440 options = "";
431 elseif (strcmp (dev, "svg")) 441 elseif (strcmp (dev, "svg"))
432 ## Referring to size, either "dynamic" or "fixed" 442 ## Referring to size, either "dynamic" or "fixed"
433 options = "fixed"; 443 options = "fixed";
434 else 444 else
501 else 511 else
502 ghostscript_output = strcat (name, ".pdf"); 512 ghostscript_output = strcat (name, ".pdf");
503 endif 513 endif
504 name = cstrcat (tmpnam, ".ps"); 514 name = cstrcat (tmpnam, ".ps");
505 termn = "postscript"; 515 termn = "postscript";
506 options = cstrcat (options, " portrait");
507 ## All "options" for pdf work for postscript as well. 516 ## All "options" for pdf work for postscript as well.
508 else 517 else
509 error ("print: the device, \"%s\", is not available.", dev) 518 error ("print: the device, \"%s\", is not available.", dev)
510 endif 519 endif
511 endif 520 endif
512 521
522 is_eps_file = strncmp (dev, "eps", 3);
523 p.units = get (gcf, "units");
524 p.paperunits = get (gcf, "paperunits");
525 p.papersize = get (gcf, "papersize");
526 p.paperposition = get (gcf, "paperposition");
527 p.paperpositionmode = get (gcf, "paperpositionmode");
528 p.paperorientation = get (gcf, "paperorientation");
529 if (p.papersize(1) > p.papersize(2))
530 paperorientation = "landscape";
531 else
532 paperorientation = "portrait";
533 endif
534 if (! strcmp (paperorientation, get (gcf, "paperorientation"))
535 && warn_on_inconsistent_orientation)
536 msg = {"print.m - inconsistent papersize and paperorientation properties.\n",
537 sprintf(" papersize = %.2f, %.2f\n", p.papersize),
538 sprintf(" paperorientation = \"%s\"\n", p.paperorientation),
539 " the paperorientation property has been ignored"};
540 warning ("%s",msg{:})
541 warn_on_inconsistent_orientation = false;
542 endif
543
544 if (strcmp (termn, "postscript") && ! strncmp (dev, "eps", 3))
545 if (isempty (orientation))
546 orientation = paperorientation;
547 endif
548 ## This is done here to accommodate ghostscript conversion.
549 options = cstrcat (orientation, " ", options);
550 end
551
513 new_terminal = cstrcat (termn, " ", options); 552 new_terminal = cstrcat (termn, " ", options);
514 553
515 mono = use_color < 0; 554 mono = (use_color < 0);
555
556 terminals_for_prn = {"postscript", "pdf", "pdfcairo"};
557 output_for_printer = any (strncmp (termn, terminals_for_prn, numel(termn)));
516 558
517 if (isempty (resolution)) 559 if (isempty (resolution))
518 if (any (strcmp (dev, {"emf", "svg"}))) 560 if (any (strcmp (dev, {"emf", "svg"})) || output_for_printer)
519 resolution = get (0, "screenpixelsperinch"); 561 resolution = get (0, "screenpixelsperinch");
520 else 562 else
521 resolution = 150; 563 resolution = 150;
522 endif 564 endif
523 else 565 else
531 addproperty ("__pixels_per_inch__", gcf, "double", resolution); 573 addproperty ("__pixels_per_inch__", gcf, "double", resolution);
532 endif 574 endif
533 set (gcf, "__pixels_per_inch__", resolution) 575 set (gcf, "__pixels_per_inch__", resolution)
534 576
535 unwind_protect 577 unwind_protect
536 paper_position_mode = get (gcf, "paperpositionmode"); 578 set (gcf, "paperunits", "inches");
537 terminals_for_prn = {"postscript", "pdf", "pdfcairo"}; 579 set (gcf, "units", "pixels");
538 restore_properties = false; 580 restore_properties = true;
539 is_eps_file = strncmp (dev, "eps", 3);
540 output_for_printer = any (strncmp (termn, terminals_for_prn, numel(termn)));
541 if ((! output_for_printer || is_eps_file) && ! doprint) 581 if ((! output_for_printer || is_eps_file) && ! doprint)
542 ## If not PDF or PostScript, and the result is not being sent to a printer, 582 ## If not PDF or PostScript, and the result is not being sent to a printer,
543 ## render an image the size of the paperposition box. 583 ## render an image the size of the paperposition box.
544 restore_properties = true; 584 ## Trigger the listener to convert all paper props to inches.
545 p.paperunits = get (gcf, "paperunits");
546 p.papertype = get (gcf, "papertype");
547 p.papersize = get (gcf, "papersize");
548 p.paperposition = get (gcf, "paperposition");
549 p.paperpositionmode = get (gcf, "paperpositionmode");
550 set (gcf, "paperunits", "inches");
551 if (! isempty (canvas_size)) 585 if (! isempty (canvas_size))
552 size_in_pixels = sscanf (canvas_size ,"%d, %d"); 586 size_in_pixels = sscanf (canvas_size ,"%d, %d");
553 size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]); 587 size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]);
554 papersize_in_inches = size_in_pixels ./ resolution; 588 papersize_in_inches = size_in_pixels ./ resolution;
555 paperposition_in_inches = [0, 0, papersize_in_inches]; 589 paperposition_in_inches = [0, 0, papersize_in_inches];
556 else 590 else
557 paperposition_in_inches = get (gcf, "paperposition"); 591 paperposition_in_inches = get (gcf, "paperposition");
558 paperposition_in_inches(1:2) = 0; 592 paperposition_in_inches(1:2) = 0;
559 papersize_in_inches = paperposition_in_inches(3:4); 593 papersize_in_inches = paperposition_in_inches(3:4);
560 endif 594 endif
561 set (gcf, "papertype", "<custom>");
562 set (gcf, "papersize", papersize_in_inches); 595 set (gcf, "papersize", papersize_in_inches);
563 set (gcf, "paperposition", paperposition_in_inches); 596 set (gcf, "paperposition", paperposition_in_inches);
564 set (gcf, "paperpositionmode", "manual"); 597 set (gcf, "paperpositionmode", "manual");
598 else
599 if (strcmp (p.paperpositionmode, "auto"))
600 size_in_pixels = get (gcf, "position")(3:4);
601 paperposition_in_inches(3:4) = size_in_pixels ./ resolution;
602 paperposition_in_inches(1:2) = (p.papersize - paperposition_in_inches(3:4))/2;
603 else
604 paperposition_in_inches = p.paperposition;
605 endif
606 if (! isempty (orientation) && ! strcmp (orientation, paperorientation))
607 ## When -landscape/portrait changes the orientation, flip both the
608 ## papersize and paperposition.
609 restore_properties = true;
610 set (gcf, "papersize", p.papersize([2, 1]));
611 set (gcf, "paperposition", paperposition_in_inches([2, 1, 4, 3]));
612 else
613 set (gcf, "paperposition", paperposition_in_inches);
614 endif
565 endif 615 endif
566 if (use_color < 0) 616 if (use_color < 0)
567 [objs_with_color, color_of_objs] = convert_color2mono (gcf); 617 [objs_with_color, color_of_objs] = convert_color2mono (gcf);
568 endif 618 endif
569 if (debug) 619 if (debug)