KiCad PCB EDA Suite
KIGFX::CAIRO_PRINT_CTX Class Reference

CAIRO_PRINT_CTX provides 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

CAIRO_PRINT_CTX provides 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 35 of file cairo_print.h.

Constructor & Destructor Documentation

CAIRO_PRINT_CTX::CAIRO_PRINT_CTX ( wxDC *  aDC)

Definition at line 42 of file cairo_print.cpp.

References m_ctx, m_dpi, m_gcdc, and m_surface.

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 }
cairo_surface_t * m_surface
Definition: cairo_print.h:68
CAIRO_PRINT_CTX::~CAIRO_PRINT_CTX ( )

Definition at line 98 of file cairo_print.cpp.

References m_ctx, m_gcdc, and m_surface.

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 }
cairo_surface_t * m_surface
Definition: cairo_print.h:68

Member Function Documentation

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

Definition at line 41 of file cairo_print.h.

References m_ctx.

42  {
43  return m_ctx;
44  }
double KIGFX::CAIRO_PRINT_CTX::GetNativeDPI ( ) const
inlineoverridevirtual

Implements KIGFX::PRINT_CONTEXT.

Definition at line 51 of file cairo_print.h.

References m_dpi.

52  {
53  return m_dpi;
54  }
cairo_surface_t* KIGFX::CAIRO_PRINT_CTX::GetSurface ( ) const
inline

Definition at line 46 of file cairo_print.h.

References m_surface.

47  {
48  return m_surface;
49  }
cairo_surface_t * m_surface
Definition: cairo_print.h:68
bool KIGFX::CAIRO_PRINT_CTX::HasNativeLandscapeRotation ( ) const
inlineoverridevirtual

Implements KIGFX::PRINT_CONTEXT.

Definition at line 56 of file cairo_print.h.

57  {
58 #if defined(__WXGTK__) && !defined(__WXGTK3__)
59  return false;
60 #else
61  return true;
62 #endif
63  }

Member Data Documentation

cairo_t* KIGFX::CAIRO_PRINT_CTX::m_ctx
private

Definition at line 67 of file cairo_print.h.

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

double KIGFX::CAIRO_PRINT_CTX::m_dpi
private

Definition at line 75 of file cairo_print.h.

Referenced by CAIRO_PRINT_CTX(), and GetNativeDPI().

wxGCDC* KIGFX::CAIRO_PRINT_CTX::m_gcdc
private

Definition at line 66 of file cairo_print.h.

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

cairo_surface_t* KIGFX::CAIRO_PRINT_CTX::m_surface
private

Definition at line 68 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: