Mercurial > hg > octave-lyh
annotate scripts/plot/private/__tight_eps_bbox__.m @ 14112:6e9fee72a01c stable
Add missing ";" to line in waitbar.m demo.
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Tue, 27 Dec 2011 15:08:21 -0500 |
parents | b0084095098e |
children | 72c96de7a403 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2010-2011 Ben Abbott |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2 ## |
11104 | 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 ## | |
10834 | 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", | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
38 "print.m: no bounding box found in '%s'", eps_file_name); |
10834 | 39 endif |
40 | |
41 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
|
42 cmd = sprintf ("\"%s\" %s \"%s\" 2>&1", opts.ghostscript.binary, |
10834 | 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'", | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
51 eps_file_name); |
10834 | 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", ... | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
99 "print.m: no bounding box found in '%s'.", eps_file_name); |
10834 | 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 |