comparison scripts/plot/private/__gnuplot_print__.m @ 16108:3cce6b4e0f7c

Update gnuplot plotting scripts for faster or more modern synta * scripts/plot/__gnuplot_drawnow__.m: Use default values in function header. Check number of arguments to function immediately. Use single quotes to avoid lots of backslashing. Use strtok() to replace hand-coded functionality. Initialize persistent variables in declaration (10% faster). * scripts/plot/gnuplot_binary.in: Replace tabs with spaces. Simplify input validation. Add %!test block. * scripts/plot/private/__gnuplot_get_var__.m: Use default values in function header. Use "*char" in fread to automatically convert to char variable. * scripts/plot/private/__gnuplot_ginput__.m: Check immediately for required version of gnuplot. Use "*char" in fread to automatically convert to char variable. Use fputs in place of fprintf to match rest of code. * scripts/plot/private/__gnuplot_has_feature__.m: Initialize persistent varibles in declaration. Use false () rather than logical (zeros()) construction. * scripts/plot/private/__gnuplot_has_terminal__.m: Use strtok() to replace hand-coded functionality * scripts/plot/private/__gnuplot_print__.m: Replace sprintf calls with direct string char matrix concatenation (2.4x faster) where possible. Replace for loop with multiple argument form of set(). Use single quotes to avoid lots of backslashing. * scripts/plot/private/__gnuplot_version__.m: Use single quotes to avoid lots of backslashing.
author Rik <rik@octave.org>
date Mon, 25 Feb 2013 21:01:36 -0800
parents 049e8bbff782
children d3beeaf1d363
comparison
equal deleted inserted replaced
16107:3b791008b88e 16108:3cce6b4e0f7c
49 pipeline = ""; 49 pipeline = "";
50 50
51 switch (lower (opts.devopt)) 51 switch (lower (opts.devopt))
52 case {"eps", "eps2", "epsc", "epsc2"} 52 case {"eps", "eps2", "epsc", "epsc2"}
53 if (any (strcmp (opts.devopt, {"eps", "epsc"}))) 53 if (any (strcmp (opts.devopt, {"eps", "epsc"})))
54 gp_opts = sprintf ("%s level1", gp_opts); 54 gp_opts = [gp_opts " level1"];
55 endif 55 endif
56 if (opts.tight_flag || ! isempty (opts.preview)) 56 if (opts.tight_flag || ! isempty (opts.preview))
57 tmp_file = strcat (tmpnam (), ".eps"); 57 tmp_file = strcat (tmpnam (), ".eps");
58 eps_drawnow (opts, tmp_file, gp_opts); 58 eps_drawnow (opts, tmp_file, gp_opts);
59 if (dos_shell) 59 if (dos_shell)
60 cleanup = sprintf (" & del %s", strrep (tmp_file, '/', '\')); 60 cleanup = [" & del " strrep(tmp_file, '/', '\')];
61 else 61 else
62 cleanup = sprintf (" ; rm %s", tmp_file); 62 cleanup = [" ; rm " tmp_file];
63 endif 63 endif
64 pipeline = {sprintf("%s %s", 64 pipeline = {sprintf("%s %s",
65 opts.epstool_cmd (opts, tmp_file, opts.name), 65 opts.epstool_cmd (opts, tmp_file, opts.name),
66 cleanup)}; 66 cleanup)};
67 else 67 else
87 term = sprintf ("%s ", opts.devopt); 87 term = sprintf ("%s ", opts.devopt);
88 endif 88 endif
89 if (__gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix")) 89 if (__gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix"))
90 suffix = "tex"; 90 suffix = "tex";
91 else 91 else
92 %% Gnuplot 4.0 wants a ".eps" suffix. 92 ## Gnuplot 4.0 wants a ".eps" suffix.
93 suffix = "eps"; 93 suffix = "eps";
94 endif 94 endif
95 local_drawnow (sprintf ("%s %s", term, gp_opts), 95 local_drawnow ([term " " gp_opts],
96 strcat (name, ".", suffix), opts); 96 strcat (name, ".", suffix), opts);
97 case "tikz" 97 case "tikz"
98 if (__gnuplot_has_terminal__ ("tikz")) 98 if (__gnuplot_has_terminal__ ("tikz"))
99 local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, opts); 99 local_drawnow (["lua tikz " gp_opts], opts.name, opts);
100 else 100 else
101 error (sprintf ("print:no%soutput", opts.devopt), 101 error (sprintf ("print:no%soutput", opts.devopt),
102 "print.m: '%s' output is not available for gnuplot-%s", 102 "print.m: '%s' output is not available for gnuplot-%s",
103 upper (opts.devopt), __gnuplot_version__ ()); 103 upper (opts.devopt), __gnuplot_version__ ());
104 endif 104 endif
105 case "svg" 105 case "svg"
106 local_drawnow (sprintf ("svg dynamic %s", gp_opts), opts.name, opts); 106 local_drawnow (["svg dynamic " gp_opts], opts.name, opts);
107 case {"aifm", "corel", "eepic", "emf", "fig"} 107 case {"aifm", "corel", "eepic", "emf", "fig"}
108 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts); 108 local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
109 case {"pdfcairo", "pngcairo"} 109 case {"pdfcairo", "pngcairo"}
110 if (__gnuplot_has_terminal__ (opts.devopt)) 110 if (__gnuplot_has_terminal__ (opts.devopt))
111 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts); 111 local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
112 else 112 else
113 error (sprintf ("print:no%soutput", opts.devopt), 113 error (sprintf ("print:no%soutput", opts.devopt),
114 "print.m: '%s' output is not available for gnuplot-%s", 114 "print.m: '%s' output is not available for gnuplot-%s",
115 upper (opts.devopt), __gnuplot_version__ ()); 115 upper (opts.devopt), __gnuplot_version__ ());
116 endif 116 endif
117 case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"} 117 case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"}
118 local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts); 118 local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
119 case opts.ghostscript.device 119 case opts.ghostscript.device
120 gp_opts = font_spec (opts, "devopt", "eps"); 120 gp_opts = font_spec (opts, "devopt", "eps");
121 opts.ghostscript.output = opts.name; 121 opts.ghostscript.output = opts.name;
122 opts.ghostscript.source = strcat (tmpnam (), ".eps"); 122 opts.ghostscript.source = strcat (tmpnam (), ".eps");
123 eps_drawnow (opts, opts.ghostscript.source, gp_opts); 123 eps_drawnow (opts, opts.ghostscript.source, gp_opts);
124 [cmd_gs, cmd_cleanup] = __ghostscript__ (opts.ghostscript); 124 [cmd_gs, cmd_cleanup] = __ghostscript__ (opts.ghostscript);
125 if (opts.send_to_printer || isempty (opts.name)) 125 if (opts.send_to_printer || isempty (opts.name))
126 cmd_lpr = opts.lpr_cmd (opts); 126 cmd_lpr = opts.lpr_cmd (opts);
127 cmd = sprintf ("%s | %s", cmd_gs, cmd_lpr); 127 cmd = [cmd_gs " | " cmd_lpr];
128 else 128 else
129 cmd = sprintf ("%s", cmd_gs); 129 cmd = cmd_gs;
130 endif 130 endif
131 if (dos_shell) 131 if (dos_shell)
132 cmd = sprintf ("%s & del %s", cmd, strrep (opts.ghostscript.source, '/', '\')); 132 cmd = sprintf ("%s & del %s", cmd, strrep (opts.ghostscript.source, '/', '\'));
133 else 133 else
134 cmd = sprintf ("%s ; rm %s", cmd, opts.ghostscript.source); 134 cmd = sprintf ("%s ; rm %s", cmd, opts.ghostscript.source);
135 endif 135 endif
136 if (! isempty (cmd_cleanup)) 136 if (! isempty (cmd_cleanup))
137 if (dos_shell) 137 if (dos_shell)
138 pipeline = {sprintf("%s & %s", cmd, cmd_cleanup)}; 138 pipeline = {[cmd " & " cmd_cleanup]};
139 else 139 else
140 pipeline = {sprintf("%s ; %s", cmd, cmd_cleanup)}; 140 pipeline = {[cmd " ; " cmd_cleanup]};
141 endif 141 endif
142 else 142 else
143 pipeline = {cmd}; 143 pipeline = {cmd};
144 endif 144 endif
145 otherwise 145 otherwise
168 endfunction 168 endfunction
169 169
170 function eps_drawnow (opts, epsfile, gp_opts) 170 function eps_drawnow (opts, epsfile, gp_opts)
171 [h, fontsize] = get_figure_text_objs (opts); 171 [h, fontsize] = get_figure_text_objs (opts);
172 unwind_protect 172 unwind_protect
173 for n = 1:numel (h) 173 fontsize_2x = cellfun (@(x) 2*x, fontsize, "uniformoutput", false);
174 set (h(n), "fontsize", 2 * fontsize{n}); 174 set (h, {"fontsize"}, fontsize_2x);
175 endfor 175 local_drawnow (["postscript eps " gp_opts], epsfile, opts);
176 local_drawnow (sprintf ("postscript eps %s", gp_opts), epsfile, opts);
177 unwind_protect_cleanup 176 unwind_protect_cleanup
178 for n = 1:numel (h) 177 set (h, {"fontsize"}, fontsize);
179 set (h(n), "fontsize", fontsize{n});
180 endfor
181 end_unwind_protect 178 end_unwind_protect
182 endfunction 179 endfunction
183 180
184 function local_drawnow (term, file, opts) 181 function local_drawnow (term, file, opts)
185 if (opts.use_color < 0) 182 if (opts.use_color < 0)
201 endfor 198 endfor
202 f = ""; 199 f = "";
203 switch (opts.devopt) 200 switch (opts.devopt)
204 case "cgm" 201 case "cgm"
205 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 202 if (! isempty (opts.font) && ! isempty (opts.fontsize))
206 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); 203 f = sprintf ('font "%s,%d"', opts.font, opts.fontsize);
207 elseif (! isempty (opts.font)) 204 elseif (! isempty (opts.font))
208 f = sprintf ("font ""%s""", opts.font); 205 f = sprintf ('font "%s"', opts.font);
209 elseif (! isempty (opts.fontsize)) 206 elseif (! isempty (opts.fontsize))
210 f = sprintf ("%d", opts.fontsize); 207 f = sprintf ("%d", opts.fontsize);
211 endif 208 endif
212 case {"eps", "eps2", "epsc", "epsc2"} 209 case {"eps", "eps2", "epsc", "epsc2"}
213 ## Gnuplot renders fonts as half their specification, which 210 ## Gnuplot renders fonts as half their specification, which
214 ## results in a tight spacing for the axes-labels and tick-labels. 211 ## results in a tight spacing for the axes-labels and tick-labels.
215 ## Compensate for the half scale. This will produce the proper 212 ## Compensate for the half scale. This will produce the proper
216 ## spacing for the requested fontsize. 213 ## spacing for the requested fontsize.
217 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 214 if (! isempty (opts.font) && ! isempty (opts.fontsize))
218 f = sprintf ("font ""%s,%d""", opts.font, 2 * opts.fontsize); 215 f = sprintf ('font "%s,%d"', opts.font, 2 * opts.fontsize);
219 elseif (! isempty (opts.font)) 216 elseif (! isempty (opts.font))
220 f = sprintf ("font ""%s""", opts.font); 217 f = sprintf ('font "%s"', opts.font);
221 elseif (! isempty (opts.fontsize)) 218 elseif (! isempty (opts.fontsize))
222 f = sprintf ("%d", 2 * opts.fontsize); 219 f = sprintf ("%d", 2 * opts.fontsize);
223 endif 220 endif
224 case "svg" 221 case "svg"
225 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 222 if (! isempty (opts.font) && ! isempty (opts.fontsize))
226 fontsize = round (opts.fontsize * 0.75); 223 fontsize = round (opts.fontsize * 0.75);
227 f = sprintf ("fname ""%s"" fsize %d", opts.font, fontsize); 224 f = sprintf ('fname "%s" fsize %d', opts.font, fontsize);
228 elseif (! isempty (opts.font)) 225 elseif (! isempty (opts.font))
229 f = sprintf ("fname ""%s""", opts.font); 226 f = sprintf ('fname "%s"', opts.font);
230 elseif (! isempty (opts.fontsize)) 227 elseif (! isempty (opts.fontsize))
231 fontsize = round (opts.fontsize * 0.75); 228 fontsize = round (opts.fontsize * 0.75);
232 f = sprintf ("%s fsize %d", f, fontsize); 229 f = sprintf ("%s fsize %d", f, fontsize);
233 endif 230 endif
234 case "pdf" 231 case "pdf"
235 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 232 if (! isempty (opts.font) && ! isempty (opts.fontsize))
236 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); 233 f = sprintf ('font "%s,%d"', opts.font, opts.fontsize);
237 elseif (! isempty (opts.font)) 234 elseif (! isempty (opts.font))
238 f = sprintf ("font ""%s""", opts.font); 235 f = sprintf ('font "%s"', opts.font);
239 elseif (! isempty (opts.fontsize)) 236 elseif (! isempty (opts.fontsize))
240 f = sprintf ("fsize %d", f, opts.fontsize); 237 f = sprintf ("fsize %d", f, opts.fontsize);
241 endif 238 endif
242 case {"pdfcairo", "pngcairo"} 239 case {"pdfcairo", "pngcairo"}
243 if (! isempty (opts.font)) 240 if (! isempty (opts.font))
244 f = sprintf ("font ""%s""", opts.font); 241 f = sprintf ('font "%s"', opts.font);
245 endif 242 endif
246 case {"epslatex", "epslatexstandalone"} 243 case {"epslatex", "epslatexstandalone"}
247 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 244 if (! isempty (opts.font) && ! isempty (opts.fontsize))
248 f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize); 245 f = sprintf ('font "%s,%d"', opts.font, opts.fontsize);
249 elseif (! isempty (opts.font)) 246 elseif (! isempty (opts.font))
250 f = sprintf ("font ""%s""", opts.font); 247 f = sprintf ('font "%s"', opts.font);
251 elseif (! isempty (opts.fontsize)) 248 elseif (! isempty (opts.fontsize))
252 f = sprintf ("%d", opts.fontsize); 249 f = sprintf ("%d", opts.fontsize);
253 endif 250 endif
254 case "pslatex" 251 case "pslatex"
255 if (! isempty (opts.fontsize)) 252 if (! isempty (opts.fontsize))
256 f = sprintf ("%d", opts.fontsize); 253 f = sprintf ("%d", opts.fontsize);
257 endif 254 endif
258 case {"gif", "jpeg", "png"} 255 case {"gif", "jpeg", "png"}
259 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 256 if (! isempty (opts.font) && ! isempty (opts.fontsize))
260 f = sprintf ("font ""%s ,%d""", opts.font, opts.fontsize); 257 f = sprintf ('font "%s ,%d"', opts.font, opts.fontsize);
261 elseif (! isempty (opts.font)) 258 elseif (! isempty (opts.font))
262 f = sprintf ("font ""%s""", opts.font); 259 f = sprintf ('font "%s"', opts.font);
263 elseif (! isempty (opts.fontsize)) 260 elseif (! isempty (opts.fontsize))
264 f = sprintf ("font ""%d""", opts.fontsize); 261 f = sprintf ('font "%d"', opts.fontsize);
265 endif 262 endif
266 case "emf" 263 case "emf"
267 if (! isempty (opts.font) && ! isempty (opts.fontsize)) 264 if (! isempty (opts.font) && ! isempty (opts.fontsize))
268 f = sprintf ("""%s"" %d", opts.font, opts.fontsize); 265 f = sprintf ('"%s" %d', opts.font, opts.fontsize);
269 elseif (! isempty (opts.font)) 266 elseif (! isempty (opts.font))
270 f = sprintf ("""%s""", opts.font); 267 f = sprintf ('"%s"', opts.font);
271 elseif (! isempty (opts.fontsize)) 268 elseif (! isempty (opts.fontsize))
272 f = sprintf ("%d", opts.fontsize); 269 f = sprintf ("%d", opts.fontsize);
273 endif 270 endif
274 case "canvas" 271 case "canvas"
275 if (! isempty (opts.fontsize)) 272 if (! isempty (opts.fontsize))