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.

213 lines
7.0 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 at wanadoo.fr
  5. * Copyright (C) 1992-2022 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 <gal/graphics_abstraction_layer.h>
  30. #include <gal/painter.h>
  31. #include <pcbplot.h>
  32. #include <settings/app_settings.h>
  33. BOARD_PRINTOUT_SETTINGS::BOARD_PRINTOUT_SETTINGS( const PAGE_INFO& aPageInfo )
  34. : PRINTOUT_SETTINGS( aPageInfo )
  35. {
  36. m_LayerSet.set();
  37. m_Mirror = false;
  38. }
  39. void BOARD_PRINTOUT_SETTINGS::Load( APP_SETTINGS_BASE* aConfig )
  40. {
  41. PRINTOUT_SETTINGS::Load( aConfig );
  42. m_LayerSet.reset();
  43. for( int layer : aConfig->m_Printing.layers )
  44. m_LayerSet.set( layer, true );
  45. }
  46. void BOARD_PRINTOUT_SETTINGS::Save( APP_SETTINGS_BASE* aConfig )
  47. {
  48. PRINTOUT_SETTINGS::Save( aConfig );
  49. aConfig->m_Printing.layers.clear();
  50. for( unsigned layer = 0; layer < m_LayerSet.size(); ++layer )
  51. if( m_LayerSet.test( layer ) )
  52. aConfig->m_Printing.layers.push_back( layer );
  53. }
  54. BOARD_PRINTOUT::BOARD_PRINTOUT( const BOARD_PRINTOUT_SETTINGS& aParams,
  55. const KIGFX::VIEW* aView, const wxString& aTitle ) :
  56. wxPrintout( aTitle ),
  57. m_settings( aParams )
  58. {
  59. m_view = aView;
  60. m_gerbviewPrint = false;
  61. }
  62. void BOARD_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
  63. {
  64. *minPage = 1;
  65. *selPageFrom = 1;
  66. *maxPage = m_settings.m_pageCount;
  67. *selPageTo = m_settings.m_pageCount;
  68. }
  69. void BOARD_PRINTOUT::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
  70. {
  71. wxDC* dc = GetDC();
  72. KIGFX::GAL_DISPLAY_OPTIONS options;
  73. std::unique_ptr<KIGFX::GAL_PRINT> galPrint = KIGFX::GAL_PRINT::Create( options, dc );
  74. KIGFX::GAL* gal = galPrint->GetGAL();
  75. KIGFX::PRINT_CONTEXT* printCtx = galPrint->GetPrintCtx();
  76. std::unique_ptr<KIGFX::PAINTER> painter = getPainter( gal );
  77. std::unique_ptr<KIGFX::VIEW> view( m_view->DataReference() );
  78. // Target paper size
  79. wxRect pageSizePx = GetLogicalPageRect();
  80. const VECTOR2D pageSizeIn( (double) pageSizePx.width / dc->GetPPI().x,
  81. (double) pageSizePx.height / dc->GetPPI().y );
  82. const VECTOR2D pageSizeIU( milsToIU( pageSizeIn.x * 1000 ), milsToIU( pageSizeIn.y * 1000 ) );
  83. galPrint->SetSheetSize( pageSizeIn );
  84. view->SetGAL( gal );
  85. view->SetPainter( painter.get() );
  86. view->SetScaleLimits( 10e9, 0.0001 );
  87. view->SetScale( 1.0 );
  88. // Set the color scheme
  89. RENDER_SETTINGS* dstSettings = view->GetPainter()->GetSettings();
  90. dstSettings->LoadColors( m_settings.m_colorSettings );
  91. if( m_settings.m_blackWhite )
  92. {
  93. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  94. dstSettings->SetLayerColor( i, COLOR4D::BLACK );
  95. // In B&W mode, draw the background only in wxhite, because any other color
  96. // will be replaced by a black background
  97. dstSettings->SetBackgroundColor( COLOR4D::WHITE );
  98. }
  99. else // color enabled
  100. {
  101. for( int i = 0; i < LAYER_ID_COUNT; ++i )
  102. {
  103. // Cairo does not support translucent colors on PostScript surfaces
  104. // see 'Features support by the PostScript surface' on
  105. // https://www.cairographics.org/documentation/using_the_postscript_surface/
  106. dstSettings->SetLayerColor( i, dstSettings->GetLayerColor( i ).WithAlpha( 1.0 ) );
  107. }
  108. }
  109. dstSettings->SetIsPrinting( true );
  110. setupPainter( *painter );
  111. setupViewLayers( *view, m_settings.m_LayerSet );
  112. dstSettings->SetPrintLayers( m_settings.m_LayerSet );
  113. dstSettings->SetLayerName( aLayerName );
  114. VECTOR2I sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
  115. VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.x ),
  116. milsToIU( sheetSizeMils.y ) );
  117. BOX2I drawingAreaBBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
  118. // When printing the board without worksheet items, move board center to the
  119. // drawing area center.
  120. if( !m_settings.PrintBorderAndTitleBlock() )
  121. drawingAreaBBox = getBoundingBox();
  122. view->SetLayerVisible( LAYER_DRAWINGSHEET, m_settings.PrintBorderAndTitleBlock() );
  123. // Fit to page (drawingAreaBBox)
  124. if( m_settings.m_scale <= 0.0 )
  125. {
  126. if( drawingAreaBBox.GetWidth() == 0 || drawingAreaBBox.GetHeight() == 0 )
  127. {
  128. // Nothing to print (empty board and no worksheet)
  129. m_settings.m_scale = 1.0;
  130. }
  131. else
  132. {
  133. double scaleX = (double) pageSizeIU.x / drawingAreaBBox.GetWidth();
  134. double scaleY = (double) pageSizeIU.y / drawingAreaBBox.GetHeight();
  135. m_settings.m_scale = std::min( scaleX, scaleY );
  136. }
  137. }
  138. setupGal( gal );
  139. galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
  140. gal->SetLookAtPoint( drawingAreaBBox.Centre() );
  141. gal->SetZoomFactor( m_settings.m_scale );
  142. gal->SetClearColor( dstSettings->GetBackgroundColor() );
  143. gal->ClearScreen();
  144. if( m_gerbviewPrint )
  145. // Mandatory in Gerbview to use the same order for printing as for screen redraw
  146. // due to negative objects that need a specific order
  147. view->UseDrawPriority( true );
  148. {
  149. KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
  150. view->Redraw();
  151. }
  152. }
  153. void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
  154. {
  155. // Disable all layers by default, let specific implementations enable required layers
  156. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
  157. {
  158. aView.SetLayerVisible( i, false );
  159. aView.SetTopLayer( i, false );
  160. aView.SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
  161. }
  162. }
  163. void BOARD_PRINTOUT::setupPainter( KIGFX::PAINTER& aPainter )
  164. {
  165. if( !m_settings.m_background )
  166. aPainter.GetSettings()->SetBackgroundColor( COLOR4D::WHITE );
  167. }
  168. void BOARD_PRINTOUT::setupGal( KIGFX::GAL* aGal )
  169. {
  170. aGal->SetFlip( m_settings.m_Mirror, false );
  171. }