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 |
|
74 ## @end table |
|
75 ## |
|
76 ## Other devices are supported by "convert" from ImageMagick. Type |
|
77 ## system("convert") to see what formats are available. |
|
78 ## |
|
79 ## If the device is omitted, it is inferred from the file extension, |
|
80 ## or if there is no filename it is sent to the printer as postscript. |
|
81 ## |
|
82 ## @item -F@var{fontname} |
|
83 ## @itemx -F@var{fontname}:@var{size} |
|
84 ## @itemx -F:@var{size} |
|
85 ## @var{fontname} set the postscript font (for use with postscript, |
|
86 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm, |
|
87 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'. |
|
88 ## @var{size} is given in points. @var{fontname} is ignored for the |
|
89 ## fig device. |
|
90 ## @end table |
|
91 ## |
|
92 ## The filename and options can be given in any order. |
5521
|
93 ## |
|
94 ## @strong{Note}, the print function does not work with gnuplot's |
|
95 ## muplitplot mode. This problem is a known bug, and it will be fixed |
|
96 ## in a future version of Octave, but there is no simple fix (or it |
|
97 ## would have been fixed long ago). You can work around the bug by |
|
98 ## setting the terminal type and output yourself, then executing the |
|
99 ## multiplot plotting commands. For example |
|
100 ## |
|
101 ## @example |
|
102 ## @group |
|
103 ## __gnuplot_set__ terminal postscript |
|
104 ## __gnuplot_set__ output "foo.ps" |
|
105 ## subplot (2, 1, 1); |
|
106 ## sombrero (21); |
|
107 ## subplot (2, 1, 2); |
|
108 ## sombrero (41); |
|
109 ## oneplot |
|
110 ## __gnuplot_set__ terminal x11 |
|
111 ## __gnuplot_set__ output |
|
112 ## @end group |
|
113 ## @end example |
|
114 ## |
|
115 ## will save both figures on a single page in the PostScript file |
|
116 ## @file{foo.ps}. |
5361
|
117 ## @end deftypefn |
|
118 |
|
119 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
5362
|
120 ## Adapted-By: jwe |
5361
|
121 |
5362
|
122 ## PKG_ADD: mark_as_command print |
|
123 |
|
124 function print (varargin) |
5361
|
125 |
5365
|
126 orientation = orient (); |
5361
|
127 use_color = 0; # 0=default, -1=mono, +1=color |
|
128 force_solid = 0; # 0=default, -1=dashed, +1=solid |
5363
|
129 fontsize = ""; |
|
130 font = ""; |
|
131 name = ""; |
|
132 devopt = ""; |
|
133 printer = ""; |
5361
|
134 |
5362
|
135 for i = 1:nargin |
5363
|
136 arg = varargin{i}; |
5443
|
137 if (ischar (arg)) |
5362
|
138 if (strcmp (arg, "-color")) |
5361
|
139 use_color = 1; |
5362
|
140 elseif (strcmp (arg, "-mono")) |
5361
|
141 use_color = -1; |
5362
|
142 elseif (strcmp (arg, "-solid")) |
5361
|
143 force_solid = 1; |
5362
|
144 elseif (strcmp (arg, "-dashed")) |
5361
|
145 force_solid = -1; |
5362
|
146 elseif (strcmp (arg, "-portrait")) |
5361
|
147 orientation = "portrait"; |
5362
|
148 elseif (strcmp (arg, "-landscape")) |
5361
|
149 orientation = "landscape"; |
5362
|
150 elseif (length (arg) > 2 && arg(1:2) == "-d") |
5361
|
151 devopt = arg(3:length(arg)); |
5362
|
152 elseif (length (arg) > 2 && arg(1:2) == "-P") |
5361
|
153 printer = arg; |
5362
|
154 elseif (length (arg) > 2 && arg(1:2) == "-F") |
5361
|
155 idx = rindex(arg, ":"); |
|
156 if (idx) |
|
157 font = arg(3:idx-1); |
|
158 fontsize = arg(idx+1:length(arg)); |
|
159 else |
|
160 font = arg(3:length(arg)); |
|
161 endif |
5362
|
162 elseif (length (arg) >= 1 && arg(1) == "-") |
|
163 error ("print: unknown option `%s'", arg); |
|
164 elseif (length (arg) > 0) |
5361
|
165 name = arg; |
|
166 endif |
|
167 else |
5362
|
168 error ("print: expects string options"); |
5361
|
169 endif |
|
170 endfor |
|
171 |
5362
|
172 doprint = isempty (name); |
|
173 if (doprint) |
|
174 if (isempty (devopt)) |
5363
|
175 printname = strcat (tmpnam, ".ps"); |
5361
|
176 else |
5363
|
177 printname = strcat (tmpnam, ".", devopt); |
5361
|
178 endif |
|
179 name = printname; |
|
180 endif |
|
181 |
5362
|
182 if (isempty (devopt)) |
|
183 dot = rindex (name, "."); |
5363
|
184 if (dot == 0) |
5361
|
185 error ("print: no format specified"); |
|
186 else |
5362
|
187 dev = tolower (name(dot+1:end)); |
5361
|
188 endif |
|
189 else |
|
190 dev = devopt; |
|
191 endif |
|
192 |
5362
|
193 if (strcmp (dev, "ill")) |
5361
|
194 dev = "aifm"; |
5362
|
195 elseif (strcmp (dev, "cdr")) |
5361
|
196 dev = "corel"; |
|
197 endif |
|
198 |
|
199 ## check if we have to use convert |
5363
|
200 dev_list = {"aifm" "corel" "fig" "png" "pbm" "dxf" "mf" "hpgl", ... |
|
201 "ps" "ps2" "psc" "psc2" "eps" "eps2" "epsc" "epsc2"}; |
5361
|
202 convertname = ""; |
5363
|
203 idx = cellidx (dev_list, dev); |
|
204 if (! idx) |
5362
|
205 if (! isempty (devopt)) |
5363
|
206 convertname = strcat (devopt, ":", name); |
5361
|
207 else |
|
208 convertname = name; |
|
209 endif |
|
210 dev = "epsc"; |
5363
|
211 name = strcat (tmpnam, ".eps"); |
5361
|
212 endif |
5363
|
213 |
5361
|
214 unwind_protect |
|
215 |
5362
|
216 if (strcmp (dev, "ps") || strcmp (dev, "ps2") ... |
|
217 || strcmp (dev, "psc") || strcmp (dev, "psc2") |
|
218 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") |
|
219 || strcmp (dev, "eps") || strcmp (dev, "eps2")) |
5361
|
220 ## Various postscript options |
5362
|
221 if (dev(1) == "e") |
5361
|
222 options = "eps "; |
|
223 else |
5363
|
224 options = strcat (orientation, " "); |
5361
|
225 endif |
5363
|
226 options = strcat (options, "enhanced "); |
|
227 |
5362
|
228 if (any (dev == "c") || use_color > 0) |
|
229 if (force_solid < 0) |
5363
|
230 options = strcat (options, "color dashed "); |
5361
|
231 else |
5363
|
232 options = strcat (options, "color solid "); |
5361
|
233 endif |
|
234 else |
5362
|
235 if (force_solid > 0) |
5363
|
236 options = strcat (options, "mono solid "); |
5361
|
237 else |
5363
|
238 options = strcat (options, "mono dashed "); |
5361
|
239 endif |
|
240 endif |
|
241 |
5362
|
242 if (! isempty (font)) |
5363
|
243 options = strcat (options, "\"", font, "\" "); |
5361
|
244 endif |
5362
|
245 if (! isempty (fontsize)) |
5363
|
246 options = strcat (options, " ", fontsize); |
5361
|
247 endif |
|
248 |
5368
|
249 __gnuplot_raw__ ("set terminal push;\n"); |
|
250 __gnuplot_raw__ (sprintf ("set terminal postscript %s;\n", options)); |
5361
|
251 |
|
252 |
5362
|
253 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) |
5361
|
254 ## Adobe Illustrator, CorelDraw |
|
255 if (use_color >= 0) |
|
256 options = " color"; |
|
257 else |
|
258 options = " mono"; |
|
259 endif |
5362
|
260 if (! isempty (font)) |
5363
|
261 options = strcat (options, " \"", font, "\""); |
5361
|
262 endif |
5362
|
263 if (! isempty (fontsize)) |
5363
|
264 options = strcat (options, " ", fontsize); |
5361
|
265 endif |
|
266 |
5368
|
267 __gnuplot_raw__ ("set terminal push;\n"); |
|
268 __gnuplot_raw__ (sprintf ("set terminal %s %s;\n", dev, options)); |
5361
|
269 |
5362
|
270 elseif (strcmp (dev, "fig")) |
5361
|
271 ## XFig |
|
272 options = orientation; |
|
273 if (use_color >= 0) |
|
274 options = " color"; |
|
275 else |
|
276 options = " mono"; |
|
277 endif |
5362
|
278 if (! isempty (fontsize)) |
5363
|
279 options = strcat (options, " fontsize ", fontsize); |
5361
|
280 endif |
5368
|
281 __gnuplot_raw__ ("set terminal push;\n"); |
|
282 __gnuplot_raw__ (sprintf ("set terminal fig %s;\n", options)); |
5361
|
283 |
5362
|
284 elseif (strcmp (dev, "png") || strcmp (dev, "pbm")) |
5361
|
285 ## Portable network graphics, PBMplus |
|
286 |
5362
|
287 ## XXX FIXME XXX -- New PNG interface takes color as "xRRGGBB" |
|
288 ## where x is the literal character 'x' and 'RRGGBB' are the red, |
|
289 ## green and blue components in hex. For now we just ignore it |
|
290 ## and use default. The png terminal now is so rich with options, |
|
291 ## that one perhaps has to write a separate printpng.m function. |
5361
|
292 ## DAS |
|
293 |
|
294 ## if (use_color >= 0) |
|
295 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); |
|
296 ##else |
|
297 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); |
|
298 ##endif |
5363
|
299 |
5368
|
300 __gnuplot_raw__ ("set terminal push;\n"); |
|
301 __gnuplot_raw__ ("set terminal png large;\n") |
5363
|
302 |
5362
|
303 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) |
5361
|
304 ## AutoCad DXF, METAFONT, HPGL |
5368
|
305 __gnuplot_raw__ ("set terminal push;\n"); |
|
306 __gnuplot_raw__ (sprintf ("set terminal %s;\n", dev)); |
5362
|
307 endif |
5363
|
308 |
5361
|
309 ## Gnuplot expects " around output file name |
|
310 |
5363
|
311 __gnuplot_raw__ (sprintf ("set output \"%s\";\n", name)); |
5361
|
312 __gnuplot_replot__ |
5363
|
313 |
5361
|
314 unwind_protect_cleanup |
|
315 |
|
316 ## Restore init state |
5363
|
317 __gnuplot_raw__ ("set terminal pop;\n"); |
|
318 __gnuplot_raw__ ("set output;\n") |
5361
|
319 __gnuplot_replot__ |
|
320 |
|
321 end_unwind_protect |
|
322 |
5362
|
323 if (! isempty (convertname)) |
5363
|
324 command = sprintf ("convert '%s' '%s'", name, convertname); |
5361
|
325 [output, errcode] = system (command); |
|
326 unlink (name); |
|
327 if (errcode) |
|
328 error ("print: could not convert"); |
|
329 endif |
|
330 endif |
5362
|
331 |
|
332 ## XXX FIXME XXX -- This looks like a dirty, Unix-specific hack. |
5361
|
333 ## DAS |
5362
|
334 if (doprint) |
|
335 system (sprintf ("lpr %s '%s'", printer, printname)); |
|
336 unlink (printname); |
5361
|
337 endif |
5363
|
338 |
5361
|
339 endfunction |