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 48 of file cairo_print.cpp.

49  : m_gcdc( nullptr ), m_ctx( nullptr ), m_surface( nullptr )
50 {
51  if( wxPrinterDC* printerDC = dynamic_cast<wxPrinterDC*>( aDC ) )
52  m_gcdc = new wxGCDC( *printerDC );
53  else if( wxMemoryDC* memoryDC = dynamic_cast<wxMemoryDC*>( aDC ) )
54  m_gcdc = new wxGCDC( *memoryDC );
55  else if( wxWindowDC* windowDC = dynamic_cast<wxWindowDC*>( aDC ) )
56  m_gcdc = new wxGCDC( *windowDC );
57 #ifdef __WXMSW__
58  else if( wxEnhMetaFileDC* enhMFDC = dynamic_cast<wxEnhMetaFileDC*>( aDC ) )
59  m_gcdc = new wxGCDC( *enhMFDC );
60 #endif /* __WXMSW__ */
61  else
62  throw std::runtime_error( "Unhandled wxDC type" );
63 
64  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
65 
66  if( !gctx )
67  throw std::runtime_error( "Could not get the Graphics Context" );
68 
69 #ifdef __WXGTK__
70  m_ctx = static_cast<cairo_t*>( gctx->GetNativeContext() );
71  m_surface = cairo_get_target( m_ctx );
72  // On linux, cairo printers have 72 DPI by default.
73  // This is an unusable resolution for us.
74  // A better resolution could be 4800 DPI (at 600 DPI, we still have minor
75  // but visible artifacts, for instance with arcs, but not at 4800 DPI)
76  // so modify the default:
77  #define DEFAULT_DPI 72.0
78  #define KICAD_PRINTER_DPI 4800.0
79  // our device scale is DEFAULT_DPI / KICAD_PRINTER_DPI
80  cairo_surface_set_device_scale( m_surface, DEFAULT_DPI/KICAD_PRINTER_DPI, DEFAULT_DPI/KICAD_PRINTER_DPI );
81  m_dpi = KICAD_PRINTER_DPI;
82 #endif /* __WXGTK__ */
83 
84 #ifdef __WXMSW__
85  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
86  m_hdc = g->GetHDC();
87  m_surface = cairo_win32_printing_surface_create( static_cast<HDC>( m_hdc ) );
88  m_ctx = cairo_create( m_surface );
89  m_dpi = GetDeviceCaps( static_cast<HDC>( m_hdc ), LOGPIXELSX );
90 #endif /* __WXMSW__ */
91 
92 #ifdef __WXMAC__
93  wxSize size = m_gcdc->GetSize();
94  CGContextRef cg = (CGContextRef) gctx->GetNativeContext();
95  m_surface = cairo_quartz_surface_create_for_cg_context( cg, size.x, size.y );
96  m_ctx = cairo_create( m_surface );
97  wxASSERT( aDC->GetPPI().x == aDC->GetPPI().y );
98  m_dpi = aDC->GetPPI().x;
99 #endif /* __WXMAC__ */
100 
101  if( !m_ctx || cairo_status( m_ctx ) != CAIRO_STATUS_SUCCESS )
102  throw std::runtime_error( "Could not create Cairo context" );
103 
104  if( !m_surface || cairo_surface_status( m_surface ) != CAIRO_STATUS_SUCCESS )
105  throw std::runtime_error( "Could not create Cairo surface" );
106 
107  cairo_reference( m_ctx );
108  cairo_surface_reference( m_surface );
109 }
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 112 of file cairo_print.cpp.

113 {
114 #ifdef __WXMSW__
115  cairo_surface_show_page( m_surface );
116  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
117  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
118  g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
119 #endif /* __WXMSW__ */
120 
121  cairo_surface_destroy( m_surface );
122  cairo_destroy( m_ctx );
123  delete m_gcdc;
124 }
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: