0

Extend PDFiumPage text run breaking logic for highlight annotations

This CL extends the work in http://crrev.com/c/1714773 for highlights.
The set of text breakpoints in
PDFiumPage::CalculatePageObjectTextRunBreaks() are extended to include
highlight end points as well. PDFiumPage::GetTextRunInfo() uses this set
to determine text run boundaries. A unit test validating this change
for highlights has been added.

Bug: 1008775
Change-Id: Ib7c66395aec6897af374e7ad6feb993f8f7dc9c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1928374
Commit-Queue: Kalpak Tapas <katapas@microsoft.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718125}
This commit is contained in:
Kalpak Tapas
2019-11-22 14:30:19 +00:00
committed by Commit Bot
parent 2114d6e317
commit 3c413e4fe7
2 changed files with 73 additions and 0 deletions

@ -273,6 +273,20 @@ void PDFiumPage::CalculatePageObjectTextRunBreaks() {
}
}
}
PopulateHighlights();
for (const auto& highlight : highlights_) {
if (highlight.start_char_index >= 0 &&
highlight.start_char_index < chars_count) {
page_object_text_run_breaks_.insert(highlight.start_char_index);
int next_text_run_break_index =
highlight.start_char_index + highlight.char_count;
// Don't insert a break if the highlight is at the end of the page text.
if (next_text_run_break_index < chars_count) {
page_object_text_run_breaks_.insert(next_text_run_break_index);
}
}
}
}
void PDFiumPage::CalculateTextRunStyleInfo(

@ -291,6 +291,65 @@ TEST_F(PDFiumPageTextTest, GetTextRunInfo) {
ASSERT_FALSE(text_run_info_result.has_value());
}
TEST_F(PDFiumPageTextTest, TestHighlightTextRunInfo) {
TestClient client;
std::unique_ptr<PDFiumEngine> engine =
InitializeEngine(&client, FILE_PATH_LITERAL("highlights.pdf"));
ASSERT_TRUE(engine);
// Highlights span across text run indices 0, 2 and 3.
static const pp::PDF::PrivateAccessibilityTextStyleInfo kExpectedStyle = {
"Helvetica", 0, PP_TEXTRENDERINGMODE_FILL, 16, 0xff000000, 0xff000000,
false, false};
pp::PDF::PrivateAccessibilityTextRunInfo expected_text_runs[] = {
{5,
PP_MakeFloatRectFromXYWH(1.3333334f, 198.66667f, 46.666668f, 14.666672f),
PP_PrivateDirection::PP_PRIVATEDIRECTION_LTR, kExpectedStyle},
{7,
PP_MakeFloatRectFromXYWH(50.666668f, 198.66667f, 47.999996f, 17.333328f),
PP_PrivateDirection::PP_PRIVATEDIRECTION_LTR, kExpectedStyle},
{7,
PP_MakeFloatRectFromXYWH(106.66666f, 198.66667f, 73.333336f, 18.666672f),
PP_PrivateDirection::PP_PRIVATEDIRECTION_LTR, kExpectedStyle},
{2, PP_MakeFloatRectFromXYWH(188.0f, 202.66667f, 9.333333f, 14.666667f),
PP_PrivateDirection::PP_PRIVATEDIRECTION_NONE, kExpectedStyle},
{2,
PP_MakeFloatRectFromXYWH(198.66667f, 202.66667f, 21.333328f, 10.666672f),
PP_PrivateDirection::PP_PRIVATEDIRECTION_LTR, kExpectedStyle}};
if (IsRunningOnChromeOS()) {
expected_text_runs[2].bounds = PP_MakeFloatRectFromXYWH(
106.66666f, 198.66667f, 73.333336f, 19.999985f);
expected_text_runs[4].bounds = PP_MakeFloatRectFromXYWH(
198.66667f, 201.33333f, 21.333328f, 12.000015f);
}
PDFiumPage* page = GetPDFiumPageForTest(engine.get(), 0);
ASSERT_TRUE(page);
int current_char_index = 0;
for (const auto& expected_text_run : expected_text_runs) {
base::Optional<pp::PDF::PrivateAccessibilityTextRunInfo>
text_run_info_result = engine->GetTextRunInfo(0, current_char_index);
ASSERT_TRUE(text_run_info_result.has_value());
const auto& text_run_info = text_run_info_result.value();
EXPECT_EQ(expected_text_run.len, text_run_info.len);
CompareRect(expected_text_run.bounds, text_run_info.bounds);
EXPECT_EQ(expected_text_run.direction, text_run_info.direction);
EXPECT_EQ(kExpectedStyle.font_name, text_run_info.style.font_name);
EXPECT_EQ(kExpectedStyle.font_weight, text_run_info.style.font_weight);
EXPECT_EQ(kExpectedStyle.render_mode, text_run_info.style.render_mode);
EXPECT_EQ(kExpectedStyle.font_size, text_run_info.style.font_size);
EXPECT_EQ(kExpectedStyle.fill_color, text_run_info.style.fill_color);
EXPECT_EQ(kExpectedStyle.stroke_color, text_run_info.style.stroke_color);
EXPECT_EQ(kExpectedStyle.is_italic, text_run_info.style.is_italic);
EXPECT_EQ(kExpectedStyle.is_bold, text_run_info.style.is_bold);
current_char_index += text_run_info.len;
}
}
using PDFiumPageHighlightTest = PDFiumTestBase;
TEST_F(PDFiumPageHighlightTest, TestPopulateHighlights) {