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