changeset 10954:ee9d74048827

Consolidate gs code into print.m.
author Ben Abbott <bpabbott@mac.com>
date Wed, 08 Sep 2010 18:15:28 -0400
parents 9c0b366583cb
children 2786e3b7072e
files scripts/ChangeLog scripts/plot/__fltk_print__.m scripts/plot/__gnuplot_print__.m scripts/plot/__print_parse_opts__.m scripts/plot/print.m scripts/plot/private/__ghostscript__.m
diffstat 6 files changed, 235 insertions(+), 280 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,5 +1,8 @@
 2010-09-08  Ben Abbott <bpabbott@mac.com>
 
+	* plot/__fltk_print__.m, plot/private/__ghostscript__.m,
+	plot/__gnuplot_print__.m, plot/__print_parse_opts__.m,
+	plot/print.m: Consolidate gs code into print.m.
 	* plot/colorbar.m, plot/axis.m,
 	plot/private/__actual_axis_position__.m, plot/__go_draw_axes__.m:
 	Revert treatment of activepositionproperty from the gnuplot backend.
--- a/scripts/plot/__fltk_print__.m
+++ b/scripts/plot/__fltk_print__.m
@@ -21,13 +21,11 @@
 ## Undocumented internal function.
 ## @end deftypefn
 
-function __fltk_print__ (opts)
+function opts = __fltk_print__ (opts)
 
   figure (opts.figure)
   drawnow ("expose")
 
-  file2unlink = "";
-
   if (! isempty (opts.fig2dev_binary))
     fig2dev_devices = {"pstex", "mf", "emf"};
   else
@@ -35,121 +33,70 @@
     fig2dev_devices = {"pstex", "mf"};
   endif
 
-  unwind_protect
-
-    switch lower (opts.devopt)
-    case {"eps", "eps2", "epsc", "epsc2"}
-      drawnow ("eps", opts.name);
-      if (opts.tight_flag)
-        __tight_eps_bbox__ (opts, opts.name);
-      endif
-    case {"epslatex", "pslatex", "pdflatex", "epslatexstandalone", ...
-          "pslatexstandalone", "pdflatexstandalone"}
-      ## format GL2PS_TEX
-      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", "pdf"})))
-          ## If user provides eps/ps/pdf suffix, use it.
-          suffix = opts.name(dot+1:end);
-        endif
-      elseif (dot == numel (opts.name))
-        name = opts.name;
-      endif
-      drawnow (strcat (lower (suffix), "notxt"), strcat (name, ".", suffix));
-      drawnow ("tex", strcat (name, ".", suffix, ".tex"));
-      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
-      if (! isempty (strfind (opts.devopt, "standalone")))
-        __latex_standalone__ (strcat (name, ".tex"));
-      endif
-    case {"tikz"}
-      ## format GL2PS_PGF
-      drawnow ("pgf", opts.name);
-    case {"ps", "ps2", "psc", "psc2", "pdf"}
-      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
-        opts.ghostscript.device = "pswrite";
-      endif
-      opts.ghostscript.output = opts.name;
-      drawnow ("eps", opts.ghostscript.source);
-      if (opts.tight_flag)
-        __tight_eps_bbox__ (opts, opts.ghostscript.source);
+  switch lower (opts.devopt)
+  case {"eps", "eps2", "epsc", "epsc2"}
+    drawnow ("eps", opts.name);
+    if (opts.tight_flag)
+      __tight_eps_bbox__ (opts, opts.name);
+    endif
+  case {"epslatex", "pslatex", "pdflatex", "epslatexstandalone", ...
+        "pslatexstandalone", "pdflatexstandalone"}
+    ## format GL2PS_TEX
+    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", "pdf"})))
+        ## If user provides eps/ps/pdf suffix, use it.
+        suffix = opts.name(dot+1:end);
       endif
-    case {"svg"}
-      ## format GL2PS_SVG
-      drawnow ("svg", opts.name);
-    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;
-      drawnow ("eps", opts.ghostscript.source)
-      if (opts.tight_flag)
-        __tight_eps_bbox__ (opts, opts.ghostscript.source);
-      endif
-    case fig2dev_devices
-      tmp_figfile = strcat (tmpnam (), ".fig");
-      file2unlink = tmp_figfile;
-      status = __pstoedit__ (opts, "fig", tmp_figfile);
-      if (status == 0)
-        status = __fig2dev__ (opts, tmp_figfile);
-      endif
-    case {"aifm", "dxf", "emf", "fig", "hpgl"}
-      status = __pstoedit__ (opts);
-    case {"corel", "gif"}
-      error ("print:unsupporteddevice",
-             "print.m: %s output is not available for the FLTK 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;
-      ## Empirical observatin: "-dpxlcolor" requires a sign change.
-      opts.ghostscript.pageoffset = opts.ghostscript.pageoffset .* [1, -1];
-      drawnow ("eps", opts.ghostscript.source)
-      if (opts.tight_flag)
-        __tight_eps_bbox__ (opts, opts.ghostscript.source);
-      endif
-    endswitch
-  
-    if (! isempty (opts.ghostscript.device))
-      status = __ghostscript__ (opts.ghostscript);
+    elseif (dot == numel (opts.name))
+      name = opts.name;
+    endif
+    drawnow (strcat (lower (suffix), "notxt"), strcat (name, ".", suffix));
+    drawnow ("tex", strcat (name, ".tex"));
+    if (opts.tight_flag && strcmp (suffix, "eps"))
+      __tight_eps_bbox__ (opts, strcat (name, ".", suffix));
+    endif
+    if (! isempty (strfind (opts.devopt, "standalone")))
+      __latex_standalone__ (strcat (name, ".tex"));
     endif
-
-  unwind_protect_cleanup
-    if (! isempty (file2unlink))
-      [status, output] = unlink (file2unlink);
-      if (status != 0)
-        warning ("print.m: %s, '%s'.", output, file2unlink)
+  case {"tikz"}
+    ## format GL2PS_PGF
+    drawnow ("pgf", opts.name);
+  case {"svg"}
+    ## format GL2PS_SVG
+    drawnow ("svg", opts.name);
+  case fig2dev_devices
+    tmp_figfile = strcat (tmpnam (), ".fig");
+    opts.unlink{end+1} = tmp_figfile;
+    status = __pstoedit__ (opts, "fig", tmp_figfile);
+    if (status == 0)
+      status = __fig2dev__ (opts, tmp_figfile);
+      if (strcmp (opts.devopt, "pstex") && status == 0)
+        if (strfind (opts.name, ".ps") == numel(opts.name) - 2)
+          devfile = strcat (opts.name(1:end-2), "tex");
+        else
+          devfile = strcat (opts.name, ".tex");
+        endif
+        status = __fig2dev__ (opts, tmp_figfile, "pstex_t", devfile);
       endif
     endif
-  end_unwind_protect
+  case {"aifm"}
+    status = __pstoedit__ (opts, "ps2ai");
+  case {"dxf", "emf", "fig", "hpgl"}
+    status = __pstoedit__ (opts);
+  case {"corel", "gif"}
+    error ("print:unsupporteddevice",
+           "print.m: %s output is not available for the FLTK backend.",
+           upper (opts.devopt))
+  case opts.ghostscript.device
+    drawnow ("eps", opts.ghostscript.source);
+  endswitch
 
 endfunction
 
@@ -249,17 +196,4 @@
   endif
 endfunction
 
-function device_list = gs_based_devices ();
-  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", "psc", "ps2", "psc2"};
-endfunction
 
--- a/scripts/plot/__gnuplot_print__.m
+++ b/scripts/plot/__gnuplot_print__.m
@@ -25,9 +25,7 @@
 ## Author: Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
 ## Adapted-By: jwe
 
-function __gnuplot_print__ (opts)
-
-  file2unlink = "";
+function opts = __gnuplot_print__ (opts)
 
   if (isempty (opts.fontsize))
     ## If no fontsize, determine the nominal axes fontsize.
@@ -46,116 +44,56 @@
   ## the font spec given in "set terminal ..."
   gp_opts = font_spec (opts);
 
-  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
-      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
-      if (strfind (opts.devopt, "standalone"))
-        term = sprintf ("%s ",
-                        strrep (opts.devopt, "standalone", " standalone"));
-      else
-        term = sprintf ("%s ", opts.devopt);
-      endif
-      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
-    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
+  switch lower (opts.devopt)
+  case {"eps", "eps2", "epsc", "epsc2"}
+    if (any (strcmp (opts.devopt, {"eps", "epsc"})))
+      gp_opts = sprintf ("%s level1", gp_opts);
+    endif
+    eps_drawnow (opts, opts.name, gp_opts);
+  case {"epslatex", "pslatex", "pstex", "epslatexstandalone"}
+    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);
+    endif
+    if (strfind (opts.devopt, "standalone"))
+      term = sprintf ("%s ",
+                      strrep (opts.devopt, "standalone", " standalone"));
+    else
+      term = sprintf ("%s ", opts.devopt);
+    endif
+    if (__gnuplot_has_feature__ ("epslatex_implies_eps_filesuffix"))
+      suffix = "tex";
+    else
+      %% Gnuplot 4.0 wants a ".eps" suffix.
+      suffix = "eps";
+    endif
+    local_drawnow (sprintf ("%s %s", term, gp_opts),
+                   strcat (name, ".", suffix), opts)
+    if (opts.tight_flag && strncmpi (opts.devopt, "eps", 3))
+      __tight_eps_bbox__ (opts, strcat (name, "-inc.eps"));
+    endif
+  case {"tikz"}
+    local_drawnow (sprintf ("lua tikz %s", gp_opts), opts.name, 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 {"canvas", "dxf", "hpgl", "mf", "gif", "pstricks", "texdraw"}
+    local_drawnow (sprintf ("%s %s", opts.devopt, gp_opts), opts.name, opts)
+  case opts.ghostscript.device
+    if (opts.formatted_for_printing)
       ## 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
-        opts.ghostscript.device = "pswrite";
-        opts.ghostscript.level = 1;
-      endif
-      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
-  
-  unwind_protect_cleanup
-    if (! isempty (file2unlink))
-      [status, output] = unlink (file2unlink);
-      if (status != 0)
-        warning ("print.m: %s, '%s'.", output, file2unlink)
-      endif
-    endif
-  end_unwind_protect
+    gp_opts = font_spec (opts, "devopt", "eps");
+    eps_drawnow (opts, opts.ghostscript.source, gp_opts);
+  otherwise
+    error (sprintf ("print:no%soutput", opts.devopt),
+           "print.m: %s output is not available for the GNUPLOT backend.",
+           upper (opts.devopt))
+  endswitch
 
 endfunction
 
@@ -190,22 +128,6 @@
   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 f = font_spec (opts, varargin)
   for n = 1:2:numel(varargin)
     opts.(varargin{n}) = varargin{n+1};
@@ -220,7 +142,7 @@
     elseif (! isempty (opts.fontsize))
       f = sprintf ("%d", opts.fontsize);
     endif
-  case {"eps", "eps2", "epsc", "epsc2", gs_based_devices(){:}}
+  case {"eps", "eps2", "epsc", "epsc2"}
     ## 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
--- a/scripts/plot/__print_parse_opts__.m
+++ b/scripts/plot/__print_parse_opts__.m
@@ -36,22 +36,26 @@
   arg_st.fontsize = "";
   arg_st.font = "";
   arg_st.force_solid = 0; # 0=default, -1=dashed, +1=solid
+  arg_st.formatted_for_printing = false;
   arg_st.ghostscript.binary = __quote_path__ (__ghostscript_binary__ ());
+  arg_st.ghostscript.debug = false;
   arg_st.ghostscript.device = "";
+  arg_st.ghostscript.epscrop = true;
+  arg_st.ghostscript.level = [];
   arg_st.ghostscript.output = "";
   arg_st.ghostscript.papersize = "";
   arg_st.ghostscript.pageoffset = [];
-  arg_st.ghostscript.debug = false;
-  arg_st.ghostscript.epscrop = false;
   arg_st.ghostscript.resolution = 150;
+  arg_st.ghostscript.antialiasing = false;
+  arg_st.loose = false;
+  arg_st.name = "";
   arg_st.orientation = "";
   arg_st.pstoedit_binary = __quote_path__ (__find_binary__ ("pstoedit"));
-  arg_st.name = "";
   arg_st.printer = "";
+  arg_st.send_to_printer = false;
   arg_st.special_flag = "textnormal";
   arg_st.tight_flag = false;
   arg_st.use_color = 0; # 0=default, -1=mono, +1=color
-  arg_st.send_to_printer = false;
   
   if (isunix ())
     arg_st.lpr_options = "-l";
@@ -79,6 +83,8 @@
         arg_st.orientation = "portrait";
       elseif (strncmp (arg, "-landscape", numel (arg)))
         arg_st.orientation = "landscape";
+      elseif (strcmp (arg, "-loose"))
+        arg_st.loose = true;
       elseif (strcmp (arg, "-tight"))
         arg_st.tight_flag = true;
       elseif (strcmp (arg, "-loose"))
@@ -98,7 +104,8 @@
       elseif ((length (arg) > 2) && arg(1:2) == "-G")
         arg_st.ghostscript.binary = file_in_path (EXEC_PATH, arg(3:end));
         if (isempty (arg_st.ghostscript.binary))
-          error ("print: Ghostscript binary ""%s"" could not be located", arg(3:end))
+          error ("print: Ghostscript binary ""%s"" could not be located",
+                 arg(3:end))
         else
           arg_st.ghostscript_binary = __quote_path__ (arg_st.ghostscript_binary);
         endif
@@ -220,6 +227,10 @@
     arg_st.name = strcat (arg_st.name, ".", default_suffix);
   endif
 
+  if (any (strcmp (arg_st.devopt, {"ps", "ps2", "psc", "psc2", "pdf"})))
+    arg_st.formatted_for_printing = true;
+  endif
+
   if (arg_st.append_to_file)
     if (isempty (arg_st.name))
       arg_st.append_to_file = false;
@@ -249,9 +260,32 @@
     endif
   endif
 
-  if (all (! strcmp (arg_st.devopt, dev_list)))
+  aliases = gs_aliases ();
+  if (any (strcmp (arg_st.devopt, fieldnames (aliases))))
+    arg_st.devopt = aliases.(arg_st.devopt);
+  endif
+
+  if (strcmp (arg_st.devopt, "pswrite"))
+    arg_st.ghostscript.level = 1;
+  elseif (strcmp (arg_st.devopt, "ps2write"))
+    arg_st.ghostscript.level = 2;
+  endif
+
+  if (any (strcmp (arg_st.devopt, gs_device_list)) &&
+      ! arg_st.formatted_for_printing)
+    ## Use ghostscript for graphic formats
     arg_st.ghostscript.device = arg_st.devopt;
     arg_st.ghostscript.output = arg_st.name;
+    arg_st.ghostscript.antialiasing = true;
+    ## pstoedit throws errors if the EPS file isn't cropped
+    arg_st.ghostscript.epscrop = true;
+  elseif (all (! strcmp (arg_st.devopt, dev_list)))
+    ## Assume we are formating output for a printer
+    arg_st.formatted_for_printing = true;
+    arg_st.ghostscript.device = arg_st.devopt;
+    arg_st.ghostscript.output = arg_st.name;
+    arg_st.ghostscript.antialiasing = false;
+    arg_st.ghostscript.epscrop = ! arg_st.loose;
   endif
 
   if (isempty (arg_st.canvas_size))
@@ -266,23 +300,27 @@
     arg_st.canvas_size = paperposition(3:4);
     arg_st.ghostscript.pageoffset = paperposition(1:2);
   else
-    ## Canvas size in points.
+    ## Convert canvas size to points from pixles.
     arg_st.canvas_size = arg_st.canvas_size * 72 / arg_st.ghostscript.resolution;
     arg_st.ghostscript.papersize = arg_st.canvas_size;
     arg_st.ghostscript.epscrop = true;
     arg_st.ghostscript.pageoffset = [0, 0];
   endif
 
+  if (arg_st.formatted_for_printing)
+    arg_st.ghostscript.resolution = [];
+  endif
+
 endfunction
 
 %!test
 %! opts = __print_parse_opts__ ();
-%! assert (opts.devopt, "psc");
+%! assert (opts.devopt, "pswrite");
 %! assert (opts.use_color, 1);
 %! assert (opts.send_to_printer, true);
 %! assert (opts.name, opts.unlink{1})
 %! assert (opts.canvas_size, [576, 432]);
-%! assert (opts.ghostscript.device, "")
+%! assert (opts.ghostscript.device, "pswrite")
 %! for n = 1:numel(opts.unlink)
 %!   unlink (opts.unlink{n});
 %! endfor
@@ -292,13 +330,14 @@
 %! assert (opts.canvas_size, [307.2, 230.4], 0.1);
 
 %!test
-%! opts = __print_parse_opts__ ("-dpsc", "-append");
-%! assert (opts.devopt, "psc");
+%! opts = __print_parse_opts__ ("-dpsc", "-append", "-loose");
+%! assert (opts.devopt, "pswrite");
 %! assert (opts.name(end+(-2:0)), ".ps");
 %! assert (opts.send_to_printer, true);
 %! assert (opts.use_color, 1);
 %! assert (opts.append_to_file, false);
-%! assert (opts.ghostscript.device, "")
+%! assert (opts.ghostscript.device, "pswrite")
+%! assert (opts.ghostscript.epscrop, false);
 %! for n = 1:numel(opts.unlink)
 %!   unlink (opts.unlink{n});
 %! endfor
@@ -315,14 +354,14 @@
 %! endfor
 
 %!test
-%! opts = __print_parse_opts__ ("-djpg", "foobar", "-mono");
+%! opts = __print_parse_opts__ ("-djpg", "foobar", "-mono", "-loose");
 %! assert (opts.devopt, "jpeg")
 %! assert (opts.name, "foobar.jpg")
-%! assert (opts.ghostscript.device, "")
+%! assert (opts.ghostscript.device, "jpeg")
+%! assert (opts.ghostscript.epscrop, true);
 %! assert (opts.send_to_printer, false);
 %! assert (opts.printer, "");
 %! assert (opts.use_color, -1);
-%! assert (opts.ghostscript.device, "")
 
 %!test
 %! opts = __print_parse_opts__ ("-ddeskjet", "foobar", "-mono", "-Pmyprinter");
@@ -503,3 +542,34 @@
     endswitch
 endfunction
 
+function device_list = gs_device_list ();
+  ## Graphics formats/languages, not priners.
+  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"};
+endfunction
+
+function aliases = gs_aliases ();
+  ## Aliases for other devices: "bmp", "png", "tiff", "tiffn", "pdf",
+  ##                            "ps", "ps2", "psc", "psc2"
+  ##
+  ## eps, epsc, eps2, epsc2 are not included here because those are 
+  ## are generated by the backend.
+  aliases.bmp = "bmp32b";
+  aliases.pdf = "pdfwrite";
+  aliases.png = "png16m";
+  aliases.ps = "pswrite";
+  aliases.ps2 = "ps2write";
+  aliases.psc = "pswrite";
+  aliases.psc2 = "ps2write";
+  aliases.tiff = "tiff24nc";
+  aliases.tiffn = "tiff24nc";
+endfunction
+
--- a/scripts/plot/print.m
+++ b/scripts/plot/print.m
@@ -328,8 +328,25 @@
       endif
     endif
 
+    if (strcmp (opts.devopt, opts.ghostscript.device))
+      opts.ghostscript.output = opts.name;
+      opts.ghostscript.source = strcat (tmpnam (), ".eps");
+      opts.unlink{end+1} = opts.ghostscript.source;
+    endif
+
     ## call the backend print script
-    feval (strcat ("__", get (opts.figure, "__backend__") , "_print__"), opts);
+    opts = feval (strcat ("__", get (opts.figure, "__backend__"), "_print__"),
+                  opts);
+
+    if (strcmp (opts.devopt, opts.ghostscript.device))
+      if (opts.tight_flag && ! opts.formatted_for_printing)
+        __tight_eps_bbox__ (opts, opts.ghostscript.source);
+      endif
+      status = __ghostscript__ (opts.ghostscript);
+      if (status != 0)
+        warning ("print.m:gsfailed", "print.m: ghostscript failure")
+      endif
+    endif
 
     ## Send to the printer
     if (opts.send_to_printer)
--- a/scripts/plot/private/__ghostscript__.m
+++ b/scripts/plot/private/__ghostscript__.m
@@ -30,7 +30,7 @@
   opts.device = "";
   opts.epscrop = false;
   opts.antialiasing  = false;
-  opts.resolution = 150;
+  opts.resolution = [];
   opts.papersize = "";
   opts.pageoffset = [0 0];
   opts.debug = false;
@@ -51,16 +51,23 @@
     opts.(args{n}) = args{n+1};
   endfor
 
-  gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s", opts.device);
+  gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s",
+                     opts.device);
 
   if (! isempty (opts.level) && ismember (opts.level, [1, 2, 3]))
     gs_opts = sprintf ("%s -dLanguageLevel=%d", gs_opts, round (opts.level));
   endif
 
-  if (opts.antialiasing && isempty (strfind (opts.device, "write")))
-    ## Apply anti-aliasing to all bitmap formats/devices
-    gs_opts = sprintf ("%s -dTextAlphaBits=4 -dGraphicsAlphaBits=4", gs_opts);
-    gs_opts = sprintf ("%s -r%dx%d", gs_opts, [1, 1] * opts.resolution);
+  if (isempty (strfind (opts.device, "write")))
+    ## Empirical observation: "-dpxlcolor" requires a sign change as
+    ##                        compared to pdfwrite, or pswrite output.
+    opts.pageoffset = opts.pageoffset .* [1, -1];
+    if (! isempty (opts.resolution))
+      gs_opts = sprintf ("%s -r%dx%d", gs_opts, [1, 1] * opts.resolution);
+    endif
+    if (opts.antialiasing)
+      gs_opts = sprintf ("%s -dTextAlphaBits=4 -dGraphicsAlphaBits=4", gs_opts);
+    endif
   elseif (any (strcmp (opts.device, {"pswrite", "ps2write", "pdfwrite"})))
     gs_opts = sprintf ("%s -dEmbedAllFonts=true", gs_opts);
     if (strcmp (opts.device, "pdfwrite"))
@@ -76,10 +83,12 @@
     if (ischar (opts.papersize))
       gs_opts = sprintf ("%s -sPAPERSIZE=%s", gs_opts, opts.papersize);
     elseif (isnumeric (opts.papersize) && numel (opts.papersize) == 2)
-      gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d", gs_opts, opts.papersize);
+      gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d",
+                         gs_opts, opts.papersize);
       if (opts.papersize(1) > opts.papersize(2))
-        ## Lanscape mode: This option will result in automatic rotation of the document page if the
-        ##                requested page size matches one of the default page sizes
+        ## Lanscape mode: This option will result in automatic rotation of the
+        ##                document page if the requested page size matches one
+        ##                of the default page sizes.
         gs_opts = sprintf ("%s -dNORANGEPAGESIZE", gs_opts);
       endif
     else
@@ -112,7 +121,7 @@
     endif
   endif
 
-  cmd = sprintf ("%s %s -sOutputFile=%s %s %s", 
+  cmd = sprintf ("%s %s -sOutputFile=%s %s %s 2>&1", 
                  opts.binary, gs_opts,
                  opts.output, offsetfile, opts.source);