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