scripts/plot/__go_draw_figure__.m
author Ben Abbott <bpabbott@mac.com>
Thu Aug 06 07:30:34 2009 +0200 (2009-08-06)
changeset 9395 54a3fa5d4376
parent 9366 d213af40ee1f
permissions -rw-r--r--
Avoid the flickering x11 window seen with rapid gnuplot updates.
     1 ## Copyright (C) 2005, 2007, 2008, 2009 John W. Eaton
     2 ##
     3 ## This file is part of Octave.
     4 ##
     5 ## Octave is free software; you can redistribute it and/or modify it
     6 ## under the terms of the GNU General Public License as published by
     7 ## the Free Software Foundation; either version 3 of the License, or (at
     8 ## your option) any later version.
     9 ##
    10 ## Octave is distributed in the hope that it will be useful, but
    11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
    12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13 ## General Public License for more details.
    14 ##
    15 ## You should have received a copy of the GNU General Public License
    16 ## along with Octave; see the file COPYING.  If not, see
    17 ## <http://www.gnu.org/licenses/>.
    18 
    19 ## -*- texinfo -*-
    20 ## @deftypefn {Function File} {} __go_draw_figure__ (@var{h}, @var{plot_stream}, @var{enhanced}, @var{mono})
    21 ## Undocumented internal function.
    22 ## @end deftypefn
    23 
    24 ## Author: jwe
    25 
    26 function __go_draw_figure__ (h, plot_stream, enhanced, mono, output_to_paper, implicit_margin)
    27 
    28   if (nargin < 5)
    29     output_to_paper = false;
    30   elseif (nargin < 6)
    31     ## Gnuplot has implicit margins for some output. For example, for postscript
    32     ## the margin is 50pts. If not specified asssume 0.
    33     implicit_margin = 0;
    34   endif
    35 
    36   if (nargin >= 4 && nargin <= 6)
    37     htype = get (h, "type");
    38     if (strcmp (htype, "figure"))
    39       ## When printing, set paperunits to inches and rely on a listener to convert
    40       ## the values for papersize and paperposition.
    41       if (output_to_paper)
    42 	orig_paper_units = get (h, "paperunits");
    43 	gpval_term = __gnuplot_get_var__ (h, "GPVAL_TERM");
    44 	gpval_termoptions = __gnuplot_get_var__ (h, "GPVAL_TERMOPTIONS");
    45 	unwind_protect
    46 	  set (h, "paperunits", "inches");
    47           paper_size = get (h, "papersize");
    48           paper_position = get (h, "paperposition");
    49           paper_position = paper_position ./ paper_size([1, 2, 1, 2]);
    50 	  implicit_margin = implicit_margin ./ paper_size;
    51 	unwind_protect_cleanup
    52 	  set (h, "paperunits", orig_paper_units);
    53 	end_unwind_protect
    54 	if (strcmp (gpval_term, "postscript")
    55 	    && ! isempty (strfind (gpval_termoptions, "landscape")))
    56 	  ## This needed to obtain the expected result.
    57 	  implicit_margin(2) = -implicit_margin(2);
    58 	endif
    59       else
    60 	implicit_margin = implicit_margin * [1 1];
    61       endif
    62 
    63       ## Get complete list of children.
    64       kids = allchild (h);
    65       nkids = length (kids);
    66 
    67       if (nkids > 0)
    68 	fputs (plot_stream, "\nreset;\n");
    69 	fputs (plot_stream, "set autoscale keepfix;\n");
    70 	fputs (plot_stream, "set origin 0, 0\n");
    71 	fputs (plot_stream, "set size 1, 1\n");
    72 	for i = 1:nkids
    73 	  type = get (kids(i), "type");
    74 	  switch (type)
    75 	    case "axes"
    76 	      ## Rely upon listener to convert axes position to "normalized" units.
    77 	      orig_axes_units = get (kids(i), "units");
    78 	      orig_axes_position = get (kids(i), "position");
    79 	      unwind_protect
    80 		set (kids(i), "units", "normalized");
    81 		if (output_to_paper)
    82 		  axes_position_on_page = orig_axes_position .* paper_position([3, 4, 3 ,4]);
    83 		  axes_position_on_page(1:2) = axes_position_on_page(1:2) +  paper_position(1:2);
    84 		  set (kids(i), "position", axes_position_on_page);
    85 		  __go_draw_axes__ (kids(i), plot_stream, enhanced, mono, implicit_margin);
    86 		else
    87 		  ## Return axes "units" and "position" back to their original values.
    88 		  __go_draw_axes__ (kids(i), plot_stream, enhanced, mono, implicit_margin);
    89 		endif
    90 		unwind_protect_cleanup
    91 		set (kids(i), "units", orig_axes_units);
    92 		set (kids(i), "position", orig_axes_position);
    93 	      end_unwind_protect
    94 	    otherwise
    95 	      error ("__go_draw_figure__: unknown object class, %s", type);
    96 	  endswitch
    97 	endfor
    98       else
    99 	fputs (plot_stream, "\nreset; clear;\n");
   100 	fflush (plot_stream);
   101       endif
   102     else
   103       error ("__go_draw_figure__: expecting figure object, found `%s'",
   104 	     htype);
   105     endif
   106   else
   107     print_usage ();
   108   endif    
   109 
   110 endfunction
   111