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 | |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 9 | #include "core/fxcrt/fx_system.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 10 | #include "public/fpdf_annot.h" |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 11 | #include "public/fpdf_edit.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 12 | #include "public/fpdfview.h" |
| 13 | #include "testing/embedder_test.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 16 | static constexpr char kContentsKey[] = "Contents"; |
| 17 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 18 | class FPDFAnnotEmbeddertest : public EmbedderTest {}; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 19 | |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 20 | TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) { |
| 21 | // Open a file with one annotation and load its first page. |
| 22 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
| 23 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 24 | ASSERT_TRUE(page); |
| 25 | |
| 26 | // This annotation has a malformed appearance stream, which does not have its |
| 27 | // normal appearance defined, only its rollover appearance. In this case, its |
| 28 | // normal appearance should be generated, allowing the highlight annotation to |
| 29 | // still display. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 30 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT); |
Jane Liu | e17011d | 2017-06-21 12:18:37 -0400 | [diff] [blame] | 31 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 32 | FPDFBitmap_Destroy(bitmap); |
| 33 | |
| 34 | UnloadPage(page); |
| 35 | } |
| 36 | |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 37 | TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) { |
| 38 | // Open a file with one annotation and load its first page. |
| 39 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 40 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 41 | ASSERT_TRUE(page); |
| 42 | |
| 43 | // Check that there is a total of 1 annotation on its first page. |
| 44 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 45 | |
| 46 | // Check that the annotation is of type "highlight". |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 47 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 48 | ASSERT_TRUE(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 49 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 50 | |
| 51 | // Check that the annotation color is yellow. |
| 52 | unsigned int R; |
| 53 | unsigned int G; |
| 54 | unsigned int B; |
| 55 | unsigned int A; |
| 56 | EXPECT_TRUE( |
| 57 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 58 | EXPECT_EQ(255u, R); |
| 59 | EXPECT_EQ(255u, G); |
| 60 | EXPECT_EQ(0u, B); |
| 61 | EXPECT_EQ(255u, A); |
| 62 | |
| 63 | // Check that the author is correct. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 64 | static constexpr char kAuthorKey[] = "T"; |
| 65 | EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kAuthorKey)); |
| 66 | unsigned long len = FPDFAnnot_GetStringValue(annot, kAuthorKey, nullptr, 0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 67 | std::vector<char> buf(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 68 | EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot, kAuthorKey, buf.data(), len)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 69 | EXPECT_STREQ(L"Jae Hyun Park", |
| 70 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 71 | .c_str()); |
| 72 | |
| 73 | // Check that the content is correct. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 74 | EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kContentsKey)); |
| 75 | len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 76 | buf.clear(); |
| 77 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 78 | EXPECT_EQ(2690u, |
| 79 | FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 80 | const wchar_t contents[] = |
| 81 | L"This is a note for that highlight annotation. Very long highlight " |
| 82 | "annotation. Long long long Long long longLong long longLong long " |
| 83 | "longLong long longLong long longLong long longLong long longLong long " |
| 84 | "longLong long longLong long longLong long longLong long longLong long " |
| 85 | "longLong long longLong long longLong long longLong long longLong long " |
| 86 | "longLong long longLong long longLong long longLong long longLong long " |
| 87 | "longLong long longLong long longLong long longLong long longLong long " |
| 88 | "longLong long longLong long longLong long longLong long longLong long " |
| 89 | "longLong long longLong long longLong long longLong long longLong long " |
| 90 | "longLong long longLong long longLong long longLong long longLong long " |
| 91 | "longLong long longLong long longLong long longLong long longLong long " |
| 92 | "longLong long longLong long longLong long longLong long longLong long " |
| 93 | "longLong long longLong long longLong long longLong long longLong long " |
| 94 | "longLong long longLong long longLong long longLong long longLong long " |
| 95 | "longLong long longLong long longLong long longLong long longLong long " |
| 96 | "longLong long longLong long longLong long longLong long longLong long " |
| 97 | "longLong long longLong long longLong long longLong long longLong long " |
| 98 | "longLong long longLong long longLong long longLong long longLong long " |
| 99 | "longLong long longLong long longLong long longLong long longLong long " |
| 100 | "longLong long long. END"; |
| 101 | EXPECT_STREQ(contents, |
| 102 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 103 | .c_str()); |
| 104 | |
| 105 | // Check that the quadpoints are correct. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 106 | FS_QUADPOINTSF quadpoints; |
| 107 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 108 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 109 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 110 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 111 | EXPECT_EQ(706.264465f, quadpoints.y4); |
| 112 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 113 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 114 | UnloadPage(page); |
| 115 | } |
| 116 | |
| 117 | TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) { |
| 118 | // Open a file with three annotations and load its first page. |
| 119 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 120 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 121 | ASSERT_TRUE(page); |
| 122 | |
| 123 | // Check that there is a total of 3 annotation on its first page. |
| 124 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 125 | |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 126 | // Check that the third annotation is of type "ink". |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 127 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 2); |
| 128 | ASSERT_TRUE(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 129 | EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot)); |
| 130 | |
| 131 | // Check that the annotation color is blue with opacity. |
| 132 | unsigned int R; |
| 133 | unsigned int G; |
| 134 | unsigned int B; |
| 135 | unsigned int A; |
| 136 | EXPECT_TRUE( |
| 137 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 138 | EXPECT_EQ(0u, R); |
| 139 | EXPECT_EQ(0u, G); |
| 140 | EXPECT_EQ(255u, B); |
| 141 | EXPECT_EQ(76u, A); |
| 142 | |
| 143 | // Check that there is no content. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 144 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 145 | |
| 146 | // Check that the rectange coordinates are correct. |
| 147 | // Note that upon rendering, the rectangle coordinates will be adjusted. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 148 | FS_RECTF rect; |
| 149 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 150 | EXPECT_EQ(351.820404f, rect.left); |
| 151 | EXPECT_EQ(583.830688f, rect.bottom); |
| 152 | EXPECT_EQ(475.336090f, rect.right); |
| 153 | EXPECT_EQ(681.535034f, rect.top); |
| 154 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 155 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 156 | UnloadPage(page); |
| 157 | } |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 158 | |
| 159 | TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) { |
| 160 | // Open a file with one annotation and load its first page. |
| 161 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 162 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 163 | ASSERT_TRUE(page); |
| 164 | |
| 165 | // Add an annotation with an illegal subtype. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 166 | ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 167 | |
| 168 | UnloadPage(page); |
| 169 | } |
| 170 | |
Jane Liu | d321ef9 | 2017-06-14 09:56:22 -0400 | [diff] [blame] | 171 | TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) { |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 172 | // Open a file with no annotation and load its first page. |
| 173 | ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| 174 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 175 | ASSERT_TRUE(page); |
| 176 | EXPECT_EQ(0, FPDFPage_GetAnnotCount(page)); |
| 177 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 178 | // Add a text annotation to the page. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 179 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT); |
| 180 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 181 | |
| 182 | // Check that there is now 1 annotations on this page. |
| 183 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
| 184 | |
| 185 | // Check that the subtype of the annotation is correct. |
| 186 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 187 | FPDFPage_CloseAnnot(annot); |
| 188 | |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 189 | annot = FPDFPage_GetAnnot(page, 0); |
| 190 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 191 | EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot)); |
| 192 | |
| 193 | // Set the color of the annotation. |
| 194 | ASSERT_TRUE( |
| 195 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204)); |
| 196 | // Check that the color has been set correctly. |
| 197 | unsigned int R; |
| 198 | unsigned int G; |
| 199 | unsigned int B; |
| 200 | unsigned int A; |
| 201 | EXPECT_TRUE( |
| 202 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 203 | EXPECT_EQ(51u, R); |
| 204 | EXPECT_EQ(102u, G); |
| 205 | EXPECT_EQ(153u, B); |
| 206 | EXPECT_EQ(204u, A); |
| 207 | |
| 208 | // Change the color of the annotation. |
| 209 | ASSERT_TRUE( |
| 210 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51)); |
| 211 | // Check that the color has been set correctly. |
| 212 | EXPECT_TRUE( |
| 213 | FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)); |
| 214 | EXPECT_EQ(204u, R); |
| 215 | EXPECT_EQ(153u, G); |
| 216 | EXPECT_EQ(102u, B); |
| 217 | EXPECT_EQ(51u, A); |
| 218 | |
| 219 | // Set the annotation rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 220 | FS_RECTF rect; |
| 221 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 222 | EXPECT_EQ(0.f, rect.left); |
| 223 | EXPECT_EQ(0.f, rect.right); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 224 | rect.left = 35; |
| 225 | rect.bottom = 150; |
| 226 | rect.right = 53; |
| 227 | rect.top = 165; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 228 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 229 | // Check that the annotation rectangle has been set correctly. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 230 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 231 | EXPECT_EQ(35.f, rect.left); |
| 232 | EXPECT_EQ(150.f, rect.bottom); |
| 233 | EXPECT_EQ(53.f, rect.right); |
| 234 | EXPECT_EQ(165.f, rect.top); |
| 235 | |
| 236 | // Set the content of the annotation. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 237 | static constexpr wchar_t contents[] = L"Hello! This is a customized content."; |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 238 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 239 | GetFPDFWideString(contents); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 240 | ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, kContentsKey, text.get())); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 241 | // Check that the content has been set correctly. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 242 | unsigned long len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 243 | std::vector<char> buf(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 244 | EXPECT_EQ(74u, |
| 245 | FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 246 | EXPECT_STREQ(contents, |
| 247 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 248 | .c_str()); |
| 249 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 250 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 251 | UnloadPage(page); |
| 252 | } |
| 253 | |
| 254 | TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) { |
| 255 | // Open a file with one annotation and load its first page. |
| 256 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 257 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 258 | ASSERT_TRUE(page); |
| 259 | |
| 260 | // Check that there is a total of one annotation on its first page, and verify |
| 261 | // its quadpoints. |
| 262 | EXPECT_EQ(1, FPDFPage_GetAnnotCount(page)); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 263 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 264 | ASSERT_TRUE(annot); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 265 | FS_QUADPOINTSF quadpoints; |
| 266 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 267 | EXPECT_EQ(115.802643f, quadpoints.x1); |
| 268 | EXPECT_EQ(718.913940f, quadpoints.y1); |
| 269 | EXPECT_EQ(157.211182f, quadpoints.x4); |
| 270 | EXPECT_EQ(706.264465f, quadpoints.y4); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 271 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 272 | |
| 273 | // Add an underline annotation to the page and set its quadpoints. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 274 | annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE); |
| 275 | ASSERT_TRUE(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 276 | quadpoints.x1 = 140.802643f; |
| 277 | quadpoints.x3 = 140.802643f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 278 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 279 | FPDFPage_CloseAnnot(annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 280 | |
| 281 | // Save the document, closing the page and document. |
| 282 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 283 | FPDF_ClosePage(page); |
| 284 | |
| 285 | // Open the saved document. |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 286 | const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5"; |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 287 | |
| 288 | OpenSavedDocument(); |
| 289 | LoadSavedPage(); |
| 290 | VerifySavedRendering(612, 792, md5); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 291 | |
| 292 | // Check that the saved document has 2 annotations on the first page |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 293 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(m_SavedPage)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 294 | |
| 295 | // Check that the second annotation is an underline annotation and verify |
| 296 | // its quadpoints. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 297 | FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 1); |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 298 | ASSERT_TRUE(new_annot); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 299 | EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 300 | FS_QUADPOINTSF new_quadpoints; |
| 301 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints)); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 302 | EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f); |
| 303 | EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f); |
| 304 | EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f); |
| 305 | EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f); |
| 306 | |
Jane Liu | e10509a | 2017-06-20 16:47:41 -0400 | [diff] [blame] | 307 | FPDFPage_CloseAnnot(new_annot); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 308 | |
| 309 | CloseSavedPage(); |
| 310 | CloseSavedDocument(); |
Jane Liu | 20eafda | 2017-06-07 10:33:24 -0400 | [diff] [blame] | 311 | } |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 312 | |
| 313 | TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 314 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 315 | const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941"; |
| 316 | const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2"; |
| 317 | const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 318 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 319 | const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 320 | const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 321 | const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 322 | #else |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 323 | const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5"; |
| 324 | const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e"; |
| 325 | const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563"; |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 326 | #endif |
| 327 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 328 | // Open a file with four annotations and load its first page. |
| 329 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 330 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 331 | ASSERT_TRUE(page); |
| 332 | EXPECT_EQ(4, FPDFPage_GetAnnotCount(page)); |
| 333 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 334 | // Check that the original file renders correctly. |
| 335 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 336 | CompareBitmap(bitmap, 612, 792, md5_original); |
| 337 | FPDFBitmap_Destroy(bitmap); |
| 338 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 339 | // Retrieve the highlight annotation which has its AP stream already defined. |
| 340 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 341 | ASSERT_TRUE(annot); |
| 342 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
| 343 | |
| 344 | // Check that color cannot be set when an AP stream is defined already. |
| 345 | EXPECT_FALSE( |
| 346 | FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204)); |
| 347 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 348 | // Verify its attachment points. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 349 | FS_QUADPOINTSF quadpoints; |
| 350 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 351 | EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f); |
| 352 | EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f); |
| 353 | EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f); |
| 354 | EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 355 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 356 | // Check that updating the attachment points would succeed. |
| 357 | quadpoints.x1 -= 50.f; |
| 358 | quadpoints.x2 -= 50.f; |
| 359 | quadpoints.x3 -= 50.f; |
| 360 | quadpoints.x4 -= 50.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 361 | ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 362 | FS_QUADPOINTSF new_quadpoints; |
| 363 | ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 364 | EXPECT_EQ(quadpoints.x1, new_quadpoints.x1); |
| 365 | EXPECT_EQ(quadpoints.y1, new_quadpoints.y1); |
| 366 | EXPECT_EQ(quadpoints.x4, new_quadpoints.x4); |
| 367 | EXPECT_EQ(quadpoints.y4, new_quadpoints.y4); |
| 368 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 369 | // Check that updating quadpoints does not change the annotation's position. |
| 370 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 371 | CompareBitmap(bitmap, 612, 792, md5_original); |
| 372 | FPDFBitmap_Destroy(bitmap); |
| 373 | |
| 374 | // Verify its annotation rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 375 | FS_RECTF rect; |
| 376 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 377 | EXPECT_NEAR(67.7299f, rect.left, 0.001f); |
| 378 | EXPECT_NEAR(704.296f, rect.bottom, 0.001f); |
| 379 | EXPECT_NEAR(136.325f, rect.right, 0.001f); |
| 380 | EXPECT_NEAR(721.292f, rect.top, 0.001f); |
| 381 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 382 | // Check that updating the rectangle would succeed. |
| 383 | rect.left -= 60.f; |
| 384 | rect.right -= 60.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 385 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 386 | FS_RECTF new_rect; |
| 387 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 388 | EXPECT_EQ(rect.right, new_rect.right); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 389 | FPDFPage_CloseAnnot(annot); |
| 390 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 391 | // Check that updating the rectangle changes the annotation's position. |
| 392 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 393 | CompareBitmap(bitmap, 612, 792, md5_modified_highlight); |
| 394 | FPDFBitmap_Destroy(bitmap); |
| 395 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 396 | // Retrieve the square annotation which has its AP stream already defined. |
| 397 | annot = FPDFPage_GetAnnot(page, 2); |
| 398 | ASSERT_TRUE(annot); |
| 399 | EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot)); |
| 400 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 401 | // Check that updating the rectangle would succeed. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 402 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 403 | rect.left += 70.f; |
| 404 | rect.right += 70.f; |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 405 | ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 406 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 407 | EXPECT_EQ(rect.right, new_rect.right); |
| 408 | |
Jane Liu | b370e5a | 2017-08-16 13:24:58 -0400 | [diff] [blame] | 409 | // Check that updating the rectangle changes the square annotation's position. |
| 410 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 411 | CompareBitmap(bitmap, 612, 792, md5_modified_square); |
| 412 | FPDFBitmap_Destroy(bitmap); |
| 413 | |
Jane Liu | 0646275 | 2017-06-27 16:41:14 -0400 | [diff] [blame] | 414 | FPDFPage_CloseAnnot(annot); |
| 415 | UnloadPage(page); |
| 416 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 417 | |
| 418 | TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) { |
| 419 | // Open a file with 3 annotations on its first page. |
| 420 | ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); |
| 421 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 422 | ASSERT_TRUE(page); |
| 423 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 424 | |
| 425 | // Check that the annotations have the expected rectangle coordinates. |
| 426 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 427 | FS_RECTF rect; |
| 428 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 429 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 430 | FPDFPage_CloseAnnot(annot); |
| 431 | |
| 432 | annot = FPDFPage_GetAnnot(page, 1); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 433 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 434 | EXPECT_NEAR(149.8127f, rect.left, 0.001f); |
| 435 | FPDFPage_CloseAnnot(annot); |
| 436 | |
| 437 | annot = FPDFPage_GetAnnot(page, 2); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 438 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 439 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 440 | FPDFPage_CloseAnnot(annot); |
| 441 | |
| 442 | // Check that nothing happens when attempting to remove an annotation with an |
| 443 | // out-of-bound index. |
| 444 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); |
| 445 | EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); |
| 446 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); |
| 447 | |
| 448 | // Remove the second annotation. |
| 449 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); |
| 450 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 451 | EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); |
| 452 | |
| 453 | // Save the document, closing the page and document. |
| 454 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 455 | FPDF_ClosePage(page); |
| 456 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 457 | // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1?? |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 458 | // Open the saved document. |
| 459 | std::string new_file = GetString(); |
| 460 | FPDF_FILEACCESS file_access; |
| 461 | memset(&file_access, 0, sizeof(file_access)); |
| 462 | file_access.m_FileLen = new_file.size(); |
| 463 | file_access.m_GetBlock = GetBlockFromString; |
| 464 | file_access.m_Param = &new_file; |
| 465 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 466 | ASSERT_TRUE(new_doc); |
| 467 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 468 | ASSERT_TRUE(new_page); |
| 469 | |
| 470 | // Check that the saved document has 2 annotations on the first page. |
| 471 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); |
| 472 | |
| 473 | // Check that the remaining 2 annotations are the original 1st and 3rd ones by |
| 474 | // verifying their rectangle coordinates. |
| 475 | annot = FPDFPage_GetAnnot(new_page, 0); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 476 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 477 | EXPECT_NEAR(86.1971f, rect.left, 0.001f); |
| 478 | FPDFPage_CloseAnnot(annot); |
| 479 | |
| 480 | annot = FPDFPage_GetAnnot(new_page, 1); |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 481 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect)); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 482 | EXPECT_NEAR(351.8204f, rect.left, 0.001f); |
| 483 | FPDFPage_CloseAnnot(annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 484 | FPDF_ClosePage(new_page); |
| 485 | FPDF_CloseDocument(new_doc); |
| 486 | } |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 487 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 488 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 489 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 490 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 491 | const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8"; |
| 492 | const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff"; |
| 493 | const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 494 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 495 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 496 | const char md5_modified_path[] = "3f77b88ce6048e08e636c9a03921b2e5"; |
| 497 | const char md5_two_paths[] = "bffbf5ecd15862b9fe553c795400ff8e"; |
| 498 | const char md5_new_annot[] = "e020534c7eeea76be537c70d6e359a40"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 499 | #else |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 500 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 501 | const char md5_modified_path[] = "3f77b88ce6048e08e636c9a03921b2e5"; |
| 502 | const char md5_two_paths[] = "bffbf5ecd15862b9fe553c795400ff8e"; |
| 503 | const char md5_new_annot[] = "e020534c7eeea76be537c70d6e359a40"; |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 504 | #endif |
| 505 | |
| 506 | // Open a file with two annotations and load its first page. |
| 507 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 508 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 509 | ASSERT_TRUE(page); |
| 510 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 511 | |
| 512 | // Check that the page renders correctly. |
| 513 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 514 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 515 | FPDFBitmap_Destroy(bitmap); |
| 516 | |
| 517 | // Retrieve the stamp annotation which has its AP stream already defined. |
| 518 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 519 | ASSERT_TRUE(annot); |
| 520 | |
| 521 | // Check that this annotation has one path object and retrieve it. |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 522 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 523 | FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot, 1); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 524 | EXPECT_FALSE(path); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 525 | path = FPDFAnnot_GetObject(annot, 0); |
| 526 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 527 | EXPECT_TRUE(path); |
| 528 | |
| 529 | // Modify the color of the path object. |
| 530 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 531 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, path)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 532 | |
| 533 | // Check that the page with the modified annotation renders correctly. |
| 534 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 535 | CompareBitmap(bitmap, 595, 842, md5_modified_path); |
| 536 | FPDFBitmap_Destroy(bitmap); |
| 537 | |
| 538 | // Add a second path object to the same annotation. |
| 539 | FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84); |
| 540 | EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88)); |
| 541 | EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100)); |
| 542 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14)); |
| 543 | EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1)); |
| 544 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, dot)); |
| 545 | EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot)); |
| 546 | |
| 547 | // Check that the page with an annotation with two paths renders correctly. |
| 548 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 549 | CompareBitmap(bitmap, 595, 842, md5_two_paths); |
| 550 | FPDFBitmap_Destroy(bitmap); |
| 551 | |
| 552 | // Delete the newly added path object. |
| 553 | EXPECT_TRUE(FPDFAnnot_RemoveObject(annot, 1)); |
| 554 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 555 | FPDFPage_CloseAnnot(annot); |
| 556 | |
| 557 | // Check that the page renders the same as before. |
| 558 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 559 | CompareBitmap(bitmap, 595, 842, md5_modified_path); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 560 | FPDFBitmap_Destroy(bitmap); |
| 561 | |
| 562 | // Create another stamp annotation and set its annotation rectangle. |
| 563 | annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 564 | ASSERT_TRUE(annot); |
| 565 | FS_RECTF rect; |
| 566 | rect.left = 200.f; |
| 567 | rect.bottom = 400.f; |
| 568 | rect.right = 500.f; |
| 569 | rect.top = 600.f; |
| 570 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 571 | |
| 572 | // Add a new path to the annotation. |
| 573 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500); |
| 574 | EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400)); |
| 575 | EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600)); |
| 576 | EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550)); |
| 577 | EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450)); |
| 578 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180)); |
| 579 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 580 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 581 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, check)); |
| 582 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 583 | |
| 584 | // Check that the annotation's bounding box came from its rectangle. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 585 | FS_RECTF new_rect; |
| 586 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 587 | EXPECT_EQ(rect.left, new_rect.left); |
| 588 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 589 | EXPECT_EQ(rect.right, new_rect.right); |
| 590 | EXPECT_EQ(rect.top, new_rect.top); |
| 591 | |
| 592 | // Save the document, closing the page and document. |
| 593 | FPDFPage_CloseAnnot(annot); |
| 594 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 595 | FPDF_ClosePage(page); |
| 596 | |
| 597 | // Open the saved document. |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 598 | OpenSavedDocument(); |
| 599 | LoadSavedPage(); |
| 600 | VerifySavedRendering(595, 842, md5_new_annot); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 601 | |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 602 | // Check that the document has a correct count of annotations and objects. |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 603 | EXPECT_EQ(3, FPDFPage_GetAnnotCount(m_SavedPage)); |
| 604 | annot = FPDFPage_GetAnnot(m_SavedPage, 2); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 605 | ASSERT_TRUE(annot); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 606 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 607 | |
| 608 | // Check that the new annotation's rectangle is as defined. |
Jane Liu | 0c6b07d | 2017-08-15 10:50:22 -0400 | [diff] [blame] | 609 | ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect)); |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 610 | EXPECT_EQ(rect.left, new_rect.left); |
| 611 | EXPECT_EQ(rect.bottom, new_rect.bottom); |
| 612 | EXPECT_EQ(rect.right, new_rect.right); |
| 613 | EXPECT_EQ(rect.top, new_rect.top); |
| 614 | |
Jane Liu | baa7ff4 | 2017-06-29 19:18:23 -0400 | [diff] [blame] | 615 | FPDFPage_CloseAnnot(annot); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 616 | CloseSavedPage(); |
| 617 | CloseSavedDocument(); |
Jane Liu | 8ce58f5 | 2017-06-29 13:40:22 -0400 | [diff] [blame] | 618 | } |
Jane Liu | b137e75 | 2017-07-05 15:04:33 -0400 | [diff] [blame] | 619 | |
| 620 | TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) { |
| 621 | // Open a file with an annotation and load its first page. |
| 622 | ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); |
| 623 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 624 | ASSERT_TRUE(page); |
| 625 | |
| 626 | // Check that the page renders correctly. |
| 627 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 628 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 629 | FPDFBitmap_Destroy(bitmap); |
| 630 | |
| 631 | // Retrieve the annotation. |
| 632 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 633 | ASSERT_TRUE(annot); |
| 634 | |
| 635 | // Check that the original flag values are as expected. |
| 636 | int flags = FPDFAnnot_GetFlags(annot); |
| 637 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 638 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 639 | |
| 640 | // Set the HIDDEN flag. |
| 641 | flags |= FPDF_ANNOT_FLAG_HIDDEN; |
| 642 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); |
| 643 | flags = FPDFAnnot_GetFlags(annot); |
| 644 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 645 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 646 | |
| 647 | // Check that the page renders correctly without rendering the annotation. |
| 648 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 649 | CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); |
| 650 | FPDFBitmap_Destroy(bitmap); |
| 651 | |
| 652 | // Unset the HIDDEN flag. |
| 653 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, FPDF_ANNOT_FLAG_NONE)); |
| 654 | EXPECT_FALSE(FPDFAnnot_GetFlags(annot)); |
| 655 | flags &= ~FPDF_ANNOT_FLAG_HIDDEN; |
| 656 | EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); |
| 657 | flags = FPDFAnnot_GetFlags(annot); |
| 658 | EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); |
| 659 | EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); |
| 660 | |
| 661 | // Check that the page renders correctly as before. |
| 662 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
| 663 | CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); |
| 664 | FPDFBitmap_Destroy(bitmap); |
| 665 | |
| 666 | FPDFPage_CloseAnnot(annot); |
| 667 | UnloadPage(page); |
| 668 | } |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 669 | |
| 670 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 671 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 672 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 673 | const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596"; |
| 674 | const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 675 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 676 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 677 | const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208"; |
| 678 | const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 679 | #else |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 680 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 681 | const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208"; |
| 682 | const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 683 | #endif |
| 684 | |
| 685 | // Open a file with two annotations and load its first page. |
| 686 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 687 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 688 | ASSERT_TRUE(page); |
| 689 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 690 | |
| 691 | // Check that the page renders correctly. |
| 692 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 693 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 694 | FPDFBitmap_Destroy(bitmap); |
| 695 | |
| 696 | // Create a stamp annotation and set its annotation rectangle. |
| 697 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 698 | ASSERT_TRUE(annot); |
| 699 | FS_RECTF rect; |
| 700 | rect.left = 200.f; |
| 701 | rect.bottom = 600.f; |
| 702 | rect.right = 400.f; |
| 703 | rect.top = 800.f; |
| 704 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 705 | |
| 706 | // Add a solid-color translucent image object to the new annotation. |
| 707 | constexpr int kBitmapSize = 200; |
| 708 | FPDF_BITMAP image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1); |
| 709 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xeeeecccc); |
| 710 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap)); |
| 711 | EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap)); |
| 712 | FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document()); |
| 713 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 714 | ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0, |
| 715 | kBitmapSize, 0, 0)); |
| 716 | FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600); |
| 717 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, image_object)); |
| 718 | FPDFPage_CloseAnnot(annot); |
| 719 | |
| 720 | // Check that the page renders correctly with the new image object. |
| 721 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 722 | CompareBitmap(bitmap, 595, 842, md5_new_image); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 723 | FPDFBitmap_Destroy(bitmap); |
| 724 | |
| 725 | // Retrieve the newly added stamp annotation and its image object. |
| 726 | annot = FPDFPage_GetAnnot(page, 2); |
| 727 | ASSERT_TRUE(annot); |
| 728 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 729 | image_object = FPDFAnnot_GetObject(annot, 0); |
| 730 | EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object)); |
| 731 | |
| 732 | // Modify the image in the new annotation. |
| 733 | FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xff000000); |
| 734 | ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap)); |
| 735 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, image_object)); |
| 736 | FPDFPage_CloseAnnot(annot); |
| 737 | |
| 738 | // Save the document, closing the page and document. |
| 739 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 740 | FPDF_ClosePage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 741 | FPDFBitmap_Destroy(image_bitmap); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 742 | |
| 743 | // Test that the saved document renders the modified image object correctly. |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 744 | VerifySavedDocument(595, 842, md5_modified_image); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) { |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 748 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 749 | const char md5_original[] = "c35408717759562d1f8bf33d317483d2"; |
| 750 | const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286"; |
| 751 | const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 752 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 753 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 754 | const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5"; |
| 755 | const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 756 | #else |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 757 | const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e"; |
| 758 | const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5"; |
| 759 | const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0"; |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 760 | #endif |
| 761 | |
| 762 | // Open a file with two annotations and load its first page. |
| 763 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 764 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 765 | ASSERT_TRUE(page); |
| 766 | EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); |
| 767 | |
| 768 | // Check that the page renders correctly. |
| 769 | FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 770 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 771 | FPDFBitmap_Destroy(bitmap); |
| 772 | |
| 773 | // Create a stamp annotation and set its annotation rectangle. |
| 774 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP); |
| 775 | ASSERT_TRUE(annot); |
| 776 | FS_RECTF rect; |
| 777 | rect.left = 200.f; |
| 778 | rect.bottom = 550.f; |
| 779 | rect.right = 450.f; |
| 780 | rect.top = 650.f; |
| 781 | EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect)); |
| 782 | |
| 783 | // Add a translucent text object to the new annotation. |
| 784 | FPDF_PAGEOBJECT text_object = |
| 785 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 786 | EXPECT_TRUE(text_object); |
| 787 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 788 | GetFPDFWideString(L"I'm a translucent text laying on other text."); |
| 789 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 790 | EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150)); |
| 791 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600); |
| 792 | EXPECT_TRUE(FPDFAnnot_AppendObject(annot, text_object)); |
| 793 | FPDFPage_CloseAnnot(annot); |
| 794 | |
| 795 | // Check that the page renders correctly with the new text object. |
| 796 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 797 | CompareBitmap(bitmap, 595, 842, md5_new_text); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 798 | FPDFBitmap_Destroy(bitmap); |
| 799 | |
| 800 | // Retrieve the newly added stamp annotation and its text object. |
| 801 | annot = FPDFPage_GetAnnot(page, 2); |
| 802 | ASSERT_TRUE(annot); |
| 803 | EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot)); |
| 804 | text_object = FPDFAnnot_GetObject(annot, 0); |
| 805 | EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object)); |
| 806 | |
| 807 | // Modify the text in the new annotation. |
| 808 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text = |
| 809 | GetFPDFWideString(L"New text!"); |
| 810 | EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get())); |
| 811 | EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, text_object)); |
| 812 | FPDFPage_CloseAnnot(annot); |
| 813 | |
| 814 | // Check that the page renders correctly with the modified text object. |
| 815 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 816 | CompareBitmap(bitmap, 595, 842, md5_modified_text); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 817 | FPDFBitmap_Destroy(bitmap); |
| 818 | |
| 819 | // Remove the new annotation, and check that the page renders as before. |
| 820 | EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2)); |
| 821 | bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); |
Jane Liu | 7a9a38b | 2017-07-11 13:47:37 -0400 | [diff] [blame] | 822 | CompareBitmap(bitmap, 595, 842, md5_original); |
Jane Liu | 3656774 | 2017-07-06 11:13:35 -0400 | [diff] [blame] | 823 | FPDFBitmap_Destroy(bitmap); |
| 824 | |
| 825 | UnloadPage(page); |
| 826 | } |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 827 | |
| 828 | TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) { |
| 829 | // Open a file with four annotations and load its first page. |
| 830 | ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf")); |
| 831 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 832 | ASSERT_TRUE(page); |
| 833 | |
| 834 | // Retrieve the first annotation. |
| 835 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 836 | ASSERT_TRUE(annot); |
| 837 | |
| 838 | // Check that a non-existent key does not exist. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 839 | EXPECT_FALSE(FPDFAnnot_HasKey(annot, "none")); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 840 | |
| 841 | // Check that the string value of a non-string dictionary entry is empty. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 842 | static constexpr char kApKey[] = "AP"; |
| 843 | EXPECT_TRUE(FPDFAnnot_HasKey(annot, kApKey)); |
| 844 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kApKey)); |
| 845 | EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kApKey, nullptr, 0)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 846 | |
| 847 | // Check that the string value of the hash is correct. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 848 | static constexpr char kHashKey[] = "AAPL:Hash"; |
| 849 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey)); |
| 850 | unsigned long len = FPDFAnnot_GetStringValue(annot, kHashKey, nullptr, 0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 851 | std::vector<char> buf(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 852 | EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot, kHashKey, buf.data(), len)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 853 | EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad", |
| 854 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 855 | .c_str()); |
| 856 | |
| 857 | // Check that the string value of the modified date is correct. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 858 | static constexpr char kDateKey[] = "M"; |
| 859 | EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey)); |
| 860 | len = FPDFAnnot_GetStringValue(annot, kDateKey, nullptr, 0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 861 | buf.clear(); |
| 862 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 863 | EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot, kDateKey, buf.data(), len)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 864 | EXPECT_STREQ(L"D:201706071721Z00'00'", |
| 865 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 866 | .c_str()); |
| 867 | |
| 868 | // Update the date entry for the annotation. |
| 869 | const wchar_t new_date[] = L"D:201706282359Z00'00'"; |
| 870 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 871 | GetFPDFWideString(new_date); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 872 | EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, kDateKey, text.get())); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 873 | |
| 874 | // Save the document, closing the page and document. |
| 875 | FPDFPage_CloseAnnot(annot); |
| 876 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 877 | FPDF_ClosePage(page); |
| 878 | |
| 879 | // Open the saved annotation. |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 880 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Artem Strygin | d24b97e | 2017-08-09 18:50:59 +0300 | [diff] [blame] | 881 | const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 882 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 883 | const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 884 | #else |
Henrique Nakashima | 53d443f | 2017-10-26 11:12:09 -0400 | [diff] [blame^] | 885 | const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01"; |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 886 | #endif |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 887 | OpenSavedDocument(); |
| 888 | LoadSavedPage(); |
| 889 | VerifySavedRendering(595, 842, md5); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 890 | FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 0); |
| 891 | |
| 892 | // Check that the string value of the modified date is the newly-set value. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 893 | EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(new_annot, kDateKey)); |
| 894 | len = FPDFAnnot_GetStringValue(new_annot, kDateKey, nullptr, 0); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 895 | buf.clear(); |
| 896 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 897 | EXPECT_EQ(44u, |
| 898 | FPDFAnnot_GetStringValue(new_annot, kDateKey, buf.data(), len)); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 899 | EXPECT_STREQ(new_date, |
| 900 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 901 | .c_str()); |
| 902 | |
| 903 | FPDFPage_CloseAnnot(new_annot); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 904 | CloseSavedPage(); |
| 905 | CloseSavedDocument(); |
Jane Liu | 2e1a32b | 2017-07-06 12:01:25 -0400 | [diff] [blame] | 906 | } |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 907 | |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 908 | TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) { |
| 909 | // Open a file with annotations and load its first page. |
| 910 | ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf")); |
| 911 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 912 | ASSERT_TRUE(page); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 913 | EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 914 | |
| 915 | // Retrieve the highlight annotation which has its popup defined. |
| 916 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 917 | ASSERT_TRUE(annot); |
| 918 | EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 919 | EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot)); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 920 | static constexpr char kPopupKey[] = "Popup"; |
| 921 | ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPopupKey)); |
| 922 | ASSERT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPopupKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 923 | |
| 924 | // Retrieve and verify the popup of the highlight annotation. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 925 | FPDF_ANNOTATION popup = FPDFAnnot_GetLinkedAnnot(annot, kPopupKey); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 926 | ASSERT_TRUE(popup); |
| 927 | EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup)); |
Jane Liu | d1ed1ce | 2017-08-24 12:31:10 -0400 | [diff] [blame] | 928 | EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 929 | FS_RECTF rect; |
| 930 | ASSERT_TRUE(FPDFAnnot_GetRect(popup, &rect)); |
| 931 | EXPECT_NEAR(612.0f, rect.left, 0.001f); |
| 932 | EXPECT_NEAR(578.792, rect.bottom, 0.001f); |
| 933 | |
| 934 | // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, since |
| 935 | // "IRT" is not a key in |annot|'s dictionary. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 936 | static constexpr char kIRTKey[] = "IRT"; |
| 937 | ASSERT_FALSE(FPDFAnnot_HasKey(annot, kIRTKey)); |
| 938 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kIRTKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 939 | |
| 940 | // Attempting to retrieve |annot|'s parent dictionary as an annotation would |
| 941 | // fail, since its parent is not an annotation. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 942 | static constexpr char kPKey[] = "P"; |
| 943 | ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPKey)); |
| 944 | EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPKey)); |
| 945 | EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kPKey)); |
Jane Liu | 300bb27 | 2017-08-21 14:37:53 -0400 | [diff] [blame] | 946 | |
| 947 | FPDFPage_CloseAnnot(popup); |
| 948 | FPDFPage_CloseAnnot(annot); |
| 949 | UnloadPage(page); |
| 950 | } |
| 951 | |
Diana Gage | 7e0c05d | 2017-07-19 17:33:33 -0700 | [diff] [blame] | 952 | TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) { |
| 953 | // Open file with form text fields. |
| 954 | ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 955 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 956 | ASSERT_TRUE(page); |
| 957 | |
| 958 | // Retrieve the first annotation: user-editable text field. |
| 959 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 960 | ASSERT_TRUE(annot); |
| 961 | |
| 962 | // Check that the flag values are as expected. |
| 963 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 964 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 965 | FPDFPage_CloseAnnot(annot); |
| 966 | |
| 967 | // Retrieve the second annotation: read-only text field. |
| 968 | annot = FPDFPage_GetAnnot(page, 1); |
| 969 | ASSERT_TRUE(annot); |
| 970 | |
| 971 | // Check that the flag values are as expected. |
| 972 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 973 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 974 | FPDFPage_CloseAnnot(annot); |
| 975 | |
| 976 | UnloadPage(page); |
| 977 | } |
| 978 | |
| 979 | TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) { |
| 980 | // Open file with form text fields. |
| 981 | ASSERT_TRUE(OpenDocument("combobox_form.pdf")); |
| 982 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 983 | ASSERT_TRUE(page); |
| 984 | |
| 985 | // Retrieve the first annotation: user-editable combobox. |
| 986 | FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); |
| 987 | ASSERT_TRUE(annot); |
| 988 | |
| 989 | // Check that the flag values are as expected. |
| 990 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 991 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 992 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 993 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 994 | FPDFPage_CloseAnnot(annot); |
| 995 | |
| 996 | // Retrieve the second annotation: regular combobox. |
| 997 | annot = FPDFPage_GetAnnot(page, 1); |
| 998 | ASSERT_TRUE(annot); |
| 999 | |
| 1000 | // Check that the flag values are as expected. |
| 1001 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1002 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1003 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1004 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1005 | FPDFPage_CloseAnnot(annot); |
| 1006 | |
| 1007 | // Retrieve the third annotation: read-only combobox. |
| 1008 | annot = FPDFPage_GetAnnot(page, 2); |
| 1009 | ASSERT_TRUE(annot); |
| 1010 | |
| 1011 | // Check that the flag values are as expected. |
| 1012 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1013 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1014 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1015 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1016 | FPDFPage_CloseAnnot(annot); |
| 1017 | |
| 1018 | UnloadPage(page); |
| 1019 | } |
Diana Gage | 40870db | 2017-07-19 18:16:03 -0700 | [diff] [blame] | 1020 | |
| 1021 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) { |
| 1022 | // Open file with form text fields. |
| 1023 | EXPECT_TRUE(OpenDocument("text_form.pdf")); |
| 1024 | FPDF_PAGE page = LoadPage(0); |
| 1025 | ASSERT_TRUE(page); |
| 1026 | |
| 1027 | // Attempt to get an annotation where no annotation exists on page. |
| 1028 | FPDF_ANNOTATION annot = |
| 1029 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0); |
| 1030 | EXPECT_FALSE(annot); |
| 1031 | |
| 1032 | UnloadPage(page); |
| 1033 | } |
| 1034 | |
| 1035 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) { |
| 1036 | // Open file with form text fields. |
| 1037 | EXPECT_TRUE(OpenDocument("text_form_multiple.pdf")); |
| 1038 | FPDF_PAGE page = LoadPage(0); |
| 1039 | ASSERT_TRUE(page); |
| 1040 | |
| 1041 | // Retrieve user-editable text field annotation. |
| 1042 | FPDF_ANNOTATION annot = |
| 1043 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118); |
| 1044 | ASSERT_TRUE(annot); |
| 1045 | |
| 1046 | // Check that interactive form annotation flag values are as expected. |
| 1047 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1048 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1049 | FPDFPage_CloseAnnot(annot); |
| 1050 | |
| 1051 | // Retrieve read-only text field annotation. |
| 1052 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202); |
| 1053 | ASSERT_TRUE(annot); |
| 1054 | |
| 1055 | // Check that interactive form annotation flag values are as expected. |
| 1056 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1057 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1058 | FPDFPage_CloseAnnot(annot); |
| 1059 | |
| 1060 | UnloadPage(page); |
| 1061 | } |
| 1062 | |
| 1063 | TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) { |
| 1064 | // Open file with form comboboxes. |
| 1065 | EXPECT_TRUE(OpenDocument("combobox_form.pdf")); |
| 1066 | FPDF_PAGE page = LoadPage(0); |
| 1067 | ASSERT_TRUE(page); |
| 1068 | |
| 1069 | // Retrieve user-editable combobox annotation. |
| 1070 | FPDF_ANNOTATION annot = |
| 1071 | FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63); |
| 1072 | ASSERT_TRUE(annot); |
| 1073 | |
| 1074 | // Check that interactive form annotation flag values are as expected. |
| 1075 | int flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1076 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1077 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1078 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1079 | FPDFPage_CloseAnnot(annot); |
| 1080 | |
| 1081 | // Retrieve regular combobox annotation. |
| 1082 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113); |
| 1083 | ASSERT_TRUE(annot); |
| 1084 | |
| 1085 | // Check that interactive form annotation flag values are as expected. |
| 1086 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1087 | EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY); |
| 1088 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1089 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1090 | FPDFPage_CloseAnnot(annot); |
| 1091 | |
| 1092 | // Retrieve read-only combobox annotation. |
| 1093 | annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213); |
| 1094 | ASSERT_TRUE(annot); |
| 1095 | |
| 1096 | // Check that interactive form annotation flag values are as expected. |
| 1097 | flags = FPDFAnnot_GetFormFieldFlags(page, annot); |
| 1098 | EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY); |
| 1099 | EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO); |
| 1100 | EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT); |
| 1101 | FPDFPage_CloseAnnot(annot); |
| 1102 | |
| 1103 | UnloadPage(page); |
| 1104 | } |