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