You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Copyright (C) 2018 CERN
  7. * Author: Maciej Suminski <maciej.suminski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <board_printout.h>
  27. #include <view/view.h>
  28. #include <gal/gal_print.h>
  29. #include <painter.h>
  30. #include <pcbplot.h>
  31. BOARD_PRINTOUT_SETTINGS::BOARD_PRINTOUT_SETTINGS( const PAGE_INFO& aPageInfo )
  32. : PRINTOUT_SETTINGS( aPageInfo )
  33. {
  34. m_layerSet.set();
  35. m_mirror = false;
  36. }
  37. void BOARD_PRINTOUT_SETTINGS::Load( wxConfigBase* aConfig )
  38. {
  39. PRINTOUT_SETTINGS::Load( aConfig );
  40. for( unsigned layer = 0; layer < m_layerSet.size(); ++layer )
  41. {
  42. int tmp;
  43. wxString key = wxString::Format( OPTKEY_LAYERBASE, layer );
  44. aConfig->Read( key, &tmp, 1 );
  45. m_layerSet.set( layer, tmp );
  46. }
  47. }
  48. void BOARD_PRINTOUT_SETTINGS::Save( wxConfigBase* aConfig )
  49. {
  50. PRINTOUT_SETTINGS::Save( aConfig );
  51. for( unsigned layer = 0; layer < m_layerSet.size(); ++layer )
  52. {
  53. wxString key = wxString::Format( OPTKEY_LAYERBASE, layer );
  54. aConfig->Write( key, m_layerSet.test( layer ) );
  55. }
  56. }
  57. BOARD_PRINTOUT::BOARD_PRINTOUT( const BOARD_PRINTOUT_SETTINGS& aParams,
  58. const KIGFX::VIEW* aView, const wxString& aTitle ) :
  59. wxPrintout( aTitle ), m_settings( aParams )
  60. {
  61. m_view = aView;
  62. }
  63. void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
  64. {
  65. *minPage = 1;
  66. *selPageFrom = 1;
  67. *maxPage = m_settings.m_pageCount;
  68. *selPageTo = m_settings.m_pageCount;
  69. }
  70. void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
  71. {
  72. auto dc = GetDC();
  73. KIGFX::GAL_DISPLAY_OPTIONS options;
  74. auto galPrint = KIGFX::GAL_PRINT::Create( options, dc );
  75. auto gal = galPrint->GetGAL();
  76. auto printCtx = galPrint->GetPrintCtx();
  77. auto painter = getPainter( gal );
  78. std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
  79. // Target paper size
  80. wxRect pageSizePx = GetLogicalPageRect();
  81. const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
  82. (double) pageSizePx.height / dc->GetPPI().y );
  83. galPrint->SetSheetSize( pageSizeIn );
  84. const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
  85. view->SetGAL( gal );
  86. view->SetPainter( painter.get() );
  87. view->SetScaleLimits( 10e9, 0.0001 );
  88. view->SetScale( 1.0 );
  89. // Set the color scheme
  90. auto srcSettings = m_view->GetPainter()->GetSettings();
  91. auto dstSettings = view->GetPainter()->GetSettings();
  92. if( m_settings.m_blackWhite )
  93. {
  94. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  95. dstSettings->SetLayerColor( i, COLOR4D::BLACK );
  96. }
  97. else // color enabled
  98. {
  99. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  100. {
  101. // Cairo does not support translucent colors on PostScript surfaces
  102. // see 'Features support by the PostScript surface' on
  103. // ttps://www.cairographics.org/documentation/using_the_postscript_surface/
  104. dstSettings->SetLayerColor( i, srcSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
  105. }
  106. }
  107. setupViewLayers( view, m_settings.m_layerSet );
  108. setupPainter( painter );
  109. auto sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
  110. VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.GetWidth() ), milsToIU( sheetSizeMils.GetHeight() ) );
  111. BOX2I bBox;
  112. // Determine printout bounding box
  113. if( m_settings.PrintBorderAndTitleBlock() )
  114. {
  115. bBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
  116. view->SetLayerVisible( LAYER_WORKSHEET, true );
  117. }
  118. else
  119. {
  120. EDA_RECT targetBbox = getBoundingBox();
  121. bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
  122. view->SetLayerVisible( LAYER_WORKSHEET, false );
  123. }
  124. // Fit to page
  125. if( m_settings.m_scale <= 0.0 )
  126. {
  127. if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
  128. {
  129. // Nothing to print
  130. m_settings.m_scale = 1.0;
  131. }
  132. else
  133. {
  134. double scaleX = (double) pageSizeIU.x / bBox.GetWidth();
  135. double scaleY = (double) pageSizeIU.y / bBox.GetHeight();
  136. m_settings.m_scale = std::min( scaleX, scaleY );
  137. }
  138. }
  139. view->SetPrintMode( 1 );
  140. setupGal( gal );
  141. galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
  142. gal->SetLookAtPoint( bBox.Centre() );
  143. gal->SetZoomFactor( m_settings.m_scale );
  144. {
  145. KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
  146. view->Redraw();
  147. }
  148. view->SetPrintMode( 0 );
  149. }
  150. void BOARD_PRINTOUT::setupViewLayers( const std::unique_ptr<KIGFX::VIEW>& aView,
  151. const LSET& aLayerSet )
  152. {
  153. // Disable all layers by default, let specific implementions enable required layers
  154. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
  155. {
  156. aView->SetLayerVisible( i, false );
  157. aView->SetTopLayer( i, false );
  158. aView->SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
  159. }
  160. }
  161. void BOARD_PRINTOUT::setupPainter( const std::unique_ptr<KIGFX::PAINTER>& aPainter )
  162. {
  163. aPainter->GetSettings()->SetBackgroundColor( COLOR4D::WHITE );
  164. }
  165. void BOARD_PRINTOUT::setupGal( KIGFX::GAL* aGal )
  166. {
  167. aGal->SetFlip( m_settings.m_mirror, false );
  168. }