Bug 1769161 Part 1 - Refactor scaling factor due to page-size for nsPageFrame to be in its own function. r=dholbert

This includes two versions of the function, one which takes an already-computed
page size to avoid computing this multiple times for a caller, and the other
which computes this value itself for convenience.

Differential Revision: https://phabricator.services.mozilla.com/D146601
This commit is contained in:
Emily McDonough 2022-06-10 19:37:22 +00:00
Родитель ca262b23ce
Коммит c381f19b0e
2 изменённых файлов: 42 добавлений и 22 удалений

Просмотреть файл

@ -499,32 +499,12 @@ static gfx::Matrix4x4 ComputePagesPerSheetAndPageSizeTransform(
// Variables that we use in our transform (initialized with reasonable
// defaults that work for the regular one-page-per-sheet scenario):
float scale = 1.0f;
const nsSize contentPageSize = pageFrame->ComputePageSize();
float scale = pageFrame->ComputePageSizeScale(contentPageSize);
nsPoint gridOrigin;
uint32_t rowIdx = 0;
uint32_t colIdx = 0;
// Compute scaling due to a possible mismatch in the paper size we are
// printing to (from the pres context) and the specified page size when the
// content uses "@page {size: ...}" to specify a page size for the content.
const nsSize actualPaperSize = pageFrame->PresContext()->GetPageSize();
const nsSize contentPageSize = pageFrame->ComputePageSize();
{
nscoord contentPageHeight = contentPageSize.height;
// Scale down if the target is too wide.
if (contentPageSize.width > actualPaperSize.width) {
scale *= float(actualPaperSize.width) / float(contentPageSize.width);
contentPageHeight = NSToCoordRound(contentPageHeight * scale);
}
// Scale down if the target is too tall.
if (contentPageHeight > actualPaperSize.height) {
scale *= float(actualPaperSize.height) / float(contentPageHeight);
}
}
MOZ_ASSERT(
scale <= 1.0f,
"Page-size mismatches should only have caused us to scale down, not up.");
if (nsSharedPageData* pd = pageFrame->GetSharedPageData()) {
const auto* ppsInfo = pd->PagesPerSheetInfo();
if (ppsInfo->mNumPages > 1) {
@ -599,6 +579,37 @@ nsSize nsPageFrame::ComputePageSize() const {
return size;
}
float nsPageFrame::ComputePageSizeScale(const nsSize aContentPageSize) const {
MOZ_ASSERT(aContentPageSize == ComputePageSize(),
"Incorrect content page size");
// Check for the simplest case first, an auto page-size which requires no
// scaling at all.
if (PageContentFrame()->StylePage()->mSize.IsAuto()) {
return 1.0f;
}
// Compute scaling due to a possible mismatch in the paper size we are
// printing to (from the pres context) and the specified page size when the
// content uses "@page {size: ...}" to specify a page size for the content.
float scale = 1.0f;
const nsSize actualPaperSize = PresContext()->GetPageSize();
nscoord contentPageHeight = aContentPageSize.height;
// Scale down if the target is too wide.
if (aContentPageSize.width > actualPaperSize.width) {
scale *= float(actualPaperSize.width) / float(aContentPageSize.width);
contentPageHeight = NSToCoordRound(contentPageHeight * scale);
}
// Scale down if the target is too tall.
if (contentPageHeight > actualPaperSize.height) {
scale *= float(actualPaperSize.height) / float(contentPageHeight);
}
MOZ_ASSERT(
scale <= 1.0f,
"Page-size mismatches should only have caused us to scale down, not up.");
return scale;
}
void nsPageFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists) {
nsDisplayList content(aBuilder);

Просмотреть файл

@ -72,6 +72,15 @@ class nsPageFrame final : public nsContainerFrame {
nsSize ComputePageSize() const;
// Computes the scaling factor caused by a CSS page-size that is to large to
// fit on the paper we are printing to.
// Callers that have already computed the page size with ComputePageSize
// should use the first version of the function, which avoids recomputing it.
float ComputePageSizeScale(const nsSize aContentPageSize) const;
inline float ComputePageSizeScale() const {
return ComputePageSizeScale(ComputePageSize());
}
protected:
explicit nsPageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext);
virtual ~nsPageFrame();