Bug 1631465 part 3: Move calculation of unwritable margins to WinUtils. r=jwatt

At some point it might be good to move all of the interaction with the native
Windows printer API into some sort of wrapper class, but this keeps it simple
for now.

Differential Revision: https://phabricator.services.mozilla.com/D84008
This commit is contained in:
Bob Owen
2020-07-22 15:08:13 +00:00
parent b0a7d797f0
commit 43fa369e5c
3 changed files with 48 additions and 29 deletions

View File

@@ -673,6 +673,40 @@ int WinUtils::GetSystemMetricsForDpi(int nIndex, UINT dpi) {
}
}
/* static */
gfx::MarginDouble WinUtils::GetUnwriteableMarginsForDeviceInInches(HDC aHdc) {
if (!aHdc) {
return gfx::MarginDouble();
}
int pixelsPerInchY = ::GetDeviceCaps(aHdc, LOGPIXELSY);
int marginTop = ::GetDeviceCaps(aHdc, PHYSICALOFFSETY);
int printableAreaHeight = ::GetDeviceCaps(aHdc, VERTRES);
int physicalHeight = ::GetDeviceCaps(aHdc, PHYSICALHEIGHT);
double marginTopInch = double(marginTop) / pixelsPerInchY;
double printableAreaHeightInch = double(printableAreaHeight) / pixelsPerInchY;
double physicalHeightInch = double(physicalHeight) / pixelsPerInchY;
double marginBottomInch =
physicalHeightInch - printableAreaHeightInch - marginTopInch;
int pixelsPerInchX = ::GetDeviceCaps(aHdc, LOGPIXELSX);
int marginLeft = ::GetDeviceCaps(aHdc, PHYSICALOFFSETX);
int printableAreaWidth = ::GetDeviceCaps(aHdc, HORZRES);
int physicalWidth = ::GetDeviceCaps(aHdc, PHYSICALWIDTH);
double marginLeftInch = double(marginLeft) / pixelsPerInchX;
double printableAreaWidthInch = double(printableAreaWidth) / pixelsPerInchX;
double physicalWidthInch = double(physicalWidth) / pixelsPerInchX;
double marginRightInch =
physicalWidthInch - printableAreaWidthInch - marginLeftInch;
return gfx::MarginDouble(marginTopInch, marginRightInch, marginBottomInch,
marginLeftInch);
}
#ifdef ACCESSIBILITY
/* static */
a11y::Accessible* WinUtils::GetRootAccessibleForHWND(HWND aHwnd) {

View File

@@ -234,6 +234,13 @@ class WinUtils {
static bool HasSystemMetricsForDpi();
static int GetSystemMetricsForDpi(int nIndex, UINT dpi);
/**
* @param aHdc HDC for printer
* @return unwritable margins for currently set page on aHdc or empty margins
* if aHdc is null
*/
static gfx::MarginDouble GetUnwriteableMarginsForDeviceInInches(HDC aHdc);
/**
* Logging helpers that dump output to prlog module 'Widget', console, and
* OutputDebugString. Note these output in both debug and release builds.

View File

@@ -6,6 +6,7 @@
#include "mozilla/ArrayUtils.h"
#include "nsCRT.h"
#include "WinUtils.h"
// Using paper sizes from wingdi.h and the units given there, plus a little
// extra research for the ones it doesn't give. Looks like the list hasn't
@@ -264,36 +265,13 @@ nsPrintSettingsWin::GetEffectivePageSize(double* aWidth, double* aHeight) {
}
void nsPrintSettingsWin::InitUnwriteableMargin(HDC aHdc) {
int32_t pixelsPerInchY = GetDeviceCaps(aHdc, LOGPIXELSY);
int32_t pixelsPerInchX = GetDeviceCaps(aHdc, LOGPIXELSX);
mozilla::gfx::MarginDouble margin =
mozilla::widget::WinUtils::GetUnwriteableMarginsForDeviceInInches(aHdc);
int32_t marginLeft = GetDeviceCaps(aHdc, PHYSICALOFFSETX);
int32_t marginTop = GetDeviceCaps(aHdc, PHYSICALOFFSETY);
double marginLeftInch = double(marginLeft) / pixelsPerInchX;
double marginTopInch = double(marginTop) / pixelsPerInchY;
int32_t printableAreaWidth = GetDeviceCaps(aHdc, HORZRES);
int32_t printableAreaHeight = GetDeviceCaps(aHdc, VERTRES);
double printableAreaWidthInch = double(printableAreaWidth) / pixelsPerInchX;
double printableAreaHeightInch = double(printableAreaHeight) / pixelsPerInchY;
int32_t physicalWidth = GetDeviceCaps(aHdc, PHYSICALWIDTH);
int32_t physicalHeight = GetDeviceCaps(aHdc, PHYSICALHEIGHT);
double physicalWidthInch = double(physicalWidth) / pixelsPerInchX;
double physicalHeightInch = double(physicalHeight) / pixelsPerInchY;
double marginBottomInch =
physicalHeightInch - printableAreaHeightInch - marginTopInch;
double marginRightInch =
physicalWidthInch - printableAreaWidthInch - marginLeftInch;
mUnwriteableMargin.SizeTo(NS_INCHES_TO_INT_TWIPS(marginTopInch),
NS_INCHES_TO_INT_TWIPS(marginRightInch),
NS_INCHES_TO_INT_TWIPS(marginBottomInch),
NS_INCHES_TO_INT_TWIPS(marginLeftInch));
mUnwriteableMargin.SizeTo(NS_INCHES_TO_INT_TWIPS(margin.top),
NS_INCHES_TO_INT_TWIPS(margin.right),
NS_INCHES_TO_INT_TWIPS(margin.bottom),
NS_INCHES_TO_INT_TWIPS(margin.left));
}
void nsPrintSettingsWin::CopyFromNative(HDC aHdc, DEVMODEW* aDevMode) {