Mercurial > hg > octave-lyh
comparison scripts/plot/print.m @ 5362:ef053d682e42
[project @ 2005-05-23 19:21:54 by jwe]
author | jwe |
---|---|
date | Mon, 23 May 2005 19:21:54 +0000 |
parents | 0ac38aea9f76 |
children | 37f62a7778c2 |
comparison
equal
deleted
inserted
replaced
5361:0ac38aea9f76 | 5362:ef053d682e42 |
---|---|
1 ## Copyright (C) 1999 Daniel Heiserer | |
1 ## Copyright (C) 2001 Laurent Mazet | 2 ## Copyright (C) 2001 Laurent Mazet |
2 ## | 3 ## |
3 ## This program is free software; it is distributed in the hope that it | 4 ## This file is part of Octave. |
4 ## will be useful, but WITHOUT ANY WARRANTY; without even the implied | 5 ## |
5 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | 6 ## Octave is free software; you can redistribute it and/or modify it |
6 ## the GNU General Public License for more details. | 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. | |
7 ## | 15 ## |
8 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
9 ## along with this file; see the file COPYING. If not, write to the | 17 ## along with Octave; see the file COPYING. If not, write to the Free |
10 ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
11 ## 02111-1307, USA. | 19 ## 02110-1301, USA. |
12 ## | |
13 ## Copyright (C) 1999 Daniel Heiserer | |
14 | 20 |
15 ## -*- texinfo -*- | 21 ## -*- texinfo -*- |
16 ## @deftypefn {Function File} {} print (@var{filename}, @var{options}) | 22 ## @deftypefn {Function File} {} print (@var{filename}, @var{options}) |
17 ## | 23 ## |
18 ## Print a graph, or save it to a file | 24 ## Print a graph, or save it to a file |
82 ## @var{size} is given in points. @var{fontname} is ignored for the | 88 ## @var{size} is given in points. @var{fontname} is ignored for the |
83 ## fig device. | 89 ## fig device. |
84 ## @end table | 90 ## @end table |
85 ## | 91 ## |
86 ## The filename and options can be given in any order. | 92 ## The filename and options can be given in any order. |
87 ## | |
88 ## If you are using Octave 2.1.x or above, command("print") will change | |
89 ## print from a function to a command, so instead of typing | |
90 ## print("-FTimes-Roman:14", "-dashed", "-depsc", "out.ps") | |
91 ## you can type | |
92 ## print -FTimes-Roman:14 -dashed -depsc out.ps | |
93 ## | |
94 ## See also: orient, command | |
95 ## @end deftypefn | 93 ## @end deftypefn |
96 | 94 |
97 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> | 95 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
98 ## 2001-03-23 Laurent Mazet <mazet@crm.mot.com> | 96 ## Adapted-By: jwe |
99 ## * simplified interface: guess the device from the extension | 97 |
100 ## * font support | 98 ## PKG_ADD: mark_as_command print |
101 ## 2001-03-25 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | 99 |
102 ## * add unwind_protect | 100 function print (varargin) |
103 ## * use tmpnam to generate temporary name | |
104 ## * move "set term" before "set output" as required by gnuplot | |
105 ## * more options, and flexible options | |
106 ## 2001-03-29 Laurent Mazet <mazet@crm.mot.com> | |
107 ## * add solid and dashed options | |
108 ## * change PBMplus device | |
109 ## * add Corel device | |
110 ## * take care of the default terminal settings to restore them. | |
111 ## * add color, mono, dashed and solid support for printing and convert. | |
112 ## * add orientation for printing. | |
113 ## * create a .ps for printing (avoid some filtering problems). | |
114 ## * default printing is mono, default convert is color. | |
115 ## * add font size support. | |
116 ## 2001-03-30 Laurent Mazet <mazet@crm.mot.com> | |
117 ## * correct correl into corel | |
118 ## * delete a irrelevant test | |
119 ## * check for convert before choosing the ouput device | |
120 ## 2001-03-31 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
121 ## * use -Ffontname:size instead of -F"fontname size" | |
122 ## * add font size support to fig option | |
123 ## * update documentation | |
124 ## 2003-10-01 Laurent Mazet <mazet@crm.mot.com> | |
125 ## * clean documentation | |
126 | |
127 function print(varargin) | |
128 | 101 |
129 ## take care of the settings we had before | 102 ## take care of the settings we had before |
130 origterm = gget("terminal"); | 103 origterm = gget ("terminal"); |
131 origout = gget("output"); | 104 origout = gget ("output"); |
132 ## End of line trmination for __gnuplot_raw__ command strings. | 105 ## End of line trmination for __gnuplot_raw__ command strings. |
133 endl = ";\n"; | 106 endl = ";\n"; |
134 _automatic_replot = automatic_replot; | 107 _automatic_replot = automatic_replot; |
135 | 108 |
136 ## take care of the default terminal settings to restore them. | 109 ## take care of the default terminal settings to restore them. |
141 force_solid = 0; # 0=default, -1=dashed, +1=solid | 114 force_solid = 0; # 0=default, -1=dashed, +1=solid |
142 fontsize = font = name = devopt = printer = ""; | 115 fontsize = font = name = devopt = printer = ""; |
143 | 116 |
144 va_arg_cnt = 1; | 117 va_arg_cnt = 1; |
145 | 118 |
146 for i=1:nargin | 119 for i = 1:nargin |
147 arg = nth (varargin, va_arg_cnt++); | 120 arg = nth (varargin, va_arg_cnt++); |
148 if isstr(arg) | 121 if (isstr (arg)) |
149 if strcmp(arg, "-color") | 122 if (strcmp (arg, "-color")) |
150 use_color = 1; | 123 use_color = 1; |
151 elseif strcmp(arg, "-mono") | 124 elseif (strcmp (arg, "-mono")) |
152 use_color = -1; | 125 use_color = -1; |
153 elseif strcmp(arg, "-solid") | 126 elseif (strcmp (arg, "-solid")) |
154 force_solid = 1; | 127 force_solid = 1; |
155 elseif strcmp(arg, "-dashed") | 128 elseif (strcmp (arg, "-dashed")) |
156 force_solid = -1; | 129 force_solid = -1; |
157 elseif strcmp(arg, "-portrait") | 130 elseif (strcmp (arg, "-portrait")) |
158 orientation = "portrait"; | 131 orientation = "portrait"; |
159 elseif strcmp(arg, "-landscape") | 132 elseif (strcmp (arg, "-landscape")) |
160 orientation = "landscape"; | 133 orientation = "landscape"; |
161 elseif length(arg) > 2 && arg(1:2) == "-d" | 134 elseif (length (arg) > 2 && arg(1:2) == "-d") |
162 devopt = arg(3:length(arg)); | 135 devopt = arg(3:length(arg)); |
163 elseif length(arg) > 2 && arg(1:2) == "-P" | 136 elseif (length (arg) > 2 && arg(1:2) == "-P") |
164 printer = arg; | 137 printer = arg; |
165 elseif length(arg) > 2 && arg(1:2) == "-F" | 138 elseif (length (arg) > 2 && arg(1:2) == "-F") |
166 idx = rindex(arg, ":"); | 139 idx = rindex(arg, ":"); |
167 if (idx) | 140 if (idx) |
168 font = arg(3:idx-1); | 141 font = arg(3:idx-1); |
169 fontsize = arg(idx+1:length(arg)); | 142 fontsize = arg(idx+1:length(arg)); |
170 else | 143 else |
171 font = arg(3:length(arg)); | 144 font = arg(3:length(arg)); |
172 endif | 145 endif |
173 elseif length(arg) >= 1 && arg(1) == "-" | 146 elseif (length (arg) >= 1 && arg(1) == "-") |
174 error([ "print: unknown option ", arg ]); | 147 error ("print: unknown option `%s'", arg); |
175 elseif length(arg) > 0 | 148 elseif (length (arg) > 0) |
176 name = arg; | 149 name = arg; |
177 endif | 150 endif |
178 else | 151 else |
179 error("print: expects string options"); | 152 error ("print: expects string options"); |
180 endif | 153 endif |
181 endfor | 154 endfor |
182 | 155 |
183 doprint = isempty(name); | 156 doprint = isempty (name); |
184 if doprint | 157 if (doprint) |
185 if isempty(devopt) | 158 if (isempty (devopt)) |
186 printname = [ tmpnam, ".ps" ]; | 159 printname = [ tmpnam, ".ps" ]; |
187 else | 160 else |
188 printname = [ tmpnam, ".", devopt ]; | 161 printname = [ tmpnam, ".", devopt ]; |
189 endif | 162 endif |
190 name = printname; | 163 name = printname; |
191 endif | 164 endif |
192 | 165 |
193 if isempty(devopt) | 166 if (isempty (devopt)) |
194 dot = rindex(name, "."); | 167 dot = rindex (name, "."); |
195 if (dot == 0) | 168 if (dot == 0) |
196 error ("print: no format specified"); | 169 error ("print: no format specified"); |
197 else | 170 else |
198 dev = tolower(name(dot+1:length(name))); | 171 dev = tolower (name(dot+1:end)); |
199 endif | 172 endif |
200 else | 173 else |
201 dev = devopt; | 174 dev = devopt; |
202 endif | 175 endif |
203 | 176 |
204 if strcmp(dev, "ill") | 177 if (strcmp (dev, "ill")) |
205 dev = "aifm"; | 178 dev = "aifm"; |
206 elseif strcmp(dev, "cdr") | 179 elseif (strcmp (dev, "cdr")) |
207 dev = "corel"; | 180 dev = "corel"; |
208 endif | 181 endif |
209 | 182 |
210 ## check if we have to use convert | 183 ## check if we have to use convert |
211 dev_list = [" aifm corel fig png pbm dxf mf hpgl", ... | 184 dev_list = [" aifm corel fig png pbm dxf mf hpgl", ... |
212 " ps ps2 psc psc2 eps eps2 epsc epsc2 " ]; | 185 " ps ps2 psc psc2 eps eps2 epsc epsc2 " ]; |
213 convertname = ""; | 186 convertname = ""; |
214 if isempty(findstr(dev_list , [ " ", dev, " " ])) | 187 if (isempty (findstr (dev_list , [ " ", dev, " " ])) |
215 if !isempty(devopt) | 188 if (! isempty (devopt)) |
216 convertname = [ devopt ":" name ]; | 189 convertname = [ devopt ":" name ]; |
217 else | 190 else |
218 convertname = name; | 191 convertname = name; |
219 endif | 192 endif |
220 dev = "epsc"; | 193 dev = "epsc"; |
222 endif | 195 endif |
223 | 196 |
224 unwind_protect | 197 unwind_protect |
225 automatic_replot = 0; | 198 automatic_replot = 0; |
226 | 199 |
227 if strcmp(dev, "ps") || strcmp(dev, "ps2") ... | 200 if (strcmp (dev, "ps") || strcmp (dev, "ps2") ... |
228 || strcmp(dev, "psc") || strcmp(dev, "psc2") ... | 201 || strcmp (dev, "psc") || strcmp (dev, "psc2") |
229 || strcmp(dev, "epsc") || strcmp(dev, "epsc2") ... | 202 || strcmp (dev, "epsc") || strcmp (dev, "epsc2") |
230 || strcmp(dev, "eps") || strcmp(dev, "eps2") | 203 || strcmp (dev, "eps") || strcmp (dev, "eps2")) |
231 ## Various postscript options | 204 ## Various postscript options |
232 ## FIXME: Do we need this? DAS | 205 ## XXX FIXME XXX -- Do we need this? DAS |
233 ##__gnuplot_set__ term postscript | 206 ##__gnuplot_set__ term postscript |
234 terminal_default = gget ("terminal"); | 207 terminal_default = gget ("terminal"); |
235 | 208 |
236 if dev(1) == "e" | 209 if (dev(1) == "e") |
237 options = "eps "; | 210 options = "eps "; |
238 else | 211 else |
239 options = [ orientation, " " ]; | 212 options = [ orientation, " " ]; |
240 endif | 213 endif |
241 options = [ options, "enhanced " ]; | 214 options = [ options, "enhanced " ]; |
242 | 215 |
243 if any( dev == "c" ) || use_color > 0 | 216 if (any (dev == "c") || use_color > 0) |
244 if force_solid < 0 | 217 if (force_solid < 0) |
245 options = [ options, "color dashed " ]; | 218 options = [ options, "color dashed " ]; |
246 else | 219 else |
247 options = [ options, "color solid " ]; | 220 options = [ options, "color solid " ]; |
248 endif | 221 endif |
249 else | 222 else |
250 if force_solid > 0 | 223 if (force_solid > 0) |
251 options = [ options, "mono solid " ]; | 224 options = [ options, "mono solid " ]; |
252 else | 225 else |
253 options = [ options, "mono dashed " ]; | 226 options = [ options, "mono dashed " ]; |
254 endif | 227 endif |
255 endif | 228 endif |
256 | 229 |
257 if !isempty(font) | 230 if (! isempty (font)) |
258 options = [ options, "\"", font, "\" " ]; | 231 options = [ options, "\"", font, "\" " ]; |
259 endif | 232 endif |
260 if !isempty(fontsize) | 233 if (! isempty (fontsize)) |
261 options = [ options, " ", fontsize ]; | 234 options = [ options, " ", fontsize ]; |
262 endif | 235 endif |
263 | 236 |
264 __gnuplot_raw__ (["set term postscript ", options, endl]); | 237 __gnuplot_raw__ (["set term postscript ", options, endl]); |
265 | 238 |
266 | 239 |
267 elseif strcmp(dev, "aifm") || strcmp(dev, "corel") | 240 elseif (strcmp (dev, "aifm") || strcmp (dev, "corel")) |
268 ## Adobe Illustrator, CorelDraw | 241 ## Adobe Illustrator, CorelDraw |
269 ## FIXME: Do we need it? DAS | 242 ## FIXME: Do we need it? DAS |
270 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); | 243 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); |
271 terminal_default = gget ("terminal"); | 244 terminal_default = gget ("terminal"); |
272 if (use_color >= 0) | 245 if (use_color >= 0) |
273 options = " color"; | 246 options = " color"; |
274 else | 247 else |
275 options = " mono"; | 248 options = " mono"; |
276 endif | 249 endif |
277 if !isempty(font) | 250 if (! isempty (font)) |
278 options = [ options, " \"" , font, "\"" ]; | 251 options = [ options, " \"" , font, "\"" ]; |
279 endif | 252 endif |
280 if !isempty(fontsize) | 253 if (! isempty (fontsize)) |
281 options = [ options, " ", fontsize ]; | 254 options = [ options, " ", fontsize ]; |
282 endif | 255 endif |
283 | 256 |
284 __gnuplot_raw__ (["set term ", dev, " ", options, endl]); | 257 __gnuplot_raw__ (["set term ", dev, " ", options, endl]); |
285 | 258 |
286 elseif strcmp(dev, "fig") | 259 elseif (strcmp (dev, "fig")) |
287 ## XFig | 260 ## XFig |
288 ## FIXME: Do we need it? DAS | 261 ## FIXME: Do we need it? DAS |
289 ## __gnuplot_set__ term fig | 262 ## __gnuplot_set__ term fig |
290 terminal_default = gget ("terminal"); | 263 terminal_default = gget ("terminal"); |
291 options = orientation; | 264 options = orientation; |
292 if (use_color >= 0) | 265 if (use_color >= 0) |
293 options = " color"; | 266 options = " color"; |
294 else | 267 else |
295 options = " mono"; | 268 options = " mono"; |
296 endif | 269 endif |
297 if !isempty(fontsize) | 270 if (! isempty (fontsize)) |
298 options = [ options, " fontsize ", fontsize ]; | 271 options = [ options, " fontsize ", fontsize ]; |
299 endif | 272 endif |
300 __gnuplot_raw__ (["set term fig ", options, endl]); | 273 __gnuplot_raw__ (["set term fig ", options, endl]); |
301 | 274 |
302 elseif strcmp(dev, "png") || strcmp(dev, "pbm") | 275 elseif (strcmp (dev, "png") || strcmp (dev, "pbm")) |
303 ## Portable network graphics, PBMplus | 276 ## Portable network graphics, PBMplus |
304 ## FIXME: Do we need it? DAS | 277 ## FIXME: Do we need it? DAS |
305 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); | 278 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); |
306 terminal_default = gget ("terminal"); | 279 terminal_default = gget ("terminal"); |
307 | 280 |
308 ## FIXME | 281 ## XXX FIXME XXX -- New PNG interface takes color as "xRRGGBB" |
309 ## New PNG interface takes color as "xRRGGBB" where x is the literal character | 282 ## where x is the literal character 'x' and 'RRGGBB' are the red, |
310 ## 'x' and 'RRGGBB' are the red, green and blue components in hex. | 283 ## green and blue components in hex. For now we just ignore it |
311 ## For now we just ignore it and use default. | 284 ## and use default. The png terminal now is so rich with options, |
312 ## The png terminal now is so rich with options, that one perhaps | 285 ## that one perhaps has to write a separate printpng.m function. |
313 ## has to write a separate printpng.m function. | |
314 ## DAS | 286 ## DAS |
315 | 287 |
316 ## | |
317 ## if (use_color >= 0) | 288 ## if (use_color >= 0) |
318 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); | 289 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); |
319 ##else | 290 ##else |
320 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); | 291 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); |
321 ##endif | 292 ##endif |
322 | 293 |
323 __gnuplot_raw__ ("set term png large;\n") | 294 __gnuplot_raw__ ("set term png large;\n") |
324 | 295 |
325 elseif strcmp(dev,"dxf") || strcmp(dev,"mf") || strcmp(dev, "hpgl") | 296 elseif (strcmp (dev, "dxf") || strcmp (dev, "mf") || strcmp (dev, "hpgl")) |
326 ## AutoCad DXF, METAFONT, HPGL | 297 ## AutoCad DXF, METAFONT, HPGL |
327 __gnuplot_raw__ (["set terminal ", dev, endl]); | 298 __gnuplot_raw__ (["set terminal ", dev, endl]); |
328 | 299 endif |
329 endif; | |
330 | 300 |
331 ## Gnuplot expects " around output file name | 301 ## Gnuplot expects " around output file name |
332 | 302 |
333 __gnuplot_raw__ (["set output \"", name, "\"", endl]); | 303 __gnuplot_raw__ (["set output \"", name, "\"", endl]); |
334 __gnuplot_replot__ | 304 __gnuplot_replot__ |
335 | 305 |
336 unwind_protect_cleanup | 306 unwind_protect_cleanup |
337 | 307 |
338 ## Restore init state | 308 ## Restore init state |
339 if ! isempty (terminal_default) | 309 if (! isempty (terminal_default)) |
340 __gnuplot_raw__ (["set terminal ", terminal_default, endl]); | 310 __gnuplot_raw__ (["set terminal ", terminal_default, endl]); |
341 endif | 311 endif |
342 __gnuplot_raw__ (["set terminal ", origterm, endl]); | 312 __gnuplot_raw__ (["set terminal ", origterm, endl]); |
343 if isempty (origout) | 313 if (isempty (origout)) |
344 __gnuplot_raw__ ("set output;\n") | 314 __gnuplot_raw__ ("set output;\n") |
345 else | 315 else |
346 ## Gnuplot expects " around output file name | 316 ## Gnuplot expects " around output file name |
347 | |
348 __gnuplot_raw__ (["set output \"", origout, "\"", endl]); | 317 __gnuplot_raw__ (["set output \"", origout, "\"", endl]); |
349 end | 318 end |
350 __gnuplot_replot__ | 319 __gnuplot_replot__ |
351 | 320 |
352 automatic_replot = _automatic_replot ; | 321 automatic_replot = _automatic_replot ; |
353 | 322 |
354 end_unwind_protect | 323 end_unwind_protect |
355 | 324 |
356 if !isempty(convertname) | 325 if (! isempty (convertname)) |
357 command = [ "convert '", name, "' '", convertname, "'" ]; | 326 command = [ "convert '", name, "' '", convertname, "'" ]; |
358 [output, errcode] = system (command); | 327 [output, errcode] = system (command); |
359 unlink (name); | 328 unlink (name); |
360 if (errcode) | 329 if (errcode) |
361 error ("print: could not convert"); | 330 error ("print: could not convert"); |
362 endif | 331 endif |
363 endif | 332 endif |
364 ## FIXME: This looks like a dirty, Unix-specific hack | 333 |
334 ## XXX FIXME XXX -- This looks like a dirty, Unix-specific hack. | |
365 ## DAS | 335 ## DAS |
366 if doprint | 336 if (doprint) |
367 system(sprintf ("lpr %s '%s'", printer, printname)); | 337 system (sprintf ("lpr %s '%s'", printer, printname)); |
368 unlink(printname); | 338 unlink (printname); |
369 endif | 339 endif |
370 | 340 |
371 endfunction | 341 endfunction |