1 ## Copyright (C) 2010-2012 Ben Abbott
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{bbox} =} __tight_eps_bbox__ (@var{@dots{}})
21 ## Undocumented internal function.
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2010-07-26
27 function bb = __tight_eps_bbox__ (opts, eps_file_name)
29 box_string = "%%BoundingBox:";
31 cmd = sprintf ("\"%s\" \"%s\" 2>&1", "head", eps_file_name);
32 [status, output] = system (cmd);
35 orig_bbox_line = get_bbox (output);
37 error ("print:noboundingbox",
38 "print.m: no bounding box found in '%s'", eps_file_name);
41 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4 -sDEVICE=bbox";
42 cmd = sprintf ("\"%s\" %s \"%s\" 2>&1", opts.ghostscript.binary,
43 ghostscript_options, eps_file_name);
44 [status, output] = system (cmd);
47 tight_bbox_line = get_bbox (output);
49 warning ("print:nogsboundingbox",
50 "print.m: ghostscript failed to determine the bounding for '%s'",
54 ## Attempt to fix the bbox in place.
55 fid = fopen (eps_file_name, "r+");
57 bbox_replaced = false;
58 looking_for_bbox = true;
59 while (looking_for_bbox)
60 current_line = fgetl (fid);
61 if (strncmpi (current_line, box_string, numel(box_string)))
62 line_length = numel (current_line);
63 num_spaces = line_length - numel (tight_bbox_line);
64 if (numel (current_line) >= numel (tight_bbox_line))
65 new_line = tight_bbox_line;
66 new_line(end+1:numel(current_line)) = " ";
68 ## Back up to the beginning of the line (include EOL characters).
70 fseek (fid, -line_length-2, "cof");
72 fseek (fid, -line_length-1, "cof");
74 count = fprintf (fid, "%s", new_line);
76 looking_for_bbox = false;
77 elseif (! ischar (current_line))
78 looking_for_bbox = false;
81 unwind_protect_cleanup
85 ## If necessary load the eps-file and replace the bbox (can be slow).
87 fid = fopen (eps_file_name, "r");
89 data = char (fread (fid, Inf)).';
90 unwind_protect_cleanup
93 n = strfind (data, box_string);
95 ## Only replace one instance.
98 error ("print:noboundingbox", ...
99 "print.m: no bounding box found in '%s'.", eps_file_name);
101 m = numel (orig_bbox_line);
102 data = horzcat (data(1:(n-1)), tight_bbox_line, data((n+m):end));
103 fid = fopen (eps_file_name, "w");
105 fprintf (fid, "%s", data);
106 unwind_protect_cleanup
113 function bbox_line = get_bbox (lines)
114 box_string = "%%BoundingBox:";
115 pattern = strcat (box_string, "[^%]*");
116 pattern = pattern(1:find(double(pattern)>32, 1, "last"));
117 bbox_line = regexp (lines, pattern, "match");
118 if (iscell (bbox_line))
119 bbox_line = bbox_line{1};
121 ## Remove the EOL characters.
122 bbox_line(double(bbox_line)<32) = "";