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) |
6739
|
56 ## @item tex |
|
57 ## @itemx epslatex |
|
58 ## @itemx epslatexstandalone |
|
59 ## @itemx pstex |
|
60 ## @itemx pslatex |
|
61 ## Generate a LaTeX (or TeX) file for labels, and eps/ps for |
|
62 ## graphics. The file produced by @code{epslatexstandalone} can be |
|
63 ## processed directly by LaTeX. The other formats are intended to |
|
64 ## be included in a LaTeX (or TeX) document. The @code{tex} device |
|
65 ## is the same as the @code{epslatex} device. |
5361
|
66 ## @item ill |
|
67 ## @itemx aifm |
|
68 ## Adobe Illustrator |
|
69 ## @item cdr |
|
70 ## @itemx corel |
|
71 ## CorelDraw |
|
72 ## @item hpgl |
|
73 ## HP plotter language |
|
74 ## @item fig |
|
75 ## XFig |
|
76 ## @item dxf |
|
77 ## AutoCAD |
|
78 ## @item mf |
|
79 ## Metafont |
|
80 ## @item png |
|
81 ## Portable network graphics |
|
82 ## @item pbm |
|
83 ## PBMplus |
5794
|
84 ## @item emf |
|
85 ## Microsoft Enhanced Metafile |
5361
|
86 ## @end table |
|
87 ## |
|
88 ## Other devices are supported by "convert" from ImageMagick. Type |
|
89 ## system("convert") to see what formats are available. |
|
90 ## |
|
91 ## If the device is omitted, it is inferred from the file extension, |
|
92 ## or if there is no filename it is sent to the printer as postscript. |
|
93 ## |
|
94 ## @item -F@var{fontname} |
|
95 ## @itemx -F@var{fontname}:@var{size} |
|
96 ## @itemx -F:@var{size} |
|
97 ## @var{fontname} set the postscript font (for use with postscript, |
|
98 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm, |
|
99 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'. |
|
100 ## @var{size} is given in points. @var{fontname} is ignored for the |
|
101 ## fig device. |
|
102 ## @end table |
|
103 ## |
|
104 ## The filename and options can be given in any order. |
|
105 ## @end deftypefn |
|
106 |
|
107 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
5362
|
108 ## Adapted-By: jwe |
5361
|
109 |
5362
|
110 ## PKG_ADD: mark_as_command print |
|
111 |
|
112 function print (varargin) |
5361
|
113 |
5365
|
114 orientation = orient (); |
5361
|
115 use_color = 0; # 0=default, -1=mono, +1=color |
|
116 force_solid = 0; # 0=default, -1=dashed, +1=solid |
5363
|
117 fontsize = ""; |
|
118 font = ""; |
|
119 name = ""; |
|
120 devopt = ""; |
|
121 printer = ""; |
5361
|
122 |
5362
|
123 for i = 1:nargin |
5363
|
124 arg = varargin{i}; |
5443
|
125 if (ischar (arg)) |
5362
|
126 if (strcmp (arg, "-color")) |
5361
|
127 use_color = 1; |
5362
|
128 elseif (strcmp (arg, "-mono")) |
5361
|
129 use_color = -1; |
5362
|
130 elseif (strcmp (arg, "-solid")) |
5361
|
131 force_solid = 1; |
5362
|
132 elseif (strcmp (arg, "-dashed")) |
5361
|
133 force_solid = -1; |
5362
|
134 elseif (strcmp (arg, "-portrait")) |
5361
|
135 orientation = "portrait"; |
5362
|
136 elseif (strcmp (arg, "-landscape")) |
5361
|
137 orientation = "landscape"; |
5362
|
138 elseif (length (arg) > 2 && arg(1:2) == "-d") |
5361
|
139 devopt = arg(3:length(arg)); |
5362
|
140 elseif (length (arg) > 2 && arg(1:2) == "-P") |
5361
|
141 printer = arg; |
5362
|
142 elseif (length (arg) > 2 && arg(1:2) == "-F") |
5361
|
143 idx = rindex(arg, ":"); |
|
144 if (idx) |
|
145 font = arg(3:idx-1); |
|
146 fontsize = arg(idx+1:length(arg)); |
|
147 else |
|
148 font = arg(3:length(arg)); |
|
149 endif |
5362
|
150 elseif (length (arg) >= 1 && arg(1) == "-") |
|
151 error ("print: unknown option `%s'", arg); |
|
152 elseif (length (arg) > 0) |
5361
|
153 name = arg; |
|
154 endif |
|
155 else |
5362
|
156 error ("print: expects string options"); |
5361
|
157 endif |
|
158 endfor |
|
159 |
5362
|
160 doprint = isempty (name); |
|
161 if (doprint) |
|
162 if (isempty (devopt)) |
5363
|
163 printname = strcat (tmpnam, ".ps"); |
5361
|
164 else |
5363
|
165 printname = strcat (tmpnam, ".", devopt); |
5361
|
166 endif |
|
167 name = printname; |
|
168 endif |
|
169 |
5362
|
170 if (isempty (devopt)) |
|
171 dot = rindex (name, "."); |
5363
|
172 if (dot == 0) |
5361
|
173 error ("print: no format specified"); |
|
174 else |
5362
|
175 dev = tolower (name(dot+1:end)); |
5361
|
176 endif |
|
177 else |
|
178 dev = devopt; |
|
179 endif |
|
180 |
6727
|
181 if (strcmp (dev, "tex")) |
|
182 dev = "epslatex"; |
|
183 ## gnuplot 4.0 wants ".eps" in the output name |
|
184 if (compare_versions (__gnuplot_version__, "4.2", "<")) |
|
185 name = strcat (name(1:dot), "eps"); |
|
186 endif |
|
187 elseif (strcmp (dev, "ill")) |
5361
|
188 dev = "aifm"; |
5362
|
189 elseif (strcmp (dev, "cdr")) |
5361
|
190 dev = "corel"; |
|
191 endif |
|
192 |
|
193 ## check if we have to use convert |
6727
|
194 dev_list = {"aifm", "corel", "fig", "png", "pbm", "dxf", "mf", ... |
|
195 "hpgl", "ps", "ps2", "psc", "psc2", "eps", "eps2", ... |
|
196 "epsc", "epsc2", "emf", "pstex", "pslatex", ... |
|
197 "epslatex", "epslatexstandalone"}; |
5361
|
198 convertname = ""; |
6272
|
199 [idx, errmsg] = cellidx (dev_list, dev); |
5363
|
200 if (! idx) |
5362
|
201 if (! isempty (devopt)) |
5363
|
202 convertname = strcat (devopt, ":", name); |
5361
|
203 else |
|
204 convertname = name; |
|
205 endif |
|
206 dev = "epsc"; |
5363
|
207 name = strcat (tmpnam, ".eps"); |
5361
|
208 endif |
5363
|
209 |
6179
|
210 if (strcmp (dev, "ps") || strcmp (dev, "ps2") ... |
|
211 || strcmp (dev, "psc") || strcmp (dev, "psc2") |
|
212 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") |
6727
|
213 || strcmp (dev, "eps") || strcmp (dev, "eps2") |
|
214 || strcmp (dev, "pstex")|| strcmp (dev, "pslatex") |
|
215 || strcmp (dev, "epslatex") || strcmp (dev, "epslatexstandalone")) |
|
216 |
6179
|
217 ## Various postscript options |
6727
|
218 if (strcmp (dev, "pstex")|| strcmp (dev, "pslatex") |
|
219 || strcmp (dev, "epslatex")) |
|
220 termn = dev; |
|
221 options = ""; |
|
222 elseif (strcmp (dev, "epslatexstandalone")) |
|
223 if (compare_versions (__gnuplot_version__, "4.2", ">=")) |
|
224 termn = "epslatex"; |
|
225 options = "standalone "; |
|
226 else |
|
227 error ("print: epslatexstandalone needs gnuplot 4.2 or higher"); |
|
228 endif |
6179
|
229 else |
6727
|
230 if (dev(1) == "e") |
|
231 options = "eps "; |
|
232 else |
|
233 options = strcat (orientation, " "); |
|
234 endif |
|
235 options = strcat (options, "enhanced "); |
|
236 termn = "postscript"; |
6179
|
237 endif |
6727
|
238 |
6179
|
239 if (any (dev == "c") || use_color > 0) |
|
240 if (force_solid < 0) |
|
241 options = strcat (options, "color dashed "); |
5361
|
242 else |
6179
|
243 options = strcat (options, "color solid "); |
5361
|
244 endif |
6179
|
245 else |
|
246 if (force_solid > 0) |
|
247 options = strcat (options, "mono solid "); |
5625
|
248 else |
6179
|
249 options = strcat (options, "mono dashed "); |
5625
|
250 endif |
6179
|
251 endif |
6178
|
252 |
6179
|
253 if (! isempty (font)) |
|
254 options = strcat (options, "\"", font, "\" "); |
|
255 endif |
|
256 if (! isempty (fontsize)) |
|
257 options = strcat (options, " ", fontsize); |
|
258 endif |
6727
|
259 |
|
260 new_terminal = strcat (termn, " ", options); |
|
261 |
6179
|
262 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) |
|
263 ## Adobe Illustrator, CorelDraw |
|
264 if (use_color >= 0) |
|
265 options = " color"; |
|
266 else |
|
267 options = " mono"; |
|
268 endif |
|
269 if (! isempty (font)) |
|
270 options = strcat (options, " \"", font, "\""); |
|
271 endif |
|
272 if (! isempty (fontsize)) |
|
273 options = strcat (options, " ", fontsize); |
5362
|
274 endif |
5363
|
275 |
6179
|
276 new_terminal = strcat (dev, " ", options); |
5361
|
277 |
6179
|
278 elseif (strcmp (dev, "fig")) |
|
279 ## XFig |
|
280 options = orientation; |
|
281 if (use_color >= 0) |
|
282 options = " color"; |
|
283 else |
|
284 options = " mono"; |
|
285 endif |
|
286 if (! isempty (fontsize)) |
|
287 options = strcat (options, " fontsize ", fontsize); |
|
288 endif |
6178
|
289 |
6179
|
290 new_terminal = strcat ("fig ", options); |
5363
|
291 |
6179
|
292 elseif (strcmp (dev, "emf")) |
|
293 ## Enhanced Metafile format |
|
294 options = " "; |
|
295 if (use_color >= 0) |
|
296 options = " color"; |
|
297 else |
|
298 options = " mono"; |
|
299 endif |
|
300 if (force_solid >= 0) |
|
301 options = strcat (options, " solid"); |
|
302 endif |
|
303 if (! isempty (font)) |
|
304 options = strcat (options, " \"", font, "\""); |
|
305 endif |
|
306 if (! isempty (fontsize)) |
|
307 options = strcat (options, " ", fontsize); |
|
308 endif |
|
309 |
|
310 new_terminal = strcat ("emf ", options); |
5361
|
311 |
6179
|
312 elseif (strcmp (dev, "png") || strcmp (dev, "pbm")) |
|
313 ## Portable network graphics, PBMplus |
|
314 |
|
315 ## FIXME -- New PNG interface takes color as "xRRGGBB" |
|
316 ## where x is the literal character 'x' and 'RRGGBB' are the red, |
|
317 ## green and blue components in hex. For now we just ignore it |
|
318 ## and use default. The png terminal now is so rich with options, |
|
319 ## that one perhaps has to write a separate printpng.m function. |
|
320 ## DAS |
5361
|
321 |
6179
|
322 ## if (use_color >= 0) |
|
323 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); |
|
324 ##else |
|
325 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); |
|
326 ##endif |
6178
|
327 |
6179
|
328 new_terminal = "png large"; |
|
329 |
|
330 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) |
|
331 ## AutoCad DXF, METAFONT, HPGL |
|
332 new_terminal = dev; |
|
333 endif |
|
334 |
6257
|
335 drawnow (new_terminal, name); |
5361
|
336 |
5362
|
337 if (! isempty (convertname)) |
5363
|
338 command = sprintf ("convert '%s' '%s'", name, convertname); |
5659
|
339 [errcode, output] = system (command); |
5361
|
340 unlink (name); |
|
341 if (errcode) |
|
342 error ("print: could not convert"); |
|
343 endif |
|
344 endif |
5362
|
345 |
5775
|
346 ## FIXME -- This looks like a dirty, Unix-specific hack. |
5361
|
347 ## DAS |
5362
|
348 if (doprint) |
|
349 system (sprintf ("lpr %s '%s'", printer, printname)); |
|
350 unlink (printname); |
5361
|
351 endif |
5363
|
352 |
5361
|
353 endfunction |