blob: 7be5d751774094edde6bbdc6a8b42549a6171173 [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"
Tom Sepeze08d2b12018-04-25 18:49:32 +000011#include "public/cpp/fpdf_scopers.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
Lei Zhang7557e7b2018-09-14 17:02:40 +000031TEST_F(FPDFAnnotEmbeddertest, BadParams) {
32 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
33 FPDF_PAGE page = LoadPage(0);
34 ASSERT_TRUE(page);
35
36 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
37
38 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
43
44 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
45
46 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
47
48 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
51
52 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
53
54 static constexpr wchar_t kContents[] = L"Bar";
55 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
56 GetFPDFWideString(kContents);
57 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
58
59 char buffer[128];
60 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
62 EXPECT_EQ(0u,
63 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
64
65 UnloadPage(page);
66}
67
Jane Liue17011d2017-06-21 12:18:37 -040068TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
69 // Open a file with one annotation and load its first page.
70 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000071 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040072 ASSERT_TRUE(page);
73
74 // This annotation has a malformed appearance stream, which does not have its
75 // normal appearance defined, only its rollover appearance. In this case, its
76 // normal appearance should be generated, allowing the highlight annotation to
77 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000078 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000079 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040080
81 UnloadPage(page);
82}
83
Ralf Sipplb3a52402018-03-19 23:30:28 +000084TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
85 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000086 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000087 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
88 FPDF_PAGE page = LoadPage(0);
89 ASSERT_TRUE(page);
90
Tom Sepeze08d2b12018-04-25 18:49:32 +000091 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000092 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
93
94 UnloadPage(page);
95}
96
Jane Liu4fd9a472017-06-01 18:56:09 -040097TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
98 // Open a file with one annotation and load its first page.
99 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000100 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
101 // the FORM API calls in LoadPage() makes this test fail. So use
102 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400103 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
104 ASSERT_TRUE(page);
105
106 // Check that there is a total of 1 annotation on its first page.
107 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
108
109 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000110 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000111 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000112 ASSERT_TRUE(annot);
113 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400114
Lei Zhanga21d5932018-02-05 18:28:38 +0000115 // Check that the annotation color is yellow.
116 unsigned int R;
117 unsigned int G;
118 unsigned int B;
119 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000120 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000121 &G, &B, &A));
122 EXPECT_EQ(255u, R);
123 EXPECT_EQ(255u, G);
124 EXPECT_EQ(0u, B);
125 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400126
Lei Zhanga21d5932018-02-05 18:28:38 +0000127 // Check that the author is correct.
128 static constexpr char kAuthorKey[] = "T";
129 EXPECT_EQ(FPDF_OBJECT_STRING,
130 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
131 unsigned long len =
132 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
133 std::vector<char> buf(len);
134 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
135 len));
136 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400137
Lei Zhanga21d5932018-02-05 18:28:38 +0000138 // Check that the content is correct.
139 EXPECT_EQ(FPDF_OBJECT_STRING,
140 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
141 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
142 buf.clear();
143 buf.resize(len);
144 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
145 buf.data(), len));
146 const wchar_t contents[] =
147 L"This is a note for that highlight annotation. Very long highlight "
148 "annotation. Long long long Long long longLong long longLong long "
149 "longLong long longLong long longLong long longLong long longLong long "
150 "longLong long longLong long longLong long longLong long longLong long "
151 "longLong long longLong long longLong long longLong long longLong long "
152 "longLong long longLong long longLong long longLong long longLong long "
153 "longLong long longLong long longLong long longLong long longLong long "
154 "longLong long longLong long longLong long longLong long longLong long "
155 "longLong long longLong long longLong long longLong long longLong long "
156 "longLong long longLong long longLong long longLong long longLong long "
157 "longLong long longLong long longLong long longLong long longLong long "
158 "longLong long longLong long longLong long longLong long longLong long "
159 "longLong long longLong long longLong long longLong long longLong long "
160 "longLong long longLong long longLong long longLong long longLong long "
161 "longLong long longLong long longLong long longLong long longLong long "
162 "longLong long longLong long longLong long longLong long longLong long "
163 "longLong long longLong long longLong long longLong long longLong long "
164 "longLong long longLong long longLong long longLong long longLong long "
165 "longLong long longLong long longLong long longLong long longLong long "
166 "longLong long long. END";
167 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400168
Lei Zhanga21d5932018-02-05 18:28:38 +0000169 // Check that the quadpoints are correct.
170 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000171 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000172 EXPECT_EQ(115.802643f, quadpoints.x1);
173 EXPECT_EQ(718.913940f, quadpoints.y1);
174 EXPECT_EQ(157.211182f, quadpoints.x4);
175 EXPECT_EQ(706.264465f, quadpoints.y4);
176 }
Lei Zhang75c81712018-02-08 17:22:39 +0000177 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400178}
179
180TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
181 // Open a file with three annotations and load its first page.
182 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000183 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
184 // the FORM API calls in LoadPage() makes this test fail. So use
185 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400186 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
187 ASSERT_TRUE(page);
188
189 // Check that there is a total of 3 annotation on its first page.
190 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
191
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 {
193 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000194 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000195 ASSERT_TRUE(annot);
196 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400197
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 // Check that the annotation color is blue with opacity.
199 unsigned int R;
200 unsigned int G;
201 unsigned int B;
202 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000203 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 &G, &B, &A));
205 EXPECT_EQ(0u, R);
206 EXPECT_EQ(0u, G);
207 EXPECT_EQ(255u, B);
208 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400209
Lei Zhanga21d5932018-02-05 18:28:38 +0000210 // Check that there is no content.
211 EXPECT_EQ(2u,
212 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400213
Lei Zhanga21d5932018-02-05 18:28:38 +0000214 // Check that the rectange coordinates are correct.
215 // Note that upon rendering, the rectangle coordinates will be adjusted.
216 FS_RECTF rect;
217 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
218 EXPECT_EQ(351.820404f, rect.left);
219 EXPECT_EQ(583.830688f, rect.bottom);
220 EXPECT_EQ(475.336090f, rect.right);
221 EXPECT_EQ(681.535034f, rect.top);
222 }
Lei Zhang75c81712018-02-08 17:22:39 +0000223 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400224}
Jane Liu20eafda2017-06-07 10:33:24 -0400225
226TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
227 // Open a file with one annotation and load its first page.
228 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000229 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400230 ASSERT_TRUE(page);
231
232 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400233 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400234
235 UnloadPage(page);
236}
237
Jane Liud321ef92017-06-14 09:56:22 -0400238TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400239 // Open a file with no annotation and load its first page.
240 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000241 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400242 ASSERT_TRUE(page);
243 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
244
Lei Zhanga21d5932018-02-05 18:28:38 +0000245 {
246 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000247 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000248 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400249
Lei Zhanga21d5932018-02-05 18:28:38 +0000250 // Check that there is now 1 annotations on this page.
251 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400252
Lei Zhanga21d5932018-02-05 18:28:38 +0000253 // Check that the subtype of the annotation is correct.
254 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
255 }
Jane Liue10509a2017-06-20 16:47:41 -0400256
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000258 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000259 ASSERT_TRUE(annot);
260 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400261
Lei Zhanga21d5932018-02-05 18:28:38 +0000262 // Set the color of the annotation.
263 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
264 102, 153, 204));
265 // Check that the color has been set correctly.
266 unsigned int R;
267 unsigned int G;
268 unsigned int B;
269 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000270 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000271 &G, &B, &A));
272 EXPECT_EQ(51u, R);
273 EXPECT_EQ(102u, G);
274 EXPECT_EQ(153u, B);
275 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400276
Lei Zhanga21d5932018-02-05 18:28:38 +0000277 // Change the color of the annotation.
278 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
279 153, 102, 51));
280 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000281 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000282 &G, &B, &A));
283 EXPECT_EQ(204u, R);
284 EXPECT_EQ(153u, G);
285 EXPECT_EQ(102u, B);
286 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400287
Lei Zhanga21d5932018-02-05 18:28:38 +0000288 // Set the annotation rectangle.
289 FS_RECTF rect;
290 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
291 EXPECT_EQ(0.f, rect.left);
292 EXPECT_EQ(0.f, rect.right);
293 rect.left = 35;
294 rect.bottom = 150;
295 rect.right = 53;
296 rect.top = 165;
297 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
298 // Check that the annotation rectangle has been set correctly.
299 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
300 EXPECT_EQ(35.f, rect.left);
301 EXPECT_EQ(150.f, rect.bottom);
302 EXPECT_EQ(53.f, rect.right);
303 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400304
Lei Zhanga21d5932018-02-05 18:28:38 +0000305 // Set the content of the annotation.
306 static constexpr wchar_t kContents[] =
307 L"Hello! This is a customized content.";
308 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
309 GetFPDFWideString(kContents);
310 ASSERT_TRUE(
311 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
312 // Check that the content has been set correctly.
313 unsigned long len =
314 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
315 std::vector<char> buf(len);
316 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
317 buf.data(), len));
318 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
319 }
Jane Liu20eafda2017-06-07 10:33:24 -0400320 UnloadPage(page);
321}
322
323TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
324 // Open a file with one annotation and load its first page.
325 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000326 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400327 ASSERT_TRUE(page);
328
329 // Check that there is a total of one annotation on its first page, and verify
330 // its quadpoints.
331 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400332 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000333 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000334 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000335 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000336 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000337 EXPECT_EQ(115.802643f, quadpoints.x1);
338 EXPECT_EQ(718.913940f, quadpoints.y1);
339 EXPECT_EQ(157.211182f, quadpoints.x4);
340 EXPECT_EQ(706.264465f, quadpoints.y4);
341 }
Jane Liu20eafda2017-06-07 10:33:24 -0400342
343 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000344 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000345 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000346 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
347 ASSERT_TRUE(annot);
348 quadpoints.x1 = 140.802643f;
349 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000350 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000351 }
Jane Liu20eafda2017-06-07 10:33:24 -0400352
353 // Save the document, closing the page and document.
354 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000355 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400356
357 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000358 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400359
Tom Sepezb9c3e272018-08-14 18:22:06 +0000360 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000361 page = LoadSavedPage(0);
362 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400363
364 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000365 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400366
Lei Zhanga21d5932018-02-05 18:28:38 +0000367 {
368 // Check that the second annotation is an underline annotation and verify
369 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000370 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000371 ASSERT_TRUE(new_annot);
372 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
373 FS_QUADPOINTSF new_quadpoints;
374 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000375 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000376 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
377 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
378 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
379 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
380 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400381
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000382 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400383 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400384}
Jane Liu06462752017-06-27 16:41:14 -0400385
Ralf Sippl16381792018-04-12 21:20:26 +0000386TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
387 // Open a file with four annotations and load its first page.
388 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
389 FPDF_PAGE page = LoadPage(0);
390 ASSERT_TRUE(page);
391 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
392
393 // Retrieve the highlight annotation.
394 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
395 ASSERT_TRUE(annot);
396 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
397
398 FS_QUADPOINTSF quadpoints;
399 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
400
401 {
402 // Verify the current one set of quadpoints.
403 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
404
405 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
406 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
407 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
408 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
409 }
410
411 {
412 // Update the quadpoints.
413 FS_QUADPOINTSF new_quadpoints = quadpoints;
414 new_quadpoints.y1 -= 20.f;
415 new_quadpoints.y2 -= 20.f;
416 new_quadpoints.y3 -= 20.f;
417 new_quadpoints.y4 -= 20.f;
418 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
419
420 // Verify added quadpoint set
421 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
422 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
423 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
424 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
425 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
426 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
427 }
428
429 {
430 // Append a new set of quadpoints.
431 FS_QUADPOINTSF new_quadpoints = quadpoints;
432 new_quadpoints.y1 += 20.f;
433 new_quadpoints.y2 += 20.f;
434 new_quadpoints.y3 += 20.f;
435 new_quadpoints.y4 += 20.f;
436 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
437
438 // Verify added quadpoint set
439 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
440 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
441 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
442 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
443 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
444 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
445 }
446
447 {
448 // Setting and getting quadpoints at out-of-bound index should fail
449 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
450 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
451 }
452
453 FPDFPage_CloseAnnot(annot);
454
455 // Retrieve the square annotation
456 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
457
458 {
459 // Check that attempting to set its quadpoints would fail
460 ASSERT_TRUE(squareAnnot);
461 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
462 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
463 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
464 }
465
466 FPDFPage_CloseAnnot(squareAnnot);
467
468 UnloadPage(page);
469}
470
Jane Liu06462752017-06-27 16:41:14 -0400471TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400472#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400473 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
474 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
475 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400476#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000477 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
478 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
479 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400480#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000481 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
482 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
483 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400484#endif
485
Jane Liu06462752017-06-27 16:41:14 -0400486 // Open a file with four annotations and load its first page.
487 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000488 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400489 ASSERT_TRUE(page);
490 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
491
Jane Liub370e5a2017-08-16 13:24:58 -0400492 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000493 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000494 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000495 CompareBitmap(bitmap.get(), 612, 792, md5_original);
496 }
Jane Liub370e5a2017-08-16 13:24:58 -0400497
Jane Liu0c6b07d2017-08-15 10:50:22 -0400498 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400499 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000500
501 // Retrieve the highlight annotation which has its AP stream already defined.
502 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000503 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000504 ASSERT_TRUE(annot);
505 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
506
507 // Check that color cannot be set when an AP stream is defined already.
508 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
509 102, 153, 204));
510
511 // Verify its attachment points.
512 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000513 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000514 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
515 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
516 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
517 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
518
519 // Check that updating the attachment points would succeed.
520 quadpoints.x1 -= 50.f;
521 quadpoints.x2 -= 50.f;
522 quadpoints.x3 -= 50.f;
523 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000524 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000526 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
528 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
529 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
530 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
531
532 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000533 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000534 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000535 CompareBitmap(bitmap.get(), 612, 792, md5_original);
536 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000537
538 // Verify its annotation rectangle.
539 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
540 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
541 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
542 EXPECT_NEAR(136.325f, rect.right, 0.001f);
543 EXPECT_NEAR(721.292f, rect.top, 0.001f);
544
545 // Check that updating the rectangle would succeed.
546 rect.left -= 60.f;
547 rect.right -= 60.f;
548 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
549 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
550 EXPECT_EQ(rect.right, new_rect.right);
551 }
Jane Liu06462752017-06-27 16:41:14 -0400552
Jane Liub370e5a2017-08-16 13:24:58 -0400553 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000554 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000555 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000556 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
557 }
Jane Liub370e5a2017-08-16 13:24:58 -0400558
Lei Zhanga21d5932018-02-05 18:28:38 +0000559 {
560 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000561 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000562 ASSERT_TRUE(annot);
563 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400564
Lei Zhanga21d5932018-02-05 18:28:38 +0000565 // Check that updating the rectangle would succeed.
566 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
567 rect.left += 70.f;
568 rect.right += 70.f;
569 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
570 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
571 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400572
Lei Zhanga21d5932018-02-05 18:28:38 +0000573 // Check that updating the rectangle changes the square annotation's
574 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000575 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000576 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000577 }
Jane Liub370e5a2017-08-16 13:24:58 -0400578
Jane Liu06462752017-06-27 16:41:14 -0400579 UnloadPage(page);
580}
Jane Liu8ce58f52017-06-29 13:40:22 -0400581
Henrique Nakashima5098b252018-03-26 21:46:00 +0000582TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
583 // Open a file with multiline markup annotations.
584 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
585 FPDF_PAGE page = LoadPage(0);
586 ASSERT_TRUE(page);
587 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000589 ASSERT_TRUE(annot);
590
591 // This is a three line annotation.
592 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
593 }
594 UnloadPage(page);
595
596 // null annotation should return 0
597 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
598}
599
Jane Liu8ce58f52017-06-29 13:40:22 -0400600TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
601 // Open a file with 3 annotations on its first page.
602 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000603 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
604 // the FORM API calls in LoadPage() makes this test fail. So use
605 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400606 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
607 ASSERT_TRUE(page);
608 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
609
Jane Liu0c6b07d2017-08-15 10:50:22 -0400610 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400611
Lei Zhanga21d5932018-02-05 18:28:38 +0000612 // Check that the annotations have the expected rectangle coordinates.
613 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000614 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
616 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
617 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400618
Lei Zhanga21d5932018-02-05 18:28:38 +0000619 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000620 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000621 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
622 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
623 }
624
625 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000626 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000627 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
628 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
629 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400630
631 // Check that nothing happens when attempting to remove an annotation with an
632 // out-of-bound index.
633 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
634 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
635 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
636
637 // Remove the second annotation.
638 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
639 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
640 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
641
642 // Save the document, closing the page and document.
643 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
644 FPDF_ClosePage(page);
645
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400646 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400647 // Open the saved document.
648 std::string new_file = GetString();
649 FPDF_FILEACCESS file_access;
650 memset(&file_access, 0, sizeof(file_access));
651 file_access.m_FileLen = new_file.size();
652 file_access.m_GetBlock = GetBlockFromString;
653 file_access.m_Param = &new_file;
654 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
655 ASSERT_TRUE(new_doc);
656 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
657 ASSERT_TRUE(new_page);
658
659 // Check that the saved document has 2 annotations on the first page.
660 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
661
Lei Zhanga21d5932018-02-05 18:28:38 +0000662 // Check that the remaining 2 annotations are the original 1st and 3rd ones
663 // by verifying their rectangle coordinates.
664 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000665 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000666 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
667 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
668 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400669
Lei Zhanga21d5932018-02-05 18:28:38 +0000670 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000671 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
673 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
674 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400675 FPDF_ClosePage(new_page);
676 FPDF_CloseDocument(new_doc);
677}
Jane Liu8ce58f52017-06-29 13:40:22 -0400678
Jane Liubaa7ff42017-06-29 19:18:23 -0400679TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400680#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400681 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000682 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
683 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
684 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000685#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
686 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
687 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
688 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
689 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400690#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000691 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000692 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
693 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
694 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400695#endif
696
697 // Open a file with two annotations and load its first page.
698 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000699 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400700 ASSERT_TRUE(page);
701 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
702
703 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000704 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000705 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000706 CompareBitmap(bitmap.get(), 595, 842, md5_original);
707 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400708
Lei Zhanga21d5932018-02-05 18:28:38 +0000709 {
710 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000711 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000712 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400713
Lei Zhanga21d5932018-02-05 18:28:38 +0000714 // Check that this annotation has one path object and retrieve it.
715 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000716 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000717 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
718 EXPECT_FALSE(path);
719 path = FPDFAnnot_GetObject(annot.get(), 0);
720 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
721 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400722
Lei Zhanga21d5932018-02-05 18:28:38 +0000723 // Modify the color of the path object.
724 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
725 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400726
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000728 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000729 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000730 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
731 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400732
Lei Zhanga21d5932018-02-05 18:28:38 +0000733 // Add a second path object to the same annotation.
734 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
735 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
736 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
737 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
738 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
739 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
740 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400741
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000742 // The object is in the annontation, not in the page, so the page object
743 // array should not change.
744 ASSERT_EQ(32, FPDFPage_CountObjects(page));
745
Lei Zhanga21d5932018-02-05 18:28:38 +0000746 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000747 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000748 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000749 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
750 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400751
Lei Zhanga21d5932018-02-05 18:28:38 +0000752 // Delete the newly added path object.
753 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
754 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000755 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000756 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400757
758 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000759 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000760 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000761 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
762 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400763
Jane Liubaa7ff42017-06-29 19:18:23 -0400764 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400765
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 {
767 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000768 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000769 ASSERT_TRUE(annot);
770 rect.left = 200.f;
771 rect.bottom = 400.f;
772 rect.right = 500.f;
773 rect.top = 600.f;
774 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400775
Lei Zhanga21d5932018-02-05 18:28:38 +0000776 // Add a new path to the annotation.
777 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
778 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
779 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
780 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
781 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
782 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
783 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
784 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
785 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
786 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
787
788 // Check that the annotation's bounding box came from its rectangle.
789 FS_RECTF new_rect;
790 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
791 EXPECT_EQ(rect.left, new_rect.left);
792 EXPECT_EQ(rect.bottom, new_rect.bottom);
793 EXPECT_EQ(rect.right, new_rect.right);
794 EXPECT_EQ(rect.top, new_rect.top);
795 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400796
797 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400798 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000799 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400800
801 // Open the saved document.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000802 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000803 page = LoadSavedPage(0);
804 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400805
Jane Liu36567742017-07-06 11:13:35 -0400806 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000807 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400808
Lei Zhanga21d5932018-02-05 18:28:38 +0000809 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000810 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000811 ASSERT_TRUE(annot);
812 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400813
Lei Zhanga21d5932018-02-05 18:28:38 +0000814 // Check that the new annotation's rectangle is as defined.
815 FS_RECTF new_rect;
816 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
817 EXPECT_EQ(rect.left, new_rect.left);
818 EXPECT_EQ(rect.bottom, new_rect.bottom);
819 EXPECT_EQ(rect.right, new_rect.right);
820 EXPECT_EQ(rect.top, new_rect.top);
821 }
822
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000823 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400824 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400825}
Jane Liub137e752017-07-05 15:04:33 -0400826
827TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
828 // Open a file with an annotation and load its first page.
829 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000830 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400831 ASSERT_TRUE(page);
832
833 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000834 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000835 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000836 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
837 }
Jane Liub137e752017-07-05 15:04:33 -0400838
Lei Zhanga21d5932018-02-05 18:28:38 +0000839 {
840 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000841 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000842 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400843
Lei Zhanga21d5932018-02-05 18:28:38 +0000844 // Check that the original flag values are as expected.
845 int flags = FPDFAnnot_GetFlags(annot.get());
846 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
847 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 // Set the HIDDEN flag.
850 flags |= FPDF_ANNOT_FLAG_HIDDEN;
851 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
852 flags = FPDFAnnot_GetFlags(annot.get());
853 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
854 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400855
Lei Zhanga21d5932018-02-05 18:28:38 +0000856 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000857 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000858 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000859 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
860 }
Jane Liub137e752017-07-05 15:04:33 -0400861
Lei Zhanga21d5932018-02-05 18:28:38 +0000862 // Unset the HIDDEN flag.
863 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
864 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
865 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
866 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
867 flags = FPDFAnnot_GetFlags(annot.get());
868 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
869 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400870
Lei Zhanga21d5932018-02-05 18:28:38 +0000871 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000872 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000873 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000874 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
875 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000876 }
Jane Liub137e752017-07-05 15:04:33 -0400877
Jane Liub137e752017-07-05 15:04:33 -0400878 UnloadPage(page);
879}
Jane Liu36567742017-07-06 11:13:35 -0400880
881TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400882#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400883 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
884 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
885 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000886#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
887 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
888 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
889 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400890#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000891 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
892 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
893 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400894#endif
895
896 // Open a file with two annotations and load its first page.
897 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000898 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400899 ASSERT_TRUE(page);
900 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
901
902 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000903 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000904 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000905 CompareBitmap(bitmap.get(), 595, 842, md5_original);
906 }
Jane Liu36567742017-07-06 11:13:35 -0400907
Jane Liu36567742017-07-06 11:13:35 -0400908 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000909 FPDF_BITMAP image_bitmap;
910
911 {
912 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000913 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000914 ASSERT_TRUE(annot);
915 FS_RECTF rect;
916 rect.left = 200.f;
917 rect.bottom = 600.f;
918 rect.right = 400.f;
919 rect.top = 800.f;
920 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
921
922 // Add a solid-color translucent image object to the new annotation.
923 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
924 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
925 0xeeeecccc);
926 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
927 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
928 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
929 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
930 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
931 kBitmapSize, 0, 0));
932 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
933 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
934 }
Jane Liu36567742017-07-06 11:13:35 -0400935
936 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000937 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000938 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000939 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
940 }
Jane Liu36567742017-07-06 11:13:35 -0400941
Lei Zhanga21d5932018-02-05 18:28:38 +0000942 {
943 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000944 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000945 ASSERT_TRUE(annot);
946 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
947 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
948 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400949
Lei Zhanga21d5932018-02-05 18:28:38 +0000950 // Modify the image in the new annotation.
951 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
952 0xff000000);
953 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
954 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
955 }
Jane Liu36567742017-07-06 11:13:35 -0400956
957 // Save the document, closing the page and document.
958 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000959 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400960 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400961
962 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400963 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400964}
965
966TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400967#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400968 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
969 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
970 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000971#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
972 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
973 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
974 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400975#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000976 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
977 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
978 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400979#endif
980
981 // Open a file with two annotations and load its first page.
982 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000983 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400984 ASSERT_TRUE(page);
985 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
986
987 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000988 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000989 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000990 CompareBitmap(bitmap.get(), 595, 842, md5_original);
991 }
Jane Liu36567742017-07-06 11:13:35 -0400992
Lei Zhanga21d5932018-02-05 18:28:38 +0000993 {
994 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000995 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000996 ASSERT_TRUE(annot);
997 FS_RECTF rect;
998 rect.left = 200.f;
999 rect.bottom = 550.f;
1000 rect.right = 450.f;
1001 rect.top = 650.f;
1002 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001003
Lei Zhanga21d5932018-02-05 18:28:38 +00001004 // Add a translucent text object to the new annotation.
1005 FPDF_PAGEOBJECT text_object =
1006 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1007 EXPECT_TRUE(text_object);
1008 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1009 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1010 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1011 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1012 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1013 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1014 }
Jane Liu36567742017-07-06 11:13:35 -04001015
1016 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001017 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001018 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001019 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1020 }
Jane Liu36567742017-07-06 11:13:35 -04001021
Lei Zhanga21d5932018-02-05 18:28:38 +00001022 {
1023 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001024 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001025 ASSERT_TRUE(annot);
1026 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1027 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1028 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001029
Lei Zhanga21d5932018-02-05 18:28:38 +00001030 // Modify the text in the new annotation.
1031 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1032 GetFPDFWideString(L"New text!");
1033 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1034 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1035 }
Jane Liu36567742017-07-06 11:13:35 -04001036
1037 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001038 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001039 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001040 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1041 }
Jane Liu36567742017-07-06 11:13:35 -04001042
1043 // Remove the new annotation, and check that the page renders as before.
1044 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001045 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001046 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001047 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1048 }
Jane Liu36567742017-07-06 11:13:35 -04001049
1050 UnloadPage(page);
1051}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052
1053TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1054 // Open a file with four annotations and load its first page.
1055 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001056 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001057 ASSERT_TRUE(page);
1058
Lei Zhangdf064df2017-08-31 02:33:27 -07001059 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001061
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 {
1063 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001064 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001065 ASSERT_TRUE(annot);
1066
1067 // Check that a non-existent key does not exist.
1068 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1069
1070 // Check that the string value of a non-string dictionary entry is empty.
1071 static constexpr char kApKey[] = "AP";
1072 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1073 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1074 FPDFAnnot_GetValueType(annot.get(), kApKey));
1075 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1076
1077 // Check that the string value of the hash is correct.
1078 static constexpr char kHashKey[] = "AAPL:Hash";
1079 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1080 unsigned long len =
1081 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1082 std::vector<char> buf(len);
1083 EXPECT_EQ(66u,
1084 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1085 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1086 BufferToWString(buf).c_str());
1087
1088 // Check that the string value of the modified date is correct.
1089 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1090 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1091 buf.clear();
1092 buf.resize(len);
1093 EXPECT_EQ(44u,
1094 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1095 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1096
1097 // Update the date entry for the annotation.
1098 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1099 GetFPDFWideString(kNewDate);
1100 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1101 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001102
1103 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001104 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001105 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001106
Dan Sinclair698aed72017-09-26 16:24:49 -04001107#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001108 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001109#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1110 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001111#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001112 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001113#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001114
1115 // Open the saved annotation.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001116 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001117 page = LoadSavedPage(0);
1118 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001119 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001120 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001121
Lei Zhanga21d5932018-02-05 18:28:38 +00001122 // Check that the string value of the modified date is the newly-set value.
1123 EXPECT_EQ(FPDF_OBJECT_STRING,
1124 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1125 unsigned long len =
1126 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1127 std::vector<char> buf(len);
1128 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1129 buf.data(), len));
1130 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1131 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001132
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001133 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001134 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001135}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001136
Henrique Nakashima5970a472018-01-11 22:40:59 +00001137TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001138 // Open a file with four annotations and load its first page.
1139 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001140 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001141 ASSERT_TRUE(page);
1142
Lei Zhanga21d5932018-02-05 18:28:38 +00001143 {
1144 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001145 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001146 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001147
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 // Check that the string value of an AP returns the expected length.
1149 unsigned long normal_len = FPDFAnnot_GetAP(
1150 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1151 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001152
Lei Zhanga21d5932018-02-05 18:28:38 +00001153 // Check that the string value of an AP is not returned if the buffer is too
1154 // small. The result buffer should be overwritten with an empty string.
1155 std::vector<char> buf(normal_len - 1);
1156 // Write L"z" in the buffer to verify it's not overwritten.
1157 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1158 EXPECT_EQ(73970u,
1159 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1160 buf.data(), buf.size()));
1161 std::string ap = BufferToString(buf);
1162 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001163
Lei Zhanga21d5932018-02-05 18:28:38 +00001164 // Check that the string value of an AP is returned through a buffer that is
1165 // the right size.
1166 buf.clear();
1167 buf.resize(normal_len);
1168 EXPECT_EQ(73970u,
1169 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1170 buf.data(), buf.size()));
1171 ap = BufferToString(buf);
1172 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1173 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001174
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 // Check that the string value of an AP is returned through a buffer that is
1176 // larger than necessary.
1177 buf.clear();
1178 buf.resize(normal_len + 1);
1179 EXPECT_EQ(73970u,
1180 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1181 buf.data(), buf.size()));
1182 ap = BufferToString(buf);
1183 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1184 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 // Check that getting an AP for a mode that does not have an AP returns an
1187 // empty string.
1188 unsigned long rollover_len = FPDFAnnot_GetAP(
1189 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1190 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001191
Lei Zhanga21d5932018-02-05 18:28:38 +00001192 buf.clear();
1193 buf.resize(1000);
1194 EXPECT_EQ(2u,
1195 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1196 buf.data(), buf.size()));
1197 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 // Check that setting the AP for an invalid appearance mode fails.
1200 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1201 GetFPDFWideString(L"new test ap");
1202 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1203 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1204 apText.get()));
1205 EXPECT_FALSE(FPDFAnnot_SetAP(
1206 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001207
Lei Zhanga21d5932018-02-05 18:28:38 +00001208 // Set the AP correctly now.
1209 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1210 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001211
Lei Zhanga21d5932018-02-05 18:28:38 +00001212 // Check that the new annotation value is equal to the value we just set.
1213 rollover_len = FPDFAnnot_GetAP(
1214 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1215 EXPECT_EQ(24u, rollover_len);
1216 buf.clear();
1217 buf.resize(rollover_len);
1218 EXPECT_EQ(24u,
1219 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1220 buf.data(), buf.size()));
1221 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001222
Lei Zhanga21d5932018-02-05 18:28:38 +00001223 // Check that the Normal AP was not touched when the Rollover AP was set.
1224 buf.clear();
1225 buf.resize(normal_len);
1226 EXPECT_EQ(73970u,
1227 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1228 buf.data(), buf.size()));
1229 ap = BufferToString(buf);
1230 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1231 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1232 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001233
1234 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001235 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001236 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001237
Tom Sepezb9c3e272018-08-14 18:22:06 +00001238 OpenSavedDocument(nullptr);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001239 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001240 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001241 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001242
Lei Zhanga21d5932018-02-05 18:28:38 +00001243 // Check that the new annotation value is equal to the value we set before
1244 // saving.
1245 unsigned long rollover_len = FPDFAnnot_GetAP(
1246 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1247 EXPECT_EQ(24u, rollover_len);
1248 std::vector<char> buf(rollover_len);
1249 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1250 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1251 buf.data(), buf.size()));
1252 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1253 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001254
1255 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001256 CloseSavedPage(page);
1257 CloseSavedDocument();
1258}
1259
1260TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1261 // Open a file with four annotations and load its first page.
1262 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001263 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001264 ASSERT_TRUE(page);
1265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 {
1267 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001268 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001269 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001270
Lei Zhanga21d5932018-02-05 18:28:38 +00001271 // Set Down AP. Normal AP is already set.
1272 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1273 GetFPDFWideString(L"new test ap");
1274 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1275 apText.get()));
1276 EXPECT_EQ(73970u,
1277 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1278 nullptr, 0));
1279 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1280 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001281
Lei Zhanga21d5932018-02-05 18:28:38 +00001282 // Check that setting the Down AP to null removes the Down entry but keeps
1283 // Normal intact.
1284 EXPECT_TRUE(
1285 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1286 EXPECT_EQ(73970u,
1287 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1288 nullptr, 0));
1289 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1290 nullptr, 0));
1291 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001292
Lei Zhang75c81712018-02-08 17:22:39 +00001293 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001294}
1295
1296TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1297 // Open a file with four annotations and load its first page.
1298 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001299 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001300 ASSERT_TRUE(page);
1301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 {
1303 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001304 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001305 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001306
Lei Zhanga21d5932018-02-05 18:28:38 +00001307 // Set Down AP. Normal AP is already set.
1308 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1309 GetFPDFWideString(L"new test ap");
1310 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1311 apText.get()));
1312 EXPECT_EQ(73970u,
1313 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1314 nullptr, 0));
1315 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1316 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001317
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 // Check that setting the Normal AP to null removes the whole AP dictionary.
1319 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1320 nullptr));
1321 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1322 nullptr, 0));
1323 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1324 nullptr, 0));
1325 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001326
Lei Zhang75c81712018-02-08 17:22:39 +00001327 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001328}
1329
Jane Liu300bb272017-08-21 14:37:53 -04001330TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1331 // Open a file with annotations and load its first page.
1332 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001333 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001334 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001335 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001336
Lei Zhanga21d5932018-02-05 18:28:38 +00001337 {
1338 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001339 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001340 ASSERT_TRUE(annot);
1341 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1342 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1343 static constexpr char kPopupKey[] = "Popup";
1344 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1345 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1346 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001347
Lei Zhanga21d5932018-02-05 18:28:38 +00001348 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001349 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001350 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1351 ASSERT_TRUE(popup);
1352 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1353 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1354 FS_RECTF rect;
1355 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1356 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1357 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1360 // since "IRT" is not a key in |annot|'s dictionary.
1361 static constexpr char kIRTKey[] = "IRT";
1362 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1363 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001364
Lei Zhanga21d5932018-02-05 18:28:38 +00001365 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1366 // would fail, since its parent is not an annotation.
1367 static constexpr char kPKey[] = "P";
1368 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1369 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1370 FPDFAnnot_GetValueType(annot.get(), kPKey));
1371 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1372 }
Jane Liu300bb272017-08-21 14:37:53 -04001373
Jane Liu300bb272017-08-21 14:37:53 -04001374 UnloadPage(page);
1375}
1376
Diana Gage7e0c05d2017-07-19 17:33:33 -07001377TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1378 // Open file with form text fields.
1379 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001380 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001381 ASSERT_TRUE(page);
1382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 {
1384 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001385 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001386 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001387
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 // Check that the flag values are as expected.
1389 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1390 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1391 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 {
1394 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001395 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001396 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001397
Lei Zhanga21d5932018-02-05 18:28:38 +00001398 // Check that the flag values are as expected.
1399 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1400 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1401 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001402
1403 UnloadPage(page);
1404}
1405
1406TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1407 // Open file with form text fields.
1408 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001409 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001410 ASSERT_TRUE(page);
1411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 {
1413 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001414 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001415 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001416
Lei Zhanga21d5932018-02-05 18:28:38 +00001417 // Check that the flag values are as expected.
1418 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1419 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1420 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1421 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1422 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 {
1425 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001426 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001427 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001428
Lei Zhanga21d5932018-02-05 18:28:38 +00001429 // Check that the flag values are as expected.
1430 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1431 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1432 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1433 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1434 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 {
1437 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001438 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001440
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 // Check that the flag values are as expected.
1442 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1443 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1444 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1445 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1446 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001447
1448 UnloadPage(page);
1449}
Diana Gage40870db2017-07-19 18:16:03 -07001450
1451TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1452 // Open file with form text fields.
1453 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1454 FPDF_PAGE page = LoadPage(0);
1455 ASSERT_TRUE(page);
1456
1457 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001458 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1459
1460 {
1461 // Verify there is an annotation.
1462 ScopedFPDFAnnotation annot(
1463 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1464 EXPECT_TRUE(annot);
1465 }
1466
1467 // Try other bad inputs at a valid location.
1468 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1469 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1470 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001471
1472 UnloadPage(page);
1473}
1474
1475TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1476 // Open file with form text fields.
1477 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1478 FPDF_PAGE page = LoadPage(0);
1479 ASSERT_TRUE(page);
1480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 {
1482 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001483 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1485 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001486
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 // Check that interactive form annotation flag values are as expected.
1488 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1489 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1490 }
Diana Gage40870db2017-07-19 18:16:03 -07001491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 {
1493 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001494 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1496 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001497
Lei Zhanga21d5932018-02-05 18:28:38 +00001498 // Check that interactive form annotation flag values are as expected.
1499 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1500 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1501 }
Diana Gage40870db2017-07-19 18:16:03 -07001502
1503 UnloadPage(page);
1504}
1505
1506TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1507 // Open file with form comboboxes.
1508 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1509 FPDF_PAGE page = LoadPage(0);
1510 ASSERT_TRUE(page);
1511
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 {
1513 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001514 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001515 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001516 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001517
Lei Zhanga21d5932018-02-05 18:28:38 +00001518 // Check that interactive form annotation flag values are as expected.
1519 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1520 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1521 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1522 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1523 }
Diana Gage40870db2017-07-19 18:16:03 -07001524
Lei Zhanga21d5932018-02-05 18:28:38 +00001525 {
1526 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001527 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001528 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001529 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001530
Lei Zhanga21d5932018-02-05 18:28:38 +00001531 // Check that interactive form annotation flag values are as expected.
1532 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1533 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1534 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1535 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1536 }
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 {
1539 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001540 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001541 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001542 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001543
Lei Zhanga21d5932018-02-05 18:28:38 +00001544 // Check that interactive form annotation flag values are as expected.
1545 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1546 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1547 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1548 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1549 }
Diana Gage40870db2017-07-19 18:16:03 -07001550
1551 UnloadPage(page);
1552}