0

[PDF Ink Signatures] Add CalculatePageBoundaryIntersectPoint() helper

Add a utility function that calculates where a line that starts inside a
rect and ends outside a rect intersects the rect. This will be used to
calculate where strokes enter/exit the page, given input events do not
always perfectly land at page boundaries.

Bug: 352578791
Change-Id: If8e33d09a0fe29ffd8f5b578d3ab8c8d2455ec97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5751342
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Andy Phan <andyphan@chromium.org>
Reviewed-by: Alan Screen <awscreen@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1335789}
This commit is contained in:
Lei Zhang
2024-08-01 01:35:54 +00:00
committed by Chromium LUCI CQ
parent 0c9e8edd59
commit c38fabe91a
4 changed files with 389 additions and 0 deletions

@ -212,6 +212,8 @@ if (enable_pdf) {
if (enable_pdf_ink2) {
sources += [
"draw_utils/page_boundary_intersect.cc",
"draw_utils/page_boundary_intersect.h",
"pdf_ink_brush.cc",
"pdf_ink_brush.h",
"pdf_ink_module.cc",
@ -470,6 +472,7 @@ if (enable_pdf) {
if (enable_pdf_ink2) {
sources += [
"draw_utils/page_boundary_intersect_unittest.cc",
"pdf_ink_brush_unittest.cc",
"pdf_ink_module_unittest.cc",
"pdf_ink_transform_unittest.cc",

@ -0,0 +1,87 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/draw_utils/page_boundary_intersect.h"
#include "base/check.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
namespace chrome_pdf {
namespace {
// gfx::Rect considers points on its bottom and right border to be not contained
// within the rect. So to make sure CalculatePageBoundaryIntersectPoint()
// returns points within the page, use this constant to move them inwards a bit.
constexpr float kBoundaryEpsilon = 0.0001f;
} // namespace
gfx::PointF CalculatePageBoundaryIntersectPoint(
const gfx::Rect& page_rect,
const gfx::PointF& inside_point,
const gfx::PointF& outside_point) {
const gfx::RectF rect(page_rect);
CHECK(!rect.IsEmpty());
CHECK(rect.Contains(inside_point));
CHECK(!rect.Contains(outside_point));
const float x_diff = outside_point.x() - inside_point.x();
const float y_diff = outside_point.y() - inside_point.y();
// Handle the special case where calculating the slope would divide by 0.
if (x_diff == 0) {
if (y_diff > 0) {
return {inside_point.x(), rect.bottom() - kBoundaryEpsilon};
}
return {inside_point.x(), rect.y()};
}
// Handle the special case where dividing by the slope would be dividing by 0.
if (y_diff == 0) {
if (x_diff > 0) {
return {rect.right() - kBoundaryEpsilon, inside_point.y()};
}
return {rect.x(), inside_point.y()};
}
// For all other cases, calculate where the line between `inside_point` and
// `outside_point` would intersect `rect` on its horizontal and vertical
// boundaries, assuming the line going towards `outside_point` is infinitely
// long in that direction, and the boundary lines also extends infinitely.
const float slope = y_diff / x_diff;
gfx::PointF left_or_right_boundary_intersection_point;
if (x_diff > 0) {
float y_distance = (rect.right() - inside_point.x()) * slope;
left_or_right_boundary_intersection_point = {
rect.right() - kBoundaryEpsilon, inside_point.y() + y_distance};
} else {
float y_distance = (inside_point.x() - rect.x()) * slope;
left_or_right_boundary_intersection_point = {rect.x(),
inside_point.y() - y_distance};
}
gfx::PointF top_or_bottom_boundary_intersection_point;
if (y_diff > 0) {
float x_distance = (rect.bottom() - inside_point.y()) / slope;
top_or_bottom_boundary_intersection_point = {
inside_point.x() + x_distance, rect.bottom() - kBoundaryEpsilon};
} else {
float x_distance = (inside_point.y() - rect.y()) / slope;
top_or_bottom_boundary_intersection_point = {inside_point.x() - x_distance,
rect.y()};
}
// Then return the result closest to `inside_point`.
float left_or_right_point_length =
(left_or_right_boundary_intersection_point - inside_point).Length();
float top_or_bottom_point_length =
(top_or_bottom_boundary_intersection_point - inside_point).Length();
return left_or_right_point_length < top_or_bottom_point_length
? left_or_right_boundary_intersection_point
: top_or_bottom_boundary_intersection_point;
}
} // namespace chrome_pdf

@ -0,0 +1,30 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PDF_DRAW_UTILS_PAGE_BOUNDARY_INTERSECT_H_
#define PDF_DRAW_UTILS_PAGE_BOUNDARY_INTERSECT_H_
#include "ui/gfx/geometry/point_f.h"
namespace gfx {
class Rect;
} // namespace gfx
namespace chrome_pdf {
// Given:
// - `page_rect` must be non-empty.
// - `inside_point` must be inside of `page_rect`.
// - `outside_point` must be outside of `page_rect`.
//
// A straight line from `inside_point` to `outside_point` must intersect the
// boundary of `page_rect`. Return that intersection point.
gfx::PointF CalculatePageBoundaryIntersectPoint(
const gfx::Rect& page_rect,
const gfx::PointF& inside_point,
const gfx::PointF& outside_point);
} // namespace chrome_pdf
#endif // PDF_DRAW_UTILS_PAGE_BOUNDARY_INTERSECT_H_

@ -0,0 +1,269 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/draw_utils/page_boundary_intersect.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/test/geometry_util.h"
namespace chrome_pdf {
namespace {
constexpr gfx::Rect kRect(5, 10, 20, 30);
constexpr gfx::PointF kInsidePoint(15, 20);
constexpr gfx::PointF kTopLeftPoint(5, 10);
constexpr gfx::PointF kTopPoint(15, 10);
constexpr gfx::PointF kTopRightPoint(24.9999f, 10);
constexpr gfx::PointF kRightPoint(24.9999f, 30);
constexpr gfx::PointF kBottomRightPoint(24.9999f, 39.9999f);
constexpr gfx::PointF kBottomPoint(20, 39.9999f);
constexpr gfx::PointF kBottomLeftPoint(5, 39.9999f);
constexpr gfx::PointF kLeftPoint(5, 30);
constexpr float kTolerance = 0.001f;
} // namespace
TEST(PageBoundaryIntersectPoint, Left) {
EXPECT_POINTF_NEAR(gfx::PointF(5, 13.3333f),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(0, 10)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(5, 20),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(0, 20)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(5, 33.3333f),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(0, 40)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, Top) {
EXPECT_POINTF_NEAR(gfx::PointF(10, 10),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(5, 0)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(15, 10),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(15, 0)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(17.5f, 10),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(20, 0)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, Right) {
EXPECT_POINTF_NEAR(gfx::PointF(25, 16.6667f),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(30, 15)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(25, 20),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(30, 20)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(25, 23.3333f),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(30, 25)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, Bottom) {
EXPECT_POINTF_NEAR(gfx::PointF(8.3333f, 40),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(5, 50)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(15, 40),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(15, 50)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(18.3333f, 40),
CalculatePageBoundaryIntersectPoint(kRect, kInsidePoint,
gfx::PointF(20, 50)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, BorderGoingOutwards) {
EXPECT_POINTF_NEAR(kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(10, 0)),
kTolerance);
EXPECT_POINTF_NEAR(
kTopPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopPoint, gfx::PointF(10, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(10, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kRightPoint,
gfx::PointF(30, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(30, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomPoint,
CalculatePageBoundaryIntersectPoint(kRect, kBottomPoint,
gfx::PointF(15, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(15, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kLeftPoint,
gfx::PointF(0, 10)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, BorderGoingAcrossPage) {
EXPECT_POINTF_NEAR(gfx::PointF(12.5f, 40),
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(15, 50)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(15, 40),
CalculatePageBoundaryIntersectPoint(kRect, kTopPoint,
gfx::PointF(15, 50)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(5, 34),
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(0, 40)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(22.5f, 40),
CalculatePageBoundaryIntersectPoint(kRect, kRightPoint,
gfx::PointF(20, 50)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(21.25f, 10),
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(20, 0)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(20, 10),
CalculatePageBoundaryIntersectPoint(kRect, kBottomPoint,
gfx::PointF(20, 0)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(16.25f, 10),
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(20, 0)),
kTolerance);
EXPECT_POINTF_NEAR(gfx::PointF(25, 26),
CalculatePageBoundaryIntersectPoint(kRect, kLeftPoint,
gfx::PointF(80, 15)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, BorderTopBottomEdge) {
EXPECT_POINTF_NEAR(kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(0, 10)),
kTolerance);
EXPECT_POINTF_NEAR(
kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopPoint, gfx::PointF(0, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(0, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(30, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopPoint,
gfx::PointF(30, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(30, 10)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(0, 40)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kBottomPoint,
gfx::PointF(0, 40)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(0, 40)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(30, 40)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kBottomPoint,
gfx::PointF(30, 40)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(30, 40)),
kTolerance);
}
TEST(PageBoundaryIntersectPoint, BorderLeftRightEdge) {
EXPECT_POINTF_NEAR(kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(5, 0)),
kTolerance);
EXPECT_POINTF_NEAR(
kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kLeftPoint, gfx::PointF(5, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kTopLeftPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(5, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopLeftPoint,
gfx::PointF(5, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(kRect, kLeftPoint,
gfx::PointF(5, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomLeftPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomLeftPoint, gfx::PointF(5, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(25, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kRightPoint,
gfx::PointF(25, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kTopRightPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(25, 0)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kTopRightPoint,
gfx::PointF(25, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(kRect, kRightPoint,
gfx::PointF(25, 50)),
kTolerance);
EXPECT_POINTF_NEAR(kBottomRightPoint,
CalculatePageBoundaryIntersectPoint(
kRect, kBottomRightPoint, gfx::PointF(25, 50)),
kTolerance);
}
} // namespace chrome_pdf