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

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

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

◆ ~CAIRO_PRINT_CTX()

CAIRO_PRINT_CTX::~CAIRO_PRINT_CTX ( )

Definition at line 119 of file cairo_print.cpp.

120 {
121 #ifdef __WXMSW__
122  cairo_surface_show_page( m_surface );
123  wxGraphicsContext* gctx = m_gcdc->GetGraphicsContext();
124  Gdiplus::Graphics* g = static_cast<Gdiplus::Graphics*>( gctx->GetNativeContext() );
125  g->ReleaseHDC( static_cast<HDC>( m_hdc ) );
126 #endif /* __WXMSW__ */
127 
128  cairo_surface_destroy( m_surface );
129  cairo_destroy( m_ctx );
130  delete m_gcdc;
131 }
cairo_surface_t * m_surface
Definition: cairo_print.h:71

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

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: