Mercurial > hg > octave-nkf
comparison scripts/plot/print.m @ 5361:0ac38aea9f76
[project @ 2005-05-23 19:08:36 by jwe]
author | jwe |
---|---|
date | Mon, 23 May 2005 19:08:36 +0000 |
parents | |
children | ef053d682e42 |
comparison
equal
deleted
inserted
replaced
5360:33adb987c033 | 5361:0ac38aea9f76 |
---|---|
1 ## Copyright (C) 2001 Laurent Mazet | |
2 ## | |
3 ## This program is free software; it is distributed in the hope that it | |
4 ## will be useful, but WITHOUT ANY WARRANTY; without even the implied | |
5 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
6 ## the GNU General Public License for more details. | |
7 ## | |
8 ## 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 | |
10 ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
11 ## 02111-1307, USA. | |
12 ## | |
13 ## Copyright (C) 1999 Daniel Heiserer | |
14 | |
15 ## -*- texinfo -*- | |
16 ## @deftypefn {Function File} {} print (@var{filename}, @var{options}) | |
17 ## | |
18 ## Print a graph, or save it to a file | |
19 ## | |
20 ## @var{filename} defines the file name of the output file. If no | |
21 ## filename is specified, output is sent to the printer. | |
22 ## | |
23 ## @var{options}: | |
24 ## @table @code | |
25 ## @item -P@var{printer} | |
26 ## Set the @var{printer} name to which the graph is sent if no | |
27 ## @var{filename} is specified. | |
28 ## @item -color | |
29 ## @itemx -mono | |
30 ## Monochrome or colour lines. | |
31 ## @item -solid | |
32 ## @itemx -dashed | |
33 ## Solid or dashed lines. | |
34 ## @item -portrait | |
35 ## @itemx -landscape | |
36 ## Plot orientation, as returned by "orient". | |
37 ## @item -d@var{device} | |
38 ## Output device, where @var{device} is one of: | |
39 ## @table @code | |
40 ## @item ps | |
41 ## @itemx ps2 | |
42 ## @itemx psc | |
43 ## @itemx psc2 | |
44 ## Postscript (level 1 and 2, mono and color) | |
45 ## @item eps | |
46 ## @itemx eps2 | |
47 ## @itemx epsc | |
48 ## @itemx epsc2 | |
49 ## Encapsulated postscript (level 1 and 2, mono and color) | |
50 ## @item ill | |
51 ## @itemx aifm | |
52 ## Adobe Illustrator | |
53 ## @item cdr | |
54 ## @itemx corel | |
55 ## CorelDraw | |
56 ## @item hpgl | |
57 ## HP plotter language | |
58 ## @item fig | |
59 ## XFig | |
60 ## @item dxf | |
61 ## AutoCAD | |
62 ## @item mf | |
63 ## Metafont | |
64 ## @item png | |
65 ## Portable network graphics | |
66 ## @item pbm | |
67 ## PBMplus | |
68 ## @end table | |
69 ## | |
70 ## Other devices are supported by "convert" from ImageMagick. Type | |
71 ## system("convert") to see what formats are available. | |
72 ## | |
73 ## If the device is omitted, it is inferred from the file extension, | |
74 ## or if there is no filename it is sent to the printer as postscript. | |
75 ## | |
76 ## @item -F@var{fontname} | |
77 ## @itemx -F@var{fontname}:@var{size} | |
78 ## @itemx -F:@var{size} | |
79 ## @var{fontname} set the postscript font (for use with postscript, | |
80 ## aifm, corel and fig). By default, 'Helvetica' is set for PS/Aifm, | |
81 ## and 'SwitzerlandLight' for Corel. It can also be 'Times-Roman'. | |
82 ## @var{size} is given in points. @var{fontname} is ignored for the | |
83 ## fig device. | |
84 ## @end table | |
85 ## | |
86 ## 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 | |
96 | |
97 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> | |
98 ## 2001-03-23 Laurent Mazet <mazet@crm.mot.com> | |
99 ## * simplified interface: guess the device from the extension | |
100 ## * font support | |
101 ## 2001-03-25 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
102 ## * add unwind_protect | |
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 | |
129 ## take care of the settings we had before | |
130 origterm = gget("terminal"); | |
131 origout = gget("output"); | |
132 ## End of line trmination for __gnuplot_raw__ command strings. | |
133 endl = ";\n"; | |
134 _automatic_replot = automatic_replot; | |
135 | |
136 ## take care of the default terminal settings to restore them. | |
137 terminal_default = ""; | |
138 | |
139 orientation = orient; | |
140 use_color = 0; # 0=default, -1=mono, +1=color | |
141 force_solid = 0; # 0=default, -1=dashed, +1=solid | |
142 fontsize = font = name = devopt = printer = ""; | |
143 | |
144 va_arg_cnt = 1; | |
145 | |
146 for i=1:nargin | |
147 arg = nth (varargin, va_arg_cnt++); | |
148 if isstr(arg) | |
149 if strcmp(arg, "-color") | |
150 use_color = 1; | |
151 elseif strcmp(arg, "-mono") | |
152 use_color = -1; | |
153 elseif strcmp(arg, "-solid") | |
154 force_solid = 1; | |
155 elseif strcmp(arg, "-dashed") | |
156 force_solid = -1; | |
157 elseif strcmp(arg, "-portrait") | |
158 orientation = "portrait"; | |
159 elseif strcmp(arg, "-landscape") | |
160 orientation = "landscape"; | |
161 elseif length(arg) > 2 && arg(1:2) == "-d" | |
162 devopt = arg(3:length(arg)); | |
163 elseif length(arg) > 2 && arg(1:2) == "-P" | |
164 printer = arg; | |
165 elseif length(arg) > 2 && arg(1:2) == "-F" | |
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 | |
173 elseif length(arg) >= 1 && arg(1) == "-" | |
174 error([ "print: unknown option ", arg ]); | |
175 elseif length(arg) > 0 | |
176 name = arg; | |
177 endif | |
178 else | |
179 error("print: expects string options"); | |
180 endif | |
181 endfor | |
182 | |
183 doprint = isempty(name); | |
184 if doprint | |
185 if isempty(devopt) | |
186 printname = [ tmpnam, ".ps" ]; | |
187 else | |
188 printname = [ tmpnam, ".", devopt ]; | |
189 endif | |
190 name = printname; | |
191 endif | |
192 | |
193 if isempty(devopt) | |
194 dot = rindex(name, "."); | |
195 if (dot == 0) | |
196 error ("print: no format specified"); | |
197 else | |
198 dev = tolower(name(dot+1:length(name))); | |
199 endif | |
200 else | |
201 dev = devopt; | |
202 endif | |
203 | |
204 if strcmp(dev, "ill") | |
205 dev = "aifm"; | |
206 elseif strcmp(dev, "cdr") | |
207 dev = "corel"; | |
208 endif | |
209 | |
210 ## check if we have to use convert | |
211 dev_list = [" aifm corel fig png pbm dxf mf hpgl", ... | |
212 " ps ps2 psc psc2 eps eps2 epsc epsc2 " ]; | |
213 convertname = ""; | |
214 if isempty(findstr(dev_list , [ " ", dev, " " ])) | |
215 if !isempty(devopt) | |
216 convertname = [ devopt ":" name ]; | |
217 else | |
218 convertname = name; | |
219 endif | |
220 dev = "epsc"; | |
221 name = [ tmpnam, ".eps" ]; | |
222 endif | |
223 | |
224 unwind_protect | |
225 automatic_replot = 0; | |
226 | |
227 if strcmp(dev, "ps") || strcmp(dev, "ps2") ... | |
228 || strcmp(dev, "psc") || strcmp(dev, "psc2") ... | |
229 || strcmp(dev, "epsc") || strcmp(dev, "epsc2") ... | |
230 || strcmp(dev, "eps") || strcmp(dev, "eps2") | |
231 ## Various postscript options | |
232 ## FIXME: Do we need this? DAS | |
233 ##__gnuplot_set__ term postscript | |
234 terminal_default = gget ("terminal"); | |
235 | |
236 if dev(1) == "e" | |
237 options = "eps "; | |
238 else | |
239 options = [ orientation, " " ]; | |
240 endif | |
241 options = [ options, "enhanced " ]; | |
242 | |
243 if any( dev == "c" ) || use_color > 0 | |
244 if force_solid < 0 | |
245 options = [ options, "color dashed " ]; | |
246 else | |
247 options = [ options, "color solid " ]; | |
248 endif | |
249 else | |
250 if force_solid > 0 | |
251 options = [ options, "mono solid " ]; | |
252 else | |
253 options = [ options, "mono dashed " ]; | |
254 endif | |
255 endif | |
256 | |
257 if !isempty(font) | |
258 options = [ options, "\"", font, "\" " ]; | |
259 endif | |
260 if !isempty(fontsize) | |
261 options = [ options, " ", fontsize ]; | |
262 endif | |
263 | |
264 __gnuplot_raw__ (["set term postscript ", options, endl]); | |
265 | |
266 | |
267 elseif strcmp(dev, "aifm") || strcmp(dev, "corel") | |
268 ## Adobe Illustrator, CorelDraw | |
269 ## FIXME: Do we need it? DAS | |
270 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); | |
271 terminal_default = gget ("terminal"); | |
272 if (use_color >= 0) | |
273 options = " color"; | |
274 else | |
275 options = " mono"; | |
276 endif | |
277 if !isempty(font) | |
278 options = [ options, " \"" , font, "\"" ]; | |
279 endif | |
280 if !isempty(fontsize) | |
281 options = [ options, " ", fontsize ]; | |
282 endif | |
283 | |
284 __gnuplot_raw__ (["set term ", dev, " ", options, endl]); | |
285 | |
286 elseif strcmp(dev, "fig") | |
287 ## XFig | |
288 ## FIXME: Do we need it? DAS | |
289 ## __gnuplot_set__ term fig | |
290 terminal_default = gget ("terminal"); | |
291 options = orientation; | |
292 if (use_color >= 0) | |
293 options = " color"; | |
294 else | |
295 options = " mono"; | |
296 endif | |
297 if !isempty(fontsize) | |
298 options = [ options, " fontsize ", fontsize ]; | |
299 endif | |
300 __gnuplot_raw__ (["set term fig ", options, endl]); | |
301 | |
302 elseif strcmp(dev, "png") || strcmp(dev, "pbm") | |
303 ## Portable network graphics, PBMplus | |
304 ## FIXME: Do we need it? DAS | |
305 ## eval(sprintf ("__gnuplot_set__ term %s", dev)); | |
306 terminal_default = gget ("terminal"); | |
307 | |
308 ## FIXME | |
309 ## New PNG interface takes color as "xRRGGBB" where x is the literal character | |
310 ## 'x' and 'RRGGBB' are the red, green and blue components in hex. | |
311 ## For now we just ignore it and use default. | |
312 ## The png terminal now is so rich with options, that one perhaps | |
313 ## has to write a separate printpng.m function. | |
314 ## DAS | |
315 | |
316 ## | |
317 ## if (use_color >= 0) | |
318 ## eval (sprintf ("__gnuplot_set__ term %s color medium", dev)); | |
319 ##else | |
320 ##eval (sprintf ("__gnuplot_set__ term %s mono medium", dev)); | |
321 ##endif | |
322 | |
323 __gnuplot_raw__ ("set term png large;\n") | |
324 | |
325 elseif strcmp(dev,"dxf") || strcmp(dev,"mf") || strcmp(dev, "hpgl") | |
326 ## AutoCad DXF, METAFONT, HPGL | |
327 __gnuplot_raw__ (["set terminal ", dev, endl]); | |
328 | |
329 endif; | |
330 | |
331 ## Gnuplot expects " around output file name | |
332 | |
333 __gnuplot_raw__ (["set output \"", name, "\"", endl]); | |
334 __gnuplot_replot__ | |
335 | |
336 unwind_protect_cleanup | |
337 | |
338 ## Restore init state | |
339 if ! isempty (terminal_default) | |
340 __gnuplot_raw__ (["set terminal ", terminal_default, endl]); | |
341 endif | |
342 __gnuplot_raw__ (["set terminal ", origterm, endl]); | |
343 if isempty (origout) | |
344 __gnuplot_raw__ ("set output;\n") | |
345 else | |
346 ## Gnuplot expects " around output file name | |
347 | |
348 __gnuplot_raw__ (["set output \"", origout, "\"", endl]); | |
349 end | |
350 __gnuplot_replot__ | |
351 | |
352 automatic_replot = _automatic_replot ; | |
353 | |
354 end_unwind_protect | |
355 | |
356 if !isempty(convertname) | |
357 command = [ "convert '", name, "' '", convertname, "'" ]; | |
358 [output, errcode] = system (command); | |
359 unlink (name); | |
360 if (errcode) | |
361 error ("print: could not convert"); | |
362 endif | |
363 endif | |
364 ## FIXME: This looks like a dirty, Unix-specific hack | |
365 ## DAS | |
366 if doprint | |
367 system(sprintf ("lpr %s '%s'", printer, printname)); | |
368 unlink(printname); | |
369 endif | |
370 | |
371 endfunction |