0

[PDF Ink Signatures] Add brush invalidation area method

Points collected while doing stroking with a brush will not be visible
to the user unless the viewport is properly notified to repaint the
affected areas.

Introduce a PdfInkBrush method to help determine the minimal area to be
invalidated based on the size of the brush.

Bug: 335517471
Change-Id: Ie0b787d6fc8b5a028d1e39b42749cdf02e173738
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5634395
Commit-Queue: Alan Screen <awscreen@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1316610}
This commit is contained in:
Alan Screen
2024-06-18 19:03:39 +00:00
committed by Chromium LUCI CQ
parent cd48c09121
commit a955b76c7c
4 changed files with 89 additions and 2 deletions

@ -457,6 +457,7 @@ if (enable_pdf) {
if (enable_pdf_ink2) {
sources += [
"ink_module_unittest.cc",
"pdf_ink_brush_unittest.cc",
"pdf_ink_transform_unittest.cc",
"pdf_ink_undo_redo_model_unittest.cc",
]

@ -13,6 +13,8 @@
#include "pdf/ink/ink_brush_family.h"
#include "pdf/ink/ink_brush_paint.h"
#include "pdf/ink/ink_brush_tip.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
namespace chrome_pdf {
@ -64,6 +66,17 @@ std::unique_ptr<InkBrush> CreateInkBrush(PdfInkBrush::Type type,
/*epsilon=*/0.1f);
}
// Determine the area to invalidate centered around a point where a brush is
// applied.
gfx::Rect GetPointInvalidateArea(float brush_diameter,
const gfx::PointF& center) {
// Choose a rectangle that surrounds the point for the brush radius.
float brush_radius = brush_diameter / 2;
return gfx::ToEnclosingRect(gfx::RectF(center.x() - brush_radius,
center.y() - brush_radius,
brush_diameter, brush_diameter));
}
} // namespace
// static
@ -89,4 +102,17 @@ const InkBrush& PdfInkBrush::GetInkBrush() const {
return *ink_brush_;
}
// Determine the area to invalidate encompassing a line between two
// consecutive points where a brush is applied.
gfx::Rect PdfInkBrush::GetInvalidateArea(const gfx::PointF& center1,
const gfx::PointF& center2) const {
// For a line connecting `center1` to `center2`, the invalidate
// region is the union between the areas affected by them both.
float brush_diameter = ink_brush_->GetSize();
gfx::Rect area1 = GetPointInvalidateArea(brush_diameter, center1);
gfx::Rect area2 = GetPointInvalidateArea(brush_diameter, center2);
area2.Union(area1);
return area2;
}
} // namespace chrome_pdf

@ -10,13 +10,18 @@
#include <string>
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/rect.h"
namespace gfx {
class PointF;
}
namespace chrome_pdf {
class InkBrush;
// A wrapper class used to create ink brushes specifically for PDF annotation
// mode.
// A class used to create ink brushes for PDF annotation mode and support
// invalidation for rendering.
class PdfInkBrush {
public:
// The types of brushes supported in PDF annotation mode.
@ -37,6 +42,13 @@ class PdfInkBrush {
PdfInkBrush& operator=(const PdfInkBrush&) = delete;
~PdfInkBrush();
// Determine the area to invalidate encompassing a line between two
// consecutive points where a brush is applied. Values are in screen-based
// coordinates. The area to invalidated is correlated to the size of the
// brush.
gfx::Rect GetInvalidateArea(const gfx::PointF& center1,
const gfx::PointF& center2) const;
// Converts `brush_type` to a `Type`, returning `std::nullopt` if `brush_type`
// does not correspond to any `Type`.
static std::optional<Type> StringToType(const std::string& brush_type);

@ -0,0 +1,48 @@
// 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/pdf_ink_brush.h"
#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
namespace chrome_pdf {
namespace {
std::unique_ptr<chrome_pdf::PdfInkBrush> CreatePdfInkBrush(float size) {
return std::make_unique<chrome_pdf::PdfInkBrush>(
chrome_pdf::PdfInkBrush::Type::kPen,
chrome_pdf::PdfInkBrush::Params{SK_ColorBLACK, size});
}
} // namespace
TEST(PdfInkBrushTest, InvalidateSinglePoint) {
constexpr gfx::PointF kPoint(40.0f, 16.0f);
auto brush = CreatePdfInkBrush(/*size=*/10.0f);
EXPECT_EQ(gfx::Rect(35, 11, 10, 10),
brush->GetInvalidateArea(kPoint, kPoint));
}
TEST(PdfInkBrushTest, InvalidateSinglePointNearBorder) {
// Using a point closer to the border than the radius of the brush results in
// the invalidation area including a negative origin.
constexpr gfx::PointF kPoint(3.0f, 4.0f);
auto brush = CreatePdfInkBrush(/*size=*/13.0f);
EXPECT_EQ(gfx::Rect(-4, -3, 14, 14),
brush->GetInvalidateArea(kPoint, kPoint));
}
TEST(PdfInkBrushTest, InvalidateDifferentPoints) {
auto brush = CreatePdfInkBrush(/*size=*/10.0f);
EXPECT_EQ(gfx::Rect(10, 11, 35, 26),
brush->GetInvalidateArea(gfx::PointF(40.0f, 16.0f),
gfx::PointF(15.0f, 32.0f)));
}
} // namespace chrome_pdf