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

* __gnuplot_drawnow__.m (__gnuplot_drawnow__): Pass variable "mono" into
gnuplot_set_term() and return variable "monoterm".  Pass "mono && !monoterm",
i.e., simulated grayscale, into __go_draw_figure__() along with monoterm.
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.  Return
"monoterm" indicating if gnuplot does translation.  Change "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 conversion.
(font_spec): Add a comma before the numerical fontsize specification string.

* __go_draw_axes__.m (__go_draw_axes__): Drop "mono" case, and always include
color description in gnuplot command.  Replace round(colorspec) with
rgbc(colorspec, mono).  Remove redundant !mono in conditional test for
markerfacecolor and markeredgecolor.
(create_fontspec): Add an empty string test to the conditional for ",%d" case.
(do_linestyle_command): Remove redundant !mono in conditional test for
markerfacecolor and markeredgecolor.  Drop "mono" case, and always include
color description.  Replace round(colorspec) with rgbc(colorspec, mono).
(get_text_colorspec): Drop "mono" case, and always include color description.
Replace round(colorspec) with rgbc(colorspec, mono).
(get_fontname_and_size): Make unknown/default font an empty string, not "{}".
(rgbc): Added.  Map colorvec to gray if directed and round.

* __go_draw_figure__.m (__go_draw_figure__): Add "monoterm" as input variable.
Pass color vector rgbc() before use in fprintf() for proper gnuplot command
format.  Add test cases to not draw the axes background rectangle if the color
is similar to that of the plot background color dependent upon mono and
monoterm settings.
(rgbc): Added.  Map colorvec to gray if directed and round.

* __print_parse_opts__.m (__print_parse_opts__): Fix several bugs where
strings indexed with {a:b} should be indexed with (a:b).

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,12 +39,12 @@ 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);
-        __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
-        if (nargin == 5)
+        [enhanced, monoterm] = gnuplot_set_term (plot_stream(1), true, h, term, file, mono);
+        __go_draw_figure__ (h, plot_stream(1), enhanced, (mono && ! monoterm), monoterm);
+        if (nargin >= 5)
           fid = fopen (debug_file, "wb");
-          enhanced = gnuplot_set_term (fid, true, h, term, file);
-          __go_draw_figure__ (h, fid, enhanced, mono);
+          [enhanced, monoterm] = gnuplot_set_term (fid, true, h, term, file, mono);
+          __go_draw_figure__ (h, fid, enhanced, (mono && ! monoterm), monoterm);
         endif
       else
         error ('__gnuplot_drawnow__: the gnuplot terminal, "%s", is not available',
@@ -78,12 +78,13 @@ function __gnuplot_drawnow__ (h, term, f
     if (strcmp (term, "dumb"))
       ## 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);
+      [enhanced, monoterm] = gnuplot_set_term (plot_stream(1), new_stream, h,
+                                               term, dumb_tmp_file, mono);
     else
-      enhanced = gnuplot_set_term (plot_stream(1), new_stream, h, term);
+      [enhanced, monoterm] = gnuplot_set_term (plot_stream(1), new_stream, h,
+                                               term, "", mono);
     endif
-    __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
+    __go_draw_figure__ (h, plot_stream(1), enhanced, (mono && ! monoterm), monoterm);
     fflush (plot_stream(1));
     if (strcmp (term, "dumb"))
       fid = -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, monoterm] = 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,27 @@ 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", "eps", "epscairo", "epslatex", "fig", ...
+                           "mp", "pcl5", "pdfcairo", "pngcairo", "postscript", ...
+                           "pslatex", "pstex", "tgif", "tikz"};
+    terminals_with_color = {"cairolatex", "cgm", "context", "corel", ...
+                            "eepic", "emf", "eps", "epscairo", "epslatex", "fig", ...
+                            "mp", "pcl5", "pdfcairo", "pngcairo", "postscript", ...
+                            "pslatex", "pstex", "tgif", "tikz"};
+    monoterm = false;
+    if (mono)
+      if (any (strcmp (term, terminals_with_mono)))
+        term_str = [term_str " mono"];
+        monoterm = true;
+      endif
+    elseif (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 +340,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 +348,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"];
@@ -261,7 +275,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
@@ -781,15 +781,13 @@ function __go_draw_axes__ (h, plot_strea
                                     [[xcol; xcol(end)], [ycol; ycol(end)], ...
                                     [zcol; zcol(end)], [ccdat; ccdat(end)]]'];
              else
-               if (mono)
-                 colorspec = "";
-               elseif (__gnuplot_has_feature__ ("transparent_patches")
-                       && isscalar (obj.facealpha))
+               if (__gnuplot_has_feature__ ("transparent_patches")
+                   && isscalar (obj.facealpha))
                  colorspec = sprintf ("lc rgb \"#%02x%02x%02x\" fillstyle transparent solid %f",
-                                      round (255*color), obj.facealpha);
+                                      rgbc (255*color, mono), obj.facealpha);
                else
                  colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
-                                      round (255*color));
+                                      rgbc (255*color, mono));
                endif
 
                withclause{data_idx} = sprintf ("with filledcurve %s",
@@ -917,15 +915,11 @@ function __go_draw_axes__ (h, plot_strea
              pt2 = sprintf ("pointtype %s", pt2);
            endif
 
-           if (mono)
-             colorspec = "";
+           if (ischar (color))
+             colorspec = "palette";
            else
-             if (ischar (color))
-               colorspec = "palette";
-             else
-               colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
-                                    round (255*color));
-             endif
+             colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
+                                  rgbc (255*color, mono));
            endif
 
            sidx = 1;
@@ -966,9 +960,9 @@ function __go_draw_axes__ (h, plot_strea
                                           colorspec);
                  sidx ++;
                endif
-               if (isnumeric (obj.markerfacecolor) && ! mono)
+               if (isnumeric (obj.markerfacecolor))
                  colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
-                                      round (255*obj.markerfacecolor));
+                                      rgbc (255*obj.markerfacecolor, mono));
                endif
                style = "points";
                if (isfield (obj, "markersize"))
@@ -1023,14 +1017,12 @@ function __go_draw_axes__ (h, plot_strea
                endif
 
                if (!isempty (pt))
-                 if (! mono)
-                   if (strcmp (obj.markeredgecolor, "auto"))
-                     colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
-                                          round (255*color));
-                   elseif (isnumeric (obj.markeredgecolor) && ! mono)
-                     colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
-                                          round (255*obj.markeredgecolor));
-                   endif
+                 if (strcmp (obj.markeredgecolor, "auto"))
+                   colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
+                                        rgbc (255*color, mono));
+                 elseif (isnumeric (obj.markeredgecolor))
+                   colorspec = sprintf ("lc rgb \"#%02x%02x%02x\"",
+                                        rgbc (255*obj.markeredgecolor, mono));
                  endif
                  style = "points";
                  if (isfield (obj, "markersize"))
@@ -1790,7 +1782,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);
@@ -1807,10 +1799,8 @@ function style = do_linestyle_command (o
   found_style = false;
   if (isnumeric (linecolor))
     color = linecolor;
-    if (! mono)
-      fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
-               round (255*color));
-    endif
+    fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
+             rgbc (255*color, mono));
   else
     color = [0, 0, 0];
   endif
@@ -1889,9 +1879,9 @@ function style = do_linestyle_command (o
         endif
         fprintf (plot_stream, "set style line %d default;\n", idx);
         fprintf (plot_stream, "set style line %d", idx);
-        if (isnumeric (obj.markerfacecolor) && ! mono)
+        if (isnumeric (obj.markerfacecolor))
           fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
-                   round (255*obj.markerfacecolor));
+                   rgbc (255*obj.markerfacecolor, mono));
         endif
         if (! isempty (pt2))
           style {sidx} = "points";
@@ -1932,14 +1922,12 @@ function style = do_linestyle_command (o
         endif
         fprintf (plot_stream, "set style line %d default;\n", idx);
         fprintf (plot_stream, "set style line %d", idx);
-        if (! mono)
-          if (strcmp (obj.markeredgecolor, "auto"))
-            fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
-                     round (255*color));
-          elseif (isnumeric (obj.markeredgecolor) && ! mono)
-            fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
-                     round (255*obj.markeredgecolor));
-          endif
+        if (strcmp (obj.markeredgecolor, "auto"))
+          fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
+                   rgbc (255*color, mono));
+        elseif (isnumeric (obj.markeredgecolor))
+          fprintf (plot_stream, " linecolor rgb \"#%02x%02x%02x\"",
+                   rgbc (255*obj.markeredgecolor, mono));
         endif
         if (! isempty (pt))
           style {sidx} = "points";
@@ -2233,17 +2221,13 @@ function ticklabel = ticklabel_to_cell (
 endfunction
 
 function colorspec = get_text_colorspec (color, mono)
-  if (mono)
-    colorspec = "";
-  else
-    colorspec = sprintf ("textcolor rgb \"#%02x%02x%02x\"",
-                         round (255*color));
-  endif
+  colorspec = sprintf ("textcolor rgb \"#%02x%02x%02x\"",
+                       rgbc (255*color, mono));
 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
@@ -2648,3 +2632,14 @@ function maybe_do_x2tick_mirror (plot_st
                           axis_obj.tickdir);
   endif
 endfunction
+
+function rgbcolor = rgbc (colorvec, map_to_gray)
+
+  if (map_to_gray)
+    rgbcolor = ones (size (colorvec)) * mean (colorvec);
+    rgbcolor = round (rgbcolor);
+  else
+    rgbcolor = round (colorvec);
+  endif
+
+endfunction
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
@@ -17,13 +17,13 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {} __go_draw_figure__ (@var{h}, @var{plot_stream}, @var{enhanced}, @var{mono})
+## @deftypefn {Function File} {} __go_draw_figure__ (@var{h}, @var{plot_stream}, @var{enhanced}, @var{mono}, @var{monoterm})
 ## Undocumented internal function.
 ## @end deftypefn
 
 ## Author: jwe
 
-function __go_draw_figure__ (h, plot_stream, enhanced, mono)
+function __go_draw_figure__ (h, plot_stream, enhanced, mono, monoterm)
 
   htype = get (h, "type");
   if (strcmp (htype, "figure"))
@@ -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", round (255 * bg));
+        fprintf (plot_stream, "set obj 1 rectangle from screen 0,0 to screen 1,1 behind fc rgb \"#%02x%02x%02x\"\n", rgbc (255 * bg, mono));
         bg_is_set = true;
       else
         bg_is_set = false;
@@ -104,7 +104,7 @@ function __go_draw_figure__ (h, plot_str
                     endfor
 
                     if (! isempty (ll))
-                      if (!isempty (lm))
+                      if (! isempty (lm))
                         toberm = [toberm, line("xdata",[0,0],"ydata",[0,0], "color", get(lm,"color"), "linestyle", get(ll,"linestyle"), "marker", get(lm,"marker"), "markeredgecolor", get(lm,"markeredgecolor"), "markerfacecolor", get(lm,"markerfacecolor"), "markersize", get (lm, "markersize"), "displayname", displayname, "parent", kids(i))];
                       else
                         toberm = [toberm, line("xdata",[0,0],"ydata",[0,0], "color", get(ll,"color"), "linestyle", get(ll,"linestyle"), "marker", "none", "displayname", displayname, "parent", kids(i))];
@@ -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", round (255 * (1 - bg)));
+                    fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", rgbc (255 * (1 - bg), mono));
                   endif
                   __go_draw_axes__ (kids(i), plot_stream, enhanced, mono,
                                     bg_is_set, false, hlgnd);
@@ -139,8 +139,15 @@ function __go_draw_figure__ (h, plot_str
               unwind_protect
                 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", round (255 * fg));
+                ## The test against [1 1 1] circumvents gnuplot bug (up to at
+                ## least v5.0) which maps white to black for "-mono" because
+                ## of incorrect color to gray math conversion.
+                if (isnumeric (fg) && strcmp (get (kids(i), "visible"), "on") && ...
+                    ((! isnumeric (bg) && ! monoterm) || ...
+                     (isnumeric (bg) && ! isequal (fg, bg)) || ...
+                     (monoterm && ! isequal (fg, [1 1 1]))))
+                  colorstr = sprintf ("rgb \"#%02x%02x%02x\"", rgbc (255 * fg, mono));
+                  fprintf (plot_stream, "set obj 2 rectangle from graph 0,0 to graph 1,1 behind fc %s\n", colorstr);
                   fg_is_set = true;
                   fg_was_set = true;
                 elseif (fg_was_set)
@@ -151,7 +158,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", round (255 * (1 - bg)));
+                  fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", rgbc (255 * (1 - bg), mono));
                 endif
                 ## Find if this axes has an associated legend axes and pass it
                 ## to __go_draw_axes__
@@ -204,3 +211,13 @@ function __go_draw_figure__ (h, plot_str
 
 endfunction
 
+function rgbcolor = rgbc (colorvec, map_to_gray)
+
+  if (map_to_gray)
+    rgbcolor = ones (size (colorvec)) * mean (colorvec);
+    rgbcolor = round (rgbcolor);
+  else
+    rgbcolor = round (colorvec);
+  endif
+
+endfunction
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"))
exporting patch:
<fdopen>
