Bug 1156742 Part 7: Refactor nsDeviceContext.cpp to use printing surface for size and nsIDeviceContextSpec for DPI and scale. r=roc

These changes are to make using an off screen surface behind our DrawTarget in the child easier.
It still creates the real printing surface for some of the calculations,
removing this will be required for future tightening of the sandbox.
This commit is contained in:
Bob Owen 2016-01-05 10:08:57 +00:00
parent 72f0cb895d
commit 2a789531dc
7 changed files with 51 additions and 69 deletions

View File

@ -14,9 +14,11 @@ class nsIDOMWindow;
class PPrintProgressDialogParent;
class PPrintSettingsDialogParent;
typedef mozilla::layout::PRemotePrintJobParent PRemotePrintJobParent;
namespace mozilla {
namespace layout {
class PRemotePrintJobParent;
}
namespace embedding {
class PrintingParent final : public PPrintingParent

View File

@ -22,8 +22,7 @@
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::embedding;
typedef mozilla::layout::RemotePrintJobChild RemotePrintJobChild;
using namespace mozilla::layout;
static StaticRefPtr<nsPrintingProxy> sPrintingProxyInstance;

View File

@ -9,7 +9,11 @@
#include "nsIPrintingPromptService.h"
#include "mozilla/embedding/PPrintingChild.h"
typedef mozilla::layout::PRemotePrintJobChild PRemotePrintJobChild;
namespace mozilla {
namespace layout {
class PRemotePrintJobChild;
}
}
class nsPrintingProxy: public nsIPrintingPromptService,
public mozilla::embedding::PPrintingChild

View File

@ -314,30 +314,10 @@ nsDeviceContext::SetDPI()
{
float dpi = -1.0f;
// PostScript, PDF and Mac (when printing) all use 72 dpi
// Use a printing DC to determine the other dpi values
if (mPrintingSurface) {
switch (mPrintingSurface->GetType()) {
case gfxSurfaceType::PDF:
case gfxSurfaceType::PS:
case gfxSurfaceType::Quartz:
dpi = 72.0f;
break;
#ifdef XP_WIN
case gfxSurfaceType::Win32:
case gfxSurfaceType::Win32Printing: {
HDC dc = reinterpret_cast<gfxWindowsSurface*>(mPrintingSurface.get())->GetDC();
int32_t OSVal = GetDeviceCaps(dc, LOGPIXELSY);
dpi = 144.0f;
mPrintingScale = float(OSVal) / dpi;
break;
}
#endif
default:
NS_NOTREACHED("Unexpected printing surface type");
break;
}
// Use the printing DC to determine DPI values, if we have one.
if (mDeviceContextSpec) {
dpi = mDeviceContextSpec->GetDPI();
mPrintingScale = mDeviceContextSpec->GetPrintingScale();
mAppUnitsPerDevPixelAtUnitFullZoom =
NS_lround((AppUnitsPerCSSPixel() * 96) / dpi);
} else {
@ -698,34 +678,7 @@ nsDeviceContext::CalcPrintingSize()
gfxSize size(0, 0);
switch (mPrintingSurface->GetType()) {
case gfxSurfaceType::Image:
inPoints = false;
size = reinterpret_cast<gfxImageSurface*>(mPrintingSurface.get())->GetSize();
break;
#if defined(MOZ_PDF_PRINTING)
case gfxSurfaceType::PDF:
inPoints = true;
size = reinterpret_cast<gfxPDFSurface*>(mPrintingSurface.get())->GetSize();
break;
#endif
#ifdef MOZ_WIDGET_GTK
case gfxSurfaceType::PS:
inPoints = true;
size = reinterpret_cast<gfxPSSurface*>(mPrintingSurface.get())->GetSize();
break;
#endif
#ifdef XP_MACOSX
case gfxSurfaceType::Quartz:
inPoints = true; // this is really only true when we're printing
size = reinterpret_cast<gfxQuartzSurface*>(mPrintingSurface.get())->GetSize();
break;
#endif
#ifdef XP_WIN
case gfxSurfaceType::Win32:
case gfxSurfaceType::Win32Printing:
{
inPoints = false;
@ -740,10 +693,8 @@ nsDeviceContext::CalcPrintingSize()
break;
}
#endif
default:
gfxCriticalError() << "Printing to unknown surface type " << (int)mPrintingSurface->GetType();
NS_ERROR("trying to print to unknown surface type");
size = mPrintingSurface->GetSize();
}
if (inPoints) {

View File

@ -13,8 +13,8 @@ class nsIPrintSettings;
class gfxASurface;
#define NS_IDEVICE_CONTEXT_SPEC_IID \
{ 0xb5548fb1, 0xf43e, 0x4921, \
{ 0x82, 0x19, 0xc3, 0x82, 0x06, 0xee, 0x74, 0x5c } }
{ 0xf407cfba, 0xbe28, 0x46c9, \
{ 0x8a, 0xba, 0x04, 0x2d, 0xae, 0xbb, 0x4f, 0x23 } }
class nsIDeviceContextSpec : public nsISupports
{
@ -34,6 +34,20 @@ public:
NS_IMETHOD GetSurfaceForPrinter(gfxASurface **nativeSurface) = 0;
/**
* Override to return something other than the default.
*
* @return DPI for printing.
*/
virtual float GetDPI() { return 72.0f; }
/**
* Override to return something other than the default.
*
* @return the printing scale to be applied to the context for printing.
*/
virtual float GetPrintingScale() { return 1.0f; }
NS_IMETHOD BeginDocument(const nsAString& aTitle,
char16_t* aPrintToFileName,
int32_t aStartPage,

View File

@ -270,6 +270,7 @@ NS_IMETHODIMP nsDeviceContextSpecWin::GetSurfaceForPrinter(gfxASurface **surface
{
NS_ASSERTION(mDevMode, "DevMode can't be NULL here");
*surface = nullptr;
RefPtr<gfxASurface> newSurface;
int16_t outputFormat = 0;
@ -315,19 +316,24 @@ NS_IMETHODIMP nsDeviceContextSpecWin::GetSurfaceForPrinter(gfxASurface **surface
newSurface = new gfxWindowsSurface(dc, gfxWindowsSurface::FLAG_TAKE_DC | gfxWindowsSurface::FLAG_FOR_PRINTING);
if (newSurface->GetType() == (gfxSurfaceType)-1) {
gfxCriticalError() << "Invalid windows surface from " << gfx::hexa(dc);
newSurface = nullptr;
return NS_ERROR_FAILURE;
}
}
}
if (newSurface) {
*surface = newSurface;
NS_ADDREF(*surface);
return NS_OK;
}
mPrintingSurface = newSurface;
newSurface.forget(surface);
return NS_OK;
}
*surface = nullptr;
return NS_ERROR_FAILURE;
float
nsDeviceContextSpecWin::GetPrintingScale()
{
MOZ_ASSERT(mPrintingSurface);
HDC dc = reinterpret_cast<gfxWindowsSurface*>(mPrintingSurface.get())->GetDC();
int32_t OSVal = GetDeviceCaps(dc, LOGPIXELSY);
return float(OSVal) / GetDPI();
}
//----------------------------------------------------------------------------------

View File

@ -13,6 +13,7 @@
#include "nsISupportsPrimitives.h"
#include <windows.h>
#include "mozilla/Attributes.h"
#include "mozilla/RefPtr.h"
class nsIWidget;
@ -34,6 +35,10 @@ public:
NS_IMETHOD Init(nsIWidget* aWidget, nsIPrintSettings* aPS, bool aIsPrintPreview) override;
float GetDPI() final { return 144.0f; }
float GetPrintingScale() final;
void GetDriverName(wchar_t *&aDriverName) const { aDriverName = mDriverName; }
void GetDeviceName(wchar_t *&aDeviceName) const { aDeviceName = mDeviceName; }
@ -64,6 +69,7 @@ protected:
LPDEVMODEW mDevMode;
nsCOMPtr<nsIPrintSettings> mPrintSettings;
RefPtr<gfxASurface> mPrintingSurface;
};