# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1508181886 -7200
#      Mon Oct 16 21:24:46 2017 +0200
# Node ID b74b50b368e3c1bba94fc7bda563ec9d043df7c2
# Parent  5d2d0e570e468acbb6145633afd1c95af21da07f
Add support for transparent polygons in svg output (bug #39535)

* gl-render.h/cc (opengl_renderer::set_blending): new virtual method to toggle
  the states of GL_BLEND and GL_ALPHA_TEST
  (opengl_renderer::init_gl_context, opengl_renderer::draw_text): use new method

* gl2ps-print.cc (gl2ps_renderer::set_blending): reimplement method to toggle 
  GL2PS_BLEND as well.
  (gl2ps_renderer::draw): remove GL2PS_NO_BLENDING from gl2psBeginPage options.

diff -r 5d2d0e570e46 -r b74b50b368e3 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Thu Oct 12 23:17:25 2017 +0200
+++ b/libinterp/corefcn/gl-render.cc	Mon Oct 16 21:24:46 2017 +0200
@@ -795,7 +795,7 @@
 
     if (enhanced)
       {
-        glEnable (GL_BLEND);
+        set_blending (true);
         glEnable (GL_MULTISAMPLE);
         bool has_multisample = false;
         if (! glGetError ())
@@ -821,7 +821,7 @@
       }
     else
       {
-        glDisable (GL_BLEND);
+        set_blending (false);
         glDisable (GL_LINE_SMOOTH);
       }
 
@@ -3732,6 +3732,44 @@
 #endif
   }
 
+  bool
+  opengl_renderer::set_blending (bool on, bool enable_alpha)
+  {
+#if defined (HAVE_OPENGL)
+
+    bool current = glIsEnabled (GL_BLEND);
+    
+    if (on && ! current)
+      {
+        printf ("Enabling!\n");
+        glEnable (GL_BLEND);
+        if (enable_alpha)
+          glEnable (GL_ALPHA_TEST);
+      }
+    else if (! on && current)
+      {
+        printf ("Disabling!\n");
+        if (! enable_alpha)
+          glDisable (GL_ALPHA_TEST);
+
+        glDisable (GL_BLEND);
+      }
+
+    return current;
+
+#else
+
+    octave_unused_parameter (on);
+    octave_unused_parameter (enable_alpha);
+
+    // This shouldn't happen because construction of opengl_renderer
+    // objects is supposed to be impossible if OpenGL is not available.
+
+    panic_impossible ();
+
+#endif
+  }
+  
   void
   opengl_renderer::set_color (const Matrix& c)
   {
@@ -3752,7 +3790,7 @@
 
 #endif
   }
-
+  
   void
   opengl_renderer::set_font (const base_properties& props)
   {
@@ -4296,18 +4334,14 @@
         uint8NDArray pixels;
         text_to_pixels (txt, pixels, bbox, halign, valign, rotation);
 
-        bool blend = glIsEnabled (GL_BLEND);
-
-        glEnable (GL_BLEND);
-        glEnable (GL_ALPHA_TEST);
+        bool blend = set_blending (true, true);
+
         glRasterPos3d (x, y, z);
         glBitmap(0, 0, 0, 0, bbox(0), bbox(1), nullptr);
         glDrawPixels (bbox(2), bbox(3),
                       GL_RGBA, GL_UNSIGNED_BYTE, pixels.data ());
-        glDisable (GL_ALPHA_TEST);
-
-        if (! blend)
-          glDisable (GL_BLEND);
+        
+        set_blending (blend);
       }
 
     return bbox;
diff -r 5d2d0e570e46 -r b74b50b368e3 libinterp/corefcn/gl-render.h
--- a/libinterp/corefcn/gl-render.h	Thu Oct 12 23:17:25 2017 +0200
+++ b/libinterp/corefcn/gl-render.h	Mon Oct 16 21:24:46 2017 +0200
@@ -84,6 +84,7 @@
     virtual void init_gl_context (bool enhanced, const Matrix& backgroundColor);
     virtual void setup_opengl_transformation (const axes::properties& props);
 
+    virtual bool set_blending (bool on, bool enable_alpha = false);
     virtual void set_color (const Matrix& c);
     virtual void set_polygon_offset (bool on, float offset = 0.0f);
     virtual void set_linewidth (float w);
diff -r 5d2d0e570e46 -r b74b50b368e3 libinterp/corefcn/gl2ps-print.cc
--- a/libinterp/corefcn/gl2ps-print.cc	Thu Oct 12 23:17:25 2017 +0200
+++ b/libinterp/corefcn/gl2ps-print.cc	Mon Oct 16 21:24:46 2017 +0200
@@ -127,6 +127,16 @@
     void draw_pixels (int w, int h, const uint8_t *data);
     void draw_pixels (int w, int h, const uint16_t *data);
 
+    bool set_blending (bool on, bool enable_alpha)
+    {
+      opengl_renderer::set_blending (on, enable_alpha);
+ 
+      if (on)
+        gl2psEnable (GL2PS_BLEND);
+      else
+        gl2psDisable (GL2PS_BLEND);
+    }
+
     void set_linestyle (const std::string& s, bool use_stipple = false,
                         double linewidth = 0.5)
     {
@@ -335,10 +345,9 @@
               include_graph = "foobar-inc";
 
             // GL2PS_SILENT was removed to allow gl2ps to print errors on stderr
-            GLint ret = gl2psBeginPage ("gl2ps_renderer figure", "Octave", nullptr,
-                                        gl2ps_term, gl2ps_sort,
-                                        (GL2PS_NO_BLENDING
-                                         | GL2PS_OCCLUSION_CULL
+            GLint ret = gl2psBeginPage ("gl2ps_renderer figure", "Octave", 
+                                        nullptr, gl2ps_term, gl2ps_sort,
+                                        (GL2PS_OCCLUSION_CULL
                                          | GL2PS_BEST_ROOT
                                          | gl2ps_text
                                          | GL2PS_DRAW_BACKGROUND
