diff scripts/plot/__gnuplot_print__.m @ 10913:dd6b90f44ae5

Unify gnuplot printing with the fltk backend.
author Ben Abbott <bpabbott@mac.com>
date Fri, 27 Aug 2010 06:39:36 -0400
parents d5a7db05d591
children 2a25f282e965
line wrap: on
line diff
--- a/scripts/plot/__gnuplot_print__.m
+++ b/scripts/plot/__gnuplot_print__.m
@@ -18,7 +18,7 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {} __gnuplot_ginput__ (@var{@dots{}})
+## @deftypefn {Function File} {} __gnuplot_print__ (@var{@dots{}})
 ## Undocumented internal function.
 ## @end deftypefn
 
@@ -27,552 +27,291 @@
 
 function __gnuplot_print__ (opts)
 
-  persistent warn_on_inconsistent_orientation = true
-
-  old_fig = get (0, "currentfigure");
-  unwind_protect
-    have_ghostscript = ! isempty (opts.ghostscript_binary);
+  file2unlink = "";
 
-    doprint = isempty (opts.name);
-    if (doprint)
-      if (isempty (opts.devopt))
-        if (opts.use_color < 0)
-          opts.devopt = "ps";
-          printname = cstrcat (tmpnam, ".ps");
-        else
-          opts.devopt = "psc";
-          printname = cstrcat (tmpnam, ".psc");
-        endif
-      else
-        printname = cstrcat (tmpnam, ".", devopt);
-      endif
-      opts.name = printname;
+  if (isempty (opts.fontsize))
+    ## If no fontsize, determine the nominal axes fontsize.
+    defaultfontsize = get (0, "defaultaxesfontsize");
+    axesfontsize = get (findobj (opts.figure, "type", "axes"), "fontsize");
+    if (iscell (axesfontsize))
+      axesfontsize = round (median (cell2mat (axesfontsize)));
     endif
-
-    dot = rindex (opts.name, ".");
-    if (isempty (opts.devopt))
-      if (dot == 0)
-        error ("print: no format specified");
-      else
-        dev = tolower (opts.name(dot+1:end));
-      endif
+    if (isempty (axesfontsize))
+      opts.fontsize = defaultfontsize;
     else
-      dev = opts.devopt;
+      opts.fontsize = axesfontsize;
     endif
-
-    dev_list = {"aifm", "corel", "fig", "png", "jpeg", ...
-                "gif", "pbm", "dxf", "mf", "svg", "hpgl", ...
-                "ps", "ps2", "psc", "psc2", "eps", "eps2", ...
-                "epsc", "epsc2", "emf", "pdf", "pslatex", ...
-                "epslatex", "epslatexstandalone", "pstex", "tikz"};
-    suffixes = {"ai", "cdr", "fig", "png", "jpeg", ...
-                "gif", "pbm", "dxf", "mf", "svg", "hpgl", ...
-                "ps", "ps", "ps", "ps", "eps", "eps", ...
-                "eps", "eps", "emf", "pdf", "tex", ...
-                "tex", "tex", "tex", "tikz"};
-    if (dot == 0)
-      opts.name = strcat (opts.name, ".", suffixes {strcmpi (dev_list, dev)});
-      dot = rindex (name, ".");
-    endif
-
-    if (opts.append_to_file)
-      if (any (strcmpi (dev, {"ps", "ps2", "psc", "psc2", "pdf"})))
-        if (have_ghostscript)
-          file_exists = ((numel (dir (opts.name)) == 1) && (! isdir (opts.name)));
-          if (! file_exists)
-            opts.append_to_file = 0;
-          end
-        end
-      else
-        warning ("print.m: appended output is not supported for device '%s'", dev)
-        opts.append_to_file = 0;
-      endif
-    endif
+  end
+  ## The axes-label and tick-label spacing is determined by
+  ## the font spec given in "set terminal ..."
+  gp_opts = font_spec (opts);
 
-    if (strcmp (dev, "tex"))
-      dev = "epslatex";
-    elseif (strcmp (dev, "ill"))
-      dev = "aifm";
-    elseif (strcmp (dev, "cdr"))
-      dev = "corel";
-    elseif (strcmp (dev, "meta"))
-      dev = "emf";
-    elseif (strcmp (dev, "jpg"))
-      dev = "jpeg";
-    endif
-
-    if (strcmp (dev, "epslatex"))
-      ## gnuplot 4.0 wants ".eps" in the output name    
-      if (! __gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix"))
-        opts.name = cstrcat (name(1:dot), "eps");
+  unwind_protect
+    switch lower (opts.devopt)
+    case {"eps", "eps2", "epsc", "epsc2"}
+      if (any (strcmp (opts.devopt, {"eps", "epsc"})))
+        gp_opts = sprintf ("%s level1", gp_opts);
       endif
-    endif
-
-    ## Check if the specified device is one that is supported by gnuplot.
-    ## If not, assume it is a device/format supported by Ghostscript.
-    if (! any (strcmp (dev, dev_list)) && have_ghostscript)
-      ghostscript_output = opts.name;
-      ghostscript_device = dev;
-      if (doprint)
-        ## If printing, use color postscript.
-        dev = "psc";
-        opts.name = cstrcat (tmpnam, ".ps");
-      else
-        ## If saving to a file, use color encapsulated postscript.
-        dev = "epsc";
-        opts.name = cstrcat (tmpnam, ".eps");
+      eps_drawnow (opts, opts.name, gp_opts);
+    case {"epslatex", "pslatex", "pstex", "epslatexstandalone"}
+      n = find (opts.devopt == "l", 1);
+      suffix = opts.devopt(1:n-1);
+      dot = find (opts.name == ".", 1, "last");
+      if ((! isempty (dot))
+          && any (strcmpi (opts.name(dot:end),
+                  {".eps", ".ps", ".pdf", ".tex", "."})))
+        name = opts.name(1:dot-1);
+        if (dot < numel (opts.name)
+            && any (strcmpi (opts.name(dot+1:end), {"eps", "ps"})))
+          ## If user provides eps/ps suffix, use it.
+          suffix = opts.name(dot+1:end);
+        endif
+      elseif (dot == numel (opts.name))
+        name = opts.name;
       endif
-    elseif (doprint && all (! strcmpi (suffixes {strcmpi (dev_list, dev)}, 
-                                       {"ps", "eps", "hpgl"})))
-      ## When not using Ghostscript, verify the format is compatible with
-      ## hard copy output.
-      error ("print: missing file name, or invalid print format.");
-    else
-      ghostscript_output = "";
-    endif
-
-    termn = dev;
-
-    ## SVG isn't actually a bitmap, but gnuplot treats its size option as it
-    ## does the bitmap terminals.
-    bitmap_devices = {"emf", "gif", "jpeg", "pbm", "png", "svg"};
-
-    if (any (strcmp (dev, {"ps", "ps2", "psc", "psc2", "epsc", "epsc2", ...
-                           "eps", "eps2", "pstex", "pslatex", "epslatex", ...
-                           "epslatexstandalone"})))
-
-      ## Various postscript options
-      if (any (strcmp (dev, {"pstex", "pslatex", "epslatex"})))
-        options = "";
-      elseif (strcmp (dev, "epslatexstandalone"))
-        if (__gnuplot_has_feature__ ("epslatexstandalone_terminal"))
-          termn = "epslatex";
-          options = "standalone ";
-        else
-          error ("print: epslatexstandalone needs gnuplot 4.2 or higher");
-        endif
+      if (strfind (opts.devopt, "standalone"))
+        term = sprintf ("%s ",
+                        strrep (opts.devopt, "standalone", " standalone"));
       else
-        if (dev(1) == "e")
-          options = "eps ";
-        else
-          options = "";
-        endif
-        termn = "postscript";
-      endif
-
-      if (any (dev == "c") || opts.use_color > 0
-          || (! isempty (strfind (dev, "tex")) && opts.use_color == 0))
-        opts.use_color = 1;
-      else
-        opts.use_color = -1;
-      endif
-      
-      if (opts.use_color > 0)
-        if (opts.force_solid < 0)
-          options = cstrcat (options, "color dashed ");
-        else
-          options = cstrcat (options, "color solid ");
-        endif
-      else
-        if (opts.force_solid > 0)
-          options = cstrcat (options, "mono solid ");
-        else
-          options = cstrcat (options, "mono dashed ");
-        endif
-      endif
-
-      if (! isempty (opts.font))
-        options = cstrcat (options, "\"", opts.font, "\" ");
-      endif
-      if (! isempty (opts.fontsize))
-        options = cstrcat (options, " ", opts.fontsize);
-      endif
-
-    elseif (strcmp (dev, "tikz"))
-      if (! isempty (font) && ! isempty (opts.fontsize))
-        options = cstrcat (options, "\"", opts.font, ",", opts.fontsize, "\" ");
-      elseif (! isempty (font))
-        options = cstrcat (options, "\"", opts.font, "\" ");
-      else
-        options = "";
-      endif
-
-    elseif (strcmp (dev, "aifm") || strcmp (dev, "corel"))
-      ## Adobe Illustrator, CorelDraw
-      if (opts.use_color >= 0)
-        options = " color";
-      else
-        options = " mono";
-      endif
-      if (! isempty (opts.font))
-        options = cstrcat (options, " \"", opts.font, "\"");
-      endif
-      if (! isempty (opts.fontsize))
-        options = cstrcat (options, " ", opts.fontsize);
+        term = sprintf ("%s ", opts.devopt);
       endif
-
-    elseif (strcmp (dev, "fig"))
-      ## XFig
-      options = opts.orientation;
-      if (opts.use_color >= 0)
-        options = " color";
-      else
-        options = " mono";
-      endif
-      options = cstrcat (options, " ", opts.special_flag);
-      if (! isempty (opts.fontsize))
-        options = cstrcat (options, " fontsize ", opts.fontsize);
-      endif
-
-    elseif (strcmp (dev, "emf"))
-      ## Enhanced Metafile format
-      options = " ";
-      if (opts.use_color >= 0)
-        options = " color";
-      else
-        options = " mono";
-      endif
-      if (opts.force_solid >= 0)
-        options = cstrcat (options, " solid");
-      endif
-      if (! isempty (opts.font))
-        options = cstrcat (options, " \"", opts.font, "\"");
-      endif
-      if (! isempty (opts.fontsize))
-        options = cstrcat (options, " ", opts.fontsize);
+      local_drawnow (sprintf ("%s %s", term, gp_opts),
+               strcat (name, ".", suffix, ".tex"), opts)
+      movefile (strcat (name, ".", suffix, ".tex"), strcat (name, ".tex"));
+      if (opts.tight_flag && strncmpi (opts.devopt, "eps", 3))
+        __tight_eps_bbox__ (opts, strcat (opts.name, ".eps"));
       endif
-
-    elseif (any (strcmp (dev, bitmap_devices)))
-
-      if (isempty (opts.canvas_size) && isempty (opts.resolution) 
-          && any (strcmp (dev, {"pbm", "gif", "jpeg", "png"})))
-        options = "";
-      elseif (strcmp (dev, "svg"))
-        ## Referring to size, either "dynamic" or "fixed"
-        options = "fixed";
-      else
-        options = "";
-      end
-      if (! isempty (opts.canvas_size))
-        options = sprintf ("%s size %d, %d", options, opts.canvas_size);
-      endif
-
-    elseif (any (strcmp (dev, {"dxf", "mf", "hpgl"})))
-      ## AutoCad DXF, METAFONT, HPGL
-      options = "";
-
-    elseif (strcmp (dev, "pdf"))
-      ## Portable Document format
-      options = " ";
-      if (opts.use_color >= 0)
-        options = "color";
-      else
-        options = "mono";
-      endif
-      if (opts.force_solid >= 0)
-        options = cstrcat (options, " solid");
-      elseif (opts.force_solid < 0)
-        options = cstrcat (options, " dashed");
-      endif
-      if (! isempty (opts.font))
-        options = cstrcat (options, "\"", opts.font, "\" ");
+    case {"tikz"}
+      local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, opts);
+    case {"ps", "ps2", "psc", "psc2", "pdf"}
+      if (any (strcmp (opts.devopt, {"ps", "psc"})))
+        gp_opts = sprintf ("%s level1", gp_opts);
       endif
-      if (! isempty (opts.fontsize))
-        options = cstrcat (options, " ", opts.fontsize);
-      endif
-
-    endif
- 
-    if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS"))
-      available_terminals = __gnuplot_get_var__ (gcf, "GPVAL_TERMINALS");
-      available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match");
-      ## Favor the cairo terminals.
-      if (strcmp (termn, "pdf") 
-          && any (strcmp (available_terminals, "pdfcairo")))
-        termn = "pdfcairo";
-        gnuplot_supports_term = true;
-      elseif (strcmp (termn, "png")
-              && any (strcmp (available_terminals, "pngcairo")))
-        termn = "pngcairo";
-        gnuplot_supports_term = true;
+      ## Gnuplot's BBox LLHC is located at [50,50]
+      opts.ghostscript.pageoffset = opts.ghostscript.pageoffset - 50;
+      opts.ghostscript.source = strcat (tmpnam (), ".eps");
+      file2unlink = opts.ghostscript.source;
+      if (strcmp (opts.devopt, "pdf"))
+        opts.ghostscript.device = "pdfwrite";
+      elseif (any (opts.devopt == '2'))
+        opts.ghostscript.device = "ps2write";
       else
-        gnuplot_supports_term = any (strcmp (available_terminals, termn));
-      endif
-    elseif (strcmp (termn, "pdf"))
-      ## Some Linux variants do not include a "pdf" capable gnuplot.
-      ## To be safe, use Ghostscript.
-      if (have_ghostscript)
-        gnuplot_supports_term = false;
-        ghostscript_device = "pdfwrite";
-      else
-        gnuplot_supports_term = true;
-      endif
-    else
-      gnuplot_supports_term = true;
-    endif
-
-    if (! gnuplot_supports_term)
-      if (strcmp (termn, "pdf"))
-        ## If there the installed gnuplot does not support pdf, use Ghostscript.
-        ghostscript_device = "pdfwrite";
-        if (strfind (opts.name, ".pdf") == numel (opts.name) - 3)
-          ghostscript_output = opts.name;
-        else
-          ghostscript_output = strcat (opts.name, ".pdf");
-        endif
-        name = cstrcat (tmpnam, ".ps");
-        termn = "postscript";
-        ## All "options" for pdf work for postscript as well.
-      else
-        error ("print: the device, \"%s\", is not available.", dev)
+        opts.ghostscript.device = "pswrite";
+        opts.ghostscript.level = 1;
       endif
-    endif
-
-    is_eps_file = strncmp (dev, "eps", 3);
-    p.units = get (gcf, "units");
-    p.paperunits = get (gcf, "paperunits");
-    p.papersize = get (gcf, "papersize");
-    p.paperposition = get (gcf, "paperposition");
-    p.paperpositionmode = get (gcf, "paperpositionmode");
-    p.paperorientation = get (gcf, "paperorientation");
-    p.color = get (gcf, "color");
-    if (p.papersize(1) > p.papersize(2))
-      paperorientation = "landscape";
-    else
-      paperorientation = "portrait";
-    endif
-    if (! strcmp (paperorientation, get (gcf, "paperorientation"))
-        && warn_on_inconsistent_orientation)
-       msg = {"print.m - inconsistent papersize and paperorientation properties.\n",
-               sprintf("         papersize = %.2f, %.2f\n", p.papersize),
-               sprintf("         paperorientation = \"%s\"\n", p.paperorientation),
-                       "         the paperorientation property has been ignored"};
-      warning ("%s",msg{:})
-      warn_on_inconsistent_orientation = false;
-    endif
-
-    if (strcmp (termn, "postscript") && ! strncmp (dev, "eps", 3))
-      if (isempty (opts.orientation))
-        opts.orientation = paperorientation;
-      endif
-      ## This is done here to accommodate ghostscript conversion.
-      options = cstrcat (opts.orientation, " ", options);
-    end
-
-    new_terminal = cstrcat (termn, " ", options);
-
-    mono = (opts.use_color < 0);
-
-    terminals_for_prn = {"postscript", "pdf", "pdfcairo"};
-    output_for_printer = any (strncmp (termn, terminals_for_prn, numel(termn)));
-
-    if (isempty (opts.resolution))
-      if (any (strcmp (dev, {"emf", "svg"})) || output_for_printer)
-        opts.resolution = get (0, "screenpixelsperinch");
-      else
-        opts.resolution = 150;
-      endif
-    else
-      opts.resolution = str2num (opts.resolution);
-      if (opts.resolution == 0)
-        opts.resolution = get (0, "screenpixelsperinch");
-      endif
-    endif
-    figure_properties = get (gcf);
-    if (! isfield (figure_properties, "__pixels_per_inch__"))
-      addproperty ("__pixels_per_inch__", gcf, "double", opts.resolution);
+      opts.ghostscript.output = opts.name;
+      eps_drawnow (opts, opts.ghostscript.source, gp_opts);
+    case {"svg"}
+      local_drawnow (sprintf ("svg dynamic %s", gp_opts), opts.name, opts);
+    case {"aifm", "corel", "eepic", "emf", "fig", "pdfcairo", "pngcairo"}
+      local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts);
+    case gs_based_devices ()
+      opts.ghostscript.antialiasing = true;
+      switch opts.devopt
+      case "bmp"
+        opts.ghostscript.device = "bmp32b";
+      case "png"
+        opts.ghostscript.device = "png16m";
+      case {"tiff", "tiffn"}
+        opts.ghostscript.device = "tiff24nc";
+      otherwise
+        opts.ghostscript.device = opts.devopt;
+      endswitch
+      opts.ghostscript.output = opts.name;
+      opts.ghostscript.source = strcat (tmpnam (), ".eps");
+      opts.ghostscript.epscrop = true;
+      file2unlink = opts.ghostscript.source;
+      eps_drawnow (opts, opts.ghostscript.source, gp_opts);
+    case {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"}
+      local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts)
+    case {"pdflatex", "pslatexstandalone", "pdflatexstandalone"}
+      error (sprintf ("print:no%soutput", opts.devopt),
+             "print.m: %s output is not available for the GNUPLOT backend.",
+             upper (opts.devopt))
+    otherwise
+      ## various ghostscript devices for printers
+      opts.ghostscript.device = opts.devopt;
+      opts.ghostscript.output = opts.name;
+      opts.ghostscript.epscrop = false;
+      opts.ghostscript.source = strcat (tmpnam (), ".eps");
+      file2unlink = opts.ghostscript.source;
+      ## Gnuplot's BBox LLHC is located at [50,50]
+      opts.ghostscript.pageoffset = opts.ghostscript.pageoffset - 50;
+      ## Empirical observation: "-dpxlcolor" requires a sign change.
+      opts.ghostscript.pageoffset = opts.ghostscript.pageoffset .* [1, -1];
+      ## Printers are not included in gs_devices()
+      gp_opts = font_spec (opts, "devopt", "eps");
+      eps_drawnow (opts, opts.ghostscript.source, gp_opts);
+    endswitch
+  
+    if (! isempty (opts.ghostscript.device))
+      status = __ghostscript__ (opts.ghostscript);
     endif
-    set (gcf, "__pixels_per_inch__", opts.resolution)
-
-    unwind_protect
-      set (gcf, "paperunits", "inches");
-      set (gcf, "units", "pixels");
-      set (gcf, "color", "none");
-      restore_properties = true;
-      if ((! output_for_printer || is_eps_file) && ! doprint)
-        ## If not PDF or PostScript, and the result is not being sent to a printer,
-        ## render an image the size of the paperposition box.
-        ## Trigger the listener to convert all paper props to inches.
-        if (! isempty (opts.canvas_size))
-          size_in_pixels = opts.canvas_size;
-          size_in_pixels = reshape (size_in_pixels, [1, numel(size_in_pixels)]);
-          papersize_in_inches = size_in_pixels ./ opts.resolution;
-          paperposition_in_inches = [0, 0, papersize_in_inches];
-        else
-          paperposition_in_inches = get (gcf, "paperposition");
-          paperposition_in_inches(1:2) = 0;
-          papersize_in_inches = paperposition_in_inches(3:4);
-        endif
-        set (gcf, "papersize", papersize_in_inches);
-        set (gcf, "paperposition", paperposition_in_inches);
-        set (gcf, "paperpositionmode", "manual");
-      else
-        if (strcmp (p.paperpositionmode, "auto"))
-          size_in_pixels = get (gcf, "position")(3:4);
-          paperposition_in_inches(3:4) = size_in_pixels ./ opts.resolution;
-          paperposition_in_inches(1:2) = (p.papersize - paperposition_in_inches(3:4))/2;
-        else
-          paperposition_in_inches = p.paperposition;
-        endif
-        if (! isempty (opts.orientation) && ! strcmp (opts.orientation, paperorientation))
-          ## When -landscape/portrait changes the orientation, flip both the
-          ## papersize and paperposition.
-          restore_properties = true;
-          set (gcf, "papersize", p.papersize([2, 1]));
-          set (gcf, "paperposition", paperposition_in_inches([2, 1, 4, 3]));
-        else
-          set (gcf, "paperposition", paperposition_in_inches);
-        endif
-      endif
-      if (opts.use_color < 0)
-        [objs_with_color, color_of_objs] = convert_color2mono (gcf);
-      endif
-    if (opts.append_to_file)
-         appended_file_name = opts.name;
-         if (index(termn, "pdf"))
-           opts.name = cstrcat (tmpnam, ".pdf");
-           temp_name = cstrcat (tmpnam, ".pdf");
-           ghostscript_device = "pdfwrite";
-         else
-           opts.name = cstrcat (tmpnam, ".ps");
-           temp_name = cstrcat (tmpnam, ".ps");
-           ghostscript_device = "pswrite";
-         endif
-    endif
-      if (opts.debug)
-        drawnow (new_terminal, opts.name, mono, opts.debug_file);
-      else
-        drawnow (new_terminal, opts.name, mono);
-      endif
-      if (opts.append_to_file)
-        ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE";
-        command = sprintf ("%s %s -sDEVICE=%s -sOutputFile=%s %s %s -q", ...
-                    opts.ghostscript_binary, ghostscript_options, ghostscript_device,  ...
-                    temp_name, appended_file_name, opts.name);
-        status1 = system (command);
-        status2 = system (sprintf ("mv %s %s", temp_name, appended_file_name));
-        if (status1 != 0 || status2 != 0)
-          error ("print: output failed to append to '%s'.", appended_file_name);
-        endif
+  
+  unwind_protect_cleanup
+    if (! isempty (file2unlink))
+      [status, output] = unlink (file2unlink);
+      if (status != 0)
+        warning ("print.m: %s, '%s'.", output, file2unlink)
       endif
-    unwind_protect_cleanup
-      ## FIXME - it would be nice to delete "__pixels_per_inch__" property here.
-      if (restore_properties)
-        props = fieldnames (p);
-        for n = 1:numel(props)
-          set (gcf, props{n}, p.(props{n}))
-        endfor
-      endif
-      if (opts.use_color < 0)
-        convert_mono_to_or_from_color (objs_with_color, color_of_objs, false);
-      endif
-    end_unwind_protect
-
-    if (! isempty (ghostscript_output))
-      if (is_eps_file && opts.tight_flag)
-        ## If gnuplot's output is an eps-file then crop at the bounding box.
-        __fix_eps_bbox__ (name);
-      endif
-      ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE -dTextAlphaBits=4";
-      if (is_eps_file)
-        ghostscript_options = sprintf ("%s -dEPSCrop", ghostscript_options);
-      endif
-      if (isempty (strfind (lower (ghostscript_device), "write")))
-        ## If output is a bitmap then include the resolution
-        ghostscript_options = sprintf ("%s -r%d", ghostscript_options, opts.resolution);
-      endif
-      ghostscript_options = sprintf ("%s -sDEVICE=%s", ghostscript_options,
-                                     ghostscript_device);
-      command = sprintf ("\"%s\" %s -sOutputFile=\"%s\" \"%s\" 2>&1", 
-                         opts.ghostscript_binary,
-                         ghostscript_options, ghostscript_output, opts.name);
-      [errcode, output] = system (command);
-      unlink (name);
-      if (errcode)
-        error ("print: Conversion failed, %s -> %s.\nError was:\n%s\n",
-               name, ghostscript_output, output);
-      endif
-    elseif (is_eps_file && opts.tight_flag && ! doprint)
-      ## If the saved output file is an eps file, use ghostscript to set a tight bbox.
-      ## This may result in a smaller or larger bbox geometry.
-      if (have_ghostscript)
-        __fix_eps_bbox__ (name);
-      endif
-    endif
-
-    if (doprint)
-      if (isunix ())
-        prn_opt = "-l";
-      elseif (ispc ())
-        prn_opt = "-o l";
-      else
-        ## FIXME - besides Unix and Windows, what other OS's might be considered.
-        prn_opt = "";
-      endif
-      if (isempty (opts.printer))
-        prn_cmd = sprintf ("lpr %s '%s' 2>&1", prn_opt, printname);
-      else
-        prn_cmd = sprintf ("lpr %s -P %s '%s' 2>&1", prn_opt, opts.printer, printname);
-      endif
-      [status, output] = system (prn_cmd);
-      if (status != 0)
-        disp (output)
-        warning ("print.m: printing failed.")
-      endif
-      [status, output] = unlink (printname);
-      if (status != 0)
-        disp (output)
-        warning ("print.m: failed to delete temporay file, '%s'.", printname)
-      endif
-    endif
-
-  unwind_protect_cleanup
-    if (isfigure (old_fig))
-      figure (old_fig)
     endif
   end_unwind_protect
 
 endfunction
 
-function [h, c] = convert_color2mono (hfig)
+function eps_drawnow (opts, epsfile, gp_opts)
+  [h, fontsize] = get_figure_text_objs (opts);
   unwind_protect
-    showhiddenhandles = get (0, "showhiddenhandles");
-    set (0, "showhiddenhandles", "on");
-    h.color = findobj (hfig, "-property", "color");
-    h.facecolor = findobj (hfig, "-property", "facecolor");
-    h.edgecolor = findobj (hfig, "-property", "edgecolor");
-    h.backgroundcolor = findobj (hfig, "-property", "backgroundcolor");
-    h.colormap = findobj (hfig, "-property", "colormap");
+    for n = 1:numel(h)
+      set (h, "fontsize", 2 * fontsize{n});
+    endfor
+    local_drawnow (sprintf ("postscript eps %s", gp_opts), epsfile, opts);
+    if (opts.tight_flag)
+      __tight_eps_bbox__ (opts, epsfile);
+    endif
   unwind_protect_cleanup
-    set (0, "showhiddenhandles", showhiddenhandles);
+    for n = 1:numel(h)
+      set (h, "fontsize", fontsize{n});
+    endfor
   end_unwind_protect
-  f = fieldnames (h);
-  for nf = 1:numel(f)
-    if (! isempty (h.(f{nf})))
-      v = get (h.(f{nf}), f{nf});
-      if (! iscell (v))
-        v = {v};
-      endif
-      c.(f{nf}) = v;
-    endif
-  endfor
-  convert_mono_to_or_from_color (h, c, true)
+endfunction
+
+function local_drawnow (term, file, opts)
+  if (opts.use_color < 0)
+    mono = true;
+  else
+    mono = false;
+  endif
+  figure (opts.figure)
+  if (isempty (opts.debug_file) || ! opts.debug)
+    drawnow (term, file, mono);
+  else
+    drawnow (term, file, mono, opts.debug_file);
+  endif
+endfunction
+
+function device_list = gs_based_devices ();
+  ## Aliases for other devices: "bmp", "png", "tiff", "tiffn", "pdf",
+  ##                            "ps", "ps2", "psc", "psc2"
+  device_list = {"bmp16", "bmp16m", "bmp256", "bmp32b", "bmpgray", ...
+                 "jpeg", "jpegcymk", "jpeggray", "pbm", "pbmraw", ...
+                 "pcx16", "pcx24b", "pcx256", "pcx2up", "pcxcmyk", ...
+                 "pcxgray", "pcxmono", "pdfwrite", "pgm", "pgmraw", ...
+                 "pgnm", "pgnmraw", "png16", "png16m", "png256", ...
+                 "png48", "pngalpha", "pnggray", "pngmono", "pnm", ...
+                 "pnmraw", "ppm", "ppmraw", "ps2write", "pswrite", ...
+                 "tiff12nc", "tiff24nc", "tiff32nc", "tiffcrle", ...
+                 "tiffg3", "tiffg32d", "tiffg4", "tiffgray", "tifflzw", ...
+                 "tiffpack", "tiffsep", "bmp", "png", "tiff", "tiffn", ...
+                 "pdf", "ps", "ps2", "psc", "psc2"};
 endfunction
 
-function convert_mono_to_or_from_color (h, c, mono)
-  f = fieldnames (h);
-  for nf = 1:numel(f)
-    for nh = 1:numel (h.(f{nf}))
-      color = c.(f{nf}){nh};
-      ## Ignore color == {"none", "flat", ...}
-      if (isfloat (color))
-        if (mono)
-          ## Same method as used by rgb2gray in the image pkg.
-          color = rgb2ntsc (color)(:,1) * ones (1, 3);
-        endif
-        set (h.(f{nf})(nh), f{nf}, color);
-      endif
-    endfor
+function f = font_spec (opts, varargin)
+  for n = 1:2:numel(varargin)
+    opts.(varargin{n}) = varargin{n+1};
   endfor
+  f = "";
+  switch opts.devopt
+  case {"cgm"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("%d", opts.fontsize);
+    endif
+  case {"eps", "eps2", "epsc", "epsc2", gs_based_devices(){:}}
+    ## Gnuplot renders fonts as half their specification, which 
+    ## results in a tight spacing for the axes-labels and tick-labels.
+    ## Compensate for the half scale. This will produce the proper
+    ## spacing for the requested fontsize.
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font ""%s,%d""", opts.font, 2 * opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("%d", 2 * opts.fontsize);
+    endif
+  case {"svg"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      fontsize = round (opts.fontsize * 0.75);
+      f = sprintf ("fname ""%s"" fsize %d", opts.font, fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("fname ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      fontsize = round (opts.fontsize * 0.75);
+      f = sprintf ("%s fsize %d", f, fontsize);
+    endif
+  case {"pdf"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("fsize %d", f, opts.fontsize);
+    endif
+  case {"pdfcairo", "pngcairo"}
+    if (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    endif
+  case {"epslatex", "epslatexstandalone"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font ""%s,%d""", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("%d", opts.fontsize);
+    endif
+  case {"pslatex"}
+    if (! isempty (opts.fontsize))
+      f = sprintf ("%d", opts.fontsize);
+    endif
+  case {"gif", "jpeg", "png"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font ""%s ,%d""", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font ""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("font ""%d""", opts.fontsize);
+    endif
+  case {"emf"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("""%s"" %d", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("""%s""", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("%d", opts.fontsize);
+    endif
+  case {"canvas"}
+    if (! isempty (opts.fontsize))
+      f = sprintf ("fsize %d", opts.fontsize);
+    endif
+  case {"aifm", "corel"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("%s %d", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("%s", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("%d", opts.fontsize);
+    endif
+  case {"fig"}
+    if (! isempty (opts.font) && ! isempty (opts.fontsize))
+      f = sprintf ("font %s fontsize %d", opts.font, opts.fontsize);
+    elseif (! isempty (opts.font))
+      f = sprintf ("font %s", opts.font);
+    elseif (! isempty (opts.fontsize))
+      f = sprintf ("fontsize %d", opts.fontsize);
+    endif
+  endswitch
 endfunction
 
+function [h, fontsize] = get_figure_text_objs (opts)
+  h = findall (opts.figure, "-property", "fontsize");
+  fontsize = get (h, "fontsize");
+  switch numel (fontsize)
+  case 0
+    fontsize = {};
+  case 1
+    fontsize = {fontsize};
+  endswitch
+endfunction