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