From 6c689305a69da4eb20663645b1666591dd371812 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 19 Sep 2018 11:24:48 +0200 Subject: [PATCH] Decouple GAL printing interface and its Cairo-based implementation --- common/gal/cairo/cairo_print.cpp | 19 ++++++--- include/gal/cairo/cairo_print.h | 26 ++++++++---- include/gal/gal_print.h | 70 ++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 include/gal/gal_print.h diff --git a/common/gal/cairo/cairo_print.cpp b/common/gal/cairo/cairo_print.cpp index 4ef673fdb5..44f487db72 100644 --- a/common/gal/cairo/cairo_print.cpp +++ b/common/gal/cairo/cairo_print.cpp @@ -111,15 +111,17 @@ CAIRO_PRINT_CTX::~CAIRO_PRINT_CTX() CAIRO_PRINT_GAL::CAIRO_PRINT_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, - cairo_t* aContext, cairo_surface_t* aSurface ) + std::unique_ptr aContext ) : CAIRO_GAL_BASE( aDisplayOptions ) { - cairo_reference( aContext ); - cairo_surface_reference( aSurface ); - context = currentContext = aContext; - surface = aSurface; + m_printCtx = std::move( aContext ); + context = currentContext = m_printCtx->GetContext(); + surface = m_printCtx->GetSurface(); + cairo_reference( context ); + cairo_surface_reference( surface ); m_clearColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 ); resetContext(); + SetScreenDPI( m_printCtx->GetNativeDPI() ); } @@ -179,3 +181,10 @@ void CAIRO_PRINT_GAL::SetSheetSize( const VECTOR2D& aSize ) SetScreenSize( VECTOR2I( std::ceil( aSize.x * screenDPI ) * 2, std::ceil( aSize.y * screenDPI ) * 2 ) ); } + + +std::unique_ptr GAL_PRINT::Create( GAL_DISPLAY_OPTIONS& aOptions, wxDC* aDC ) +{ + auto printCtx = std::make_unique( aDC ); + return std::make_unique( aOptions, std::move( printCtx ) ); +} diff --git a/include/gal/cairo/cairo_print.h b/include/gal/cairo/cairo_print.h index 90d2b665da..ddd61105c6 100644 --- a/include/gal/cairo/cairo_print.h +++ b/include/gal/cairo/cairo_print.h @@ -21,6 +21,7 @@ #define _CAIRO_PRINT_H_ #include +#include class wxDC; class wxGCDC; @@ -31,7 +32,7 @@ namespace KIGFX * 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. */ -class CAIRO_PRINT_CTX +class CAIRO_PRINT_CTX : public PRINT_CONTEXT { public: CAIRO_PRINT_CTX( wxDC* aDC ); @@ -47,12 +48,12 @@ public: return m_surface; } - double GetNativeDPI() const + double GetNativeDPI() const override { return m_dpi; } - bool HasNativeLandscapeRotation() const + bool HasNativeLandscapeRotation() const override { #ifdef __WXGTK__ return false; @@ -75,25 +76,35 @@ private: }; -class CAIRO_PRINT_GAL : public CAIRO_GAL_BASE +class CAIRO_PRINT_GAL : public CAIRO_GAL_BASE, public GAL_PRINT { public: CAIRO_PRINT_GAL( GAL_DISPLAY_OPTIONS& aDisplayOptions, - cairo_t* aContext, cairo_surface_t* aSurface ); + std::unique_ptr aContext ); void ComputeWorldScreenMatrix() override; + GAL* GetGAL() override + { + return this; + } + + PRINT_CONTEXT* GetPrintCtx() const override + { + return m_printCtx.get(); + } + /** * @param aSize is the printing sheet size expressed in inches. * @param aRotateIfLandscape true if the platform requires 90 degrees * rotation in order to print in landscape format. */ - void SetNativePaperSize( const VECTOR2D& aSize, bool aRotateIfLandscape ); + void SetNativePaperSize( const VECTOR2D& aSize, bool aRotateIfLandscape ) override; /** * @param aSize is the schematics sheet size expressed in inches. */ - void SetSheetSize( const VECTOR2D& aSize ); + void SetSheetSize( const VECTOR2D& aSize ) override; private: ///> Returns true if page orientation is landscape @@ -109,6 +120,7 @@ private: ///> GAL needs to handle it in the transformation matrix bool m_hasNativeLandscapeRotation; + std::unique_ptr m_printCtx; }; } // namespace KIGFX diff --git a/include/gal/gal_print.h b/include/gal/gal_print.h new file mode 100644 index 0000000000..e0dc01122e --- /dev/null +++ b/include/gal/gal_print.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2018 CERN + * Author: Maciej Suminski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef GAL_PRINT_H +#define GAL_PRINT_H + +#ifdef WX_COMPATIBILITY +class wxDC; +#endif /* WX_COMPATIBILITY */ + +namespace KIGFX { +class GAL; +class GAL_DISPLAY_OPTIONS; + + +class PRINT_CONTEXT +{ +public: + virtual ~PRINT_CONTEXT() {} + virtual double GetNativeDPI() const = 0; + virtual bool HasNativeLandscapeRotation() const = 0; +}; + + +/** + * @brief Wrapper around GAL to provide information needed for printing. + */ +class GAL_PRINT +{ +public: +#ifdef WX_COMPATIBILITY + static std::unique_ptr Create( GAL_DISPLAY_OPTIONS& aOptions, wxDC* aDC ); +#endif /* WX_COMPATIBILITY */ + virtual ~GAL_PRINT() {} + + virtual GAL* GetGAL() = 0; + + virtual PRINT_CONTEXT* GetPrintCtx() const = 0; + + /** + * @param aSize is the printing sheet size expressed in inches. + * @param aRotateIfLandscape true if the platform requires 90 degrees + * rotation in order to print in landscape format. + */ + virtual void SetNativePaperSize( const VECTOR2D& aSize, bool aRotateIfLandscape ) = 0; + + /** + * @param aSize is the schematics sheet size expressed in inches. + */ + virtual void SetSheetSize( const VECTOR2D& aSize ) = 0; +}; + +}; // end namespace KIGFX + +#endif /* GAL_PRINT_H */