KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Author: Tomasz Wlostowski <[email protected]>
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
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>
32using std::max;
33using std::min;
34#endif
35
36#ifdef __WXMSW__
37#include <windows.h>
38#include <ObjIdl.h>
39#include <gdiplus.h>
40#include <cairo-win32.h>
41#include <wx/msw/enhmeta.h>
42#endif /* __WXMSW__ */
43
44
45#ifdef __WXMAC__
46#include <ApplicationServices/ApplicationServices.h>
47#include <cairo-quartz.h>
48#endif /* __WXMAC__ */
49
50using namespace KIGFX;
51
53 m_gcdc( nullptr ),
54 m_ctx( nullptr ),
55 m_surface( nullptr )
56{
57 if( wxPrinterDC* printerDC = dynamic_cast<wxPrinterDC*>( aDC ) )
58 m_gcdc = new wxGCDC( *printerDC );
59 else if( wxMemoryDC* memoryDC = dynamic_cast<wxMemoryDC*>( aDC ) )
60 m_gcdc = new wxGCDC( *memoryDC );
61 else if( wxWindowDC* windowDC = dynamic_cast<wxWindowDC*>( aDC ) )
62 m_gcdc = new wxGCDC( *windowDC );
63#ifdef __WXMSW__
64 else if( wxEnhMetaFileDC* enhMFDC = dynamic_cast<wxEnhMetaFileDC*>( aDC ) )
65 m_gcdc = new wxGCDC( *enhMFDC );
66#endif /* __WXMSW__ */
67 else
68 throw std::runtime_error( "Unhandled wxDC type" );
69
70 wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
71
72 if( !gctx )
73 throw std::runtime_error( "Could not get the Graphics Context" );
74
75#ifdef __WXGTK__
76 m_ctx = static_cast<cairo_t*>( gctx->GetNativeContext() );
77 m_surface = cairo_get_target( m_ctx );
78
79// On linux, cairo printers have 72 DPI by default.
80// This is an unusable resolution for us.
81// A better resolution could be 4800 DPI (at 600 DPI, we still have minor
82// but visible artifacts, for instance with arcs, but not at 4800 DPI)
83// so modify the default:
84#define DEFAULT_DPI 72.0
85#define KICAD_PRINTER_DPI 4800.0
86
87 // our device scale is DEFAULT_DPI / KICAD_PRINTER_DPI
88 cairo_surface_set_device_scale( m_surface, DEFAULT_DPI / KICAD_PRINTER_DPI,
89 DEFAULT_DPI / KICAD_PRINTER_DPI );
90 m_dpi = KICAD_PRINTER_DPI;
91#endif /* __WXGTK__ */
92
93#ifdef __WXMSW__
94 Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
95 m_hdc = g->GetHDC();
96 m_surface = cairo_win32_printing_surface_create( static_cast<HDC>( m_hdc ) );
97 m_ctx = cairo_create( m_surface );
98 m_dpi = GetDeviceCaps( static_cast<HDC>( m_hdc ), LOGPIXELSX );
99#endif /* __WXMSW__ */
100
101#ifdef __WXMAC__
102 wxSize size = m_gcdc->GetSize();
103 CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
104 m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
105 m_ctx = cairo_create( m_surface );
106 wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
107 m_dpi = aDC->GetPPI().x;
108#endif /* __WXMAC__ */
109
110 if( !m_ctx || cairo_status( m_ctx ) != CAIRO_STATUS_SUCCESS )
111 throw std::runtime_error( "Could not create Cairo context" );
112
113 if( !m_surface || cairo_surface_status( m_surface ) != CAIRO_STATUS_SUCCESS )
114 throw std::runtime_error( "Could not create Cairo surface" );
115
116 cairo_reference( m_ctx );
117 cairo_surface_reference( m_surface );
118}
119
120
122{
123#ifdef __WXMSW__
124 cairo_surface_show_page( m_surface );
125 wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
126 Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
127 g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
128#endif /* __WXMSW__ */
129
130 cairo_surface_destroy( m_surface );
131 cairo_destroy( m_ctx );
132 delete m_gcdc;
133}
134
135
137 std::unique_ptr<CAIRO_PRINT_CTX> aContext ) :
138 CAIRO_GAL_BASE( aDisplayOptions )
139{
140 m_printCtx = std::move( aContext );
141 m_context = m_currentContext = m_printCtx->GetContext();
142 m_surface = m_printCtx->GetSurface();
143 cairo_reference( m_context );
144 cairo_surface_reference( m_surface );
145 m_clearColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
147 resetContext();
148
149 SetScreenDPI( m_printCtx->GetNativeDPI() );
150}
151
152
154{
156 const VECTOR2D paperSizeIU = VECTOR2D( m_nativePaperSize.y, m_nativePaperSize.x ) /* inches */
157 / m_worldUnitLength; /* 1" in IU */
158 const VECTOR2D paperSizeIUTransposed( paperSizeIU.y, paperSizeIU.x );
159
160 MATRIX3x3D scale, translation, flip, rotate, lookat;
161
162 scale.SetIdentity();
163 translation.SetIdentity();
164 flip.SetIdentity();
165 rotate.SetIdentity();
166 lookat.SetIdentity();
167
169 {
170 translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIUTransposed );
171 }
172 else
173 {
174 if( isLandscape() )
175 {
176 translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIU );
177 rotate.SetRotation( 90.0 * M_PI / 180.0 );
178 }
179 else
180 {
181 translation.SetTranslation( 0.5 / m_zoomFactor * paperSizeIUTransposed );
182 }
183 }
184
185 scale.SetScale( VECTOR2D( m_worldScale, m_worldScale ) );
186 flip.SetScale( VECTOR2D( m_globalFlipX ? -1.0 : 1.0, m_globalFlipY ? -1.0 : 1.0 ) );
187 lookat.SetTranslation( -m_lookAtPoint );
188
189 m_worldScreenMatrix = scale * translation * flip * rotate * lookat;
191}
192
193
194void CAIRO_PRINT_GAL::SetNativePaperSize( const VECTOR2D& aSize, bool aHasNativeLandscapeRotation )
195{
196 m_nativePaperSize = aSize;
197 m_hasNativeLandscapeRotation = aHasNativeLandscapeRotation;
198}
199
200
202{
203 // Convert aSize (inches) to pixels
204 SetScreenSize( VECTOR2I( std::ceil( aSize.x * m_screenDPI ) * 2,
205 std::ceil( aSize.y * m_screenDPI ) * 2 ) );
206}
207
208
209std::unique_ptr<GAL_PRINT> GAL_PRINT::Create( GAL_DISPLAY_OPTIONS& aOptions, wxDC* aDC )
210{
211 auto printCtx = std::make_unique<CAIRO_PRINT_CTX>( aDC );
212 return std::make_unique<CAIRO_PRINT_GAL>( aOptions, std::move( printCtx ) );
213}
#define DEFAULT_DPI
cairo_surface_t * m_surface
Cairo surface.
Definition: cairo_gal.h:368
cairo_t * m_context
Cairo image.
Definition: cairo_gal.h:367
cairo_t * m_currentContext
Currently used Cairo context for drawing.
Definition: cairo_gal.h:366
CAIRO_PRINT_CTX(wxDC *aDC)
Definition: cairo_print.cpp:52
cairo_surface_t * m_surface
Definition: cairo_print.h:71
void SetSheetSize(const VECTOR2D &aSize) override
bool isLandscape() const
< Returns true if page orientation is landscape
Definition: cairo_print.h:114
CAIRO_PRINT_GAL(GAL_DISPLAY_OPTIONS &aDisplayOptions, std::unique_ptr< CAIRO_PRINT_CTX > aContext)
void SetNativePaperSize(const VECTOR2D &aSize, bool aRotateIfLandscape) override
std::unique_ptr< CAIRO_PRINT_CTX > m_printCtx
Definition: cairo_print.h:126
void ComputeWorldScreenMatrix() override
Compute the world <-> screen transformation matrix.
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
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
static std::unique_ptr< GAL_PRINT > Create(GAL_DISPLAY_OPTIONS &aOptions, wxDC *aDC)
MATRIX3x3D m_worldScreenMatrix
World transformation.
MATRIX3x3D m_screenWorldMatrix
Screen transformation.
double m_zoomFactor
The zoom factor.
double m_worldUnitLength
The unit length of the world coordinates [inch].
double m_worldScale
The scale factor world->screen.
bool m_globalFlipY
Flag for Y axis flipping.
void SetScreenSize(const VECTOR2I &aSize)
double m_screenDPI
The dots per inch of the screen.
bool m_globalFlipX
Flag for X axis flipping.
VECTOR2D m_lookAtPoint
Point to be looked at in world space.
void SetScreenDPI(double aScreenDPI)
Set the dots per inch of the screen.
void SetIdentity()
Set the matrix to the identity matrix.
Definition: matrix3x3.h:240
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:275
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:287
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:256
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:384
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
const int scale
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690