# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1547802447 -3600
#      Fri Jan 18 10:07:27 2019 +0100
# Branch stable
# Node ID ec1baa8799ff22e8326d7ac5b3fe0fda0622dbb8
# Parent  50439c2e8bbfe2ee9662fc465b6656642e6a1fb5
[mq]: protect_strlist

diff -r 50439c2e8bbf -r ec1baa8799ff libinterp/corefcn/base-text-renderer.h
--- a/libinterp/corefcn/base-text-renderer.h	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/base-text-renderer.h	Fri Jan 18 10:07:27 2019 +0100
@@ -52,6 +52,9 @@
 
     virtual ~base_text_renderer (void) = default;
 
+    virtual void
+    clear (void) = 0;
+    
     virtual Matrix
     get_extent (text_element *elt, double rotation) = 0;
 
diff -r 50439c2e8bbf -r ec1baa8799ff libinterp/corefcn/ft-text-renderer.cc
--- a/libinterp/corefcn/ft-text-renderer.cc	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/ft-text-renderer.cc	Fri Jan 18 10:07:27 2019 +0100
@@ -376,8 +376,9 @@
     ft_text_renderer (void)
       : base_text_renderer (), font (), bbox (1, 4, 0.0), halign (0),
         xoffset (0), line_yoffset (0), yoffset (0), mode (MODE_BBOX),
-        color (dim_vector (1, 3), 0), line_xoffset (0), m_ymin (0), m_ymax (0),
-        m_deltax (0), m_max_fontsize (0)
+        color (dim_vector (1, 3), 0), m_do_strlist (false), m_strlist (),
+        line_xoffset (0), m_ymin (0), m_ymax (0), m_deltax (0),
+        m_max_fontsize (0)
     { }
 
     // No copying!
@@ -410,6 +411,8 @@
 
     void reset (void);
 
+    void clear (void) { reset (); };
+
     uint8NDArray get_pixels (void) const { return pixels; }
 
     Matrix get_boundingbox (void) const { return bbox; }
@@ -530,7 +533,8 @@
     uint8NDArray color;
 
     // A list of parsed strings to be used for printing.
-    std::list<text_renderer::string> strlist;
+    bool m_do_strlist;
+    std::list<text_renderer::string> m_strlist;
 
     // The X offset of the baseline for the current line.
     int line_xoffset;
@@ -863,8 +867,11 @@
     // First run text_to_pixels which will also build the string list
 
     text_to_pixels (txt, pxls, box, ha, va, rot, interp, false);
-
-    lst = strlist;
+    
+  octave::unwind_protect frame;
+  frame.protect_var (m_do_strlist);
+  m_do_strlist = true;
+  lst = m_strlist;
   }
 
   void
@@ -894,38 +901,44 @@
             // character
             int mblen = octave_u8_strmbtouc_wrapper (&u32_c, c + icurr);
             n -= mblen;
-            
-            if (u32_c == 10)
+
+            if (m_do_strlist)
               {
-                // Finish previous string in strlist before processing
-                // the newline character
-                fs.set_y (line_yoffset + yoffset);
-                fs.set_color (color);
+                if (u32_c == 10)
+                  {
+                    // Finish previous string in m_strlist before processing
+                    // the newline character
+                    fs.set_y (line_yoffset + yoffset);
+                    fs.set_color (color);
                 
-                std::string s = str.substr (ibegin, icurr - ibegin);
-                if (! s.empty ())
-                  {
-                    fs.set_string (s);
-                    fs.set_y (line_yoffset + yoffset);
-                    fs.set_xdata (xdata);
-                    fs.set_family (fname);
-                    strlist.push_back (fs);
+                    std::string s = str.substr (ibegin, icurr - ibegin);
+                    if (! s.empty ())
+                      {
+                        fs.set_string (s);
+                        fs.set_y (line_yoffset + yoffset);
+                        fs.set_xdata (xdata);
+                        fs.set_family (fname);
+                        m_strlist.push_back (fs);
+                      }
                   }
+                else
+                  xdata.push_back (xoffset);
               }
-            else
-              xdata.push_back (xoffset);
 
             glyph_index = process_character (u32_c, previous);
 
-
             if (u32_c == 10)
               {
                 previous = 0;
-                // Start a new string in strlist
-                ibegin = icurr+1;
-                xdata.clear ();
-                fs = text_renderer::string (str.substr (ibegin), font,
+                
+                if (m_do_strlist)
+                  {
+                    // Start a new string in m_strlist
+                    ibegin = icurr+1;
+                    xdata.clear ();
+                    fs = text_renderer::string (str.substr (ibegin), font,
                                             line_xoffset, yoffset);
+                  }
               }
             else
               previous = glyph_index;
@@ -933,13 +946,13 @@
             icurr += mblen;
           }
 
-        if (! fs.get_string ().empty ())
+        if (m_do_strlist && ! fs.get_string ().empty ())
           {
             fs.set_y (line_yoffset + yoffset);
             fs.set_color (color);
             fs.set_xdata (xdata);
             fs.set_family (fname);
-            strlist.push_back (fs);
+            m_strlist.push_back (fs);
           }
       }
   }
@@ -1088,18 +1101,21 @@
     if (code != text_element_symbol::invalid_code && font.is_valid ())
       {
         process_character (code);
-        fs.set_code (code);
-        fs.set_xdata (xdata);
+        if (m_do_strlist)
+          {
+            fs.set_code (code);
+            fs.set_xdata (xdata);
+          }
       }
     else if (font.is_valid ())
       ::warning ("ignoring unknown symbol: %d", e.get_symbol ());
 
-    if (fs.get_code ())
+    if (m_do_strlist && fs.get_code ())
       {
         fs.set_y (line_yoffset + yoffset);
         fs.set_color (color);
         fs.set_family (font.get_face ()->family_name);
-        strlist.push_back (fs);
+        m_strlist.push_back (fs);
       }
   }
 
@@ -1124,6 +1140,7 @@
   {
     set_mode (MODE_BBOX);
     set_color (Matrix (1, 3, 0.0));
+    m_strlist.clear ();
   }
 
   void
@@ -1149,7 +1166,7 @@
 
     set_mode (MODE_RENDER);
     // Clear the list of parsed strings
-    strlist.clear ();
+    m_strlist.clear ();
 
     if (pixels.numel () > 0)
       {
diff -r 50439c2e8bbf -r ec1baa8799ff libinterp/corefcn/graphics.cc
--- a/libinterp/corefcn/graphics.cc	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/graphics.cc	Fri Jan 18 10:07:27 2019 +0100
@@ -5438,6 +5438,7 @@
   xset (title.handle_value (), "fontsize", octave_value (fs));
   xset (title.handle_value (), "fontweight", titlefontweight.get ());
 
+  txt_renderer.clear ();
   update_transform ();
   sync_positions ();
   override_defaults (bgo);
diff -r 50439c2e8bbf -r ec1baa8799ff libinterp/corefcn/text-renderer.cc
--- a/libinterp/corefcn/text-renderer.cc	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/text-renderer.cc	Fri Jan 18 10:07:27 2019 +0100
@@ -50,6 +50,25 @@
     delete rep;
   }
 
+  void
+  text_renderer::clear (void)
+  {
+    static bool warned = false;
+
+    if (! rep)
+      {
+        if (! warned)
+          {
+            warn_disabled_feature ("opengl_renderer::render_text",
+                                   "rendering text (FreeType)");
+
+            warned = true;
+          }
+      }
+    else
+      rep->clear ();
+  }
+
   bool
   text_renderer::ok (void) const
   {
diff -r 50439c2e8bbf -r ec1baa8799ff libinterp/corefcn/text-renderer.h
--- a/libinterp/corefcn/text-renderer.h	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/text-renderer.h	Fri Jan 18 10:07:27 2019 +0100
@@ -55,6 +55,8 @@
 
     ~text_renderer (void);
 
+    void clear (void);
+
     bool ok (void) const;
 
     Matrix get_extent (text_element *elt, double rotation = 0.0);
