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