Mercurial > hg > octave-max
comparison scripts/plot/__gnuplot_print__.m @ 10913:dd6b90f44ae5
Unify gnuplot printing with the fltk backend.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Fri, 27 Aug 2010 06:39:36 -0400 |
parents | d5a7db05d591 |
children | 2a25f282e965 |
comparison
equal
deleted
inserted
replaced
10912:9abc67b4bd4f | 10913:dd6b90f44ae5 |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} __gnuplot_ginput__ (@var{@dots{}}) | 21 ## @deftypefn {Function File} {} __gnuplot_print__ (@var{@dots{}}) |
22 ## Undocumented internal function. | 22 ## Undocumented internal function. |
23 ## @end deftypefn | 23 ## @end deftypefn |
24 | 24 |
25 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> | 25 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
26 ## Adapted-By: jwe | 26 ## Adapted-By: jwe |
27 | 27 |
28 function __gnuplot_print__ (opts) | 28 function __gnuplot_print__ (opts) |
29 | 29 |
30 persistent warn_on_inconsistent_orientation = true | 30 file2unlink = ""; |
31 | 31 |
32 old_fig = get (0, "currentfigure"); | 32 if (isempty (opts.fontsize)) |
33 ## If no fontsize, determine the nominal axes fontsize. | |
34 defaultfontsize = get (0, "defaultaxesfontsize"); | |
35 axesfontsize = get (findobj (opts.figure, "type", "axes"), "fontsize"); | |
36 if (iscell (axesfontsize)) | |
37 axesfontsize = round (median (cell2mat (axesfontsize))); | |
38 endif | |
39 if (isempty (axesfontsize)) | |
40 opts.fontsize = defaultfontsize; | |
41 else | |
42 opts.fontsize = axesfontsize; | |
43 endif | |
44 end | |
45 ## The axes-label and tick-label spacing is determined by | |
46 ## the font spec given in "set terminal ..." | |
47 gp_opts = font_spec (opts); | |
48 | |
33 unwind_protect | 49 unwind_protect |
34 have_ghostscript = ! isempty (opts.ghostscript_binary); | 50 switch lower (opts.devopt) |
35 | 51 case {"eps", "eps2", "epsc", "epsc2"} |
36 doprint = isempty (opts.name); | 52 if (any (strcmp (opts.devopt, {"eps", "epsc"}))) |
37 if (doprint) | 53 gp_opts = sprintf ("%s level1", gp_opts); |
38 if (isempty (opts.devopt)) | 54 endif |
39 if (opts.use_color < 0) | 55 eps_drawnow (opts, opts.name, gp_opts); |
40 opts.devopt = "ps"; | 56 case {"epslatex", "pslatex", "pstex", "epslatexstandalone"} |
41 printname = cstrcat (tmpnam, ".ps"); | 57 n = find (opts.devopt == "l", 1); |
42 else | 58 suffix = opts.devopt(1:n-1); |
43 opts.devopt = "psc"; | 59 dot = find (opts.name == ".", 1, "last"); |
44 printname = cstrcat (tmpnam, ".psc"); | 60 if ((! isempty (dot)) |
61 && any (strcmpi (opts.name(dot:end), | |
62 {".eps", ".ps", ".pdf", ".tex", "."}))) | |
63 name = opts.name(1:dot-1); | |
64 if (dot < numel (opts.name) | |
65 && any (strcmpi (opts.name(dot+1:end), {"eps", "ps"}))) | |
66 ## If user provides eps/ps suffix, use it. | |
67 suffix = opts.name(dot+1:end); | |
45 endif | 68 endif |
69 elseif (dot == numel (opts.name)) | |
70 name = opts.name; | |
71 endif | |
72 if (strfind (opts.devopt, "standalone")) | |
73 term = sprintf ("%s ", | |
74 strrep (opts.devopt, "standalone", " standalone")); | |
46 else | 75 else |
47 printname = cstrcat (tmpnam, ".", devopt); | 76 term = sprintf ("%s ", opts.devopt); |
48 endif | 77 endif |
49 opts.name = printname; | 78 local_drawnow (sprintf ("%s %s", term, gp_opts), |
50 endif | 79 strcat (name, ".", suffix, ".tex"), opts) |
51 | 80 movefile (strcat (name, ".", suffix, ".tex"), strcat (name, ".tex")); |
52 dot = rindex (opts.name, "."); | 81 if (opts.tight_flag && strncmpi (opts.devopt, "eps", 3)) |
53 if (isempty (opts.devopt)) | 82 __tight_eps_bbox__ (opts, strcat (opts.name, ".eps")); |
54 if (dot == 0) | 83 endif |
55 error ("print: no format specified"); | 84 case {"tikz"} |
85 local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, opts); | |
86 case {"ps", "ps2", "psc", "psc2", "pdf"} | |
87 if (any (strcmp (opts.devopt, {"ps", "psc"}))) | |
88 gp_opts = sprintf ("%s level1", gp_opts); | |
89 endif | |
90 ## Gnuplot's BBox LLHC is located at [50,50] | |
91 opts.ghostscript.pageoffset = opts.ghostscript.pageoffset - 50; | |
92 opts.ghostscript.source = strcat (tmpnam (), ".eps"); | |
93 file2unlink = opts.ghostscript.source; | |
94 if (strcmp (opts.devopt, "pdf")) | |
95 opts.ghostscript.device = "pdfwrite"; | |
96 elseif (any (opts.devopt == '2')) | |
97 opts.ghostscript.device = "ps2write"; | |
56 else | 98 else |
57 dev = tolower (opts.name(dot+1:end)); | 99 opts.ghostscript.device = "pswrite"; |
58 endif | 100 opts.ghostscript.level = 1; |
59 else | 101 endif |
60 dev = opts.devopt; | 102 opts.ghostscript.output = opts.name; |
61 endif | 103 eps_drawnow (opts, opts.ghostscript.source, gp_opts); |
62 | 104 case {"svg"} |
63 dev_list = {"aifm", "corel", "fig", "png", "jpeg", ... | 105 local_drawnow (sprintf ("svg dynamic %s", gp_opts), opts.name, opts); |
64 "gif", "pbm", "dxf", "mf", "svg", "hpgl", ... | 106 case {"aifm", "corel", "eepic", "emf", "fig", "pdfcairo", "pngcairo"} |
65 "ps", "ps2", "psc", "psc2", "eps", "eps2", ... | 107 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts); |
66 "epsc", "epsc2", "emf", "pdf", "pslatex", ... | 108 case gs_based_devices () |
67 "epslatex", "epslatexstandalone", "pstex", "tikz"}; | 109 opts.ghostscript.antialiasing = true; |
68 suffixes = {"ai", "cdr", "fig", "png", "jpeg", ... | 110 switch opts.devopt |
69 "gif", "pbm", "dxf", "mf", "svg", "hpgl", ... | 111 case "bmp" |
70 "ps", "ps", "ps", "ps", "eps", "eps", ... | 112 opts.ghostscript.device = "bmp32b"; |
71 "eps", "eps", "emf", "pdf", "tex", ... | 113 case "png" |
72 "tex", "tex", "tex", "tikz"}; | 114 opts.ghostscript.device = "png16m"; |
73 if (dot == 0) | 115 case {"tiff", "tiffn"} |
74 opts.name = strcat (opts.name, ".", suffixes {strcmpi (dev_list, dev)}); | 116 opts.ghostscript.device = "tiff24nc"; |
75 dot = rindex (name, "."); | 117 otherwise |
76 endif | 118 opts.ghostscript.device = opts.devopt; |
77 | 119 endswitch |
78 if (opts.append_to_file) | 120 opts.ghostscript.output = opts.name; |
79 if (any (strcmpi (dev, {"ps", "ps2", "psc", "psc2", "pdf"}))) | 121 opts.ghostscript.source = strcat (tmpnam (), ".eps"); |
80 if (have_ghostscript) | 122 opts.ghostscript.epscrop = true; |
81 file_exists = ((numel (dir (opts.name)) == 1) && (! isdir (opts.name))); | 123 file2unlink = opts.ghostscript.source; |
82 if (! file_exists) | 124 eps_drawnow (opts, opts.ghostscript.source, gp_opts); |
83 opts.append_to_file = 0; | 125 case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"} |
84 end | 126 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts) |
85 end | 127 case {"pdflatex", "pslatexstandalone", "pdflatexstandalone"} |
86 else | 128 error (sprintf ("print:no%soutput", opts.devopt), |
87 warning ("print.m: appended output is not supported for device '%s'", dev) | 129 "print.m: %s output is not available for the GNUPLOT backend.", |
88 opts.append_to_file = 0; | 130 upper (opts.devopt)) |
89 endif | 131 otherwise |
90 endif | 132 ## various ghostscript devices for printers |
91 | 133 opts.ghostscript.device = opts.devopt; |
92 if (strcmp (dev, "tex")) | 134 opts.ghostscript.output = opts.name; |
93 dev = "epslatex"; | 135 opts.ghostscript.epscrop = false; |
94 elseif (strcmp (dev, "ill")) | 136 opts.ghostscript.source = strcat (tmpnam (), ".eps"); |
95 dev = "aifm"; | 137 file2unlink = opts.ghostscript.source; |
96 elseif (strcmp (dev, "cdr")) | 138 ## Gnuplot's BBox LLHC is located at [50,50] |
97 dev = "corel"; | 139 opts.ghostscript.pageoffset = opts.ghostscript.pageoffset - 50; |
98 elseif (strcmp (dev, "meta")) | 140 ## Empirical observation: "-dpxlcolor" requires a sign change. |
99 dev = "emf"; | 141 opts.ghostscript.pageoffset = opts.ghostscript.pageoffset .* [1, -1]; |
100 elseif (strcmp (dev, "jpg")) | 142 ## Printers are not included in gs_devices() |
101 dev = "jpeg"; | 143 gp_opts = font_spec (opts, "devopt", "eps"); |
102 endif | 144 eps_drawnow (opts, opts.ghostscript.source, gp_opts); |
103 | 145 endswitch |
104 if (strcmp (dev, "epslatex")) | 146 |
105 ## gnuplot 4.0 wants ".eps" in the output name | 147 if (! isempty (opts.ghostscript.device)) |
106 if (! __gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix")) | 148 status = __ghostscript__ (opts.ghostscript); |
107 opts.name = cstrcat (name(1:dot), "eps"); | 149 endif |
108 endif | 150 |
109 endif | 151 unwind_protect_cleanup |
110 | 152 if (! isempty (file2unlink)) |
111 ## Check if the specified device is one that is supported by gnuplot. | 153 [status, output] = unlink (file2unlink); |
112 ## If not, assume it is a device/format supported by Ghostscript. | |
113 if (! any (strcmp (dev, dev_list)) && have_ghostscript) | |
114 ghostscript_output = opts.name; | |
115 ghostscript_device = dev; | |
116 if (doprint) | |
117 ## If printing, use color postscript. | |
118 dev = "psc"; | |
119 opts.name = cstrcat (tmpnam, ".ps"); | |
120 else | |
121 ## If saving to a file, use color encapsulated postscript. | |
122 dev = "epsc"; | |
123 opts.name = cstrcat (tmpnam, ".eps"); | |
124 endif | |
125 elseif (doprint && all (! strcmpi (suffixes {strcmpi (dev_list, dev)}, | |
126 {"ps", "eps", "hpgl"}))) | |
127 ## When not using Ghostscript, verify the format is compatible with | |
128 ## hard copy output. | |
129 error ("print: missing file name, or invalid print format."); | |
130 else | |
131 ghostscript_output = ""; | |
132 endif | |
133 | |
134 termn = dev; | |
135 | |
136 ## SVG isn't actually a bitmap, but gnuplot treats its size option as it | |
137 ## does the bitmap terminals. | |
138 bitmap_devices = {"emf", "gif", "jpeg", "pbm", "png", "svg"}; | |
139 | |
140 if (any (strcmp (dev, {"ps", "ps2", "psc", "psc2", "epsc", "epsc2", ... | |
141 "eps", "eps2", "pstex", "pslatex", "epslatex", ... | |
142 "epslatexstandalone"}))) | |
143 | |
144 ## Various postscript options | |
145 if (any (strcmp (dev, {"pstex", "pslatex", "epslatex"}))) | |
146 options = ""; | |
147 elseif (strcmp (dev, "epslatexstandalone")) | |
148 if (__gnuplot_has_feature__ ("epslatexstandalone_terminal")) | |
149 termn = "epslatex"; | |
150 options = "standalone "; | |
151 else | |
152 error ("print: epslatexstandalone needs gnuplot 4.2 or higher"); | |
153 endif | |
154 else | |
155 if (dev(1) == "e") | |
156 options = "eps "; | |
157 else | |
158 options = ""; | |
159 endif | |
160 termn = "postscript"; | |
161 endif | |
162 | |
163 if (any (dev == "c") || opts.use_color > 0 | |
164 || (! isempty (strfind (dev, "tex")) && opts.use_color == 0)) | |
165 opts.use_color = 1; | |
166 else | |
167 opts.use_color = -1; | |
168 endif | |
169 | |
170 if (opts.use_color > 0) | |
171 if (opts.force_solid < 0) | |
172 options = cstrcat (options, "color dashed "); | |
173 else | |
174 options = cstrcat (options, "color solid "); | |
175 endif | |
176 else | |
177 if (opts.force_solid > 0) | |
178 options = cstrcat (options, "mono solid "); | |
179 else | |
180 options = cstrcat (options, "mono dashed "); | |
181 endif | |
182 endif | |
183 | |
184 if (! isempty (opts.font)) | |
185 options = cstrcat (options, "\"", opts.font, "\" "); | |
186 endif | |
187 if (! isempty (opts.fontsize)) | |
188 options = cstrcat (options, " ", opts.fontsize); | |
189 endif | |
190 | |
191 elseif (strcmp (dev, "tikz")) | |
192 if (! isempty (font) && ! isempty (opts.fontsize)) | |
193 options = cstrcat (options, "\"", opts.font, ",", opts.fontsize, "\" "); | |
194 elseif (! isempty (font)) | |
195 options = cstrcat (options, "\"", opts.font, "\" "); | |
196 else | |
197 options = ""; | |
198 endif | |
199 | |
200 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) | |
201 ## Adobe Illustrator, CorelDraw | |
202 if (opts.use_color >= 0) | |
203 options = " color"; | |
204 else | |
205 options = " mono"; | |
206 endif | |
207 if (! isempty (opts.font)) | |
208 options = cstrcat (options, " \"", opts.font, "\""); | |
209 endif | |
210 if (! isempty (opts.fontsize)) | |
211 options = cstrcat (options, " ", opts.fontsize); | |
212 endif | |
213 | |
214 elseif (strcmp (dev, "fig")) | |
215 ## XFig | |
216 options = opts.orientation; | |
217 if (opts.use_color >= 0) | |
218 options = " color"; | |
219 else | |
220 options = " mono"; | |
221 endif | |
222 options = cstrcat (options, " ", opts.special_flag); | |
223 if (! isempty (opts.fontsize)) | |
224 options = cstrcat (options, " fontsize ", opts.fontsize); | |
225 endif | |
226 | |
227 elseif (strcmp (dev, "emf")) | |
228 ## Enhanced Metafile format | |
229 options = " "; | |
230 if (opts.use_color >= 0) | |
231 options = " color"; | |
232 else | |
233 options = " mono"; | |
234 endif | |
235 if (opts.force_solid >= 0) | |
236 options = cstrcat (options, " solid"); | |
237 endif | |
238 if (! isempty (opts.font)) | |
239 options = cstrcat (options, " \"", opts.font, "\""); | |
240 endif | |
241 if (! isempty (opts.fontsize)) | |
242 options = cstrcat (options, " ", opts.fontsize); | |
243 endif | |
244 | |
245 elseif (any (strcmp (dev, bitmap_devices))) | |
246 | |
247 if (isempty (opts.canvas_size) && isempty (opts.resolution) | |
248 && any (strcmp (dev, {"pbm", "gif", "jpeg", "png"}))) | |
249 options = ""; | |
250 elseif (strcmp (dev, "svg")) | |
251 ## Referring to size, either "dynamic" or "fixed" | |
252 options = "fixed"; | |
253 else | |
254 options = ""; | |
255 end | |
256 if (! isempty (opts.canvas_size)) | |
257 options = sprintf ("%s size %d, %d", options, opts.canvas_size); | |
258 endif | |
259 | |
260 elseif (any (strcmp (dev, {"dxf", "mf", "hpgl"}))) | |
261 ## AutoCad DXF, METAFONT, HPGL | |
262 options = ""; | |
263 | |
264 elseif (strcmp (dev, "pdf")) | |
265 ## Portable Document format | |
266 options = " "; | |
267 if (opts.use_color >= 0) | |
268 options = "color"; | |
269 else | |
270 options = "mono"; | |
271 endif | |
272 if (opts.force_solid >= 0) | |
273 options = cstrcat (options, " solid"); | |
274 elseif (opts.force_solid < 0) | |
275 options = cstrcat (options, " dashed"); | |
276 endif | |
277 if (! isempty (opts.font)) | |
278 options = cstrcat (options, "\"", opts.font, "\" "); | |
279 endif | |
280 if (! isempty (opts.fontsize)) | |
281 options = cstrcat (options, " ", opts.fontsize); | |
282 endif | |
283 | |
284 endif | |
285 | |
286 if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS")) | |
287 available_terminals = __gnuplot_get_var__ (gcf, "GPVAL_TERMINALS"); | |
288 available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match"); | |
289 ## Favor the cairo terminals. | |
290 if (strcmp (termn, "pdf") | |
291 && any (strcmp (available_terminals, "pdfcairo"))) | |
292 termn = "pdfcairo"; | |
293 gnuplot_supports_term = true; | |
294 elseif (strcmp (termn, "png") | |
295 && any (strcmp (available_terminals, "pngcairo"))) | |
296 termn = "pngcairo"; | |
297 gnuplot_supports_term = true; | |
298 else | |
299 gnuplot_supports_term = any (strcmp (available_terminals, termn)); | |
300 endif | |
301 elseif (strcmp (termn, "pdf")) | |
302 ## Some Linux variants do not include a "pdf" capable gnuplot. | |
303 ## To be safe, use Ghostscript. | |
304 if (have_ghostscript) | |
305 gnuplot_supports_term = false; | |
306 ghostscript_device = "pdfwrite"; | |
307 else | |
308 gnuplot_supports_term = true; | |
309 endif | |
310 else | |
311 gnuplot_supports_term = true; | |
312 endif | |
313 | |
314 if (! gnuplot_supports_term) | |
315 if (strcmp (termn, "pdf")) | |
316 ## If there the installed gnuplot does not support pdf, use Ghostscript. | |
317 ghostscript_device = "pdfwrite"; | |
318 if (strfind (opts.name, ".pdf") == numel (opts.name) - 3) | |
319 ghostscript_output = opts.name; | |
320 else | |
321 ghostscript_output = strcat (opts.name, ".pdf"); | |
322 endif | |
323 name = cstrcat (tmpnam, ".ps"); | |
324 termn = "postscript"; | |
325 ## All "options" for pdf work for postscript as well. | |
326 else | |
327 error ("print: the device, \"%s\", is not available.", dev) | |
328 endif | |
329 endif | |
330 | |
331 is_eps_file = strncmp (dev, "eps", 3); | |
332 p.units = get (gcf, "units"); | |
333 p.paperunits = get (gcf, "paperunits"); | |
334 p.papersize = get (gcf, "papersize"); | |
335 p.paperposition = get (gcf, "paperposition"); | |
336 p.paperpositionmode = get (gcf, "paperpositionmode"); | |
337 p.paperorientation = get (gcf, "paperorientation"); | |
338 p.color = get (gcf, "color"); | |
339 if (p.papersize(1) > p.papersize(2)) | |
340 paperorientation = "landscape"; | |
341 else | |
342 paperorientation = "portrait"; | |
343 endif | |
344 if (! strcmp (paperorientation, get (gcf, "paperorientation")) | |
345 && warn_on_inconsistent_orientation) | |
346 msg = {"print.m - inconsistent papersize and paperorientation properties.\n", | |
347 sprintf(" papersize = %.2f, %.2f\n", p.papersize), | |
348 sprintf(" paperorientation = \"%s\"\n", p.paperorientation), | |
349 " the paperorientation property has been ignored"}; | |
350 warning ("%s",msg{:}) | |
351 warn_on_inconsistent_orientation = false; | |
352 endif | |
353 | |
354 if (strcmp (termn, "postscript") && ! strncmp (dev, "eps", 3)) | |
355 if (isempty (opts.orientation)) | |
356 opts.orientation = paperorientation; | |
357 endif | |
358 ## This is done here to accommodate ghostscript conversion. | |
359 options = cstrcat (opts.orientation, " ", options); | |
360 end | |
361 | |
362 new_terminal = cstrcat (termn, " ", options); | |
363 | |
364 mono = (opts.use_color < 0); | |
365 | |
366 terminals_for_prn = {"postscript", "pdf", "pdfcairo"}; | |
367 output_for_printer = any (strncmp (termn, terminals_for_prn, numel(termn))); | |
368 | |
369 if (isempty (opts.resolution)) | |
370 if (any (strcmp (dev, {"emf", "svg"})) || output_for_printer) | |
371 opts.resolution = get (0, "screenpixelsperinch"); | |
372 else | |
373 opts.resolution = 150; | |
374 endif | |
375 else | |
376 opts.resolution = str2num (opts.resolution); | |
377 if (opts.resolution == 0) | |
378 opts.resolution = get (0, "screenpixelsperinch"); | |
379 endif | |
380 endif | |
381 figure_properties = get (gcf); | |
382 if (! isfield (figure_properties, "__pixels_per_inch__")) | |
383 addproperty ("__pixels_per_inch__", gcf, "double", opts.resolution); | |
384 endif | |
385 set (gcf, "__pixels_per_inch__", opts.resolution) | |
386 | |
387 unwind_protect | |
388 set (gcf, "paperunits", "inches"); | |
389 set (gcf, "units", "pixels"); | |
390 set (gcf, "color", "none"); | |
391 restore_properties = true; | |
392 if ((! output_for_printer || is_eps_file) && ! doprint) | |
393 ## If not PDF or PostScript, and the result is not being sent to a printer, | |
394 ## render an image the size of the paperposition box. | |
395 ## Trigger the listener to convert all paper props to inches. | |
396 if (! isempty (opts.canvas_size)) | |
397 size_in_pixels = opts.canvas_size; | |
398 size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]); | |
399 papersize_in_inches = size_in_pixels ./ opts.resolution; | |
400 paperposition_in_inches = [0, 0, papersize_in_inches]; | |
401 else | |
402 paperposition_in_inches = get (gcf, "paperposition"); | |
403 paperposition_in_inches(1:2) = 0; | |
404 papersize_in_inches = paperposition_in_inches(3:4); | |
405 endif | |
406 set (gcf, "papersize", papersize_in_inches); | |
407 set (gcf, "paperposition", paperposition_in_inches); | |
408 set (gcf, "paperpositionmode", "manual"); | |
409 else | |
410 if (strcmp (p.paperpositionmode, "auto")) | |
411 size_in_pixels = get (gcf, "position")(3:4); | |
412 paperposition_in_inches(3:4) = size_in_pixels ./ opts.resolution; | |
413 paperposition_in_inches(1:2) = (p.papersize - paperposition_in_inches(3:4))/2; | |
414 else | |
415 paperposition_in_inches = p.paperposition; | |
416 endif | |
417 if (! isempty (opts.orientation) && ! strcmp (opts.orientation, paperorientation)) | |
418 ## When -landscape/portrait changes the orientation, flip both the | |
419 ## papersize and paperposition. | |
420 restore_properties = true; | |
421 set (gcf, "papersize", p.papersize([2, 1])); | |
422 set (gcf, "paperposition", paperposition_in_inches([2, 1, 4, 3])); | |
423 else | |
424 set (gcf, "paperposition", paperposition_in_inches); | |
425 endif | |
426 endif | |
427 if (opts.use_color < 0) | |
428 [objs_with_color, color_of_objs] = convert_color2mono (gcf); | |
429 endif | |
430 if (opts.append_to_file) | |
431 appended_file_name = opts.name; | |
432 if (index(termn, "pdf")) | |
433 opts.name = cstrcat (tmpnam, ".pdf"); | |
434 temp_name = cstrcat (tmpnam, ".pdf"); | |
435 ghostscript_device = "pdfwrite"; | |
436 else | |
437 opts.name = cstrcat (tmpnam, ".ps"); | |
438 temp_name = cstrcat (tmpnam, ".ps"); | |
439 ghostscript_device = "pswrite"; | |
440 endif | |
441 endif | |
442 if (opts.debug) | |
443 drawnow (new_terminal, opts.name, mono, opts.debug_file); | |
444 else | |
445 drawnow (new_terminal, opts.name, mono); | |
446 endif | |
447 if (opts.append_to_file) | |
448 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE"; | |
449 command = sprintf ("%s %s -sDEVICE=%s -sOutputFile=%s %s %s -q", ... | |
450 opts.ghostscript_binary, ghostscript_options, ghostscript_device, ... | |
451 temp_name, appended_file_name, opts.name); | |
452 status1 = system (command); | |
453 status2 = system (sprintf ("mv %s %s", temp_name, appended_file_name)); | |
454 if (status1 != 0 || status2 != 0) | |
455 error ("print: output failed to append to '%s'.", appended_file_name); | |
456 endif | |
457 endif | |
458 unwind_protect_cleanup | |
459 ## FIXME - it would be nice to delete "__pixels_per_inch__" property here. | |
460 if (restore_properties) | |
461 props = fieldnames (p); | |
462 for n = 1:numel(props) | |
463 set (gcf, props{n}, p.(props{n})) | |
464 endfor | |
465 endif | |
466 if (opts.use_color < 0) | |
467 convert_mono_to_or_from_color (objs_with_color, color_of_objs, false); | |
468 endif | |
469 end_unwind_protect | |
470 | |
471 if (! isempty (ghostscript_output)) | |
472 if (is_eps_file && opts.tight_flag) | |
473 ## If gnuplot's output is an eps-file then crop at the bounding box. | |
474 __fix_eps_bbox__ (name); | |
475 endif | |
476 ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4"; | |
477 if (is_eps_file) | |
478 ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options); | |
479 endif | |
480 if (isempty (strfind (lower (ghostscript_device), "write"))) | |
481 ## If output is a bitmap then include the resolution | |
482 ghostscript_options = sprintf ("%s -r%d", ghostscript_options, opts.resolution); | |
483 endif | |
484 ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options, | |
485 ghostscript_device); | |
486 command = sprintf ("\"%s\" %s -sOutputFile=\"%s\" \"%s\" 2>&1", | |
487 opts.ghostscript_binary, | |
488 ghostscript_options, ghostscript_output, opts.name); | |
489 [errcode, output] = system (command); | |
490 unlink (name); | |
491 if (errcode) | |
492 error ("print: Conversion failed, %s -> %s.\nError was:\n%s\n", | |
493 name, ghostscript_output, output); | |
494 endif | |
495 elseif (is_eps_file && opts.tight_flag && ! doprint) | |
496 ## If the saved output file is an eps file, use ghostscript to set a tight bbox. | |
497 ## This may result in a smaller or larger bbox geometry. | |
498 if (have_ghostscript) | |
499 __fix_eps_bbox__ (name); | |
500 endif | |
501 endif | |
502 | |
503 if (doprint) | |
504 if (isunix ()) | |
505 prn_opt = "-l"; | |
506 elseif (ispc ()) | |
507 prn_opt = "-o l"; | |
508 else | |
509 ## FIXME - besides Unix and Windows, what other OS's might be considered. | |
510 prn_opt = ""; | |
511 endif | |
512 if (isempty (opts.printer)) | |
513 prn_cmd = sprintf ("lpr %s '%s' 2>&1", prn_opt, printname); | |
514 else | |
515 prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", prn_opt, opts.printer, printname); | |
516 endif | |
517 [status, output] = system (prn_cmd); | |
518 if (status != 0) | 154 if (status != 0) |
519 disp (output) | 155 warning ("print.m: %s, '%s'.", output, file2unlink) |
520 warning ("print.m: printing failed.") | 156 endif |
521 endif | 157 endif |
522 [status, output] = unlink (printname); | 158 end_unwind_protect |
523 if (status != 0) | 159 |
524 disp (output) | 160 endfunction |
525 warning ("print.m: failed to delete temporay file, '%s'.", printname) | 161 |
526 endif | 162 function eps_drawnow (opts, epsfile, gp_opts) |
527 endif | 163 [h, fontsize] = get_figure_text_objs (opts); |
528 | 164 unwind_protect |
165 for n = 1:numel(h) | |
166 set (h, "fontsize", 2 * fontsize{n}); | |
167 endfor | |
168 local_drawnow (sprintf ("postscript eps %s", gp_opts), epsfile, opts); | |
169 if (opts.tight_flag) | |
170 __tight_eps_bbox__ (opts, epsfile); | |
171 endif | |
529 unwind_protect_cleanup | 172 unwind_protect_cleanup |
530 if (isfigure (old_fig)) | 173 for n = 1:numel(h) |
531 figure (old_fig) | 174 set (h, "fontsize", fontsize{n}); |
532 endif | 175 endfor |
533 end_unwind_protect | 176 end_unwind_protect |
534 | 177 endfunction |
535 endfunction | 178 |
536 | 179 function local_drawnow (term, file, opts) |
537 function [h, c] = convert_color2mono (hfig) | 180 if (opts.use_color < 0) |
538 unwind_protect | 181 mono = true; |
539 showhiddenhandles = get (0, "showhiddenhandles"); | 182 else |
540 set (0, "showhiddenhandles", "on"); | 183 mono = false; |
541 h.color = findobj (hfig, "-property", "color"); | 184 endif |
542 h.facecolor = findobj (hfig, "-property", "facecolor"); | 185 figure (opts.figure) |
543 h.edgecolor = findobj (hfig, "-property", "edgecolor"); | 186 if (isempty (opts.debug_file) || ! opts.debug) |
544 h.backgroundcolor = findobj (hfig, "-property", "backgroundcolor"); | 187 drawnow (term, file, mono); |
545 h.colormap = findobj (hfig, "-property", "colormap"); | 188 else |
546 unwind_protect_cleanup | 189 drawnow (term, file, mono, opts.debug_file); |
547 set (0, "showhiddenhandles", showhiddenhandles); | 190 endif |
548 end_unwind_protect | 191 endfunction |
549 f = fieldnames (h); | 192 |
550 for nf = 1:numel(f) | 193 function device_list = gs_based_devices (); |
551 if (! isempty (h.(f{nf}))) | 194 ## Aliases for other devices: "bmp", "png", "tiff", "tiffn", "pdf", |
552 v = get (h.(f{nf}), f{nf}); | 195 ## "ps", "ps2", "psc", "psc2" |
553 if (! iscell (v)) | 196 device_list = {"bmp16", "bmp16m", "bmp256", "bmp32b", "bmpgray", ... |
554 v = {v}; | 197 "jpeg", "jpegcymk", "jpeggray", "pbm", "pbmraw", ... |
555 endif | 198 "pcx16", "pcx24b", "pcx256", "pcx2up", "pcxcmyk", ... |
556 c.(f{nf}) = v; | 199 "pcxgray", "pcxmono", "pdfwrite", "pgm", "pgmraw", ... |
557 endif | 200 "pgnm", "pgnmraw", "png16", "png16m", "png256", ... |
201 "png48", "pngalpha", "pnggray", "pngmono", "pnm", ... | |
202 "pnmraw", "ppm", "ppmraw", "ps2write", "pswrite", ... | |
203 "tiff12nc", "tiff24nc", "tiff32nc", "tiffcrle", ... | |
204 "tiffg3", "tiffg32d", "tiffg4", "tiffgray", "tifflzw", ... | |
205 "tiffpack", "tiffsep", "bmp", "png", "tiff", "tiffn", ... | |
206 "pdf", "ps", "ps2", "psc", "psc2"}; | |
207 endfunction | |
208 | |
209 function f = font_spec (opts, varargin) | |
210 for n = 1:2:numel(varargin) | |
211 opts.(varargin{n}) = varargin{n+1}; | |
558 endfor | 212 endfor |
559 convert_mono_to_or_from_color (h, c, true) | 213 f = ""; |
560 endfunction | 214 switch opts.devopt |
561 | 215 case {"cgm"} |
562 function convert_mono_to_or_from_color (h, c, mono) | 216 if (! isempty (opts.font) && ! isempty (opts.fontsize)) |
563 f = fieldnames (h); | 217 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); |
564 for nf = 1:numel(f) | 218 elseif (! isempty (opts.font)) |
565 for nh = 1:numel (h.(f{nf})) | 219 f = sprintf ("font ""%s""", opts.font); |
566 color = c.(f{nf}){nh}; | 220 elseif (! isempty (opts.fontsize)) |
567 ## Ignore color == {"none", "flat", ...} | 221 f = sprintf ("%d", opts.fontsize); |
568 if (isfloat (color)) | 222 endif |
569 if (mono) | 223 case {"eps", "eps2", "epsc", "epsc2", gs_based_devices(){:}} |
570 ## Same method as used by rgb2gray in the image pkg. | 224 ## Gnuplot renders fonts as half their specification, which |
571 color = rgb2ntsc (color)(:,1) * ones (1, 3); | 225 ## results in a tight spacing for the axes-labels and tick-labels. |
572 endif | 226 ## Compensate for the half scale. This will produce the proper |
573 set (h.(f{nf})(nh), f{nf}, color); | 227 ## spacing for the requested fontsize. |
574 endif | 228 if (! isempty (opts.font) && ! isempty (opts.fontsize)) |
575 endfor | 229 f = sprintf ("font ""%s,%d""", opts.font, 2 * opts.fontsize); |
576 endfor | 230 elseif (! isempty (opts.font)) |
577 endfunction | 231 f = sprintf ("font ""%s""", opts.font); |
578 | 232 elseif (! isempty (opts.fontsize)) |
233 f = sprintf ("%d", 2 * opts.fontsize); | |
234 endif | |
235 case {"svg"} | |
236 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
237 fontsize = round (opts.fontsize * 0.75); | |
238 f = sprintf ("fname ""%s"" fsize %d", opts.font, fontsize); | |
239 elseif (! isempty (opts.font)) | |
240 f = sprintf ("fname ""%s""", opts.font); | |
241 elseif (! isempty (opts.fontsize)) | |
242 fontsize = round (opts.fontsize * 0.75); | |
243 f = sprintf ("%s fsize %d", f, fontsize); | |
244 endif | |
245 case {"pdf"} | |
246 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
247 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); | |
248 elseif (! isempty (opts.font)) | |
249 f = sprintf ("font ""%s""", opts.font); | |
250 elseif (! isempty (opts.fontsize)) | |
251 f = sprintf ("fsize %d", f, opts.fontsize); | |
252 endif | |
253 case {"pdfcairo", "pngcairo"} | |
254 if (! isempty (opts.font)) | |
255 f = sprintf ("font ""%s""", opts.font); | |
256 endif | |
257 case {"epslatex", "epslatexstandalone"} | |
258 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
259 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); | |
260 elseif (! isempty (opts.font)) | |
261 f = sprintf ("font ""%s""", opts.font); | |
262 elseif (! isempty (opts.fontsize)) | |
263 f = sprintf ("%d", opts.fontsize); | |
264 endif | |
265 case {"pslatex"} | |
266 if (! isempty (opts.fontsize)) | |
267 f = sprintf ("%d", opts.fontsize); | |
268 endif | |
269 case {"gif", "jpeg", "png"} | |
270 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
271 f = sprintf ("font ""%s ,%d""", opts.font, opts.fontsize); | |
272 elseif (! isempty (opts.font)) | |
273 f = sprintf ("font ""%s""", opts.font); | |
274 elseif (! isempty (opts.fontsize)) | |
275 f = sprintf ("font ""%d""", opts.fontsize); | |
276 endif | |
277 case {"emf"} | |
278 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
279 f = sprintf ("""%s"" %d", opts.font, opts.fontsize); | |
280 elseif (! isempty (opts.font)) | |
281 f = sprintf ("""%s""", opts.font); | |
282 elseif (! isempty (opts.fontsize)) | |
283 f = sprintf ("%d", opts.fontsize); | |
284 endif | |
285 case {"canvas"} | |
286 if (! isempty (opts.fontsize)) | |
287 f = sprintf ("fsize %d", opts.fontsize); | |
288 endif | |
289 case {"aifm", "corel"} | |
290 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
291 f = sprintf ("%s %d", opts.font, opts.fontsize); | |
292 elseif (! isempty (opts.font)) | |
293 f = sprintf ("%s", opts.font); | |
294 elseif (! isempty (opts.fontsize)) | |
295 f = sprintf ("%d", opts.fontsize); | |
296 endif | |
297 case {"fig"} | |
298 if (! isempty (opts.font) && ! isempty (opts.fontsize)) | |
299 f = sprintf ("font %s fontsize %d", opts.font, opts.fontsize); | |
300 elseif (! isempty (opts.font)) | |
301 f = sprintf ("font %s", opts.font); | |
302 elseif (! isempty (opts.fontsize)) | |
303 f = sprintf ("fontsize %d", opts.fontsize); | |
304 endif | |
305 endswitch | |
306 endfunction | |
307 | |
308 function [h, fontsize] = get_figure_text_objs (opts) | |
309 h = findall (opts.figure, "-property", "fontsize"); | |
310 fontsize = get (h, "fontsize"); | |
311 switch numel (fontsize) | |
312 case 0 | |
313 fontsize = {}; | |
314 case 1 | |
315 fontsize = {fontsize}; | |
316 endswitch | |
317 endfunction |