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

diff -r 50439c2e8bbf -r cc59c1aca759 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 11:18:23 2019 +0100
@@ -52,6 +52,9 @@
 
     virtual ~base_text_renderer (void) = default;
 
+    virtual void
+    reset (void) = 0;
+    
     virtual Matrix
     get_extent (text_element *elt, double rotation) = 0;
 
diff -r 50439c2e8bbf -r cc59c1aca759 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 11:18:23 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!
@@ -530,7 +531,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;
@@ -862,9 +864,13 @@
 
     // First run text_to_pixels which will also build the string list
 
+    octave::unwind_protect frame;
+    frame.protect_var (m_do_strlist);
+    m_do_strlist = true;
+    
     text_to_pixels (txt, pxls, box, ha, va, rot, interp, false);
 
-    lst = strlist;
+    lst = m_strlist;
   }
 
   void
@@ -894,38 +900,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 +945,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 +1100,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 +1139,7 @@
   {
     set_mode (MODE_BBOX);
     set_color (Matrix (1, 3, 0.0));
+    m_strlist.clear ();
   }
 
   void
@@ -1149,7 +1165,7 @@
 
     set_mode (MODE_RENDER);
     // Clear the list of parsed strings
-    strlist.clear ();
+    m_strlist.clear ();
 
     if (pixels.numel () > 0)
       {
diff -r 50439c2e8bbf -r cc59c1aca759 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/gl-render.cc	Fri Jan 18 11:18:23 2019 +0100
@@ -712,6 +712,7 @@
     // Initialize OpenGL context
 
     init_gl_context (props.is_graphicssmoothing (), props.get_color_rgb ());
+    txt_renderer.reset ();
 
 #if defined (HAVE_OPENGL)
 
diff -r 50439c2e8bbf -r cc59c1aca759 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 11:18:23 2019 +0100
@@ -5438,6 +5438,8 @@
   xset (title.handle_value (), "fontsize", octave_value (fs));
   xset (title.handle_value (), "fontweight", titlefontweight.get ());
 
+  txt_renderer.reset ();
+
   update_transform ();
   sync_positions ();
   override_defaults (bgo);
@@ -9110,6 +9112,15 @@
 }
 
 void
+text::reset_default_properties (void)
+{
+  remove_all_listeners ();
+  xreset_default_properties (get_handle (),
+                             xproperties.factory_defaults ());
+  xproperties.txt_renderer.reset ();
+}
+
+void
 text::properties::set_fontunits (const octave_value& val)
 {
   caseless_str old_fontunits = get_fontunits ();
diff -r 50439c2e8bbf -r cc59c1aca759 libinterp/corefcn/graphics.in.h
--- a/libinterp/corefcn/graphics.in.h	Wed Jan 16 20:32:15 2019 -0800
+++ b/libinterp/corefcn/graphics.in.h	Fri Jan 18 11:18:23 2019 +0100
@@ -4490,6 +4490,8 @@
 
   bool valid_object (void) const { return true; }
 
+  void reset_default_properties (void);
+    
   bool has_readonly_property (const caseless_str& pname) const
   {
     bool retval = xproperties.has_readonly_property (pname);
diff -r 50439c2e8bbf -r cc59c1aca759 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 11:18:23 2019 +0100
@@ -50,6 +50,25 @@
     delete rep;
   }
 
+  void
+  text_renderer::reset (void)
+  {
+    static bool warned = false;
+
+    if (! rep)
+      {
+        if (! warned)
+          {
+            warn_disabled_feature ("opengl_renderer::render_text",
+                                   "rendering text (FreeType)");
+
+            warned = true;
+          }
+      }
+    else
+      rep->reset ();
+  }
+
   bool
   text_renderer::ok (void) const
   {
diff -r 50439c2e8bbf -r cc59c1aca759 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 11:18:23 2019 +0100
@@ -55,6 +55,8 @@
 
     ~text_renderer (void);
 
+    void reset (void);
+
     bool ok (void) const;
 
     Matrix get_extent (text_element *elt, double rotation = 0.0);
