# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1584042347 -3600
#      Thu Mar 12 20:45:47 2020 +0100
# Branch stable
# Node ID 6faa61306a933e3ea14a31f7c271c263bc03bc5f
# Parent  4609d001daeefb6939b923a78806b332c91091f5
Fix inconsistent marker size between screen and printout (bug #57552)

* gl2ps-print.cc (gl2ps_renderer::init_marker): undo changes from cset
  46fe22bf1458.

* gl-render.h, gl-render.cc (m_printing): New bool data member.
  (opengl_renderer::draw_figure): Initialize m_printing with the depending on
  "__printing__" property.
  (opengl_renderer::points_to_pixels): New method to convert points data to
  pixels. Returns the orignal value when renderering for printing purpose.
  (opengl_renderer::set_linewidth, opengl_renderer::set_linewidth,
  opengl_renderer::make_marker_list): Use points_to_pixels.  


diff -r 4609d001daee -r 6faa61306a93 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Wed Mar 11 13:11:38 2020 -0700
+++ b/libinterp/corefcn/gl-render.cc	Thu Mar 12 20:45:47 2020 +0100
@@ -690,7 +690,7 @@
       m_devpixratio (1.), xform (), toolkit (), xZ1 (), xZ2 (), marker_id (),
       filled_marker_id (), camera_pos (), camera_dir (), view_vector (),
       interpreter ("none"), txt_renderer (), m_current_light (0),
-      m_max_lights (0), selecting (false)
+      m_max_lights (0), selecting (false), m_printing (false)
   {
     // This constructor will fail if we don't have OpenGL or if the data
     // types we assumed in our public interface aren't compatible with the
@@ -776,6 +776,8 @@
   void
   opengl_renderer::draw_figure (const figure::properties& props)
   {
+    m_printing = props.is___printing__ ();
+
     // Initialize OpenGL context
     init_gl_context (props.is_graphicssmoothing (), props.get_color_rgb ());
 
@@ -4088,19 +4090,8 @@
   opengl_renderer::set_linewidth (float w)
   {
 #if defined (HAVE_OPENGL)
-    gh_manager& gh_mgr = __get_gh_manager__ ("opengl_renderer::set_linewidth");
-
     // FIXME: See bug #53056 (measure LineWidth in points).
-    //        pts2pix and m_devpixratio should eventually be combined in to a
-    //        a single conversion factor so that only one multiplication per
-    //        function call is required.
-    // FIXME: Should this be static?  What happens if window is moved to a second
-    //        monitor with a different screenpixelsperinch?
-    const static double pts2pix
-      = (gh_mgr.get_object (0).get ("screenpixelsperinch").double_value ()
-         / 72.0);
-
-    m_glfcns.glLineWidth (w * pts2pix * m_devpixratio);
+    m_glfcns.glLineWidth (points_to_pixels (w)  * m_devpixratio);
 
 #else
 
@@ -4119,13 +4110,8 @@
                                   double linewidth)
   {
 #if defined (HAVE_OPENGL)
-    gh_manager& gh_mgr = __get_gh_manager__ ("opengl_renderer::set_linestyle");
-
     // FIXME: See bug #53056 (measure LineWidth in points).
-    const static double pts2pix
-      = (gh_mgr.get_object (0).get ("screenpixelsperinch").double_value ()
-         / 72.0);
-    int factor = math::round (linewidth * pts2pix * m_devpixratio);
+    int factor = math::round (points_to_pixels (linewidth)  * m_devpixratio);
     if (factor < 1)
       factor = 1;
 
@@ -4453,6 +4439,22 @@
 #endif
   }
 
+  double
+  opengl_renderer::points_to_pixels (const double val) const
+  {
+    gh_manager& gh_mgr = __get_gh_manager__ ("opengl_renderer::set_linestyle");
+
+    static const double pix_per_inch =
+      gh_mgr.get_object (0).get ("screenpixelsperinch").double_value ();
+
+    double retval = val;
+
+    if (! m_printing)
+      retval *= (pix_per_inch / 72.0);
+
+    return retval;
+  }
+
   unsigned int
   opengl_renderer::make_marker_list (const std::string& marker, double size,
                                      bool filled) const
@@ -4464,19 +4466,10 @@
     if (filled && (c == '+' || c == 'x' || c == '*' || c == '.'))
       return 0;
 
-    gh_manager& gh_mgr
-      = __get_gh_manager__ ("opengl_renderer::make_marker_list");
-
     unsigned int ID = m_glfcns.glGenLists (1);
 
     // FIXME: See bug #53056 (measure LineWidth in points).
-    // FIXME: Should this be static?  What happens if window is moved to a second
-    //        monitor with a different screenpixelsperinch?
-    const static double pts2pix
-      = (gh_mgr.get_object (0).get ("screenpixelsperinch").double_value ()
-         / 72.0);
-
-    double sz = size * pts2pix;
+    double sz = points_to_pixels (size);
 
     // constants for the * marker
     const double sqrt2d4 = 0.35355339059327;
diff -r 4609d001daee -r 6faa61306a93 libinterp/corefcn/gl-render.h
--- a/libinterp/corefcn/gl-render.h	Wed Mar 11 13:11:38 2020 -0700
+++ b/libinterp/corefcn/gl-render.h	Thu Mar 12 20:45:47 2020 +0100
@@ -183,6 +183,8 @@
 
     void set_normal (int bfl_mode, const NDArray& n, int j, int i);
 
+    double points_to_pixels (const double val) const;
+
     unsigned int make_marker_list (const std::string& m, double size,
                                    bool filled) const;
 
@@ -240,6 +242,9 @@
     // Indicate we are drawing for selection purpose
     bool selecting;
 
+    // Indicate we are drawing for printing purpose
+    bool m_printing;
+
   private:
     class patch_tessellator;
   };
diff -r 4609d001daee -r 6faa61306a93 libinterp/corefcn/gl2ps-print.cc
--- a/libinterp/corefcn/gl2ps-print.cc	Wed Mar 11 13:11:38 2020 -0700
+++ b/libinterp/corefcn/gl2ps-print.cc	Thu Mar 12 20:45:47 2020 +0100
@@ -189,15 +189,7 @@
 
     void init_marker (const std::string& m, double size, float width)
     {
-      // FIXME: Undo scaling that will take place in opengl_renderer::make_marker_list
-      gh_manager& gh_mgr
-        = __get_gh_manager__ ("gl2ps_renderer::init_marker");
-      // FIXME: Should this be static?  What happens if window is moved to a second
-      //        monitor with a different screenpixelsperinch?
-      const static double rescale
-        = 72.0 / gh_mgr.get_object (0).get ("screenpixelsperinch").double_value ();
-
-      opengl_renderer::init_marker (m, size * rescale, width);
+      opengl_renderer::init_marker (m, size, width);
 
       // FIXME: gl2ps can't handle closed contours so we set linecap/linejoin
       //        round to obtain a better looking result for some markers.
