scripts/plot/gnuplot_drawnow.m
author Ben Abbott <bpabbott@mac.com>
Thu Aug 06 07:30:34 2009 +0200 (2009-08-06)
changeset 9395 54a3fa5d4376
parent 9367 a7dbfd45c2aa
child 9416 7ed8182b783d
permissions -rw-r--r--
Avoid the flickering x11 window seen with rapid gnuplot updates.
jwe@8920
     1
## Copyright (C) 2005, 2006, 2007, 2008, 2009 John W. Eaton
jwe@7408
     2
##
jwe@7408
     3
## This file is part of Octave.
jwe@7408
     4
##
jwe@7408
     5
## Octave is free software; you can redistribute it and/or modify it
jwe@7408
     6
## under the terms of the GNU General Public License as published by
jwe@7408
     7
## the Free Software Foundation; either version 3 of the License, or (at
jwe@7408
     8
## your option) any later version.
jwe@7408
     9
##
jwe@7408
    10
## Octave is distributed in the hope that it will be useful, but
jwe@7408
    11
## WITHOUT ANY WARRANTY; without even the implied warranty of
jwe@7408
    12
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
jwe@7408
    13
## General Public License for more details.
jwe@7408
    14
##
jwe@7408
    15
## You should have received a copy of the GNU General Public License
jwe@7408
    16
## along with Octave; see the file COPYING.  If not, see
jwe@7408
    17
## <http://www.gnu.org/licenses/>.
jwe@7408
    18
jwe@7408
    19
## -*- texinfo -*-
jwe@7408
    20
## @deftypefn {Function File} {} drawnow ()
jwe@7408
    21
## Update and display the current graphics.
jwe@7408
    22
##
jwe@7408
    23
## Octave automatically calls drawnow just before printing a prompt,
jwe@7408
    24
## when @code{sleep} or @code{pause} is called, or while waiting for
jwe@7408
    25
## command-line input.
jwe@7408
    26
## @end deftypefn
jwe@7408
    27
jwe@7408
    28
## Author: jwe
jwe@7408
    29
jwe@7408
    30
function gnuplot_drawnow (h, term, file, mono, debug_file)
jwe@7408
    31
jwe@7408
    32
  if (nargin < 4)
jwe@7408
    33
    mono = false;
jwe@7408
    34
  endif
jwe@7408
    35
jwe@7408
    36
  if (nargin >= 3 && nargin <= 5)
bpabbott@8624
    37
    ## Produce various output formats, or redirect gnuplot stream to a
bpabbott@8624
    38
    ## debug file.
jwe@7408
    39
    plot_stream = [];
jwe@7408
    40
    fid = [];
bpabbott@8889
    41
    printing = ! output_to_screen (gnuplot_trim_term (term));
bpabbott@9055
    42
    default_plot_stream = get (h, "__plot_stream__");
jwe@7408
    43
    unwind_protect
bpabbott@9113
    44
      plot_stream = __gnuplot_open_stream__ (2, h);
bpabbott@9008
    45
      if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS"))
bpabbott@9008
    46
        available_terminals = __gnuplot_get_var__ (plot_stream, "GPVAL_TERMINALS");
bpabbott@9008
    47
        available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match");
bpabbott@9008
    48
        gnuplot_supports_term = any (strcmpi (available_terminals,
bpabbott@9008
    49
                                              gnuplot_trim_term (term)));
bpabbott@9008
    50
      else
bpabbott@9008
    51
        gnuplot_supports_term = true;
bpabbott@9008
    52
      endif
bpabbott@9008
    53
      if (gnuplot_supports_term)
bpabbott@9008
    54
        [enhanced, implicit_margin] = gnuplot_set_term (plot_stream (1), true,
bpabbott@9008
    55
                                                        h, term, file);
bpabbott@9011
    56
        __go_draw_figure__ (h, plot_stream(1), enhanced, mono, printing, implicit_margin);
bpabbott@9002
    57
        if (nargin == 5)
bpabbott@9002
    58
          fid = fopen (debug_file, "wb");
bpabbott@9002
    59
          [enhanced, implicit_margin] = gnuplot_set_term (fid, true, h, term, file);
bpabbott@9002
    60
          __go_draw_figure__ (h, fid, enhanced, mono, printing, implicit_margin);
bpabbott@9002
    61
        endif
bpabbott@9002
    62
      else
bpabbott@9002
    63
        error ("gnuplot_drawnow: the gnuplot terminal, \"%s\", is not available.",
bpabbott@9002
    64
               gnuplot_trim_term (term))
jwe@7408
    65
      endif
jwe@7408
    66
    unwind_protect_cleanup
bpabbott@9055
    67
      set (h, "__plot_stream__", default_plot_stream);
jwe@7408
    68
      if (! isempty (plot_stream))
bpabbott@9011
    69
        pclose (plot_stream(1));
bpabbott@9011
    70
        if (numel (plot_stream) == 2)
bpabbott@9011
    71
          pclose (plot_stream(2));
bpabbott@9011
    72
        endif
jwe@7408
    73
      endif
jwe@7408
    74
      if (! isempty (fid))
jwe@7408
    75
        fclose (fid);
jwe@7408
    76
      endif
jwe@7408
    77
    end_unwind_protect
jwe@7408
    78
  elseif (nargin == 1)
bpabbott@8624
    79
    ##  Graphics terminal for display.
jwe@8249
    80
    plot_stream = get (h, "__plot_stream__");
jwe@7408
    81
    if (isempty (plot_stream))
bpabbott@9113
    82
      plot_stream = __gnuplot_open_stream__ (2, h);
bpabbott@8789
    83
      new_stream = true;
bpabbott@8789
    84
    else
bpabbott@8789
    85
      new_stream = false;
jwe@7408
    86
    endif
bpabbott@8789
    87
    enhanced = gnuplot_set_term (plot_stream (1), new_stream, h);
bpabbott@8889
    88
    __go_draw_figure__ (h, plot_stream (1), enhanced, mono, 0);
dbateman@7680
    89
    fflush (plot_stream (1));
jwe@7408
    90
  else
jwe@7408
    91
    print_usage ();
jwe@7408
    92
  endif
jwe@7408
    93
jwe@7408
    94
endfunction
jwe@7408
    95
bpabbott@8889
    96
function implicit_margin = gnuplot_implicit_margin (term, opts_str)
bpabbott@8889
    97
  ## gnuplot has an implicit margin of 50pts for PS output.
bpabbott@8901
    98
  if (strcmpi (term, "postscript"))
bpabbott@8901
    99
    if (isempty (strfind (opts_str, " eps"))
bpabbott@8901
   100
        && isempty (strfind (opts_str, "eps ")))
bpabbott@8901
   101
      implicit_margin = 50/72;
bpabbott@8901
   102
    else
bpabbott@8901
   103
      ## When zero, the behavior of gnuplot changes.
bpabbott@8901
   104
      implicit_margin = 1/72;
bpabbott@8901
   105
    endif
bpabbott@8889
   106
  else
bpabbott@8889
   107
    implicit_margin = 0.0;
bpabbott@8889
   108
  endif
bpabbott@8889
   109
endfunction
bpabbott@8889
   110
bpabbott@8889
   111
function [enhanced, implicit_margin] = gnuplot_set_term (plot_stream, new_stream, h, term, file)
bpabbott@9366
   112
  ## Generate the gnuplot "set terminal <term> ..." command.
bpabbott@9366
   113
  ## When "term" originates from print.m, it may include other options.
jwe@8793
   114
  if (nargin < 4)
bpabbott@8624
   115
    ## This supports the gnuplot backend.
bpabbott@8889
   116
    term = gnuplot_default_term ();
bpabbott@8356
   117
    opts_str = "";
bpabbott@8356
   118
  else
bpabbott@8624
   119
    ## Get the one word terminal id and save the remaining as options to
bpabbott@8624
   120
    ## be passed on to gnuplot.  The terminal may respect the backend.
bpabbott@8356
   121
    [term, opts_str] = gnuplot_trim_term (term);
bpabbott@8889
   122
    if (strcmpi (term, "pdf") && strcmpi (opts_str, "color"))
bpabbott@8889
   123
      ## FIXME -- "color" for the pdf terminal produces a gnuplot error.
bpabbott@8889
   124
      opts_str = "";
bpabbott@8889
   125
    endif
bpabbott@8356
   126
  endif
jwe@7408
   127
bpabbott@8889
   128
  implicit_margin = gnuplot_implicit_margin (term, opts_str);
bpabbott@8889
   129
bpabbott@8356
   130
  enhanced = gnuplot_is_enhanced_term (term);
jwe@7408
   131
bpabbott@8624
   132
  ## Set the terminal.
bpabbott@8356
   133
  if (! isempty (term))
jwe@7408
   134
jwe@7408
   135
    if (enhanced)
jwe@7408
   136
      enh_str = "enhanced";
jwe@7408
   137
    else
jwe@7408
   138
      enh_str = "";
jwe@7408
   139
    endif
jwe@7408
   140
bpabbott@8356
   141
    if (! isempty (h) && isfigure (h))
bpabbott@8356
   142
bpabbott@8624
   143
      ## Generate gnuoplot title string for backend plot windows.
bpabbott@8889
   144
      if (output_to_screen (term))
bpabbott@8356
   145
        fig.numbertitle = get (h, "numbertitle");
bpabbott@8356
   146
        fig.name = get (h, "name");
bpabbott@8356
   147
        if (strcmpi (get (h, "numbertitle"), "on"))
bpabbott@8356
   148
          title_str = sprintf ("Figure %d", h);
bpabbott@8356
   149
        else
bpabbott@8356
   150
          title_str = "";
jwe@8610
   151
        endif
bpabbott@8356
   152
        if (! isempty (fig.name) && ! isempty (title_str))
bpabbott@8356
   153
          title_str = sprintf ("%s: %s", title_str, fig.name);
bpabbott@8356
   154
        elseif (! isempty (fig.name) && isempty (title_str))
bpabbott@8356
   155
          title_str = fig.name;
jwe@8610
   156
        endif
bpabbott@8356
   157
        if (! isempty (title_str))
bpabbott@8356
   158
          title_str = sprintf ("title \"%s\"", title_str);
bpabbott@8356
   159
        endif
bpabbott@8356
   160
      else
bpabbott@8356
   161
        title_str = "";
bpabbott@8356
   162
      endif
bpabbott@8889
   163
      if (! (any (strfind (opts_str, " size ") > 0) 
bpabbott@8889
   164
	  || any (strfind (opts_str, "size ") == 1)))
bpabbott@8356
   165
        ## Convert position to units used by gnuplot.
bpabbott@8889
   166
        if (output_to_screen (term))
bpabbott@9366
   167
          ## Get figure size in pixels.  Rely on listener
bpabbott@9366
   168
	  ## to handle coversion of position property.
bpabbott@9366
   169
	  units = get (h, "units");
bpabbott@9366
   170
	  unwind_protect
bpabbott@9366
   171
	    set (h, "units", "pixels");
bpabbott@9366
   172
	    position_in_pixesl = get (h, "position");
bpabbott@9366
   173
	  unwind_protect_cleanup
bpabbott@9366
   174
	    set (h, "units", units);
bpabbott@9366
   175
	  end_unwind_protect
bpabbott@9366
   176
	  gnuplot_pos = position_in_pixesl(1:2);
bpabbott@9366
   177
	  gnuplot_size = position_in_pixesl(3:4);
bpabbott@8356
   178
        else
bpabbott@9366
   179
          ## Get size of the printed plot in inches. Rely on listener
bpabbott@9366
   180
	  ## to handle coversion of papersize property.
bpabbott@9366
   181
	  paperunits = get (h, "paperunits");
bpabbott@9366
   182
	  unwind_protect
bpabbott@9366
   183
	    set (h, "paperunits", "inches");
bpabbott@9366
   184
            gnuplot_size = get (h, "papersize");
bpabbott@9366
   185
	  unwind_protect_cleanup
bpabbott@9366
   186
	    set (h, "paperunits", paperunits);
bpabbott@9366
   187
	  end_unwind_protect
bpabbott@8889
   188
          if (term_units_are_pixels (term))
bpabbott@9107
   189
	    ## Convert to inches using the property set by print().
bpabbott@8889
   190
	    gnuplot_size = gnuplot_size * get (h, "__pixels_per_inch__");
bpabbott@8889
   191
	  else
bpabbott@8889
   192
	    ## Implicit margins are in units of "inches"
bpabbott@8889
   193
	    gnuplot_size = gnuplot_size - implicit_margin;
bpabbott@8356
   194
          endif
bpabbott@8356
   195
        endif
bpabbott@8889
   196
	[begin_match, end_match, te, match] = regexp (opts_str, "(\\s-r\\d+)|(^-r\\d+)");
bpabbott@8889
   197
	if (! isempty (begin_match))
bpabbott@8889
   198
	  error ("gnuplot_drawnow.m: specifying resultion, '%s', not supported for terminal '%s'",
bpabbott@8889
   199
	         strtrim (match{1}), term)
bpabbott@8889
   200
	endif
bpabbott@8356
   201
        if (all (gnuplot_size > 0))
bpabbott@8624
   202
          ## Set terminal size.
bpabbott@8356
   203
          terminals_with_size = {"emf", "gif", "jpeg", "latex", "pbm", ...
bpabbott@9161
   204
                                 "pdf", "png", "postscript", "svg", ...
bpabbott@8356
   205
                                 "epslatex", "pstex", "pslatex"};
bpabbott@8889
   206
          if (__gnuplot_has_feature__ ("x11_figure_position"))
bpabbott@8638
   207
            terminals_with_size{end+1} = "x11";
bpabbott@9367
   208
          endif
bpabbott@9367
   209
          if (__gnuplot_has_feature__ ("wxt_figure_size"))
bpabbott@9161
   210
            terminals_with_size{end+1} = "wxt";
bpabbott@8638
   211
          endif
bpabbott@8356
   212
          if (any (strncmpi (term, terminals_with_size, 3)))
bpabbott@8889
   213
	    if (term_units_are_pixels (term))
bpabbott@9366
   214
              size_str = sprintf ("size %d,%d", gnuplot_size);
bpabbott@8889
   215
	    else
bpabbott@9366
   216
              size_str = sprintf ("size %.15g,%.15g", gnuplot_size);
bpabbott@8889
   217
	    endif
bpabbott@8889
   218
            if (strncmpi (term, "X11", 3) && __gnuplot_has_feature__ ("x11_figure_position"))
bpabbott@8889
   219
	      ## X11 allows the window to be positioned as well.
bpabbott@9366
   220
	      units = get (0, "units");
bpabbott@9366
   221
	      unwind_protect
bpabbott@9366
   222
	        set (0, "units", "pixels");
bpabbott@9366
   223
	        screen_size = get (0, "screensize")(3:4);
bpabbott@9366
   224
	      unwind_protect_cleanup
bpabbott@9366
   225
	        set (0, "units", units);
bpabbott@9366
   226
	      end_unwind_protect
bpabbott@8638
   227
              if (all (screen_size > 0))
bpabbott@8638
   228
                ## For X11, set the figure positon as well as the size
bpabbott@8638
   229
                ## gnuplot position is UL, Octave's is LL (same for screen/window)
bpabbott@8638
   230
                gnuplot_pos(2) = screen_size(2) - gnuplot_pos(2) - gnuplot_size(2);
bpabbott@8638
   231
                gnuplot_pos = max (gnuplot_pos, 1);
bpabbott@8638
   232
                size_str = sprintf ("%s position %d,%d", size_str, 
bpabbott@8638
   233
                                    gnuplot_pos(1), gnuplot_pos(2));
bpabbott@8638
   234
              endif
bpabbott@8638
   235
            endif
bpabbott@8889
   236
          elseif (strncmpi (term, "aqua", 3))
bpabbott@8889
   237
            ## Aqua has size, but the format is different.
bpabbott@9366
   238
            size_str = sprintf ("size %d %d", gnuplot_size);
bpabbott@8889
   239
          elseif (strncmpi (term, "fig", 3))
bpabbott@8889
   240
            ## Fig also has size, but the format is different.
bpabbott@9366
   241
            size_str = sprintf ("size %.15g %.15g", gnuplot_size);
bpabbott@8356
   242
          elseif (any (strncmpi (term, {"corel", "hpgl"}, 3)))
bpabbott@8624
   243
            ## The size for corel and hpgl are goes at the end (implicit).
bpabbott@9366
   244
            size_str = sprintf ("%.15g %.15g", gnuplot_size);
bpabbott@8356
   245
          elseif (any (strncmpi (term, {"dxf"}, 3)))
bpabbott@8624
   246
            ## DXF uses autocad units.
bpabbott@8356
   247
            size_str = "";
bpabbott@8356
   248
          else
bpabbott@8356
   249
            size_str = "";
bpabbott@8356
   250
          endif
bpabbott@8356
   251
        else
bpabbott@8356
   252
          size_str = "";
bpabbott@8356
   253
	  warning ("gnuplot_set_term: size is zero")
bpabbott@8356
   254
        endif
bpabbott@8356
   255
      else
bpabbott@8356
   256
        ## A specified size take priority over the figure properies.
bpabbott@8356
   257
        size_str = "";
bpabbott@8356
   258
      endif
bpabbott@8356
   259
    else
bpabbott@8356
   260
      if isempty (h)
bpabbott@8356
   261
        disp ("gnuplot_set_term: figure handle is empty")
bpabbott@8356
   262
      elseif !isfigure(h)
bpabbott@8356
   263
        disp ("gnuplot_set_term: not a figure handle")
bpabbott@8356
   264
      endif
bpabbott@8356
   265
      title_str = "";
bpabbott@8356
   266
      size_str = "";
jwe@7408
   267
    endif
jwe@7408
   268
bpabbott@9395
   269
    ## Set the gnuplot terminal (type, enhanced, title, options & size).
bpabbott@9395
   270
    term_str = sprintf ("set terminal %s", term);
bpabbott@9395
   271
    if (! isempty (enh_str))
bpabbott@9395
   272
      term_str = sprintf ("%s %s", term_str, enh_str);
bpabbott@9395
   273
    endif
bpabbott@9395
   274
    if (! isempty (title_str))
bpabbott@9395
   275
      term_str = sprintf ("%s %s", term_str, title_str);
bpabbott@9395
   276
    endif
bpabbott@9395
   277
    if (nargin > 3 && ischar (opts_str))
bpabbott@9395
   278
      ## Options must go last.
bpabbott@9395
   279
      term_str = sprintf ("%s %s", term_str, opts_str);
bpabbott@9395
   280
    endif
bpabbott@9395
   281
    if (! isempty (size_str) && new_stream)
bpabbott@9395
   282
      ## size_str comes after other options to permit specification of
bpabbott@9395
   283
      ## the canvas size for terminals cdr/corel.
bpabbott@9395
   284
      term_str = sprintf ("%s %s", term_str, size_str);
bpabbott@9395
   285
    endif
bpabbott@9395
   286
    ## Work around the gnuplot feature of growing the x11 window when
bpabbott@9395
   287
    ## the mouse and multiplot are set.
bpabbott@9395
   288
    fputs (plot_stream, "unset multiplot;\n");
bpabbott@9395
   289
    if (! strcmp (term, "x11")
bpabbott@9395
   290
        || numel (findall (h, "type", "axes")) > 1
bpabbott@9395
   291
        || numel (findall (h, "type", "image")) > 0)
bpabbott@9395
   292
      fprintf (plot_stream, "%s\n", term_str);
bpabbott@9395
   293
      if (nargin == 5)
bpabbott@9395
   294
        if (! isempty (file))
bpabbott@9395
   295
          fprintf (plot_stream, "set output '%s';\n", file);
bpabbott@9395
   296
        endif
bpabbott@8638
   297
      endif
bpabbott@9395
   298
      fputs (plot_stream, "set multiplot;\n");
bpabbott@9395
   299
    elseif (strcmp (term, "x11"))
bpabbott@9395
   300
      fprintf (plot_stream, "%s\n", term_str);
bpabbott@9395
   301
      if (nargin == 5)
bpabbott@9395
   302
        if (! isempty (file))
bpabbott@9395
   303
          fprintf (plot_stream, "set output '%s';\n", file);
bpabbott@9395
   304
        endif
jwe@8610
   305
      endif
jwe@8610
   306
    endif
bpabbott@9395
   307
  else
bpabbott@9395
   308
    ## gnuplot will pick up the GNUTERM environment variable itself
bpabbott@9395
   309
    ## so no need to set the terminal type if not also setting the
bpabbott@9395
   310
    ## figure title, enhanced mode, or position.
jwe@7408
   311
  endif
jwe@7408
   312
bpabbott@8889
   313
jwe@7408
   314
endfunction
jwe@7408
   315
bpabbott@8889
   316
function term = gnuplot_default_term ()
bpabbott@8356
   317
  term = getenv ("GNUTERM");
bpabbott@8356
   318
  ## If not specified, guess the terminal type.
bpabbott@8356
   319
  if (isempty (term))
bpabbott@8356
   320
    if (ismac ())
bpabbott@8356
   321
      term = "aqua";
bpabbott@8356
   322
    elseif (! isunix ())
bpabbott@8356
   323
      term = "windows";
bpabbott@8356
   324
    else
bpabbott@8356
   325
      term = "x11";
bpabbott@8356
   326
    endif
bpabbott@8356
   327
  endif
bpabbott@8356
   328
endfunction
bpabbott@8356
   329
bpabbott@8356
   330
function [term, opts] = gnuplot_trim_term (string)
bpabbott@8356
   331
  ## Extract the terminal type and terminal options (from print.m)
bpabbott@8356
   332
  string = deblank (string);
bpabbott@8356
   333
  n = strfind (string, ' ');
bpabbott@8356
   334
  if (isempty (n))
bpabbott@8356
   335
    term = string;
bpabbott@8356
   336
    opts = "";
bpabbott@8356
   337
  else
bpabbott@8356
   338
    term = string(1:(n-1));
bpabbott@8356
   339
    opts = string((n+1):end);
bpabbott@8356
   340
  endif
bpabbott@8356
   341
endfunction
bpabbott@8356
   342
bpabbott@8356
   343
function have_enhanced = gnuplot_is_enhanced_term (term)
jwe@7408
   344
  persistent enhanced_terminals;
jwe@7408
   345
  if (isempty (enhanced_terminals))
jwe@7408
   346
    ## Don't include pstex, pslatex or epslatex here as the TeX commands
jwe@7408
   347
    ## should not be interpreted in that case.
bpabbott@8356
   348
    enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ...
bpabbott@8356
   349
                          "windows", "wxt", "svg", "postscript", "x11", "pdf"};
jwe@7408
   350
  endif
bpabbott@8356
   351
  if (nargin < 1)
bpabbott@8356
   352
    ## Determine the default gnuplot terminal.
bpabbott@8889
   353
    term = gnuplot_default_term ();
bpabbott@8356
   354
  endif
bpabbott@9332
   355
  have_enhanced = any (strncmp (enhanced_terminals, term, min (numel (term), 3)));
jwe@7408
   356
endfunction
bpabbott@8356
   357
bpabbott@8889
   358
function ret = output_to_screen (term)
bpabbott@8356
   359
  ret = any (strcmpi ({"aqua", "wxt", "x11", "windows", "pm"}, term));
bpabbott@8356
   360
endfunction
bpabbott@8356
   361
bpabbott@8889
   362
function ret = term_units_are_pixels (term)
bpabbott@9332
   363
  ret = any (strncmpi ({"emf", "gif", "jpeg", "pbm", "png", "svg"}, term, 3));
bpabbott@8356
   364
endfunction
bpabbott@8356
   365