blob: 5b5c29717bdfd6cc2c3ac509e92f2646534f98d7 [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// 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
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Lei Zhanga21d5932018-02-05 18:28:38 +000011#include "public/cpp/fpdf_deleters.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Jane Liue17011d2017-06-21 12:18:37 -040031TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
32 // Open a file with one annotation and load its first page.
33 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
34 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
35 ASSERT_TRUE(page);
36
37 // This annotation has a malformed appearance stream, which does not have its
38 // normal appearance defined, only its rollover appearance. In this case, its
39 // normal appearance should be generated, allowing the highlight annotation to
40 // still display.
Lei Zhanga98e3662018-02-07 20:28:35 +000041 std::unique_ptr<void, FPDFBitmapDeleter> bitmap =
42 RenderLoadedPageWithFlags(page, FPDF_ANNOT);
43 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040044
45 UnloadPage(page);
46}
47
Jane Liu4fd9a472017-06-01 18:56:09 -040048TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
49 // Open a file with one annotation and load its first page.
50 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
51 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
52 ASSERT_TRUE(page);
53
54 // Check that there is a total of 1 annotation on its first page.
55 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
56
57 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000058 {
59 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
60 FPDFPage_GetAnnot(page, 0));
61 ASSERT_TRUE(annot);
62 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040063
Lei Zhanga21d5932018-02-05 18:28:38 +000064 // Check that the annotation color is yellow.
65 unsigned int R;
66 unsigned int G;
67 unsigned int B;
68 unsigned int A;
69 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
70 &G, &B, &A));
71 EXPECT_EQ(255u, R);
72 EXPECT_EQ(255u, G);
73 EXPECT_EQ(0u, B);
74 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040075
Lei Zhanga21d5932018-02-05 18:28:38 +000076 // Check that the author is correct.
77 static constexpr char kAuthorKey[] = "T";
78 EXPECT_EQ(FPDF_OBJECT_STRING,
79 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
80 unsigned long len =
81 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
82 std::vector<char> buf(len);
83 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
84 len));
85 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -040086
Lei Zhanga21d5932018-02-05 18:28:38 +000087 // Check that the content is correct.
88 EXPECT_EQ(FPDF_OBJECT_STRING,
89 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
90 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
91 buf.clear();
92 buf.resize(len);
93 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
94 buf.data(), len));
95 const wchar_t contents[] =
96 L"This is a note for that highlight annotation. Very long highlight "
97 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
101 "longLong long longLong long longLong long longLong long longLong long "
102 "longLong long longLong long longLong long longLong long longLong long "
103 "longLong long longLong long longLong long longLong long longLong long "
104 "longLong long longLong long longLong long longLong long longLong long "
105 "longLong long longLong long longLong long longLong long longLong long "
106 "longLong long longLong long longLong long longLong long longLong long "
107 "longLong long longLong long longLong long longLong long longLong long "
108 "longLong long longLong long longLong long longLong long longLong long "
109 "longLong long longLong long longLong long longLong long longLong long "
110 "longLong long longLong long longLong long longLong long longLong long "
111 "longLong long longLong long longLong long longLong long longLong long "
112 "longLong long longLong long longLong long longLong long longLong long "
113 "longLong long longLong long longLong long longLong long longLong long "
114 "longLong long longLong long longLong long longLong long longLong long "
115 "longLong long long. END";
116 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400117
Lei Zhanga21d5932018-02-05 18:28:38 +0000118 // Check that the quadpoints are correct.
119 FS_QUADPOINTSF quadpoints;
120 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
121 EXPECT_EQ(115.802643f, quadpoints.x1);
122 EXPECT_EQ(718.913940f, quadpoints.y1);
123 EXPECT_EQ(157.211182f, quadpoints.x4);
124 EXPECT_EQ(706.264465f, quadpoints.y4);
125 }
Jane Liu4fd9a472017-06-01 18:56:09 -0400126 UnloadPage(page);
127}
128
129TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
130 // Open a file with three annotations and load its first page.
131 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
132 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
133 ASSERT_TRUE(page);
134
135 // Check that there is a total of 3 annotation on its first page.
136 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
137
Lei Zhanga21d5932018-02-05 18:28:38 +0000138 {
139 // Check that the third annotation is of type "ink".
140 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
141 FPDFPage_GetAnnot(page, 2));
142 ASSERT_TRUE(annot);
143 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400144
Lei Zhanga21d5932018-02-05 18:28:38 +0000145 // Check that the annotation color is blue with opacity.
146 unsigned int R;
147 unsigned int G;
148 unsigned int B;
149 unsigned int A;
150 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
151 &G, &B, &A));
152 EXPECT_EQ(0u, R);
153 EXPECT_EQ(0u, G);
154 EXPECT_EQ(255u, B);
155 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400156
Lei Zhanga21d5932018-02-05 18:28:38 +0000157 // Check that there is no content.
158 EXPECT_EQ(2u,
159 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400160
Lei Zhanga21d5932018-02-05 18:28:38 +0000161 // Check that the rectange coordinates are correct.
162 // Note that upon rendering, the rectangle coordinates will be adjusted.
163 FS_RECTF rect;
164 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
165 EXPECT_EQ(351.820404f, rect.left);
166 EXPECT_EQ(583.830688f, rect.bottom);
167 EXPECT_EQ(475.336090f, rect.right);
168 EXPECT_EQ(681.535034f, rect.top);
169 }
Jane Liu4fd9a472017-06-01 18:56:09 -0400170 UnloadPage(page);
171}
Jane Liu20eafda2017-06-07 10:33:24 -0400172
173TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
174 // Open a file with one annotation and load its first page.
175 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
176 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
177 ASSERT_TRUE(page);
178
179 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400180 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400181
182 UnloadPage(page);
183}
184
Jane Liud321ef92017-06-14 09:56:22 -0400185TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400186 // Open a file with no annotation and load its first page.
187 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
188 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
189 ASSERT_TRUE(page);
190 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
191
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 {
193 // Add a text annotation to the page.
194 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
195 FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
196 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400197
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 // Check that there is now 1 annotations on this page.
199 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400200
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 // Check that the subtype of the annotation is correct.
202 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
203 }
Jane Liue10509a2017-06-20 16:47:41 -0400204
Lei Zhanga21d5932018-02-05 18:28:38 +0000205 {
206 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
207 FPDFPage_GetAnnot(page, 0));
208 ASSERT_TRUE(annot);
209 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400210
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 // Set the color of the annotation.
212 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
213 102, 153, 204));
214 // Check that the color has been set correctly.
215 unsigned int R;
216 unsigned int G;
217 unsigned int B;
218 unsigned int A;
219 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
220 &G, &B, &A));
221 EXPECT_EQ(51u, R);
222 EXPECT_EQ(102u, G);
223 EXPECT_EQ(153u, B);
224 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400225
Lei Zhanga21d5932018-02-05 18:28:38 +0000226 // Change the color of the annotation.
227 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
228 153, 102, 51));
229 // Check that the color has been set correctly.
230 EXPECT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
231 &G, &B, &A));
232 EXPECT_EQ(204u, R);
233 EXPECT_EQ(153u, G);
234 EXPECT_EQ(102u, B);
235 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400236
Lei Zhanga21d5932018-02-05 18:28:38 +0000237 // Set the annotation rectangle.
238 FS_RECTF rect;
239 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
240 EXPECT_EQ(0.f, rect.left);
241 EXPECT_EQ(0.f, rect.right);
242 rect.left = 35;
243 rect.bottom = 150;
244 rect.right = 53;
245 rect.top = 165;
246 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
247 // Check that the annotation rectangle has been set correctly.
248 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
249 EXPECT_EQ(35.f, rect.left);
250 EXPECT_EQ(150.f, rect.bottom);
251 EXPECT_EQ(53.f, rect.right);
252 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400253
Lei Zhanga21d5932018-02-05 18:28:38 +0000254 // Set the content of the annotation.
255 static constexpr wchar_t kContents[] =
256 L"Hello! This is a customized content.";
257 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
258 GetFPDFWideString(kContents);
259 ASSERT_TRUE(
260 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
261 // Check that the content has been set correctly.
262 unsigned long len =
263 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
264 std::vector<char> buf(len);
265 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
266 buf.data(), len));
267 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
268 }
Jane Liu20eafda2017-06-07 10:33:24 -0400269 UnloadPage(page);
270}
271
272TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
273 // Open a file with one annotation and load its first page.
274 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
275 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
276 ASSERT_TRUE(page);
277
278 // Check that there is a total of one annotation on its first page, and verify
279 // its quadpoints.
280 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400281 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 {
283 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
284 FPDFPage_GetAnnot(page, 0));
285 ASSERT_TRUE(annot);
286 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
287 EXPECT_EQ(115.802643f, quadpoints.x1);
288 EXPECT_EQ(718.913940f, quadpoints.y1);
289 EXPECT_EQ(157.211182f, quadpoints.x4);
290 EXPECT_EQ(706.264465f, quadpoints.y4);
291 }
Jane Liu20eafda2017-06-07 10:33:24 -0400292
293 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000294 {
295 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
296 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
297 ASSERT_TRUE(annot);
298 quadpoints.x1 = 140.802643f;
299 quadpoints.x3 = 140.802643f;
300 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
301 }
Jane Liu20eafda2017-06-07 10:33:24 -0400302
303 // Save the document, closing the page and document.
304 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
305 FPDF_ClosePage(page);
306
307 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000308 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400309
310 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000311 page = LoadSavedPage(0);
312 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400313
314 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000315 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400316
Lei Zhanga21d5932018-02-05 18:28:38 +0000317 {
318 // Check that the second annotation is an underline annotation and verify
319 // its quadpoints.
320 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
321 FPDFPage_GetAnnot(page, 1));
322 ASSERT_TRUE(new_annot);
323 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
324 FS_QUADPOINTSF new_quadpoints;
325 ASSERT_TRUE(
326 FPDFAnnot_GetAttachmentPoints(new_annot.get(), &new_quadpoints));
327 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
328 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
329 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
330 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
331 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400332
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000333 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400334 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400335}
Jane Liu06462752017-06-27 16:41:14 -0400336
337TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400338#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400339 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
340 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
341 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400342#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000343 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
344 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
345 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400346#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000347 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
348 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
349 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400350#endif
351
Jane Liu06462752017-06-27 16:41:14 -0400352 // Open a file with four annotations and load its first page.
353 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
354 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
355 ASSERT_TRUE(page);
356 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
357
Jane Liub370e5a2017-08-16 13:24:58 -0400358 // Check that the original file renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000359 FPDF_BITMAP bitmap =
360 RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liub370e5a2017-08-16 13:24:58 -0400361 CompareBitmap(bitmap, 612, 792, md5_original);
362 FPDFBitmap_Destroy(bitmap);
363
Jane Liu0c6b07d2017-08-15 10:50:22 -0400364 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400365 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000366
367 // Retrieve the highlight annotation which has its AP stream already defined.
368 {
369 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
370 FPDFPage_GetAnnot(page, 0));
371 ASSERT_TRUE(annot);
372 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
373
374 // Check that color cannot be set when an AP stream is defined already.
375 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
376 102, 153, 204));
377
378 // Verify its attachment points.
379 FS_QUADPOINTSF quadpoints;
380 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &quadpoints));
381 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
382 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
383 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
384 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
385
386 // Check that updating the attachment points would succeed.
387 quadpoints.x1 -= 50.f;
388 quadpoints.x2 -= 50.f;
389 quadpoints.x3 -= 50.f;
390 quadpoints.x4 -= 50.f;
391 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), &quadpoints));
392 FS_QUADPOINTSF new_quadpoints;
393 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), &new_quadpoints));
394 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
395 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
396 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
397 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
398
399 // Check that updating quadpoints does not change the annotation's position.
Lei Zhanga98e3662018-02-07 20:28:35 +0000400 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000401 CompareBitmap(bitmap, 612, 792, md5_original);
402 FPDFBitmap_Destroy(bitmap);
403
404 // Verify its annotation rectangle.
405 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
406 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
407 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
408 EXPECT_NEAR(136.325f, rect.right, 0.001f);
409 EXPECT_NEAR(721.292f, rect.top, 0.001f);
410
411 // Check that updating the rectangle would succeed.
412 rect.left -= 60.f;
413 rect.right -= 60.f;
414 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
415 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
416 EXPECT_EQ(rect.right, new_rect.right);
417 }
Jane Liu06462752017-06-27 16:41:14 -0400418
Jane Liub370e5a2017-08-16 13:24:58 -0400419 // Check that updating the rectangle changes the annotation's position.
Lei Zhanga98e3662018-02-07 20:28:35 +0000420 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liub370e5a2017-08-16 13:24:58 -0400421 CompareBitmap(bitmap, 612, 792, md5_modified_highlight);
422 FPDFBitmap_Destroy(bitmap);
423
Lei Zhanga21d5932018-02-05 18:28:38 +0000424 {
425 // Retrieve the square annotation which has its AP stream already defined.
426 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
427 FPDFPage_GetAnnot(page, 2));
428 ASSERT_TRUE(annot);
429 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400430
Lei Zhanga21d5932018-02-05 18:28:38 +0000431 // Check that updating the rectangle would succeed.
432 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
433 rect.left += 70.f;
434 rect.right += 70.f;
435 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
436 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
437 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400438
Lei Zhanga21d5932018-02-05 18:28:38 +0000439 // Check that updating the rectangle changes the square annotation's
440 // position.
Lei Zhanga98e3662018-02-07 20:28:35 +0000441 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000442 CompareBitmap(bitmap, 612, 792, md5_modified_square);
443 FPDFBitmap_Destroy(bitmap);
444 }
Jane Liub370e5a2017-08-16 13:24:58 -0400445
Jane Liu06462752017-06-27 16:41:14 -0400446 UnloadPage(page);
447}
Jane Liu8ce58f52017-06-29 13:40:22 -0400448
449TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
450 // Open a file with 3 annotations on its first page.
451 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
452 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
453 ASSERT_TRUE(page);
454 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
455
Jane Liu0c6b07d2017-08-15 10:50:22 -0400456 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400457
Lei Zhanga21d5932018-02-05 18:28:38 +0000458 // Check that the annotations have the expected rectangle coordinates.
459 {
460 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
461 FPDFPage_GetAnnot(page, 0));
462 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
463 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
464 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400465
Lei Zhanga21d5932018-02-05 18:28:38 +0000466 {
467 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
468 FPDFPage_GetAnnot(page, 1));
469 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
470 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
471 }
472
473 {
474 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
475 FPDFPage_GetAnnot(page, 2));
476 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
477 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
478 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400479
480 // Check that nothing happens when attempting to remove an annotation with an
481 // out-of-bound index.
482 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
483 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
484 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
485
486 // Remove the second annotation.
487 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
488 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
489 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
490
491 // Save the document, closing the page and document.
492 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
493 FPDF_ClosePage(page);
494
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400495 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400496 // Open the saved document.
497 std::string new_file = GetString();
498 FPDF_FILEACCESS file_access;
499 memset(&file_access, 0, sizeof(file_access));
500 file_access.m_FileLen = new_file.size();
501 file_access.m_GetBlock = GetBlockFromString;
502 file_access.m_Param = &new_file;
503 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
504 ASSERT_TRUE(new_doc);
505 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
506 ASSERT_TRUE(new_page);
507
508 // Check that the saved document has 2 annotations on the first page.
509 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
510
Lei Zhanga21d5932018-02-05 18:28:38 +0000511 // Check that the remaining 2 annotations are the original 1st and 3rd ones
512 // by verifying their rectangle coordinates.
513 {
514 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
515 FPDFPage_GetAnnot(new_page, 0));
516 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
517 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
518 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400519
Lei Zhanga21d5932018-02-05 18:28:38 +0000520 {
521 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
522 FPDFPage_GetAnnot(new_page, 1));
523 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
524 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
525 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400526 FPDF_ClosePage(new_page);
527 FPDF_CloseDocument(new_doc);
528}
Jane Liu8ce58f52017-06-29 13:40:22 -0400529
Jane Liubaa7ff42017-06-29 19:18:23 -0400530TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400531#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400532 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
533 const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8";
534 const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff";
535 const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25";
Jane Liubaa7ff42017-06-29 19:18:23 -0400536#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000537 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
538 const char md5_modified_path[] = "3f77b88ce6048e08e636c9a03921b2e5";
539 const char md5_two_paths[] = "bffbf5ecd15862b9fe553c795400ff8e";
540 const char md5_new_annot[] = "e020534c7eeea76be537c70d6e359a40";
Jane Liubaa7ff42017-06-29 19:18:23 -0400541#endif
542
543 // Open a file with two annotations and load its first page.
544 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
545 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
546 ASSERT_TRUE(page);
547 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
548
549 // Check that the page renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000550 FPDF_BITMAP bitmap =
551 RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400552 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liubaa7ff42017-06-29 19:18:23 -0400553 FPDFBitmap_Destroy(bitmap);
554
Lei Zhanga21d5932018-02-05 18:28:38 +0000555 {
556 // Retrieve the stamp annotation which has its AP stream already defined.
557 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
558 FPDFPage_GetAnnot(page, 0));
559 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400560
Lei Zhanga21d5932018-02-05 18:28:38 +0000561 // Check that this annotation has one path object and retrieve it.
562 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
563 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
564 EXPECT_FALSE(path);
565 path = FPDFAnnot_GetObject(annot.get(), 0);
566 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
567 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400568
Lei Zhanga21d5932018-02-05 18:28:38 +0000569 // Modify the color of the path object.
570 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
571 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400572
Lei Zhanga21d5932018-02-05 18:28:38 +0000573 // Check that the page with the modified annotation renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000574 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 CompareBitmap(bitmap, 595, 842, md5_modified_path);
576 FPDFBitmap_Destroy(bitmap);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400577
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 // Add a second path object to the same annotation.
579 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
580 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
581 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
582 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
583 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
584 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
585 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400586
Lei Zhanga21d5932018-02-05 18:28:38 +0000587 // Check that the page with an annotation with two paths renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000588 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000589 CompareBitmap(bitmap, 595, 842, md5_two_paths);
590 FPDFBitmap_Destroy(bitmap);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400591
Lei Zhanga21d5932018-02-05 18:28:38 +0000592 // Delete the newly added path object.
593 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
594 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
595 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400596
597 // Check that the page renders the same as before.
Lei Zhanga98e3662018-02-07 20:28:35 +0000598 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400599 CompareBitmap(bitmap, 595, 842, md5_modified_path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400600 FPDFBitmap_Destroy(bitmap);
601
Jane Liubaa7ff42017-06-29 19:18:23 -0400602 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400603
Lei Zhanga21d5932018-02-05 18:28:38 +0000604 {
605 // Create another stamp annotation and set its annotation rectangle.
606 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
607 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
608 ASSERT_TRUE(annot);
609 rect.left = 200.f;
610 rect.bottom = 400.f;
611 rect.right = 500.f;
612 rect.top = 600.f;
613 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400614
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 // Add a new path to the annotation.
616 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
617 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
618 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
619 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
620 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
621 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
622 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
623 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
624 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
625 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
626
627 // Check that the annotation's bounding box came from its rectangle.
628 FS_RECTF new_rect;
629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
630 EXPECT_EQ(rect.left, new_rect.left);
631 EXPECT_EQ(rect.bottom, new_rect.bottom);
632 EXPECT_EQ(rect.right, new_rect.right);
633 EXPECT_EQ(rect.top, new_rect.top);
634 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400635
636 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400637 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
638 FPDF_ClosePage(page);
639
640 // Open the saved document.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400641 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000642 page = LoadSavedPage(0);
643 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400644
Jane Liu36567742017-07-06 11:13:35 -0400645 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000646 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400647
Lei Zhanga21d5932018-02-05 18:28:38 +0000648 {
649 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
650 FPDFPage_GetAnnot(page, 2));
651 ASSERT_TRUE(annot);
652 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400653
Lei Zhanga21d5932018-02-05 18:28:38 +0000654 // Check that the new annotation's rectangle is as defined.
655 FS_RECTF new_rect;
656 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
657 EXPECT_EQ(rect.left, new_rect.left);
658 EXPECT_EQ(rect.bottom, new_rect.bottom);
659 EXPECT_EQ(rect.right, new_rect.right);
660 EXPECT_EQ(rect.top, new_rect.top);
661 }
662
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000663 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400664 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400665}
Jane Liub137e752017-07-05 15:04:33 -0400666
667TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
668 // Open a file with an annotation and load its first page.
669 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
670 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
671 ASSERT_TRUE(page);
672
673 // Check that the page renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000674 FPDF_BITMAP bitmap =
675 RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liub137e752017-07-05 15:04:33 -0400676 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
677 FPDFBitmap_Destroy(bitmap);
678
Lei Zhanga21d5932018-02-05 18:28:38 +0000679 {
680 // Retrieve the annotation.
681 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
682 FPDFPage_GetAnnot(page, 0));
683 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400684
Lei Zhanga21d5932018-02-05 18:28:38 +0000685 // Check that the original flag values are as expected.
686 int flags = FPDFAnnot_GetFlags(annot.get());
687 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
688 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400689
Lei Zhanga21d5932018-02-05 18:28:38 +0000690 // Set the HIDDEN flag.
691 flags |= FPDF_ANNOT_FLAG_HIDDEN;
692 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
693 flags = FPDFAnnot_GetFlags(annot.get());
694 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
695 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400696
Lei Zhanga21d5932018-02-05 18:28:38 +0000697 // Check that the page renders correctly without rendering the annotation.
Lei Zhanga98e3662018-02-07 20:28:35 +0000698 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000699 CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
700 FPDFBitmap_Destroy(bitmap);
Jane Liub137e752017-07-05 15:04:33 -0400701
Lei Zhanga21d5932018-02-05 18:28:38 +0000702 // Unset the HIDDEN flag.
703 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
704 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
705 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
706 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
707 flags = FPDFAnnot_GetFlags(annot.get());
708 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
709 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400710
Lei Zhanga21d5932018-02-05 18:28:38 +0000711 // Check that the page renders correctly as before.
Lei Zhanga98e3662018-02-07 20:28:35 +0000712 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Lei Zhanga21d5932018-02-05 18:28:38 +0000713 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
714 FPDFBitmap_Destroy(bitmap);
715 }
Jane Liub137e752017-07-05 15:04:33 -0400716
Jane Liub137e752017-07-05 15:04:33 -0400717 UnloadPage(page);
718}
Jane Liu36567742017-07-06 11:13:35 -0400719
720TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400721#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400722 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
723 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
724 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400725#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000726 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
727 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
728 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400729#endif
730
731 // Open a file with two annotations and load its first page.
732 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
733 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
734 ASSERT_TRUE(page);
735 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
736
737 // Check that the page renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000738 FPDF_BITMAP bitmap =
739 RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400740 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400741 FPDFBitmap_Destroy(bitmap);
742
Jane Liu36567742017-07-06 11:13:35 -0400743 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000744 FPDF_BITMAP image_bitmap;
745
746 {
747 // Create a stamp annotation and set its annotation rectangle.
748 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
749 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
750 ASSERT_TRUE(annot);
751 FS_RECTF rect;
752 rect.left = 200.f;
753 rect.bottom = 600.f;
754 rect.right = 400.f;
755 rect.top = 800.f;
756 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
757
758 // Add a solid-color translucent image object to the new annotation.
759 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
760 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
761 0xeeeecccc);
762 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
763 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
764 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
765 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
766 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
767 kBitmapSize, 0, 0));
768 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
769 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
770 }
Jane Liu36567742017-07-06 11:13:35 -0400771
772 // Check that the page renders correctly with the new image object.
Lei Zhanga98e3662018-02-07 20:28:35 +0000773 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400774 CompareBitmap(bitmap, 595, 842, md5_new_image);
Jane Liu36567742017-07-06 11:13:35 -0400775 FPDFBitmap_Destroy(bitmap);
776
Lei Zhanga21d5932018-02-05 18:28:38 +0000777 {
778 // Retrieve the newly added stamp annotation and its image object.
779 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
780 FPDFPage_GetAnnot(page, 2));
781 ASSERT_TRUE(annot);
782 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
783 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
784 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400785
Lei Zhanga21d5932018-02-05 18:28:38 +0000786 // Modify the image in the new annotation.
787 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
788 0xff000000);
789 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
790 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
791 }
Jane Liu36567742017-07-06 11:13:35 -0400792
793 // Save the document, closing the page and document.
794 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
795 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400796 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400797
798 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400799 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400800}
801
802TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400803#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400804 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
805 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
806 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400807#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000808 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
809 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
810 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400811#endif
812
813 // Open a file with two annotations and load its first page.
814 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
815 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
816 ASSERT_TRUE(page);
817 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
818
819 // Check that the page renders correctly.
Lei Zhanga98e3662018-02-07 20:28:35 +0000820 FPDF_BITMAP bitmap =
821 RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400822 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400823 FPDFBitmap_Destroy(bitmap);
824
Lei Zhanga21d5932018-02-05 18:28:38 +0000825 {
826 // Create a stamp annotation and set its annotation rectangle.
827 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
828 FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
829 ASSERT_TRUE(annot);
830 FS_RECTF rect;
831 rect.left = 200.f;
832 rect.bottom = 550.f;
833 rect.right = 450.f;
834 rect.top = 650.f;
835 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400836
Lei Zhanga21d5932018-02-05 18:28:38 +0000837 // Add a translucent text object to the new annotation.
838 FPDF_PAGEOBJECT text_object =
839 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
840 EXPECT_TRUE(text_object);
841 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
842 GetFPDFWideString(L"I'm a translucent text laying on other text.");
843 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
844 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
845 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
846 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
847 }
Jane Liu36567742017-07-06 11:13:35 -0400848
849 // Check that the page renders correctly with the new text object.
Lei Zhanga98e3662018-02-07 20:28:35 +0000850 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400851 CompareBitmap(bitmap, 595, 842, md5_new_text);
Jane Liu36567742017-07-06 11:13:35 -0400852 FPDFBitmap_Destroy(bitmap);
853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 {
855 // Retrieve the newly added stamp annotation and its text object.
856 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
857 FPDFPage_GetAnnot(page, 2));
858 ASSERT_TRUE(annot);
859 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
860 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
861 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400862
Lei Zhanga21d5932018-02-05 18:28:38 +0000863 // Modify the text in the new annotation.
864 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
865 GetFPDFWideString(L"New text!");
866 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
867 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
868 }
Jane Liu36567742017-07-06 11:13:35 -0400869
870 // Check that the page renders correctly with the modified text object.
Lei Zhanga98e3662018-02-07 20:28:35 +0000871 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400872 CompareBitmap(bitmap, 595, 842, md5_modified_text);
Jane Liu36567742017-07-06 11:13:35 -0400873 FPDFBitmap_Destroy(bitmap);
874
875 // Remove the new annotation, and check that the page renders as before.
876 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhanga98e3662018-02-07 20:28:35 +0000877 bitmap = RenderPageWithFlagsDeprecated(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400878 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400879 FPDFBitmap_Destroy(bitmap);
880
881 UnloadPage(page);
882}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400883
884TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
885 // Open a file with four annotations and load its first page.
886 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
887 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
888 ASSERT_TRUE(page);
889
Lei Zhangdf064df2017-08-31 02:33:27 -0700890 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +0000891 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400892
Lei Zhanga21d5932018-02-05 18:28:38 +0000893 {
894 // Retrieve the first annotation.
895 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
896 FPDFPage_GetAnnot(page, 0));
897 ASSERT_TRUE(annot);
898
899 // Check that a non-existent key does not exist.
900 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
901
902 // Check that the string value of a non-string dictionary entry is empty.
903 static constexpr char kApKey[] = "AP";
904 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
905 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
906 FPDFAnnot_GetValueType(annot.get(), kApKey));
907 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
908
909 // Check that the string value of the hash is correct.
910 static constexpr char kHashKey[] = "AAPL:Hash";
911 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
912 unsigned long len =
913 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
914 std::vector<char> buf(len);
915 EXPECT_EQ(66u,
916 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
917 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
918 BufferToWString(buf).c_str());
919
920 // Check that the string value of the modified date is correct.
921 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
922 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
923 buf.clear();
924 buf.resize(len);
925 EXPECT_EQ(44u,
926 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
927 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
928
929 // Update the date entry for the annotation.
930 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
931 GetFPDFWideString(kNewDate);
932 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
933 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400934
935 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400936 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
937 FPDF_ClosePage(page);
938
939 // Open the saved annotation.
Dan Sinclair698aed72017-09-26 16:24:49 -0400940#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300941 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400942#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000943 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400944#endif
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400945 OpenSavedDocument();
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000946 page = LoadSavedPage(0);
947 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +0000948 {
949 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
950 FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -0400951
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 // Check that the string value of the modified date is the newly-set value.
953 EXPECT_EQ(FPDF_OBJECT_STRING,
954 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
955 unsigned long len =
956 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
957 std::vector<char> buf(len);
958 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
959 buf.data(), len));
960 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
961 }
Jane Liu2e1a32b2017-07-06 12:01:25 -0400962
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000963 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400964 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -0400965}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700966
Henrique Nakashima5970a472018-01-11 22:40:59 +0000967TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000968 // Open a file with four annotations and load its first page.
969 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
970 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
971 ASSERT_TRUE(page);
972
Lei Zhanga21d5932018-02-05 18:28:38 +0000973 {
974 // Retrieve the first annotation.
975 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
976 FPDFPage_GetAnnot(page, 0));
977 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000978
Lei Zhanga21d5932018-02-05 18:28:38 +0000979 // Check that the string value of an AP returns the expected length.
980 unsigned long normal_len = FPDFAnnot_GetAP(
981 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
982 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000983
Lei Zhanga21d5932018-02-05 18:28:38 +0000984 // Check that the string value of an AP is not returned if the buffer is too
985 // small. The result buffer should be overwritten with an empty string.
986 std::vector<char> buf(normal_len - 1);
987 // Write L"z" in the buffer to verify it's not overwritten.
988 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
989 EXPECT_EQ(73970u,
990 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
991 buf.data(), buf.size()));
992 std::string ap = BufferToString(buf);
993 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +0000994
Lei Zhanga21d5932018-02-05 18:28:38 +0000995 // Check that the string value of an AP is returned through a buffer that is
996 // the right size.
997 buf.clear();
998 buf.resize(normal_len);
999 EXPECT_EQ(73970u,
1000 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1001 buf.data(), buf.size()));
1002 ap = BufferToString(buf);
1003 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1004 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001005
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 // Check that the string value of an AP is returned through a buffer that is
1007 // larger than necessary.
1008 buf.clear();
1009 buf.resize(normal_len + 1);
1010 EXPECT_EQ(73970u,
1011 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1012 buf.data(), buf.size()));
1013 ap = BufferToString(buf);
1014 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1015 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001016
Lei Zhanga21d5932018-02-05 18:28:38 +00001017 // Check that getting an AP for a mode that does not have an AP returns an
1018 // empty string.
1019 unsigned long rollover_len = FPDFAnnot_GetAP(
1020 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1021 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001022
Lei Zhanga21d5932018-02-05 18:28:38 +00001023 buf.clear();
1024 buf.resize(1000);
1025 EXPECT_EQ(2u,
1026 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1027 buf.data(), buf.size()));
1028 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001029
Lei Zhanga21d5932018-02-05 18:28:38 +00001030 // Check that setting the AP for an invalid appearance mode fails.
1031 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1032 GetFPDFWideString(L"new test ap");
1033 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1034 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1035 apText.get()));
1036 EXPECT_FALSE(FPDFAnnot_SetAP(
1037 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001038
Lei Zhanga21d5932018-02-05 18:28:38 +00001039 // Set the AP correctly now.
1040 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1041 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001042
Lei Zhanga21d5932018-02-05 18:28:38 +00001043 // Check that the new annotation value is equal to the value we just set.
1044 rollover_len = FPDFAnnot_GetAP(
1045 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1046 EXPECT_EQ(24u, rollover_len);
1047 buf.clear();
1048 buf.resize(rollover_len);
1049 EXPECT_EQ(24u,
1050 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1051 buf.data(), buf.size()));
1052 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001053
Lei Zhanga21d5932018-02-05 18:28:38 +00001054 // Check that the Normal AP was not touched when the Rollover AP was set.
1055 buf.clear();
1056 buf.resize(normal_len);
1057 EXPECT_EQ(73970u,
1058 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1059 buf.data(), buf.size()));
1060 ap = BufferToString(buf);
1061 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1062 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1063 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001064
1065 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001066 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1067 FPDF_ClosePage(page);
1068
1069 OpenSavedDocument();
1070 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 {
1072 std::unique_ptr<void, FPDFAnnotationDeleter> new_annot(
1073 FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001074
Lei Zhanga21d5932018-02-05 18:28:38 +00001075 // Check that the new annotation value is equal to the value we set before
1076 // saving.
1077 unsigned long rollover_len = FPDFAnnot_GetAP(
1078 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1079 EXPECT_EQ(24u, rollover_len);
1080 std::vector<char> buf(rollover_len);
1081 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1082 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1083 buf.data(), buf.size()));
1084 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1085 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001086
1087 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001088 CloseSavedPage(page);
1089 CloseSavedDocument();
1090}
1091
1092TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1093 // Open a file with four annotations and load its first page.
1094 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1095 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1096 ASSERT_TRUE(page);
1097
Lei Zhanga21d5932018-02-05 18:28:38 +00001098 {
1099 // Retrieve the first annotation.
1100 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1101 FPDFPage_GetAnnot(page, 0));
1102 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001103
Lei Zhanga21d5932018-02-05 18:28:38 +00001104 // Set Down AP. Normal AP is already set.
1105 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1106 GetFPDFWideString(L"new test ap");
1107 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1108 apText.get()));
1109 EXPECT_EQ(73970u,
1110 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1111 nullptr, 0));
1112 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1113 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001114
Lei Zhanga21d5932018-02-05 18:28:38 +00001115 // Check that setting the Down AP to null removes the Down entry but keeps
1116 // Normal intact.
1117 EXPECT_TRUE(
1118 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1119 EXPECT_EQ(73970u,
1120 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1121 nullptr, 0));
1122 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1123 nullptr, 0));
1124 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001125
Henrique Nakashima5970a472018-01-11 22:40:59 +00001126 FPDF_ClosePage(page);
1127}
1128
1129TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1130 // Open a file with four annotations and load its first page.
1131 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1132 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1133 ASSERT_TRUE(page);
1134
Lei Zhanga21d5932018-02-05 18:28:38 +00001135 {
1136 // Retrieve the first annotation.
1137 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1138 FPDFPage_GetAnnot(page, 0));
1139 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001140
Lei Zhanga21d5932018-02-05 18:28:38 +00001141 // Set Down AP. Normal AP is already set.
1142 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1143 GetFPDFWideString(L"new test ap");
1144 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1145 apText.get()));
1146 EXPECT_EQ(73970u,
1147 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1148 nullptr, 0));
1149 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1150 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001151
Lei Zhanga21d5932018-02-05 18:28:38 +00001152 // Check that setting the Normal AP to null removes the whole AP dictionary.
1153 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1154 nullptr));
1155 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1156 nullptr, 0));
1157 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1158 nullptr, 0));
1159 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001160
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001161 FPDF_ClosePage(page);
1162}
1163
Jane Liu300bb272017-08-21 14:37:53 -04001164TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1165 // Open a file with annotations and load its first page.
1166 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
1167 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1168 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001169 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 {
1172 // Retrieve the highlight annotation which has its popup defined.
1173 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1174 FPDFPage_GetAnnot(page, 0));
1175 ASSERT_TRUE(annot);
1176 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1177 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1178 static constexpr char kPopupKey[] = "Popup";
1179 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1180 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1181 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001182
Lei Zhanga21d5932018-02-05 18:28:38 +00001183 // Retrieve and verify the popup of the highlight annotation.
1184 std::unique_ptr<void, FPDFAnnotationDeleter> popup(
1185 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1186 ASSERT_TRUE(popup);
1187 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1188 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1189 FS_RECTF rect;
1190 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1191 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1192 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001193
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1195 // since "IRT" is not a key in |annot|'s dictionary.
1196 static constexpr char kIRTKey[] = "IRT";
1197 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1198 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1201 // would fail, since its parent is not an annotation.
1202 static constexpr char kPKey[] = "P";
1203 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1204 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1205 FPDFAnnot_GetValueType(annot.get(), kPKey));
1206 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1207 }
Jane Liu300bb272017-08-21 14:37:53 -04001208
Jane Liu300bb272017-08-21 14:37:53 -04001209 UnloadPage(page);
1210}
1211
Diana Gage7e0c05d2017-07-19 17:33:33 -07001212TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1213 // Open file with form text fields.
1214 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1215 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1216 ASSERT_TRUE(page);
1217
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 {
1219 // Retrieve the first annotation: user-editable text field.
1220 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1221 FPDFPage_GetAnnot(page, 0));
1222 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001223
Lei Zhanga21d5932018-02-05 18:28:38 +00001224 // Check that the flag values are as expected.
1225 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1226 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1227 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001228
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 {
1230 // Retrieve the second annotation: read-only text field.
1231 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1232 FPDFPage_GetAnnot(page, 1));
1233 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001234
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 // Check that the flag values are as expected.
1236 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1237 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1238 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001239
1240 UnloadPage(page);
1241}
1242
1243TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1244 // Open file with form text fields.
1245 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1246 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
1247 ASSERT_TRUE(page);
1248
Lei Zhanga21d5932018-02-05 18:28:38 +00001249 {
1250 // Retrieve the first annotation: user-editable combobox.
1251 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1252 FPDFPage_GetAnnot(page, 0));
1253 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001254
Lei Zhanga21d5932018-02-05 18:28:38 +00001255 // Check that the flag values are as expected.
1256 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1257 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1258 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1259 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1260 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001261
Lei Zhanga21d5932018-02-05 18:28:38 +00001262 {
1263 // Retrieve the second annotation: regular combobox.
1264 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1265 FPDFPage_GetAnnot(page, 1));
1266 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001267
Lei Zhanga21d5932018-02-05 18:28:38 +00001268 // Check that the flag values are as expected.
1269 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1270 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1271 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1272 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1273 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001274
Lei Zhanga21d5932018-02-05 18:28:38 +00001275 {
1276 // Retrieve the third annotation: read-only combobox.
1277 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1278 FPDFPage_GetAnnot(page, 2));
1279 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001280
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 // Check that the flag values are as expected.
1282 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1283 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1284 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1285 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1286 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001287
1288 UnloadPage(page);
1289}
Diana Gage40870db2017-07-19 18:16:03 -07001290
1291TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1292 // Open file with form text fields.
1293 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1294 FPDF_PAGE page = LoadPage(0);
1295 ASSERT_TRUE(page);
1296
1297 // Attempt to get an annotation where no annotation exists on page.
1298 FPDF_ANNOTATION annot =
1299 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1300 EXPECT_FALSE(annot);
1301
1302 UnloadPage(page);
1303}
1304
1305TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1306 // Open file with form text fields.
1307 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1308 FPDF_PAGE page = LoadPage(0);
1309 ASSERT_TRUE(page);
1310
Lei Zhanga21d5932018-02-05 18:28:38 +00001311 {
1312 // Retrieve user-editable text field annotation.
1313 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1314 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1315 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001316
Lei Zhanga21d5932018-02-05 18:28:38 +00001317 // Check that interactive form annotation flag values are as expected.
1318 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1319 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1320 }
Diana Gage40870db2017-07-19 18:16:03 -07001321
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 {
1323 // Retrieve read-only text field annotation.
1324 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1325 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1326 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001327
Lei Zhanga21d5932018-02-05 18:28:38 +00001328 // Check that interactive form annotation flag values are as expected.
1329 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1330 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1331 }
Diana Gage40870db2017-07-19 18:16:03 -07001332
1333 UnloadPage(page);
1334}
1335
1336TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1337 // Open file with form comboboxes.
1338 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1339 FPDF_PAGE page = LoadPage(0);
1340 ASSERT_TRUE(page);
1341
Lei Zhanga21d5932018-02-05 18:28:38 +00001342 {
1343 // Retrieve user-editable combobox annotation.
1344 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1345 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63));
1346 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001347
Lei Zhanga21d5932018-02-05 18:28:38 +00001348 // Check that interactive form annotation flag values are as expected.
1349 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1350 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1351 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1352 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1353 }
Diana Gage40870db2017-07-19 18:16:03 -07001354
Lei Zhanga21d5932018-02-05 18:28:38 +00001355 {
1356 // Retrieve regular combobox annotation.
1357 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1358 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113));
1359 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001360
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 // Check that interactive form annotation flag values are as expected.
1362 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1363 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1364 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1365 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1366 }
Diana Gage40870db2017-07-19 18:16:03 -07001367
Lei Zhanga21d5932018-02-05 18:28:38 +00001368 {
1369 // Retrieve read-only combobox annotation.
1370 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1371 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213));
1372 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001373
Lei Zhanga21d5932018-02-05 18:28:38 +00001374 // Check that interactive form annotation flag values are as expected.
1375 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1376 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1377 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1378 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1379 }
Diana Gage40870db2017-07-19 18:16:03 -07001380
1381 UnloadPage(page);
1382}