annotate scripts/plot/private/__ghostscript__.m @ 10854:d5a7db05d591

Add PS and PDF output for the fltk backend.
author Ben Abbott <bpabbott@mac.com>
date Wed, 04 Aug 2010 20:32:49 -0400
parents 05ba991794d4
children dd6b90f44ae5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
1 ## Copyright (C) 2010 Ben Abbott
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## (at your option) any later version.
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ##
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## GNU General Public License for more details.
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ##
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ## along with Octave; see the file COPYING. If not, see
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## <http://www.gnu.org/licenses/>.
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## -*- texinfo -*-
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18 ## @deftypefn {Function File} {} __ghostscript__ (@var{@dots{}})
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ## Undocumented internal function.
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## @end deftypefn
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
22 ## Author: Ben Abbott <bpabbott@mac.com>
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
23 ## Created: 2010-07-26
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
24
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
25 function status = __ghostscript__ (varargin);
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
27 opts.binary = "";
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
28 opts.source = "";
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
29 opts.output = "";
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;
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
33 opts.resolution = 150;
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;
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
37
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
38 offsetfile = "";
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 args = varargin;
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
41 n = find (cellfun (@isstruct, args));
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
42 if (! isempty (n))
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
43 f = fieldnames (args{n});
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
44 for m = 1:numel(f)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
45 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
46 endfor
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
47 args(n) = [];
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 endif
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
49 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
50 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
51 endfor
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
52
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
53 gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s", opts.device);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
54 if (opts.antialiasing)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
55 gs_opts = sprintf ("%s -dTextAlphaBits=4 -dGraphicsAlphaBits=4", gs_opts);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
56 gs_opts = sprintf ("%s -r%dx%d", gs_opts, [1, 1] * opts.resolution);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
57 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
58 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
59 if (strcmp (opts.device, "pdfwrite"))
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
60 ## Optimize for loading
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
61 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
62 endif
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 endif
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
65 if (opts.epscrop)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
66 ## 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
67 gs_opts = sprintf ("%s -dEPSCrop", gs_opts);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
68 elseif (! isempty (opts.papersize))
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
69 if (ischar (opts.papersize))
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
70 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
71 elseif (isnumeric (opts.papersize) && numel (opts.papersize) == 2)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
72 gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d", gs_opts, opts.papersize);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
73 if (opts.papersize(1) > opts.papersize(2))
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
74 ## Lanscape mode: This option will result in automatic rotation of the document page if the
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
75 ## requested page size matches one of the default page sizes
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
76 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
77 endif
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
78 else
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
79 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
80 endif
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
81 gs_opts = sprintf ("%s -dFIXEDMEDIA", gs_opts);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
82 offsetfile = strcat (tmpnam (), ".ps");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
83 fid = fopen (offsetfile, "w");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
84 if (fid == -1)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
85 error ("print:fopenfailed", "__ghostscript__.m: fopen() failed.");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
86 endif
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
87 fprintf (fid, "%s\n", "%!PS-Adobe-3.0")
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
88 fprintf (fid, "%s [%d %d] %s\n", "<< /Margins [0 0] /.HWMargins [0 0 0 0] /PageOffset",
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
89 opts.pageoffset, ">> setpagedevice");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
90 fprintf (fid, "%%EOF");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
91 status = fclose (fid);
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
92 if (status == -1)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
93 error ("print:fclosefailed", "__ghostscript__.m: fclose() failed.");
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
94 endif
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
95 if (opts.debug)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
96 [~,output] = system (sprintf ("cat %s", offsetfile));
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
97 fprintf ("---- begin %s ----\n", offsetfile)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
98 disp (output)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
99 fprintf ("----- end %s -----\n", offsetfile)
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
100 endif
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 endif
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
103 cmd = sprintf ("%s %s -sOutputFile=%s %s %s",
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
104 opts.binary, gs_opts,
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
105 opts.output, offsetfile, opts.source);
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107 if (opts.debug)
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 fprintf ("Ghostscript command: %s\n", cmd);
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 endif
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111 [status, output] = system (cmd);
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 if (status != 0)
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 warning ("print:ghostscripterror",
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
115 "print.m: ghostscript failed to convert output to file '%s'.", opts.output)
10834
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 endif
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117
05ba991794d4 Improvements for fltk printing.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
118 endfunction
10854
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
119
d5a7db05d591 Add PS and PDF output for the fltk backend.
Ben Abbott <bpabbott@mac.com>
parents: 10834
diff changeset
120