Decouple GAL printing interface and its Cairo-based implementation

This commit is contained in:
Maciej Suminski 2018-09-19 11:24:48 +02:00
parent e36f6b476d
commit 6c689305a6
3 changed files with 103 additions and 12 deletions

View File

@ -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<CAIRO_PRINT_CTX> 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> GAL_PRINT::Create( GAL_DISPLAY_OPTIONS& aOptions, wxDC* aDC )
{
auto printCtx = std::make_unique<CAIRO_PRINT_CTX>( aDC );
return std::make_unique<CAIRO_PRINT_GAL>( aOptions, std::move( printCtx ) );
}

View File

@ -21,6 +21,7 @@
#define _CAIRO_PRINT_H_
#include <gal/cairo/cairo_gal.h>
#include <gal/gal_print.h>
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<CAIRO_PRINT_CTX> 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<CAIRO_PRINT_CTX> m_printCtx;
};
} // namespace KIGFX

70
include/gal/gal_print.h Normal file
View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2018 CERN
* Author: Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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<GAL_PRINT> 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 */