Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 1 | // Copyright 2017 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 5 | #include <algorithm> |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 6 | #include <cwchar> |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 11 | #include "constants/annotation_common.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 12 | #include "core/fxcrt/fx_system.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 13 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 14 | #include "public/fpdf_annot.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 15 | #include "public/fpdf_edit.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 16 | #include "public/fpdfview.h" |
| 17 | #include "testing/embedder_test.h" |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 18 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 21 | class FPDFAnnotEmbedderTest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 22 | |
Lei Zhang | 4afee17 | 2018-01-10 20:57:15 +0000 | [diff] [blame] | 23 | std::wstring BufferToWString(const std::vector<char>& buf) { |
| 24 | return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data())); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Lei Zhang | 4afee17 | 2018-01-10 20:57:15 +0000 | [diff] [blame] | 27 | std::string BufferToString(const std::vector<char>& buf) { |
| 28 | return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data())); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 31 | TEST_F(FPDFAnnotEmbedderTest, BadParams) { |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 32 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 33 | FPDF_PAGE page = LoadPage(0); |
| 34 | ASSERT_TRUE(page); |
| 35 | |
| 36 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr)); |
| 37 | |
| 38 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0)); |
| 39 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1)); |
| 40 | EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1)); |
| 41 | EXPECT_FALSE(FPDFPage_GetAnnot(page, -1)); |
| 42 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 1)); |
| 43 | |
| 44 | EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr)); |
| 45 | |
| 46 | EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr)); |
| 47 | |
| 48 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0)); |
| 49 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1)); |
| 50 | EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1)); |
| 51 | |
| 52 | EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo")); |
| 53 | |
| 54 | static constexpr wchar_t kContents[] = L"Bar"; |
| 55 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 56 | GetFPDFWideString(kContents); |
| 57 | EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get())); |
| 58 | |
| 59 | char buffer[128]; |
| 60 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0)); |
| 61 | EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0)); |
| 62 | EXPECT_EQ(0u, |
| 63 | FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer))); |
| 64 | |
| 65 | UnloadPage(page); |
| 66 | } |
| 67 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 68 | TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) { |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 69 | // Open a file with one annotation and load its first page. |
| 70 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 71 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 72 | ASSERT_TRUE(page); |
| 73 | |
| 74 | // This annotation has a malformed appearance stream, which does not have its |
| 75 | // normal appearance defined, only its rollover appearance. In this case, its |
| 76 | // normal appearance should be generated, allowing the highlight annotation to |
| 77 | // still display. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 78 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | a98e366 | 2018-02-07 20:28:35 +0000 | [diff] [blame] | 79 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 80 | |
| 81 | UnloadPage(page); |
| 82 | } |
| 83 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 84 | TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) { |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 85 | const char md5_hash[] = "76512832d88017668d9acc7aacd13dae"; |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 86 | // Open a file with multiline markup annotations. |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 87 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 88 | FPDF_PAGE page = LoadPage(0); |
| 89 | ASSERT_TRUE(page); |
| 90 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 91 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Ralf Sippl | b3a5240 | 2018-03-19 23:30:28 +0000 | [diff] [blame] | 92 | CompareBitmap(bitmap.get(), 595, 842, md5_hash); |
| 93 | |
| 94 | UnloadPage(page); |
| 95 | } |
| 96 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 97 | TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 98 | // Open a file with one annotation and load its first page. |
| 99 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 100 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 101 | ASSERT_TRUE(page); |
| 102 | |
| 103 | // Check that there is a total of 1 annotation on its first page. |
| 104 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 105 | |
| 106 | // Check that the annotation is of type "highlight". |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 107 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 108 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 109 | ASSERT_TRUE(annot); |
| 110 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 111 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 112 | // Check that the annotation color is yellow. |
| 113 | unsigned int R; |
| 114 | unsigned int G; |
| 115 | unsigned int B; |
| 116 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 117 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 118 | &G, &B, &A)); |
| 119 | EXPECT_EQ(255u, R); |
| 120 | EXPECT_EQ(255u, G); |
| 121 | EXPECT_EQ(0u, B); |
| 122 | EXPECT_EQ(255u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 123 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 124 | // Check that the author is correct. |
| 125 | static constexpr char kAuthorKey[] = "T"; |
| 126 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 127 | FPDFAnnot_GetValueType(annot.get(), kAuthorKey)); |
| 128 | unsigned long len = |
| 129 | FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0); |
| 130 | std::vector<char> buf(len); |
| 131 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(), |
| 132 | len)); |
| 133 | EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str()); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 134 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 135 | // Check that the content is correct. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 136 | EXPECT_EQ( |
| 137 | FPDF_OBJECT_STRING, |
| 138 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents)); |
| 139 | len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents, |
| 140 | nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 141 | buf.clear(); |
| 142 | buf.resize(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 143 | EXPECT_EQ(2690u, |
| 144 | FPDFAnnot_GetStringValue( |
| 145 | annot.get(), pdfium::annotation::kContents, buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 146 | const wchar_t contents[] = |
| 147 | L"This is a note for that highlight annotation. Very long highlight " |
| 148 | "annotation. Long long long Long long longLong long longLong long " |
| 149 | "longLong long longLong long longLong long longLong long longLong long " |
| 150 | "longLong long longLong long longLong long longLong long longLong long " |
| 151 | "longLong long longLong long longLong long longLong long longLong long " |
| 152 | "longLong long longLong long longLong long longLong long longLong long " |
| 153 | "longLong long longLong long longLong long longLong long longLong long " |
| 154 | "longLong long longLong long longLong long longLong long longLong long " |
| 155 | "longLong long longLong long longLong long longLong long longLong long " |
| 156 | "longLong long longLong long longLong long longLong long longLong long " |
| 157 | "longLong long longLong long longLong long longLong long longLong long " |
| 158 | "longLong long longLong long longLong long longLong long longLong long " |
| 159 | "longLong long longLong long longLong long longLong long longLong long " |
| 160 | "longLong long longLong long longLong long longLong long longLong long " |
| 161 | "longLong long longLong long longLong long longLong long longLong long " |
| 162 | "longLong long longLong long longLong long longLong long longLong long " |
| 163 | "longLong long longLong long longLong long longLong long longLong long " |
| 164 | "longLong long longLong long longLong long longLong long longLong long " |
| 165 | "longLong long longLong long longLong long longLong long longLong long " |
| 166 | "longLong long long. END"; |
| 167 | EXPECT_STREQ(contents, BufferToWString(buf).c_str()); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 168 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 169 | // Check that the quadpoints are correct. |
| 170 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 171 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 172 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 173 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 174 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 175 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 176 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 177 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 180 | TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 181 | // Open a file with three annotations and load its first page. |
| 182 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 183 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 184 | ASSERT_TRUE(page); |
| 185 | |
| 186 | // Check that there is a total of 3 annotation on its first page. |
| 187 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 188 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 189 | { |
| 190 | // Check that the third annotation is of type "ink". |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 191 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 192 | ASSERT_TRUE(annot); |
| 193 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 194 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 195 | // Check that the annotation color is blue with opacity. |
| 196 | unsigned int R; |
| 197 | unsigned int G; |
| 198 | unsigned int B; |
| 199 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 200 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 201 | &G, &B, &A)); |
| 202 | EXPECT_EQ(0u, R); |
| 203 | EXPECT_EQ(0u, G); |
| 204 | EXPECT_EQ(255u, B); |
| 205 | EXPECT_EQ(76u, A); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 206 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 207 | // Check that there is no content. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 208 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue( |
| 209 | annot.get(), pdfium::annotation::kContents, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 210 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 211 | // Check that the rectange coordinates are correct. |
| 212 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
| 213 | FS_RECTF rect; |
| 214 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 215 | EXPECT_EQ(351.820404f, rect.left); |
| 216 | EXPECT_EQ(583.830688f, rect.bottom); |
| 217 | EXPECT_EQ(475.336090f, rect.right); |
| 218 | EXPECT_EQ(681.535034f, rect.top); |
| 219 | } |
Tom Sepez | ef43c26 | 2018-11-07 16:41:32 +0000 | [diff] [blame] | 220 | { |
| 221 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
| 222 | CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a"); |
| 223 | } |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 224 | UnloadPageNoEvents(page); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 225 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 226 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 227 | TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 228 | // Open a file with one annotation and load its first page. |
| 229 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 230 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 231 | ASSERT_TRUE(page); |
| 232 | |
| 233 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 234 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 235 | |
| 236 | UnloadPage(page); |
| 237 | } |
| 238 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 239 | TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 240 | // Open a file with no annotation and load its first page. |
| 241 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 242 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 243 | ASSERT_TRUE(page); |
| 244 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 245 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 246 | { |
| 247 | // Add a text annotation to the page. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 248 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 249 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 250 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 251 | // Check that there is now 1 annotations on this page. |
| 252 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 253 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 254 | // Check that the subtype of the annotation is correct. |
| 255 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 256 | } |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 257 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 258 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 259 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 260 | ASSERT_TRUE(annot); |
| 261 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 262 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 263 | // Set the color of the annotation. |
| 264 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 265 | 102, 153, 204)); |
| 266 | // Check that the color has been set correctly. |
| 267 | unsigned int R; |
| 268 | unsigned int G; |
| 269 | unsigned int B; |
| 270 | unsigned int A; |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 271 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 272 | &G, &B, &A)); |
| 273 | EXPECT_EQ(51u, R); |
| 274 | EXPECT_EQ(102u, G); |
| 275 | EXPECT_EQ(153u, B); |
| 276 | EXPECT_EQ(204u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 277 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 278 | // Change the color of the annotation. |
| 279 | ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204, |
| 280 | 153, 102, 51)); |
| 281 | // Check that the color has been set correctly. |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 282 | ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R, |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 283 | &G, &B, &A)); |
| 284 | EXPECT_EQ(204u, R); |
| 285 | EXPECT_EQ(153u, G); |
| 286 | EXPECT_EQ(102u, B); |
| 287 | EXPECT_EQ(51u, A); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 288 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 289 | // Set the annotation rectangle. |
| 290 | FS_RECTF rect; |
| 291 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 292 | EXPECT_EQ(0.f, rect.left); |
| 293 | EXPECT_EQ(0.f, rect.right); |
| 294 | rect.left = 35; |
| 295 | rect.bottom = 150; |
| 296 | rect.right = 53; |
| 297 | rect.top = 165; |
| 298 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 299 | // Check that the annotation rectangle has been set correctly. |
| 300 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 301 | EXPECT_EQ(35.f, rect.left); |
| 302 | EXPECT_EQ(150.f, rect.bottom); |
| 303 | EXPECT_EQ(53.f, rect.right); |
| 304 | EXPECT_EQ(165.f, rect.top); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 305 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 306 | // Set the content of the annotation. |
| 307 | static constexpr wchar_t kContents[] = |
| 308 | L"Hello! This is a customized content."; |
| 309 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 310 | GetFPDFWideString(kContents); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 311 | ASSERT_TRUE(FPDFAnnot_SetStringValue( |
| 312 | annot.get(), pdfium::annotation::kContents, text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 313 | // Check that the content has been set correctly. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 314 | unsigned long len = FPDFAnnot_GetStringValue( |
| 315 | annot.get(), pdfium::annotation::kContents, nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 316 | std::vector<char> buf(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 317 | EXPECT_EQ(74u, |
| 318 | FPDFAnnot_GetStringValue( |
| 319 | annot.get(), pdfium::annotation::kContents, buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 320 | EXPECT_STREQ(kContents, BufferToWString(buf).c_str()); |
| 321 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 322 | UnloadPage(page); |
| 323 | } |
| 324 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 325 | TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 326 | // Open a file with one annotation and load its first page. |
| 327 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 328 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 329 | ASSERT_TRUE(page); |
| 330 | |
| 331 | // Check that there is a total of one annotation on its first page, and verify |
| 332 | // its quadpoints. |
| 333 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 334 | FS_QUADPOINTSF quadpoints; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 335 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 336 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 337 | ASSERT_TRUE(annot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 338 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 339 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 340 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 341 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 342 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 343 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 344 | |
| 345 | // Add an underline annotation to the page and set its quadpoints. |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 346 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 347 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 348 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 349 | ASSERT_TRUE(annot); |
| 350 | quadpoints.x1 = 140.802643f; |
| 351 | quadpoints.x3 = 140.802643f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 352 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 353 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 354 | |
| 355 | // Save the document, closing the page and document. |
| 356 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 357 | UnloadPage(page); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 358 | |
| 359 | // Open the saved document. |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 360 | const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 361 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 362 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 363 | page = LoadSavedPage(0); |
| 364 | VerifySavedRendering(page, 612, 792, md5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 365 | |
| 366 | // Check that the saved document has 2 annotations on the first page |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 367 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 368 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 369 | { |
| 370 | // Check that the second annotation is an underline annotation and verify |
| 371 | // its quadpoints. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 372 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 373 | ASSERT_TRUE(new_annot); |
| 374 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get())); |
| 375 | FS_QUADPOINTSF new_quadpoints; |
| 376 | ASSERT_TRUE( |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 377 | FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 378 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 379 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 380 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 381 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 382 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 383 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 384 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 385 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 386 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 387 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 388 | TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) { |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 389 | // Open a file with four annotations and load its first page. |
| 390 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 391 | FPDF_PAGE page = LoadPage(0); |
| 392 | ASSERT_TRUE(page); |
| 393 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 394 | |
| 395 | // Retrieve the highlight annotation. |
| 396 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 397 | ASSERT_TRUE(annot); |
| 398 | ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 399 | |
| 400 | FS_QUADPOINTSF quadpoints; |
| 401 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 402 | |
| 403 | { |
| 404 | // Verify the current one set of quadpoints. |
| 405 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 406 | |
| 407 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 408 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 409 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 410 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 411 | } |
| 412 | |
| 413 | { |
| 414 | // Update the quadpoints. |
| 415 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 416 | new_quadpoints.y1 -= 20.f; |
| 417 | new_quadpoints.y2 -= 20.f; |
| 418 | new_quadpoints.y3 -= 20.f; |
| 419 | new_quadpoints.y4 -= 20.f; |
| 420 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints)); |
| 421 | |
| 422 | // Verify added quadpoint set |
| 423 | ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 424 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints)); |
| 425 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 426 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 427 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 428 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 429 | } |
| 430 | |
| 431 | { |
| 432 | // Append a new set of quadpoints. |
| 433 | FS_QUADPOINTSF new_quadpoints = quadpoints; |
| 434 | new_quadpoints.y1 += 20.f; |
| 435 | new_quadpoints.y2 += 20.f; |
| 436 | new_quadpoints.y3 += 20.f; |
| 437 | new_quadpoints.y4 += 20.f; |
| 438 | ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints)); |
| 439 | |
| 440 | // Verify added quadpoint set |
| 441 | ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot)); |
| 442 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints)); |
| 443 | EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f); |
| 444 | EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f); |
| 445 | EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f); |
| 446 | EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f); |
| 447 | } |
| 448 | |
| 449 | { |
| 450 | // Setting and getting quadpoints at out-of-bound index should fail |
| 451 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints)); |
| 452 | EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints)); |
| 453 | } |
| 454 | |
| 455 | FPDFPage_CloseAnnot(annot); |
| 456 | |
| 457 | // Retrieve the square annotation |
| 458 | FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2); |
| 459 | |
| 460 | { |
| 461 | // Check that attempting to set its quadpoints would fail |
| 462 | ASSERT_TRUE(squareAnnot); |
| 463 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot)); |
| 464 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot)); |
| 465 | EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints)); |
| 466 | } |
| 467 | |
| 468 | FPDFPage_CloseAnnot(squareAnnot); |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 469 | UnloadPage(page); |
| 470 | } |
| 471 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 472 | TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 473 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 474 | const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941"; |
| 475 | const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2"; |
| 476 | const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 477 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 478 | const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 479 | const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 480 | const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 481 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 482 | const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 483 | const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 484 | const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 485 | #endif |
| 486 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 487 | // Open a file with four annotations and load its first page. |
| 488 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 489 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 490 | ASSERT_TRUE(page); |
| 491 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 492 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 493 | // Check that the original file renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 494 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 495 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 496 | CompareBitmap(bitmap.get(), 612, 792, md5_original); |
| 497 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 498 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 499 | FS_RECTF rect; |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 500 | FS_RECTF new_rect; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 501 | |
| 502 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 503 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 504 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 505 | ASSERT_TRUE(annot); |
| 506 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 507 | |
| 508 | // Check that color cannot be set when an AP stream is defined already. |
| 509 | EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51, |
| 510 | 102, 153, 204)); |
| 511 | |
| 512 | // Verify its attachment points. |
| 513 | FS_QUADPOINTSF quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 514 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 515 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 516 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 517 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 518 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
| 519 | |
| 520 | // Check that updating the attachment points would succeed. |
| 521 | quadpoints.x1 -= 50.f; |
| 522 | quadpoints.x2 -= 50.f; |
| 523 | quadpoints.x3 -= 50.f; |
| 524 | quadpoints.x4 -= 50.f; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 525 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 526 | FS_QUADPOINTSF new_quadpoints; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 527 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 528 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 529 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 530 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 531 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 532 | |
| 533 | // Check that updating quadpoints does not change the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 534 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 535 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 536 | CompareBitmap(bitmap.get(), 612, 792, md5_original); |
| 537 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 538 | |
| 539 | // Verify its annotation rectangle. |
| 540 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 541 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 542 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 543 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 544 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 545 | |
| 546 | // Check that updating the rectangle would succeed. |
| 547 | rect.left -= 60.f; |
| 548 | rect.right -= 60.f; |
| 549 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 550 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 551 | EXPECT_EQ(rect.right, new_rect.right); |
| 552 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 553 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 554 | // Check that updating the rectangle changes the annotation's position. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 555 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 556 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 557 | CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight); |
| 558 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 559 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 560 | { |
| 561 | // Retrieve the square annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 562 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 563 | ASSERT_TRUE(annot); |
| 564 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get())); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 565 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 566 | // Check that updating the rectangle would succeed. |
| 567 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 568 | rect.left += 70.f; |
| 569 | rect.right += 70.f; |
| 570 | ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 571 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 572 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 573 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 574 | // Check that updating the rectangle changes the square annotation's |
| 575 | // position. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 576 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 577 | CompareBitmap(bitmap.get(), 612, 792, md5_modified_square); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 578 | } |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 579 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 580 | UnloadPage(page); |
| 581 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 582 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 583 | TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) { |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 584 | // Open a file with multiline markup annotations. |
| 585 | ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf")); |
| 586 | FPDF_PAGE page = LoadPage(0); |
| 587 | ASSERT_TRUE(page); |
| 588 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 589 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5098b25 | 2018-03-26 21:46:00 +0000 | [diff] [blame] | 590 | ASSERT_TRUE(annot); |
| 591 | |
| 592 | // This is a three line annotation. |
| 593 | EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get())); |
| 594 | } |
| 595 | UnloadPage(page); |
| 596 | |
| 597 | // null annotation should return 0 |
| 598 | EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr)); |
| 599 | } |
| 600 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 601 | TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) { |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 602 | // Open a file with 3 annotations on its first page. |
| 603 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 604 | FPDF_PAGE page = LoadPageNoEvents(0); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 605 | ASSERT_TRUE(page); |
| 606 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 607 | |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 608 | FS_RECTF rect; |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 609 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 610 | // Check that the annotations have the expected rectangle coordinates. |
| 611 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 612 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 613 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 614 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 615 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 616 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 617 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 618 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 619 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 620 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 621 | } |
| 622 | |
| 623 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 624 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 625 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 626 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 627 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 628 | |
| 629 | // Check that nothing happens when attempting to remove an annotation with an |
| 630 | // out-of-bound index. |
| 631 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 632 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 633 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 634 | |
| 635 | // Remove the second annotation. |
| 636 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 637 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 638 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 639 | |
| 640 | // Save the document, closing the page and document. |
| 641 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Tom Sepez | 507d019 | 2018-11-07 16:37:51 +0000 | [diff] [blame] | 642 | UnloadPageNoEvents(page); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 643 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 644 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 645 | // Open the saved document. |
| 646 | std::string new_file = GetString(); |
| 647 | FPDF_FILEACCESS file_access; |
| 648 | memset(&file_access, 0, sizeof(file_access)); |
| 649 | file_access.m_FileLen = new_file.size(); |
| 650 | file_access.m_GetBlock = GetBlockFromString; |
| 651 | file_access.m_Param = &new_file; |
| 652 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 653 | ASSERT_TRUE(new_doc); |
| 654 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 655 | ASSERT_TRUE(new_page); |
| 656 | |
| 657 | // Check that the saved document has 2 annotations on the first page. |
| 658 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 659 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 660 | // Check that the remaining 2 annotations are the original 1st and 3rd ones |
| 661 | // by verifying their rectangle coordinates. |
| 662 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 663 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 664 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 665 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 666 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 667 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 668 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 669 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 670 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect)); |
| 671 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 672 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 673 | FPDF_ClosePage(new_page); |
| 674 | FPDF_CloseDocument(new_doc); |
| 675 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 676 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 677 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 678 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 679 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
Dan Sinclair | 844d79e | 2018-02-16 03:46:26 +0000 | [diff] [blame] | 680 | const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b"; |
| 681 | const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7"; |
| 682 | const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 683 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 684 | const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 685 | const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7"; |
| 686 | const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13"; |
| 687 | const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 688 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 689 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
Dan Sinclair | 844d79e | 2018-02-16 03:46:26 +0000 | [diff] [blame] | 690 | const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38"; |
| 691 | const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0"; |
| 692 | const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 693 | #endif |
| 694 | |
| 695 | // Open a file with two annotations and load its first page. |
| 696 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 697 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 698 | ASSERT_TRUE(page); |
| 699 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 700 | |
| 701 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 702 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 703 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 704 | CompareBitmap(bitmap.get(), 595, 842, md5_original); |
| 705 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 706 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 707 | { |
| 708 | // Retrieve the stamp annotation which has its AP stream already defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 709 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 710 | ASSERT_TRUE(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 711 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 712 | // Check that this annotation has one path object and retrieve it. |
| 713 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 714 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 715 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1); |
| 716 | EXPECT_FALSE(path); |
| 717 | path = FPDFAnnot_GetObject(annot.get(), 0); |
| 718 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
| 719 | EXPECT_TRUE(path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 720 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 721 | // Modify the color of the path object. |
| 722 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255)); |
| 723 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 724 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 725 | // Check that the page with the modified annotation renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 726 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 727 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 728 | CompareBitmap(bitmap.get(), 595, 842, md5_modified_path); |
| 729 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 730 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 731 | // Add a second path object to the same annotation. |
| 732 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 733 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
| 734 | EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 735 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14)); |
| 736 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 737 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot)); |
| 738 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 739 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 740 | // The object is in the annontation, not in the page, so the page object |
| 741 | // array should not change. |
| 742 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
| 743 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 744 | // Check that the page with an annotation with two paths renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 745 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 746 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 747 | CompareBitmap(bitmap.get(), 595, 842, md5_two_paths); |
| 748 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 749 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 750 | // Delete the newly added path object. |
| 751 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1)); |
| 752 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 753 | ASSERT_EQ(32, FPDFPage_CountObjects(page)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 754 | } |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 755 | |
| 756 | // Check that the page renders the same as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 757 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 758 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 759 | CompareBitmap(bitmap.get(), 595, 842, md5_modified_path); |
| 760 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 761 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 762 | FS_RECTF rect; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 763 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 764 | { |
| 765 | // Create another stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 766 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 767 | ASSERT_TRUE(annot); |
| 768 | rect.left = 200.f; |
| 769 | rect.bottom = 400.f; |
| 770 | rect.right = 500.f; |
| 771 | rect.top = 600.f; |
| 772 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 773 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 774 | // Add a new path to the annotation. |
| 775 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 776 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 777 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 778 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 779 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
| 780 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180)); |
| 781 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 782 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 783 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check)); |
| 784 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 785 | |
| 786 | // Check that the annotation's bounding box came from its rectangle. |
| 787 | FS_RECTF new_rect; |
| 788 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 789 | EXPECT_EQ(rect.left, new_rect.left); |
| 790 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 791 | EXPECT_EQ(rect.right, new_rect.right); |
| 792 | EXPECT_EQ(rect.top, new_rect.top); |
| 793 | } |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 794 | |
| 795 | // Save the document, closing the page and document. |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 796 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 797 | UnloadPage(page); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 798 | |
| 799 | // Open the saved document. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 800 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 801 | page = LoadSavedPage(0); |
| 802 | VerifySavedRendering(page, 595, 842, md5_new_annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 803 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 804 | // Check that the document has a correct count of annotations and objects. |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 805 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 806 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 807 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 808 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 809 | ASSERT_TRUE(annot); |
| 810 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 811 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 812 | // Check that the new annotation's rectangle is as defined. |
| 813 | FS_RECTF new_rect; |
| 814 | ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect)); |
| 815 | EXPECT_EQ(rect.left, new_rect.left); |
| 816 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 817 | EXPECT_EQ(rect.right, new_rect.right); |
| 818 | EXPECT_EQ(rect.top, new_rect.top); |
| 819 | } |
| 820 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 821 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 822 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 823 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 824 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 825 | TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) { |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 826 | // Open a file with an annotation and load its first page. |
| 827 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 828 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 829 | ASSERT_TRUE(page); |
| 830 | |
| 831 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 832 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 833 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 834 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 835 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 836 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 837 | { |
| 838 | // Retrieve the annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 839 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 840 | ASSERT_TRUE(annot); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 841 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 842 | // Check that the original flag values are as expected. |
| 843 | int flags = FPDFAnnot_GetFlags(annot.get()); |
| 844 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 845 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 846 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 847 | // Set the HIDDEN flag. |
| 848 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 849 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 850 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 851 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 852 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 853 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 854 | // Check that the page renders correctly without rendering the annotation. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 855 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 856 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 857 | CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); |
| 858 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 859 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 860 | // Unset the HIDDEN flag. |
| 861 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE)); |
| 862 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get())); |
| 863 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 864 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags)); |
| 865 | flags = FPDFAnnot_GetFlags(annot.get()); |
| 866 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 867 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 868 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 869 | // Check that the page renders correctly as before. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 870 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 871 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 872 | CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 873 | } |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 874 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 875 | |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 876 | UnloadPage(page); |
| 877 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 878 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 879 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 880 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 881 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 882 | const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596"; |
| 883 | const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 884 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 885 | const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 886 | const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e"; |
| 887 | const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 888 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 889 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 890 | const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208"; |
| 891 | const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 892 | #endif |
| 893 | |
| 894 | // Open a file with two annotations and load its first page. |
| 895 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 896 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 897 | ASSERT_TRUE(page); |
| 898 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 899 | |
| 900 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 901 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 902 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 903 | CompareBitmap(bitmap.get(), 595, 842, md5_original); |
| 904 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 905 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 906 | constexpr int kBitmapSize = 200; |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 907 | FPDF_BITMAP image_bitmap; |
| 908 | |
| 909 | { |
| 910 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 911 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 912 | ASSERT_TRUE(annot); |
| 913 | FS_RECTF rect; |
| 914 | rect.left = 200.f; |
| 915 | rect.bottom = 600.f; |
| 916 | rect.right = 400.f; |
| 917 | rect.top = 800.f; |
| 918 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
| 919 | |
| 920 | // Add a solid-color translucent image object to the new annotation. |
| 921 | image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 922 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 923 | 0xeeeecccc); |
| 924 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 925 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 926 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 927 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 928 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 929 | kBitmapSize, 0, 0)); |
| 930 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 931 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object)); |
| 932 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 933 | |
| 934 | // Check that the page renders correctly with the new image object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 935 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 936 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 937 | CompareBitmap(bitmap.get(), 595, 842, md5_new_image); |
| 938 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 939 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 940 | { |
| 941 | // Retrieve the newly added stamp annotation and its image object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 942 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 943 | ASSERT_TRUE(annot); |
| 944 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 945 | FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 946 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 947 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 948 | // Modify the image in the new annotation. |
| 949 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, |
| 950 | 0xff000000); |
| 951 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 952 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object)); |
| 953 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 954 | |
| 955 | // Save the document, closing the page and document. |
| 956 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 957 | UnloadPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 958 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 959 | |
| 960 | // Test that the saved document renders the modified image object correctly. |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 961 | VerifySavedDocument(595, 842, md5_modified_image); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 962 | } |
| 963 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 964 | TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 965 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 966 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 967 | const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286"; |
| 968 | const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 969 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 970 | const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275"; |
| 971 | const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55"; |
| 972 | const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 973 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 974 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 975 | const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5"; |
| 976 | const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 977 | #endif |
| 978 | |
| 979 | // Open a file with two annotations and load its first page. |
| 980 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 981 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 982 | ASSERT_TRUE(page); |
| 983 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 984 | |
| 985 | // Check that the page renders correctly. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 986 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 987 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 988 | CompareBitmap(bitmap.get(), 595, 842, md5_original); |
| 989 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 990 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 991 | { |
| 992 | // Create a stamp annotation and set its annotation rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 993 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 994 | ASSERT_TRUE(annot); |
| 995 | FS_RECTF rect; |
| 996 | rect.left = 200.f; |
| 997 | rect.bottom = 550.f; |
| 998 | rect.right = 450.f; |
| 999 | rect.top = 650.f; |
| 1000 | EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1001 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1002 | // Add a translucent text object to the new annotation. |
| 1003 | FPDF_PAGEOBJECT text_object = |
| 1004 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1005 | EXPECT_TRUE(text_object); |
| 1006 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1007 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 1008 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1009 | EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150)); |
| 1010 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 1011 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object)); |
| 1012 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1013 | |
| 1014 | // Check that the page renders correctly with the new text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1015 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1016 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1017 | CompareBitmap(bitmap.get(), 595, 842, md5_new_text); |
| 1018 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1019 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1020 | { |
| 1021 | // Retrieve the newly added stamp annotation and its text object. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1022 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1023 | ASSERT_TRUE(annot); |
| 1024 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get())); |
| 1025 | FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0); |
| 1026 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1027 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1028 | // Modify the text in the new annotation. |
| 1029 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text = |
| 1030 | GetFPDFWideString(L"New text!"); |
| 1031 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 1032 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object)); |
| 1033 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1034 | |
| 1035 | // Check that the page renders correctly with the modified text object. |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1036 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1037 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1038 | CompareBitmap(bitmap.get(), 595, 842, md5_modified_text); |
| 1039 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1040 | |
| 1041 | // Remove the new annotation, and check that the page renders as before. |
| 1042 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1043 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1044 | ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1045 | CompareBitmap(bitmap.get(), 595, 842, md5_original); |
| 1046 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 1047 | |
| 1048 | UnloadPage(page); |
| 1049 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1050 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1051 | TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) { |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1052 | // Open a file with four annotations and load its first page. |
| 1053 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1054 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1055 | ASSERT_TRUE(page); |
| 1056 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1057 | static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1058 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1059 | { |
| 1060 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1061 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1062 | ASSERT_TRUE(annot); |
| 1063 | |
| 1064 | // Check that a non-existent key does not exist. |
| 1065 | EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none")); |
| 1066 | |
| 1067 | // Check that the string value of a non-string dictionary entry is empty. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1068 | EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1069 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1070 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP)); |
| 1071 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP, |
| 1072 | nullptr, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1073 | |
| 1074 | // Check that the string value of the hash is correct. |
| 1075 | static constexpr char kHashKey[] = "AAPL:Hash"; |
| 1076 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
| 1077 | unsigned long len = |
| 1078 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0); |
| 1079 | std::vector<char> buf(len); |
| 1080 | EXPECT_EQ(66u, |
| 1081 | FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len)); |
| 1082 | EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad", |
| 1083 | BufferToWString(buf).c_str()); |
| 1084 | |
| 1085 | // Check that the string value of the modified date is correct. |
| 1086 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey)); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1087 | len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr, |
| 1088 | 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1089 | buf.clear(); |
| 1090 | buf.resize(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1091 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, |
| 1092 | buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1093 | EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str()); |
| 1094 | |
| 1095 | // Update the date entry for the annotation. |
| 1096 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1097 | GetFPDFWideString(kNewDate); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1098 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM, |
| 1099 | text.get())); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1100 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1101 | |
| 1102 | // Save the document, closing the page and document. |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1103 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1104 | UnloadPage(page); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1105 | |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1106 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 1107 | const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c"; |
Lei Zhang | 5e2c51c | 2018-07-27 22:33:34 +0000 | [diff] [blame] | 1108 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 1109 | const char md5[] = "9ee141f698c3fcb56c050dffd6c82624"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1110 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 1111 | const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1112 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1113 | |
| 1114 | // Open the saved annotation. |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1115 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1116 | page = LoadSavedPage(0); |
| 1117 | VerifySavedRendering(page, 595, 842, md5); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1118 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1119 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1120 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1121 | // Check that the string value of the modified date is the newly-set value. |
| 1122 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1123 | FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM)); |
| 1124 | unsigned long len = FPDFAnnot_GetStringValue( |
| 1125 | new_annot.get(), pdfium::annotation::kM, nullptr, 0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1126 | std::vector<char> buf(len); |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1127 | EXPECT_EQ(44u, |
| 1128 | FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM, |
| 1129 | buf.data(), len)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1130 | EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str()); |
| 1131 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1132 | |
Henrique Nakashima | 8baea3c | 2017-11-10 20:27:23 +0000 | [diff] [blame] | 1133 | CloseSavedPage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1134 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 1135 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1136 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1137 | TEST_F(FPDFAnnotEmbedderTest, GetSetAP) { |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1138 | // Open a file with four annotations and load its first page. |
| 1139 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1140 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1141 | ASSERT_TRUE(page); |
| 1142 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1143 | { |
| 1144 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1145 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1146 | ASSERT_TRUE(annot); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1147 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1148 | // Check that the string value of an AP returns the expected length. |
| 1149 | unsigned long normal_len = FPDFAnnot_GetAP( |
| 1150 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0); |
| 1151 | EXPECT_EQ(73970u, normal_len); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1152 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1153 | // Check that the string value of an AP is not returned if the buffer is too |
| 1154 | // small. The result buffer should be overwritten with an empty string. |
| 1155 | std::vector<char> buf(normal_len - 1); |
| 1156 | // Write L"z" in the buffer to verify it's not overwritten. |
| 1157 | wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z"); |
| 1158 | EXPECT_EQ(73970u, |
| 1159 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1160 | buf.data(), buf.size())); |
| 1161 | std::string ap = BufferToString(buf); |
| 1162 | EXPECT_STREQ("z", ap.c_str()); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1163 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1164 | // Check that the string value of an AP is returned through a buffer that is |
| 1165 | // the right size. |
| 1166 | buf.clear(); |
| 1167 | buf.resize(normal_len); |
| 1168 | EXPECT_EQ(73970u, |
| 1169 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1170 | buf.data(), buf.size())); |
| 1171 | ap = BufferToString(buf); |
| 1172 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1173 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1174 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1175 | // Check that the string value of an AP is returned through a buffer that is |
| 1176 | // larger than necessary. |
| 1177 | buf.clear(); |
| 1178 | buf.resize(normal_len + 1); |
| 1179 | EXPECT_EQ(73970u, |
| 1180 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1181 | buf.data(), buf.size())); |
| 1182 | ap = BufferToString(buf); |
| 1183 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1184 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1185 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1186 | // Check that getting an AP for a mode that does not have an AP returns an |
| 1187 | // empty string. |
| 1188 | unsigned long rollover_len = FPDFAnnot_GetAP( |
| 1189 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1190 | EXPECT_EQ(2u, rollover_len); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1191 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1192 | buf.clear(); |
| 1193 | buf.resize(1000); |
| 1194 | EXPECT_EQ(2u, |
| 1195 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1196 | buf.data(), buf.size())); |
| 1197 | EXPECT_STREQ("", BufferToString(buf).c_str()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1198 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1199 | // Check that setting the AP for an invalid appearance mode fails. |
| 1200 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText = |
| 1201 | GetFPDFWideString(L"new test ap"); |
| 1202 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get())); |
| 1203 | EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT, |
| 1204 | apText.get())); |
| 1205 | EXPECT_FALSE(FPDFAnnot_SetAP( |
| 1206 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1207 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1208 | // Set the AP correctly now. |
| 1209 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1210 | apText.get())); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1211 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1212 | // Check that the new annotation value is equal to the value we just set. |
| 1213 | rollover_len = FPDFAnnot_GetAP( |
| 1214 | annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1215 | EXPECT_EQ(24u, rollover_len); |
| 1216 | buf.clear(); |
| 1217 | buf.resize(rollover_len); |
| 1218 | EXPECT_EQ(24u, |
| 1219 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1220 | buf.data(), buf.size())); |
| 1221 | EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1222 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1223 | // Check that the Normal AP was not touched when the Rollover AP was set. |
| 1224 | buf.clear(); |
| 1225 | buf.resize(normal_len); |
| 1226 | EXPECT_EQ(73970u, |
| 1227 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1228 | buf.data(), buf.size())); |
| 1229 | ap = BufferToString(buf); |
| 1230 | EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J")); |
| 1231 | EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q")); |
| 1232 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Save the modified document, then reopen it. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1235 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1236 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1237 | |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1238 | ASSERT_TRUE(OpenSavedDocument()); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1239 | page = LoadSavedPage(0); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1240 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1241 | ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1242 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1243 | // Check that the new annotation value is equal to the value we set before |
| 1244 | // saving. |
| 1245 | unsigned long rollover_len = FPDFAnnot_GetAP( |
| 1246 | new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0); |
| 1247 | EXPECT_EQ(24u, rollover_len); |
| 1248 | std::vector<char> buf(rollover_len); |
| 1249 | EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(), |
| 1250 | FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1251 | buf.data(), buf.size())); |
| 1252 | EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str()); |
| 1253 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Close saved document. |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1256 | CloseSavedPage(page); |
| 1257 | CloseSavedDocument(); |
| 1258 | } |
| 1259 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1260 | TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1261 | // Open a file with four annotations and load its first page. |
| 1262 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1263 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1264 | ASSERT_TRUE(page); |
| 1265 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1266 | { |
| 1267 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1268 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1269 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1270 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1271 | // Set Down AP. Normal AP is already set. |
| 1272 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText = |
| 1273 | GetFPDFWideString(L"new test ap"); |
| 1274 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1275 | apText.get())); |
| 1276 | EXPECT_EQ(73970u, |
| 1277 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1278 | nullptr, 0)); |
| 1279 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1280 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1281 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1282 | // Check that setting the Down AP to null removes the Down entry but keeps |
| 1283 | // Normal intact. |
| 1284 | EXPECT_TRUE( |
| 1285 | FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr)); |
| 1286 | EXPECT_EQ(73970u, |
| 1287 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1288 | nullptr, 0)); |
| 1289 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1290 | nullptr, 0)); |
| 1291 | } |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1292 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1293 | UnloadPage(page); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1296 | TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) { |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1297 | // Open a file with four annotations and load its first page. |
| 1298 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1299 | FPDF_PAGE page = LoadPage(0); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1300 | ASSERT_TRUE(page); |
| 1301 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1302 | { |
| 1303 | // Retrieve the first annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1304 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1305 | ASSERT_TRUE(annot); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1306 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1307 | // Set Down AP. Normal AP is already set. |
| 1308 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText = |
| 1309 | GetFPDFWideString(L"new test ap"); |
| 1310 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1311 | apText.get())); |
| 1312 | EXPECT_EQ(73970u, |
| 1313 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1314 | nullptr, 0)); |
| 1315 | EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1316 | nullptr, 0)); |
Henrique Nakashima | 5970a47 | 2018-01-11 22:40:59 +0000 | [diff] [blame] | 1317 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1318 | // Check that setting the Normal AP to null removes the whole AP dictionary. |
| 1319 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1320 | nullptr)); |
| 1321 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, |
| 1322 | nullptr, 0)); |
| 1323 | EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, |
| 1324 | nullptr, 0)); |
| 1325 | } |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1326 | |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1327 | UnloadPage(page); |
Henrique Nakashima | a74e75d | 2018-01-10 18:06:55 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1330 | TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) { |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1331 | // Open a file with annotations and load its first page. |
| 1332 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1333 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1334 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 1335 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1336 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1337 | { |
| 1338 | // Retrieve the highlight annotation which has its popup defined. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1339 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1340 | ASSERT_TRUE(annot); |
| 1341 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get())); |
| 1342 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get())); |
| 1343 | static constexpr char kPopupKey[] = "Popup"; |
| 1344 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey)); |
| 1345 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, |
| 1346 | FPDFAnnot_GetValueType(annot.get(), kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1347 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1348 | // Retrieve and verify the popup of the highlight annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1349 | ScopedFPDFAnnotation popup( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1350 | FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey)); |
| 1351 | ASSERT_TRUE(popup); |
| 1352 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get())); |
| 1353 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get())); |
| 1354 | FS_RECTF rect; |
| 1355 | ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect)); |
| 1356 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 1357 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1358 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1359 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, |
| 1360 | // since "IRT" is not a key in |annot|'s dictionary. |
| 1361 | static constexpr char kIRTKey[] = "IRT"; |
| 1362 | ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey)); |
| 1363 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1364 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1365 | // Attempting to retrieve |annot|'s parent dictionary as an annotation |
| 1366 | // would fail, since its parent is not an annotation. |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1367 | ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1368 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, |
Lei Zhang | a5c1daf | 2019-01-31 21:56:47 +0000 | [diff] [blame] | 1369 | FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP)); |
| 1370 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1371 | } |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1372 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 1373 | UnloadPage(page); |
| 1374 | } |
| 1375 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1376 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1377 | // Open file with form text fields. |
| 1378 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1379 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1380 | ASSERT_TRUE(page); |
| 1381 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1382 | { |
| 1383 | // Retrieve the first annotation: user-editable text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1384 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1385 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1386 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1387 | // Check that the flag values are as expected. |
| 1388 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1389 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1390 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1391 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1392 | { |
| 1393 | // Retrieve the second annotation: read-only text field. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1394 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1395 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1396 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1397 | // Check that the flag values are as expected. |
| 1398 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1399 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1400 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1401 | |
| 1402 | UnloadPage(page); |
| 1403 | } |
| 1404 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1405 | TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) { |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1406 | // Open file with form text fields. |
| 1407 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1408 | FPDF_PAGE page = LoadPage(0); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1409 | ASSERT_TRUE(page); |
| 1410 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1411 | { |
| 1412 | // Retrieve the first annotation: user-editable combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1413 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1414 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1415 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1416 | // Check that the flag values are as expected. |
| 1417 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1418 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1419 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1420 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1421 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1422 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1423 | { |
| 1424 | // Retrieve the second annotation: regular combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1425 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1426 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1427 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1428 | // Check that the flag values are as expected. |
| 1429 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1430 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1431 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1432 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1433 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1434 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1435 | { |
| 1436 | // Retrieve the third annotation: read-only combobox. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1437 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1438 | ASSERT_TRUE(annot); |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1439 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1440 | // Check that the flag values are as expected. |
| 1441 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1442 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1443 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1444 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1445 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 1446 | |
| 1447 | UnloadPage(page); |
| 1448 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1449 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1450 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1451 | // Open file with form text fields. |
| 1452 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1453 | FPDF_PAGE page = LoadPage(0); |
| 1454 | ASSERT_TRUE(page); |
| 1455 | |
| 1456 | // Attempt to get an annotation where no annotation exists on page. |
Lei Zhang | 7557e7b | 2018-09-14 17:02:40 +0000 | [diff] [blame] | 1457 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0)); |
| 1458 | |
| 1459 | { |
| 1460 | // Verify there is an annotation. |
| 1461 | ScopedFPDFAnnotation annot( |
| 1462 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120)); |
| 1463 | EXPECT_TRUE(annot); |
| 1464 | } |
| 1465 | |
| 1466 | // Try other bad inputs at a valid location. |
| 1467 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120)); |
| 1468 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120)); |
| 1469 | EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120)); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1470 | |
| 1471 | UnloadPage(page); |
| 1472 | } |
| 1473 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1474 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1475 | // Open file with form text fields. |
| 1476 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1477 | FPDF_PAGE page = LoadPage(0); |
| 1478 | ASSERT_TRUE(page); |
| 1479 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1480 | { |
| 1481 | // Retrieve user-editable text field annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1482 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1483 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118)); |
| 1484 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1485 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1486 | // Check that interactive form annotation flag values are as expected. |
| 1487 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1488 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1489 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1490 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1491 | { |
| 1492 | // Retrieve read-only text field annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1493 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1494 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202)); |
| 1495 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1496 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1497 | // Check that interactive form annotation flag values are as expected. |
| 1498 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1499 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1500 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1501 | |
| 1502 | UnloadPage(page); |
| 1503 | } |
| 1504 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 1505 | TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) { |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1506 | // Open file with form comboboxes. |
| 1507 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1508 | FPDF_PAGE page = LoadPage(0); |
| 1509 | ASSERT_TRUE(page); |
| 1510 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1511 | { |
| 1512 | // Retrieve user-editable combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1513 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1514 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1515 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1516 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1517 | // Check that interactive form annotation flag values are as expected. |
| 1518 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1519 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1520 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1521 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1522 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1523 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1524 | { |
| 1525 | // Retrieve regular combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1526 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1527 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1528 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1529 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1530 | // Check that interactive form annotation flag values are as expected. |
| 1531 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1532 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1533 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1534 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1535 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1536 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1537 | { |
| 1538 | // Retrieve read-only combobox annotation. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1539 | ScopedFPDFAnnotation annot( |
Henrique Nakashima | 2083092 | 2018-03-19 21:21:46 +0000 | [diff] [blame] | 1540 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513)); |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1541 | ASSERT_TRUE(annot); |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1542 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1543 | // Check that interactive form annotation flag values are as expected. |
| 1544 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get()); |
| 1545 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1546 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1547 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1548 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1549 | |
| 1550 | UnloadPage(page); |
| 1551 | } |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1552 | |
| 1553 | TEST_F(FPDFAnnotEmbedderTest, BUG_1212) { |
| 1554 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 1555 | FPDF_PAGE page = LoadPage(0); |
| 1556 | ASSERT_TRUE(page); |
| 1557 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 1558 | |
| 1559 | static const char kTestKey[] = "test"; |
| 1560 | static constexpr wchar_t kData[] = L"\xf6\xe4"; |
| 1561 | std::vector<char> buf(12); |
| 1562 | |
| 1563 | { |
| 1564 | // Add a text annotation to the page. |
| 1565 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT)); |
| 1566 | ASSERT_TRUE(annot); |
| 1567 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 1568 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1569 | |
| 1570 | // Make sure there is no test key, add set a value there, and read it back. |
| 1571 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1572 | ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1573 | buf.size())); |
| 1574 | EXPECT_STREQ(L"", BufferToWString(buf).c_str()); |
| 1575 | |
| 1576 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1577 | GetFPDFWideString(kData); |
| 1578 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get())); |
| 1579 | |
| 1580 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1581 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1582 | buf.size())); |
| 1583 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1584 | } |
| 1585 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1586 | { |
| 1587 | ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP)); |
| 1588 | ASSERT_TRUE(annot); |
| 1589 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 1590 | EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get())); |
| 1591 | // Also do the same test for its appearance string. |
| 1592 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1593 | ASSERT_EQ(2u, |
| 1594 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1595 | buf.data(), buf.size())); |
| 1596 | EXPECT_STREQ(L"", BufferToWString(buf).c_str()); |
| 1597 | |
| 1598 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1599 | GetFPDFWideString(kData); |
| 1600 | EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1601 | text.get())); |
| 1602 | |
| 1603 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1604 | ASSERT_EQ(6u, |
| 1605 | FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, |
| 1606 | buf.data(), buf.size())); |
| 1607 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1608 | } |
| 1609 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1610 | UnloadPage(page); |
| 1611 | |
| 1612 | { |
| 1613 | // Save a copy, open the copy, and check the annotation again. |
| 1614 | // Note that it renders the rotation. |
| 1615 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Lei Zhang | 0b49405 | 2019-01-31 21:41:15 +0000 | [diff] [blame] | 1616 | ASSERT_TRUE(OpenSavedDocument()); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1617 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1618 | ASSERT_TRUE(saved_page); |
| 1619 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1620 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page)); |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1621 | { |
| 1622 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1623 | ASSERT_TRUE(annot); |
| 1624 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1625 | |
| 1626 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1627 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1628 | buf.size())); |
| 1629 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1630 | } |
| 1631 | |
Lei Zhang | 05ec64c | 2019-01-09 03:00:06 +0000 | [diff] [blame] | 1632 | { |
| 1633 | ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0)); |
| 1634 | ASSERT_TRUE(annot); |
| 1635 | // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason. |
| 1636 | // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get())); |
| 1637 | |
| 1638 | std::fill(buf.begin(), buf.end(), 'x'); |
| 1639 | ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(), |
| 1640 | buf.size())); |
| 1641 | EXPECT_STREQ(kData, BufferToWString(buf).c_str()); |
| 1642 | } |
| 1643 | |
Lei Zhang | f5fcd9e | 2018-12-23 03:11:50 +0000 | [diff] [blame] | 1644 | CloseSavedPage(saved_page); |
| 1645 | CloseSavedDocument(); |
| 1646 | } |
| 1647 | } |