summaryrefslogtreecommitdiff
path: root/fpdfsdk/fpdftext_embeddertest.cpp
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-11-30 21:02:41 +0000
committerChromium commit bot <commit-bot@chromium.org>2017-11-30 21:02:41 +0000
commit8b357e7504ea804293983453540ae91c9fc57922 (patch)
tree7b8f611eac73034f9149b014fb547d6886e0d5b7 /fpdfsdk/fpdftext_embeddertest.cpp
parent0ae8e03cc2d310ba0ba19b878ea448f17a577cdb (diff)
downloadpdfium-8b357e7504ea804293983453540ae91c9fc57922.tar.xz
Rewrite lower level details of extracting text from page
The current implementation of text extraction was difficult to understand, duplicated logic that existed in other methods, and wasn't clear about the units the inputs were in. It also didn't handle control characters correctly. The new implementation leans on the methods for converting indices between the text buffer index and character list index spaces to avoid duplication of code. It also makes it clear to the reader that inputs are in the character list index space. Finally, it fixes issues being seen in Chrome with respect of ranges being slightly off. This CL also adds a test for extracting text that has control characters. BUG=pdfium:942,chromium:654578 Change-Id: Id9d1f360c2d7492c7b5a48d6c9ae29f530892742 Reviewed-on: https://pdfium-review.googlesource.com/20014 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org> Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'fpdfsdk/fpdftext_embeddertest.cpp')
-rw-r--r--fpdfsdk/fpdftext_embeddertest.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/fpdfsdk/fpdftext_embeddertest.cpp b/fpdfsdk/fpdftext_embeddertest.cpp
index 51216b9818..60654057b9 100644
--- a/fpdfsdk/fpdftext_embeddertest.cpp
+++ b/fpdfsdk/fpdftext_embeddertest.cpp
@@ -590,3 +590,38 @@ TEST_F(FPDFTextEmbeddertest, bug_782596) {
FPDFText_ClosePage(textpage);
UnloadPage(page);
}
+
+TEST_F(FPDFTextEmbeddertest, ControlCharacters) {
+ EXPECT_TRUE(OpenDocument("control_characters.pdf"));
+ FPDF_PAGE page = LoadPage(0);
+ EXPECT_TRUE(page);
+
+ FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
+ EXPECT_TRUE(textpage);
+
+ // Should not include the control characters in the output
+ static const char expected[] = "Hello, world!\r\nGoodbye, world!";
+ unsigned short fixed_buffer[128];
+ memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
+ int num_chars = FPDFText_GetText(textpage, 0, 128, fixed_buffer);
+
+ ASSERT_GE(num_chars, 0);
+ EXPECT_EQ(sizeof(expected), static_cast<size_t>(num_chars));
+ EXPECT_TRUE(check_unsigned_shorts(expected, fixed_buffer, sizeof(expected)));
+
+ // Attempting to get a chunk of text after the control characters
+ static const char expected_substring[] = "Goodbye, world!";
+ // Offset is the length of 'Hello, world!\r\n' + 2 control characters in the
+ // original stream
+ static const int offset = 17;
+ memset(fixed_buffer, 0xbd, sizeof(fixed_buffer));
+ num_chars = FPDFText_GetText(textpage, offset, 128, fixed_buffer);
+
+ ASSERT_GE(num_chars, 0);
+ EXPECT_EQ(sizeof(expected_substring), static_cast<size_t>(num_chars));
+ EXPECT_TRUE(check_unsigned_shorts(expected_substring, fixed_buffer,
+ sizeof(expected_substring)));
+
+ FPDFText_ClosePage(textpage);
+ UnloadPage(page);
+}