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