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.

207 lines
6.3 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. #include <settings/app_settings.h>
  32. BOARD_PRINTOUT_SETTINGS::BOARD_PRINTOUT_SETTINGS( const PAGE_INFO& aPageInfo )
  33. : PRINTOUT_SETTINGS( aPageInfo )
  34. {
  35. m_layerSet.set();
  36. m_mirror = false;
  37. }
  38. void BOARD_PRINTOUT_SETTINGS::Load( APP_SETTINGS_BASE* aConfig )
  39. {
  40. PRINTOUT_SETTINGS::Load( aConfig );
  41. m_layerSet.reset();
  42. for( int layer : aConfig->m_Printing.layers )
  43. m_layerSet.set( layer, true );
  44. }
  45. void BOARD_PRINTOUT_SETTINGS::Save( APP_SETTINGS_BASE* aConfig )
  46. {
  47. PRINTOUT_SETTINGS::Save( aConfig );
  48. aConfig->m_Printing.layers.clear();
  49. for( unsigned layer = 0; layer < m_layerSet.size(); ++layer )
  50. if( m_layerSet.test( layer ) )
  51. aConfig->m_Printing.layers.push_back( layer );
  52. }
  53. BOARD_PRINTOUT::BOARD_PRINTOUT( const BOARD_PRINTOUT_SETTINGS& aParams,
  54. const KIGFX::VIEW* aView, const wxString& aTitle ) :
  55. wxPrintout( aTitle ),
  56. m_settings( aParams )
  57. {
  58. m_view = aView;
  59. }
  60. void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
  61. {
  62. *minPage = 1;
  63. *selPageFrom = 1;
  64. *maxPage = m_settings.m_pageCount;
  65. *selPageTo = m_settings.m_pageCount;
  66. }
  67. void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
  68. {
  69. auto dc = GetDC();
  70. KIGFX::GAL_DISPLAY_OPTIONS options;
  71. auto galPrint = KIGFX::GAL_PRINT::Create( options, dc );
  72. auto gal = galPrint->GetGAL();
  73. auto printCtx = galPrint->GetPrintCtx();
  74. auto painter = getPainter( gal );
  75. std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
  76. // Target paper size
  77. wxRect pageSizePx = GetLogicalPageRect();
  78. const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
  79. (double) pageSizePx.height / dc->GetPPI().y );
  80. galPrint->SetSheetSize( pageSizeIn );
  81. const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
  82. view->SetGAL( gal );
  83. view->SetPainter( painter.get() );
  84. view->SetScaleLimits( 10e9, 0.0001 );
  85. view->SetScale( 1.0 );
  86. // Set the color scheme
  87. auto dstSettings = view->GetPainter()->GetSettings();
  88. dstSettings->LoadColors( m_settings.m_colorSettings );
  89. if( m_settings.m_blackWhite )
  90. {
  91. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  92. dstSettings->SetLayerColor( i, COLOR4D::BLACK );
  93. }
  94. else // color enabled
  95. {
  96. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  97. {
  98. // Cairo does not support translucent colors on PostScript surfaces
  99. // see 'Features support by the PostScript surface' on
  100. // https://www.cairographics.org/documentation/using_the_postscript_surface/
  101. dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
  102. }
  103. }
  104. setupViewLayers( *view, m_settings.m_layerSet );
  105. setupPainter( *painter );
  106. auto sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
  107. VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.GetWidth() ), milsToIU( sheetSizeMils.GetHeight() ) );
  108. BOX2I bBox;
  109. // Determine printout bounding box
  110. if( m_settings.PrintBorderAndTitleBlock() )
  111. {
  112. bBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
  113. view->SetLayerVisible( LAYER_WORKSHEET, true );
  114. }
  115. else
  116. {
  117. EDA_RECT targetBbox = getBoundingBox();
  118. bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
  119. view->SetLayerVisible( LAYER_WORKSHEET, false );
  120. }
  121. // Fit to page
  122. if( m_settings.m_scale <= 0.0 )
  123. {
  124. if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
  125. {
  126. // Nothing to print
  127. m_settings.m_scale = 1.0;
  128. }
  129. else
  130. {
  131. double scaleX = (double) pageSizeIU.x / bBox.GetWidth();
  132. double scaleY = (double) pageSizeIU.y / bBox.GetHeight();
  133. m_settings.m_scale = std::min( scaleX, scaleY );
  134. }
  135. }
  136. view->SetPrintMode( 1 );
  137. setupGal( gal );
  138. galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
  139. gal->SetLookAtPoint( bBox.Centre() );
  140. gal->SetZoomFactor( m_settings.m_scale );
  141. gal->SetClearColor( dstSettings->GetBackgroundColor() );
  142. gal->ClearScreen();
  143. {
  144. KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
  145. view->Redraw();
  146. }
  147. view->SetPrintMode( 0 );
  148. }
  149. void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
  150. {
  151. // Disable all layers by default, let specific implementions enable required layers
  152. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
  153. {
  154. aView.SetLayerVisible( i, false );
  155. aView.SetTopLayer( i, false );
  156. aView.SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
  157. }
  158. }
  159. void BOARD_PRINTOUT::setupPainter( KIGFX::PAINTER& aPainter )
  160. {
  161. if( !m_settings.m_background )
  162. aPainter.GetSettings()->SetBackgroundColor( COLOR4D::WHITE );
  163. }
  164. void BOARD_PRINTOUT::setupGal( KIGFX::GAL* aGal )
  165. {
  166. aGal->SetFlip( m_settings.m_mirror, false );
  167. }