Mercurial > hg > octave-nkf
annotate scripts/plot/private/__tight_eps_bbox__.m @ 10913:dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Fri, 27 Aug 2010 06:39:36 -0400 |
parents | 05ba991794d4 |
children | 2c356a35d7f5 |
rev | line source |
---|---|
10834 | 1 ## Copyright (C) 2010 Ben Abbott |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify | |
4 ## it under the terms of the GNU General Public License as published by | |
5 ## the Free Software Foundation; either version 2 of the License, or | |
6 ## (at your option) any later version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, | |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 ## GNU General Public License for more details. | |
12 ## | |
13 ## You should have received a copy of the GNU General Public License | |
14 ## along with Octave; see the file COPYING. If not, see | |
15 ## <http://www.gnu.org/licenses/>. | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Function File} {@var{bbox} =} __tight_eps_bbox__ (@var{@dots{}}) | |
19 ## Undocumented internal function. | |
20 ## @end deftypefn | |
21 | |
22 ## Author: Ben Abbott <bpabbott@mac.com> | |
23 ## Created: 2010-07-26 | |
24 | |
25 function bb = __tight_eps_bbox__ (opts, eps_file_name) | |
26 | |
27 box_string = "%%BoundingBox:"; | |
28 | |
29 cmd = sprintf ("\"%s\" \"%s\" 2>&1", "head", eps_file_name); | |
30 [status, output] = system (cmd); | |
31 | |
32 if (status == 0) | |
33 orig_bbox_line = get_bbox (output); | |
34 else | |
35 error ("print:noboundingbox", | |
36 "print.m: no bounding box found in '%s'", eps_file_name) | |
37 endif | |
38 | |
39 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4 -sDEVICE=bbox"; | |
10913
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
40 cmd = sprintf ("\"%s\" %s \"%s\" 2>&1", opts.ghostscript.binary, |
10834 | 41 ghostscript_options, eps_file_name); |
42 [status, output] = system (cmd); | |
43 | |
44 if (status == 0) | |
45 tight_bbox_line = get_bbox (output); | |
46 else | |
47 warning ("print:nogsboundingbox", | |
48 "print.m: ghostscript failed to determine the bounding for '%s'", | |
49 eps_file_name) | |
50 endif | |
51 | |
52 ## Attempt to fix the bbox in place. | |
53 fid = fopen (eps_file_name, "r+"); | |
54 unwind_protect | |
55 bbox_replaced = false; | |
56 looking_for_bbox = true; | |
57 while (looking_for_bbox) | |
58 current_line = fgetl (fid); | |
59 if (strncmpi (current_line, box_string, numel(box_string))) | |
60 line_length = numel (current_line); | |
61 num_spaces = line_length - numel (tight_bbox_line); | |
62 if (numel (current_line) >= numel (tight_bbox_line)) | |
63 new_line = tight_bbox_line; | |
64 new_line(end+1:numel(current_line)) = " "; | |
65 bbox_replaced = true; | |
66 ## Back up to the beginning of the line (include EOL characters). | |
67 if (ispc ()) | |
68 fseek (fid, -line_length-2, "cof"); | |
69 else | |
70 fseek (fid, -line_length-1, "cof"); | |
71 endif | |
72 count = fprintf (fid, "%s", new_line); | |
73 endif | |
74 looking_for_bbox = false; | |
75 elseif (! ischar (current_line)) | |
76 looking_for_bbox = false; | |
77 endif | |
78 endwhile | |
79 unwind_protect_cleanup | |
80 fclose (fid); | |
81 end_unwind_protect | |
82 | |
83 ## If necessary load the eps-file and replace the bbox (can be slow). | |
84 if (! bbox_replaced) | |
85 fid = fopen (eps_file_name, "r"); | |
86 unwind_protect | |
87 data = char (fread (fid, Inf)).'; | |
88 unwind_protect_cleanup | |
89 fclose (fid); | |
90 end_unwind_protect | |
91 n = strfind (data, box_string); | |
92 if (numel (n) > 1) | |
93 ## Only replace one instance. | |
94 n = n(1); | |
95 elseif (isempty (n)) | |
96 error ("print:noboundingbox", ... | |
97 "print.m: no bounding box found in '%s'.", eps_file_name) | |
98 endif | |
99 m = numel (orig_bbox_line); | |
100 data = horzcat (data(1:(n-1)), tight_bbox_line, data((n+m):end)); | |
101 fid = fopen (eps_file_name, "w"); | |
102 unwind_protect | |
103 fprintf (fid, "%s", data); | |
104 unwind_protect_cleanup | |
105 fclose (fid); | |
106 end_unwind_protect | |
107 endif | |
108 | |
109 endfunction | |
110 | |
111 function bbox_line = get_bbox (lines) | |
112 box_string = "%%BoundingBox:"; | |
113 pattern = strcat (box_string, "[^%]*"); | |
114 pattern = pattern(1:find(double(pattern)>32, 1, "last")); | |
115 bbox_line = regexp (lines, pattern, "match"); | |
116 if (iscell (bbox_line)) | |
117 bbox_line = bbox_line{1}; | |
118 endif | |
119 ## Remove the EOL characters. | |
120 bbox_line(double(bbox_line)<32) = ""; | |
121 endfunction | |
122 |