25#include <wx/dcclient.h>
26#include <wx/dcgraph.h>
27#include <wx/dcmemory.h>
28#include <wx/dcprint.h>
41#include <cairo-win32.h>
42#include <wx/msw/enhmeta.h>
47#include <ApplicationServices/ApplicationServices.h>
48#include <cairo-quartz.h>
60 m_surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, aImage->GetWidth(), aImage->GetHeight() );
63 throw std::runtime_error(
"Could not create Cairo surface" );
67 if( !
m_ctx || cairo_status(
m_ctx ) != CAIRO_STATUS_SUCCESS )
68 throw std::runtime_error(
"Could not create Cairo context" );
70 cairo_set_antialias(
m_ctx, CAIRO_ANTIALIAS_GOOD );
76 if( wxPrinterDC* printerDC =
dynamic_cast<wxPrinterDC*
>( aDC ) )
77 m_gcdc =
new wxGCDC( *printerDC );
78 else if( wxMemoryDC* memoryDC =
dynamic_cast<wxMemoryDC*
>( aDC ) )
79 m_gcdc =
new wxGCDC( *memoryDC );
80 else if( wxWindowDC* windowDC =
dynamic_cast<wxWindowDC*
>( aDC ) )
81 m_gcdc =
new wxGCDC( *windowDC );
83 else if( wxEnhMetaFileDC* enhMFDC =
dynamic_cast<wxEnhMetaFileDC*
>( aDC ) )
84 m_gcdc =
new wxGCDC( *enhMFDC );
87 throw std::runtime_error(
"Unhandled wxDC type" );
89 wxGraphicsContext* gctx =
m_gcdc->GetGraphicsContext();
92 throw std::runtime_error(
"Could not get the Graphics Context" );
95 m_ctx =
static_cast<cairo_t*
>( gctx->GetNativeContext() );
103#define DEFAULT_DPI 72.0
104#define KICAD_PRINTER_DPI 4800.0
109 m_dpi = KICAD_PRINTER_DPI;
111 cairo_reference(
m_ctx );
116 Gdiplus::Graphics* g =
static_cast<Gdiplus::Graphics*
>( gctx->GetNativeContext() );
118 m_surface = cairo_win32_printing_surface_create(
static_cast<HDC
>( m_hdc ) );
120 wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
121 m_dpi = aDC->GetPPI().x;
125 wxSize size =
m_gcdc->GetSize();
126 CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
127 m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
129 wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
130 m_dpi = aDC->GetPPI().x;
133 if( !
m_ctx || cairo_status(
m_ctx ) != CAIRO_STATUS_SUCCESS )
134 throw std::runtime_error(
"Could not create Cairo context" );
137 throw std::runtime_error(
"Could not create Cairo surface" );
150 unsigned char* srcData = cairo_image_surface_get_data(
m_surface );
151 int height = cairo_image_surface_get_height(
m_surface );
152 int stride = cairo_image_surface_get_stride(
m_surface );
156 unsigned char* srcRow = srcData;
158 for(
int y = 0; y < height; y++ )
160 for(
int x = 0; x < stride; x += 4 )
162 const uint32_t pix = *(uint32_t*) ( srcRow + x );
164 const uint8_t b = pix >> 0;
165 const uint8_t g = pix >> 8;
166 const uint8_t r = pix >> 16;
167 const uint8_t alpha = pix >> 24;
172 dstRGB[0] = dstRGB[1] = dstRGB[2] = 0;
177 dstRGB[0] = ( (uint32_t) r * 255 ) / alpha;
178 dstRGB[1] = ( (uint32_t) g * 255 ) / alpha;
179 dstRGB[2] = ( (uint32_t) b * 255 ) / alpha;
195 cairo_destroy(
m_ctx );
202 std::unique_ptr<CAIRO_PRINT_CTX> aContext ) :
223 const VECTOR2D paperSizeIUTransposed( paperSizeIU.
y, paperSizeIU.
x );
276 auto printCtx = std::make_unique<CAIRO_PRINT_CTX>( aDC );
277 return std::make_unique<CAIRO_PRINT_GAL>( aOptions, std::move( printCtx ) );
283 auto printCtx = std::make_unique<CAIRO_PRINT_CTX>( aImage, aDPI );
284 return std::make_unique<CAIRO_PRINT_GAL>( aOptions, std::move( printCtx ) );
cairo_surface_t * m_surface
Cairo surface.
CAIRO_GAL_BASE(GAL_DISPLAY_OPTIONS &aDisplayOptions)
cairo_t * m_context
Cairo image.
cairo_t * m_currentContext
Currently used Cairo context for drawing.
CAIRO_PRINT_CTX(wxImage *aImage, double aDPI)
cairo_surface_t * m_surface
void SetSheetSize(const VECTOR2D &aSize) override
bool isLandscape() const
< Returns true if page orientation is landscape
bool m_hasNativeLandscapeRotation
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
static std::unique_ptr< CAIRO_PRINT_GAL > Create(GAL_DISPLAY_OPTIONS &aOptions, wxImage *aImage, double aDPI)
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...
A color representation with 4 components: red, green, blue, alpha.
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.
void SetRotation(T aAngle)
Set the rotation components of the matrix.
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
MATRIX3x3< double > MATRIX3x3D
The Cairo implementation of the graphics abstraction layer.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D