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.

209 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. RENDER_SETTINGS* 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. dstSettings->SetIsPrinting( true );
  105. setupPainter( *painter );
  106. setupViewLayers( *view, m_settings.m_LayerSet );
  107. auto sheetSizeMils = m_settings.m_pageInfo.GetSizeMils();
  108. VECTOR2I sheetSizeIU( milsToIU( sheetSizeMils.GetWidth() ), milsToIU( sheetSizeMils.GetHeight() ) );
  109. BOX2I bBox;
  110. // Determine printout bounding box
  111. if( m_settings.PrintBorderAndTitleBlock() )
  112. {
  113. bBox = BOX2I( VECTOR2I( 0, 0 ), VECTOR2I( sheetSizeIU ) );
  114. view->SetLayerVisible( LAYER_DRAWINGSHEET, true );
  115. }
  116. else
  117. {
  118. EDA_RECT targetBbox = getBoundingBox();
  119. bBox = BOX2I( targetBbox.GetOrigin(), targetBbox.GetSize() );
  120. view->SetLayerVisible( LAYER_DRAWINGSHEET, false );
  121. }
  122. // Fit to page
  123. if( m_settings.m_scale <= 0.0 )
  124. {
  125. if( bBox.GetWidth() == 0 || bBox.GetHeight() == 0 )
  126. {
  127. // Nothing to print
  128. m_settings.m_scale = 1.0;
  129. }
  130. else
  131. {
  132. double scaleX = (double) pageSizeIU.x / bBox.GetWidth();
  133. double scaleY = (double) pageSizeIU.y / bBox.GetHeight();
  134. m_settings.m_scale = std::min( scaleX, scaleY );
  135. }
  136. }
  137. view->SetPrintMode( 1 );
  138. setupGal( gal );
  139. galPrint->SetNativePaperSize( pageSizeIn, printCtx->HasNativeLandscapeRotation() );
  140. gal->SetLookAtPoint( bBox.Centre() );
  141. gal->SetZoomFactor( m_settings.m_scale );
  142. gal->SetClearColor( dstSettings->GetBackgroundColor() );
  143. gal->ClearScreen();
  144. {
  145. KIGFX::GAL_DRAWING_CONTEXT ctx( gal );
  146. view->Redraw();
  147. }
  148. view->SetPrintMode( 0 );
  149. }
  150. void BOARD_PRINTOUT::setupViewLayers( KIGFX::VIEW& aView, const LSET& aLayerSet )
  151. {
  152. // Disable all layers by default, let specific implementions enable required layers
  153. for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; ++i )
  154. {
  155. aView.SetLayerVisible( i, false );
  156. aView.SetTopLayer( i, false );
  157. aView.SetLayerTarget( i, KIGFX::TARGET_NONCACHED );
  158. }
  159. }
  160. void BOARD_PRINTOUT::setupPainter( KIGFX::PAINTER& aPainter )
  161. {
  162. if( !m_settings.m_background )
  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. }