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::CAIRO_PRINT_CTX ( wxDC *  aDC)

Definition at line 42 of file cairo_print.cpp.

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  // On linux, cairo printers are 72 DPI by default. This is an ususable resolution for us.
67  // A reasonable resolution is 600 DPI
68  // so modify the default:
69  #define DEFAULT_DPI 72.0
70  #define KICAD_PRINTER_DPI 600.0
71  // our device scale is DEFAULT_DPI / KICAD_PRINTER_DPI
72  cairo_surface_set_device_scale( m_surface, DEFAULT_DPI/KICAD_PRINTER_DPI, DEFAULT_DPI/KICAD_PRINTER_DPI );
73  m_dpi = KICAD_PRINTER_DPI;
74 #endif /* __WXGTK__ */
75 
76 #ifdef __WXMSW__
77  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
78  m_hdc = g->GetHDC();
79  m_surface = cairo_win32_printing_surface_create( static_cast<HDC>( m_hdc ) );
80  m_ctx = cairo_create( m_surface );
81  m_dpi = GetDeviceCaps( static_cast<HDC>( m_hdc ), LOGPIXELSX );
82 #endif /* __WXMSW__ */
83 
84 #ifdef __WXMAC__
85  wxSize size = m_gcdc->GetSize();
86  CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
87  m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
88  m_ctx = cairo_create( m_surface );
89  wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
90  m_dpi = aDC->GetPPI().x;
91 #endif /* __WXMAC__ */
92 
93  if( !m_ctx || cairo_status( m_ctx ) != CAIRO_STATUS_SUCCESS )
94  throw std::runtime_error( "Could not create Cairo context" );
95 
96  if( !m_surface || cairo_surface_status( m_surface ) != CAIRO_STATUS_SUCCESS )
97  throw std::runtime_error( "Could not create Cairo surface" );
98 
99  cairo_reference( m_ctx );
100  cairo_surface_reference( m_surface );
101 }
cairo_surface_t * m_surface
Definition: cairo_print.h:68

References m_ctx, m_dpi, m_gcdc, and m_surface.

◆ ~CAIRO_PRINT_CTX()

CAIRO_PRINT_CTX::~CAIRO_PRINT_CTX ( )

Definition at line 104 of file cairo_print.cpp.

105 {
106 #ifdef __WXMSW__
107  cairo_surface_show_page( m_surface );
108  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
109  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
110  g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
111 #endif /* __WXMSW__ */
112 
113  cairo_surface_destroy( m_surface );
114  cairo_destroy( m_ctx );
115  delete m_gcdc;
116 }
cairo_surface_t * m_surface
Definition: cairo_print.h:68

References m_ctx, m_gcdc, and m_surface.

Member Function Documentation

◆ GetContext()

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

Definition at line 41 of file cairo_print.h.

42  {
43  return m_ctx;
44  }

References m_ctx.

◆ GetNativeDPI()

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

Implements KIGFX::PRINT_CONTEXT.

Definition at line 51 of file cairo_print.h.

52  {
53  return m_dpi;
54  }

References m_dpi.

◆ GetSurface()

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

Definition at line 46 of file cairo_print.h.

47  {
48  return m_surface;
49  }
cairo_surface_t * m_surface
Definition: cairo_print.h:68

References m_surface.

◆ HasNativeLandscapeRotation()

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

◆ m_ctx

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().

◆ m_dpi

double KIGFX::CAIRO_PRINT_CTX::m_dpi
private

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