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