1 ## Copyright (C) 2010-2012 Ben Abbott
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} __ghostscript__ (@var{@dots{}})
21 ## Undocumented internal function.
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2010-07-26
27 function [gs_cmd, cleanup_cmd] = __ghostscript__ (varargin);
34 opts.antialiasing = false;
35 opts.resolution = 150;
37 opts.pageoffset = [0 0];
47 n = find (cellfun ("isclass", args, "struct"));
49 f = fieldnames (args{n});
51 opts.(f{m}) = args{n}.(f{m});
55 for n = 1:2:numel(args)
56 opts.(args{n}) = args{n+1};
59 if (isempty (opts.papersize))
60 format_for_printer = false;
62 format_for_printer = true;
65 gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s", opts.device);
67 if (! isempty (opts.level) && ismember (opts.level, [1, 2, 3]))
68 gs_opts = sprintf ("%s -dLanguageLevel=%d", gs_opts, round (opts.level));
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);
75 elseif (any (strcmp (opts.device, {"pswrite", "ps2write", "pdfwrite"})))
76 gs_opts = sprintf ("%s -dEmbedAllFonts=true", gs_opts);
77 if (strcmp (opts.device, "pdfwrite"))
78 ## Optimize for loading
79 gs_opts = sprintf ("%s -dOptimize=true", gs_opts);
84 ## papersize is specified by the eps bbox
85 gs_opts = sprintf ("%s -dEPSCrop", gs_opts);
87 if (format_for_printer)
88 if (ischar (opts.papersize))
89 gs_opts = sprintf ("%s -sPAPERSIZE=%s", gs_opts, opts.papersize);
90 elseif (isnumeric (opts.papersize) && numel (opts.papersize) == 2)
91 gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d",
92 gs_opts, opts.papersize);
93 if (opts.papersize(1) > opts.papersize(2))
94 ## Lanscape mode: This option will result in automatic rotation of the
95 ## document page if the requested page size matches one
96 ## of the default page sizes
97 gs_opts = sprintf ("%s -dNORANGEPAGESIZE", gs_opts);
100 error ("print:badpapersize", "__ghostscript__.m: invalid 'papersize'");
102 gs_opts = sprintf ("%s -dFIXEDMEDIA", gs_opts);
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;
111 offsetfile = strcat (tmpnam (), ".ps");
112 cleanup_cmd = sprintf ("rm %s", offsetfile);
115 fid = fopen (offsetfile, "w");
117 error ("print:fopenfailed", "__ghostscript__.m: fopen() failed");
119 fprintf (fid, "%s\n", offset_ps{:});
120 unwind_protect_cleanup
121 status = fclose (fid);
123 error ("print:fclosefailed", "__ghostscript__.m: fclose() failed");
127 fprintf ("---- begin %s ----\n", offsetfile);
128 fprintf ("%s\n", offset_ps{:});
129 fprintf ("----- end %s -----\n", offsetfile);
133 if (isempty (opts.binary))
134 error ("print:no_ghostscript", "__ghostscript__.m: ghostscript is required.");
135 elseif (isempty (opts.output))
136 cmd = sprintf ("%s %s", opts.binary, gs_opts);
138 cmd = sprintf ("%s %s -sOutputFile=%s", opts.binary, gs_opts, opts.output);
140 if (! isempty (opts.prepend)
141 && any (strcmpi (opts.device, {"pswrite", "ps2write", "pdfwrite"})))
142 ## FIXME - Fonts get may be mangled when appending ps/ps2.
143 ## See "How to concatenate several PS files" at the link,
144 ## http://en.wikibooks.org/wiki/PostScript_FAQ
145 cmd = sprintf ("%s %s", cmd, opts.prepend);
146 if (isempty (cleanup_cmd))
147 cleanup_cmd = sprintf ("rm %s", opts.prepend);
149 cleanup_cmd = sprintf ("%s ; rm %s", cleanup_cmd, opts.prepend);
152 if (! isempty (offsetfile) && format_for_printer)
153 cmd = sprintf ("%s %s", cmd, offsetfile);
155 if (! isempty (opts.source))
156 cmd = sprintf ("%s %s", cmd, opts.source);
160 fprintf ("Ghostscript command: '%s'\n", cmd);