Improve print preview and printing with high DPI

All print related scaling is based on 96DPI, so always let the printing contexts return this,
and also adjust the font to this DPI.
This commit is contained in:
Maarten Bent
2022-04-20 20:24:31 +02:00
parent 3da3103eb5
commit 1a1d19e93b
7 changed files with 38 additions and 11 deletions

View File

@@ -36,6 +36,13 @@ public:
virtual wxRect GetPaperRect() const wxOVERRIDE; virtual wxRect GetPaperRect() const wxOVERRIDE;
virtual wxSize FromDIP(const wxSize& sz) const wxOVERRIDE
{
return sz;
}
void SetFont(const wxFont& font) wxOVERRIDE;
protected: protected:
virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y, virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
bool useMask = false) wxOVERRIDE; bool useMask = false) wxOVERRIDE;

View File

@@ -45,6 +45,7 @@
#include "wx/print.h" #include "wx/print.h"
#include "wx/dcprint.h" #include "wx/dcprint.h"
#include "wx/artprov.h" #include "wx/artprov.h"
#include "wx/display.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -638,7 +639,7 @@ bool wxPrintout::SetUp(wxDC& dc)
{ {
wxCHECK_MSG( dc.IsOk(), false, "should have a valid DC to set up" ); wxCHECK_MSG( dc.IsOk(), false, "should have a valid DC to set up" );
SetPPIScreen(wxGetDisplayPPI()); SetPPIScreen(wxDisplay::GetStdPPI());
// We need to know printer PPI. In most ports, this can be retrieved from // We need to know printer PPI. In most ports, this can be retrieved from
// the printer DC, but in others it is computed (probably for legacy // the printer DC, but in others it is computed (probably for legacy
@@ -1613,7 +1614,7 @@ void wxPreviewControlBar::CreateButtons()
}; };
int n = WXSIZEOF(choices); int n = WXSIZEOF(choices);
m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(70,wxDefaultCoord), n, choices, 0 ); m_zoomControl = new wxChoice( this, wxID_PREVIEW_ZOOM, wxDefaultPosition, wxSize(FromDIP(70),wxDefaultCoord), n, choices, 0 );
sizer.Add(m_zoomControl); sizer.Add(m_zoomControl);
SetZoomControl(m_printPreview->GetZoom()); SetZoomControl(m_printPreview->GetZoom());

View File

@@ -41,6 +41,7 @@
#endif #endif
#include "wx/printdlg.h" #include "wx/printdlg.h"
#include "wx/display.h"
#include "wx/msw/printdlg.h" #include "wx/msw/printdlg.h"
// mingw32 defines GDI_ERROR incorrectly // mingw32 defines GDI_ERROR incorrectly
@@ -230,6 +231,13 @@ wxRect wxPrinterDCImpl::GetPaperRect() const
return wxRect(x, y, w, h); return wxRect(x, y, w, h);
} }
void wxPrinterDCImpl::SetFont(const wxFont& font)
{
wxFont scaledFont = font;
if ( scaledFont.IsOk() )
scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI());
wxMSWDCImpl::SetFont(scaledFont);
}
#if !wxUSE_PS_PRINTING #if !wxUSE_PS_PRINTING

View File

@@ -33,6 +33,7 @@
#include "wx/metafile.h" #include "wx/metafile.h"
#include "wx/clipbrd.h" #include "wx/clipbrd.h"
#include "wx/display.h"
#include "wx/msw/private.h" #include "wx/msw/private.h"
@@ -223,6 +224,19 @@ public:
const wxString& description ); const wxString& description );
virtual ~wxEnhMetaFileDCImpl(); virtual ~wxEnhMetaFileDCImpl();
wxSize FromDIP(const wxSize& sz) const wxOVERRIDE
{
return sz;
}
void SetFont(const wxFont& font) wxOVERRIDE
{
wxFont scaledFont = font;
if (scaledFont.IsOk())
scaledFont.WXAdjustToPPI(wxDisplay::GetStdPPI());
wxMSWDCImpl::SetFont(scaledFont);
}
// obtain a pointer to the new metafile (caller should delete it) // obtain a pointer to the new metafile (caller should delete it)
wxEnhMetaFile *Close(); wxEnhMetaFile *Close();

View File

@@ -2544,12 +2544,10 @@ wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer
void wxGDIPlusPrintingContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const void wxGDIPlusPrintingContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const
{ {
// override to use same scaling as wxWindowsPrintPreview::DetermineScaling
const wxSize dpi = wxGetDPIofHDC(ScreenHDC());
if ( dpiX ) if ( dpiX )
*dpiX = dpi.x; *dpiX = 96.0;
if ( dpiY ) if ( dpiY )
*dpiY = dpi.y; *dpiY = 96.0;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -46,6 +46,7 @@
#include "wx/msw/private.h" #include "wx/msw/private.h"
#include "wx/msw/dcprint.h" #include "wx/msw/dcprint.h"
#include "wx/msw/enhmeta.h" #include "wx/msw/enhmeta.h"
#include "wx/display.h"
#include <stdlib.h> #include <stdlib.h>
@@ -310,7 +311,7 @@ bool wxWindowsPrintPreview::Print(bool interactive)
void wxWindowsPrintPreview::DetermineScaling() void wxWindowsPrintPreview::DetermineScaling()
{ {
const wxSize logPPIScreen = wxGetDPIofHDC(ScreenHDC()); const wxSize logPPIScreen = wxDisplay::GetStdPPI();
m_previewPrintout->SetPPIScreen(logPPIScreen); m_previewPrintout->SetPPIScreen(logPPIScreen);
// Get a device context for the currently selected printer // Get a device context for the currently selected printer

View File

@@ -30,6 +30,7 @@
#include "wx/printdlg.h" #include "wx/printdlg.h"
#include "wx/paper.h" #include "wx/paper.h"
#include "wx/display.h"
#include "wx/osx/printdlg.h" #include "wx/osx/printdlg.h"
#include <stdlib.h> #include <stdlib.h>
@@ -746,10 +747,7 @@ bool wxMacPrintPreview::Print(bool interactive)
void wxMacPrintPreview::DetermineScaling() void wxMacPrintPreview::DetermineScaling()
{ {
int screenWidth , screenHeight ; wxSize ppiScreen = wxDisplay::GetStdPPI();
wxDisplaySize( &screenWidth , &screenHeight ) ;
wxSize ppiScreen = wxGetDisplayPPI();
wxSize ppiPrinter( 72 , 72 ) ; wxSize ppiPrinter( 72 , 72 ) ;
m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ; m_previewPrintout->SetPPIScreen( ppiScreen.x , ppiScreen.y ) ;