KiCad PCB EDA Suite
cairo_print.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 CERN
3  * Copyright (C) 2021 Kicad Developers, see AUTHORS.txt for contributors.
4  *
5  * Author: Maciej Suminski <maciej.suminski@cern.ch>
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <gal/cairo/cairo_print.h>
23 
24 #include <stdexcept>
25 #include <wx/dcclient.h>
26 #include <wx/dcgraph.h>
27 #include <wx/dcmemory.h>
28 #include <wx/dcprint.h>
29 
30 #ifdef NOMINMAX /* workaround for gdiplus.h */
31 #include <algorithm>
32 using std::max;
33 using std::min;
34 #endif
35 
36 #ifdef __WXMSW__
37 #include <windows.h>
38 #include <gdiplus.h>
39 #include <cairo-win32.h>
40 #include <wx/msw/enhmeta.h>
41 #endif /* __WXMSW__ */
42 
43 #ifdef __WXMAC__
44 #include <ApplicationServices/ApplicationServices.h>
45 #include <cairo-quartz.h>
46 #endif /* __WXMAC__ */
47 
48 using namespace KIGFX;
49 
51  m_gcdc( nullptr ),
52  m_ctx( nullptr ),
53  m_surface( nullptr )
54 {
55  if( wxPrinterDC* printerDC = dynamic_cast<wxPrinterDC*>( aDC ) )
56  m_gcdc = new wxGCDC( *printerDC );
57  else if( wxMemoryDC* memoryDC = dynamic_cast<wxMemoryDC*>( aDC ) )
58  m_gcdc = new wxGCDC( *memoryDC );
59  else if( wxWindowDC* windowDC = dynamic_cast<wxWindowDC*>( aDC ) )
60  m_gcdc = new wxGCDC( *windowDC );
61 #ifdef __WXMSW__
62  else if( wxEnhMetaFileDC* enhMFDC = dynamic_cast<wxEnhMetaFileDC*>( aDC ) )
63  m_gcdc = new wxGCDC( *enhMFDC );
64 #endif /* __WXMSW__ */
65  else
66  throw std::runtime_error( "Unhandled wxDC type" );
67 
68  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
69 
70  if( !gctx )
71  throw std::runtime_error( "Could not get the Graphics Context" );
72 
73 #ifdef __WXGTK__
74  m_ctx = static_cast<cairo_t*>( gctx->GetNativeContext() );
75  m_surface = cairo_get_target( m_ctx );
76 
77 // On linux, cairo printers have 72 DPI by default.
78 // This is an unusable resolution for us.
79 // A better resolution could be 4800 DPI (at 600 DPI, we still have minor
80 // but visible artifacts, for instance with arcs, but not at 4800 DPI)
81 // so modify the default:
82 #define DEFAULT_DPI 72.0
83 #define KICAD_PRINTER_DPI 4800.0
84 
85  // our device scale is DEFAULT_DPI / KICAD_PRINTER_DPI
86  cairo_surface_set_device_scale( m_surface, DEFAULT_DPI / KICAD_PRINTER_DPI,
87  DEFAULT_DPI / KICAD_PRINTER_DPI );
88  m_dpi = KICAD_PRINTER_DPI;
89 #endif /* __WXGTK__ */
90 
91 #ifdef __WXMSW__
92  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
93  m_hdc = g->GetHDC();
94  m_surface = cairo_win32_printing_surface_create( static_cast<HDC>( m_hdc ) );
95  m_ctx = cairo_create( m_surface );
96  m_dpi = GetDeviceCaps( static_cast<HDC>( m_hdc ), LOGPIXELSX );
97 #endif /* __WXMSW__ */
98 
99 #ifdef __WXMAC__
100  wxSize size = m_gcdc->GetSize();
101  CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
102  m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
103  m_ctx = cairo_create( m_surface );
104  wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
105  m_dpi = aDC->GetPPI().x;
106 #endif /* __WXMAC__ */
107 
108  if( !m_ctx || cairo_status( m_ctx ) != CAIRO_STATUS_SUCCESS )
109  throw std::runtime_error( "Could not create Cairo context" );
110 
111  if( !m_surface || cairo_surface_status( m_surface ) != CAIRO_STATUS_SUCCESS )
112  throw std::runtime_error( "Could not create Cairo surface" );
113 
114  cairo_reference( m_ctx );
115  cairo_surface_reference( m_surface );
116 }
117 
118 
120 {
121 #ifdef __WXMSW__
122  cairo_surface_show_page( m_surface );
123  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
124  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
125  g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
126 #endif /* __WXMSW__ */
127 
128  cairo_surface_destroy( m_surface );
129  cairo_destroy( m_ctx );
130  delete m_gcdc;
131 }
132 
133 
135  std::unique_ptr<CAIRO_PRINT_CTX> aContext ) :
136  CAIRO_GAL_BASE( aDisplayOptions )
137 {
138  m_printCtx = std::move( aContext );
139  m_context = m_currentContext = m_printCtx->GetContext();
140  m_surface = m_printCtx->GetSurface();
141  cairo_reference( m_context );
142  cairo_surface_reference( m_surface );
143  m_clearColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
145  resetContext();
146 
147  SetScreenDPI( m_printCtx->GetNativeDPI() );
148 }
149 
150 
152 {
154  const VECTOR2D paperSizeIU = VECTOR2D( m_nativePaperSize.y, m_nativePaperSize.x ) /* inches */
155  / m_worldUnitLength; /* 1" in IU */
156  const VECTOR2D paperSizeIUTransposed( paperSizeIU.y, paperSizeIU.x );
157 
158  MATRIX3x3D scale, translation, flip, rotate, lookat;
159 
160  scale.SetIdentity();
161  translation.SetIdentity();
162  flip.SetIdentity();
163  rotate.SetIdentity();
164  lookat.SetIdentity();
165 
167  {
168  translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIUTransposed );
169  }
170  else
171  {
172  if( isLandscape() )
173  {
174  translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIU );
175  rotate.SetRotation( 90.0 * M_PI / 180.0 );
176  }
177  else
178  {
179  translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIUTransposed );
180  }
181  }
182 
183  scale.SetScale( VECTOR2D( m_worldScale, m_worldScale ) );
184  flip.SetScale( VECTOR2D( m_globalFlipX ? -1.0 : 1.0, m_globalFlipY ? -1.0 : 1.0 ) );
185  lookat.SetTranslation( -m_lookAtPoint );
186 
187  m_worldScreenMatrix = scale * translation * flip * rotate * lookat;
189 }
190 
191 
192 void CAIRO_PRINT_GAL::SetNativePaperSize( const VECTOR2D& aSize, bool aHasNativeLandscapeRotation )
193 {
194  m_nativePaperSize = aSize;
195  m_hasNativeLandscapeRotation = aHasNativeLandscapeRotation;
196 }
197 
198 
200 {
201  // Convert aSize (inches) to pixels
202  SetScreenSize( VECTOR2I( std::ceil( aSize.x * m_screenDPI ) * 2,
203  std::ceil( aSize.y * m_screenDPI ) * 2 ) );
204 }
205 
206 
207 std::unique_ptr<GAL_PRINT> GAL_PRINT::Create( GAL_DISPLAY_OPTIONS& aOptions, wxDC* aDC )
208 {
209  auto printCtx = std::make_unique<CAIRO_PRINT_CTX>( aDC );
210  return std::make_unique<CAIRO_PRINT_GAL>( aOptions, std::move( printCtx ) );
211 }
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
double m_zoomFactor
The zoom factor.
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:245
MATRIX3x3D m_screenWorldMatrix
Screen transformation.
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:339
VECTOR2D m_lookAtPoint
Point to be looked at in world space.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
void SetScreenDPI(double aScreenDPI)
Set the dots per inch of the screen.
void SetIdentity()
Set the matrix to the identity matrix.
Definition: matrix3x3.h:210
MATRIX3x3D m_worldScreenMatrix
World transformation.
double m_worldScale
The scale factor world->screen.
double m_screenDPI
The dots per inch of the screen.
bool m_globalFlipX
Flag for X axis flipping.
cairo_surface_t * m_surface
Definition: cairo_print.h:71
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
void SetNativePaperSize(const VECTOR2D &aSize, bool aRotateIfLandscape) override
std::unique_ptr< CAIRO_PRINT_CTX > m_printCtx
Definition: cairo_print.h:126
bool isLandscape() const
< Returns true if page orientation is landscape
Definition: cairo_print.h:114
void SetScreenSize(const VECTOR2I &aSize)
void ComputeWorldScreenMatrix() override
Compute the world <-> screen transformation matrix.
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:341
cairo_t * m_context
Cairo image.
Definition: cairo_gal.h:342
const int scale
CAIRO_PRINT_CTX(wxDC *aDC)
Definition: cairo_print.cpp:50
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:257
bool m_globalFlipY
Flag for Y axis flipping.
CAIRO_PRINT_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, std::unique_ptr< CAIRO_PRINT_CTX > aContext)
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:226
#define DEFAULT_DPI
VECTOR2D m_nativePaperSize
Flag indicating whether the platform rotates page automatically or GAL needs to handle it in the tran...
Definition: cairo_print.h:120
double m_worldUnitLength
The unit length of the world coordinates [inch].
void SetSheetSize(const VECTOR2D &aSize) override
cairo_surface_t * m_surface
Cairo surface.
Definition: cairo_gal.h:343
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103