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