# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1423097281 21600
# Node ID c9ed1d7815fddbf6df62e29af5c823a3d5972058
# Parent  ebd27d8c63fdadbe1a08dc57a582720420ecb00d
Fix bugs and make mods in print() gnuplot toolkit behavior. (bug #44187)

* __gnuplot_drawnow__.m (__gnuplot_drawnow__): Pass variable mono into
gnuplot_set_term().  Change "nargin == 5" to "nargin >= 5", for good measure.
(gnuplot_set_term): Accept 'mono' variable and supply 'mono' or 'color' to the
gnuplot command string when appropriate, making 'color' the default.  Change
use of nargin from "nargin == 5" to "nargin >= 5" where appropriate.

* __gnuplot_print__.m (__gnuplot_print__): If the formats/languages aliases,
i.e., postscript device with translation, "png16m" or "pdfwrite" are detected,
use a gnuplot terminal if possible before utilizing a postscript terminal.
(font_spec): Add a comma before the numerical fontsize specification string.

* __go_draw_axes__.m (create_fontspec): Add an empty string test to the
conditional for ",%d" case.
(get_fontname_and_size): Make an unknown/default font empty string, not "{}".

* __go_draw_figure__.m (__go_draw_figure__): Pass several variables through
round() before use in fprintf() for proper gnuplot command format.

* __print_parse_opts__.m (__print_parse_opts__): Fix several bugs where
strings indexed with {a:b} should be indexed with (a:b).  Make PostScript
file formats also use color by default.

diff --git a/scripts/plot/util/__gnuplot_drawnow__.m b/scripts/plot/util/__gnuplot_drawnow__.m
--- a/scripts/plot/util/__gnuplot_drawnow__.m
+++ b/scripts/plot/util/__gnuplot_drawnow__.m
@@ -39,11 +39,11 @@ function __gnuplot_drawnow__ (h, term, f
       plot_stream = __gnuplot_open_stream__ (2, h);
       gnuplot_supports_term = __gnuplot_has_terminal__ (term, plot_stream);
       if (gnuplot_supports_term)
-        enhanced = gnuplot_set_term (plot_stream(1), true, h, term, file);
+        enhanced = gnuplot_set_term (plot_stream(1), true, h, term, file, mono);
         __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
-        if (nargin == 5)
+        if (nargin >= 5)
           fid = fopen (debug_file, "wb");
-          enhanced = gnuplot_set_term (fid, true, h, term, file);
+          enhanced = gnuplot_set_term (fid, true, h, term, file, mono);
           __go_draw_figure__ (h, fid, enhanced, mono);
         endif
       else
@@ -79,9 +79,10 @@ function __gnuplot_drawnow__ (h, term, f
       ## popen2 eats stdout of gnuplot, use temporary file instead
       dumb_tmp_file = tempname ();
       enhanced = gnuplot_set_term (plot_stream(1), new_stream, h,
-                                   term, dumb_tmp_file);
+                                   term, dumb_tmp_file, mono);
     else
-      enhanced = gnuplot_set_term (plot_stream(1), new_stream, h, term);
+      enhanced = gnuplot_set_term (plot_stream(1), new_stream, h,
+                                   term, "", mono);
     endif
     __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
     fflush (plot_stream(1));
@@ -106,7 +107,7 @@ function __gnuplot_drawnow__ (h, term, f
 
 endfunction
 
-function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file)
+function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file, mono)
   ## Generate the gnuplot "set terminal <term> ..." command.
   ## When "term" originates from print.m, it may include other options.
   if (nargin < 4)
@@ -266,6 +267,23 @@ function enhanced = gnuplot_set_term (pl
 
     ## Set the gnuplot terminal (type, enhanced, title, options & size).
     term_str = ["set terminal " term];
+
+    ## These two subsets should be the same, but "eepic" is odd in the sense
+    ## it uses {color|dashed} rather than {color|mono}.
+    terminals_with_mono = {"cairolatex", "cgm", "context", "corel", ...
+                                    "emf", "epscairo", "epslatex", "fig", ...
+                           "mp", "pcl5", "pdfcairo", "pngcairo", "postscript", ...
+                           "pslatex", "pstex", "tgif", "tikz"};
+    terminals_with_color = {"cairolatex", "cgm", "context", "corel", ...
+                           "eepic", "emf", "epscairo", "epslatex", "fig", ...
+                           "mp", "pcl5", "pdfcairo", "pngcairo", "postscript", ...
+                           "pslatex", "pstex", "tgif", "tikz"};
+    if (mono && any (strcmp (term, terminals_with_mono)))
+      term_str = [term_str " mono"];
+    elseif (! mono && any (strcmp (term, terminals_with_color)))
+      term_str = [term_str " color"];
+    endif
+
     if (! isempty (enh_str))
       term_str = [term_str " " enh_str];
     endif
@@ -318,7 +336,7 @@ function enhanced = gnuplot_set_term (pl
         || have_non_legend_axes (h)
         || numel (findall (h, "type", "image")) > 0)
       fprintf (plot_stream, "%s\n", term_str);
-      if (nargin == 5)
+      if (nargin >= 5)
         if (! isempty (file))
           fprintf (plot_stream, "set output '%s';\n", file);
         endif
@@ -326,7 +344,7 @@ function enhanced = gnuplot_set_term (pl
       fputs (plot_stream, "set multiplot;\n");
     elseif (any (strcmp (term, flickering_terms)))
       fprintf (plot_stream, "%s\n", term_str);
-      if (nargin == 5)
+      if (nargin >= 5)
         if (! isempty (file))
           fprintf (plot_stream, "set output '%s';\n", file);
         endif
diff --git a/scripts/plot/util/private/__gnuplot_print__.m b/scripts/plot/util/private/__gnuplot_print__.m
--- a/scripts/plot/util/private/__gnuplot_print__.m
+++ b/scripts/plot/util/private/__gnuplot_print__.m
@@ -117,6 +117,20 @@ function opts = __gnuplot_print__ (opts)
     case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"}
       local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
     case opts.ghostscript.device
+      if (any (strcmp ({"png16m"}, opts.devopt)) && ...
+          __gnuplot_has_terminal__ ("png"))
+        opts.devopt = "png";
+        gp_opts = font_spec (opts);
+        local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
+        continue;
+      endif
+      if (any (strcmp ({"pdfwrite"}, opts.devopt)) && ...
+          __gnuplot_has_terminal__ ("pdfcairo"))
+        opts.devopt = "pdfcairo";
+        gp_opts = font_spec (opts);
+        local_drawnow ([opts.devopt " " gp_opts], opts.name, opts);
+        continue;
+      endif
       gp_opts = font_spec (opts, "devopt", "eps");
       opts.ghostscript.output = opts.name;
       opts.ghostscript.source = [tempname() ".eps"];
@@ -259,7 +273,7 @@ function f = font_spec (opts, varargin)
       elseif (! isempty (opts.font))
         f = sprintf ('font "%s"', opts.font);
       elseif (! isempty (opts.fontsize))
-        f = sprintf ('font "%d"', opts.fontsize);
+        f = sprintf ('font ",%d"', opts.fontsize);
       endif
     case "emf"
       if (! isempty (opts.font) && ! isempty (opts.fontsize))
diff --git a/scripts/plot/util/private/__go_draw_axes__.m b/scripts/plot/util/private/__go_draw_axes__.m
--- a/scripts/plot/util/private/__go_draw_axes__.m
+++ b/scripts/plot/util/private/__go_draw_axes__.m
@@ -1788,7 +1788,7 @@ function spacing_spec = create_spacingsp
 endfunction
 
 function fontspec = create_fontspec (f, s, gp_term)
-  if (strcmp (f, "*") || strcmp (gp_term, "tikz"))
+  if (isempty (f) || strcmp (f, "*") || strcmp (gp_term, "tikz"))
     fontspec = sprintf ("font \",%d\"", s);
   else
     fontspec = sprintf ("font \"%s,%d\"", f, s);
@@ -2241,7 +2241,7 @@ endfunction
 
 function [f, s, fnt, it, bld] = get_fontname_and_size (t)
   if (isempty (t.fontname) || strcmp (t.fontname, "*"))
-    fnt = "{}";
+    fnt = "";
   else
     fnt = t.fontname;
   endif
diff --git a/scripts/plot/util/private/__go_draw_figure__.m b/scripts/plot/util/private/__go_draw_figure__.m
--- a/scripts/plot/util/private/__go_draw_figure__.m
+++ b/scripts/plot/util/private/__go_draw_figure__.m
@@ -38,7 +38,7 @@ function __go_draw_figure__ (h, plot_str
       fputs (plot_stream, "set size 1, 1\n");
       bg = get (h, "color");
       if (isnumeric (bg))
-        fprintf (plot_stream, "set obj 1 rectangle from screen 0,0 to screen 1,1 behind fc rgb \"#%02x%02x%02x\"\n", 255 * bg);
+        fprintf (plot_stream, "set obj 1 rectangle from screen 0,0 to screen 1,1 behind fc rgb \"#%02x%02x%02x\"\n", round (255 * bg));
         bg_is_set = true;
       else
         bg_is_set = false;
@@ -114,7 +114,7 @@ function __go_draw_figure__ (h, plot_str
                     endif
                   endfor
                   if (bg_is_set)
-                    fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", 255 * (1 - bg));
+                    fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", round (255 * (1 - bg)));
                   endif
                   __go_draw_axes__ (kids(i), plot_stream, enhanced, mono,
                                     bg_is_set, false, hlgnd);
@@ -140,7 +140,7 @@ function __go_draw_figure__ (h, plot_str
                 set (kids(i), "units", "normalized");
                 fg = get (kids(i), "color");
                 if (isnumeric (fg) && strcmp (get (kids(i), "visible"), "on"))
-                  fprintf (plot_stream, "set obj 2 rectangle from graph 0,0 to graph 1,1 behind fc rgb \"#%02x%02x%02x\"\n", 255 * fg);
+                  fprintf (plot_stream, "set obj 2 rectangle from graph 0,0 to graph 1,1 behind fc rgb \"#%02x%02x%02x\"\n", round (255 * fg));
                   fg_is_set = true;
                   fg_was_set = true;
                 elseif (fg_was_set)
@@ -151,7 +151,7 @@ function __go_draw_figure__ (h, plot_str
                   fg_is_set = false;
                 endif
                 if (bg_is_set)
-                  fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", 255 * (1 - bg));
+                  fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", round (255 * (1 - bg)));
                 endif
                 ## Find if this axes has an associated legend axes and pass it
                 ## to __go_draw_axes__
diff --git a/scripts/plot/util/private/__print_parse_opts__.m b/scripts/plot/util/private/__print_parse_opts__.m
--- a/scripts/plot/util/private/__print_parse_opts__.m
+++ b/scripts/plot/util/private/__print_parse_opts__.m
@@ -115,11 +115,11 @@ function arg_st = __print_parse_opts__ (
       elseif (length (arg) > 2 && arg(1:2) == "-P")
         arg_st.printer = arg;
       elseif (strncmp (arg, "-EPSTOOL:", 9))
-        arg_st.epstool_binary = arg{10:end};
+        arg_st.epstool_binary = arg(10:end);
       elseif (strncmp (arg, "-FIG2DEV:", 9))
-        arg_st.fig2dev_binary = arg{10:end};
+        arg_st.fig2dev_binary = arg(10:end);
       elseif (strncmp (arg, "-PSTOEDIT:", 9))
-        arg_st.pstoedit_binary = arg{10:end};
+        arg_st.pstoedit_binary = arg(10:end);
       elseif (strncmpi (arg, "-textalphabits=", 15))
         n = find (arg == "=");
         if (! isempty (n) && n == numel (arg) - 1 && any (arg(end) == "124"))
@@ -196,11 +196,7 @@ function arg_st = __print_parse_opts__ (
   endif
 
   if (arg_st.use_color == 0)
-    if (any (strcmp ({"ps", "ps2", "eps", "eps2"}, arg_st.devopt)))
-      arg_st.use_color = -1;
-    else
-      arg_st.use_color = 1;
-    endif
+    arg_st.use_color = 1;
   endif
 
   if (strcmp (arg_st.devopt, "tex"))
exporting patch:
<fdopen>
