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