]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__tight_eps_bbox__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __tight_eps_bbox__.m
1 ## Copyright (C) 2010-2012 Ben Abbott
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{bbox} =} __tight_eps_bbox__ (@var{@dots{}})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2010-07-26
26
27 function bb = __tight_eps_bbox__ (opts, eps_file_name)
28
29   box_string = "%%BoundingBox:";
30
31   cmd = sprintf ("\"%s\" \"%s\" 2>&1", "head", eps_file_name);
32   [status, output] = system (cmd);
33
34   if (status == 0)
35     orig_bbox_line = get_bbox (output);
36   else
37     error ("print:noboundingbox",
38            "print.m: no bounding box found in '%s'", eps_file_name);
39   endif
40
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);
45
46   if (status == 0)
47     tight_bbox_line = get_bbox (output);
48   else
49     warning ("print:nogsboundingbox",
50              "print.m: ghostscript failed to determine the bounding for '%s'",
51              eps_file_name);
52   endif
53
54   ## Attempt to fix the bbox in place.
55   fid = fopen (eps_file_name, "r+");
56   unwind_protect
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)) = " ";
67           bbox_replaced = true;
68           ## Back up to the beginning of the line (include EOL characters).
69           if (ispc ())
70             fseek (fid, -line_length-2, "cof");
71           else
72             fseek (fid, -line_length-1, "cof");
73           endif
74           count = fprintf (fid, "%s", new_line);
75         endif
76         looking_for_bbox = false;
77       elseif (! ischar (current_line))
78         looking_for_bbox = false;
79       endif
80     endwhile
81   unwind_protect_cleanup
82     fclose (fid);
83   end_unwind_protect
84
85   ## If necessary load the eps-file and replace the bbox (can be slow).
86   if (! bbox_replaced)
87     fid = fopen (eps_file_name, "r");
88     unwind_protect
89       data = char (fread (fid, Inf)).';
90     unwind_protect_cleanup
91       fclose (fid);
92     end_unwind_protect
93     n = strfind (data, box_string);
94     if (numel (n) > 1)
95       ## Only replace one instance.
96       n = n(1);
97     elseif (isempty (n))
98       error ("print:noboundingbox", ...
99              "print.m: no bounding box found in '%s'.", eps_file_name);
100     endif
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");
104     unwind_protect
105       fprintf (fid, "%s", data);
106     unwind_protect_cleanup
107       fclose (fid);
108     end_unwind_protect
109   endif
110
111 endfunction
112
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};
120   endif
121   ## Remove the EOL characters.
122   bbox_line(double(bbox_line)<32) = "";
123 endfunction
124