changeset 10423:dfc662a47b7a

print.m: Add '-append' option.
author Ben Abbott <bpabbott@mac.com>
date Thu, 18 Mar 2010 20:08:35 -0400
parents 9c4f403bec2b
children 0b05b204775b
files scripts/ChangeLog scripts/plot/print.m
diffstat 2 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Mar 18 19:52:37 2010 -0400
+++ b/scripts/ChangeLog	Thu Mar 18 20:08:35 2010 -0400
@@ -1,3 +1,7 @@
+2010-03-18  Petr Mikulik <mikulik@physics.muni.cz>
+
+	* /plot/print.m: Add '-append' option.
+
 2010-03-18  Ben Abbott <bpabbott@mac.com>
 
 	* /plot/print.m: print.m: Detabify.
--- a/scripts/plot/print.m	Thu Mar 18 19:52:37 2010 -0400
+++ b/scripts/plot/print.m	Thu Mar 18 20:08:35 2010 -0400
@@ -110,7 +110,7 @@
 ##   If the device is omitted, it is inferred from the file extension,
 ## or if there is no filename it is sent to the printer as postscript.
 ##
-## @item -d@var{gs_device}
+## @item -d@var{ghostscript_device}
 ##   Additional devices are supported by Ghostscript.
 ## Some examples are;
 ##
@@ -139,6 +139,10 @@
 ## is sent to a file the size is determined by the figure's
 ## "paperposition" property.
 ##
+## @itemx -append
+##   Appends the output to a pre-existing file. Presently only PDF,
+## and Postscript files are supported.
+##
 ## @itemx -r@var{NUM}
 ##   Resolution of bitmaps in pixels per inch.  For both metafiles and 
 ## SVG the default is the screen resolution, for other it is 150 dpi.
@@ -177,6 +181,7 @@
   persistent warn_on_inconsistent_orientation = true
   orientation = "";
   use_color = 0; # 0=default, -1=mono, +1=color
+  append_to_file = 0;
   force_solid = 0; # 0=default, -1=dashed, +1=solid
   fontsize = "";
   font = "";
@@ -218,6 +223,8 @@
       if (ischar (arg))
         if (strcmp (arg, "-color"))
           use_color = 1;
+        elseif (strcmp (arg, "-append"))
+          append_to_file = 1;
         elseif (strcmp (arg, "-mono"))
           use_color = -1;
         elseif (strcmp (arg, "-solid"))
@@ -302,6 +309,20 @@
       dev = devopt;
     endif
 
+    if (append_to_file)
+      if (any (strcmpi (dev, {"ps", "ps2", "psc", "psc2", "pdf"})))
+        if (have_ghostscript)
+          file_exists = ((numel (dir (name)) == 1) && (! isdir (name)));
+          if (! file_exists)
+            append_to_file = 0;
+          end
+        end
+      else
+        warning ("print.m: appended output is not supported for device '%s'", dev)
+        append_to_file = 0;
+      endif
+    endif
+
     if (strcmp (dev, "tex"))
       dev = "epslatex";
       ## gnuplot 4.0 wants ".eps" in the output name    
@@ -635,11 +656,34 @@
       if (use_color < 0)
         [objs_with_color, color_of_objs] = convert_color2mono (gcf);
       endif
+    if (append_to_file)
+         appended_file_name = name;
+         if (index(termn, "pdf"))
+           name = cstrcat (tmpnam, ".pdf");
+           temp_name = cstrcat (tmpnam, ".pdf");
+           ghostscript_device = "pdfwrite";
+         else
+           name = cstrcat (tmpnam, ".ps");
+           temp_name = cstrcat (tmpnam, ".ps");
+           ghostscript_device = "pswrite";
+         endif
+    endif
       if (debug)
         drawnow (new_terminal, name, mono, debug_file);
       else
         drawnow (new_terminal, name, mono);
       endif
+      if (append_to_file)
+        ghostscript_options = "-q -dBATCH -dSAFER -dNOPAUSE";
+        command = sprintf ("%s %s -sDEVICE=%s -sOutputFile=%s %s %s -q", ...
+                    ghostscript_binary, ghostscript_options, ghostscript_device,  ...
+                    temp_name, appended_file_name, name);
+        status1 = system (command);
+        status2 = system (sprintf ("mv %s %s", temp_name, appended_file_name));
+        if (status1 != 0 || status2 != 0)
+          error ("print.m: output failed to append to '%s'.", appended_file_name);
+        endif
+      endif
     unwind_protect_cleanup
       ## FIXME - it would be nice to delete "__pixels_per_inch__" property here.
       if (restore_properties)