Mercurial > hg > octave-lyh
annotate scripts/plot/private/__ghostscript__.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | d5bd2766c640 |
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} {} __ghostscript__ (@var{@dots{}}) | |
21 ## Undocumented internal function. | |
22 ## @end deftypefn | |
23 | |
24 ## Author: Ben Abbott <bpabbott@mac.com> | |
25 ## Created: 2010-07-26 | |
26 | |
11009 | 27 function [gs_cmd, cleanup_cmd] = __ghostscript__ (varargin); |
10834 | 28 |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
29 opts.binary = ""; |
11009 | 30 opts.source = "-"; |
31 opts.output = "-"; | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
32 opts.device = ""; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
33 opts.epscrop = false; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
34 opts.antialiasing = false; |
11009 | 35 opts.resolution = 150; |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
36 opts.papersize = ""; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
37 opts.pageoffset = [0 0]; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
38 opts.debug = false; |
10913
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
39 opts.level = []; |
11009 | 40 opts.prepend = ""; |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
41 |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
42 offsetfile = ""; |
11009 | 43 offset_ps = {}; |
44 cleanup_cmd = ""; | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
45 |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
46 args = varargin; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
47 n = find (cellfun (@isstruct, args)); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
48 if (! isempty (n)) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
49 f = fieldnames (args{n}); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
50 for m = 1:numel(f) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
51 opts.(f{m}) = args{n}.(f{m}); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
52 endfor |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
53 args(n) = []; |
10834 | 54 endif |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
55 for n = 1:2:numel(args) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
56 opts.(args{n}) = args{n+1}; |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
57 endfor |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
58 |
11009 | 59 if (isempty (opts.papersize)) |
60 format_for_printer = false; | |
61 else | |
62 format_for_printer = true; | |
63 endif | |
64 | |
65 gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s", opts.device); | |
10913
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
66 |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
67 if (! isempty (opts.level) && ismember (opts.level, [1, 2, 3])) |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
68 gs_opts = sprintf ("%s -dLanguageLevel=%d", gs_opts, round (opts.level)); |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
69 endif |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
70 |
11009 | 71 if (opts.antialiasing && isempty (strfind (opts.device, "write"))) |
72 ## Apply anti-aliasing to all bitmap formats/devices | |
73 gs_opts = sprintf ("%s -dTextAlphaBits=4 -dGraphicsAlphaBits=4", gs_opts); | |
74 gs_opts = sprintf ("%s -r%dx%d", gs_opts, [1, 1] * opts.resolution); | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
75 elseif (any (strcmp (opts.device, {"pswrite", "ps2write", "pdfwrite"}))) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
76 gs_opts = sprintf ("%s -dEmbedAllFonts=true", gs_opts); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
77 if (strcmp (opts.device, "pdfwrite")) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
78 ## Optimize for loading |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
79 gs_opts = sprintf ("%s -dOptimize=true", gs_opts); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
80 endif |
10834 | 81 endif |
82 | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
83 if (opts.epscrop) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
84 ## papersize is specified by the eps bbox |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
85 gs_opts = sprintf ("%s -dEPSCrop", gs_opts); |
11009 | 86 endif |
87 if (format_for_printer) | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
88 if (ischar (opts.papersize)) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
89 gs_opts = sprintf ("%s -sPAPERSIZE=%s", gs_opts, opts.papersize); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
90 elseif (isnumeric (opts.papersize) && numel (opts.papersize) == 2) |
10954
ee9d74048827
Consolidate gs code into print.m.
Ben Abbott <bpabbott@mac.com>
parents:
10919
diff
changeset
|
91 gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d", |
ee9d74048827
Consolidate gs code into print.m.
Ben Abbott <bpabbott@mac.com>
parents:
10919
diff
changeset
|
92 gs_opts, opts.papersize); |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
93 if (opts.papersize(1) > opts.papersize(2)) |
10954
ee9d74048827
Consolidate gs code into print.m.
Ben Abbott <bpabbott@mac.com>
parents:
10919
diff
changeset
|
94 ## Lanscape mode: This option will result in automatic rotation of the |
ee9d74048827
Consolidate gs code into print.m.
Ben Abbott <bpabbott@mac.com>
parents:
10919
diff
changeset
|
95 ## document page if the requested page size matches one |
11009 | 96 ## of the default page sizes |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
97 gs_opts = sprintf ("%s -dNORANGEPAGESIZE", gs_opts); |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
98 endif |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
99 else |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
100 error ("print:badpapersize", "__ghostscript__.m: invalid 'papersize'") |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
101 endif |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
102 gs_opts = sprintf ("%s -dFIXEDMEDIA", gs_opts); |
11009 | 103 ## "pageoffset" is relative to the coordinates, not the BBox LLHC. |
104 str = sprintf ("%s [%d %d] %s", "<< /Margins [0 0] /.HWMargins [0 0 0 0] /PageOffset", | |
105 opts.pageoffset, ">> setpagedevice"); | |
106 offset_ps = {"%!PS-Adobe-3.0", str, "%%EOF"}; | |
107 if (isfield (opts, "offsetfile")) | |
108 offsetfile = opts.offsetfile; | |
109 cleanup_cmd = ""; | |
110 else | |
111 offsetfile = strcat (tmpnam (), ".ps"); | |
112 cleanup_cmd = sprintf ("rm %s", offsetfile); | |
113 endif | |
10913
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
114 unwind_protect |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
115 fid = fopen (offsetfile, "w"); |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
116 if (fid == -1) |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
117 error ("print:fopenfailed", "__ghostscript__.m: fopen() failed."); |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
118 endif |
11009 | 119 fprintf (fid, "%s\n", offset_ps{:}) |
10913
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
120 unwind_protect_cleanup |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
121 status = fclose (fid); |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
122 if (status == -1) |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
123 error ("print:fclosefailed", "__ghostscript__.m: fclose() failed."); |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
124 endif |
dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10854
diff
changeset
|
125 end_unwind_protect |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
126 if (opts.debug) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
127 fprintf ("---- begin %s ----\n", offsetfile) |
11009 | 128 fprintf ("%s\n", offset_ps{:}) |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
129 fprintf ("----- end %s -----\n", offsetfile) |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
130 endif |
10834 | 131 endif |
132 | |
11009 | 133 if (isempty (opts.output)) |
134 cmd = sprintf ("%s %s", opts.binary, gs_opts); | |
135 else | |
136 cmd = sprintf ("%s %s -sOutputFile=%s", opts.binary, gs_opts, opts.output); | |
137 endif | |
138 if (! isempty (opts.prepend) | |
139 && any (strcmpi (opts.device, {"pswrite", "ps2write", "pdfwrite"}))) | |
140 ## FIXME - Fonts get may be mangled when appending ps/ps2. | |
141 ## See "How to concatenate several PS files" at the link, | |
142 ## http://en.wikibooks.org/wiki/PostScript_FAQ | |
143 cmd = sprintf ("%s %s", cmd, opts.prepend); | |
11014
92bb25c0da9e
When appending, delete temporary files at the end of the ghostscript pipeline.
Ben Abbott <bpabbott@mac.com>
parents:
11009
diff
changeset
|
144 if (isempty (cleanup_cmd)) |
92bb25c0da9e
When appending, delete temporary files at the end of the ghostscript pipeline.
Ben Abbott <bpabbott@mac.com>
parents:
11009
diff
changeset
|
145 cleanup_cmd = sprintf ("rm %s", opts.prepend); |
92bb25c0da9e
When appending, delete temporary files at the end of the ghostscript pipeline.
Ben Abbott <bpabbott@mac.com>
parents:
11009
diff
changeset
|
146 else |
92bb25c0da9e
When appending, delete temporary files at the end of the ghostscript pipeline.
Ben Abbott <bpabbott@mac.com>
parents:
11009
diff
changeset
|
147 cleanup_cmd = sprintf ("%s ; rm %s", cleanup_cmd, opts.prepend); |
92bb25c0da9e
When appending, delete temporary files at the end of the ghostscript pipeline.
Ben Abbott <bpabbott@mac.com>
parents:
11009
diff
changeset
|
148 endif |
11009 | 149 endif |
150 if (! isempty (offsetfile) && format_for_printer) | |
151 cmd = sprintf ("%s %s", cmd, offsetfile); | |
152 endif | |
153 if (! isempty (opts.source)) | |
154 cmd = sprintf ("%s %s", cmd, opts.source); | |
155 endif | |
10834 | 156 |
157 if (opts.debug) | |
11009 | 158 fprintf ("Ghostscript command: '%s'\n", cmd); |
10834 | 159 endif |
160 | |
11009 | 161 gs_cmd = cmd; |
10834 | 162 |
163 endfunction | |
10854
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
164 |
d5a7db05d591
Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents:
10834
diff
changeset
|
165 |