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

Improvements for fltk printing.
author Ben Abbott <bpabbott@mac.com>
date Thu, 29 Jul 2010 19:44:07 -0400
parents be55736a0783
children d5a7db05d591
comparison
equal deleted inserted replaced
10833:e5c752231985 10834:05ba991794d4
27 27
28 function __gnuplot_print__ (varargin) 28 function __gnuplot_print__ (varargin)
29 29
30 persistent warn_on_inconsistent_orientation = true 30 persistent warn_on_inconsistent_orientation = true
31 31
32 persistent ghostscript_binary = "";
33 if (isempty (ghostscript_binary))
34 ghostscript_binary = getenv ("GSC");
35 ng = 0;
36 if (isunix ())
37 ## Unix - Includes Mac OSX and Cygwin.
38 gs_binaries = {"gs", "gs.exe"};
39 else
40 ## pc - Includes Win32 and mingw.
41 gs_binaries = {"gs.exe", "gswin32c.exe"};
42 endif
43 while (ng < numel (gs_binaries) && isempty (ghostscript_binary))
44 ng = ng + 1;
45 ghostscript_binary = file_in_path (EXEC_PATH, gs_binaries{ng});
46 endwhile
47 endif
48
49 old_fig = get (0, "currentfigure"); 32 old_fig = get (0, "currentfigure");
50 unwind_protect 33 unwind_protect
51 opts = __print_parse_opts__ (varargin{:}); 34 opts = __print_parse_opts__ (varargin{:});
52 have_ghostscript = (exist (ghostscript_binary, "file") == 2); 35 have_ghostscript = ! isempty (opts.ghostscript_binary);
53 36
54 doprint = isempty (opts.name); 37 doprint = isempty (opts.name);
55 if (doprint) 38 if (doprint)
56 if (isempty (opts.devopt)) 39 if (isempty (opts.devopt))
57 if (opts.use_color < 0) 40 if (opts.use_color < 0)
463 drawnow (new_terminal, opts.name, mono); 446 drawnow (new_terminal, opts.name, mono);
464 endif 447 endif
465 if (opts.append_to_file) 448 if (opts.append_to_file)
466 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE"; 449 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE";
467 command = sprintf ("%s %s -sDEVICE=%s -sOutputFile=%s %s %s -q", ... 450 command = sprintf ("%s %s -sDEVICE=%s -sOutputFile=%s %s %s -q", ...
468 ghostscript_binary, ghostscript_options, ghostscript_device, ... 451 opts.ghostscript_binary, ghostscript_options, ghostscript_device, ...
469 temp_name, appended_file_name, opts.name); 452 temp_name, appended_file_name, opts.name);
470 status1 = system (command); 453 status1 = system (command);
471 status2 = system (sprintf ("mv %s %s", temp_name, appended_file_name)); 454 status2 = system (sprintf ("mv %s %s", temp_name, appended_file_name));
472 if (status1 != 0 || status2 != 0) 455 if (status1 != 0 || status2 != 0)
473 error ("print: output failed to append to '%s'.", appended_file_name); 456 error ("print: output failed to append to '%s'.", appended_file_name);
487 end_unwind_protect 470 end_unwind_protect
488 471
489 if (! isempty (ghostscript_output)) 472 if (! isempty (ghostscript_output))
490 if (is_eps_file && opts.tight_flag) 473 if (is_eps_file && opts.tight_flag)
491 ## If gnuplot's output is an eps-file then crop at the bounding box. 474 ## If gnuplot's output is an eps-file then crop at the bounding box.
492 fix_eps_bbox (name, ghostscript_binary); 475 __fix_eps_bbox__ (name);
493 endif 476 endif
494 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4"; 477 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4";
495 if (is_eps_file) 478 if (is_eps_file)
496 ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options); 479 ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options);
497 endif 480 endif
499 ## If output is a bitmap then include the resolution 482 ## If output is a bitmap then include the resolution
500 ghostscript_options = sprintf ("%s -r%d", ghostscript_options, opts.resolution); 483 ghostscript_options = sprintf ("%s -r%d", ghostscript_options, opts.resolution);
501 endif 484 endif
502 ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options, 485 ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options,
503 ghostscript_device); 486 ghostscript_device);
504 command = sprintf ("\"%s\" %s -sOutputFile=\"%s\" \"%s\" 2>&1", ghostscript_binary, 487 command = sprintf ("\"%s\" %s -sOutputFile=\"%s\" \"%s\" 2>&1",
505 ghostscript_options, ghostscript_output, opts.name); 488 opts.ghostscript_binary,
489 ghostscript_options, ghostscript_output, opts.name);
506 [errcode, output] = system (command); 490 [errcode, output] = system (command);
507 unlink (name); 491 unlink (name);
508 if (errcode) 492 if (errcode)
509 error ("print: Conversion failed, %s -> %s.\nError was:\n%s\n", 493 error ("print: Conversion failed, %s -> %s.\nError was:\n%s\n",
510 name, ghostscript_output, output); 494 name, ghostscript_output, output);
511 endif 495 endif
512 elseif (is_eps_file && opts.tight_flag && ! doprint) 496 elseif (is_eps_file && opts.tight_flag && ! doprint)
513 ## If the saved output file is an eps file, use ghostscript to set a tight bbox. 497 ## If the saved output file is an eps file, use ghostscript to set a tight bbox.
514 ## This may result in a smaller or larger bbox geometry. 498 ## This may result in a smaller or larger bbox geometry.
515 if (have_ghostscript) 499 if (have_ghostscript)
516 fix_eps_bbox (name, ghostscript_binary); 500 __fix_eps_bbox__ (name);
517 endif 501 endif
518 endif 502 endif
519 503
520 if (doprint) 504 if (doprint)
521 if (isunix ()) 505 if (isunix ())
546 unwind_protect_cleanup 530 unwind_protect_cleanup
547 if (isfigure (old_fig)) 531 if (isfigure (old_fig))
548 figure (old_fig) 532 figure (old_fig)
549 endif 533 endif
550 end_unwind_protect 534 end_unwind_protect
551
552 endfunction
553
554 function bb = fix_eps_bbox (eps_file_name, ghostscript_binary)
555
556 persistent warn_on_no_ghostscript = true
557
558 box_string = "%%BoundingBox:";
559
560 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4 -sDEVICE=bbox";
561 cmd = sprintf ("\"%s\" %s \"%s\" 2>&1", ghostscript_binary,
562 ghostscript_options, eps_file_name);
563 [status, output] = system (cmd);
564
565 if (status == 0)
566
567 pattern = strcat (box_string, "[^%]*");
568 pattern = pattern(1:find(double(pattern)>32, 1, "last"));
569 bbox_line = regexp (output, pattern, "match");
570 if (iscell (bbox_line))
571 bbox_line = bbox_line{1};
572 endif
573 ## Remore the EOL characters.
574 bbox_line(double(bbox_line)<32) = "";
575
576 fid = fopen (eps_file_name, "r+");
577 unwind_protect
578 bbox_replaced = false;
579 while (! bbox_replaced)
580 current_line = fgetl (fid);
581 if (strncmpi (current_line, box_string, numel(box_string)))
582 line_length = numel (current_line);
583 num_spaces = line_length - numel (bbox_line);
584 if (numel (current_line) < numel (bbox_line))
585 ## If there new line is longer, continue with the current line.
586 new_line = current_line;
587 else
588 new_line = bbox_line;
589 new_line(end+1:numel(current_line)) = " ";
590 endif
591 ## Back up to the beginning of the line (include EOL characters).
592 if (ispc ())
593 fseek (fid, -line_length-2, "cof");
594 else
595 fseek (fid, -line_length-1, "cof");
596 endif
597 count = fprintf (fid, "%s", new_line);
598 bbox_replaced = true;
599 elseif (! ischar (current_line))
600 bbox_replaced = true;
601 warning ("print.m: no bounding box found in '%s'.", eps_file_name)
602 endif
603 endwhile
604 unwind_protect_cleanup
605 fclose (fid);
606 end_unwind_protect
607 elseif (warn_on_no_ghostscript)
608 warn_on_no_ghostscript = false;
609 warning ("print.m: Ghostscript failed to determine the bounding box.\nError was:\n%s\n", output)
610 endif
611 535
612 endfunction 536 endfunction
613 537
614 function [h, c] = convert_color2mono (hfig) 538 function [h, c] = convert_color2mono (hfig)
615 unwind_protect 539 unwind_protect