From 92c5cb288eb3ab2cff61e6c8e49bb3261352808c Mon Sep 17 00:00:00 2001
From: Maciej Suminski <maciej.suminski@cern.ch>
Date: Sat, 29 Sep 2018 12:13:06 +0200
Subject: [PATCH] Fixed scale in Gerbview printouts

---
 common/board_printout.cpp                       |  1 +
 common/gal/cairo/cairo_print.cpp                | 11 ++++-------
 gerbview/dialogs/dialog_print_using_printer.cpp |  1 +
 gerbview/gerbview_printout.cpp                  |  6 ++++++
 gerbview/gerbview_printout.h                    |  2 ++
 include/board_printout.h                        |  3 +++
 pcbnew/pcbnew_printout.cpp                      |  6 ++++++
 pcbnew/pcbnew_printout.h                        |  2 ++
 8 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/common/board_printout.cpp b/common/board_printout.cpp
index 52cd2e3115..297ba7481c 100644
--- a/common/board_printout.cpp
+++ b/common/board_printout.cpp
@@ -164,6 +164,7 @@ void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPa
     // TODO fix 'Preview' button
     VECTOR2D nps( GetLogicalPageRect().width, GetLogicalPageRect().height );
 
+    setupGal( gal );
     galPrint->SetNativePaperSize( VECTOR2D( nps.x / dc->GetPPI().x, nps.y / dc->GetPPI().y ),
             printCtx->HasNativeLandscapeRotation() );
     gal->SetLookAtPoint( bBox.Centre() );
diff --git a/common/gal/cairo/cairo_print.cpp b/common/gal/cairo/cairo_print.cpp
index ccfa1acbfc..2f558314a0 100644
--- a/common/gal/cairo/cairo_print.cpp
+++ b/common/gal/cairo/cairo_print.cpp
@@ -135,11 +135,8 @@ void CAIRO_PRINT_GAL::SetLineWidth( float aLineWidth )
 
 void CAIRO_PRINT_GAL::ComputeWorldScreenMatrix()
 {
-    // worldUnitLength = inch per integer
-    worldUnitLength = 1e-9 /* 1 nm */ / 0.0254 /* 1 inch in meters */;
     worldScale = screenDPI * worldUnitLength * zoomFactor;
-
-    const auto paperSizeIU = VECTOR2D( m_nativePaperSize.y, m_nativePaperSize.x ) /* inches */ * 0.0254 * 1e9 /* 1 inch in nm */;
+    const auto paperSizeIU = VECTOR2D( m_nativePaperSize.y, m_nativePaperSize.x ) /* inches */ / worldUnitLength; /* 1 inch in IU */
     const auto paperSizeIUTransposed = VECTOR2D( paperSizeIU.y, paperSizeIU.x );
 
     MATRIX3x3D scale, translation, flip, rotate, lookat;
@@ -152,18 +149,18 @@ void CAIRO_PRINT_GAL::ComputeWorldScreenMatrix()
 
     if( m_hasNativeLandscapeRotation )
     {
-        translation.SetTranslation( 0.5 / GetZoomFactor() * paperSizeIUTransposed );
+        translation.SetTranslation( 0.5 / zoomFactor * paperSizeIUTransposed );
     }
     else
     {
         if( isLandscape() )
         {
-            translation.SetTranslation( 0.5 / GetZoomFactor() * paperSizeIU );
+            translation.SetTranslation( 0.5 / zoomFactor * paperSizeIU );
             rotate.SetRotation( 90.0 * M_PI / 180.0 );
         }
         else
         {
-            translation.SetTranslation( 0.5 / GetZoomFactor() * paperSizeIUTransposed );
+            translation.SetTranslation( 0.5 / zoomFactor * paperSizeIUTransposed );
         }
     }
 
diff --git a/gerbview/dialogs/dialog_print_using_printer.cpp b/gerbview/dialogs/dialog_print_using_printer.cpp
index cd8fc60054..e918f02d8a 100644
--- a/gerbview/dialogs/dialog_print_using_printer.cpp
+++ b/gerbview/dialogs/dialog_print_using_printer.cpp
@@ -226,6 +226,7 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
         m_ModeColorOption->SetSelection( 0 );
 
     s_Parameters.m_PenDefaultSize = 0;
+    s_Parameters.m_Print_Sheet_Ref = false;
 
     // Create scale adjust option
     msg.Printf( wxT( "%f" ), s_Parameters.m_XScaleAdjust );
diff --git a/gerbview/gerbview_printout.cpp b/gerbview/gerbview_printout.cpp
index 7c25b49655..ed480e3768 100644
--- a/gerbview/gerbview_printout.cpp
+++ b/gerbview/gerbview_printout.cpp
@@ -98,6 +98,12 @@ void GERBVIEW_PRINTOUT::setupViewLayers( const std::unique_ptr<KIGFX::VIEW>& aVi
 }
 
 
+void GERBVIEW_PRINTOUT::setupGal( KIGFX::GAL* aGal )
+{
+    aGal->SetWorldUnitLength( 10e-9 /* 10 nm */ / 0.0254 /* 1 inch in meters */ );
+}
+
+
 EDA_RECT GERBVIEW_PRINTOUT::getBoundingBox()
 {
     return m_layout->ComputeBoundingBox();
diff --git a/gerbview/gerbview_printout.h b/gerbview/gerbview_printout.h
index 71270cc269..07a991dfde 100644
--- a/gerbview/gerbview_printout.h
+++ b/gerbview/gerbview_printout.h
@@ -35,6 +35,8 @@ public:
 protected:
     void setupViewLayers( const std::unique_ptr<KIGFX::VIEW>& aView, const LSET& aLayerSet ) override;
 
+    void setupGal( KIGFX::GAL* aGal ) override;
+
     EDA_RECT getBoundingBox() override;
 
     std::unique_ptr<KIGFX::PAINTER> getPainter( KIGFX::GAL* aGal ) override;
diff --git a/include/board_printout.h b/include/board_printout.h
index fdc5b2ee69..e27e7dabe2 100644
--- a/include/board_printout.h
+++ b/include/board_printout.h
@@ -131,6 +131,9 @@ protected:
     ///> Configures PAINTER object for a printout
     virtual void setupPainter( const std::unique_ptr<KIGFX::PAINTER>& aPainter );
 
+    ///> Configures GAL object for a printout
+    virtual void setupGal( KIGFX::GAL* aGal ) {}
+
     ///> Returns bounding box of the printed objects (excluding worksheet frame)
     virtual EDA_RECT getBoundingBox() = 0;
 
diff --git a/pcbnew/pcbnew_printout.cpp b/pcbnew/pcbnew_printout.cpp
index 38554204e7..53aea3084b 100644
--- a/pcbnew/pcbnew_printout.cpp
+++ b/pcbnew/pcbnew_printout.cpp
@@ -159,6 +159,12 @@ void PCBNEW_PRINTOUT::setupPainter( const std::unique_ptr<KIGFX::PAINTER>& aPain
 }
 
 
+void PCBNEW_PRINTOUT::setupGal( KIGFX::GAL* aGal )
+{
+    aGal->SetWorldUnitLength( 1e-9 /* 1 nm */ / 0.0254 /* 1 inch in meters */ );
+}
+
+
 EDA_RECT PCBNEW_PRINTOUT::getBoundingBox()
 {
     return m_board->ComputeBoundingBox();
diff --git a/pcbnew/pcbnew_printout.h b/pcbnew/pcbnew_printout.h
index 0dd3f95d94..3a3f050be5 100644
--- a/pcbnew/pcbnew_printout.h
+++ b/pcbnew/pcbnew_printout.h
@@ -38,6 +38,8 @@ protected:
 
     void setupPainter( const std::unique_ptr<KIGFX::PAINTER>& aPainter ) override;
 
+    void setupGal( KIGFX::GAL* aGal ) override;
+
     EDA_RECT getBoundingBox() override;
 
     std::unique_ptr<KIGFX::PAINTER> getPainter( KIGFX::GAL* aGal ) override;