blob: 59dcd99a720a73a31c2967e38d2c8068a71aaa39 [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
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00005#include <algorithm>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00006#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04007#include <memory>
8#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04009#include <vector>
10
Lei Zhanga5c1daf2019-01-31 21:56:47 +000011#include "constants/annotation_common.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040012#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000013#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040015#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040016#include "public/fpdfview.h"
17#include "testing/embedder_test.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000018#include "testing/fx_string_testhelpers.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000019#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040020#include "testing/gtest/include/gtest/gtest.h"
21
Lei Zhangab41f252018-12-23 03:10:50 +000022class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040023
Lei Zhang4afee172018-01-10 20:57:15 +000024std::wstring BufferToWString(const std::vector<char>& buf) {
25 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000026}
27
Lei Zhang4afee172018-01-10 20:57:15 +000028std::string BufferToString(const std::vector<char>& buf) {
29 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030}
31
Lei Zhangab41f252018-12-23 03:10:50 +000032TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000033 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
34 FPDF_PAGE page = LoadPage(0);
35 ASSERT_TRUE(page);
36
37 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
38
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
43 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
44
45 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
46
47 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
48
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
51 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
52
53 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
54
55 static constexpr wchar_t kContents[] = L"Bar";
56 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
57 GetFPDFWideString(kContents);
58 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
59
60 char buffer[128];
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
62 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
63 EXPECT_EQ(0u,
64 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
65
66 UnloadPage(page);
67}
68
Lei Zhang3d9a0972019-03-04 19:34:09 +000069TEST_F(FPDFAnnotEmbedderTest, BadAnnotsEntry) {
70 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
71 FPDF_PAGE page = LoadPage(0);
72 ASSERT_TRUE(page);
73
74 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Lei Zhang98dc8c02019-03-04 19:40:30 +000075 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
Lei Zhang3d9a0972019-03-04 19:34:09 +000076
77 UnloadPage(page);
78}
79
Lei Zhangab41f252018-12-23 03:10:50 +000080TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040081 // Open a file with one annotation and load its first page.
82 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000083 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040084 ASSERT_TRUE(page);
85
86 // This annotation has a malformed appearance stream, which does not have its
87 // normal appearance defined, only its rollover appearance. In this case, its
88 // normal appearance should be generated, allowing the highlight annotation to
89 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000090 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000091 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040092
93 UnloadPage(page);
94}
95
Lei Zhangab41f252018-12-23 03:10:50 +000096TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Ralf Sipplb3a52402018-03-19 23:30:28 +000097 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000098 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000099 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
100 FPDF_PAGE page = LoadPage(0);
101 ASSERT_TRUE(page);
102
Tom Sepeze08d2b12018-04-25 18:49:32 +0000103 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +0000104 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
105
106 UnloadPage(page);
107}
108
Lei Zhangab41f252018-12-23 03:10:50 +0000109TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400110 // Open a file with one annotation and load its first page.
111 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000112 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400113 ASSERT_TRUE(page);
114
115 // Check that there is a total of 1 annotation on its first page.
116 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
117
118 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000119 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000120 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000121 ASSERT_TRUE(annot);
122 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400123
Lei Zhanga21d5932018-02-05 18:28:38 +0000124 // Check that the annotation color is yellow.
125 unsigned int R;
126 unsigned int G;
127 unsigned int B;
128 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000129 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000130 &G, &B, &A));
131 EXPECT_EQ(255u, R);
132 EXPECT_EQ(255u, G);
133 EXPECT_EQ(0u, B);
134 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400135
Lei Zhanga21d5932018-02-05 18:28:38 +0000136 // Check that the author is correct.
137 static constexpr char kAuthorKey[] = "T";
138 EXPECT_EQ(FPDF_OBJECT_STRING,
139 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
140 unsigned long len =
141 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
142 std::vector<char> buf(len);
143 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
144 len));
145 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400146
Lei Zhanga21d5932018-02-05 18:28:38 +0000147 // Check that the content is correct.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000148 EXPECT_EQ(
149 FPDF_OBJECT_STRING,
150 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
151 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kContents,
152 nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000153 buf.clear();
154 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000155 EXPECT_EQ(2690u,
156 FPDFAnnot_GetStringValue(
157 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 const wchar_t contents[] =
159 L"This is a note for that highlight annotation. Very long highlight "
160 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
167 "longLong long longLong long longLong long longLong long longLong long "
168 "longLong long longLong long longLong long longLong long longLong long "
169 "longLong long longLong long longLong long longLong long longLong long "
170 "longLong long longLong long longLong long longLong long longLong long "
171 "longLong long longLong long longLong long longLong long longLong long "
172 "longLong long longLong long longLong long longLong long longLong long "
173 "longLong long longLong long longLong long longLong long longLong long "
174 "longLong long longLong long longLong long longLong long longLong long "
175 "longLong long longLong long longLong long longLong long longLong long "
176 "longLong long longLong long longLong long longLong long longLong long "
177 "longLong long longLong long longLong long longLong long longLong long "
178 "longLong long long. END";
179 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400180
Lei Zhanga21d5932018-02-05 18:28:38 +0000181 // Check that the quadpoints are correct.
182 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000183 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000184 EXPECT_EQ(115.802643f, quadpoints.x1);
185 EXPECT_EQ(718.913940f, quadpoints.y1);
186 EXPECT_EQ(157.211182f, quadpoints.x4);
187 EXPECT_EQ(706.264465f, quadpoints.y4);
188 }
Tom Sepez507d0192018-11-07 16:37:51 +0000189 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400190}
191
Lei Zhangab41f252018-12-23 03:10:50 +0000192TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400193 // Open a file with three annotations and load its first page.
194 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000195 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400196 ASSERT_TRUE(page);
197
198 // Check that there is a total of 3 annotation on its first page.
199 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
200
Lei Zhanga21d5932018-02-05 18:28:38 +0000201 {
202 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000203 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 ASSERT_TRUE(annot);
205 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400206
Lei Zhanga21d5932018-02-05 18:28:38 +0000207 // Check that the annotation color is blue with opacity.
208 unsigned int R;
209 unsigned int G;
210 unsigned int B;
211 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000212 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 &G, &B, &A));
214 EXPECT_EQ(0u, R);
215 EXPECT_EQ(0u, G);
216 EXPECT_EQ(255u, B);
217 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400218
Lei Zhanga21d5932018-02-05 18:28:38 +0000219 // Check that there is no content.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000220 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
221 annot.get(), pdfium::annotation::kContents, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400222
Lei Zhanga21d5932018-02-05 18:28:38 +0000223 // Check that the rectange coordinates are correct.
224 // Note that upon rendering, the rectangle coordinates will be adjusted.
225 FS_RECTF rect;
226 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
227 EXPECT_EQ(351.820404f, rect.left);
228 EXPECT_EQ(583.830688f, rect.bottom);
229 EXPECT_EQ(475.336090f, rect.right);
230 EXPECT_EQ(681.535034f, rect.top);
231 }
Tom Sepezef43c262018-11-07 16:41:32 +0000232 {
233 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
234 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
235 }
Tom Sepez507d0192018-11-07 16:37:51 +0000236 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400237}
Jane Liu20eafda2017-06-07 10:33:24 -0400238
Lei Zhangab41f252018-12-23 03:10:50 +0000239TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400240 // Open a file with one annotation and load its first page.
241 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000242 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400243 ASSERT_TRUE(page);
244
245 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400246 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400247
248 UnloadPage(page);
249}
250
Lei Zhangab41f252018-12-23 03:10:50 +0000251TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400252 // Open a file with no annotation and load its first page.
253 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000254 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400255 ASSERT_TRUE(page);
256 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
257
Lei Zhanga21d5932018-02-05 18:28:38 +0000258 {
259 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000260 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000261 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400262
Lei Zhanga21d5932018-02-05 18:28:38 +0000263 // Check that there is now 1 annotations on this page.
264 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400265
Lei Zhanga21d5932018-02-05 18:28:38 +0000266 // Check that the subtype of the annotation is correct.
267 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
268 }
Jane Liue10509a2017-06-20 16:47:41 -0400269
Lei Zhanga21d5932018-02-05 18:28:38 +0000270 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000271 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000272 ASSERT_TRUE(annot);
273 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400274
Lei Zhanga21d5932018-02-05 18:28:38 +0000275 // Set the color of the annotation.
276 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
277 102, 153, 204));
278 // Check that the color has been set correctly.
279 unsigned int R;
280 unsigned int G;
281 unsigned int B;
282 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000283 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000284 &G, &B, &A));
285 EXPECT_EQ(51u, R);
286 EXPECT_EQ(102u, G);
287 EXPECT_EQ(153u, B);
288 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400289
Lei Zhanga21d5932018-02-05 18:28:38 +0000290 // Change the color of the annotation.
291 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
292 153, 102, 51));
293 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000294 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000295 &G, &B, &A));
296 EXPECT_EQ(204u, R);
297 EXPECT_EQ(153u, G);
298 EXPECT_EQ(102u, B);
299 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400300
Lei Zhanga21d5932018-02-05 18:28:38 +0000301 // Set the annotation rectangle.
302 FS_RECTF rect;
303 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
304 EXPECT_EQ(0.f, rect.left);
305 EXPECT_EQ(0.f, rect.right);
306 rect.left = 35;
307 rect.bottom = 150;
308 rect.right = 53;
309 rect.top = 165;
310 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
311 // Check that the annotation rectangle has been set correctly.
312 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
313 EXPECT_EQ(35.f, rect.left);
314 EXPECT_EQ(150.f, rect.bottom);
315 EXPECT_EQ(53.f, rect.right);
316 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400317
Lei Zhanga21d5932018-02-05 18:28:38 +0000318 // Set the content of the annotation.
319 static constexpr wchar_t kContents[] =
320 L"Hello! This is a customized content.";
321 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
322 GetFPDFWideString(kContents);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000323 ASSERT_TRUE(FPDFAnnot_SetStringValue(
324 annot.get(), pdfium::annotation::kContents, text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +0000325 // Check that the content has been set correctly.
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000326 unsigned long len = FPDFAnnot_GetStringValue(
327 annot.get(), pdfium::annotation::kContents, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +0000328 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +0000329 EXPECT_EQ(74u,
330 FPDFAnnot_GetStringValue(
331 annot.get(), pdfium::annotation::kContents, buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +0000332 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
333 }
Jane Liu20eafda2017-06-07 10:33:24 -0400334 UnloadPage(page);
335}
336
Lei Zhangab41f252018-12-23 03:10:50 +0000337TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400338 // Open a file with one annotation and load its first page.
339 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000340 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400341 ASSERT_TRUE(page);
342
343 // Check that there is a total of one annotation on its first page, and verify
344 // its quadpoints.
345 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400346 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000347 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000348 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000349 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000350 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000351 EXPECT_EQ(115.802643f, quadpoints.x1);
352 EXPECT_EQ(718.913940f, quadpoints.y1);
353 EXPECT_EQ(157.211182f, quadpoints.x4);
354 EXPECT_EQ(706.264465f, quadpoints.y4);
355 }
Jane Liu20eafda2017-06-07 10:33:24 -0400356
357 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000358 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000359 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000360 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
361 ASSERT_TRUE(annot);
362 quadpoints.x1 = 140.802643f;
363 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000364 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 }
Jane Liu20eafda2017-06-07 10:33:24 -0400366
367 // Save the document, closing the page and document.
368 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000369 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400370
371 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000372 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400373
Lei Zhang0b494052019-01-31 21:41:15 +0000374 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000375 page = LoadSavedPage(0);
376 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400377
378 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000379 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400380
Lei Zhanga21d5932018-02-05 18:28:38 +0000381 {
382 // Check that the second annotation is an underline annotation and verify
383 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000384 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000385 ASSERT_TRUE(new_annot);
386 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
387 FS_QUADPOINTSF new_quadpoints;
388 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000389 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000390 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
391 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
392 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
393 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
394 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400395
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000396 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400397 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400398}
Jane Liu06462752017-06-27 16:41:14 -0400399
Lei Zhangab41f252018-12-23 03:10:50 +0000400TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000401 // Open a file with four annotations and load its first page.
402 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
403 FPDF_PAGE page = LoadPage(0);
404 ASSERT_TRUE(page);
405 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
406
407 // Retrieve the highlight annotation.
408 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
409 ASSERT_TRUE(annot);
410 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
411
412 FS_QUADPOINTSF quadpoints;
413 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
414
415 {
416 // Verify the current one set of quadpoints.
417 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
418
419 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
420 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
421 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
422 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
423 }
424
425 {
426 // Update the quadpoints.
427 FS_QUADPOINTSF new_quadpoints = quadpoints;
428 new_quadpoints.y1 -= 20.f;
429 new_quadpoints.y2 -= 20.f;
430 new_quadpoints.y3 -= 20.f;
431 new_quadpoints.y4 -= 20.f;
432 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
433
434 // Verify added quadpoint set
435 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
436 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
437 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
438 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
439 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
440 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
441 }
442
443 {
444 // Append a new set of quadpoints.
445 FS_QUADPOINTSF new_quadpoints = quadpoints;
446 new_quadpoints.y1 += 20.f;
447 new_quadpoints.y2 += 20.f;
448 new_quadpoints.y3 += 20.f;
449 new_quadpoints.y4 += 20.f;
450 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
451
452 // Verify added quadpoint set
453 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
454 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
455 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
456 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
457 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
458 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
459 }
460
461 {
462 // Setting and getting quadpoints at out-of-bound index should fail
463 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
464 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
465 }
466
467 FPDFPage_CloseAnnot(annot);
468
469 // Retrieve the square annotation
470 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
471
472 {
473 // Check that attempting to set its quadpoints would fail
474 ASSERT_TRUE(squareAnnot);
475 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
476 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
477 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
478 }
479
480 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000481 UnloadPage(page);
482}
483
Lei Zhangab41f252018-12-23 03:10:50 +0000484TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400485#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400486 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
487 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
488 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000490 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
491 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
492 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400493#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000494 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
495 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
496 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400497#endif
498
Jane Liu06462752017-06-27 16:41:14 -0400499 // Open a file with four annotations and load its first page.
500 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000501 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400502 ASSERT_TRUE(page);
503 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
504
Jane Liub370e5a2017-08-16 13:24:58 -0400505 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000506 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000507 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000508 CompareBitmap(bitmap.get(), 612, 792, md5_original);
509 }
Jane Liub370e5a2017-08-16 13:24:58 -0400510
Jane Liu0c6b07d2017-08-15 10:50:22 -0400511 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400512 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000513
514 // Retrieve the highlight annotation which has its AP stream already defined.
515 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000516 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000517 ASSERT_TRUE(annot);
518 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
519
520 // Check that color cannot be set when an AP stream is defined already.
521 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
522 102, 153, 204));
523
524 // Verify its attachment points.
525 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000526 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000527 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
528 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
529 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
530 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
531
532 // Check that updating the attachment points would succeed.
533 quadpoints.x1 -= 50.f;
534 quadpoints.x2 -= 50.f;
535 quadpoints.x3 -= 50.f;
536 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000537 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000538 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000539 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
541 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
542 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
543 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
544
545 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000546 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000547 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000548 CompareBitmap(bitmap.get(), 612, 792, md5_original);
549 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000550
551 // Verify its annotation rectangle.
552 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
553 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
554 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
555 EXPECT_NEAR(136.325f, rect.right, 0.001f);
556 EXPECT_NEAR(721.292f, rect.top, 0.001f);
557
558 // Check that updating the rectangle would succeed.
559 rect.left -= 60.f;
560 rect.right -= 60.f;
561 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
562 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
563 EXPECT_EQ(rect.right, new_rect.right);
564 }
Jane Liu06462752017-06-27 16:41:14 -0400565
Jane Liub370e5a2017-08-16 13:24:58 -0400566 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000567 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000568 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000569 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
570 }
Jane Liub370e5a2017-08-16 13:24:58 -0400571
Lei Zhanga21d5932018-02-05 18:28:38 +0000572 {
573 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000574 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 ASSERT_TRUE(annot);
576 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400577
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 // Check that updating the rectangle would succeed.
579 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
580 rect.left += 70.f;
581 rect.right += 70.f;
582 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
583 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
584 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400585
Lei Zhanga21d5932018-02-05 18:28:38 +0000586 // Check that updating the rectangle changes the square annotation's
587 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000588 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000589 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 }
Jane Liub370e5a2017-08-16 13:24:58 -0400591
Jane Liu06462752017-06-27 16:41:14 -0400592 UnloadPage(page);
593}
Jane Liu8ce58f52017-06-29 13:40:22 -0400594
Lei Zhangab41f252018-12-23 03:10:50 +0000595TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000596 // Open a file with multiline markup annotations.
597 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
598 FPDF_PAGE page = LoadPage(0);
599 ASSERT_TRUE(page);
600 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000601 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000602 ASSERT_TRUE(annot);
603
604 // This is a three line annotation.
605 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
606 }
607 UnloadPage(page);
608
609 // null annotation should return 0
610 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
611}
612
Lei Zhangab41f252018-12-23 03:10:50 +0000613TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400614 // Open a file with 3 annotations on its first page.
615 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000616 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400617 ASSERT_TRUE(page);
618 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
619
Jane Liu0c6b07d2017-08-15 10:50:22 -0400620 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400621
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 // Check that the annotations have the expected rectangle coordinates.
623 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000624 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
626 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
627 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400628
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000630 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000631 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
632 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
633 }
634
635 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000636 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000637 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
638 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
639 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400640
641 // Check that nothing happens when attempting to remove an annotation with an
642 // out-of-bound index.
643 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
644 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
645 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
646
647 // Remove the second annotation.
648 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
649 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
650 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
651
652 // Save the document, closing the page and document.
653 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000654 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400655
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400656 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400657 // Open the saved document.
658 std::string new_file = GetString();
659 FPDF_FILEACCESS file_access;
660 memset(&file_access, 0, sizeof(file_access));
661 file_access.m_FileLen = new_file.size();
662 file_access.m_GetBlock = GetBlockFromString;
663 file_access.m_Param = &new_file;
664 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
665 ASSERT_TRUE(new_doc);
666 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
667 ASSERT_TRUE(new_page);
668
669 // Check that the saved document has 2 annotations on the first page.
670 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
671
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 // Check that the remaining 2 annotations are the original 1st and 3rd ones
673 // by verifying their rectangle coordinates.
674 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000675 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000676 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
677 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
678 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400679
Lei Zhanga21d5932018-02-05 18:28:38 +0000680 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000681 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000682 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
683 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
684 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400685 FPDF_ClosePage(new_page);
686 FPDF_CloseDocument(new_doc);
687}
Jane Liu8ce58f52017-06-29 13:40:22 -0400688
Lei Zhangab41f252018-12-23 03:10:50 +0000689TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400690#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400691 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000692 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
693 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
694 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000695#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
696 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
697 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
698 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
699 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400700#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000701 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000702 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
703 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
704 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400705#endif
706
707 // Open a file with two annotations and load its first page.
708 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000709 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400710 ASSERT_TRUE(page);
711 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
712
713 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000714 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000715 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000716 CompareBitmap(bitmap.get(), 595, 842, md5_original);
717 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400718
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 {
720 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000721 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400723
Lei Zhanga21d5932018-02-05 18:28:38 +0000724 // Check that this annotation has one path object and retrieve it.
725 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000726 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
728 EXPECT_FALSE(path);
729 path = FPDFAnnot_GetObject(annot.get(), 0);
730 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
731 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400732
Lei Zhanga21d5932018-02-05 18:28:38 +0000733 // Modify the color of the path object.
734 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
735 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400736
Lei Zhanga21d5932018-02-05 18:28:38 +0000737 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000738 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000739 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000740 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
741 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400742
Lei Zhanga21d5932018-02-05 18:28:38 +0000743 // Add a second path object to the same annotation.
744 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
745 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
746 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
747 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
748 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
749 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
750 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400751
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000752 // The object is in the annontation, not in the page, so the page object
753 // array should not change.
754 ASSERT_EQ(32, FPDFPage_CountObjects(page));
755
Lei Zhanga21d5932018-02-05 18:28:38 +0000756 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000757 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000758 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000759 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
760 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400761
Lei Zhanga21d5932018-02-05 18:28:38 +0000762 // Delete the newly added path object.
763 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
764 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000765 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000766 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400767
768 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000769 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000770 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000771 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
772 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400773
Jane Liubaa7ff42017-06-29 19:18:23 -0400774 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400775
Lei Zhanga21d5932018-02-05 18:28:38 +0000776 {
777 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000778 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000779 ASSERT_TRUE(annot);
780 rect.left = 200.f;
781 rect.bottom = 400.f;
782 rect.right = 500.f;
783 rect.top = 600.f;
784 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400785
Lei Zhanga21d5932018-02-05 18:28:38 +0000786 // Add a new path to the annotation.
787 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
788 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
789 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
790 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
791 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
792 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
793 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
794 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
795 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
796 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
797
798 // Check that the annotation's bounding box came from its rectangle.
799 FS_RECTF new_rect;
800 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
801 EXPECT_EQ(rect.left, new_rect.left);
802 EXPECT_EQ(rect.bottom, new_rect.bottom);
803 EXPECT_EQ(rect.right, new_rect.right);
804 EXPECT_EQ(rect.top, new_rect.top);
805 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400806
807 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400808 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000809 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400810
811 // Open the saved document.
Lei Zhang0b494052019-01-31 21:41:15 +0000812 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000813 page = LoadSavedPage(0);
814 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400815
Jane Liu36567742017-07-06 11:13:35 -0400816 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000817 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400818
Lei Zhanga21d5932018-02-05 18:28:38 +0000819 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000820 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000821 ASSERT_TRUE(annot);
822 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400823
Lei Zhanga21d5932018-02-05 18:28:38 +0000824 // Check that the new annotation's rectangle is as defined.
825 FS_RECTF new_rect;
826 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
827 EXPECT_EQ(rect.left, new_rect.left);
828 EXPECT_EQ(rect.bottom, new_rect.bottom);
829 EXPECT_EQ(rect.right, new_rect.right);
830 EXPECT_EQ(rect.top, new_rect.top);
831 }
832
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000833 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400834 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400835}
Jane Liub137e752017-07-05 15:04:33 -0400836
Lei Zhangab41f252018-12-23 03:10:50 +0000837TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400838 // Open a file with an annotation and load its first page.
839 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000840 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400841 ASSERT_TRUE(page);
842
843 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000844 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000845 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000846 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
847 }
Jane Liub137e752017-07-05 15:04:33 -0400848
Lei Zhanga21d5932018-02-05 18:28:38 +0000849 {
850 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000851 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000852 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400853
Lei Zhanga21d5932018-02-05 18:28:38 +0000854 // Check that the original flag values are as expected.
855 int flags = FPDFAnnot_GetFlags(annot.get());
856 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
857 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400858
Lei Zhanga21d5932018-02-05 18:28:38 +0000859 // Set the HIDDEN flag.
860 flags |= FPDF_ANNOT_FLAG_HIDDEN;
861 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
862 flags = FPDFAnnot_GetFlags(annot.get());
863 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
864 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400865
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000867 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000868 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
870 }
Jane Liub137e752017-07-05 15:04:33 -0400871
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 // Unset the HIDDEN flag.
873 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
874 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
875 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
876 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
877 flags = FPDFAnnot_GetFlags(annot.get());
878 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
879 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400880
Lei Zhanga21d5932018-02-05 18:28:38 +0000881 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000882 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000883 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000884 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
885 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000886 }
Jane Liub137e752017-07-05 15:04:33 -0400887
Jane Liub137e752017-07-05 15:04:33 -0400888 UnloadPage(page);
889}
Jane Liu36567742017-07-06 11:13:35 -0400890
Lei Zhangab41f252018-12-23 03:10:50 +0000891TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400892#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400893 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
894 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
895 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000896#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
897 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
898 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
899 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400900#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000901 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
902 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
903 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400904#endif
905
906 // Open a file with two annotations and load its first page.
907 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000908 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400909 ASSERT_TRUE(page);
910 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
911
912 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000913 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000914 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000915 CompareBitmap(bitmap.get(), 595, 842, md5_original);
916 }
Jane Liu36567742017-07-06 11:13:35 -0400917
Jane Liu36567742017-07-06 11:13:35 -0400918 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000919 FPDF_BITMAP image_bitmap;
920
921 {
922 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000923 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000924 ASSERT_TRUE(annot);
925 FS_RECTF rect;
926 rect.left = 200.f;
927 rect.bottom = 600.f;
928 rect.right = 400.f;
929 rect.top = 800.f;
930 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
931
932 // Add a solid-color translucent image object to the new annotation.
933 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
934 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
935 0xeeeecccc);
936 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
937 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
938 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
939 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
940 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
941 kBitmapSize, 0, 0));
942 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
943 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
944 }
Jane Liu36567742017-07-06 11:13:35 -0400945
946 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000947 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000948 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000949 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
950 }
Jane Liu36567742017-07-06 11:13:35 -0400951
Lei Zhanga21d5932018-02-05 18:28:38 +0000952 {
953 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000954 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000955 ASSERT_TRUE(annot);
956 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
957 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
958 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400959
Lei Zhanga21d5932018-02-05 18:28:38 +0000960 // Modify the image in the new annotation.
961 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
962 0xff000000);
963 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
964 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
965 }
Jane Liu36567742017-07-06 11:13:35 -0400966
967 // Save the document, closing the page and document.
968 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000969 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400970 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400971
972 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400973 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400974}
975
Lei Zhangab41f252018-12-23 03:10:50 +0000976TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400977#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400978 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Lei Zhange039bab2019-03-18 19:57:56 +0000979 const char md5_new_text[] = "60031c1b0330cf1e1575f7d46687d429";
Jane Liu7a9a38b2017-07-11 13:47:37 -0400980 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000981#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
982 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
Lei Zhange039bab2019-03-18 19:57:56 +0000983 const char md5_new_text[] = "87d55e09f9096de7e6552f5ae79afd3b";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000984 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400985#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000986 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Lei Zhange039bab2019-03-18 19:57:56 +0000987 const char md5_new_text[] = "30f3f5b989612ca03827d95f184f0979";
Henrique Nakashima09b41922017-10-27 20:39:29 +0000988 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400989#endif
990
991 // Open a file with two annotations and load its first page.
992 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000993 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400994 ASSERT_TRUE(page);
995 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
996
997 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000998 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000999 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001000 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1001 }
Jane Liu36567742017-07-06 11:13:35 -04001002
Lei Zhanga21d5932018-02-05 18:28:38 +00001003 {
1004 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001005 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001006 ASSERT_TRUE(annot);
1007 FS_RECTF rect;
1008 rect.left = 200.f;
1009 rect.bottom = 550.f;
1010 rect.right = 450.f;
1011 rect.top = 650.f;
1012 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -04001013
Lei Zhanga21d5932018-02-05 18:28:38 +00001014 // Add a translucent text object to the new annotation.
1015 FPDF_PAGEOBJECT text_object =
1016 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1017 EXPECT_TRUE(text_object);
1018 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1019 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1020 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1021 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1022 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1023 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1024 }
Jane Liu36567742017-07-06 11:13:35 -04001025
1026 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001027 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001028 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001029 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1030 }
Jane Liu36567742017-07-06 11:13:35 -04001031
Lei Zhanga21d5932018-02-05 18:28:38 +00001032 {
1033 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001035 ASSERT_TRUE(annot);
1036 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1037 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1038 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001039
Lei Zhanga21d5932018-02-05 18:28:38 +00001040 // Modify the text in the new annotation.
1041 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1042 GetFPDFWideString(L"New text!");
1043 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1044 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1045 }
Jane Liu36567742017-07-06 11:13:35 -04001046
1047 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001048 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001049 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001050 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1051 }
Jane Liu36567742017-07-06 11:13:35 -04001052
1053 // Remove the new annotation, and check that the page renders as before.
1054 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001055 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001056 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001057 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1058 }
Jane Liu36567742017-07-06 11:13:35 -04001059
1060 UnloadPage(page);
1061}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001062
Lei Zhangab41f252018-12-23 03:10:50 +00001063TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001064 // Open a file with four annotations and load its first page.
1065 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001066 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001067 ASSERT_TRUE(page);
1068
Lei Zhanga21d5932018-02-05 18:28:38 +00001069 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001070
Lei Zhanga21d5932018-02-05 18:28:38 +00001071 {
1072 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001073 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001074 ASSERT_TRUE(annot);
1075
1076 // Check that a non-existent key does not exist.
1077 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1078
1079 // Check that the string value of a non-string dictionary entry is empty.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001080 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001081 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001082 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1083 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1084 nullptr, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001085
1086 // Check that the string value of the hash is correct.
1087 static constexpr char kHashKey[] = "AAPL:Hash";
1088 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1089 unsigned long len =
1090 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1091 std::vector<char> buf(len);
1092 EXPECT_EQ(66u,
1093 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1094 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1095 BufferToWString(buf).c_str());
1096
1097 // Check that the string value of the modified date is correct.
1098 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001099 len = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM, nullptr,
1100 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001101 buf.clear();
1102 buf.resize(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001103 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1104 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001105 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1106
1107 // Update the date entry for the annotation.
1108 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1109 GetFPDFWideString(kNewDate);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001110 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1111 text.get()));
Lei Zhanga21d5932018-02-05 18:28:38 +00001112 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001113
1114 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001116 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001117
Dan Sinclair698aed72017-09-26 16:24:49 -04001118#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001119 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001120#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1121 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001122#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001123 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001124#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001125
1126 // Open the saved annotation.
Lei Zhang0b494052019-01-31 21:41:15 +00001127 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001128 page = LoadSavedPage(0);
1129 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001130 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001131 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001132
Lei Zhanga21d5932018-02-05 18:28:38 +00001133 // Check that the string value of the modified date is the newly-set value.
1134 EXPECT_EQ(FPDF_OBJECT_STRING,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001135 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1136 unsigned long len = FPDFAnnot_GetStringValue(
1137 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 std::vector<char> buf(len);
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001139 EXPECT_EQ(44u,
1140 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1141 buf.data(), len));
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1143 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001144
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001145 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001146 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001147}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001148
rycsmith3e785602019-03-05 21:48:36 +00001149TEST_F(FPDFAnnotEmbedderTest, GetNumberValue) {
1150 // Open a file with three text annotations and load its first page.
1151 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1152 FPDF_PAGE page = LoadPage(0);
1153 ASSERT_TRUE(page);
1154 {
1155 // First two annotations do not have "MaxLen" attribute.
1156 for (int i = 0; i < 2; i++) {
1157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1158 ASSERT_TRUE(annot);
1159
1160 // Verify that no "MaxLen" key present.
1161 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1162
1163 float value;
1164 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1165 }
1166
1167 // Annotation in index 2 has "MaxLen" of 10.
1168 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1169 ASSERT_TRUE(annot);
1170
1171 // Verify that "MaxLen" key present.
1172 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1173
1174 float value;
1175 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1176 EXPECT_FLOAT_EQ(10.0f, value);
1177
1178 // Check bad inputs.
1179 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1180 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1181 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1182 // Ask for key that exists but is not a number.
1183 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1184 }
1185
1186 UnloadPage(page);
1187}
1188
Lei Zhangab41f252018-12-23 03:10:50 +00001189TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001190 // Open a file with four annotations and load its first page.
1191 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001192 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001193 ASSERT_TRUE(page);
1194
Lei Zhanga21d5932018-02-05 18:28:38 +00001195 {
1196 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001197 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001199
Lei Zhanga21d5932018-02-05 18:28:38 +00001200 // Check that the string value of an AP returns the expected length.
1201 unsigned long normal_len = FPDFAnnot_GetAP(
1202 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1203 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001204
Lei Zhanga21d5932018-02-05 18:28:38 +00001205 // Check that the string value of an AP is not returned if the buffer is too
1206 // small. The result buffer should be overwritten with an empty string.
1207 std::vector<char> buf(normal_len - 1);
1208 // Write L"z" in the buffer to verify it's not overwritten.
1209 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1210 EXPECT_EQ(73970u,
1211 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1212 buf.data(), buf.size()));
1213 std::string ap = BufferToString(buf);
1214 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001215
Lei Zhanga21d5932018-02-05 18:28:38 +00001216 // Check that the string value of an AP is returned through a buffer that is
1217 // the right size.
1218 buf.clear();
1219 buf.resize(normal_len);
1220 EXPECT_EQ(73970u,
1221 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1222 buf.data(), buf.size()));
1223 ap = BufferToString(buf);
1224 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1225 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001226
Lei Zhanga21d5932018-02-05 18:28:38 +00001227 // Check that the string value of an AP is returned through a buffer that is
1228 // larger than necessary.
1229 buf.clear();
1230 buf.resize(normal_len + 1);
1231 EXPECT_EQ(73970u,
1232 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1233 buf.data(), buf.size()));
1234 ap = BufferToString(buf);
1235 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1236 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001237
Lei Zhanga21d5932018-02-05 18:28:38 +00001238 // Check that getting an AP for a mode that does not have an AP returns an
1239 // empty string.
1240 unsigned long rollover_len = FPDFAnnot_GetAP(
1241 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1242 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001243
Lei Zhanga21d5932018-02-05 18:28:38 +00001244 buf.clear();
1245 buf.resize(1000);
1246 EXPECT_EQ(2u,
1247 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1248 buf.data(), buf.size()));
1249 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001250
Lei Zhanga21d5932018-02-05 18:28:38 +00001251 // Check that setting the AP for an invalid appearance mode fails.
1252 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1253 GetFPDFWideString(L"new test ap");
1254 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1255 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1256 apText.get()));
1257 EXPECT_FALSE(FPDFAnnot_SetAP(
1258 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001259
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 // Set the AP correctly now.
1261 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1262 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001263
Lei Zhanga21d5932018-02-05 18:28:38 +00001264 // Check that the new annotation value is equal to the value we just set.
1265 rollover_len = FPDFAnnot_GetAP(
1266 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1267 EXPECT_EQ(24u, rollover_len);
1268 buf.clear();
1269 buf.resize(rollover_len);
1270 EXPECT_EQ(24u,
1271 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1272 buf.data(), buf.size()));
1273 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001274
Lei Zhanga21d5932018-02-05 18:28:38 +00001275 // Check that the Normal AP was not touched when the Rollover AP was set.
1276 buf.clear();
1277 buf.resize(normal_len);
1278 EXPECT_EQ(73970u,
1279 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1280 buf.data(), buf.size()));
1281 ap = BufferToString(buf);
1282 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1283 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1284 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001285
1286 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001287 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001288 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001289
Lei Zhang0b494052019-01-31 21:41:15 +00001290 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001291 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001292 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001293 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001294
Lei Zhanga21d5932018-02-05 18:28:38 +00001295 // Check that the new annotation value is equal to the value we set before
1296 // saving.
1297 unsigned long rollover_len = FPDFAnnot_GetAP(
1298 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1299 EXPECT_EQ(24u, rollover_len);
1300 std::vector<char> buf(rollover_len);
1301 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1302 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1303 buf.data(), buf.size()));
1304 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1305 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001306
1307 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001308 CloseSavedPage(page);
1309 CloseSavedDocument();
1310}
1311
Lei Zhangab41f252018-12-23 03:10:50 +00001312TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001313 // Open a file with four annotations and load its first page.
1314 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001315 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001316 ASSERT_TRUE(page);
1317
Lei Zhanga21d5932018-02-05 18:28:38 +00001318 {
1319 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001320 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001321 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001322
Lei Zhanga21d5932018-02-05 18:28:38 +00001323 // Set Down AP. Normal AP is already set.
1324 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1325 GetFPDFWideString(L"new test ap");
1326 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1327 apText.get()));
1328 EXPECT_EQ(73970u,
1329 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1330 nullptr, 0));
1331 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1332 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001333
Lei Zhanga21d5932018-02-05 18:28:38 +00001334 // Check that setting the Down AP to null removes the Down entry but keeps
1335 // Normal intact.
1336 EXPECT_TRUE(
1337 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1338 EXPECT_EQ(73970u,
1339 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1340 nullptr, 0));
1341 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1342 nullptr, 0));
1343 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001344
Lei Zhang75c81712018-02-08 17:22:39 +00001345 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001346}
1347
Lei Zhangab41f252018-12-23 03:10:50 +00001348TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001349 // Open a file with four annotations and load its first page.
1350 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001351 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001352 ASSERT_TRUE(page);
1353
Lei Zhanga21d5932018-02-05 18:28:38 +00001354 {
1355 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001356 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001357 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Set Down AP. Normal AP is already set.
1360 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1361 GetFPDFWideString(L"new test ap");
1362 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1363 apText.get()));
1364 EXPECT_EQ(73970u,
1365 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1366 nullptr, 0));
1367 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1368 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001369
Lei Zhanga21d5932018-02-05 18:28:38 +00001370 // Check that setting the Normal AP to null removes the whole AP dictionary.
1371 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1372 nullptr));
1373 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1374 nullptr, 0));
1375 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1376 nullptr, 0));
1377 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001378
Lei Zhang75c81712018-02-08 17:22:39 +00001379 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001380}
1381
Lei Zhangab41f252018-12-23 03:10:50 +00001382TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001383 // Open a file with annotations and load its first page.
1384 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001385 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001386 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001387 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001388
Lei Zhanga21d5932018-02-05 18:28:38 +00001389 {
1390 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001391 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 ASSERT_TRUE(annot);
1393 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1394 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1395 static constexpr char kPopupKey[] = "Popup";
1396 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1397 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1398 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001399
Lei Zhanga21d5932018-02-05 18:28:38 +00001400 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001401 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1403 ASSERT_TRUE(popup);
1404 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1405 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1406 FS_RECTF rect;
1407 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1408 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1409 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1412 // since "IRT" is not a key in |annot|'s dictionary.
1413 static constexpr char kIRTKey[] = "IRT";
1414 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1415 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001416
Lei Zhanga21d5932018-02-05 18:28:38 +00001417 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1418 // would fail, since its parent is not an annotation.
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001419 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001420 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
Lei Zhanga5c1daf2019-01-31 21:56:47 +00001421 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1422 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 }
Jane Liu300bb272017-08-21 14:37:53 -04001424
Jane Liu300bb272017-08-21 14:37:53 -04001425 UnloadPage(page);
1426}
1427
Lei Zhangab41f252018-12-23 03:10:50 +00001428TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001429 // Open file with form text fields.
1430 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001431 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001432 ASSERT_TRUE(page);
1433
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 {
1435 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001436 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001438
Lei Zhanga21d5932018-02-05 18:28:38 +00001439 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001440 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001441 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1442 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001443
Lei Zhanga21d5932018-02-05 18:28:38 +00001444 {
1445 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001446 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001447 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001448
Lei Zhanga21d5932018-02-05 18:28:38 +00001449 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001450 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001451 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1452 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001453
1454 UnloadPage(page);
1455}
1456
Lei Zhangab41f252018-12-23 03:10:50 +00001457TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001458 // Open file with form text fields.
1459 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001460 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001461 ASSERT_TRUE(page);
1462
Lei Zhanga21d5932018-02-05 18:28:38 +00001463 {
1464 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001465 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001466 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001467
Lei Zhanga21d5932018-02-05 18:28:38 +00001468 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001469 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001470 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1471 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1472 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1473 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001474
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 {
1476 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001477 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001479
Lei Zhanga21d5932018-02-05 18:28:38 +00001480 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001481 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1483 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1484 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1485 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001486
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 {
1488 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001489 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001490 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 // Check that the flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001493 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001494 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1495 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1496 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1497 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001498
1499 UnloadPage(page);
1500}
Diana Gage40870db2017-07-19 18:16:03 -07001501
Lei Zhangab41f252018-12-23 03:10:50 +00001502TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001503 // Open file with form text fields.
1504 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1505 FPDF_PAGE page = LoadPage(0);
1506 ASSERT_TRUE(page);
1507
1508 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001509 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1510
1511 {
1512 // Verify there is an annotation.
1513 ScopedFPDFAnnotation annot(
1514 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1515 EXPECT_TRUE(annot);
1516 }
1517
1518 // Try other bad inputs at a valid location.
1519 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1520 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1521 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001522
1523 UnloadPage(page);
1524}
1525
Lei Zhangab41f252018-12-23 03:10:50 +00001526TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001527 // Open file with form text fields.
1528 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1529 FPDF_PAGE page = LoadPage(0);
1530 ASSERT_TRUE(page);
1531
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 {
1533 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001534 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001535 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1536 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001539 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001540 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1541 }
Diana Gage40870db2017-07-19 18:16:03 -07001542
Lei Zhanga21d5932018-02-05 18:28:38 +00001543 {
1544 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001545 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001546 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1547 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001548
Lei Zhanga21d5932018-02-05 18:28:38 +00001549 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001550 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001551 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1552 }
Diana Gage40870db2017-07-19 18:16:03 -07001553
1554 UnloadPage(page);
1555}
1556
Lei Zhangab41f252018-12-23 03:10:50 +00001557TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001558 // Open file with form comboboxes.
1559 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1560 FPDF_PAGE page = LoadPage(0);
1561 ASSERT_TRUE(page);
1562
Lei Zhanga21d5932018-02-05 18:28:38 +00001563 {
1564 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001565 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001566 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001567 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001568
Lei Zhanga21d5932018-02-05 18:28:38 +00001569 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001570 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001571 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1572 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1573 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1574 }
Diana Gage40870db2017-07-19 18:16:03 -07001575
Lei Zhanga21d5932018-02-05 18:28:38 +00001576 {
1577 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001578 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001579 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001580 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001581
Lei Zhanga21d5932018-02-05 18:28:38 +00001582 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001583 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001584 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1585 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1586 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1587 }
Diana Gage40870db2017-07-19 18:16:03 -07001588
Lei Zhanga21d5932018-02-05 18:28:38 +00001589 {
1590 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001591 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001592 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001593 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001594
Lei Zhanga21d5932018-02-05 18:28:38 +00001595 // Check that interactive form annotation flag values are as expected.
Lei Zhange6fcdfa2019-02-14 04:07:09 +00001596 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), page, annot.get());
Lei Zhanga21d5932018-02-05 18:28:38 +00001597 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1598 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1599 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1600 }
Diana Gage40870db2017-07-19 18:16:03 -07001601
1602 UnloadPage(page);
1603}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001604
Lei Zhang992e7e22019-02-04 19:20:58 +00001605TEST_F(FPDFAnnotEmbedderTest, BUG_1206) {
1606 static constexpr size_t kExpectedSize = 1609;
1607 static const char kExpectedBitmap[] = "0d9fc05c6762fd788bd23fd87a4967bc";
1608
1609 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
1610
1611 FPDF_PAGE page = LoadPage(0);
1612 ASSERT_TRUE(page);
1613
1614 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1615 EXPECT_EQ(kExpectedSize, GetString().size());
1616 ClearString();
1617
1618 for (size_t i = 0; i < 10; ++i) {
1619 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1620 CompareBitmap(bitmap.get(), 612, 792, kExpectedBitmap);
1621
1622 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1623 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
1624 // equal, not bigger.
1625 EXPECT_LT(kExpectedSize, GetString().size());
1626 ClearString();
1627 }
1628
1629 UnloadPage(page);
1630}
1631
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001632TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1633 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1634 FPDF_PAGE page = LoadPage(0);
1635 ASSERT_TRUE(page);
1636 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1637
1638 static const char kTestKey[] = "test";
1639 static constexpr wchar_t kData[] = L"\xf6\xe4";
1640 std::vector<char> buf(12);
1641
1642 {
1643 // Add a text annotation to the page.
1644 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1645 ASSERT_TRUE(annot);
1646 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1647 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1648
1649 // Make sure there is no test key, add set a value there, and read it back.
1650 std::fill(buf.begin(), buf.end(), 'x');
1651 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1652 buf.size()));
1653 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1654
1655 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1656 GetFPDFWideString(kData);
1657 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1658
1659 std::fill(buf.begin(), buf.end(), 'x');
1660 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1661 buf.size()));
1662 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1663 }
1664
Lei Zhang05ec64c2019-01-09 03:00:06 +00001665 {
1666 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1667 ASSERT_TRUE(annot);
1668 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1669 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
1670 // Also do the same test for its appearance string.
1671 std::fill(buf.begin(), buf.end(), 'x');
1672 ASSERT_EQ(2u,
1673 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1674 buf.data(), buf.size()));
1675 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1676
1677 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1678 GetFPDFWideString(kData);
1679 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1680 text.get()));
1681
1682 std::fill(buf.begin(), buf.end(), 'x');
1683 ASSERT_EQ(6u,
1684 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1685 buf.data(), buf.size()));
1686 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1687 }
1688
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001689 UnloadPage(page);
1690
1691 {
1692 // Save a copy, open the copy, and check the annotation again.
1693 // Note that it renders the rotation.
1694 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang0b494052019-01-31 21:41:15 +00001695 ASSERT_TRUE(OpenSavedDocument());
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001696 FPDF_PAGE saved_page = LoadSavedPage(0);
1697 ASSERT_TRUE(saved_page);
1698
Lei Zhang05ec64c2019-01-09 03:00:06 +00001699 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001700 {
1701 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1702 ASSERT_TRUE(annot);
1703 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1704
1705 std::fill(buf.begin(), buf.end(), 'x');
1706 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1707 buf.size()));
1708 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1709 }
1710
Lei Zhang05ec64c2019-01-09 03:00:06 +00001711 {
1712 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1713 ASSERT_TRUE(annot);
1714 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
1715 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1716
1717 std::fill(buf.begin(), buf.end(), 'x');
1718 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1719 buf.size()));
1720 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1721 }
1722
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001723 CloseSavedPage(saved_page);
1724 CloseSavedDocument();
1725 }
1726}
rycsmithcb752f32019-02-21 18:40:53 +00001727
1728TEST_F(FPDFAnnotEmbedderTest, GetOptionCountCombobox) {
1729 // Open a file with combobox widget annotations and load its first page.
1730 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1731 FPDF_PAGE page = LoadPage(0);
1732 ASSERT_TRUE(page);
1733
1734 {
1735 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1736 ASSERT_TRUE(annot);
1737
1738 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1739
1740 annot.reset(FPDFPage_GetAnnot(page, 1));
1741 ASSERT_TRUE(annot);
1742
1743 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
Lei Zhange7033c82019-02-26 19:30:49 +00001744
1745 // Check bad form handle / annot.
1746 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
1747 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
1748 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
rycsmithcb752f32019-02-21 18:40:53 +00001749 }
1750
1751 UnloadPage(page);
1752}
1753
1754TEST_F(FPDFAnnotEmbedderTest, GetOptionCountListbox) {
1755 // Open a file with listbox widget annotations and load its first page.
1756 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1757 FPDF_PAGE page = LoadPage(0);
1758 ASSERT_TRUE(page);
1759
1760 {
1761 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1762 ASSERT_TRUE(annot);
1763
1764 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1765
1766 annot.reset(FPDFPage_GetAnnot(page, 1));
1767 ASSERT_TRUE(annot);
1768
1769 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1770 }
1771
1772 UnloadPage(page);
1773}
1774
1775TEST_F(FPDFAnnotEmbedderTest, GetOptionCountInvalidAnnotations) {
1776 // Open a file with ink annotations and load its first page.
1777 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1778 FPDF_PAGE page = LoadPage(0);
1779 ASSERT_TRUE(page);
1780
1781 {
1782 // annotations do not have "Opt" array and will return -1
1783 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1784 ASSERT_TRUE(annot);
1785
1786 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1787
1788 annot.reset(FPDFPage_GetAnnot(page, 1));
1789 ASSERT_TRUE(annot);
1790
1791 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
1792 }
1793
1794 UnloadPage(page);
1795}
1796
1797TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelCombobox) {
1798 // Open a file with combobox widget annotations and load its first page.
1799 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
1800 FPDF_PAGE page = LoadPage(0);
1801 ASSERT_TRUE(page);
1802
1803 {
1804 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1805 ASSERT_TRUE(annot);
1806
1807 int index = 0;
1808 unsigned long len =
1809 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1810 std::vector<char> buf(len);
1811 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1812 buf.data(), len));
1813 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1814
1815 annot.reset(FPDFPage_GetAnnot(page, 1));
1816 ASSERT_TRUE(annot);
1817
1818 index = 0;
1819 len =
1820 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1821 buf.clear();
1822 buf.resize(len);
1823 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1824 buf.data(), len));
1825 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1826
1827 index = 25;
1828 len =
1829 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1830 buf.clear();
1831 buf.resize(len);
1832 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1833 buf.data(), len));
1834 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1835
Lei Zhange7033c82019-02-26 19:30:49 +00001836 // Indices out of range
rycsmithcb752f32019-02-21 18:40:53 +00001837 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1838 nullptr, 0));
1839 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1840 nullptr, 0));
Lei Zhange7033c82019-02-26 19:30:49 +00001841
1842 // Check bad form handle / annot.
1843 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
1844 EXPECT_EQ(0u,
1845 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
1846 EXPECT_EQ(0u,
1847 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
rycsmithcb752f32019-02-21 18:40:53 +00001848 }
1849
1850 UnloadPage(page);
1851}
1852
1853TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelListbox) {
1854 // Open a file with listbox widget annotations and load its first page.
1855 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
1856 FPDF_PAGE page = LoadPage(0);
1857 ASSERT_TRUE(page);
1858
1859 {
1860 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1861 ASSERT_TRUE(annot);
1862
1863 int index = 0;
1864 unsigned long len =
1865 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1866 std::vector<char> buf(len);
1867 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1868 buf.data(), len));
1869 EXPECT_STREQ(L"Foo", BufferToWString(buf).c_str());
1870
1871 annot.reset(FPDFPage_GetAnnot(page, 1));
1872 ASSERT_TRUE(annot);
1873
1874 index = 0;
1875 len =
1876 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1877 buf.clear();
1878 buf.resize(len);
1879 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1880 buf.data(), len));
1881 EXPECT_STREQ(L"Apple", BufferToWString(buf).c_str());
1882
1883 index = 25;
1884 len =
1885 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
1886 buf.clear();
1887 buf.resize(len);
1888 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
1889 buf.data(), len));
1890 EXPECT_STREQ(L"Zucchini", BufferToWString(buf).c_str());
1891
1892 // indices out of range
1893 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
1894 nullptr, 0));
1895 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
1896 nullptr, 0));
1897 }
1898
1899 UnloadPage(page);
1900}
1901
1902TEST_F(FPDFAnnotEmbedderTest, GetOptionLabelInvalidAnnotations) {
1903 // Open a file with ink annotations and load its first page.
1904 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1905 FPDF_PAGE page = LoadPage(0);
1906 ASSERT_TRUE(page);
1907
1908 {
1909 // annotations do not have "Opt" array and will return 0
1910 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1911 ASSERT_TRUE(annot);
1912
1913 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1914 nullptr, 0));
1915
1916 annot.reset(FPDFPage_GetAnnot(page, 1));
1917 ASSERT_TRUE(annot);
1918
1919 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
1920 nullptr, 0));
1921 }
1922
1923 UnloadPage(page);
1924}