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. |
|
93 ## @end deftypefn |
|
94 |
|
95 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
5362
|
96 ## Adapted-By: jwe |
5361
|
97 |
5362
|
98 ## PKG_ADD: mark_as_command print |
|
99 |
|
100 function print (varargin) |
5361
|
101 |
|
102 ## take care of the settings we had before |
5362
|
103 origterm = gget ("terminal"); |
|
104 origout = gget ("output"); |
5361
|
105 ## End of line trmination for __gnuplot_raw__ command strings. |
|
106 endl = ";\n"; |
|
107 _automatic_replot = automatic_replot; |
|
108 |
|
109 ## take care of the default terminal settings to restore them. |
|
110 terminal_default = ""; |
|
111 |
|
112 orientation = orient; |
|
113 use_color = 0; # 0=default, -1=mono, +1=color |
|
114 force_solid = 0; # 0=default, -1=dashed, +1=solid |
|
115 fontsize = font = name = devopt = printer = ""; |
|
116 |
|
117 va_arg_cnt = 1; |
|
118 |
5362
|
119 for i = 1:nargin |
5361
|
120 arg = nth (varargin, va_arg_cnt++); |
5362
|
121 if (isstr (arg)) |
|
122 if (strcmp (arg, "-color")) |
5361
|
123 use_color = 1; |
5362
|
124 elseif (strcmp (arg, "-mono")) |
5361
|
125 use_color = -1; |
5362
|
126 elseif (strcmp (arg, "-solid")) |
5361
|
127 force_solid = 1; |
5362
|
128 elseif (strcmp (arg, "-dashed")) |
5361
|
129 force_solid = -1; |
5362
|
130 elseif (strcmp (arg, "-portrait")) |
5361
|
131 orientation = "portrait"; |
5362
|
132 elseif (strcmp (arg, "-landscape")) |
5361
|
133 orientation = "landscape"; |
5362
|
134 elseif (length (arg) > 2 && arg(1:2) == "-d") |
5361
|
135 devopt = arg(3:length(arg)); |
5362
|
136 elseif (length (arg) > 2 && arg(1:2) == "-P") |
5361
|
137 printer = arg; |
5362
|
138 elseif (length (arg) > 2 && arg(1:2) == "-F") |
5361
|
139 idx = rindex(arg, ":"); |
|
140 if (idx) |
|
141 font = arg(3:idx-1); |
|
142 fontsize = arg(idx+1:length(arg)); |
|
143 else |
|
144 font = arg(3:length(arg)); |
|
145 endif |
5362
|
146 elseif (length (arg) >= 1 && arg(1) == "-") |
|
147 error ("print: unknown option `%s'", arg); |
|
148 elseif (length (arg) > 0) |
5361
|
149 name = arg; |
|
150 endif |
|
151 else |
5362
|
152 error ("print: expects string options"); |
5361
|
153 endif |
|
154 endfor |
|
155 |
5362
|
156 doprint = isempty (name); |
|
157 if (doprint) |
|
158 if (isempty (devopt)) |
5361
|
159 printname = [ tmpnam, ".ps" ]; |
|
160 else |
|
161 printname = [ tmpnam, ".", devopt ]; |
|
162 endif |
|
163 name = printname; |
|
164 endif |
|
165 |
5362
|
166 if (isempty (devopt)) |
|
167 dot = rindex (name, "."); |
5361
|
168 if (dot == 0) |
|
169 error ("print: no format specified"); |
|
170 else |
5362
|
171 dev = tolower (name(dot+1:end)); |
5361
|
172 endif |
|
173 else |
|
174 dev = devopt; |
|
175 endif |
|
176 |
5362
|
177 if (strcmp (dev, "ill")) |
5361
|
178 dev = "aifm"; |
5362
|
179 elseif (strcmp (dev, "cdr")) |
5361
|
180 dev = "corel"; |
|
181 endif |
|
182 |
|
183 ## check if we have to use convert |
|
184 dev_list = [" aifm corel fig png pbm dxf mf hpgl", ... |
|
185 " ps ps2 psc psc2 eps eps2 epsc epsc2 " ]; |
|
186 convertname = ""; |
5362
|
187 if (isempty (findstr (dev_list , [ " ", dev, " " ])) |
|
188 if (! isempty (devopt)) |
5361
|
189 convertname = [ devopt ":" name ]; |
|
190 else |
|
191 convertname = name; |
|
192 endif |
|
193 dev = "epsc"; |
|
194 name = [ tmpnam, ".eps" ]; |
|
195 endif |
|
196 |
|
197 unwind_protect |
|
198 automatic_replot = 0; |
|
199 |
5362
|
200 if (strcmp (dev, "ps") || strcmp (dev, "ps2") ... |
|
201 || strcmp (dev, "psc") || strcmp (dev, "psc2") |
|
202 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") |
|
203 || strcmp (dev, "eps") || strcmp (dev, "eps2")) |
5361
|
204 ## Various postscript options |
5362
|
205 ## XXX FIXME XXX -- Do we need this? DAS |
5361
|
206 ##__gnuplot_set__ term postscript |
|
207 terminal_default = gget ("terminal"); |
|
208 |
5362
|
209 if (dev(1) == "e") |
5361
|
210 options = "eps "; |
|
211 else |
|
212 options = [ orientation, " " ]; |
|
213 endif |
|
214 options = [ options, "enhanced " ]; |
|
215 |
5362
|
216 if (any (dev == "c") || use_color > 0) |
|
217 if (force_solid < 0) |
5361
|
218 options = [ options, "color dashed " ]; |
|
219 else |
|
220 options = [ options, "color solid " ]; |
|
221 endif |
|
222 else |
5362
|
223 if (force_solid > 0) |
5361
|
224 options = [ options, "mono solid " ]; |
|
225 else |
|
226 options = [ options, "mono dashed " ]; |
|
227 endif |
|
228 endif |
|
229 |
5362
|
230 if (! isempty (font)) |
5361
|
231 options = [ options, "\"", font, "\" " ]; |
|
232 endif |
5362
|
233 if (! isempty (fontsize)) |
5361
|
234 options = [ options, " ", fontsize ]; |
|
235 endif |
|
236 |
|
237 __gnuplot_raw__ (["set term postscript ", options, endl]); |
|
238 |
|
239 |
5362
|
240 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) |
5361
|
241 ## Adobe Illustrator, CorelDraw |
|
242 ## FIXME: Do we need it? DAS |
|
243 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); |
|
244 terminal_default = gget ("terminal"); |
|
245 if (use_color >= 0) |
|
246 options = " color"; |
|
247 else |
|
248 options = " mono"; |
|
249 endif |
5362
|
250 if (! isempty (font)) |
5361
|
251 options = [ options, " \"" , font, "\"" ]; |
|
252 endif |
5362
|
253 if (! isempty (fontsize)) |
5361
|
254 options = [ options, " ", fontsize ]; |
|
255 endif |
|
256 |
|
257 __gnuplot_raw__ (["set term ", dev, " ", options, endl]); |
|
258 |
5362
|
259 elseif (strcmp (dev, "fig")) |
5361
|
260 ## XFig |
|
261 ## FIXME: Do we need it? DAS |
|
262 ## __gnuplot_set__ term fig |
|
263 terminal_default = gget ("terminal"); |
|
264 options = orientation; |
|
265 if (use_color >= 0) |
|
266 options = " color"; |
|
267 else |
|
268 options = " mono"; |
|
269 endif |
5362
|
270 if (! isempty (fontsize)) |
5361
|
271 options = [ options, " fontsize ", fontsize ]; |
|
272 endif |
|
273 __gnuplot_raw__ (["set term fig ", options, endl]); |
|
274 |
5362
|
275 elseif (strcmp (dev, "png") || strcmp (dev, "pbm")) |
5361
|
276 ## Portable network graphics, PBMplus |
|
277 ## FIXME: Do we need it? DAS |
|
278 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); |
|
279 terminal_default = gget ("terminal"); |
|
280 |
5362
|
281 ## XXX FIXME XXX -- New PNG interface takes color as "xRRGGBB" |
|
282 ## where x is the literal character 'x' and 'RRGGBB' are the red, |
|
283 ## green and blue components in hex. For now we just ignore it |
|
284 ## and use default. The png terminal now is so rich with options, |
|
285 ## that one perhaps has to write a separate printpng.m function. |
5361
|
286 ## DAS |
|
287 |
|
288 ## if (use_color >= 0) |
|
289 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); |
|
290 ##else |
|
291 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); |
|
292 ##endif |
|
293 |
|
294 __gnuplot_raw__ ("set term png large;\n") |
|
295 |
5362
|
296 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) |
5361
|
297 ## AutoCad DXF, METAFONT, HPGL |
|
298 __gnuplot_raw__ (["set terminal ", dev, endl]); |
5362
|
299 endif |
5361
|
300 |
|
301 ## Gnuplot expects " around output file name |
|
302 |
|
303 __gnuplot_raw__ (["set output \"", name, "\"", endl]); |
|
304 __gnuplot_replot__ |
|
305 |
|
306 unwind_protect_cleanup |
|
307 |
|
308 ## Restore init state |
5362
|
309 if (! isempty (terminal_default)) |
5361
|
310 __gnuplot_raw__ (["set terminal ", terminal_default, endl]); |
|
311 endif |
|
312 __gnuplot_raw__ (["set terminal ", origterm, endl]); |
5362
|
313 if (isempty (origout)) |
5361
|
314 __gnuplot_raw__ ("set output;\n") |
|
315 else |
|
316 ## Gnuplot expects " around output file name |
|
317 __gnuplot_raw__ (["set output \"", origout, "\"", endl]); |
|
318 end |
|
319 __gnuplot_replot__ |
|
320 |
|
321 automatic_replot = _automatic_replot ; |
|
322 |
|
323 end_unwind_protect |
|
324 |
5362
|
325 if (! isempty (convertname)) |
5361
|
326 command = [ "convert '", name, "' '", convertname, "'" ]; |
|
327 [output, errcode] = system (command); |
|
328 unlink (name); |
|
329 if (errcode) |
|
330 error ("print: could not convert"); |
|
331 endif |
|
332 endif |
5362
|
333 |
|
334 ## XXX FIXME XXX -- This looks like a dirty, Unix-specific hack. |
5361
|
335 ## DAS |
5362
|
336 if (doprint) |
|
337 system (sprintf ("lpr %s '%s'", printer, printname)); |
|
338 unlink (printname); |
5361
|
339 endif |
|
340 |
|
341 endfunction |