comparison scripts/plot/print.m @ 9157:fce7315c1eee

print.m: Fix typo, improvments to code, and allow for creation of tight bbox.
author Ben Abbott <bpabbott@mac.com>
date Tue, 28 Apr 2009 19:45:40 -0400
parents 4c9aff0c9a61
children 923c7cb7f13f
comparison
equal deleted inserted replaced
9156:b2b8ed43b922 9157:fce7315c1eee
137 ## @itemx -r@var{NUM} 137 ## @itemx -r@var{NUM}
138 ## Resolution of bitmaps in pixels per inch. For both metafiles and 138 ## 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. 139 ## SVG the default is the screen resolution, for other it is 150 dpi.
140 ## To specify screen resolution, use "-r0". 140 ## To specify screen resolution, use "-r0".
141 ## 141 ##
142 ## @item -tight
143 ## Forces a tight bounding box for eps-files. Since the ghostscript
144 ## devices are conversion of an eps-file, this option works the those
145 ## devices as well.
146 ##
142 ## @itemx -S@var{xsize},@var{ysize} 147 ## @itemx -S@var{xsize},@var{ysize}
143 ## Plot size in pixels for EMF, GIF, JPEG, PBM, PNG and SVG. If 148 ## Plot size in pixels for EMF, GIF, JPEG, PBM, PNG and SVG. If
144 ## using the command form of the print function, you must quote the 149 ## using the command form of the print function, you must quote the
145 ## @var{xsize},@var{ysize} option. For example, by writing 150 ## @var{xsize},@var{ysize} option. For example, by writing
146 ## @code{"-S640,480"}. The size defaults to that specified by the 151 ## @code{"-S640,480"}. The size defaults to that specified by the
174 devopt = ""; 179 devopt = "";
175 printer = ""; 180 printer = "";
176 debug = false; 181 debug = false;
177 debug_file = "octave-print-commands.log"; 182 debug_file = "octave-print-commands.log";
178 special_flag = "textnormal"; 183 special_flag = "textnormal";
184 tight_flag = false;
179 resolution = ""; 185 resolution = "";
180 if (isunix ()) 186 if (isunix ())
181 ghostscript_binary = "gs"; 187 persistent ghostscript_binary = "gs";
182 elseif (ispc ()) 188 elseif (ispc ())
183 ghostscript_binary = "gswin23c"; 189 persistent ghostscript_binary = "gswin32c";
184 endif 190 endif
185 191
186 old_fig = get (0, "currentfigure"); 192 old_fig = get (0, "currentfigure");
187 unwind_protect 193 unwind_protect
188 ## Ensure the last figure is on the screen for single line commands like 194 ## Ensure the last figure is on the screen for single line commands like
189 ## plot(...); print(...); 195 ## plot(...); print(...);
202 force_solid = -1; 208 force_solid = -1;
203 elseif (strcmp (arg, "-portrait")) 209 elseif (strcmp (arg, "-portrait"))
204 orientation = "portrait"; 210 orientation = "portrait";
205 elseif (strcmp (arg, "-landscape")) 211 elseif (strcmp (arg, "-landscape"))
206 orientation = "landscape"; 212 orientation = "landscape";
213 elseif (strcmp (arg, "-tight"))
214 tight_flag = true;
207 elseif (strcmp (arg, "-textspecial")) 215 elseif (strcmp (arg, "-textspecial"))
208 special_flag = "textspecial"; 216 special_flag = "textspecial";
209 elseif (strncmp (arg, "-debug", 6)) 217 elseif (strncmp (arg, "-debug", 6))
210 debug = true; 218 debug = true;
211 if (length (arg) > 7) 219 if (length (arg) > 7)
239 else 247 else
240 error ("print: expecting inputs to be character string options or a figure handle"); 248 error ("print: expecting inputs to be character string options or a figure handle");
241 endif 249 endif
242 endfor 250 endfor
243 251
252 if (isunix ())
253 [status, output] = system (sprintf ("which %s 2>&1", ghostscript_binary));
254 have_ghostscript = (status == 0);
255 elseif (ispc ())
256 have_ghostscript = true;
257 endif
258
244 doprint = isempty (name); 259 doprint = isempty (name);
245 if (doprint) 260 if (doprint)
246 if (isempty (devopt)) 261 if (isempty (devopt))
247 if (use_color < 0) 262 if (use_color < 0)
248 devopt = "ps"; 263 devopt = "ps";
443 available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match"); 458 available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match");
444 gnuplot_supports_term = any (strcmp (available_terminals, termn)); 459 gnuplot_supports_term = any (strcmp (available_terminals, termn));
445 elseif (strcmp (termn, "pdf")) 460 elseif (strcmp (termn, "pdf"))
446 ## Some Linux variants do not include a "pdf" capable gnuplot. 461 ## Some Linux variants do not include a "pdf" capable gnuplot.
447 ## To be safe, use Ghostscript. 462 ## To be safe, use Ghostscript.
448 if (isunix ())
449 [status, output] = system (sprintf ("which %s", ghostscript_binary));
450 elseif (ispc ())
451 status = 0;
452 endif
453 have_ghostscript = (status == 0);
454 if (have_ghostscript) 463 if (have_ghostscript)
455 gnuplot_supports_term = false; 464 gnuplot_supports_term = false;
456 ghostscript_device = "pdfwrite"; 465 ghostscript_device = "pdfwrite";
457 else 466 else
458 gnuplot_supports_term = true; 467 gnuplot_supports_term = true;
503 512
504 unwind_protect 513 unwind_protect
505 paper_position_mode = get (gcf, "paperpositionmode"); 514 paper_position_mode = get (gcf, "paperpositionmode");
506 terminals_for_prn = {"postscript", "pdf"}; 515 terminals_for_prn = {"postscript", "pdf"};
507 restore_properties = false; 516 restore_properties = false;
508 if ((! any (strncmp (termn, terminals_for_prn, numel(termn))) 517 is_eps_file = strncmp (dev, "eps", 3);
509 || strncmp (dev, "eps", 3)) && ! doprint) 518 output_for_printer = any (strncmp (termn, terminals_for_prn, numel(termn)));
519 if ((! output_for_printer || is_eps_file) && ! doprint)
510 ## If not PDF or PostScript, and the result is not being sent to a printer, 520 ## If not PDF or PostScript, and the result is not being sent to a printer,
511 ## render an image the size of the paperposition box. 521 ## render an image the size of the paperposition box.
512 restore_properties = true; 522 restore_properties = true;
513 p.paperunits = get (gcf, "paperunits"); 523 p.paperunits = get (gcf, "paperunits");
514 p.papertype = get (gcf, "papertype"); 524 p.papertype = get (gcf, "papertype");
545 endfor 555 endfor
546 endif 556 endif
547 end_unwind_protect 557 end_unwind_protect
548 558
549 if (! isempty (ghostscript_output)) 559 if (! isempty (ghostscript_output))
560 if (is_eps_file && tight_flag)
561 ## If gnuplot's output is an eps-file then crop at the bounding box.
562 fix_eps_bbox (name, ghostscript_binary);
563 endif
550 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4"; 564 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4";
551 if (! isempty (strfind (options, "eps"))) 565 if (is_eps_file)
552 ## If gnuplot's output is an eps-file then crop at the bouding box.
553 ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options); 566 ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options);
554 end 567 endif
555 if (isempty (strfind (lower (ghostscript_device), "write"))) 568 if (isempty (strfind (lower (ghostscript_device), "write")))
556 ## If output is a bitmap then include the resolution 569 ## If output is a bitmap then include the resolution
557 ghostscript_options = sprintf ("%s -r%d", ghostscript_options, resolution); 570 ghostscript_options = sprintf ("%s -r%d", ghostscript_options, resolution);
558 endif 571 endif
559 ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options, 572 ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options,
560 ghostscript_device); 573 ghostscript_device);
561 command = sprintf ("%s %s -sOutputFile='%s' '%s'", ghostscript_binary, 574 command = sprintf ("%s %s -sOutputFile='%s' '%s' 2>&1", ghostscript_binary,
562 ghostscript_options, ghostscript_output, name); 575 ghostscript_options, ghostscript_output, name);
563 [errcode, output] = system (command); 576 [errcode, output] = system (command);
564 unlink (name); 577 unlink (name);
565 if (errcode) 578 if (errcode)
566 fprintf ("$ %s\n", command) 579 error ("print: Conversion failed, %s -> %s.", name, ghostscript_output);
567 disp(output) 580 endif
568 error ("print: could not convert; %s -> %s.", name, ghostscript_output); 581 elseif (is_eps_file && tight_flag && ! doprint)
569 endif 582 ## If the saved output file is an eps file, use ghostscript to set a tight bbox.
583 ## This may result in a smaller or larger bbox geometry.
584 fix_eps_bbox (name, ghostscript_binary);
570 endif 585 endif
571 586
572 if (doprint) 587 if (doprint)
573 if (isunix ()) 588 if (isunix ())
574 prn_opt = "-l"; 589 prn_opt = "-l";
577 else 592 else
578 ## FIXME - besides Unix and Windows, what other OS's might be considered. 593 ## FIXME - besides Unix and Windows, what other OS's might be considered.
579 prn_opt = ""; 594 prn_opt = "";
580 endif 595 endif
581 if (isempty (printer)) 596 if (isempty (printer))
582 prn_cmd = sprintf ("lpr %s '%s'", prn_opt, printname); 597 prn_cmd = sprintf ("lpr %s '%s' 2>&1", prn_opt, printname);
583 else 598 else
584 prn_cmd = sprintf ("lpr %s -P %s '%s'", prn_opt, printer, printname); 599 prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", prn_opt, printer, printname);
585 endif 600 endif
586 [status, output] = system (prn_cmd); 601 [status, output] = system (prn_cmd);
587 if (status != 0) 602 if (status != 0)
588 disp (output) 603 disp (output)
589 warning ("print.m: printing failed.") 604 warning ("print.m: printing failed.")
601 endif 616 endif
602 end_unwind_protect 617 end_unwind_protect
603 618
604 endfunction 619 endfunction
605 620
621 function bb = fix_eps_bbox (eps_file_name, ghostscript_binary)
622
623 persistent warn_on_no_ghostscript = true
624
625 box_string = "%%BoundingBox:";
626
627 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4 -sDEVICE=bbox";
628 cmd = sprintf ("%s %s '%s' 2>&1", ghostscript_binary, ghostscript_options, eps_file_name);
629 [status, output] = system (cmd);
630
631 if (status == 0)
632
633 pattern = strcat (box_string, "[^%]*");
634 pattern = pattern(1:find(double(pattern)>32, 1, "last"));
635 bbox_line = regexp (output, pattern, "match");
636 if (iscell (bbox_line))
637 bbox_line = bbox_line{1};
638 endif
639 ## Remore the EOL characters.
640 bbox_line(double(bbox_line)<32) = "";
641
642 fid = fopen (eps_file_name, "r+");
643 unwind_protect
644 bbox_replaced = false;
645 while (! bbox_replaced)
646 current_line = fgetl (fid);
647 if (strncmpi (current_line, box_string, numel(box_string)))
648 line_length = numel (current_line);
649 num_spaces = line_length - numel (bbox_line);
650 if (numel (current_line) < numel (bbox_line))
651 ## If there new line is longer, continue with the current line.
652 new_line = current_line;
653 else
654 new_line = bbox_line;
655 new_line(end+1:numel(current_line)) = " ";
656 endif
657 ## Back up to the beginning of the line (include EOL characters).
658 if (ispc ())
659 fseek (fid, -line_length-2, "cof");
660 else
661 fseek (fid, -line_length-1, "cof");
662 endif
663 count = fprintf (fid, "%s", new_line);
664 bbox_replaced = true;
665 elseif (! ischar (current_line))
666 bbox_replaced = true;
667 warning ("print.m: no bounding box found in '%s'.", eps_file_name)
668 endif
669 endwhile
670 unwind_protect_cleanup
671 fclose (fid);
672 end_unwind_protect
673 elseif (warn_on_no_ghostscript)
674 warn_on_no_ghostscript = false;
675 warning ("print.m: Ghostscript could not be used to adjust bounding box.")
676 endif
677
678 endfunction
679