KiCad PCB EDA Suite
KIGFX::CAIRO_PRINT_CTX Class Reference

Provide a Cairo context created from wxPrintDC. More...

#include <cairo_print.h>

Inheritance diagram for KIGFX::CAIRO_PRINT_CTX:
KIGFX::PRINT_CONTEXT

Public Member Functions

 CAIRO_PRINT_CTX (wxDC *aDC)
 
 ~CAIRO_PRINT_CTX ()
 
cairo_t * GetContext () const
 
cairo_surface_t * GetSurface () const
 
double GetNativeDPI () const override
 
bool HasNativeLandscapeRotation () const override
 

Private Attributes

wxGCDC * m_gcdc
 
cairo_t * m_ctx
 
cairo_surface_t * m_surface
 
double m_dpi
 

Detailed Description

Provide a Cairo context created from wxPrintDC.

It allows one to prepare printouts using the Cairo library and let wxWidgets handle the rest.

Definition at line 38 of file cairo_print.h.

Constructor & Destructor Documentation

◆ CAIRO_PRINT_CTX()

CAIRO_PRINT_CTX::CAIRO_PRINT_CTX ( wxDC *  aDC)

Definition at line 52 of file cairo_print.cpp.

52 :
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}
#define DEFAULT_DPI
cairo_surface_t * m_surface
Definition: cairo_print.h:71

References DEFAULT_DPI, m_ctx, m_dpi, m_gcdc, and m_surface.

◆ ~CAIRO_PRINT_CTX()

CAIRO_PRINT_CTX::~CAIRO_PRINT_CTX ( )

Definition at line 121 of file cairo_print.cpp.

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}

References m_ctx, m_gcdc, and m_surface.

Member Function Documentation

◆ GetContext()

cairo_t * KIGFX::CAIRO_PRINT_CTX::GetContext ( ) const
inline

Definition at line 44 of file cairo_print.h.

45 {
46 return m_ctx;
47 }

References m_ctx.

◆ GetNativeDPI()

double KIGFX::CAIRO_PRINT_CTX::GetNativeDPI ( ) const
inlineoverridevirtual

Implements KIGFX::PRINT_CONTEXT.

Definition at line 54 of file cairo_print.h.

55 {
56 return m_dpi;
57 }

References m_dpi.

◆ GetSurface()

cairo_surface_t * KIGFX::CAIRO_PRINT_CTX::GetSurface ( ) const
inline

Definition at line 49 of file cairo_print.h.

50 {
51 return m_surface;
52 }

References m_surface.

◆ HasNativeLandscapeRotation()

bool KIGFX::CAIRO_PRINT_CTX::HasNativeLandscapeRotation ( ) const
inlineoverridevirtual

Implements KIGFX::PRINT_CONTEXT.

Definition at line 59 of file cairo_print.h.

60 {
61#if defined(__WXGTK__) && !defined(__WXGTK3__)
62 return false;
63#else
64 return true;
65#endif
66 }

Member Data Documentation

◆ m_ctx

cairo_t* KIGFX::CAIRO_PRINT_CTX::m_ctx
private

Definition at line 70 of file cairo_print.h.

Referenced by CAIRO_PRINT_CTX(), GetContext(), and ~CAIRO_PRINT_CTX().

◆ m_dpi

double KIGFX::CAIRO_PRINT_CTX::m_dpi
private

Definition at line 78 of file cairo_print.h.

Referenced by CAIRO_PRINT_CTX(), and GetNativeDPI().

◆ m_gcdc

wxGCDC* KIGFX::CAIRO_PRINT_CTX::m_gcdc
private

Definition at line 69 of file cairo_print.h.

Referenced by CAIRO_PRINT_CTX(), and ~CAIRO_PRINT_CTX().

◆ m_surface

cairo_surface_t* KIGFX::CAIRO_PRINT_CTX::m_surface
private

Definition at line 71 of file cairo_print.h.

Referenced by CAIRO_PRINT_CTX(), GetSurface(), and ~CAIRO_PRINT_CTX().


The documentation for this class was generated from the following files: