0

[PDF Ink Signatures] Add GetCanonicalToPdfTransform() helper

Add a helper function to pdf_ink_transform.h that hands out transforms
to do conversions from canonical to PDF coordinates. This is based on
existing code in pdfium_ink_writer.cc, which has been replaced with a
call to the helper function. The transform will be used in the near
future to read paths from PDFs, so it is better to have it in a shared
location.

Bug: 353942910
Change-Id: Ib909047ca750b03479175eb549bd29a43d9878b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5981016
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Alan Screen <awscreen@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1376361}
This commit is contained in:
Lei Zhang
2024-10-31 16:10:55 +00:00
committed by Chromium LUCI CQ
parent cedeae8fb9
commit 0738dbedba
4 changed files with 38 additions and 12 deletions

@ -9,8 +9,10 @@
#include "base/check_op.h"
#include "base/notreached.h"
#include "printing/units.h"
#include "third_party/ink/src/ink/geometry/envelope.h"
#include "third_party/ink/src/ink/geometry/rect.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
@ -145,4 +147,12 @@ gfx::Rect CanonicalInkEnvelopeToInvalidationScreenRect(
return gfx::ToEnclosingRect(gfx::RectF(x, y, w, h));
}
gfx::AxisTransform2d GetCanonicalToPdfTransform(float page_height) {
CHECK_GE(page_height, 0);
constexpr float kScreenToPageScale =
static_cast<float>(printing::kPointsPerInch) / printing::kPixelsPerInch;
return gfx::AxisTransform2d::FromScaleAndTranslation(
{kScreenToPageScale, -kScreenToPageScale}, {0, page_height});
}
} // namespace chrome_pdf

@ -8,6 +8,7 @@
#include "pdf/buildflags.h"
#include "pdf/page_orientation.h"
#include "third_party/ink/src/ink/geometry/affine_transform.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
@ -110,6 +111,16 @@ gfx::Rect CanonicalInkEnvelopeToInvalidationScreenRect(
const gfx::Rect& page_content_rect,
float scale_factor);
// Returns a transform that converts from canonical coordinates (which has a
// top-left origin and a different DPI), to PDF coordinates (which has a
// bottom-left origin).
//
// `page_height` is in points. It must not be negative.
//
// Note that callers can call gfx::AxisTransform2d::Invert() to get a transform
// that does conversions in the opposite direction.
gfx::AxisTransform2d GetCanonicalToPdfTransform(float page_height);
} // namespace chrome_pdf
#endif // PDF_PDF_INK_TRANSFORM_H_

@ -13,6 +13,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/ink/src/ink/geometry/affine_transform.h"
#include "third_party/ink/src/ink/geometry/envelope.h"
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
@ -414,4 +415,17 @@ TEST(PdfInkTransformTest,
}
}
TEST(PdfInkTransformTest, GetCanonicalToPdfTransform) {
{
gfx::AxisTransform2d tr = GetCanonicalToPdfTransform(/*page_height=*/0);
EXPECT_EQ(gfx::Vector2dF(0.75f, -0.75f), tr.scale());
EXPECT_EQ(gfx::Vector2dF(0, 0), tr.translation());
}
{
gfx::AxisTransform2d tr = GetCanonicalToPdfTransform(/*page_height=*/712);
EXPECT_EQ(gfx::Vector2dF(0.75f, -0.75f), tr.scale());
EXPECT_EQ(gfx::Vector2dF(0, 712), tr.translation());
}
}
} // namespace chrome_pdf

@ -10,7 +10,7 @@
#include "base/containers/span.h"
#include "base/memory/raw_ref.h"
#include "pdf/pdf_ink_conversions.h"
#include "printing/units.h"
#include "pdf/pdf_ink_transform.h"
#include "third_party/ink/src/ink/geometry/mesh.h"
#include "third_party/ink/src/ink/geometry/modeled_shape.h"
#include "third_party/ink/src/ink/geometry/point.h"
@ -20,9 +20,6 @@
#include "ui/gfx/geometry/axis_transform2d.h"
#include "ui/gfx/geometry/point_f.h"
using printing::kPixelsPerInch;
using printing::kPointsPerInch;
namespace chrome_pdf {
namespace {
@ -146,14 +143,8 @@ ScopedFPDFPageObject WriteShapeToNewPathOnPage(const ink::ModeledShape& shape,
return nullptr; // `shape` is empty.
}
// The transform converts from canonical coordinates (which has a top-left
// origin and a different DPI), to PDF coordinates (which has a bottom-left
// origin).
constexpr float kScreenToPageScale =
static_cast<float>(kPointsPerInch) / kPixelsPerInch;
const auto transform = gfx::AxisTransform2d::FromScaleAndTranslation(
{kScreenToPageScale, -kScreenToPageScale},
{0, FPDF_GetPageHeightF(page)});
const gfx::AxisTransform2d transform =
GetCanonicalToPdfTransform(FPDF_GetPageHeightF(page));
// Create a path using the first outline.
ScopedFPDFPageObject path =