Floatify printing code and APIs.
We were passing offsets and sizes as integers. Use floats instead. In some parts of the code, sizes and offsets are in CSS pixels, in other parts they are in device pixels, and in some other parts they are in points. There are reasons for this, although it's currently a bit more convoluted than it has to be. Converting between them was done carefully with integer arithmetic and some special rounding code. This has worked mostly fine, but is fragile. I'm working on a CL that straightens out the conversions, to use CSS pixels instead of points in the Blink APIs (since that's what Blink uses internally). This would however mean that, if we were to keep on using integers, rounding errors that used to occur when printing HTML with Blink would be fixed, but, at the same time, we'd introduce new rounding errors when printing with a plug-in (when opening a PDF and printing it), since that part of the code wants things in points. So use floats to avoid this. This also allows for removal of PrintParamsWithFloatingSize. Although floats have precision issues for large integer values, this shouldn't be a problem here, since all the values changed are about page sizes, or offsets into a page (margins, unprintable area, etc.). Floats have 23 bits for the integer part, so as long as we stay (way) below a million pixels / points / whatever, we're good. It would easily become a problem if we start using floats for offsets into documents, though, as documents can become very tall. This CL isn't expected to make much of a behavior difference on its own. We'll still round down sizes to the nearest integer when entering Blink HTML layout, since we cannot reliably print fractional page sizes anyway. Furthermore, the way LocalFrame::ResizePageRectsKeepingRatio() is used to magically convert from points to pixels is inaccurate, and still causes the symptoms described in crbug.com/1444579 But it should now be more straight-forward to fix such issues without introducing new ones. Change-Id: I5fc5afeb14e5470faf970c9f7c94d0fad243ce3d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4604506 Reviewed-by: danakj <danakj@chromium.org> Commit-Queue: Morten Stenshorne <mstensho@chromium.org> Reviewed-by: Arthur Sonzogni <arthursonzogni@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/main@{#1160870}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
9c087d9626
commit
c61b68aecf
chrome/browser
printing
ui
webui
print_preview
components/printing
browser
common
renderer
test
content
renderer
web_test
pdf/pdfium
third_party/blink
public
renderer
core
exported
frame
scheduler_integration_tests
@ -88,7 +88,9 @@
|
||||
#include "third_party/blink/public/common/features.h"
|
||||
#include "third_party/blink/public/common/scheduler/web_scheduler_tracked_feature.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_OOP_PRINTING)
|
||||
#include "chrome/browser/printing/print_backend_service_manager.h"
|
||||
@ -1453,13 +1455,13 @@ IN_PROC_BROWSER_TEST_F(PrintExtensionBrowserTest,
|
||||
// Size and printable area are in device units, which is different for macOS.
|
||||
// See PrintSettings::device_units_per_inch().
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
static constexpr gfx::Size kLetterPdfPhysicalSize{612, 792};
|
||||
static constexpr gfx::Rect kExpectedPrintableArea{72, 72, 432, 684};
|
||||
static constexpr gfx::Size kExpectedContentSize{432, 656};
|
||||
static constexpr gfx::SizeF kLetterPdfPhysicalSize{612, 792};
|
||||
static constexpr gfx::RectF kExpectedPrintableArea{72, 72, 432, 684};
|
||||
static constexpr gfx::SizeF kExpectedContentSize{432, 656};
|
||||
#else
|
||||
static constexpr gfx::Size kLetterPdfPhysicalSize{2550, 3300};
|
||||
static constexpr gfx::Rect kExpectedPrintableArea{300, 300, 1800, 2850};
|
||||
static constexpr gfx::Size kExpectedContentSize{1800, 2732};
|
||||
static constexpr gfx::SizeF kLetterPdfPhysicalSize{2550, 3300};
|
||||
static constexpr gfx::RectF kExpectedPrintableArea{300, 300, 1800, 2850};
|
||||
static constexpr gfx::SizeF kExpectedContentSize{1800, 2732};
|
||||
#endif
|
||||
LoadExtensionAndNavigateToOptionPage();
|
||||
|
||||
@ -1503,9 +1505,9 @@ IN_PROC_BROWSER_TEST_F(
|
||||
// Size is in device units, which is different for macOS. See
|
||||
// PrintSettings::device_units_per_inch().
|
||||
#if BUILDFLAG(IS_MAC)
|
||||
static constexpr gfx::Size kIsoA4PdfPhysicalSize{595, 841};
|
||||
static constexpr gfx::SizeF kIsoA4PdfPhysicalSize{595, 841};
|
||||
#else
|
||||
static constexpr gfx::Size kIsoA4PdfPhysicalSize{2480, 3507};
|
||||
static constexpr gfx::SizeF kIsoA4PdfPhysicalSize{2480, 3507};
|
||||
#endif
|
||||
|
||||
LoadExtensionAndNavigateToOptionPage();
|
||||
|
@ -37,6 +37,8 @@
|
||||
#include "printing/printing_utils.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_OOP_PRINTING)
|
||||
#include "chrome/browser/printing/print_backend_service_manager.h"
|
||||
@ -64,16 +66,16 @@ namespace printing {
|
||||
namespace {
|
||||
|
||||
#if !BUILDFLAG(IS_CHROMEOS)
|
||||
constexpr gfx::Size kLetterPhysicalSize = gfx::Size(612, 792);
|
||||
constexpr gfx::Rect kLetterPrintableArea = gfx::Rect(5, 5, 602, 782);
|
||||
constexpr gfx::Size kLegalPhysicalSize = gfx::Size(612, 1008);
|
||||
constexpr gfx::Rect kLegalPrintableArea = gfx::Rect(5, 5, 602, 998);
|
||||
constexpr gfx::SizeF kLetterPhysicalSize = gfx::SizeF(612, 792);
|
||||
constexpr gfx::RectF kLetterPrintableArea = gfx::RectF(5, 5, 602, 782);
|
||||
constexpr gfx::SizeF kLegalPhysicalSize = gfx::SizeF(612, 1008);
|
||||
constexpr gfx::RectF kLegalPrintableArea = gfx::RectF(5, 5, 602, 998);
|
||||
|
||||
// The default margins are set to 1.0cm in //printing/print_settings.cc, which
|
||||
// is about 28 printer units. The resulting content size is 556 x 736 for
|
||||
// Letter, and similarly is 556 x 952 for Legal.
|
||||
constexpr gfx::Size kLetterExpectedContentSize = gfx::Size(556, 736);
|
||||
constexpr gfx::Size kLegalExpectedContentSize = gfx::Size(556, 952);
|
||||
constexpr gfx::SizeF kLetterExpectedContentSize = gfx::SizeF(556, 736);
|
||||
constexpr gfx::SizeF kLegalExpectedContentSize = gfx::SizeF(556, 952);
|
||||
#endif // !BUILDFLAG(IS_CHROMEOS)
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINT_CONTENT_ANALYSIS)
|
||||
|
@ -73,6 +73,8 @@
|
||||
#include "ui/base/ui_base_features.h"
|
||||
#include "ui/base/webui/web_ui_util.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/rect_conversions.h"
|
||||
#include "ui/gfx/geometry/size_conversions.h"
|
||||
#include "ui/web_dialogs/web_dialog_delegate.h"
|
||||
#include "ui/web_dialogs/web_dialog_ui.h"
|
||||
|
||||
@ -781,7 +783,7 @@ void PrintPreviewUI::DidStartPreview(mojom::DidStartPreviewParamsPtr params,
|
||||
pages_to_render_ = params->pages_to_render;
|
||||
pages_to_render_index_ = 0;
|
||||
pages_per_sheet_ = params->pages_per_sheet;
|
||||
page_size_ = params->page_size;
|
||||
page_size_ = ToFlooredSize(params->page_size);
|
||||
ClearAllPreviewData();
|
||||
|
||||
if (g_test_delegate)
|
||||
@ -792,7 +794,7 @@ void PrintPreviewUI::DidStartPreview(mojom::DidStartPreviewParamsPtr params,
|
||||
|
||||
void PrintPreviewUI::DidGetDefaultPageLayout(
|
||||
mojom::PageSizeMarginsPtr page_layout_in_points,
|
||||
const gfx::Rect& printable_area_in_points,
|
||||
const gfx::RectF& printable_area_in_points,
|
||||
bool all_pages_have_custom_size,
|
||||
bool all_pages_have_custom_orientation,
|
||||
int32_t request_id) {
|
||||
@ -802,7 +804,7 @@ void PrintPreviewUI::DidGetDefaultPageLayout(
|
||||
return;
|
||||
}
|
||||
// Save printable_area_in_points information for N-up conversion.
|
||||
printable_area_ = printable_area_in_points;
|
||||
printable_area_ = ToEnclosedRect(printable_area_in_points);
|
||||
|
||||
if (page_layout_in_points->margin_top < 0 ||
|
||||
page_layout_in_points->margin_left < 0 ||
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "printing/mojom/print.mojom-forward.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_OOP_PRINTING)
|
||||
@ -77,7 +78,7 @@ class PrintPreviewUI : public ConstrainedWebDialogUI,
|
||||
void PrinterSettingsInvalid(int32_t document_cookie,
|
||||
int32_t request_id) override;
|
||||
void DidGetDefaultPageLayout(mojom::PageSizeMarginsPtr page_layout_in_points,
|
||||
const gfx::Rect& printable_area_in_points,
|
||||
const gfx::RectF& printable_area_in_points,
|
||||
bool all_pages_have_custom_size,
|
||||
bool all_pages_have_custom_orientation,
|
||||
int32_t request_id) override;
|
||||
|
@ -44,9 +44,9 @@ void CreateCompositeClientIfNeeded(content::WebContents* web_contents,
|
||||
void RenderParamsFromPrintSettings(const PrintSettings& settings,
|
||||
mojom::PrintParams* params) {
|
||||
const auto& page_setup = settings.page_setup_device_units();
|
||||
params->page_size = page_setup.physical_size();
|
||||
params->content_size = page_setup.content_area().size();
|
||||
params->printable_area = page_setup.printable_area();
|
||||
params->page_size = gfx::SizeF(page_setup.physical_size());
|
||||
params->content_size = gfx::SizeF(page_setup.content_area().size());
|
||||
params->printable_area = gfx::RectF(page_setup.printable_area());
|
||||
params->margin_top = page_setup.content_area().y();
|
||||
params->margin_left = page_setup.content_area().x();
|
||||
params->dpi = settings.dpi_size();
|
||||
|
@ -76,7 +76,7 @@ struct DidStartPreviewParams {
|
||||
// number of pages per sheet and should be greater or equal to 1.
|
||||
int32 pages_per_sheet;
|
||||
// Physical size of the page, including non-printable margins.
|
||||
gfx.mojom.Size page_size;
|
||||
gfx.mojom.SizeF page_size;
|
||||
// Scaling % to fit to page
|
||||
int32 fit_to_page_scaling;
|
||||
};
|
||||
@ -104,16 +104,16 @@ struct DidPreviewDocumentParams {
|
||||
// Parameters for a render request.
|
||||
struct PrintParams {
|
||||
// Physical size of the page, including non-printable margins,
|
||||
// in pixels according to dpi.
|
||||
gfx.mojom.Size page_size;
|
||||
// In pixels according to dpi.
|
||||
gfx.mojom.Size content_size;
|
||||
// Physical printable area of the page in pixels according to dpi.
|
||||
gfx.mojom.Rect printable_area;
|
||||
// The y-offset of the printable area, in pixels according to dpi.
|
||||
int32 margin_top = 0;
|
||||
// The x-offset of the printable area, in pixels according to dpi.
|
||||
int32 margin_left = 0;
|
||||
// in device pixels.
|
||||
gfx.mojom.SizeF page_size;
|
||||
// In device pixels.
|
||||
gfx.mojom.SizeF content_size;
|
||||
// Physical printable area of the page in device pixels.
|
||||
gfx.mojom.RectF printable_area;
|
||||
// The y-offset of the printable area, in device pixels.
|
||||
float margin_top = 0;
|
||||
// The x-offset of the printable area, in device pixels.
|
||||
float margin_left = 0;
|
||||
// Specifies the page orientation.
|
||||
PageOrientation page_orientation = kUpright;
|
||||
// Specifies dots per inch in the x and y direction.
|
||||
@ -270,7 +270,7 @@ interface PrintPreviewUI {
|
||||
// additionally has a custom orientation on ALL pages, otherwise false.
|
||||
[EnableIf=enable_print_preview]
|
||||
DidGetDefaultPageLayout(PageSizeMargins page_layout_in_points,
|
||||
gfx.mojom.Rect printable_area_in_points,
|
||||
gfx.mojom.RectF printable_area_in_points,
|
||||
bool all_pages_have_custom_size,
|
||||
bool all_pages_have_custom_orientation,
|
||||
int32 request_id);
|
||||
|
@ -109,13 +109,6 @@ enum PrintPreviewHelperEvents {
|
||||
PREVIEW_EVENT_MAX,
|
||||
};
|
||||
|
||||
// The result for `CalculatePrintParamsForCss()`. `content_size` is used to
|
||||
// prevent integer truncation when calculating scaled content sizes.
|
||||
struct PrintParamsWithFloatingSize {
|
||||
mojom::PrintParamsPtr params;
|
||||
gfx::SizeF content_size;
|
||||
};
|
||||
|
||||
// Also set in third_party/WebKit/Source/core/page/PrintContext.h
|
||||
constexpr float kPrintingMinimumShrinkFactor = 1.33333333f;
|
||||
|
||||
@ -183,17 +176,17 @@ mojom::PrintParamsPtr GetCssPrintParams(blink::WebLocalFrame* frame,
|
||||
ConvertUnitFloat(page_params.page_size.width(), dpi, kPixelsPerInch),
|
||||
ConvertUnitFloat(page_params.page_size.height(), dpi, kPixelsPerInch));
|
||||
description.margin_top =
|
||||
ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch);
|
||||
description.margin_right = ConvertUnit(page_params.page_size.width() -
|
||||
page_params.content_size.width() -
|
||||
page_params.margin_left,
|
||||
dpi, kPixelsPerInch);
|
||||
description.margin_bottom = ConvertUnit(
|
||||
ConvertUnitFloat(page_params.margin_top, dpi, kPixelsPerInch);
|
||||
description.margin_right = ConvertUnitFloat(
|
||||
page_params.page_size.width() - page_params.content_size.width() -
|
||||
page_params.margin_left,
|
||||
dpi, kPixelsPerInch);
|
||||
description.margin_bottom = ConvertUnitFloat(
|
||||
page_params.page_size.height() - page_params.content_size.height() -
|
||||
page_params.margin_top,
|
||||
dpi, kPixelsPerInch);
|
||||
description.margin_left =
|
||||
ConvertUnit(page_params.margin_left, dpi, kPixelsPerInch);
|
||||
ConvertUnitFloat(page_params.margin_left, dpi, kPixelsPerInch);
|
||||
|
||||
if (frame)
|
||||
frame->GetPageDescription(page_index, &description);
|
||||
@ -213,27 +206,26 @@ mojom::PrintParamsPtr GetCssPrintParams(blink::WebLocalFrame* frame,
|
||||
page_css_params->page_orientation =
|
||||
FromBlinkPageOrientation(description.orientation);
|
||||
|
||||
page_css_params->page_size =
|
||||
gfx::Size(ConvertUnit(description.size.width(), kPixelsPerInch, dpi),
|
||||
ConvertUnit(description.size.height(), kPixelsPerInch, dpi));
|
||||
page_css_params->page_size = gfx::SizeF(
|
||||
ConvertUnitFloat(description.size.width(), kPixelsPerInch, dpi),
|
||||
ConvertUnitFloat(description.size.height(), kPixelsPerInch, dpi));
|
||||
page_css_params->content_size =
|
||||
gfx::Size(ConvertUnit(new_content_width, kPixelsPerInch, dpi),
|
||||
ConvertUnit(new_content_height, kPixelsPerInch, dpi));
|
||||
gfx::SizeF(ConvertUnitFloat(new_content_width, kPixelsPerInch, dpi),
|
||||
ConvertUnitFloat(new_content_height, kPixelsPerInch, dpi));
|
||||
|
||||
page_css_params->margin_top =
|
||||
ConvertUnit(description.margin_top, kPixelsPerInch, dpi);
|
||||
ConvertUnitFloat(description.margin_top, kPixelsPerInch, dpi);
|
||||
page_css_params->margin_left =
|
||||
ConvertUnit(description.margin_left, kPixelsPerInch, dpi);
|
||||
ConvertUnitFloat(description.margin_left, kPixelsPerInch, dpi);
|
||||
return page_css_params;
|
||||
}
|
||||
|
||||
double FitPrintParamsToPage(
|
||||
const mojom::PrintParams& page_params,
|
||||
PrintParamsWithFloatingSize& params_with_floating_size) {
|
||||
mojom::PrintParamsPtr& params_to_fit = params_with_floating_size.params;
|
||||
gfx::SizeF& content_size = params_with_floating_size.content_size;
|
||||
double content_width = static_cast<double>(content_size.width());
|
||||
double content_height = static_cast<double>(content_size.height());
|
||||
double FitPrintParamsToPage(const mojom::PrintParams& page_params,
|
||||
mojom::PrintParams* params_to_fit) {
|
||||
double content_width =
|
||||
static_cast<double>(params_to_fit->content_size.width());
|
||||
double content_height =
|
||||
static_cast<double>(params_to_fit->content_size.height());
|
||||
int default_page_size_height = page_params.page_size.height();
|
||||
int default_page_size_width = page_params.page_size.width();
|
||||
int css_page_size_height = params_to_fit->page_size.height();
|
||||
@ -259,51 +251,44 @@ double FitPrintParamsToPage(
|
||||
params_to_fit->margin_left = static_cast<int>(
|
||||
(default_page_size_width - css_page_size_width * scale_factor) / 2 +
|
||||
(params_to_fit->margin_left * scale_factor));
|
||||
params_to_fit->content_size = gfx::Size(static_cast<int>(content_width),
|
||||
static_cast<int>(content_height));
|
||||
content_size = gfx::SizeF(content_width, content_height);
|
||||
params_to_fit->content_size = gfx::SizeF(content_width, content_height);
|
||||
params_to_fit->page_size = page_params.page_size;
|
||||
return scale_factor;
|
||||
}
|
||||
|
||||
mojom::PageSizeMarginsPtr CalculatePageLayoutFromPrintParams(
|
||||
const PrintParamsWithFloatingSize& params_with_floating_size,
|
||||
const mojom::PrintParams& params,
|
||||
double scale_factor) {
|
||||
const mojom::PrintParams& params = *params_with_floating_size.params;
|
||||
bool fit_to_page = IsPrintScalingOptionFitToPage(params);
|
||||
int dpi = GetDPI(params);
|
||||
int content_width = params.content_size.width();
|
||||
int content_height = params.content_size.height();
|
||||
float content_width = params.content_size.width();
|
||||
float content_height = params.content_size.height();
|
||||
// Scale the content to its normal size for purpose of computing page layout.
|
||||
// Otherwise we will get negative margins.
|
||||
bool scale = fit_to_page || params.print_to_pdf;
|
||||
if (scale && scale_factor >= PrintRenderFrameHelper::kEpsilon) {
|
||||
// `content_size` is used to properly calculate the `content_width` and
|
||||
// `content_height` when multiplied by `scale_factor`. This avoids integer
|
||||
// truncation which would occur with a gfx::Size.
|
||||
const gfx::SizeF& content_size = params_with_floating_size.content_size;
|
||||
content_width = std::round(content_size.width() * scale_factor);
|
||||
content_height = std::round(content_size.height() * scale_factor);
|
||||
content_width = std::round(params.content_size.width() * scale_factor);
|
||||
content_height = std::round(params.content_size.height() * scale_factor);
|
||||
}
|
||||
|
||||
int margin_bottom =
|
||||
float margin_bottom =
|
||||
params.page_size.height() - content_height - params.margin_top;
|
||||
int margin_right =
|
||||
float margin_right =
|
||||
params.page_size.width() - content_width - params.margin_left;
|
||||
|
||||
auto page_layout_in_points = mojom::PageSizeMargins::New();
|
||||
page_layout_in_points->content_width =
|
||||
ConvertUnit(content_width, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(content_width, dpi, kPointsPerInch);
|
||||
page_layout_in_points->content_height =
|
||||
ConvertUnit(content_height, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(content_height, dpi, kPointsPerInch);
|
||||
page_layout_in_points->margin_top =
|
||||
ConvertUnit(params.margin_top, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(params.margin_top, dpi, kPointsPerInch);
|
||||
page_layout_in_points->margin_right =
|
||||
ConvertUnit(margin_right, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(margin_right, dpi, kPointsPerInch);
|
||||
page_layout_in_points->margin_bottom =
|
||||
ConvertUnit(margin_bottom, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(margin_bottom, dpi, kPointsPerInch);
|
||||
page_layout_in_points->margin_left =
|
||||
ConvertUnit(params.margin_left, dpi, kPointsPerInch);
|
||||
ConvertUnitFloat(params.margin_left, dpi, kPointsPerInch);
|
||||
return page_layout_in_points;
|
||||
}
|
||||
|
||||
@ -320,8 +305,8 @@ void EnsureOrientationMatches(const mojom::PrintParams& css_params,
|
||||
page_params->content_size.SetSize(page_params->content_size.height(),
|
||||
page_params->content_size.width());
|
||||
page_params->printable_area.set_size(
|
||||
gfx::Size(page_params->printable_area.height(),
|
||||
page_params->printable_area.width()));
|
||||
gfx::SizeF(page_params->printable_area.height(),
|
||||
page_params->printable_area.width()));
|
||||
}
|
||||
|
||||
blink::WebPrintParams ComputeWebKitPrintParamsInDesiredDpi(
|
||||
@ -352,19 +337,22 @@ blink::WebPrintParams ComputeWebKitPrintParamsInDesiredDpi(
|
||||
webkit_print_params.rasterize_pdf = print_params.rasterize_pdf;
|
||||
webkit_print_params.print_scaling_option = print_params.print_scaling_option;
|
||||
|
||||
webkit_print_params.print_content_area.set_size(gfx::Size(
|
||||
ConvertUnit(print_params.content_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.content_size.height(), dpi, kPointsPerInch)));
|
||||
webkit_print_params.print_content_area.set_size(gfx::SizeF(
|
||||
ConvertUnitFloat(print_params.content_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.content_size.height(), dpi,
|
||||
kPointsPerInch)));
|
||||
|
||||
webkit_print_params.printable_area = gfx::Rect(
|
||||
ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch));
|
||||
webkit_print_params.printable_area = gfx::RectF(
|
||||
ConvertUnitFloat(print_params.printable_area.x(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.y(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.width(), dpi,
|
||||
kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.height(), dpi,
|
||||
kPointsPerInch));
|
||||
|
||||
webkit_print_params.paper_size = gfx::Size(
|
||||
ConvertUnit(print_params.page_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.page_size.height(), dpi, kPointsPerInch));
|
||||
webkit_print_params.paper_size = gfx::SizeF(
|
||||
ConvertUnitFloat(print_params.page_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.page_size.height(), dpi, kPointsPerInch));
|
||||
|
||||
// The following settings is for N-up mode.
|
||||
webkit_print_params.pages_per_sheet = print_params.pages_per_sheet;
|
||||
@ -471,9 +459,9 @@ mojom::MarginType GetMarginsForPdf(blink::WebLocalFrame* frame,
|
||||
}
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
||||
gfx::Size GetPdfPageSize(const gfx::Size& page_size, int dpi) {
|
||||
return gfx::Size(ConvertUnit(page_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(page_size.height(), dpi, kPointsPerInch));
|
||||
gfx::SizeF GetPdfPageSize(const gfx::SizeF& page_size, int dpi) {
|
||||
return gfx::SizeF(ConvertUnitFloat(page_size.width(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(page_size.height(), dpi, kPointsPerInch));
|
||||
}
|
||||
|
||||
ScalingType ScalingTypeFromJobSettings(const base::Value::Dict& job_settings) {
|
||||
@ -521,55 +509,46 @@ mojom::PrintScalingOption GetPrintScalingOption(
|
||||
}
|
||||
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
||||
|
||||
// Helper function to scale and round an integer value with a double valued
|
||||
// scaling.
|
||||
int ScaleAndRound(int value, double scaling) {
|
||||
return static_cast<int>(static_cast<double>(value) / scaling);
|
||||
// Helper functions to inverse-scale float values with a double valued scaling.
|
||||
float InverseScaleDouble(float value, double scaling) {
|
||||
return static_cast<double>(value) / scaling;
|
||||
}
|
||||
gfx::SizeF InverseScaleDouble(gfx::SizeF original, double scaling) {
|
||||
return gfx::SizeF(InverseScaleDouble(original.width(), scaling),
|
||||
InverseScaleDouble(original.height(), scaling));
|
||||
}
|
||||
|
||||
// Helper function to scale the width and height of a gfx::Size by scaling.
|
||||
gfx::Size ScaleAndRoundSize(gfx::Size original, double scaling) {
|
||||
return gfx::Size(ScaleAndRound(original.width(), scaling),
|
||||
ScaleAndRound(original.height(), scaling));
|
||||
}
|
||||
|
||||
PrintParamsWithFloatingSize CalculatePrintParamsForCss(
|
||||
mojom::PrintParamsPtr CalculatePrintParamsForCss(
|
||||
blink::WebLocalFrame* frame,
|
||||
uint32_t page_index,
|
||||
const mojom::PrintParams& page_params,
|
||||
bool ignore_css_margins,
|
||||
bool fit_to_page,
|
||||
double* scale_factor) {
|
||||
PrintParamsWithFloatingSize params_with_floating_size;
|
||||
gfx::SizeF& content_size = params_with_floating_size.content_size;
|
||||
mojom::PrintParamsPtr css_params =
|
||||
GetCssPrintParams(frame, page_index, page_params);
|
||||
|
||||
mojom::PrintParamsPtr params = page_params.Clone();
|
||||
EnsureOrientationMatches(*css_params, params.get());
|
||||
content_size = gfx::SizeF(params->content_size.width() / *scale_factor,
|
||||
params->content_size.height() / *scale_factor);
|
||||
params->content_size.SetSize(static_cast<int>(content_size.width()),
|
||||
static_cast<int>(content_size.height()));
|
||||
params->content_size.SetSize(params->content_size.width() / *scale_factor,
|
||||
params->content_size.height() / *scale_factor);
|
||||
if (ignore_css_margins && fit_to_page) {
|
||||
params_with_floating_size.params = std::move(params);
|
||||
return params_with_floating_size;
|
||||
return params;
|
||||
}
|
||||
|
||||
params_with_floating_size.params = std::move(css_params);
|
||||
mojom::PrintParamsPtr& result_params = params_with_floating_size.params;
|
||||
mojom::PrintParamsPtr result_params = std::move(css_params);
|
||||
// If not printing a pdf or fitting to page, scale the page size.
|
||||
bool scale = !params->print_to_pdf;
|
||||
double page_scaling = scale ? *scale_factor : 1.0f;
|
||||
if (!fit_to_page) {
|
||||
result_params->page_size =
|
||||
ScaleAndRoundSize(result_params->page_size, page_scaling);
|
||||
InverseScaleDouble(result_params->page_size, page_scaling);
|
||||
}
|
||||
if (ignore_css_margins) {
|
||||
// Since not fitting to page, scale the page size and margins.
|
||||
params->margin_left = ScaleAndRound(params->margin_left, page_scaling);
|
||||
params->margin_top = ScaleAndRound(params->margin_top, page_scaling);
|
||||
params->page_size = ScaleAndRoundSize(params->page_size, page_scaling);
|
||||
params->margin_left = InverseScaleDouble(params->margin_left, page_scaling);
|
||||
params->margin_top = InverseScaleDouble(params->margin_top, page_scaling);
|
||||
params->page_size = InverseScaleDouble(params->page_size, page_scaling);
|
||||
|
||||
result_params->margin_top = params->margin_top;
|
||||
result_params->margin_left = params->margin_left;
|
||||
@ -577,40 +556,36 @@ PrintParamsWithFloatingSize CalculatePrintParamsForCss(
|
||||
DCHECK(!fit_to_page);
|
||||
// Since we are ignoring the margins, the css page size is no longer
|
||||
// valid for content.
|
||||
int default_margin_right = params->page_size.width() -
|
||||
params->content_size.width() -
|
||||
params->margin_left;
|
||||
int default_margin_bottom = params->page_size.height() -
|
||||
params->content_size.height() -
|
||||
params->margin_top;
|
||||
int content_width = result_params->page_size.width() -
|
||||
result_params->margin_left - default_margin_right;
|
||||
int content_height = result_params->page_size.height() -
|
||||
result_params->margin_top - default_margin_bottom;
|
||||
result_params->content_size = gfx::Size(content_width, content_height);
|
||||
content_size = gfx::SizeF(result_params->content_size);
|
||||
|
||||
float default_margin_right = params->page_size.width() -
|
||||
params->content_size.width() -
|
||||
params->margin_left;
|
||||
float default_margin_bottom = params->page_size.height() -
|
||||
params->content_size.height() -
|
||||
params->margin_top;
|
||||
result_params->content_size =
|
||||
gfx::SizeF(result_params->page_size.width() -
|
||||
result_params->margin_left - default_margin_right,
|
||||
result_params->page_size.height() -
|
||||
result_params->margin_top - default_margin_bottom);
|
||||
} else {
|
||||
// Using the CSS parameters. Scale CSS content size.
|
||||
content_size =
|
||||
result_params->content_size =
|
||||
gfx::SizeF(result_params->content_size.width() / *scale_factor,
|
||||
result_params->content_size.height() / *scale_factor);
|
||||
result_params->content_size =
|
||||
ScaleAndRoundSize(result_params->content_size, *scale_factor);
|
||||
if (fit_to_page) {
|
||||
double factor = FitPrintParamsToPage(*params, params_with_floating_size);
|
||||
double factor = FitPrintParamsToPage(*params, result_params.get());
|
||||
*scale_factor *= factor;
|
||||
} else {
|
||||
// Already scaled the page, need to also scale the CSS margins since they
|
||||
// are begin applied
|
||||
result_params->margin_left =
|
||||
ScaleAndRound(result_params->margin_left, page_scaling);
|
||||
InverseScaleDouble(result_params->margin_left, page_scaling);
|
||||
result_params->margin_top =
|
||||
ScaleAndRound(result_params->margin_top, page_scaling);
|
||||
InverseScaleDouble(result_params->margin_top, page_scaling);
|
||||
}
|
||||
}
|
||||
|
||||
return params_with_floating_size;
|
||||
return result_params;
|
||||
}
|
||||
|
||||
// Get page layout in points and fit to page if needed.
|
||||
@ -621,12 +596,10 @@ mojom::PageSizeMarginsPtr ComputePageLayoutInPointsForCss(
|
||||
bool ignore_css_margins,
|
||||
double* scale_factor) {
|
||||
double input_scale_factor = *scale_factor;
|
||||
PrintParamsWithFloatingSize params_with_floating_size =
|
||||
CalculatePrintParamsForCss(
|
||||
frame, page_index, page_params, ignore_css_margins,
|
||||
IsPrintScalingOptionFitToPage(page_params), scale_factor);
|
||||
return CalculatePageLayoutFromPrintParams(params_with_floating_size,
|
||||
input_scale_factor);
|
||||
mojom::PrintParamsPtr params = CalculatePrintParamsForCss(
|
||||
frame, page_index, page_params, ignore_css_margins,
|
||||
IsPrintScalingOptionFitToPage(page_params), scale_factor);
|
||||
return CalculatePageLayoutFromPrintParams(*params, input_scale_factor);
|
||||
}
|
||||
|
||||
bool CopyMetafileDataToReadOnlySharedMem(
|
||||
@ -685,10 +658,10 @@ void PrintHeaderAndFooter(cc::PaintCanvas* canvas,
|
||||
cc::PaintCanvasAutoRestore auto_restore(canvas, true);
|
||||
canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor);
|
||||
|
||||
gfx::Size page_size(page_layout.margin_left + page_layout.margin_right +
|
||||
page_layout.content_width,
|
||||
page_layout.margin_top + page_layout.margin_bottom +
|
||||
page_layout.content_height);
|
||||
gfx::SizeF page_size(page_layout.margin_left + page_layout.margin_right +
|
||||
page_layout.content_width,
|
||||
page_layout.margin_top + page_layout.margin_bottom +
|
||||
page_layout.content_height);
|
||||
|
||||
blink::WebView* web_view = blink::WebView::Create(
|
||||
/*client=*/nullptr,
|
||||
@ -1013,9 +986,9 @@ void PrepareFrameAndViewForPrint::ResizeForPrinting() {
|
||||
// the number that produces output with the correct physical size for elements
|
||||
// that are specified in cm, mm, pt etc.
|
||||
// This is important for sites that try to fill the page.
|
||||
gfx::Size print_layout_size(web_print_params_.print_content_area.size());
|
||||
print_layout_size.set_height(
|
||||
ScaleAndRound(print_layout_size.height(), kPrintingMinimumShrinkFactor));
|
||||
gfx::SizeF print_layout_size(web_print_params_.print_content_area.size());
|
||||
print_layout_size.set_height(InverseScaleDouble(
|
||||
print_layout_size.height(), kPrintingMinimumShrinkFactor));
|
||||
|
||||
if (!frame())
|
||||
return;
|
||||
@ -1026,7 +999,8 @@ void PrepareFrameAndViewForPrint::ResizeForPrinting() {
|
||||
return;
|
||||
|
||||
prev_view_size_ = frame()->LocalRoot()->FrameWidget()->Size();
|
||||
frame()->LocalRoot()->FrameWidget()->Resize(print_layout_size);
|
||||
frame()->LocalRoot()->FrameWidget()->Resize(
|
||||
gfx::ToFlooredSize(print_layout_size));
|
||||
}
|
||||
|
||||
void PrepareFrameAndViewForPrint::StartPrinting() {
|
||||
@ -1133,11 +1107,9 @@ void PrepareFrameAndViewForPrint::ComputeScalingAndPrintParams(
|
||||
ComputeWebKitPrintParamsInDesiredDpi(*print_params, is_pdf);
|
||||
frame->PrintBegin(web_print_params_, node_to_print_);
|
||||
double scale_factor = GetScaleFactor(print_params->scale_factor, is_pdf);
|
||||
PrintParamsWithFloatingSize params_with_floating_size =
|
||||
CalculatePrintParamsForCss(frame, /*page_index=*/0, *print_params,
|
||||
ignore_css_margins, fit_to_page,
|
||||
&scale_factor);
|
||||
print_params = std::move(params_with_floating_size.params);
|
||||
print_params = CalculatePrintParamsForCss(frame, /*page_index=*/0,
|
||||
*print_params, ignore_css_margins,
|
||||
fit_to_page, &scale_factor);
|
||||
if (selection)
|
||||
*selection = frame->SelectionAsMarkup().Utf8();
|
||||
frame->PrintEnd();
|
||||
@ -1612,7 +1584,7 @@ void PrintRenderFrameHelper::PrintFrameContent(
|
||||
|
||||
// This subframe doesn't need to fit to the page size, thus we are not using
|
||||
// printing layout for it. It just prints with the specified size.
|
||||
blink::WebPrintParams web_print_params(area_size,
|
||||
blink::WebPrintParams web_print_params(gfx::SizeF(area_size),
|
||||
/*use_printing_layout=*/false);
|
||||
|
||||
// Printing embedded pdf plugin has been broken since pdf plugin viewer was
|
||||
@ -1855,11 +1827,13 @@ PrintRenderFrameHelper::CreatePreviewDocument() {
|
||||
&all_pages_have_custom_orientation);
|
||||
int dpi = GetDPI(print_params);
|
||||
|
||||
gfx::Rect printable_area_in_points(
|
||||
ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch),
|
||||
ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch));
|
||||
gfx::RectF printable_area_in_points(
|
||||
ConvertUnitFloat(print_params.printable_area.x(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.y(), dpi, kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.width(), dpi,
|
||||
kPointsPerInch),
|
||||
ConvertUnitFloat(print_params.printable_area.height(), dpi,
|
||||
kPointsPerInch));
|
||||
|
||||
// Margins: Send default page layout to browser process.
|
||||
preview_ui_->DidGetDefaultPageLayout(
|
||||
@ -2067,7 +2041,7 @@ bool PrintRenderFrameHelper::ProcessPreviewDocument(
|
||||
}
|
||||
|
||||
int PrintRenderFrameHelper::GetFitToPageScaleFactor(
|
||||
const gfx::Rect& printable_area_in_points) {
|
||||
const gfx::RectF& printable_area_in_points) {
|
||||
if (print_preview_context_.IsModifiable())
|
||||
return 100;
|
||||
|
||||
@ -2357,8 +2331,8 @@ bool PrintRenderFrameHelper::PrintPagesNative(
|
||||
mojom::DidPrintDocumentParamsPtr page_params =
|
||||
mojom::DidPrintDocumentParams::New();
|
||||
page_params->content = mojom::DidPrintContentParams::New();
|
||||
page_params->page_size = print_params.page_size;
|
||||
page_params->content_area = gfx::Rect(print_params.page_size);
|
||||
page_params->page_size = ToFlooredSize(print_params.page_size);
|
||||
page_params->content_area = gfx::Rect(page_params->page_size);
|
||||
bool is_pdf =
|
||||
IsPrintingPdfFrame(prep_frame_view_->frame(), prep_frame_view_->node());
|
||||
for (uint32_t printed_page : printed_pages) {
|
||||
|
@ -32,6 +32,8 @@
|
||||
#include "third_party/blink/public/web/web_node.h"
|
||||
#include "third_party/blink/public/web/web_print_client.h"
|
||||
#include "third_party/blink/public/web/web_print_params.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
|
||||
// RenderViewTest-based tests crash on Android
|
||||
@ -313,7 +315,7 @@ class PrintRenderFrameHelper
|
||||
base::ReadOnlySharedMemoryRegion preview_document_region);
|
||||
|
||||
// Helper method to calculate the scale factor for fit-to-page.
|
||||
int GetFitToPageScaleFactor(const gfx::Rect& printable_area_in_points);
|
||||
int GetFitToPageScaleFactor(const gfx::RectF& printable_area_in_points);
|
||||
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
||||
|
||||
// Main printing code -------------------------------------------------------
|
||||
|
@ -47,7 +47,7 @@ void UpdateMargins(int margins_type,
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePageSizeAndScaling(const gfx::Size& page_size,
|
||||
void UpdatePageSizeAndScaling(const gfx::SizeF& page_size,
|
||||
int scale_factor,
|
||||
printing::mojom::PrintParams* params) {
|
||||
params->page_size = page_size;
|
||||
@ -160,7 +160,7 @@ void MockPrinter::ScriptedPrint(int cookie,
|
||||
void MockPrinter::UpdateSettings(printing::mojom::PrintPagesParams* params,
|
||||
const printing::PageRanges& pages,
|
||||
int margins_type,
|
||||
const gfx::Size& page_size,
|
||||
const gfx::SizeF& page_size,
|
||||
int scale_factor) {
|
||||
EXPECT_TRUE(document_cookie_.has_value());
|
||||
|
||||
|
@ -16,8 +16,8 @@
|
||||
#include "printing/image.h"
|
||||
#include "printing/mojom/print.mojom.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
// A class which represents an output page used in the MockPrinter class.
|
||||
// The MockPrinter class stores output pages in a vector, so, this class
|
||||
@ -83,7 +83,7 @@ class MockPrinter {
|
||||
void UpdateSettings(printing::mojom::PrintPagesParams* params,
|
||||
const printing::PageRanges& page_range_array,
|
||||
int margins_type,
|
||||
const gfx::Size& page_size,
|
||||
const gfx::SizeF& page_size,
|
||||
int scale_factor);
|
||||
void PrintPage(printing::mojom::DidPrintDocumentParamsPtr params);
|
||||
|
||||
@ -111,11 +111,11 @@ class MockPrinter {
|
||||
void SetPrintParams(printing::mojom::PrintParams* params);
|
||||
|
||||
// In pixels according to dpi_x and dpi_y.
|
||||
gfx::Size page_size_;
|
||||
gfx::Size content_size_;
|
||||
gfx::SizeF page_size_;
|
||||
gfx::SizeF content_size_;
|
||||
int margin_left_;
|
||||
int margin_top_;
|
||||
gfx::Rect printable_area_;
|
||||
gfx::RectF printable_area_;
|
||||
|
||||
// Specifies dots per inch.
|
||||
double dpi_;
|
||||
|
@ -250,7 +250,7 @@ class FakePrintPreviewUI : public mojom::PrintPreviewUI {
|
||||
RunQuitClosure();
|
||||
}
|
||||
void DidGetDefaultPageLayout(mojom::PageSizeMarginsPtr page_layout_in_points,
|
||||
const gfx::Rect& printable_area_in_points,
|
||||
const gfx::RectF& printable_area_in_points,
|
||||
bool all_pages_have_custom_size,
|
||||
bool all_pages_have_custom_orientation,
|
||||
int32_t request_id) override {
|
||||
|
@ -69,6 +69,7 @@
|
||||
#include "third_party/skia/src/utils/SkMultiPictureDocument.h"
|
||||
#include "ui/events/base_event_utils.h"
|
||||
#include "ui/gfx/ca_layer_result.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
#include "v8/include/v8-context.h"
|
||||
#include "v8/include/v8-exception.h"
|
||||
#include "v8/include/v8-function.h"
|
||||
@ -511,7 +512,7 @@ static void PrintDocument(blink::WebLocalFrame* frame, SkDocument* doc) {
|
||||
const float kMarginLeft = 29.0f; // 0.40 inch
|
||||
const int kContentWidth = 555; // 7.71 inch
|
||||
const int kContentHeight = 735; // 10.21 inch
|
||||
blink::WebPrintParams params(gfx::Size(kContentWidth, kContentHeight));
|
||||
blink::WebPrintParams params(gfx::SizeF(kContentWidth, kContentHeight));
|
||||
params.printer_dpi = 300;
|
||||
uint32_t page_count = frame->PrintBegin(params, blink::WebNode());
|
||||
for (uint32_t i = 0; i < page_count; ++i) {
|
||||
|
@ -113,6 +113,7 @@
|
||||
#include "ui/gfx/geometry/point_f.h"
|
||||
#include "ui/gfx/geometry/rect_conversions.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "ui/gfx/geometry/size_conversions.h"
|
||||
#include "ui/gfx/image/image_skia.h"
|
||||
#include "ui/gfx/image/image_skia_rep.h"
|
||||
#include "ui/gfx/range/range.h"
|
||||
@ -1576,9 +1577,12 @@ int PepperPluginInstanceImpl::PrintBegin(const WebPrintParams& print_params) {
|
||||
}
|
||||
|
||||
PP_PrintSettings_Dev print_settings;
|
||||
print_settings.printable_area = PP_FromGfxRect(print_params.printable_area);
|
||||
print_settings.content_area = PP_FromGfxRect(print_params.print_content_area);
|
||||
print_settings.paper_size = PP_FromGfxSize(print_params.paper_size);
|
||||
print_settings.printable_area =
|
||||
PP_FromGfxRect(gfx::ToEnclosedRect(print_params.printable_area));
|
||||
print_settings.content_area =
|
||||
PP_FromGfxRect(gfx::ToEnclosedRect(print_params.print_content_area));
|
||||
print_settings.paper_size =
|
||||
PP_FromGfxSize(gfx::ToFlooredSize(print_params.paper_size));
|
||||
print_settings.dpi = print_params.printer_dpi;
|
||||
print_settings.orientation = PP_PRINTORIENTATION_NORMAL;
|
||||
print_settings.grayscale = PP_FALSE;
|
||||
|
@ -48,8 +48,8 @@ SkBitmap PrintFrameToBitmap(blink::WebLocalFrame* web_frame,
|
||||
auto* frame_widget = web_frame->LocalRoot()->FrameWidget();
|
||||
frame_widget->UpdateAllLifecyclePhases(blink::DocumentUpdateReason::kTest);
|
||||
|
||||
uint32_t page_count =
|
||||
web_frame->PrintBegin(page_size_in_pixels, blink::WebNode());
|
||||
uint32_t page_count = web_frame->PrintBegin(
|
||||
blink::WebPrintParams(gfx::SizeF(page_size_in_pixels)), blink::WebNode());
|
||||
|
||||
blink::WebVector<uint32_t> pages(
|
||||
printing::PageNumber::GetPages(page_ranges, page_count));
|
||||
|
@ -215,8 +215,8 @@ class TestRenderFrameObserver : public RenderFrameObserver {
|
||||
void ScriptedPrint(bool user_initiated) override {
|
||||
// This is using the main frame for the size, but maybe it should be using
|
||||
// the frame's size.
|
||||
gfx::Size page_size_in_pixels =
|
||||
frame_proxy()->GetLocalRootWebFrameWidget()->Size();
|
||||
gfx::SizeF page_size_in_pixels(
|
||||
frame_proxy()->GetLocalRootWebFrameWidget()->Size());
|
||||
if (page_size_in_pixels.IsEmpty())
|
||||
return;
|
||||
blink::WebPrintParams print_params(page_size_in_pixels);
|
||||
|
@ -23,7 +23,9 @@
|
||||
#include "ui/gfx/codec/jpeg_codec.h"
|
||||
#include "ui/gfx/geometry/point_f.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/rect_conversions.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "ui/gfx/geometry/size_conversions.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
using printing::ConvertUnit;
|
||||
@ -322,10 +324,13 @@ ScopedFPDFDocument PDFiumPrint::CreatePrintPdf(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
gfx::Size int_paper_size = ToFlooredSize(print_params.paper_size);
|
||||
gfx::Rect int_printable_area = ToEnclosedRect(print_params.printable_area);
|
||||
|
||||
float scale_factor = print_params.scale_factor / 100.0f;
|
||||
FitContentsToPrintableAreaIfRequired(
|
||||
output_doc.get(), scale_factor, print_params.print_scaling_option,
|
||||
print_params.paper_size, print_params.printable_area);
|
||||
FitContentsToPrintableAreaIfRequired(output_doc.get(), scale_factor,
|
||||
print_params.print_scaling_option,
|
||||
int_paper_size, int_printable_area);
|
||||
if (!FlattenPrintData(output_doc.get()))
|
||||
return nullptr;
|
||||
|
||||
@ -334,13 +339,12 @@ ScopedFPDFDocument PDFiumPrint::CreatePrintPdf(
|
||||
return output_doc;
|
||||
|
||||
gfx::Rect symmetrical_printable_area =
|
||||
printing::PageSetup::GetSymmetricalPrintableArea(
|
||||
print_params.paper_size, print_params.printable_area);
|
||||
printing::PageSetup::GetSymmetricalPrintableArea(int_paper_size,
|
||||
int_printable_area);
|
||||
if (symmetrical_printable_area.IsEmpty())
|
||||
return nullptr;
|
||||
return CreateNupPdfDocument(std::move(output_doc), pages_per_sheet,
|
||||
print_params.paper_size,
|
||||
symmetrical_printable_area);
|
||||
int_paper_size, symmetrical_printable_area);
|
||||
}
|
||||
|
||||
ScopedFPDFDocument PDFiumPrint::CreateRasterPdf(ScopedFPDFDocument doc,
|
||||
|
@ -34,9 +34,9 @@ using ::testing::ElementsAre;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr gfx::Size kUSLetterSize = {612, 792};
|
||||
constexpr gfx::Rect kUSLetterRect = {{0, 0}, kUSLetterSize};
|
||||
constexpr gfx::Rect kPrintableAreaRect = {{18, 18}, {576, 733}};
|
||||
constexpr gfx::SizeF kUSLetterSize = {612, 792};
|
||||
constexpr gfx::RectF kUSLetterRect = {{0, 0}, kUSLetterSize};
|
||||
constexpr gfx::RectF kPrintableAreaRect = {{18, 18}, {576, 733}};
|
||||
|
||||
using ExpectedDimensions = std::vector<gfx::SizeF>;
|
||||
|
||||
|
@ -13,10 +13,10 @@ namespace blink {
|
||||
// Description of a specific page when printing. All sizes are in pixels.
|
||||
struct WebPrintPageDescription {
|
||||
gfx::SizeF size;
|
||||
int margin_top = 0;
|
||||
int margin_right = 0;
|
||||
int margin_bottom = 0;
|
||||
int margin_left = 0;
|
||||
float margin_top = 0;
|
||||
float margin_right = 0;
|
||||
float margin_bottom = 0;
|
||||
float margin_left = 0;
|
||||
PageOrientation orientation = PageOrientation::kUpright;
|
||||
};
|
||||
|
||||
|
14
third_party/blink/public/web/web_print_params.h
vendored
14
third_party/blink/public/web/web_print_params.h
vendored
@ -32,21 +32,21 @@
|
||||
#define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_PRINT_PARAMS_H_
|
||||
|
||||
#include "printing/mojom/print.mojom-shared.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "ui/gfx/geometry/rect_f.h"
|
||||
#include "ui/gfx/geometry/size_f.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
struct WebPrintParams {
|
||||
// Specifies printable content rect in points (a point is 1/72 of an inch).
|
||||
gfx::Rect print_content_area;
|
||||
gfx::RectF print_content_area;
|
||||
|
||||
// Specifies the selected printer default printable area details in
|
||||
// points.
|
||||
gfx::Rect printable_area;
|
||||
gfx::RectF printable_area;
|
||||
|
||||
// Specifies the selected printer default paper size in points.
|
||||
gfx::Size paper_size;
|
||||
gfx::SizeF paper_size;
|
||||
|
||||
// Specifies user selected DPI for printing.
|
||||
int printer_dpi = 72;
|
||||
@ -70,10 +70,10 @@ struct WebPrintParams {
|
||||
|
||||
WebPrintParams() = default;
|
||||
|
||||
WebPrintParams(const gfx::Size& paper_size)
|
||||
explicit WebPrintParams(const gfx::SizeF& paper_size)
|
||||
: WebPrintParams(paper_size, true) {}
|
||||
|
||||
WebPrintParams(const gfx::Size& paper_size, bool use_printing_layout)
|
||||
WebPrintParams(const gfx::SizeF& paper_size, bool use_printing_layout)
|
||||
: print_content_area(paper_size),
|
||||
printable_area(print_content_area),
|
||||
paper_size(paper_size),
|
||||
|
@ -5486,7 +5486,7 @@ TEST_F(WebViewTest, ResizeForPrintingViewportUnits) {
|
||||
gfx::Size page_size(300, 360);
|
||||
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
|
||||
gfx::Size expected_size = PrintICBSizeFromPageSize(page_size);
|
||||
|
||||
@ -5529,7 +5529,7 @@ TEST_F(WebViewTest, WidthMediaQueryWithPageZoomAfterPrinting) {
|
||||
Color::FromRGB(0, 128, 0),
|
||||
div->GetComputedStyle()->VisitedDependentColor(GetCSSPropertyColor()));
|
||||
|
||||
gfx::Size page_size(300, 360);
|
||||
gfx::SizeF page_size(300, 360);
|
||||
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
@ -5570,7 +5570,7 @@ TEST_F(WebViewTest, ViewportUnitsPrintingWithPageZoom) {
|
||||
int expected_width = PrintICBSizeFromPageSize(page_size).width();
|
||||
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
|
||||
frame->PrintBegin(print_params, WebNode());
|
||||
|
||||
@ -5589,7 +5589,7 @@ TEST_F(WebViewTest, ResizeWithFixedPosCrash) {
|
||||
WebLocalFrameImpl* frame = web_view->MainFrameImpl();
|
||||
gfx::Size page_size(300, 360);
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
frame->PrintBegin(print_params, WebNode());
|
||||
web_view->MainFrameWidget()->Resize(page_size);
|
||||
frame->PrintEnd();
|
||||
|
@ -9117,7 +9117,7 @@ TEST_F(WebFrameTest, PrintingBasic)
|
||||
WebLocalFrame* frame = web_view_helper.LocalMainFrame();
|
||||
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(gfx::Size(500, 500));
|
||||
print_params.print_content_area.set_size(gfx::SizeF(500, 500));
|
||||
|
||||
uint32_t page_count = frame->PrintBegin(print_params, WebNode());
|
||||
EXPECT_EQ(1u, page_count);
|
||||
@ -13537,7 +13537,7 @@ TEST_F(WebFrameTest, AltTextOnAboutBlankPage) {
|
||||
static void TestFramePrinting(WebLocalFrameImpl* frame) {
|
||||
WebPrintParams print_params;
|
||||
gfx::Size page_size(500, 500);
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
EXPECT_EQ(1u, frame->PrintBegin(print_params, WebNode()));
|
||||
cc::PaintRecorder recorder;
|
||||
frame->PrintPagesForTesting(recorder.beginRecording(), page_size, page_size);
|
||||
@ -13601,7 +13601,7 @@ std::vector<TextRunDOMNodeIdInfo> GetPrintedTextRunDOMNodeIds(
|
||||
const WebVector<uint32_t>* pages = nullptr) {
|
||||
WebPrintParams print_params;
|
||||
gfx::Size page_size(500, 500);
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
|
||||
frame->PrintBegin(print_params, WebNode());
|
||||
cc::PaintRecorder recorder;
|
||||
@ -13838,7 +13838,7 @@ TEST_F(WebFrameSimTest, PageOrientation) {
|
||||
|
||||
auto* frame = WebView().MainFrame()->ToWebLocalFrame();
|
||||
WebPrintParams print_params;
|
||||
print_params.print_content_area.set_size(page_size);
|
||||
print_params.print_content_area.set_size(gfx::SizeF(page_size));
|
||||
EXPECT_EQ(4u, frame->PrintBegin(print_params, WebNode()));
|
||||
|
||||
WebPrintPageDescription description;
|
||||
|
@ -341,6 +341,20 @@ class ChromePrintContext : public PrintContext {
|
||||
|
||||
void BeginPrintMode(float width, float height) override {
|
||||
DCHECK(!printed_page_width_);
|
||||
|
||||
// Although layout itself can handle subpixels, we'll round down to the
|
||||
// nearest integer. There are a couple of reasons for this. First of all,
|
||||
// PrintContext sets page rectangles in integers, and if the input size is
|
||||
// larger than that (by a fraction of a pixel), excess pages may be
|
||||
// created. Furthermore, the printing code will also round down the sizes
|
||||
// for the output canvas, so anything larger than that will end up getting
|
||||
// clipped.
|
||||
//
|
||||
// TODO(mstensho): Refactor page rectangle calculation, and update the
|
||||
// comment above.
|
||||
width = floorf(width);
|
||||
height = floorf(height);
|
||||
|
||||
printed_page_width_ = width;
|
||||
PrintContext::BeginPrintMode(printed_page_width_, height);
|
||||
}
|
||||
@ -533,7 +547,7 @@ class ChromePluginPrintContext final : public ChromePrintContext {
|
||||
|
||||
void BeginPrintMode(float width, float height) override {
|
||||
gfx::Rect rect(gfx::ToFlooredSize(gfx::SizeF(width, height)));
|
||||
print_params_.print_content_area = rect;
|
||||
print_params_.print_content_area = gfx::RectF(rect);
|
||||
page_rects_.Fill(rect, plugin_->PrintBegin(print_params_));
|
||||
}
|
||||
|
||||
|
@ -1986,7 +1986,7 @@ TEST_P(FrameThrottlingTest, PrintThrottledFrame) {
|
||||
auto* sub_frame = To<LocalFrame>(frame_element->ContentFrame());
|
||||
EXPECT_TRUE(sub_frame->View()->ShouldThrottleRenderingForTest());
|
||||
auto* web_frame = WebLocalFrameImpl::FromFrame(sub_frame);
|
||||
WebPrintParams print_params(gfx::Size(500, 500));
|
||||
WebPrintParams print_params(gfx::SizeF(500, 500));
|
||||
web_frame->PrintBegin(print_params, blink::WebNode());
|
||||
cc::PaintRecorder recorder;
|
||||
web_frame->PrintPage(0, recorder.beginRecording());
|
||||
|
Reference in New Issue
Block a user