KiCad PCB EDA Suite
cairo_print.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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 NOMINMAX /* workaround for gdiplus.h */
29 #include <algorithm>
30 using std::min;
31 using std::max;
32 #endif
33 
34 #ifdef __WXMSW__
35 #include <windows.h>
36 #include <gdiplus.h>
37 #include <cairo-win32.h>
38 #include <wx/msw/enhmeta.h>
39 #endif /* __WXMSW__ */
40 
41 #ifdef __WXMAC__
42 #include <ApplicationServices/ApplicationServices.h>
43 #include <cairo-quartz.h>
44 #endif /* __WXMAC__ */
45 
46 using namespace KIGFX;
47 
49  : m_gcdc( nullptr ), m_ctx( nullptr ), m_surface( nullptr )
50 {
51  if( wxPrinterDC* printerDC = dynamic_cast<wxPrinterDC*>( aDC ) )
52  m_gcdc = new wxGCDC( *printerDC );
53  else if( wxMemoryDC* memoryDC = dynamic_cast<wxMemoryDC*>( aDC ) )
54  m_gcdc = new wxGCDC( *memoryDC );
55  else if( wxWindowDC* windowDC = dynamic_cast<wxWindowDC*>( aDC ) )
56  m_gcdc = new wxGCDC( *windowDC );
57 #ifdef __WXMSW__
58  else if( wxEnhMetaFileDC* enhMFDC = dynamic_cast<wxEnhMetaFileDC*>( aDC ) )
59  m_gcdc = new wxGCDC( *enhMFDC );
60 #endif /* __WXMSW__ */
61  else
62  throw std::runtime_error( "Unhandled wxDC type" );
63 
64  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
65 
66  if( !gctx )
67  throw std::runtime_error( "Could not get the Graphics Context" );
68 
69 #ifdef __WXGTK__
70  m_ctx = static_cast<cairo_t*>( gctx->GetNativeContext() );
71  m_surface = cairo_get_target( m_ctx );
72  // On linux, cairo printers have 72 DPI by default.
73  // This is an unusable resolution for us.
74  // A better resolution could be 4800 DPI (at 600 DPI, we still have minor
75  // but visible artifacts, for instance with arcs, but not at 4800 DPI)
76  // so modify the default:
77  #define DEFAULT_DPI 72.0
78  #define KICAD_PRINTER_DPI 4800.0
79  // our device scale is DEFAULT_DPI / KICAD_PRINTER_DPI
80  cairo_surface_set_device_scale( m_surface, DEFAULT_DPI/KICAD_PRINTER_DPI, DEFAULT_DPI/KICAD_PRINTER_DPI );
81  m_dpi = KICAD_PRINTER_DPI;
82 #endif /* __WXGTK__ */
83 
84 #ifdef __WXMSW__
85  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
86  m_hdc = g->GetHDC();
87  m_surface = cairo_win32_printing_surface_create( static_cast<HDC>( m_hdc ) );
88  m_ctx = cairo_create( m_surface );
89  m_dpi = GetDeviceCaps( static_cast<HDC>( m_hdc ), LOGPIXELSX );
90 #endif /* __WXMSW__ */
91 
92 #ifdef __WXMAC__
93  wxSize size = m_gcdc->GetSize();
94  CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
95  m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
96  m_ctx = cairo_create( m_surface );
97  wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
98  m_dpi = aDC->GetPPI().x;
99 #endif /* __WXMAC__ */
100 
101  if( !m_ctx || cairo_status( m_ctx ) != CAIRO_STATUS_SUCCESS )
102  throw std::runtime_error( "Could not create Cairo context" );
103 
104  if( !m_surface || cairo_surface_status( m_surface ) != CAIRO_STATUS_SUCCESS )
105  throw std::runtime_error( "Could not create Cairo surface" );
106 
107  cairo_reference( m_ctx );
108  cairo_surface_reference( m_surface );
109 }
110 
111 
113 {
114 #ifdef __WXMSW__
115  cairo_surface_show_page( m_surface );
116  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
117  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
118  g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
119 #endif /* __WXMSW__ */
120 
121  cairo_surface_destroy( m_surface );
122  cairo_destroy( m_ctx );
123  delete m_gcdc;
124 }
125 
126 
128  std::unique_ptr<CAIRO_PRINT_CTX> aContext )
129  : CAIRO_GAL_BASE( aDisplayOptions )
130 {
131  m_printCtx = std::move( aContext );
132  context = currentContext = m_printCtx->GetContext();
133  surface = m_printCtx->GetSurface();
134  cairo_reference( context );
135  cairo_surface_reference( surface );
136  m_clearColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
138  resetContext();
139 
140  SetScreenDPI( m_printCtx->GetNativeDPI() );
141 }
142 
143 
144 void CAIRO_PRINT_GAL::SetLineWidth( float aLineWidth )
145 {
146  storePath();
147  lineWidth = aLineWidth;
148  cairo_set_line_width( currentContext, aLineWidth );
149 }
150 
151 
153 {
155  const auto paperSizeIU = VECTOR2D( m_nativePaperSize.y, m_nativePaperSize.x ) /* inches */ / worldUnitLength; /* 1 inch in IU */
156  const auto paperSizeIUTransposed = VECTOR2D( 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 / zoomFactor * paperSizeIUTransposed );
169  }
170  else
171  {
172  if( isLandscape() )
173  {
174  translation.SetTranslation( 0.5 / zoomFactor * paperSizeIU );
175  rotate.SetRotation( 90.0 * M_PI / 180.0 );
176  }
177  else
178  {
179  translation.SetTranslation( 0.5 / zoomFactor * paperSizeIUTransposed );
180  }
181  }
182 
183  scale.SetScale( VECTOR2D( worldScale, worldScale ) );
184  flip.SetScale( VECTOR2D( globalFlipX ? -1.0 : 1.0, globalFlipY ? -1.0 : 1.0 ) );
185  lookat.SetTranslation( -lookAtPoint );
186 
187  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 * screenDPI ) * 2,
203  std::ceil( aSize.y * 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 }
double zoomFactor
The zoom factor.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
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:305
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:343
void storePath()
Store the actual path.
Definition: cairo_gal.cpp:1052
MATRIX3x3D screenWorldMatrix
Screen transformation.
bool globalFlipX
Flag for X axis flipping.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
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:593
void SetNativePaperSize(const VECTOR2D &aSize, bool aRotateIfLandscape) override
std::unique_ptr< CAIRO_PRINT_CTX > m_printCtx
Definition: cairo_print.h:125
bool isLandscape() const
Returns true if page orientation is landscape
Definition: cairo_print.h:113
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:304
MATRIX3x3D worldScreenMatrix
World transformation.
const int scale
CAIRO_PRINT_CTX(wxDC *aDC)
Definition: cairo_print.cpp:48
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.
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
#define DEFAULT_DPI
VECTOR2D m_nativePaperSize
Printout size
Definition: cairo_print.h:119
cairo_t * currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:303
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.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99