5362
|
1 ## Copyright (C) 1999 Daniel Heiserer |
5361
|
2 ## Copyright (C) 2001 Laurent Mazet |
|
3 ## |
5362
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 2, or (at your option) |
|
9 ## any later version. |
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
5361
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
5362
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
5361
|
20 |
|
21 ## -*- texinfo -*- |
|
22 ## @deftypefn {Function File} {} print (@var{filename}, @var{options}) |
|
23 ## |
|
24 ## Print a graph, or save it to a file |
|
25 ## |
|
26 ## @var{filename} defines the file name of the output file. If no |
|
27 ## filename is specified, output is sent to the printer. |
|
28 ## |
|
29 ## @var{options}: |
|
30 ## @table @code |
|
31 ## @item -P@var{printer} |
|
32 ## Set the @var{printer} name to which the graph is sent if no |
|
33 ## @var{filename} is specified. |
|
34 ## @item -color |
|
35 ## @itemx -mono |
|
36 ## Monochrome or colour lines. |
|
37 ## @item -solid |
|
38 ## @itemx -dashed |
|
39 ## Solid or dashed lines. |
|
40 ## @item -portrait |
|
41 ## @itemx -landscape |
|
42 ## Plot orientation, as returned by "orient". |
|
43 ## @item -d@var{device} |
|
44 ## Output device, where @var{device} is one of: |
|
45 ## @table @code |
|
46 ## @item ps |
|
47 ## @itemx ps2 |
|
48 ## @itemx psc |
|
49 ## @itemx psc2 |
|
50 ## Postscript (level 1 and 2, mono and color) |
|
51 ## @item eps |
|
52 ## @itemx eps2 |
|
53 ## @itemx epsc |
|
54 ## @itemx epsc2 |
|
55 ## Encapsulated postscript (level 1 and 2, mono and color) |
|
56 ## @item ill |
|
57 ## @itemx aifm |
|
58 ## Adobe Illustrator |
|
59 ## @item cdr |
|
60 ## @itemx corel |
|
61 ## CorelDraw |
|
62 ## @item hpgl |
|
63 ## HP plotter language |
|
64 ## @item fig |
|
65 ## XFig |
|
66 ## @item dxf |
|
67 ## AutoCAD |
|
68 ## @item mf |
|
69 ## Metafont |
|
70 ## @item png |
|
71 ## Portable network graphics |
|
72 ## @item pbm |
|
73 ## PBMplus |
5794
|
74 ## @item emf |
|
75 ## Microsoft Enhanced Metafile |
5361
|
76 ## @end table |
|
77 ## |
|
78 ## Other devices are supported by "convert" from ImageMagick. Type |
|
79 ## system("convert") to see what formats are available. |
|
80 ## |
|
81 ## If the device is omitted, it is inferred from the file extension, |
|
82 ## or if there is no filename it is sent to the printer as postscript. |
|
83 ## |
|
84 ## @item -F@var{fontname} |
|
85 ## @itemx -F@var{fontname}:@var{size} |
|
86 ## @itemx -F:@var{size} |
|
87 ## @var{fontname} set the postscript font (for use with postscript, |
|
88 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm, |
|
89 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'. |
|
90 ## @var{size} is given in points. @var{fontname} is ignored for the |
|
91 ## fig device. |
|
92 ## @end table |
|
93 ## |
|
94 ## The filename and options can be given in any order. |
5521
|
95 ## |
|
96 ## @strong{Note}, the print function does not work with gnuplot's |
|
97 ## muplitplot mode. This problem is a known bug, and it will be fixed |
|
98 ## in a future version of Octave, but there is no simple fix (or it |
|
99 ## would have been fixed long ago). You can work around the bug by |
|
100 ## setting the terminal type and output yourself, then executing the |
|
101 ## multiplot plotting commands. For example |
|
102 ## |
|
103 ## @example |
|
104 ## @group |
|
105 ## __gnuplot_set__ terminal postscript |
|
106 ## __gnuplot_set__ output "foo.ps" |
|
107 ## subplot (2, 1, 1); |
|
108 ## sombrero (21); |
|
109 ## subplot (2, 1, 2); |
|
110 ## sombrero (41); |
|
111 ## oneplot |
|
112 ## __gnuplot_set__ terminal x11 |
|
113 ## __gnuplot_set__ output |
|
114 ## @end group |
|
115 ## @end example |
|
116 ## |
|
117 ## will save both figures on a single page in the PostScript file |
|
118 ## @file{foo.ps}. |
5361
|
119 ## @end deftypefn |
|
120 |
|
121 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
5362
|
122 ## Adapted-By: jwe |
5361
|
123 |
5362
|
124 ## PKG_ADD: mark_as_command print |
|
125 |
|
126 function print (varargin) |
5361
|
127 |
5365
|
128 orientation = orient (); |
5361
|
129 use_color = 0; # 0=default, -1=mono, +1=color |
|
130 force_solid = 0; # 0=default, -1=dashed, +1=solid |
5363
|
131 fontsize = ""; |
|
132 font = ""; |
|
133 name = ""; |
|
134 devopt = ""; |
|
135 printer = ""; |
5361
|
136 |
5362
|
137 for i = 1:nargin |
5363
|
138 arg = varargin{i}; |
5443
|
139 if (ischar (arg)) |
5362
|
140 if (strcmp (arg, "-color")) |
5361
|
141 use_color = 1; |
5362
|
142 elseif (strcmp (arg, "-mono")) |
5361
|
143 use_color = -1; |
5362
|
144 elseif (strcmp (arg, "-solid")) |
5361
|
145 force_solid = 1; |
5362
|
146 elseif (strcmp (arg, "-dashed")) |
5361
|
147 force_solid = -1; |
5362
|
148 elseif (strcmp (arg, "-portrait")) |
5361
|
149 orientation = "portrait"; |
5362
|
150 elseif (strcmp (arg, "-landscape")) |
5361
|
151 orientation = "landscape"; |
5362
|
152 elseif (length (arg) > 2 && arg(1:2) == "-d") |
5361
|
153 devopt = arg(3:length(arg)); |
5362
|
154 elseif (length (arg) > 2 && arg(1:2) == "-P") |
5361
|
155 printer = arg; |
5362
|
156 elseif (length (arg) > 2 && arg(1:2) == "-F") |
5361
|
157 idx = rindex(arg, ":"); |
|
158 if (idx) |
|
159 font = arg(3:idx-1); |
|
160 fontsize = arg(idx+1:length(arg)); |
|
161 else |
|
162 font = arg(3:length(arg)); |
|
163 endif |
5362
|
164 elseif (length (arg) >= 1 && arg(1) == "-") |
|
165 error ("print: unknown option `%s'", arg); |
|
166 elseif (length (arg) > 0) |
5361
|
167 name = arg; |
|
168 endif |
|
169 else |
5362
|
170 error ("print: expects string options"); |
5361
|
171 endif |
|
172 endfor |
|
173 |
5362
|
174 doprint = isempty (name); |
|
175 if (doprint) |
|
176 if (isempty (devopt)) |
5363
|
177 printname = strcat (tmpnam, ".ps"); |
5361
|
178 else |
5363
|
179 printname = strcat (tmpnam, ".", devopt); |
5361
|
180 endif |
|
181 name = printname; |
|
182 endif |
|
183 |
5362
|
184 if (isempty (devopt)) |
|
185 dot = rindex (name, "."); |
5363
|
186 if (dot == 0) |
5361
|
187 error ("print: no format specified"); |
|
188 else |
5362
|
189 dev = tolower (name(dot+1:end)); |
5361
|
190 endif |
|
191 else |
|
192 dev = devopt; |
|
193 endif |
|
194 |
5362
|
195 if (strcmp (dev, "ill")) |
5361
|
196 dev = "aifm"; |
5362
|
197 elseif (strcmp (dev, "cdr")) |
5361
|
198 dev = "corel"; |
|
199 endif |
|
200 |
|
201 ## check if we have to use convert |
5363
|
202 dev_list = {"aifm" "corel" "fig" "png" "pbm" "dxf" "mf" "hpgl", ... |
5625
|
203 "ps" "ps2" "psc" "psc2" "eps" "eps2" "epsc" "epsc2" "emf"}; |
5361
|
204 convertname = ""; |
5363
|
205 idx = cellidx (dev_list, dev); |
|
206 if (! idx) |
5362
|
207 if (! isempty (devopt)) |
5363
|
208 convertname = strcat (devopt, ":", name); |
5361
|
209 else |
|
210 convertname = name; |
|
211 endif |
|
212 dev = "epsc"; |
5363
|
213 name = strcat (tmpnam, ".eps"); |
5361
|
214 endif |
5363
|
215 |
5361
|
216 unwind_protect |
|
217 |
5362
|
218 if (strcmp (dev, "ps") || strcmp (dev, "ps2") ... |
|
219 || strcmp (dev, "psc") || strcmp (dev, "psc2") |
|
220 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") |
|
221 || strcmp (dev, "eps") || strcmp (dev, "eps2")) |
5361
|
222 ## Various postscript options |
5362
|
223 if (dev(1) == "e") |
5361
|
224 options = "eps "; |
|
225 else |
5363
|
226 options = strcat (orientation, " "); |
5361
|
227 endif |
5363
|
228 options = strcat (options, "enhanced "); |
|
229 |
5362
|
230 if (any (dev == "c") || use_color > 0) |
|
231 if (force_solid < 0) |
5363
|
232 options = strcat (options, "color dashed "); |
5361
|
233 else |
5363
|
234 options = strcat (options, "color solid "); |
5361
|
235 endif |
|
236 else |
5362
|
237 if (force_solid > 0) |
5363
|
238 options = strcat (options, "mono solid "); |
5361
|
239 else |
5363
|
240 options = strcat (options, "mono dashed "); |
5361
|
241 endif |
|
242 endif |
|
243 |
5362
|
244 if (! isempty (font)) |
5363
|
245 options = strcat (options, "\"", font, "\" "); |
5361
|
246 endif |
5362
|
247 if (! isempty (fontsize)) |
5363
|
248 options = strcat (options, " ", fontsize); |
5361
|
249 endif |
|
250 |
5368
|
251 __gnuplot_raw__ ("set terminal push;\n"); |
|
252 __gnuplot_raw__ (sprintf ("set terminal postscript %s;\n", options)); |
5361
|
253 |
|
254 |
5362
|
255 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) |
5361
|
256 ## Adobe Illustrator, CorelDraw |
|
257 if (use_color >= 0) |
|
258 options = " color"; |
|
259 else |
|
260 options = " mono"; |
|
261 endif |
5362
|
262 if (! isempty (font)) |
5363
|
263 options = strcat (options, " \"", font, "\""); |
5361
|
264 endif |
5362
|
265 if (! isempty (fontsize)) |
5363
|
266 options = strcat (options, " ", fontsize); |
5361
|
267 endif |
|
268 |
5368
|
269 __gnuplot_raw__ ("set terminal push;\n"); |
|
270 __gnuplot_raw__ (sprintf ("set terminal %s %s;\n", dev, options)); |
5361
|
271 |
5362
|
272 elseif (strcmp (dev, "fig")) |
5361
|
273 ## XFig |
|
274 options = orientation; |
|
275 if (use_color >= 0) |
|
276 options = " color"; |
|
277 else |
|
278 options = " mono"; |
|
279 endif |
5362
|
280 if (! isempty (fontsize)) |
5363
|
281 options = strcat (options, " fontsize ", fontsize); |
5361
|
282 endif |
5368
|
283 __gnuplot_raw__ ("set terminal push;\n"); |
|
284 __gnuplot_raw__ (sprintf ("set terminal fig %s;\n", options)); |
5361
|
285 |
5625
|
286 elseif (strcmp (dev, "emf")) |
|
287 ## Enhanced Metafile format |
|
288 options = " "; |
|
289 if (use_color >= 0) |
|
290 options = " color"; |
|
291 else |
|
292 options = " mono"; |
|
293 endif |
|
294 if (force_solid >= 0) |
|
295 options = strcat (options, " solid"); |
|
296 endif |
|
297 if (! isempty (font)) |
|
298 options = strcat (options, " \"", font, "\""); |
|
299 endif |
|
300 if (! isempty (fontsize)) |
|
301 options = strcat (options, " ", fontsize); |
|
302 endif |
|
303 __gnuplot_raw__ ("set terminal push;\n"); |
|
304 __gnuplot_raw__ (sprintf ("set terminal emf %s;\n", options)); |
|
305 |
5362
|
306 elseif (strcmp (dev, "png") || strcmp (dev, "pbm")) |
5361
|
307 ## Portable network graphics, PBMplus |
|
308 |
5775
|
309 ## FIXME -- New PNG interface takes color as "xRRGGBB" |
5362
|
310 ## where x is the literal character 'x' and 'RRGGBB' are the red, |
|
311 ## green and blue components in hex. For now we just ignore it |
|
312 ## and use default. The png terminal now is so rich with options, |
|
313 ## that one perhaps has to write a separate printpng.m function. |
5361
|
314 ## DAS |
|
315 |
|
316 ## if (use_color >= 0) |
|
317 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); |
|
318 ##else |
|
319 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); |
|
320 ##endif |
5363
|
321 |
5368
|
322 __gnuplot_raw__ ("set terminal push;\n"); |
|
323 __gnuplot_raw__ ("set terminal png large;\n") |
5363
|
324 |
5362
|
325 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) |
5361
|
326 ## AutoCad DXF, METAFONT, HPGL |
5368
|
327 __gnuplot_raw__ ("set terminal push;\n"); |
|
328 __gnuplot_raw__ (sprintf ("set terminal %s;\n", dev)); |
5362
|
329 endif |
5363
|
330 |
5361
|
331 ## Gnuplot expects " around output file name |
|
332 |
5363
|
333 __gnuplot_raw__ (sprintf ("set output \"%s\";\n", name)); |
5361
|
334 __gnuplot_replot__ |
5363
|
335 |
5361
|
336 unwind_protect_cleanup |
|
337 |
|
338 ## Restore init state |
5363
|
339 __gnuplot_raw__ ("set terminal pop;\n"); |
|
340 __gnuplot_raw__ ("set output;\n") |
5361
|
341 |
|
342 end_unwind_protect |
|
343 |
5362
|
344 if (! isempty (convertname)) |
5363
|
345 command = sprintf ("convert '%s' '%s'", name, convertname); |
5659
|
346 [errcode, output] = system (command); |
5361
|
347 unlink (name); |
|
348 if (errcode) |
|
349 error ("print: could not convert"); |
|
350 endif |
|
351 endif |
5362
|
352 |
5775
|
353 ## FIXME -- This looks like a dirty, Unix-specific hack. |
5361
|
354 ## DAS |
5362
|
355 if (doprint) |
|
356 system (sprintf ("lpr %s '%s'", printer, printname)); |
|
357 unlink (printname); |
5361
|
358 endif |
5363
|
359 |
5361
|
360 endfunction |