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