Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_annot_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "public/fpdf_annot.h"
6
7#include <limits.h>
8
9#include <algorithm>
10#include <string>
11#include <type_traits>
12#include <vector>
13
14#include "build/build_config.h"
15#include "constants/annotation_common.h"
16#include "core/fpdfapi/page/cpdf_annotcontext.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_dictionary.h"
19#include "core/fxcrt/compiler_specific.h"
20#include "core/fxcrt/containers/contains.h"
21#include "core/fxcrt/fx_memcpy_wrappers.h"
22#include "core/fxcrt/fx_system.h"
23#include "core/fxcrt/span.h"
24#include "core/fxge/cfx_defaultrenderdevice.h"
25#include "fpdfsdk/cpdfsdk_helpers.h"
26#include "public/cpp/fpdf_scopers.h"
27#include "public/fpdf_attachment.h"
28#include "public/fpdf_edit.h"
29#include "public/fpdf_formfill.h"
30#include "public/fpdfview.h"
31#include "testing/embedder_test.h"
32#include "testing/embedder_test_constants.h"
33#include "testing/fx_string_testhelpers.h"
34#include "testing/gmock/include/gmock/gmock-matchers.h"
35#include "testing/gtest/include/gtest/gtest.h"
36#include "testing/utils/hash.h"
37
38using pdfium::AnnotationStampWithApChecksum;
39
40namespace {
41
42const wchar_t kStreamData[] =
43 L"/GS gs 0.0 0.0 0.0 RG 4 w 211.8 747.6 m 211.8 744.8 "
44 L"212.6 743.0 214.2 740.8 "
45 L"c 215.4 739.0 216.8 737.1 218.9 736.1 c 220.8 735.1 221.4 733.0 "
46 L"223.7 732.4 c 232.6 729.9 242.0 730.8 251.2 730.8 c 257.5 730.8 "
47 L"263.0 732.9 269.0 734.4 c S";
48
49void VerifyFocusableAnnotSubtypes(
50 FPDF_FORMHANDLE form_handle,
51 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes) {
52 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
54
55 std::vector<FPDF_ANNOTATION_SUBTYPE> actual_subtypes(
56 expected_subtypes.size());
57 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(
58 form_handle, actual_subtypes.data(), actual_subtypes.size()));
59 for (size_t i = 0; i < expected_subtypes.size(); ++i)
60 ASSERT_EQ(expected_subtypes[i], actual_subtypes[i]);
61}
62
63void SetAndVerifyFocusableAnnotSubtypes(
64 FPDF_FORMHANDLE form_handle,
65 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> subtypes) {
66 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle, subtypes.data(),
67 subtypes.size()));
68 VerifyFocusableAnnotSubtypes(form_handle, subtypes);
69}
70
71void VerifyAnnotationSubtypesAndFocusability(
72 FPDF_FORMHANDLE form_handle,
73 FPDF_PAGE page,
74 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_subtypes,
75 pdfium::span<const FPDF_ANNOTATION_SUBTYPE> expected_focusable_subtypes) {
76 ASSERT_EQ(static_cast<int>(expected_subtypes.size()),
78 for (size_t i = 0; i < expected_subtypes.size(); ++i) {
79 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
80 ASSERT_TRUE(annot);
81 EXPECT_EQ(expected_subtypes[i], FPDFAnnot_GetSubtype(annot.get()));
82
83 bool expected_focusable =
84 pdfium::Contains(expected_focusable_subtypes, expected_subtypes[i]);
85 EXPECT_EQ(expected_focusable,
86 FORM_SetFocusedAnnot(form_handle, annot.get()));
87
88 // Kill the focus so the next test starts in an unfocused state.
89 FORM_ForceToKillFocus(form_handle);
90 }
91}
92
93void VerifyUriActionInLink(FPDF_DOCUMENT doc,
94 FPDF_LINK link,
95 const std::string& expected_uri) {
96 ASSERT_TRUE(link);
97
98 FPDF_ACTION action = FPDFLink_GetAction(link);
99 ASSERT_TRUE(action);
100 EXPECT_EQ(static_cast<unsigned long>(PDFACTION_URI),
102
103 unsigned long bufsize = FPDFAction_GetURIPath(doc, action, nullptr, 0);
104 ASSERT_EQ(expected_uri.size() + 1, bufsize);
105
106 std::vector<char> buffer(bufsize);
107 EXPECT_EQ(bufsize,
108 FPDFAction_GetURIPath(doc, action, buffer.data(), bufsize));
109 EXPECT_EQ(expected_uri, buffer.data());
110}
111
112} // namespace
113
115
117 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
118 ASSERT_TRUE(doc);
119 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
120 ASSERT_TRUE(page);
121 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
122 ASSERT_TRUE(ap_stream);
123
124 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
125 ASSERT_TRUE(annot);
126
127 // Negative case: FPDFAnnot_SetAP() should fail if bounding rect is not yet
128 // set on the annotation.
129 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
130 ap_stream.get()));
131
132 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
133 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
134
135 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
136 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/255));
137
138 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
139 ap_stream.get()));
140
141 // Verify that appearance stream is created as form XObject
142 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
143 ASSERT_TRUE(context);
144 const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
145 ASSERT_TRUE(annot_dict);
146 RetainPtr<const CPDF_Dictionary> ap_dict =
147 annot_dict->GetDictFor(pdfium::annotation::kAP);
148 ASSERT_TRUE(ap_dict);
149 RetainPtr<const CPDF_Dictionary> stream_dict = ap_dict->GetDictFor("N");
150 ASSERT_TRUE(stream_dict);
151 // Check for non-existence of resources dictionary in case of opaque color
152 RetainPtr<const CPDF_Dictionary> resources_dict =
153 stream_dict->GetDictFor("Resources");
154 ASSERT_FALSE(resources_dict);
155 ByteString type = stream_dict->GetByteStringFor(pdfium::annotation::kType);
156 EXPECT_EQ("XObject", type);
157 ByteString sub_type =
158 stream_dict->GetByteStringFor(pdfium::annotation::kSubtype);
159 EXPECT_EQ("Form", sub_type);
160
161 // Check that the appearance stream is same as we just set.
162 const uint32_t kStreamDataSize = std::size(kStreamData) * sizeof(FPDF_WCHAR);
163 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
164 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
165 ASSERT_EQ(kStreamDataSize, normal_length_bytes);
166 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
167 EXPECT_EQ(kStreamDataSize,
168 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
169 buf.data(), normal_length_bytes));
170 EXPECT_EQ(kStreamData, GetPlatformWString(buf.data()));
171}
172
174 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
175 ASSERT_TRUE(doc);
176 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
177 ASSERT_TRUE(page);
178 ScopedFPDFWideString ap_stream = GetFPDFWideString(kStreamData);
179 ASSERT_TRUE(ap_stream);
180
181 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
182 ASSERT_TRUE(annot);
183
184 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color,
185 /*R=*/255, /*G=*/0, /*B=*/0, /*A=*/102));
186
187 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
188 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
189
190 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
191 ap_stream.get()));
192
193 CPDF_AnnotContext* context = CPDFAnnotContextFromFPDFAnnotation(annot.get());
194 ASSERT_TRUE(context);
195 const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
196 ASSERT_TRUE(annot_dict);
197 RetainPtr<const CPDF_Dictionary> ap_dict =
198 annot_dict->GetDictFor(pdfium::annotation::kAP);
199 ASSERT_TRUE(ap_dict);
200 RetainPtr<const CPDF_Dictionary> stream_dict = ap_dict->GetDictFor("N");
201 ASSERT_TRUE(stream_dict);
202 RetainPtr<const CPDF_Dictionary> resources_dict =
203 stream_dict->GetDictFor("Resources");
204 ASSERT_TRUE(stream_dict);
205 RetainPtr<const CPDF_Dictionary> extGState_dict =
206 resources_dict->GetDictFor("ExtGState");
207 ASSERT_TRUE(extGState_dict);
208 RetainPtr<const CPDF_Dictionary> gs_dict = extGState_dict->GetDictFor("GS");
209 ASSERT_TRUE(gs_dict);
210 ByteString type = gs_dict->GetByteStringFor(pdfium::annotation::kType);
211 EXPECT_EQ("ExtGState", type);
212 float opacity = gs_dict->GetFloatFor("CA");
213 // Opacity value of 102 is represented as 0.4f (=104/255) in /CA entry.
214 EXPECT_FLOAT_EQ(0.4f, opacity);
215 ByteString blend_mode = gs_dict->GetByteStringFor("BM");
216 EXPECT_EQ("Normal", blend_mode);
217 bool alpha_source_flag = gs_dict->GetBooleanFor("AIS", true);
218 EXPECT_FALSE(alpha_source_flag);
219}
220
222 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
223 ASSERT_TRUE(doc);
224 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
225 ASSERT_TRUE(page);
226
227 // Create a new ink annotation.
228 ScopedFPDFAnnotation ink_annot(
229 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
230 ASSERT_TRUE(ink_annot);
231 CPDF_AnnotContext* context =
232 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
233 ASSERT_TRUE(context);
234 const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
235 ASSERT_TRUE(annot_dict);
236
237 static constexpr FS_POINTF kFirstInkStroke[] = {
238 {80.0f, 90.0f}, {81.0f, 91.0f}, {82.0f, 92.0f},
239 {83.0f, 93.0f}, {84.0f, 94.0f}, {85.0f, 95.0f}};
240 static constexpr size_t kFirstStrokePointCount = std::size(kFirstInkStroke);
241
242 static constexpr FS_POINTF kSecondInkStroke[] = {
243 {70.0f, 90.0f}, {71.0f, 91.0f}, {72.0f, 92.0f}};
244 static constexpr size_t kSecondStrokePointCount = std::size(kSecondInkStroke);
245
246 static constexpr FS_POINTF kThirdInkStroke[] = {{60.0f, 90.0f},
247 {61.0f, 91.0f},
248 {62.0f, 92.0f},
249 {63.0f, 93.0f},
250 {64.0f, 94.0f}};
251 static constexpr size_t kThirdStrokePointCount = std::size(kThirdInkStroke);
252
253 // Negative test: |annot| is passed as nullptr.
254 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(nullptr, kFirstInkStroke,
255 kFirstStrokePointCount));
256
257 // Negative test: |annot| is not ink annotation.
258 // Create a new highlight annotation.
259 ScopedFPDFAnnotation highlight_annot(
260 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
261 ASSERT_TRUE(highlight_annot);
262 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(highlight_annot.get(), kFirstInkStroke,
263 kFirstStrokePointCount));
264
265 // Negative test: passing |point_count| as 0.
266 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke, 0));
267
268 // Negative test: passing |points| array as nullptr.
269 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), nullptr,
270 kFirstStrokePointCount));
271
272 // Negative test: passing |point_count| more than ULONG_MAX/2.
273 EXPECT_EQ(-1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
274 ULONG_MAX / 2 + 1));
275
276 // InkStroke should get added to ink annotation. Also inklist should get
277 // created.
278 EXPECT_EQ(0, FPDFAnnot_AddInkStroke(ink_annot.get(), kFirstInkStroke,
279 kFirstStrokePointCount));
280
281 RetainPtr<const CPDF_Array> inklist = annot_dict->GetArrayFor("InkList");
282 ASSERT_TRUE(inklist);
283 EXPECT_EQ(1u, inklist->size());
284 EXPECT_EQ(kFirstStrokePointCount * 2, inklist->GetArrayAt(0)->size());
285
286 // Adding another inkStroke to ink annotation with all valid paremeters.
287 // InkList already exists in ink_annot.
288 EXPECT_EQ(1, FPDFAnnot_AddInkStroke(ink_annot.get(), kSecondInkStroke,
289 kSecondStrokePointCount));
290 EXPECT_EQ(2u, inklist->size());
291 EXPECT_EQ(kSecondStrokePointCount * 2, inklist->GetArrayAt(1)->size());
292
293 // Adding one more InkStroke to the ink annotation. |point_count| passed is
294 // less than the data available in |buffer|.
295 EXPECT_EQ(2, FPDFAnnot_AddInkStroke(ink_annot.get(), kThirdInkStroke,
296 kThirdStrokePointCount - 1));
297 EXPECT_EQ(3u, inklist->size());
298 EXPECT_EQ((kThirdStrokePointCount - 1) * 2, inklist->GetArrayAt(2)->size());
299}
300
302 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
303 ASSERT_TRUE(doc);
304 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
305 ASSERT_TRUE(page);
306
307 // Negative test: |annot| is passed as nullptr.
308 EXPECT_FALSE(FPDFAnnot_RemoveInkList(nullptr));
309
310 // Negative test: |annot| is not ink annotation.
311 // Create a new highlight annotation.
312 ScopedFPDFAnnotation highlight_annot(
313 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_HIGHLIGHT));
314 ASSERT_TRUE(highlight_annot);
315 EXPECT_FALSE(FPDFAnnot_RemoveInkList(highlight_annot.get()));
316
317 // Create a new ink annotation.
318 ScopedFPDFAnnotation ink_annot(
319 FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_INK));
320 ASSERT_TRUE(ink_annot);
321 CPDF_AnnotContext* context =
322 CPDFAnnotContextFromFPDFAnnotation(ink_annot.get());
323 ASSERT_TRUE(context);
324 const CPDF_Dictionary* annot_dict = context->GetAnnotDict();
325 ASSERT_TRUE(annot_dict);
326
327 static constexpr FS_POINTF kInkStroke[] = {{80.0f, 90.0f}, {81.0f, 91.0f},
328 {82.0f, 92.0f}, {83.0f, 93.0f},
329 {84.0f, 94.0f}, {85.0f, 95.0f}};
330 static constexpr size_t kPointCount = std::size(kInkStroke);
331
332 // InkStroke should get added to ink annotation. Also inklist should get
333 // created.
334 EXPECT_EQ(0,
335 FPDFAnnot_AddInkStroke(ink_annot.get(), kInkStroke, kPointCount));
336
337 RetainPtr<const CPDF_Array> inklist = annot_dict->GetArrayFor("InkList");
338 ASSERT_TRUE(inklist);
339 ASSERT_EQ(1u, inklist->size());
340 EXPECT_EQ(kPointCount * 2, inklist->GetArrayAt(0)->size());
341
342 // Remove inklist.
343 EXPECT_TRUE(FPDFAnnot_RemoveInkList(ink_annot.get()));
344 EXPECT_FALSE(annot_dict->KeyExist("InkList"));
345}
346
348 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
349 FPDF_PAGE page = LoadPage(0);
350 ASSERT_TRUE(page);
351
352 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
353
354 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
355 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
356 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
357 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
358 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
359
361
362 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
363
364 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
365 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
366 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
367
368 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
369
370 static const wchar_t kContents[] = L"Bar";
371 ScopedFPDFWideString text = GetFPDFWideString(kContents);
372 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
373
374 FPDF_WCHAR buffer[64];
375 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
376 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
377 EXPECT_EQ(0u,
378 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
379
380 UnloadPage(page);
381}
382
384 ASSERT_TRUE(OpenDocument("bad_annots_entry.pdf"));
385 FPDF_PAGE page = LoadPage(0);
386 ASSERT_TRUE(page);
387
388 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
389 EXPECT_FALSE(FPDFPage_GetAnnot(page, 0));
390
391 UnloadPage(page);
392}
393
395 // Open a file with one annotation and load its first page.
396 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
397 FPDF_PAGE page = LoadPage(0);
398 ASSERT_TRUE(page);
399
400 // This annotation has a malformed appearance stream, which does not have its
401 // normal appearance defined, only its rollover appearance. In this case, its
402 // normal appearance should be generated, allowing the highlight annotation to
403 // still display.
404 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
405 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
406
407 UnloadPage(page);
408}
409
411 const char* checksum = []() {
412 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
413 return "ec1f4ccbd0aecfdea6d53893387a0101";
414 }
415 return "76512832d88017668d9acc7aacd13dae";
416 }();
417
418 // Open a file with multiline markup annotations.
419 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
420 FPDF_PAGE page = LoadPage(0);
421 ASSERT_TRUE(page);
422
423 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
424 CompareBitmap(bitmap.get(), 595, 842, checksum);
425
426 UnloadPage(page);
427}
428
430 // Open a file with one annotation and load its first page.
431 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
432 FPDF_PAGE page = LoadPageNoEvents(0);
433 ASSERT_TRUE(page);
434
435 // Check that there is a total of 1 annotation on its first page.
436 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
437
438 // Check that the annotation is of type "highlight".
439 {
440 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
441 ASSERT_TRUE(annot);
442 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
443
444 // Check that the annotation color is yellow.
445 unsigned int R;
446 unsigned int G;
447 unsigned int B;
448 unsigned int A;
449 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
450 &G, &B, &A));
451 EXPECT_EQ(255u, R);
452 EXPECT_EQ(255u, G);
453 EXPECT_EQ(0u, B);
454 EXPECT_EQ(255u, A);
455
456 // Check that the author is correct.
457 static const char kAuthorKey[] = "T";
458 EXPECT_EQ(FPDF_OBJECT_STRING,
459 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
460 unsigned long length_bytes =
461 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
462 ASSERT_EQ(28u, length_bytes);
463 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
464 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
465 length_bytes));
466 EXPECT_EQ(L"Jae Hyun Park", GetPlatformWString(buf.data()));
467
468 // Check that the content is correct.
469 EXPECT_EQ(
471 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kContents));
472 length_bytes = FPDFAnnot_GetStringValue(
473 annot.get(), pdfium::annotation::kContents, nullptr, 0);
474 ASSERT_EQ(2690u, length_bytes);
475 buf = GetFPDFWideStringBuffer(length_bytes);
476 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(),
478 buf.data(), length_bytes));
479 static const wchar_t kContents[] =
480 L"This is a note for that highlight annotation. Very long highlight "
481 "annotation. Long long long Long long longLong long longLong long "
482 "longLong long longLong long longLong long longLong long longLong long "
483 "longLong long longLong long longLong long longLong long longLong long "
484 "longLong long longLong long longLong long longLong long longLong long "
485 "longLong long longLong long longLong long longLong long longLong long "
486 "longLong long longLong long longLong long longLong long longLong long "
487 "longLong long longLong long longLong long longLong long longLong long "
488 "longLong long longLong long longLong long longLong long longLong long "
489 "longLong long longLong long longLong long longLong long longLong long "
490 "longLong long longLong long longLong long longLong long longLong long "
491 "longLong long longLong long longLong long longLong long longLong long "
492 "longLong long longLong long longLong long longLong long longLong long "
493 "longLong long longLong long longLong long longLong long longLong long "
494 "longLong long longLong long longLong long longLong long longLong long "
495 "longLong long longLong long longLong long longLong long longLong long "
496 "longLong long longLong long longLong long longLong long longLong long "
497 "longLong long longLong long longLong long longLong long longLong long "
498 "longLong long longLong long longLong long longLong long longLong long "
499 "longLong long long. END";
500 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
501
502 // Check that the quadpoints are correct.
503 FS_QUADPOINTSF quadpoints;
504 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
505 EXPECT_EQ(115.802643f, quadpoints.x1);
506 EXPECT_EQ(718.913940f, quadpoints.y1);
507 EXPECT_EQ(157.211182f, quadpoints.x4);
508 EXPECT_EQ(706.264465f, quadpoints.y4);
509 }
510 UnloadPageNoEvents(page);
511}
512
514 // Open a file with three annotations and load its first page.
515 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
516 FPDF_PAGE page = LoadPageNoEvents(0);
517 ASSERT_TRUE(page);
518
519 // Check that there is a total of 3 annotation on its first page.
520 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
521
522 {
523 // Check that the third annotation is of type "ink".
524 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
525 ASSERT_TRUE(annot);
526 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
527
528 // Check that the annotation color is blue with opacity.
529 unsigned int R;
530 unsigned int G;
531 unsigned int B;
532 unsigned int A;
533 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
534 &G, &B, &A));
535 EXPECT_EQ(0u, R);
536 EXPECT_EQ(0u, G);
537 EXPECT_EQ(255u, B);
538 EXPECT_EQ(76u, A);
539
540 // Check that there is no content.
541 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(
542 annot.get(), pdfium::annotation::kContents, nullptr, 0));
543
544 // Check that the rectangle coordinates are correct.
545 // Note that upon rendering, the rectangle coordinates will be adjusted.
546 FS_RECTF rect;
547 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
548 EXPECT_EQ(351.820404f, rect.left);
549 EXPECT_EQ(583.830688f, rect.bottom);
550 EXPECT_EQ(475.336090f, rect.right);
551 EXPECT_EQ(681.535034f, rect.top);
552 }
553 {
554 const char* expected_hash = []() {
555 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
556#if BUILDFLAG(IS_WIN)
557 return "b4698da8e2f9e8cb82b7bbb6e7d559a9";
558#elif BUILDFLAG(IS_APPLE)
559 return "e3da57011a3d66238d15be1bedcb6696";
560#else
561 return "c2404a7a9a86ee78487cd1993949c56d";
562#endif
563 }
564 return "354002e1c4386d38fdde29ef8d61074a";
565 }();
566 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
567 CompareBitmap(bitmap.get(), 612, 792, expected_hash);
568 }
569 UnloadPageNoEvents(page);
570}
571
573 // Open a file with one annotation and load its first page.
574 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
575 FPDF_PAGE page = LoadPage(0);
576 ASSERT_TRUE(page);
577
578 // Add an annotation with an illegal subtype.
579 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
580
581 UnloadPage(page);
582}
583
585 // Open a file with no annotation and load its first page.
586 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
587 FPDF_PAGE page = LoadPage(0);
588 ASSERT_TRUE(page);
589 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
590
591 {
592 // Add a text annotation to the page.
593 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
594 ASSERT_TRUE(annot);
595
596 // Check that there is now 1 annotations on this page.
597 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
598
599 // Check that the subtype of the annotation is correct.
600 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
601 }
602
603 {
604 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
605 ASSERT_TRUE(annot);
606 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
607
608 // Set the color of the annotation.
609 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
610 102, 153, 204));
611 // Check that the color has been set correctly.
612 unsigned int R;
613 unsigned int G;
614 unsigned int B;
615 unsigned int A;
616 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
617 &G, &B, &A));
618 EXPECT_EQ(51u, R);
619 EXPECT_EQ(102u, G);
620 EXPECT_EQ(153u, B);
621 EXPECT_EQ(204u, A);
622
623 // Change the color of the annotation.
624 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
625 153, 102, 51));
626 // Check that the color has been set correctly.
627 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
628 &G, &B, &A));
629 EXPECT_EQ(204u, R);
630 EXPECT_EQ(153u, G);
631 EXPECT_EQ(102u, B);
632 EXPECT_EQ(51u, A);
633
634 // Set the annotation rectangle.
635 FS_RECTF rect;
636 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
637 EXPECT_EQ(0.f, rect.left);
638 EXPECT_EQ(0.f, rect.right);
639 rect.left = 35;
640 rect.bottom = 150;
641 rect.right = 53;
642 rect.top = 165;
643 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
644 // Check that the annotation rectangle has been set correctly.
645 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
646 EXPECT_EQ(35.f, rect.left);
647 EXPECT_EQ(150.f, rect.bottom);
648 EXPECT_EQ(53.f, rect.right);
649 EXPECT_EQ(165.f, rect.top);
650
651 // Set the content of the annotation.
652 static const wchar_t kContents[] = L"Hello! This is a customized content.";
653 ScopedFPDFWideString text = GetFPDFWideString(kContents);
654 ASSERT_TRUE(FPDFAnnot_SetStringValue(
655 annot.get(), pdfium::annotation::kContents, text.get()));
656 // Check that the content has been set correctly.
657 unsigned long length_bytes = FPDFAnnot_GetStringValue(
658 annot.get(), pdfium::annotation::kContents, nullptr, 0);
659 ASSERT_EQ(74u, length_bytes);
660 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
661 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(),
663 buf.data(), length_bytes));
664 EXPECT_EQ(kContents, GetPlatformWString(buf.data()));
665 }
666 UnloadPage(page);
667}
668
670 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
671 FPDF_PAGE page = LoadPage(0);
672 ASSERT_TRUE(page);
673 {
674 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
675 CompareBitmap(bitmap.get(), 200, 200, pdfium::HelloWorldChecksum());
676 }
677 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
678
679 constexpr char kUri[] = "https://pdfium.org/";
680
681 {
682 // Add a link annotation to the page and set its URI.
683 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_LINK));
684 ASSERT_TRUE(annot);
685 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
686 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
687 EXPECT_TRUE(FPDFAnnot_SetURI(annot.get(), kUri));
688 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
689
690 // Negative tests:
691 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, nullptr));
692 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
693 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), nullptr));
694 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
695 EXPECT_FALSE(FPDFAnnot_SetURI(nullptr, kUri));
696 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
697
698 // Position the link on top of "Hello, world!" without a border.
699 const FS_RECTF kRect = {19.0f, 48.0f, 85.0f, 60.0f};
700 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &kRect));
701 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/0.0f,
702 /*vertical_radius=*/0.0f,
703 /*border_width=*/0.0f));
704
705 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
706 kUri);
707 }
708
709 {
710 // Add an ink annotation to the page. Trying to add a link to it fails.
711 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
712 ASSERT_TRUE(annot);
713 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
714 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
715 EXPECT_FALSE(FPDFAnnot_SetURI(annot.get(), kUri));
716 }
717
718 // Remove the ink annotation added above for negative testing.
719 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
720 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
721
722 // Save the document, closing the page.
723 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
724 UnloadPage(page);
725
726 // Reopen the document and make sure it still renders the same. Since the link
727 // does not have a border, it does not affect the rendering.
728 ASSERT_TRUE(OpenSavedDocument());
729 page = LoadSavedPage(0);
730 ASSERT_TRUE(page);
731 VerifySavedRendering(page, 200, 200, pdfium::HelloWorldChecksum());
732 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
733
734 {
735 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
736 ASSERT_TRUE(annot);
737 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
738 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()), kUri);
739 VerifyUriActionInLink(document(), FPDFLink_GetLinkAtPoint(page, 40.0, 50.0),
740 kUri);
741 }
742
743 CloseSavedPage(page);
744 CloseSavedDocument();
745}
746
748 // Open a file with one annotation and load its first page.
749 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
750 FPDF_PAGE page = LoadPage(0);
751 ASSERT_TRUE(page);
752
753 // Check that there is a total of one annotation on its first page, and verify
754 // its quadpoints.
755 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
756 FS_QUADPOINTSF quadpoints;
757 {
758 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
759 ASSERT_TRUE(annot);
760 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
761 EXPECT_EQ(115.802643f, quadpoints.x1);
762 EXPECT_EQ(718.913940f, quadpoints.y1);
763 EXPECT_EQ(157.211182f, quadpoints.x4);
764 EXPECT_EQ(706.264465f, quadpoints.y4);
765 }
766
767 // Add an underline annotation to the page and set its quadpoints.
768 {
769 ScopedFPDFAnnotation annot(
771 ASSERT_TRUE(annot);
772 quadpoints.x1 = 140.802643f;
773 quadpoints.x3 = 140.802643f;
774 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
775 }
776
777 // Save the document and close the page.
778 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
779 UnloadPage(page);
780
781 // Open the saved document.
782 const char* checksum = []() {
783 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
784#if BUILDFLAG(IS_WIN)
785 return "c50012ab122cd3706d39f371ca7462ee";
786#elif BUILDFLAG(IS_APPLE)
787 return "24994ad69aa612a66d183eaf9a92aa06";
788#else
789 return "798fa41303381c9ba6d99092f5cd4d2b";
790#endif
791 }
792 return "dba153419f67b7c0c0e3d22d3e8910d5";
793 }();
794
795 ASSERT_TRUE(OpenSavedDocument());
796 page = LoadSavedPage(0);
797 ASSERT_TRUE(page);
798 VerifySavedRendering(page, 612, 792, checksum);
799
800 // Check that the saved document has 2 annotations on the first page
801 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
802
803 {
804 // Check that the second annotation is an underline annotation and verify
805 // its quadpoints.
806 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
807 ASSERT_TRUE(new_annot);
808 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
809 FS_QUADPOINTSF new_quadpoints;
810 ASSERT_TRUE(
811 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
812 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
813 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
814 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
815 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
816 }
817
818 CloseSavedPage(page);
819 CloseSavedDocument();
820}
821
823 // Open a file with four annotations and load its first page.
824 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
825 FPDF_PAGE page = LoadPage(0);
826 ASSERT_TRUE(page);
827 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
828
829 // Retrieve the highlight annotation.
830 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
831 ASSERT_TRUE(annot);
833
834 FS_QUADPOINTSF quadpoints;
835 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
836
837 {
838 // Verify the current one set of quadpoints.
839 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
840
841 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
842 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
843 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
844 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
845 }
846
847 {
848 // Update the quadpoints.
849 FS_QUADPOINTSF new_quadpoints = quadpoints;
850 new_quadpoints.y1 -= 20.f;
851 new_quadpoints.y2 -= 20.f;
852 new_quadpoints.y3 -= 20.f;
853 new_quadpoints.y4 -= 20.f;
854 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
855
856 // Verify added quadpoint set
857 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
858 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
859 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
860 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
861 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
862 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
863 }
864
865 {
866 // Append a new set of quadpoints.
867 FS_QUADPOINTSF new_quadpoints = quadpoints;
868 new_quadpoints.y1 += 20.f;
869 new_quadpoints.y2 += 20.f;
870 new_quadpoints.y3 += 20.f;
871 new_quadpoints.y4 += 20.f;
872 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
873
874 // Verify added quadpoint set
875 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
876 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
877 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
878 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
879 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
880 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
881 }
882
883 {
884 // Setting and getting quadpoints at out-of-bound index should fail
885 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
886 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
887 }
888
890
891 // Retrieve the square annotation
892 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
893
894 {
895 // Check that attempting to set its quadpoints would fail
896 ASSERT_TRUE(squareAnnot);
897 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
898 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
899 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
900 }
901
902 FPDFPage_CloseAnnot(squareAnnot);
903 UnloadPage(page);
904}
905
907 const char* md5_original = []() {
908 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
909#if BUILDFLAG(IS_WIN)
910 return "3867f6e34e801abad4e98811f6d7b887";
911#elif BUILDFLAG(IS_APPLE)
912 return "32cd26430a31752e612475bf881cc597";
913#else
914 return "2a9d1df839d5ec81a49f982347d9656c";
915#endif
916 }
917#if BUILDFLAG(IS_APPLE)
918 return "fc59468d154f397fd298c69f47ef565a";
919#else
920 return "0e27376094f11490f74c65f3dc3a42c5";
921#endif
922 }();
923 const char* md5_modified_highlight = []() {
924 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
925#if BUILDFLAG(IS_WIN)
926 return "a6f6df562dcf96b3670d40fa2999a582";
927#elif BUILDFLAG(IS_APPLE)
928 return "9a969b7089f49c029b10cf8c208b40dd";
929#else
930 return "0fb1653db0e8e8f7ce5d726bb0074bb5";
931#endif
932 }
933#if BUILDFLAG(IS_APPLE)
934 return "e64bf648f6e9354d1f3eedb47a2c9498";
935#else
936 return "66f3caef3a7d488a4fa1ad37fc06310e";
937#endif
938 }();
939 const char* md5_modified_square = []() {
940 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
941#if BUILDFLAG(IS_WIN)
942 return "cebb3bd3209f63f6dfd15b8425229e90";
943#elif BUILDFLAG(IS_APPLE)
944 return "613102f8b6d74d6d9f95c8eacd17b756";
945#else
946 return "879c77a2cb9f79ba65ffe0bbdd720ce3";
947#endif
948 }
949#if BUILDFLAG(IS_APPLE)
950 return "a66591662c8e7ad3c6059952e234bebf";
951#else
952 return "a456dad0bc6801ee2d6408a4394af563";
953#endif
954 }();
955
956 // Open a file with four annotations and load its first page.
957 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
958 FPDF_PAGE page = LoadPage(0);
959 ASSERT_TRUE(page);
960 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
961
962 // Check that the original file renders correctly.
963 {
964 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
965 CompareBitmap(bitmap.get(), 612, 792, md5_original);
966 }
967
968 FS_RECTF rect;
969 FS_RECTF new_rect;
970
971 // Retrieve the highlight annotation which has its AP stream already defined.
972 {
973 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
974 ASSERT_TRUE(annot);
975 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
976
977 // Check that color cannot be set when an AP stream is defined already.
978 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
979 102, 153, 204));
980
981 // Verify its attachment points.
982 FS_QUADPOINTSF quadpoints;
983 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
984 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
985 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
986 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
987 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
988
989 // Check that updating the attachment points would succeed.
990 quadpoints.x1 -= 50.f;
991 quadpoints.x2 -= 50.f;
992 quadpoints.x3 -= 50.f;
993 quadpoints.x4 -= 50.f;
994 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
995 FS_QUADPOINTSF new_quadpoints;
996 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
997 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
998 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
999 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
1000 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
1001
1002 // Check that updating quadpoints does not change the annotation's position.
1003 {
1004 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1005 CompareBitmap(bitmap.get(), 612, 792, md5_original);
1006 }
1007
1008 // Verify its annotation rectangle.
1009 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1010 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
1011 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
1012 EXPECT_NEAR(136.325f, rect.right, 0.001f);
1013 EXPECT_NEAR(721.292f, rect.top, 0.001f);
1014
1015 // Check that updating the rectangle would succeed.
1016 rect.left -= 60.f;
1017 rect.right -= 60.f;
1018 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1019 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1020 EXPECT_EQ(rect.right, new_rect.right);
1021 }
1022
1023 // Check that updating the rectangle changes the annotation's position.
1024 {
1025 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1026 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
1027 }
1028
1029 {
1030 // Retrieve the square annotation which has its AP stream already defined.
1031 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1032 ASSERT_TRUE(annot);
1033 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
1034
1035 // Check that updating the rectangle would succeed.
1036 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1037 rect.left += 70.f;
1038 rect.right += 70.f;
1039 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1040 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1041 EXPECT_EQ(rect.right, new_rect.right);
1042
1043 // Check that updating the rectangle changes the square annotation's
1044 // position.
1045 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1046 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
1047 }
1048
1049 UnloadPage(page);
1050}
1051
1053 // Open a file with multiline markup annotations.
1054 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
1055 FPDF_PAGE page = LoadPage(0);
1056 ASSERT_TRUE(page);
1057 {
1058 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1059 ASSERT_TRUE(annot);
1060
1061 // This is a three line annotation.
1062 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
1063 }
1064 UnloadPage(page);
1065
1066 // null annotation should return 0
1067 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
1068}
1069
1071 // Open a file with 3 annotations on its first page.
1072 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
1073 FPDF_PAGE page = LoadPageNoEvents(0);
1074 ASSERT_TRUE(page);
1075 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1076
1077 FS_RECTF rect;
1078
1079 // Check that the annotations have the expected rectangle coordinates.
1080 {
1081 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1082 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1083 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1084 }
1085
1086 {
1087 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
1088 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1089 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
1090 }
1091
1092 {
1093 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1094 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1095 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1096 }
1097
1098 // Check that nothing happens when attempting to remove an annotation with an
1099 // out-of-bound index.
1100 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
1101 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
1102 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1103
1104 // Remove the second annotation.
1105 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
1106 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1107 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
1108
1109 // Save the document and close the page.
1110 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1111 UnloadPageNoEvents(page);
1112
1113 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
1114 // Open the saved document.
1115 std::string new_file = GetString();
1116 FPDF_FILEACCESS file_access = {}; // Aggregate initialization
1117 static_assert(std::is_aggregate_v<decltype(file_access)>);
1118 file_access.m_FileLen = new_file.size();
1119 file_access.m_GetBlock = GetBlockFromString;
1120 file_access.m_Param = &new_file;
1121 ScopedFPDFDocument new_doc(FPDF_LoadCustomDocument(&file_access, nullptr));
1122 ASSERT_TRUE(new_doc);
1123 ScopedFPDFPage new_page(FPDF_LoadPage(new_doc.get(), 0));
1124 ASSERT_TRUE(new_page);
1125
1126 // Check that the saved document has 2 annotations on the first page.
1127 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page.get()));
1128
1129 // Check that the remaining 2 annotations are the original 1st and 3rd ones
1130 // by verifying their rectangle coordinates.
1131 {
1132 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page.get(), 0));
1133 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1134 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
1135 }
1136
1137 {
1138 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page.get(), 1));
1139 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
1140 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
1141 }
1142}
1143
1145 const char* md5_modified_path = []() {
1146 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1147#if BUILDFLAG(IS_WIN)
1148 return "c8ae5b9ebd9982d9fde526f50f936971";
1149#elif BUILDFLAG(IS_APPLE)
1150 return "3adf48360ca55e8794a9fc9f1ea87df1";
1151#else
1152 return "94f7f4568385c16498604ddc46f18be9";
1153#endif
1154 }
1155#if BUILDFLAG(IS_APPLE)
1156 return "34614087e04b729b7b8c37739dcf9af9";
1157#else
1158 return "31a94d22460171cd83169daf6a6956ee";
1159#endif
1160 }();
1161 const char* md5_two_paths = []() {
1162 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1163#if BUILDFLAG(IS_WIN)
1164 return "d29d4258bd9344abf20bb55e6679c065";
1165#elif BUILDFLAG(IS_APPLE)
1166 return "5f7d44d3a4ffaadb6bf20b4f1ac2a1f0";
1167#else
1168 return "1052cd0fe1c3e73865fc842525245551";
1169#endif
1170 }
1171#if BUILDFLAG(IS_APPLE)
1172 return "6cdaf6b3e5145f435d8ccae6db5cf9af";
1173#else
1174 return "ed49fefef45f14121f8150cde10006c4";
1175#endif
1176 }();
1177 const char* md5_new_annot = []() {
1178 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1179#if BUILDFLAG(IS_WIN)
1180 return "9ca0a274d1ae0db09ad814ee455dd88c";
1181#elif BUILDFLAG(IS_APPLE)
1182 return "71c8fb8eee9720c19851c48745dde152";
1183#else
1184 return "f522e1262f487cc1976bb3fc585ef469";
1185#endif
1186 }
1187#if BUILDFLAG(IS_APPLE)
1188 return "e6015f42eb81ed6003224cb2f27dcb51";
1189#else
1190 return "2e567a33390cd2ebad9dc33d82a8b054";
1191#endif
1192 }();
1193
1194 // Open a file with two annotations and load its first page.
1195 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1196 FPDF_PAGE page = LoadPage(0);
1197 ASSERT_TRUE(page);
1198 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1199
1200 // Check that the page renders correctly.
1201 {
1202 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1203 CompareBitmap(bitmap.get(), 595, 842, AnnotationStampWithApChecksum());
1204 }
1205
1206 {
1207 // Retrieve the stamp annotation which has its AP stream already defined.
1208 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1209 ASSERT_TRUE(annot);
1210
1211 // Check that this annotation has one path object and retrieve it.
1212 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1213 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1214 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
1215 EXPECT_FALSE(path);
1216 path = FPDFAnnot_GetObject(annot.get(), 0);
1218 EXPECT_TRUE(path);
1219
1220 // Modify the color of the path object.
1221 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 0, 0, 0, 255));
1222 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
1223
1224 // Check that the page with the modified annotation renders correctly.
1225 {
1226 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1227 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
1228 }
1229
1230 // Add a second path object to the same annotation.
1231 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
1232 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
1233 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(dot, 255, 0, 0, 100));
1234 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(dot, 14));
1235 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
1236 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
1237 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
1238
1239 // The object is in the annontation, not in the page, so the page object
1240 // array should not change.
1241 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1242
1243 // Check that the page with an annotation with two paths renders correctly.
1244 {
1245 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1246 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
1247 }
1248
1249 // Delete the newly added path object.
1250 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
1251 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1252 ASSERT_EQ(32, FPDFPage_CountObjects(page));
1253 }
1254
1255 // Check that the page renders the same as before.
1256 {
1257 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1258 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
1259 }
1260
1261 FS_RECTF rect;
1262
1263 {
1264 // Create another stamp annotation and set its annotation rectangle.
1265 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1266 ASSERT_TRUE(annot);
1267 rect.left = 200.f;
1268 rect.bottom = 400.f;
1269 rect.right = 500.f;
1270 rect.top = 600.f;
1271 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1272
1273 // Add a new path to the annotation.
1274 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
1275 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
1276 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
1277 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
1278 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
1279 EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 0, 255, 255, 180));
1280 EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f));
1281 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1282 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
1283 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1284
1285 // Check that the annotation's bounding box came from its rectangle.
1286 FS_RECTF new_rect;
1287 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1288 EXPECT_EQ(rect.left, new_rect.left);
1289 EXPECT_EQ(rect.bottom, new_rect.bottom);
1290 EXPECT_EQ(rect.right, new_rect.right);
1291 EXPECT_EQ(rect.top, new_rect.top);
1292 }
1293
1294 // Save the document and close the page.
1295 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1296 UnloadPage(page);
1297
1298 // Open the saved document.
1299 ASSERT_TRUE(OpenSavedDocument());
1300 page = LoadSavedPage(0);
1301 ASSERT_TRUE(page);
1302 VerifySavedRendering(page, 595, 842, md5_new_annot);
1303
1304 // Check that the document has a correct count of annotations and objects.
1305 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
1306
1307 {
1308 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1309 ASSERT_TRUE(annot);
1310 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1311
1312 // Check that the new annotation's rectangle is as defined.
1313 FS_RECTF new_rect;
1314 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
1315 EXPECT_EQ(rect.left, new_rect.left);
1316 EXPECT_EQ(rect.bottom, new_rect.bottom);
1317 EXPECT_EQ(rect.right, new_rect.right);
1318 EXPECT_EQ(rect.top, new_rect.top);
1319 }
1320
1321 CloseSavedPage(page);
1322 CloseSavedDocument();
1323}
1324
1326 // Open a file with an annotation and load its first page.
1327 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
1328 FPDF_PAGE page = LoadPage(0);
1329 ASSERT_TRUE(page);
1330
1331 // Check that the page renders correctly.
1332 {
1333 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1334 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1335 }
1336
1337 {
1338 // Retrieve the annotation.
1339 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1340 ASSERT_TRUE(annot);
1341
1342 // Check that the original flag values are as expected.
1343 int flags = FPDFAnnot_GetFlags(annot.get());
1344 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_INVISIBLE);
1345 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1346 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
1347 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOZOOM);
1348 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOROTATE);
1349 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_NOVIEW);
1350 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_READONLY);
1351 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_LOCKED);
1352 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW);
1353
1354 // Set the HIDDEN flag.
1355 flags |= FPDF_ANNOT_FLAG_HIDDEN;
1356 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1357 flags = FPDFAnnot_GetFlags(annot.get());
1358 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1359 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
1360
1361 // Check that the page renders correctly without rendering the annotation.
1362 {
1363 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1364 CompareBitmap(bitmap.get(), 612, 792, pdfium::kBlankPage612By792Checksum);
1365 }
1366
1367 // Unset the HIDDEN flag.
1368 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
1369 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
1370 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
1371 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
1372 flags = FPDFAnnot_GetFlags(annot.get());
1373 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
1374 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
1375
1376 // Check that the page renders correctly as before.
1377 {
1378 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1379 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
1380 }
1381 }
1382
1383 UnloadPage(page);
1384}
1385
1387 const char* md5_new_image = []() {
1388 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1389#if BUILDFLAG(IS_WIN)
1390 return "3515dee02973f010516e4c6c774ee281";
1391#elif BUILDFLAG(IS_APPLE)
1392 return "f740480598ff3732fb31871634509eec";
1393#else
1394 return "77bf1781c60370bfcd8d81cf91ab7b09";
1395#endif
1396 }
1397#if BUILDFLAG(IS_APPLE)
1398 return "17ac49518eabbb6a7632a547269c40a3";
1399#else
1400 return "e79446398d4508bc2cb47e6cf2a677ed";
1401#endif
1402 }();
1403 const char* md5_modified_image = []() {
1404 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1405#if BUILDFLAG(IS_WIN)
1406 return "aeb7cfb0bf6ac2bcdef4412276f3bad4";
1407#elif BUILDFLAG(IS_APPLE)
1408 return "5beae8949ee6b5c99fe17475e90aea02";
1409#else
1410 return "cfa8aa132250a1c0fec505bd13c15916";
1411#endif
1412 }
1413#if BUILDFLAG(IS_APPLE)
1414 return "25bf5ec7c197cf9ff3d12b41fc336b25";
1415#else
1416 return "dcb492d8e32528dd81bb60fa5bc900f8";
1417#endif
1418 }();
1419
1420 // Open a file with two annotations and load its first page.
1421 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1422 FPDF_PAGE page = LoadPage(0);
1423 ASSERT_TRUE(page);
1424 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1425
1426 // Check that the page renders correctly.
1427 {
1428 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1429 CompareBitmap(bitmap.get(), 595, 842, AnnotationStampWithApChecksum());
1430 }
1431
1432 constexpr int kBitmapSize = 200;
1433 FPDF_BITMAP image_bitmap;
1434
1435 {
1436 // Create a stamp annotation and set its annotation rectangle.
1437 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1438 ASSERT_TRUE(annot);
1439 FS_RECTF rect;
1440 rect.left = 200.f;
1441 rect.bottom = 600.f;
1442 rect.right = 400.f;
1443 rect.top = 800.f;
1444 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1445
1446 // Add a solid-color translucent image object to the new annotation.
1447 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
1448 ASSERT_TRUE(FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize,
1449 kBitmapSize, 0xeeeecccc));
1450 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
1451 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
1452 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
1453 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1454 static constexpr FS_MATRIX kBitmapScaleMatrix{kBitmapSize, 0, 0,
1455 kBitmapSize, 0, 0};
1456 ASSERT_TRUE(FPDFPageObj_SetMatrix(image_object, &kBitmapScaleMatrix));
1457 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
1458 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
1459 }
1460
1461 // Check that the page renders correctly with the new image object.
1462 {
1463 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1464 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
1465 }
1466
1467 {
1468 // Retrieve the newly added stamp annotation and its image object.
1469 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1470 ASSERT_TRUE(annot);
1471 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1472 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
1473 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
1474
1475 // Modify the image in the new annotation.
1476 ASSERT_TRUE(FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize,
1477 kBitmapSize, 0xff000000));
1478 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
1479 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
1480 }
1481
1482 // Save the document and close the page.
1483 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1484 UnloadPage(page);
1485 FPDFBitmap_Destroy(image_bitmap);
1486
1487 // Test that the saved document renders the modified image object correctly.
1488 VerifySavedDocument(595, 842, md5_modified_image);
1489}
1490
1492 const char* md5_new_text = []() {
1493 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1494#if BUILDFLAG(IS_WIN)
1495 return "9fbad802120d58b2b8b7edd043eeaf55";
1496#elif BUILDFLAG(IS_APPLE)
1497 return "eefdf26393df536e7f125816e7d967ff";
1498#else
1499 return "f89c413c7155ae9b4a0b7c8e4013613e";
1500#endif
1501 }
1502#if BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_ARM64)
1503 return "8eabf79dcdcfc6474c593bc60d996def";
1504#elif BUILDFLAG(IS_APPLE) && !defined(ARCH_CPU_ARM64)
1505 return "5d449d36926c9f212c6cdb6c276d18cc";
1506#else
1507 return "a9532f555aca2fd099e2107fa40b61e6";
1508#endif
1509 }();
1510 const char* md5_modified_text = []() {
1511 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1512#if BUILDFLAG(IS_WIN)
1513 return "850564e273ad58f651af09d880103e82";
1514#elif BUILDFLAG(IS_APPLE)
1515 return "25c03a641c8a7cac9845f8d38e54f90b";
1516#else
1517 return "707320c806ed846c73ca2be8b2328bcd";
1518#endif
1519 }
1520#if BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_ARM64)
1521 return "704f3eb56f82377753a816a43de250ea";
1522#elif BUILDFLAG(IS_APPLE) && !defined(ARCH_CPU_ARM64)
1523 return "8c992808db99dbe3d74006358a671f05";
1524#else
1525 return "03cae68322d6a6ba120e738ab325408c";
1526#endif
1527 }();
1528
1529 // Open a file with two annotations and load its first page.
1530 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1531 FPDF_PAGE page = LoadPage(0);
1532 ASSERT_TRUE(page);
1533 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
1534
1535 // Check that the page renders correctly.
1536 {
1537 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1538 CompareBitmap(bitmap.get(), 595, 842, AnnotationStampWithApChecksum());
1539 }
1540
1541 {
1542 // Create a stamp annotation and set its annotation rectangle.
1543 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
1544 ASSERT_TRUE(annot);
1545 FS_RECTF rect;
1546 rect.left = 200.f;
1547 rect.bottom = 550.f;
1548 rect.right = 450.f;
1549 rect.top = 650.f;
1550 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
1551
1552 // Add a translucent text object to the new annotation.
1553 FPDF_PAGEOBJECT text_object =
1554 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1555 EXPECT_TRUE(text_object);
1557 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1558 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1559 EXPECT_TRUE(FPDFPageObj_SetFillColor(text_object, 0, 0, 255, 150));
1560 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1561 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1562 }
1563
1564 // Check that the page renders correctly with the new text object.
1565 {
1566 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1567 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1568 }
1569
1570 {
1571 // Retrieve the newly added stamp annotation and its text object.
1572 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1573 ASSERT_TRUE(annot);
1574 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1575 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1576 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
1577
1578 // Modify the text in the new annotation.
1579 ScopedFPDFWideString new_text = GetFPDFWideString(L"New text!");
1580 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1581 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1582 }
1583
1584 // Check that the page renders correctly with the modified text object.
1585 {
1586 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1587 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1588 }
1589
1590 // Remove the new annotation, and check that the page renders as before.
1591 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
1592 {
1593 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
1594 CompareBitmap(bitmap.get(), 595, 842, AnnotationStampWithApChecksum());
1595 }
1596
1597 UnloadPage(page);
1598}
1599
1601 // Open a file with four annotations and load its first page.
1602 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1603 FPDF_PAGE page = LoadPage(0);
1604 ASSERT_TRUE(page);
1605
1606 static const wchar_t kNewDate[] = L"D:201706282359Z00'00'";
1607
1608 {
1609 // Retrieve the first annotation.
1610 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1611 ASSERT_TRUE(annot);
1612
1613 // Check that a non-existent key does not exist.
1614 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1615
1616 // Check that the string value of a non-string dictionary entry is empty.
1617 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kAP));
1618 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1619 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kAP));
1620 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kAP,
1621 nullptr, 0));
1622
1623 // Check that the string value of the hash is correct.
1624 static const char kHashKey[] = "AAPL:Hash";
1625 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1626 unsigned long length_bytes =
1627 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1628 ASSERT_EQ(66u, length_bytes);
1629 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1630 EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(),
1631 length_bytes));
1632 EXPECT_EQ(L"395fbcb98d558681742f30683a62a2ad",
1633 GetPlatformWString(buf.data()));
1634
1635 // Check that the string value of the modified date is correct.
1636 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1637 length_bytes = FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1638 nullptr, 0);
1639 ASSERT_EQ(44u, length_bytes);
1640 buf = GetFPDFWideStringBuffer(length_bytes);
1641 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot.get(), pdfium::annotation::kM,
1642 buf.data(), length_bytes));
1643 EXPECT_EQ(L"D:201706071721Z00'00'", GetPlatformWString(buf.data()));
1644
1645 // Update the date entry for the annotation.
1646 ScopedFPDFWideString text = GetFPDFWideString(kNewDate);
1647 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), pdfium::annotation::kM,
1648 text.get()));
1649 }
1650
1651 // Save the document and close the page.
1652 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1653 UnloadPage(page);
1654
1655 const char* md5 = []() {
1656 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
1657#if BUILDFLAG(IS_WIN)
1658 return "25b2a610cab9d656b9ab5f72746c9e2e";
1659#elif BUILDFLAG(IS_APPLE)
1660 return "0f6501f8e22441630bdd535363c93e59";
1661#else
1662 return "1814140b1a9a9776546af7894e21d17f";
1663#endif
1664 }
1665#if BUILDFLAG(IS_APPLE)
1666 return "0521eaa52fe2aa43aafd3e4495f63f0b";
1667#else
1668 return "5f19ddad9d48f5b7b87ee7d92f577db6";
1669#endif
1670 }();
1671
1672 // Open the saved annotation.
1673 ASSERT_TRUE(OpenSavedDocument());
1674 page = LoadSavedPage(0);
1675 ASSERT_TRUE(page);
1676 VerifySavedRendering(page, 595, 842, md5);
1677 {
1678 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
1679
1680 // Check that the string value of the modified date is the newly-set
1681 // value.
1682 EXPECT_EQ(FPDF_OBJECT_STRING,
1683 FPDFAnnot_GetValueType(new_annot.get(), pdfium::annotation::kM));
1684 unsigned long length_bytes = FPDFAnnot_GetStringValue(
1685 new_annot.get(), pdfium::annotation::kM, nullptr, 0);
1686 ASSERT_EQ(44u, length_bytes);
1687 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
1688 EXPECT_EQ(44u,
1689 FPDFAnnot_GetStringValue(new_annot.get(), pdfium::annotation::kM,
1690 buf.data(), length_bytes));
1691 EXPECT_EQ(kNewDate, GetPlatformWString(buf.data()));
1692 }
1693
1694 CloseSavedPage(page);
1695 CloseSavedDocument();
1696}
1697
1699 // Open a file with four text annotations and load its first page.
1700 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1701 FPDF_PAGE page = LoadPage(0);
1702 ASSERT_TRUE(page);
1703 {
1704 // First two annotations do not have "MaxLen" attribute.
1705 for (int i = 0; i < 2; i++) {
1706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
1707 ASSERT_TRUE(annot);
1708
1709 // Verify that no "MaxLen" key present.
1710 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1711
1712 float value;
1713 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1714 }
1715
1716 // Annotation in index 2 has "MaxLen" of 10.
1717 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
1718 ASSERT_TRUE(annot);
1719
1720 // Verify that "MaxLen" key present.
1721 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), "MaxLen"));
1722
1723 float value;
1724 EXPECT_TRUE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", &value));
1725 EXPECT_FLOAT_EQ(10.0f, value);
1726
1727 // Check bad inputs.
1728 EXPECT_FALSE(FPDFAnnot_GetNumberValue(nullptr, "MaxLen", &value));
1729 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), nullptr, &value));
1730 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "MaxLen", nullptr));
1731 // Ask for key that exists but is not a number.
1732 EXPECT_FALSE(FPDFAnnot_GetNumberValue(annot.get(), "V", &value));
1733 }
1734
1735 UnloadPage(page);
1736}
1737
1739 // Open a file with four annotations and load its first page.
1740 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1741 FPDF_PAGE page = LoadPage(0);
1742 ASSERT_TRUE(page);
1743
1744 {
1745 static const char kMd5NormalAP[] = "be903df0343fd774fadab9c8900cdf4a";
1746 static constexpr size_t kExpectNormalAPLength = 73970;
1747
1748 // Retrieve the first annotation.
1749 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1750 ASSERT_TRUE(annot);
1751
1752 // Check that the string value of an AP returns the expected length.
1753 unsigned long normal_length_bytes = FPDFAnnot_GetAP(
1754 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1755 ASSERT_EQ(kExpectNormalAPLength, normal_length_bytes);
1756
1757 // Check that the string value of an AP is not returned if the buffer is too
1758 // small. The result buffer should be overwritten with an empty string.
1759 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(normal_length_bytes);
1760 // Write in the buffer to verify it's not overwritten.
1761 UNSAFE_TODO(FXSYS_memcpy(buf.data(), "abcdefgh", 8));
1762 EXPECT_EQ(kExpectNormalAPLength,
1763 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1764 buf.data(), normal_length_bytes - 1));
1765 UNSAFE_TODO(EXPECT_EQ(0, memcmp(buf.data(), "abcdefgh", 8)));
1766
1767 // Check that the string value of an AP is returned through a buffer that is
1768 // the right size.
1769 EXPECT_EQ(kExpectNormalAPLength,
1770 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1771 buf.data(), normal_length_bytes));
1772 EXPECT_EQ(kMd5NormalAP, GenerateMD5Base16(pdfium::as_byte_span(buf).first(
1773 normal_length_bytes)));
1774
1775 // Check that the string value of an AP is returned through a buffer that is
1776 // larger than necessary.
1777 buf = GetFPDFWideStringBuffer(normal_length_bytes + 2);
1778 EXPECT_EQ(kExpectNormalAPLength,
1779 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1780 buf.data(), normal_length_bytes + 2));
1781 EXPECT_EQ(kMd5NormalAP, GenerateMD5Base16(pdfium::as_byte_span(buf).first(
1782 normal_length_bytes)));
1783
1784 // Check that getting an AP for a mode that does not have an AP returns an
1785 // empty string.
1786 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
1787 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1788 ASSERT_EQ(2u, rollover_length_bytes);
1789
1790 buf = GetFPDFWideStringBuffer(1000);
1791 EXPECT_EQ(2u,
1792 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1793 buf.data(), 1000));
1794 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
1795
1796 // Check that setting the AP for an invalid appearance mode fails.
1797 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
1798 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, ap_text.get()));
1799 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1800 ap_text.get()));
1801 EXPECT_FALSE(FPDFAnnot_SetAP(
1802 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, ap_text.get()));
1803
1804 // Set the AP correctly now.
1805 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1806 ap_text.get()));
1807
1808 // Check that the new annotation value is equal to the value we just set.
1809 rollover_length_bytes = FPDFAnnot_GetAP(
1810 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1811 ASSERT_EQ(24u, rollover_length_bytes);
1812 buf = GetFPDFWideStringBuffer(rollover_length_bytes);
1813 EXPECT_EQ(24u,
1814 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1815 buf.data(), rollover_length_bytes));
1816 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
1817
1818 // Check that the Normal AP was not touched when the Rollover AP was set.
1819 buf = GetFPDFWideStringBuffer(normal_length_bytes);
1820 EXPECT_EQ(kExpectNormalAPLength,
1821 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1822 buf.data(), normal_length_bytes));
1823 EXPECT_EQ(kMd5NormalAP, GenerateMD5Base16(pdfium::as_byte_span(buf).first(
1824 normal_length_bytes)));
1825 }
1826
1827 // Save the modified document, then reopen it.
1828 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1829 UnloadPage(page);
1830
1831 ASSERT_TRUE(OpenSavedDocument());
1832 page = LoadSavedPage(0);
1833 ASSERT_TRUE(page);
1834 {
1835 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
1836
1837 // Check that the new annotation value is equal to the value we set before
1838 // saving.
1839 unsigned long rollover_length_bytes = FPDFAnnot_GetAP(
1840 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1841 ASSERT_EQ(24u, rollover_length_bytes);
1842 std::vector<FPDF_WCHAR> buf =
1843 GetFPDFWideStringBuffer(rollover_length_bytes);
1844 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1846 buf.data(), rollover_length_bytes));
1847 EXPECT_EQ(L"new test ap", GetPlatformWString(buf.data()));
1848 }
1849
1850 // Close saved document.
1851 CloseSavedPage(page);
1852 CloseSavedDocument();
1853}
1854
1856 // Open a file with four annotations and load its first page.
1857 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1858 FPDF_PAGE page = LoadPage(0);
1859 ASSERT_TRUE(page);
1860
1861 {
1862 // Retrieve the first annotation.
1863 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1864 ASSERT_TRUE(annot);
1865
1866 // Set Down AP. Normal AP is already set.
1867 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
1868 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1869 ap_text.get()));
1870 EXPECT_EQ(73970u,
1871 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1872 nullptr, 0));
1873 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1874 nullptr, 0));
1875
1876 // Check that setting the Down AP to null removes the Down entry but keeps
1877 // Normal intact.
1878 EXPECT_TRUE(
1879 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1880 EXPECT_EQ(73970u,
1881 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1882 nullptr, 0));
1883 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1884 nullptr, 0));
1885 }
1886
1887 UnloadPage(page);
1888}
1889
1891 // Open a file with four annotations and load its first page.
1892 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1893 FPDF_PAGE page = LoadPage(0);
1894 ASSERT_TRUE(page);
1895
1896 {
1897 // Retrieve the first annotation.
1898 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1899 ASSERT_TRUE(annot);
1900
1901 // Set Down AP. Normal AP is already set.
1902 ScopedFPDFWideString ap_text = GetFPDFWideString(L"new test ap");
1903 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1904 ap_text.get()));
1905 EXPECT_EQ(73970u,
1906 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1907 nullptr, 0));
1908 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1909 nullptr, 0));
1910
1911 // Check that setting the Normal AP to null removes the whole AP dictionary.
1912 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1913 nullptr));
1914 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1915 nullptr, 0));
1916 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1917 nullptr, 0));
1918 }
1919
1920 UnloadPage(page);
1921}
1922
1924 // Open a file with annotations and load its first page.
1925 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
1926 FPDF_PAGE page = LoadPage(0);
1927 ASSERT_TRUE(page);
1928 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
1929
1930 {
1931 // Retrieve the highlight annotation which has its popup defined.
1932 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1933 ASSERT_TRUE(annot);
1934 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1935 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1936 static const char kPopupKey[] = "Popup";
1937 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1938 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1939 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
1940
1941 // Retrieve and verify the popup of the highlight annotation.
1942 ScopedFPDFAnnotation popup(
1943 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1944 ASSERT_TRUE(popup);
1945 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1946 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1947 FS_RECTF rect;
1948 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1949 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1950 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
1951
1952 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1953 // since "IRT" is not a key in |annot|'s dictionary.
1954 static const char kIRTKey[] = "IRT";
1955 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1956 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
1957
1958 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1959 // would fail, since its parent is not an annotation.
1960 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), pdfium::annotation::kP));
1961 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1962 FPDFAnnot_GetValueType(annot.get(), pdfium::annotation::kP));
1963 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), pdfium::annotation::kP));
1964 }
1965
1966 UnloadPage(page);
1967}
1968
1970 // Open file with form text fields.
1971 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
1972 FPDF_PAGE page = LoadPage(0);
1973 ASSERT_TRUE(page);
1974
1975 {
1976 // Retrieve the first annotation: user-editable text field.
1977 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
1978 ASSERT_TRUE(annot);
1979
1980 // Check that the flag values are as expected.
1981 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1982 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1983 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1984 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1985 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
1986 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
1987 }
1988
1989 {
1990 // Retrieve the second annotation: read-only text field.
1991 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
1992 ASSERT_TRUE(annot);
1993
1994 // Check that the flag values are as expected.
1995 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
1996 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1997 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
1998 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
1999 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
2000 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
2001 }
2002
2003 {
2004 // Retrieve the fourth annotation: user-editable password text field.
2005 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
2006 ASSERT_TRUE(annot);
2007
2008 // Check that the flag values are as expected.
2009 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2010 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2011 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2012 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2013 EXPECT_FALSE(flags & FPDF_FORMFLAG_TEXT_MULTILINE);
2014 EXPECT_TRUE(flags & FPDF_FORMFLAG_TEXT_PASSWORD);
2015 }
2016
2017 UnloadPage(page);
2018}
2019
2021 // Open file with form text fields.
2022 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2023 FPDF_PAGE page = LoadPage(0);
2024 ASSERT_TRUE(page);
2025
2026 {
2027 // Retrieve the first annotation: user-editable combobox.
2028 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2029 ASSERT_TRUE(annot);
2030
2031 // Check that the flag values are as expected.
2032 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2033 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2034 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2035 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2036 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2037 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2038 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2039 }
2040
2041 {
2042 // Retrieve the second annotation: regular combobox.
2043 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2044 ASSERT_TRUE(annot);
2045
2046 // Check that the flag values are as expected.
2047 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2048 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2049 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2050 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2051 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2052 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2053 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2054 }
2055
2056 {
2057 // Retrieve the third annotation: read-only combobox.
2058 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2059 ASSERT_TRUE(annot);
2060
2061 // Check that the flag values are as expected.
2062 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2063 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2064 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2065 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2066 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2067 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2068 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2069 }
2070
2071 UnloadPage(page);
2072}
2073
2075 // Open file with form text fields.
2076 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2077 FPDF_PAGE page = LoadPage(0);
2078 ASSERT_TRUE(page);
2079
2080 // Attempt to get an annotation where no annotation exists on page.
2081 static const FS_POINTF kOriginPoint = {0.0f, 0.0f};
2082 EXPECT_FALSE(
2083 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kOriginPoint));
2084
2085 static const FS_POINTF kValidPoint = {120.0f, 120.0f};
2086 {
2087 // Verify there is an annotation.
2088 ScopedFPDFAnnotation annot(
2089 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kValidPoint));
2090 EXPECT_TRUE(annot);
2091 }
2092
2093 // Try other bad inputs at a valid location.
2094 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, &kValidPoint));
2095 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, &kValidPoint));
2096 EXPECT_FALSE(
2097 FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, &kValidPoint));
2098
2099 UnloadPage(page);
2100}
2101
2103 // Open file with form text fields.
2104 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2105 FPDF_PAGE page = LoadPage(0);
2106 ASSERT_TRUE(page);
2107
2108 {
2109 // Retrieve user-editable text field annotation.
2110 static const FS_POINTF kPoint = {105.0f, 118.0f};
2111 ScopedFPDFAnnotation annot(
2112 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
2113 ASSERT_TRUE(annot);
2114
2115 // Check that interactive form annotation flag values are as expected.
2116 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2117 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2118 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2119 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2120 }
2121
2122 {
2123 // Retrieve read-only text field annotation.
2124 static const FS_POINTF kPoint = {105.0f, 202.0f};
2125 ScopedFPDFAnnotation annot(
2126 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
2127 ASSERT_TRUE(annot);
2128
2129 // Check that interactive form annotation flag values are as expected.
2130 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2131 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2132 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2133 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2134 }
2135
2136 UnloadPage(page);
2137}
2138
2140 // Open file with form comboboxes.
2141 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2142 FPDF_PAGE page = LoadPage(0);
2143 ASSERT_TRUE(page);
2144
2145 {
2146 // Retrieve user-editable combobox annotation.
2147 static const FS_POINTF kPoint = {102.0f, 363.0f};
2148 ScopedFPDFAnnotation annot(
2149 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
2150 ASSERT_TRUE(annot);
2151
2152 // Check that interactive form annotation flag values are as expected.
2153 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2154 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2155 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2156 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2157 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2158 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2159 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2160 }
2161
2162 {
2163 // Retrieve regular combobox annotation.
2164 static const FS_POINTF kPoint = {102.0f, 413.0f};
2165 ScopedFPDFAnnotation annot(
2166 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
2167 ASSERT_TRUE(annot);
2168
2169 // Check that interactive form annotation flag values are as expected.
2170 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2171 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
2172 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2173 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2174 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2175 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2176 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2177 }
2178
2179 {
2180 // Retrieve read-only combobox annotation.
2181 static const FS_POINTF kPoint = {102.0f, 513.0f};
2182 ScopedFPDFAnnotation annot(
2183 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, &kPoint));
2184 ASSERT_TRUE(annot);
2185
2186 // Check that interactive form annotation flag values are as expected.
2187 int flags = FPDFAnnot_GetFormFieldFlags(form_handle(), annot.get());
2188 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
2189 EXPECT_FALSE(flags & FPDF_FORMFLAG_REQUIRED);
2190 EXPECT_FALSE(flags & FPDF_FORMFLAG_NOEXPORT);
2191 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
2192 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
2193 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_MULTI_SELECT);
2194 }
2195
2196 UnloadPage(page);
2197}
2198
2200 const char* expected_bitmap = []() {
2201 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
2202 return "a1ea1ceebb26922fae576cb79ce63af0";
2203 }
2204 return "0d9fc05c6762fd788bd23fd87a4967bc";
2205 }();
2206 static constexpr size_t kExpectedMinimumOriginalSize = 1601;
2207
2208 ASSERT_TRUE(OpenDocument("bug_1206.pdf"));
2209
2210 FPDF_PAGE page = LoadPage(0);
2211 ASSERT_TRUE(page);
2212
2213 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2214 const size_t original_size = GetString().size();
2215 EXPECT_LE(kExpectedMinimumOriginalSize, original_size); // Sanity check.
2216 ClearString();
2217
2218 for (size_t i = 0; i < 10; ++i) {
2219 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
2220 CompareBitmap(bitmap.get(), 612, 792, expected_bitmap);
2221
2222 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2223 // TODO(https://crbug.com/pdfium/1206): This is wrong. The size should be
2224 // equal, not bigger.
2225 EXPECT_GT(GetString().size(), original_size);
2226 ClearString();
2227 }
2228
2229 UnloadPage(page);
2230}
2231
2233 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
2234 FPDF_PAGE page = LoadPage(0);
2235 ASSERT_TRUE(page);
2236 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
2237
2238 static const char kTestKey[] = "test";
2239 static const wchar_t kData[] = L"\xf6\xe4";
2240 static const size_t kBufSize = 12;
2241 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(kBufSize);
2242
2243 {
2244 // Add a text annotation to the page.
2245 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
2246 ASSERT_TRUE(annot);
2247 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
2248 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2249
2250 // Make sure there is no test key, add set a value there, and read it back.
2251 std::fill(buf.begin(), buf.end(), 'x');
2252 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
2253 kBufSize));
2254 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2255
2256 ScopedFPDFWideString text = GetFPDFWideString(kData);
2257 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
2258
2259 std::fill(buf.begin(), buf.end(), 'x');
2260 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
2261 kBufSize));
2262 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
2263 }
2264
2265 {
2266 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
2267 ASSERT_TRUE(annot);
2268 const FS_RECTF bounding_rect{206.0f, 753.0f, 339.0f, 709.0f};
2269 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &bounding_rect));
2270 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
2271 EXPECT_EQ(FPDF_ANNOT_STAMP, FPDFAnnot_GetSubtype(annot.get()));
2272 // Also do the same test for its appearance string.
2273 std::fill(buf.begin(), buf.end(), 'x');
2274 ASSERT_EQ(2u,
2275 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2276 buf.data(), kBufSize));
2277 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
2278
2279 ScopedFPDFWideString text = GetFPDFWideString(kData);
2280 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2281 text.get()));
2282
2283 std::fill(buf.begin(), buf.end(), 'x');
2284 ASSERT_EQ(6u,
2285 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
2286 buf.data(), kBufSize));
2287 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
2288 }
2289
2290 UnloadPage(page);
2291
2292 {
2293 // Save a copy, open the copy, and check the annotation again.
2294 // Note that it renders the rotation.
2295 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2296 ASSERT_TRUE(OpenSavedDocument());
2297 FPDF_PAGE saved_page = LoadSavedPage(0);
2298 ASSERT_TRUE(saved_page);
2299
2300 EXPECT_EQ(2, FPDFPage_GetAnnotCount(saved_page));
2301 {
2302 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2303 ASSERT_TRUE(annot);
2304 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2305
2306 std::fill(buf.begin(), buf.end(), 'x');
2307 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
2308 kBufSize));
2309 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
2310 }
2311
2312 {
2313 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
2314 ASSERT_TRUE(annot);
2315 // TODO(thestig): This return FPDF_ANNOT_UNKNOWN for some reason.
2316 // EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
2317
2318 std::fill(buf.begin(), buf.end(), 'x');
2319 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
2320 kBufSize));
2321 EXPECT_EQ(kData, GetPlatformWString(buf.data()));
2322 }
2323
2324 CloseSavedPage(saved_page);
2325 CloseSavedDocument();
2326 }
2327}
2328
2330 // Open a file with combobox widget annotations and load its first page.
2331 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2332 FPDF_PAGE page = LoadPage(0);
2333 ASSERT_TRUE(page);
2334
2335 {
2336 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2337 ASSERT_TRUE(annot);
2338
2339 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2340
2341 annot.reset(FPDFPage_GetAnnot(page, 1));
2342 ASSERT_TRUE(annot);
2343
2344 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2345
2346 // Check bad form handle / annot.
2347 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, nullptr));
2348 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), nullptr));
2349 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(nullptr, annot.get()));
2350 }
2351
2352 UnloadPage(page);
2353}
2354
2356 // Open a file with listbox widget annotations and load its first page.
2357 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2358 FPDF_PAGE page = LoadPage(0);
2359 ASSERT_TRUE(page);
2360
2361 {
2362 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2363 ASSERT_TRUE(annot);
2364
2365 EXPECT_EQ(3, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2366
2367 annot.reset(FPDFPage_GetAnnot(page, 1));
2368 ASSERT_TRUE(annot);
2369
2370 EXPECT_EQ(26, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2371 }
2372
2373 UnloadPage(page);
2374}
2375
2377 // Open a file with ink annotations and load its first page.
2378 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2379 FPDF_PAGE page = LoadPage(0);
2380 ASSERT_TRUE(page);
2381
2382 {
2383 // annotations do not have "Opt" array and will return -1
2384 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2385 ASSERT_TRUE(annot);
2386
2387 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2388
2389 annot.reset(FPDFPage_GetAnnot(page, 1));
2390 ASSERT_TRUE(annot);
2391
2392 EXPECT_EQ(-1, FPDFAnnot_GetOptionCount(form_handle(), annot.get()));
2393 }
2394
2395 UnloadPage(page);
2396}
2397
2399 // Open a file with combobox widget annotations and load its first page.
2400 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2401 FPDF_PAGE page = LoadPage(0);
2402 ASSERT_TRUE(page);
2403
2404 {
2405 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2406 ASSERT_TRUE(annot);
2407
2408 int index = 0;
2409 unsigned long length_bytes =
2410 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2411 ASSERT_EQ(8u, length_bytes);
2412 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2413 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2414 buf.data(), length_bytes));
2415 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
2416
2417 annot.reset(FPDFPage_GetAnnot(page, 1));
2418 ASSERT_TRUE(annot);
2419
2420 index = 0;
2421 length_bytes =
2422 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2423 ASSERT_EQ(12u, length_bytes);
2424 buf = GetFPDFWideStringBuffer(length_bytes);
2425 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2426 buf.data(), length_bytes));
2427 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
2428
2429 index = 25;
2430 length_bytes =
2431 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2432 buf = GetFPDFWideStringBuffer(length_bytes);
2433 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2434 buf.data(), length_bytes));
2435 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
2436
2437 // Indices out of range
2438 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2439 nullptr, 0));
2440 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2441 nullptr, 0));
2442
2443 // Check bad form handle / annot.
2444 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(nullptr, nullptr, 0, nullptr, 0));
2445 EXPECT_EQ(0u,
2446 FPDFAnnot_GetOptionLabel(nullptr, annot.get(), 0, nullptr, 0));
2447 EXPECT_EQ(0u,
2448 FPDFAnnot_GetOptionLabel(form_handle(), nullptr, 0, nullptr, 0));
2449 }
2450
2451 UnloadPage(page);
2452}
2453
2455 // Open a file with listbox widget annotations and load its first page.
2456 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2457 FPDF_PAGE page = LoadPage(0);
2458 ASSERT_TRUE(page);
2459
2460 {
2461 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2462 ASSERT_TRUE(annot);
2463
2464 int index = 0;
2465 unsigned long length_bytes =
2466 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2467 ASSERT_EQ(8u, length_bytes);
2468 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2469 EXPECT_EQ(8u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2470 buf.data(), length_bytes));
2471 EXPECT_EQ(L"Foo", GetPlatformWString(buf.data()));
2472
2473 annot.reset(FPDFPage_GetAnnot(page, 1));
2474 ASSERT_TRUE(annot);
2475
2476 index = 0;
2477 length_bytes =
2478 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2479 ASSERT_EQ(12u, length_bytes);
2480 buf = GetFPDFWideStringBuffer(length_bytes);
2481 EXPECT_EQ(12u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2482 buf.data(), length_bytes));
2483 EXPECT_EQ(L"Apple", GetPlatformWString(buf.data()));
2484
2485 index = 25;
2486 length_bytes =
2487 FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index, nullptr, 0);
2488 ASSERT_EQ(18u, length_bytes);
2489 buf = GetFPDFWideStringBuffer(length_bytes);
2490 EXPECT_EQ(18u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), index,
2491 buf.data(), length_bytes));
2492 EXPECT_EQ(L"Zucchini", GetPlatformWString(buf.data()));
2493
2494 // indices out of range
2495 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), -1,
2496 nullptr, 0));
2497 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 26,
2498 nullptr, 0));
2499 }
2500
2501 UnloadPage(page);
2502}
2503
2505 // Open a file with ink annotations and load its first page.
2506 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2507 FPDF_PAGE page = LoadPage(0);
2508 ASSERT_TRUE(page);
2509
2510 {
2511 // annotations do not have "Opt" array and will return 0
2512 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2513 ASSERT_TRUE(annot);
2514
2515 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2516 nullptr, 0));
2517
2518 annot.reset(FPDFPage_GetAnnot(page, 1));
2519 ASSERT_TRUE(annot);
2520
2521 EXPECT_EQ(0u, FPDFAnnot_GetOptionLabel(form_handle(), annot.get(), 0,
2522 nullptr, 0));
2523 }
2524
2525 UnloadPage(page);
2526}
2527
2529 // Open a file with combobox widget annotations and load its first page.
2530 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2531 FPDF_PAGE page = LoadPage(0);
2532 ASSERT_TRUE(page);
2533
2534 {
2535 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2536 ASSERT_TRUE(annot);
2537
2538 // Checks for Combobox with no Values (/V) or Selected Indices (/I) objects.
2539 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2540 ASSERT_EQ(3, count);
2541 for (int i = 0; i < count; i++) {
2542 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2543 }
2544
2545 annot.reset(FPDFPage_GetAnnot(page, 1));
2546 ASSERT_TRUE(annot);
2547
2548 // Checks for Combobox with Values (/V) object which is just a string.
2549 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2550 ASSERT_EQ(26, count);
2551 for (int i = 0; i < count; i++) {
2552 EXPECT_EQ(i == 1,
2553 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2554 }
2555
2556 // Checks for index outside bound i.e. (index >= CountOption()).
2557 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2558 /*index=*/26));
2559 // Checks for negetive index.
2560 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2561 /*index=*/-1));
2562
2563 // Checks for bad form handle/annot.
2564 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, nullptr, /*index=*/0));
2565 EXPECT_FALSE(
2566 FPDFAnnot_IsOptionSelected(form_handle(), nullptr, /*index=*/0));
2567 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(nullptr, annot.get(), /*index=*/0));
2568 }
2569
2570 UnloadPage(page);
2571}
2572
2574 // Open a file with listbox widget annotations and load its first page.
2575 ASSERT_TRUE(OpenDocument("listbox_form.pdf"));
2576 FPDF_PAGE page = LoadPage(0);
2577 ASSERT_TRUE(page);
2578
2579 {
2580 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2581 ASSERT_TRUE(annot);
2582
2583 // Checks for Listbox with no Values (/V) or Selected Indices (/I) objects.
2584 int count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2585 ASSERT_EQ(3, count);
2586 for (int i = 0; i < count; i++) {
2587 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2588 }
2589
2590 annot.reset(FPDFPage_GetAnnot(page, 1));
2591 ASSERT_TRUE(annot);
2592
2593 // Checks for Listbox with Values (/V) object which is just a string.
2594 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2595 ASSERT_EQ(26, count);
2596 for (int i = 0; i < count; i++) {
2597 EXPECT_EQ(i == 1,
2598 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2599 }
2600
2601 annot.reset(FPDFPage_GetAnnot(page, 3));
2602 ASSERT_TRUE(annot);
2603
2604 // Checks for Listbox with only Selected indices (/I) object which is an
2605 // array with multiple objects.
2606 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2607 ASSERT_EQ(5, count);
2608 for (int i = 0; i < count; i++) {
2609 bool expected = (i == 1 || i == 3);
2610 EXPECT_EQ(expected,
2611 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2612 }
2613
2614 annot.reset(FPDFPage_GetAnnot(page, 4));
2615 ASSERT_TRUE(annot);
2616
2617 // Checks for Listbox with Values (/V) object which is an array with
2618 // multiple objects.
2619 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2620 ASSERT_EQ(5, count);
2621 for (int i = 0; i < count; i++) {
2622 bool expected = (i == 2 || i == 4);
2623 EXPECT_EQ(expected,
2624 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2625 }
2626
2627 annot.reset(FPDFPage_GetAnnot(page, 5));
2628 ASSERT_TRUE(annot);
2629
2630 // Checks for Listbox with both Values (/V) and Selected Indices (/I)
2631 // objects conflict with different lengths.
2632 count = FPDFAnnot_GetOptionCount(form_handle(), annot.get());
2633 ASSERT_EQ(5, count);
2634 for (int i = 0; i < count; i++) {
2635 bool expected = (i == 0 || i == 2);
2636 EXPECT_EQ(expected,
2637 FPDFAnnot_IsOptionSelected(form_handle(), annot.get(), i));
2638 }
2639 }
2640
2641 UnloadPage(page);
2642}
2643
2645 // Open a file with multiple form field annotations and load its first page.
2646 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2647 FPDF_PAGE page = LoadPage(0);
2648 ASSERT_TRUE(page);
2649
2650 {
2651 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2652 ASSERT_TRUE(annot);
2653
2654 // Checks for link annotation.
2655 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2656 /*index=*/0));
2657
2658 annot.reset(FPDFPage_GetAnnot(page, 3));
2659 ASSERT_TRUE(annot);
2660
2661 // Checks for text field annotation.
2662 EXPECT_FALSE(FPDFAnnot_IsOptionSelected(form_handle(), annot.get(),
2663 /*index=*/0));
2664 }
2665
2666 UnloadPage(page);
2667}
2668
2670 // Open a file with combobox annotations and load its first page.
2671 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2672 FPDF_PAGE page = LoadPage(0);
2673 ASSERT_TRUE(page);
2674
2675 {
2676 // All 3 widgets have Tf font size 12.
2677 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2678 ASSERT_TRUE(annot);
2679
2680 float value;
2681 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2682 EXPECT_EQ(12.0, value);
2683
2684 annot.reset(FPDFPage_GetAnnot(page, 1));
2685 ASSERT_TRUE(annot);
2686
2687 float value_two;
2688 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2689 EXPECT_EQ(12.0, value_two);
2690
2691 annot.reset(FPDFPage_GetAnnot(page, 2));
2692 ASSERT_TRUE(annot);
2693
2694 float value_three;
2695 ASSERT_TRUE(
2696 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2697 EXPECT_EQ(12.0, value_three);
2698 }
2699
2700 UnloadPage(page);
2701}
2702
2704 // Open a file with textfield annotations and load its first page.
2705 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2706 FPDF_PAGE page = LoadPage(0);
2707 ASSERT_TRUE(page);
2708
2709 {
2710 // All 4 widgets have Tf font size 12.
2711 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2712 ASSERT_TRUE(annot);
2713
2714 float value;
2715 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2716 EXPECT_EQ(12.0, value);
2717
2718 annot.reset(FPDFPage_GetAnnot(page, 1));
2719 ASSERT_TRUE(annot);
2720
2721 float value_two;
2722 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_two));
2723 EXPECT_EQ(12.0, value_two);
2724
2725 annot.reset(FPDFPage_GetAnnot(page, 2));
2726 ASSERT_TRUE(annot);
2727
2728 float value_three;
2729 ASSERT_TRUE(
2730 FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_three));
2731 EXPECT_EQ(12.0, value_three);
2732
2733 float value_four;
2734 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value_four));
2735 EXPECT_EQ(12.0, value_four);
2736 }
2737
2738 UnloadPage(page);
2739}
2740
2742 // Open a file with ink annotations and load its first page.
2743 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
2744 FPDF_PAGE page = LoadPage(0);
2745 ASSERT_TRUE(page);
2746
2747 {
2748 // Annotations that do not have variable text and will return -1.
2749 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2750 ASSERT_TRUE(annot);
2751
2752 float value;
2753 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2754
2755 annot.reset(FPDFPage_GetAnnot(page, 1));
2756 ASSERT_TRUE(annot);
2757
2758 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2759 }
2760
2761 UnloadPage(page);
2762}
2763
2765 // Open a file with combobox annotations and load its first page.
2766 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
2767 FPDF_PAGE page = LoadPage(0);
2768 ASSERT_TRUE(page);
2769
2770 {
2771 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2772 ASSERT_TRUE(annot);
2773
2774 // Check bad form handle / annot.
2775 float value;
2776 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, annot.get(), &value));
2777 ASSERT_FALSE(FPDFAnnot_GetFontSize(form_handle(), nullptr, &value));
2778 ASSERT_FALSE(FPDFAnnot_GetFontSize(nullptr, nullptr, &value));
2779 }
2780
2781 UnloadPage(page);
2782}
2783
2785 // Open a file with textfield annotations and load its first page.
2786 ASSERT_TRUE(OpenDocument("text_form_negative_fontsize.pdf"));
2787 FPDF_PAGE page = LoadPage(0);
2788 ASSERT_TRUE(page);
2789
2790 {
2791 // Obtain the first annotation, a text field with negative font size, -12.
2792 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2793 ASSERT_TRUE(annot);
2794
2795 float value;
2796 ASSERT_TRUE(FPDFAnnot_GetFontSize(form_handle(), annot.get(), &value));
2797 EXPECT_EQ(-12.0, value);
2798 }
2799
2800 UnloadPage(page);
2801}
2802
2804 // Open a file with textfield annotations and load its first page.
2805 ASSERT_TRUE(OpenDocument("text_form_color.pdf"));
2806 FPDF_PAGE page = LoadPage(0);
2807 ASSERT_TRUE(page);
2808
2809 {
2810 // Obtain the first annotation, a text field with orange color.
2811 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2812 ASSERT_TRUE(annot);
2813
2814 // Negative testing.
2815 unsigned int R;
2816 unsigned int G;
2817 unsigned int B;
2818 ASSERT_FALSE(
2819 FPDFAnnot_GetFontColor(nullptr, nullptr, nullptr, nullptr, nullptr));
2820 ASSERT_FALSE(FPDFAnnot_GetFontColor(form_handle(), nullptr, nullptr,
2821 nullptr, nullptr));
2822 ASSERT_FALSE(FPDFAnnot_GetFontColor(form_handle(), annot.get(), nullptr,
2823 nullptr, nullptr));
2824 ASSERT_FALSE(FPDFAnnot_GetFontColor(form_handle(), annot.get(), &R, nullptr,
2825 nullptr));
2826 ASSERT_FALSE(
2827 FPDFAnnot_GetFontColor(form_handle(), annot.get(), &R, &G, nullptr));
2828
2829 // Positive testing.
2830 ASSERT_TRUE(FPDFAnnot_GetFontColor(form_handle(), annot.get(), &R, &G, &B));
2831 // Make sure it's #ff8000, i.e. orange.
2832 EXPECT_EQ(0xffU, R);
2833 EXPECT_EQ(0x80U, G);
2834 EXPECT_EQ(0x00U, B);
2835 }
2836
2837 UnloadPage(page);
2838}
2839
2841 // Open a file with checkbox and radiobuttons widget annotations and load its
2842 // first page.
2843 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2844 FPDF_PAGE page = LoadPage(0);
2845 ASSERT_TRUE(page);
2846
2847 {
2848 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2849 ASSERT_TRUE(annot);
2850 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2851 }
2852
2853 UnloadPage(page);
2854}
2855
2857 // Open a file with checkbox and radiobutton widget annotations and load its
2858 // first page.
2859 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2860 FPDF_PAGE page = LoadPage(0);
2861 ASSERT_TRUE(page);
2862
2863 {
2864 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2865 ASSERT_TRUE(annot);
2866 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2867 }
2868
2869 UnloadPage(page);
2870}
2871
2873 // Open a file with checkbox and radiobutton widget annotations and load its
2874 // first page.
2875 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2876 FPDF_PAGE page = LoadPage(0);
2877 ASSERT_TRUE(page);
2878
2879 {
2880 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 5));
2881 ASSERT_TRUE(annot);
2882 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2883
2884 annot.reset(FPDFPage_GetAnnot(page, 6));
2885 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2886
2887 annot.reset(FPDFPage_GetAnnot(page, 7));
2888 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2889 }
2890
2891 UnloadPage(page);
2892}
2893
2895 // Open a file with checkbox and radiobutton widget annotations and load its
2896 // first page.
2897 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2898 FPDF_PAGE page = LoadPage(0);
2899 ASSERT_TRUE(page);
2900
2901 {
2902 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
2903 ASSERT_TRUE(annot);
2904 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2905
2906 annot.reset(FPDFPage_GetAnnot(page, 3));
2907 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2908
2909 annot.reset(FPDFPage_GetAnnot(page, 4));
2910 ASSERT_TRUE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2911 }
2912
2913 UnloadPage(page);
2914}
2915
2917 // Open a file with checkbox and radiobuttons widget annotations and load its
2918 // first page.
2919 ASSERT_TRUE(OpenDocument("click_form.pdf"));
2920 FPDF_PAGE page = LoadPage(0);
2921 ASSERT_TRUE(page);
2922
2923 {
2924 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2925 ASSERT_TRUE(annot);
2926 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, annot.get()));
2927 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), nullptr));
2928 ASSERT_FALSE(FPDFAnnot_IsChecked(nullptr, nullptr));
2929 }
2930
2931 UnloadPage(page);
2932}
2933
2935 // Open a file with text widget annotations and load its first page.
2936 ASSERT_TRUE(OpenDocument("text_form.pdf"));
2937 FPDF_PAGE page = LoadPage(0);
2938 ASSERT_TRUE(page);
2939
2940 {
2941 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2942 ASSERT_TRUE(annot);
2943 ASSERT_FALSE(FPDFAnnot_IsChecked(form_handle(), annot.get()));
2944 }
2945
2946 UnloadPage(page);
2947}
2948
2950 ASSERT_TRUE(OpenDocument("multiple_form_types.pdf"));
2951 FPDF_PAGE page = LoadPage(0);
2952 ASSERT_TRUE(page);
2953
2954 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(form_handle(), nullptr));
2955
2956 {
2957 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
2958 ASSERT_TRUE(annot);
2959 EXPECT_EQ(-1, FPDFAnnot_GetFormFieldType(nullptr, annot.get()));
2960 }
2961
2962 static const struct {
2963 int input;
2964 int output;
2965 } kTests[] = {{0, -1},
2971 for (const auto& test : kTests) {
2972 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, test.input));
2973 ASSERT_TRUE(annot);
2974 EXPECT_EQ(test.output,
2975 FPDFAnnot_GetFormFieldType(form_handle(), annot.get()));
2976 }
2977 UnloadPage(page);
2978}
2979
2981 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
2982 FPDF_PAGE page = LoadPage(0);
2983 ASSERT_TRUE(page);
2984
2985 {
2986 EXPECT_EQ(0u,
2987 FPDFAnnot_GetFormFieldValue(form_handle(), nullptr, nullptr, 0));
2988
2989 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
2990 ASSERT_TRUE(annot);
2991
2992 EXPECT_EQ(0u,
2993 FPDFAnnot_GetFormFieldValue(nullptr, annot.get(), nullptr, 0));
2994
2995 unsigned long length_bytes =
2996 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
2997 ASSERT_EQ(2u, length_bytes);
2998 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
2999 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
3000 buf.data(), length_bytes));
3001 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
3002 }
3003 {
3004 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3005 ASSERT_TRUE(annot);
3006
3007 unsigned long length_bytes =
3008 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
3009 ASSERT_EQ(18u, length_bytes);
3010 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3011 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
3012 buf.data(), length_bytes));
3013 EXPECT_EQ(L"Elephant", GetPlatformWString(buf.data()));
3014 }
3015 UnloadPage(page);
3016}
3017
3019 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
3020 FPDF_PAGE page = LoadPage(0);
3021 ASSERT_TRUE(page);
3022
3023 {
3024 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3025 ASSERT_TRUE(annot);
3026
3027 unsigned long length_bytes =
3028 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
3029 ASSERT_EQ(2u, length_bytes);
3030 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3031 EXPECT_EQ(2u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
3032 buf.data(), length_bytes));
3033 EXPECT_EQ(L"", GetPlatformWString(buf.data()));
3034 }
3035 {
3036 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3037 ASSERT_TRUE(annot);
3038
3039 unsigned long length_bytes =
3040 FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(), nullptr, 0);
3041 ASSERT_EQ(14u, length_bytes);
3042 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3043 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldValue(form_handle(), annot.get(),
3044 buf.data(), length_bytes));
3045 EXPECT_EQ(L"Banana", GetPlatformWString(buf.data()));
3046 }
3047 UnloadPage(page);
3048}
3049
3051 ASSERT_TRUE(OpenDocument("text_form.pdf"));
3052 FPDF_PAGE page = LoadPage(0);
3053 ASSERT_TRUE(page);
3054
3055 {
3056 EXPECT_EQ(0u,
3057 FPDFAnnot_GetFormFieldName(form_handle(), nullptr, nullptr, 0));
3058
3059 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3060 ASSERT_TRUE(annot);
3061
3062 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldName(nullptr, annot.get(), nullptr, 0));
3063
3064 unsigned long length_bytes =
3065 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
3066 ASSERT_EQ(18u, length_bytes);
3067 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3068 EXPECT_EQ(18u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
3069 buf.data(), length_bytes));
3070 EXPECT_EQ(L"Text Box", GetPlatformWString(buf.data()));
3071 }
3072 UnloadPage(page);
3073}
3074
3076 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
3077 FPDF_PAGE page = LoadPage(0);
3078 ASSERT_TRUE(page);
3079
3080 {
3081 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3082 ASSERT_TRUE(annot);
3083
3084 unsigned long length_bytes =
3085 FPDFAnnot_GetFormFieldName(form_handle(), annot.get(), nullptr, 0);
3086 ASSERT_EQ(30u, length_bytes);
3087 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3088 EXPECT_EQ(30u, FPDFAnnot_GetFormFieldName(form_handle(), annot.get(),
3089 buf.data(), length_bytes));
3090 EXPECT_EQ(L"Combo_Editable", GetPlatformWString(buf.data()));
3091 }
3092 UnloadPage(page);
3093}
3094
3096 ASSERT_TRUE(OpenDocument("annots.pdf"));
3097 FPDF_PAGE page = LoadPage(0);
3098 ASSERT_TRUE(page);
3099
3100 // Verify widgets are by default focusable.
3101 const FPDF_ANNOTATION_SUBTYPE kDefaultSubtypes[] = {FPDF_ANNOT_WIDGET};
3102 VerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3103
3104 // Expected annot subtypes for page 0 of annots.pdf.
3105 const FPDF_ANNOTATION_SUBTYPE kExpectedAnnotSubtypes[] = {
3109 };
3110
3111 const FPDF_ANNOTATION_SUBTYPE kExpectedDefaultFocusableSubtypes[] = {
3113 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3114 kExpectedAnnotSubtypes,
3115 kExpectedDefaultFocusableSubtypes);
3116
3117 // Make no annotation type focusable using the preferred method.
3118 ASSERT_TRUE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr, 0));
3119 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3120
3121 // Restore the focusable type count back to 1, then set it back to 0 using a
3122 // different method.
3123 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kDefaultSubtypes);
3124 ASSERT_TRUE(
3125 FPDFAnnot_SetFocusableSubtypes(form_handle(), kDefaultSubtypes, 0));
3126 ASSERT_EQ(0, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3127
3128 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3129 kExpectedAnnotSubtypes, {});
3130
3131 // Now make links focusable.
3132 const FPDF_ANNOTATION_SUBTYPE kLinkSubtypes[] = {FPDF_ANNOT_LINK};
3133 SetAndVerifyFocusableAnnotSubtypes(form_handle(), kLinkSubtypes);
3134
3135 const FPDF_ANNOTATION_SUBTYPE kExpectedLinkocusableSubtypes[] = {
3137 VerifyAnnotationSubtypesAndFocusability(form_handle(), page,
3138 kExpectedAnnotSubtypes,
3139 kExpectedLinkocusableSubtypes);
3140
3141 // Test invalid parameters.
3142 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(nullptr, kDefaultSubtypes,
3143 std::size(kDefaultSubtypes)));
3144 EXPECT_FALSE(FPDFAnnot_SetFocusableSubtypes(form_handle(), nullptr,
3145 std::size(kDefaultSubtypes)));
3146 EXPECT_EQ(-1, FPDFAnnot_GetFocusableSubtypesCount(nullptr));
3147
3148 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(1);
3149 EXPECT_FALSE(FPDFAnnot_GetFocusableSubtypes(nullptr, subtypes.data(),
3150 subtypes.size()));
3151 EXPECT_FALSE(
3152 FPDFAnnot_GetFocusableSubtypes(form_handle(), nullptr, subtypes.size()));
3153 EXPECT_FALSE(
3154 FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(), 0));
3155
3156 UnloadPage(page);
3157}
3158
3160 ASSERT_TRUE(OpenDocument("annots.pdf"));
3161 FPDF_PAGE page = LoadPage(0);
3162 ASSERT_TRUE(page);
3163
3164 {
3165 const char* md5_sum = []() {
3166 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
3167#if BUILDFLAG(IS_WIN)
3168 return "f8c17a0b11d5e152d9a90d6469c6be96";
3169#elif BUILDFLAG(IS_APPLE)
3170 return "cd02e06aeb6555ca7d03136cb8f2e336";
3171#else
3172 return "a08901d205e54530e76f5fc81846eb6a";
3173#endif
3174 }
3175#if BUILDFLAG(IS_APPLE)
3176 return "108a46c517c4eaace9982ee83e8e3296";
3177#else
3178 return "5550d8dcb4d1af1f50e8b4bcaef2ee60";
3179#endif
3180 }();
3181 // Check the initial rendering.
3182 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3183 CompareBitmap(bitmap.get(), 612, 792, md5_sum);
3184 }
3185
3186 // Make links and highlights focusable.
3187 static constexpr FPDF_ANNOTATION_SUBTYPE kSubTypes[] = {FPDF_ANNOT_LINK,
3189 constexpr int kSubTypesCount = std::size(kSubTypes);
3190 ASSERT_TRUE(
3191 FPDFAnnot_SetFocusableSubtypes(form_handle(), kSubTypes, kSubTypesCount));
3192 ASSERT_EQ(kSubTypesCount, FPDFAnnot_GetFocusableSubtypesCount(form_handle()));
3193 std::vector<FPDF_ANNOTATION_SUBTYPE> subtypes(kSubTypesCount);
3194 ASSERT_TRUE(FPDFAnnot_GetFocusableSubtypes(form_handle(), subtypes.data(),
3195 subtypes.size()));
3196 ASSERT_EQ(FPDF_ANNOT_LINK, subtypes[0]);
3197 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, subtypes[1]);
3198
3199 {
3200 const char* md5_sum = []() {
3201 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
3202#if BUILDFLAG(IS_WIN)
3203 return "29886792e8942117a291dee7acdbf39f";
3204#elif BUILDFLAG(IS_APPLE)
3205 return "7e85e4675adccb100fc2cf1037f65f4a";
3206#else
3207 return "de2186f2f36169d0002257a810435648";
3208#endif
3209 }
3210#if BUILDFLAG(IS_APPLE)
3211 return "eb3869335e7a219e1b5f25c1c6037b97";
3212#else
3213 return "805fe7bb751ac4ed2b82bb66efe6db40";
3214#endif
3215 }();
3216 // Focus the first link and check the rendering.
3217 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3218 ASSERT_TRUE(annot);
3219 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3220 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3221 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3222 CompareBitmap(bitmap.get(), 612, 792, md5_sum);
3223 }
3224
3225 {
3226 const char* md5_sum = []() {
3227 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
3228#if BUILDFLAG(IS_WIN)
3229 return "651517d1a58558c6eae18ebb3ff90784";
3230#elif BUILDFLAG(IS_APPLE)
3231 return "96f271ee3f1520d174f887f33989bbcb";
3232#else
3233 return "27bb036f3a507fce66a74a00daf558ec";
3234#endif
3235 }
3236#if BUILDFLAG(IS_APPLE)
3237 return "d20b1978da2362d3942ea0fc6d230997";
3238#else
3239 return "c5c5dcb462af3ef5f43b298ec048feef";
3240#endif
3241 }();
3242 // Focus the first highlight and check the rendering.
3243 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3244 ASSERT_TRUE(annot);
3245 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3246 EXPECT_TRUE(FORM_SetFocusedAnnot(form_handle(), annot.get()));
3247 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3248 CompareBitmap(bitmap.get(), 612, 792, md5_sum);
3249 }
3250
3251 UnloadPage(page);
3252}
3253
3255 ASSERT_TRUE(OpenDocument("annots.pdf"));
3256 FPDF_PAGE page = LoadPage(0);
3257 ASSERT_TRUE(page);
3258 {
3259 constexpr char kExpectedResult[] =
3260 "https://cs.chromium.org/chromium/src/third_party/pdfium/public/"
3261 "fpdf_text.h";
3262
3263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3264 ASSERT_TRUE(annot);
3265 EXPECT_EQ(FPDF_ANNOT_LINK, FPDFAnnot_GetSubtype(annot.get()));
3266 VerifyUriActionInLink(document(), FPDFAnnot_GetLink(annot.get()),
3267 kExpectedResult);
3268 }
3269
3270 {
3271 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 4));
3272 ASSERT_TRUE(annot);
3273 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
3274 EXPECT_FALSE(FPDFAnnot_GetLink(annot.get()));
3275 }
3276
3277 EXPECT_FALSE(FPDFAnnot_GetLink(nullptr));
3278
3279 UnloadPage(page);
3280}
3281
3283 // Open a file with radio button widget annotations and load its first page.
3284 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3285 FPDF_PAGE page = LoadPage(0);
3286 ASSERT_TRUE(page);
3287
3288 {
3289 // Checks for bad annot.
3290 EXPECT_EQ(-1,
3291 FPDFAnnot_GetFormControlCount(form_handle(), /*annot=*/nullptr));
3292
3293 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3294 ASSERT_TRUE(annot);
3295
3296 // Checks for bad form handle.
3297 EXPECT_EQ(-1,
3298 FPDFAnnot_GetFormControlCount(/*hHandle=*/nullptr, annot.get()));
3299
3300 EXPECT_EQ(3, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3301 }
3302
3303 UnloadPage(page);
3304}
3305
3307 // Open a file with checkbox widget annotations and load its first page.
3308 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3309 FPDF_PAGE page = LoadPage(0);
3310 ASSERT_TRUE(page);
3311
3312 {
3313 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3314 ASSERT_TRUE(annot);
3315 EXPECT_EQ(1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3316 }
3317
3318 UnloadPage(page);
3319}
3320
3322 // Open a file with ink annotations and load its first page.
3323 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3324 FPDF_PAGE page = LoadPage(0);
3325 ASSERT_TRUE(page);
3326
3327 {
3328 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3329 ASSERT_TRUE(annot);
3330 EXPECT_EQ(-1, FPDFAnnot_GetFormControlCount(form_handle(), annot.get()));
3331 }
3332
3333 UnloadPage(page);
3334}
3335
3337 // Open a file with radio button widget annotations and load its first page.
3338 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3339 FPDF_PAGE page = LoadPage(0);
3340 ASSERT_TRUE(page);
3341
3342 {
3343 // Checks for bad annot.
3344 EXPECT_EQ(-1,
3345 FPDFAnnot_GetFormControlIndex(form_handle(), /*annot=*/nullptr));
3346
3347 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 3));
3348 ASSERT_TRUE(annot);
3349
3350 // Checks for bad form handle.
3351 EXPECT_EQ(-1,
3352 FPDFAnnot_GetFormControlIndex(/*hHandle=*/nullptr, annot.get()));
3353
3354 EXPECT_EQ(1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3355 }
3356
3357 UnloadPage(page);
3358}
3359
3361 // Open a file with checkbox widget annotations and load its first page.
3362 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3363 FPDF_PAGE page = LoadPage(0);
3364 ASSERT_TRUE(page);
3365
3366 {
3367 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3368 ASSERT_TRUE(annot);
3369 EXPECT_EQ(0, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3370 }
3371
3372 UnloadPage(page);
3373}
3374
3376 // Open a file with ink annotations and load its first page.
3377 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3378 FPDF_PAGE page = LoadPage(0);
3379 ASSERT_TRUE(page);
3380
3381 {
3382 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3383 ASSERT_TRUE(annot);
3384 EXPECT_EQ(-1, FPDFAnnot_GetFormControlIndex(form_handle(), annot.get()));
3385 }
3386
3387 UnloadPage(page);
3388}
3389
3391 // Open a file with radio button widget annotations and load its first page.
3392 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3393 FPDF_PAGE page = LoadPage(0);
3394 ASSERT_TRUE(page);
3395
3396 {
3397 // Checks for bad annot.
3398 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3399 form_handle(), /*annot=*/nullptr,
3400 /*buffer=*/nullptr, /*buflen=*/0));
3401
3402 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 6));
3403 ASSERT_TRUE(annot);
3404
3405 // Checks for bad form handle.
3406 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(
3407 /*hHandle=*/nullptr, annot.get(),
3408 /*buffer=*/nullptr, /*buflen=*/0));
3409
3410 unsigned long length_bytes =
3411 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3412 /*buffer=*/nullptr, /*buflen=*/0);
3413 ASSERT_EQ(14u, length_bytes);
3414 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3415 EXPECT_EQ(14u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3416 buf.data(), length_bytes));
3417 EXPECT_EQ(L"value2", GetPlatformWString(buf.data()));
3418 }
3419
3420 UnloadPage(page);
3421}
3422
3424 // Open a file with checkbox widget annotations and load its first page.
3425 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3426 FPDF_PAGE page = LoadPage(0);
3427 ASSERT_TRUE(page);
3428
3429 {
3430 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3431 ASSERT_TRUE(annot);
3432
3433 unsigned long length_bytes =
3434 FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3435 /*buffer=*/nullptr, /*buflen=*/0);
3436 ASSERT_EQ(8u, length_bytes);
3437 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3438 EXPECT_EQ(8u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3439 buf.data(), length_bytes));
3440 EXPECT_EQ(L"Yes", GetPlatformWString(buf.data()));
3441 }
3442
3443 UnloadPage(page);
3444}
3445
3447 // Open a file with ink annotations and load its first page.
3448 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
3449 FPDF_PAGE page = LoadPage(0);
3450 ASSERT_TRUE(page);
3451
3452 {
3453 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3454 ASSERT_TRUE(annot);
3455 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldExportValue(form_handle(), annot.get(),
3456 /*buffer=*/nullptr,
3457 /*buflen=*/0));
3458 }
3459
3460 UnloadPage(page);
3461}
3462
3464 ASSERT_TRUE(OpenDocument("redact_annot.pdf"));
3465 FPDF_PAGE page = LoadPage(0);
3466 ASSERT_TRUE(page);
3467 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3468
3469 {
3470 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3471 ASSERT_TRUE(annot);
3472 EXPECT_EQ(FPDF_ANNOT_REDACT, FPDFAnnot_GetSubtype(annot.get()));
3473 }
3474
3475 UnloadPage(page);
3476}
3477
3479 ASSERT_TRUE(OpenDocument("polygon_annot.pdf"));
3480 FPDF_PAGE page = LoadPage(0);
3481 ASSERT_TRUE(page);
3482 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3483
3484 {
3485 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3486 ASSERT_TRUE(annot);
3487
3488 // FPDFAnnot_GetVertices() positive testing.
3489 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3490 const size_t kExpectedSize = 3;
3491 ASSERT_EQ(kExpectedSize, size);
3492 std::vector<FS_POINTF> vertices_buffer(size);
3493 EXPECT_EQ(size,
3494 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3495 EXPECT_FLOAT_EQ(159.0f, vertices_buffer[0].x);
3496 EXPECT_FLOAT_EQ(296.0f, vertices_buffer[0].y);
3497 EXPECT_FLOAT_EQ(350.0f, vertices_buffer[1].x);
3498 EXPECT_FLOAT_EQ(411.0f, vertices_buffer[1].y);
3499 EXPECT_FLOAT_EQ(472.0f, vertices_buffer[2].x);
3500 EXPECT_FLOAT_EQ(243.42f, vertices_buffer[2].y);
3501
3502 // FPDFAnnot_GetVertices() negative testing.
3503 EXPECT_EQ(0U, FPDFAnnot_GetVertices(nullptr, nullptr, 0));
3504
3505 // vertices_buffer is not overwritten if it is too small.
3506 vertices_buffer.resize(1);
3507 vertices_buffer[0].x = 42;
3508 vertices_buffer[0].y = 43;
3509 size = FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(),
3510 vertices_buffer.size());
3511 EXPECT_EQ(kExpectedSize, size);
3512 EXPECT_FLOAT_EQ(42, vertices_buffer[0].x);
3513 EXPECT_FLOAT_EQ(43, vertices_buffer[0].y);
3514 }
3515
3516 {
3517 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3518 ASSERT_TRUE(annot);
3519
3520 // This has an odd number of elements in the vertices array, ignore the last
3521 // element.
3522 unsigned long size = FPDFAnnot_GetVertices(annot.get(), nullptr, 0);
3523 const size_t kExpectedSize = 3;
3524 ASSERT_EQ(kExpectedSize, size);
3525 std::vector<FS_POINTF> vertices_buffer(size);
3526 EXPECT_EQ(size,
3527 FPDFAnnot_GetVertices(annot.get(), vertices_buffer.data(), size));
3528 EXPECT_FLOAT_EQ(259.0f, vertices_buffer[0].x);
3529 EXPECT_FLOAT_EQ(396.0f, vertices_buffer[0].y);
3530 EXPECT_FLOAT_EQ(450.0f, vertices_buffer[1].x);
3531 EXPECT_FLOAT_EQ(511.0f, vertices_buffer[1].y);
3532 EXPECT_FLOAT_EQ(572.0f, vertices_buffer[2].x);
3533 EXPECT_FLOAT_EQ(343.0f, vertices_buffer[2].y);
3534 }
3535
3536 {
3537 // Wrong annotation type.
3538 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3539 EXPECT_EQ(0U, FPDFAnnot_GetVertices(ink_annot.get(), nullptr, 0));
3540 }
3541
3542 UnloadPage(page);
3543}
3544
3546 ASSERT_TRUE(OpenDocument("ink_annot.pdf"));
3547 FPDF_PAGE page = LoadPage(0);
3548 ASSERT_TRUE(page);
3549 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3550
3551 {
3552 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3553 ASSERT_TRUE(annot);
3554
3555 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() positive
3556 // testing.
3557 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3558 const size_t kExpectedSize = 1;
3559 ASSERT_EQ(kExpectedSize, size);
3560 const unsigned long kPathIndex = 0;
3561 unsigned long path_size =
3562 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3563 const size_t kExpectedPathSize = 3;
3564 ASSERT_EQ(kExpectedPathSize, path_size);
3565 std::vector<FS_POINTF> path_buffer(path_size);
3566 EXPECT_EQ(path_size,
3567 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3568 path_buffer.data(), path_size));
3569 EXPECT_FLOAT_EQ(159.0f, path_buffer[0].x);
3570 EXPECT_FLOAT_EQ(296.0f, path_buffer[0].y);
3571 EXPECT_FLOAT_EQ(350.0f, path_buffer[1].x);
3572 EXPECT_FLOAT_EQ(411.0f, path_buffer[1].y);
3573 EXPECT_FLOAT_EQ(472.0f, path_buffer[2].x);
3574 EXPECT_FLOAT_EQ(243.42f, path_buffer[2].y);
3575
3576 // FPDFAnnot_GetInkListCount() and FPDFAnnot_GetInkListPath() negative
3577 // testing.
3578 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(nullptr));
3579 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 0, nullptr, 0));
3580
3581 // out of bounds path_index.
3582 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(nullptr, 42, nullptr, 0));
3583
3584 // path_buffer is not overwritten if it is too small.
3585 path_buffer.resize(1);
3586 path_buffer[0].x = 42;
3587 path_buffer[0].y = 43;
3588 path_size = FPDFAnnot_GetInkListPath(
3589 annot.get(), kPathIndex, path_buffer.data(), path_buffer.size());
3590 EXPECT_EQ(kExpectedPathSize, path_size);
3591 EXPECT_FLOAT_EQ(42, path_buffer[0].x);
3592 EXPECT_FLOAT_EQ(43, path_buffer[0].y);
3593 }
3594
3595 {
3596 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3597 ASSERT_TRUE(annot);
3598
3599 // This has an odd number of elements in the path array, ignore the last
3600 // element.
3601 unsigned long size = FPDFAnnot_GetInkListCount(annot.get());
3602 const size_t kExpectedSize = 1;
3603 ASSERT_EQ(kExpectedSize, size);
3604 const unsigned long kPathIndex = 0;
3605 unsigned long path_size =
3606 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex, nullptr, 0);
3607 const size_t kExpectedPathSize = 3;
3608 ASSERT_EQ(kExpectedPathSize, path_size);
3609 std::vector<FS_POINTF> path_buffer(path_size);
3610 EXPECT_EQ(path_size,
3611 FPDFAnnot_GetInkListPath(annot.get(), kPathIndex,
3612 path_buffer.data(), path_size));
3613 EXPECT_FLOAT_EQ(259.0f, path_buffer[0].x);
3614 EXPECT_FLOAT_EQ(396.0f, path_buffer[0].y);
3615 EXPECT_FLOAT_EQ(450.0f, path_buffer[1].x);
3616 EXPECT_FLOAT_EQ(511.0f, path_buffer[1].y);
3617 EXPECT_FLOAT_EQ(572.0f, path_buffer[2].x);
3618 EXPECT_FLOAT_EQ(343.0f, path_buffer[2].y);
3619 }
3620
3621 {
3622 // Wrong annotation type.
3623 ScopedFPDFAnnotation polygon_annot(
3625 EXPECT_EQ(0U, FPDFAnnot_GetInkListCount(polygon_annot.get()));
3626 const unsigned long kPathIndex = 0;
3627 EXPECT_EQ(0U, FPDFAnnot_GetInkListPath(polygon_annot.get(), kPathIndex,
3628 nullptr, 0));
3629 }
3630
3631 UnloadPage(page);
3632}
3633
3635 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3636 FPDF_PAGE page = LoadPage(0);
3637 ASSERT_TRUE(page);
3638 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3639
3640 {
3641 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3642 ASSERT_TRUE(annot);
3643
3644 // FPDFAnnot_GetVertices() positive testing.
3645 FS_POINTF start;
3646 FS_POINTF end;
3647 ASSERT_TRUE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3648 EXPECT_FLOAT_EQ(159.0f, start.x);
3649 EXPECT_FLOAT_EQ(296.0f, start.y);
3650 EXPECT_FLOAT_EQ(472.0f, end.x);
3651 EXPECT_FLOAT_EQ(243.42f, end.y);
3652
3653 // FPDFAnnot_GetVertices() negative testing.
3654 EXPECT_FALSE(FPDFAnnot_GetLine(nullptr, nullptr, nullptr));
3655 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), nullptr, nullptr));
3656 }
3657
3658 {
3659 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3660 ASSERT_TRUE(annot);
3661
3662 // Too few elements in the line array.
3663 FS_POINTF start;
3664 FS_POINTF end;
3665 EXPECT_FALSE(FPDFAnnot_GetLine(annot.get(), &start, &end));
3666 }
3667
3668 {
3669 // Wrong annotation type.
3670 ScopedFPDFAnnotation ink_annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_INK));
3671 FS_POINTF start;
3672 FS_POINTF end;
3673 EXPECT_FALSE(FPDFAnnot_GetLine(ink_annot.get(), &start, &end));
3674 }
3675
3676 UnloadPage(page);
3677}
3678
3680 ASSERT_TRUE(OpenDocument("line_annot.pdf"));
3681 FPDF_PAGE page = LoadPage(0);
3682 ASSERT_TRUE(page);
3683 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3684
3685 {
3686 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3687 ASSERT_TRUE(annot);
3688
3689 // FPDFAnnot_GetBorder() positive testing.
3690 float horizontal_radius;
3691 float vertical_radius;
3692 float border_width;
3693 ASSERT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3694 &vertical_radius, &border_width));
3695 EXPECT_FLOAT_EQ(0.25f, horizontal_radius);
3696 EXPECT_FLOAT_EQ(0.5f, vertical_radius);
3697 EXPECT_FLOAT_EQ(2.0f, border_width);
3698
3699 // FPDFAnnot_GetBorder() negative testing.
3700 EXPECT_FALSE(FPDFAnnot_GetBorder(nullptr, nullptr, nullptr, nullptr));
3701 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), nullptr, nullptr, nullptr));
3702 }
3703
3704 {
3705 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3706 ASSERT_TRUE(annot);
3707
3708 // Too few elements in the border array.
3709 float horizontal_radius;
3710 float vertical_radius;
3711 float border_width;
3712 EXPECT_FALSE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3713 &vertical_radius, &border_width));
3714
3715 // FPDFAnnot_SetBorder() positive testing.
3716 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3717 /*vertical_radius=*/3.5f,
3718 /*border_width=*/4.0f));
3719
3720 EXPECT_TRUE(FPDFAnnot_GetBorder(annot.get(), &horizontal_radius,
3721 &vertical_radius, &border_width));
3722 EXPECT_FLOAT_EQ(2.0f, horizontal_radius);
3723 EXPECT_FLOAT_EQ(3.5f, vertical_radius);
3724 EXPECT_FLOAT_EQ(4.0f, border_width);
3725
3726 // FPDFAnnot_SetBorder() negative testing.
3727 EXPECT_FALSE(FPDFAnnot_SetBorder(nullptr, /*horizontal_radius=*/1.0f,
3728 /*vertical_radius=*/2.5f,
3729 /*border_width=*/3.0f));
3730 }
3731
3732 UnloadPage(page);
3733}
3734
3736 ASSERT_TRUE(OpenDocument("annot_javascript.pdf"));
3737 FPDF_PAGE page = LoadPage(0);
3738 ASSERT_TRUE(page);
3739 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3740
3741 {
3742 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3743 ASSERT_TRUE(annot);
3744
3745 // FPDFAnnot_GetFormAdditionalActionJavaScript() positive testing.
3746 unsigned long length_bytes = FPDFAnnot_GetFormAdditionalActionJavaScript(
3747 form_handle(), annot.get(), FPDF_ANNOT_AACTION_FORMAT, nullptr, 0);
3748 ASSERT_EQ(62u, length_bytes);
3749 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3750 EXPECT_EQ(62u, FPDFAnnot_GetFormAdditionalActionJavaScript(
3751 form_handle(), annot.get(), FPDF_ANNOT_AACTION_FORMAT,
3752 buf.data(), length_bytes));
3753 EXPECT_EQ(L"AFDate_FormatEx(\"yyyy-mm-dd\");",
3754 GetPlatformWString(buf.data()));
3755
3756 // FPDFAnnot_GetFormAdditionalActionJavaScript() negative testing.
3757 EXPECT_EQ(0u, FPDFAnnot_GetFormAdditionalActionJavaScript(
3758 form_handle(), nullptr, 0, nullptr, 0));
3759 EXPECT_EQ(0u, FPDFAnnot_GetFormAdditionalActionJavaScript(
3760 nullptr, annot.get(), 0, nullptr, 0));
3761 EXPECT_EQ(0u, FPDFAnnot_GetFormAdditionalActionJavaScript(
3762 form_handle(), annot.get(), 0, nullptr, 0));
3763 EXPECT_EQ(2u, FPDFAnnot_GetFormAdditionalActionJavaScript(
3764 form_handle(), annot.get(), FPDF_ANNOT_AACTION_KEY_STROKE,
3765 nullptr, 0));
3766 }
3767
3768 UnloadPage(page);
3769}
3770
3772 ASSERT_TRUE(OpenDocument("click_form.pdf"));
3773 FPDF_PAGE page = LoadPage(0);
3774 ASSERT_TRUE(page);
3775 EXPECT_EQ(8, FPDFPage_GetAnnotCount(page));
3776
3777 {
3778 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3779 ASSERT_TRUE(annot);
3780
3781 // FPDFAnnot_GetFormFieldAlternateName() positive testing.
3782 unsigned long length_bytes = FPDFAnnot_GetFormFieldAlternateName(
3783 form_handle(), annot.get(), nullptr, 0);
3784 ASSERT_EQ(34u, length_bytes);
3785 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3786 EXPECT_EQ(34u, FPDFAnnot_GetFormFieldAlternateName(
3787 form_handle(), annot.get(), buf.data(), length_bytes));
3788 EXPECT_EQ(L"readOnlyCheckbox", GetPlatformWString(buf.data()));
3789
3790 // FPDFAnnot_GetFormFieldAlternateName() negative testing.
3791 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldAlternateName(form_handle(), nullptr,
3792 nullptr, 0));
3793 EXPECT_EQ(0u, FPDFAnnot_GetFormFieldAlternateName(nullptr, annot.get(),
3794 nullptr, 0));
3795 }
3796
3797 UnloadPage(page);
3798}
3799
3800// Due to https://crbug.com/pdfium/570, the AnnotationBorder test above cannot
3801// actually render the line annotations inside line_annot.pdf. For now, use a
3802// square annotation in annots.pdf for testing.
3804 ASSERT_TRUE(OpenDocument("annots.pdf"));
3805 FPDF_PAGE page = LoadPage(1);
3806 ASSERT_TRUE(page);
3807 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
3808
3809 const char* original_checksum = []() {
3810 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
3811#if BUILDFLAG(IS_WIN)
3812 return "36ab186e78c0b88eeb8f7aceea93b72c";
3813#elif BUILDFLAG(IS_APPLE)
3814 return "953b14259560aeca886ea44c9529892b";
3815#else
3816 return "238dccc7df0ac61ac580c28e1109da3c";
3817#endif
3818 }
3819#if BUILDFLAG(IS_APPLE)
3820 return "522a4a6b6c7eab5bf95ded1f21ea372e";
3821#else
3822 return "12127303aecd80c6288460f7c0d79f3f";
3823#endif
3824 }();
3825 const char* modified_checksum = []() {
3826 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
3827#if BUILDFLAG(IS_WIN)
3828 return "ece1ab24a0d9425ef3b06747c95d75ce";
3829#elif BUILDFLAG(IS_APPLE)
3830 return "bfc344e98798298bf7bb0953db75c686";
3831#else
3832 return "0f326acb3eb583125ca584d703ccb13b";
3833#endif
3834 }
3835#if BUILDFLAG(IS_APPLE)
3836 return "6844019e07b83cc01723415f58218d06";
3837#else
3838 return "73d06ff4c665fe85029acef30240dcca";
3839#endif
3840 }();
3841
3842 {
3843 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
3844 ASSERT_TRUE(annot);
3845 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
3846
3847 {
3848 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3849 CompareBitmap(bitmap.get(), 612, 792, original_checksum);
3850 }
3851
3852 EXPECT_TRUE(FPDFAnnot_SetBorder(annot.get(), /*horizontal_radius=*/2.0f,
3853 /*vertical_radius=*/3.5f,
3854 /*border_width=*/4.0f));
3855
3856 {
3857 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
3858 CompareBitmap(bitmap.get(), 612, 792, modified_checksum);
3859 }
3860 }
3861
3862 // Save the document and close the page.
3863 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
3864 UnloadPage(page);
3865
3866 ASSERT_TRUE(OpenSavedDocument());
3867 page = LoadSavedPage(1);
3868 ASSERT_TRUE(page);
3869 VerifySavedRendering(page, 612, 792, modified_checksum);
3870
3871 CloseSavedPage(page);
3872 CloseSavedDocument();
3873}
3874
3876 ASSERT_TRUE(OpenDocument("annotation_fileattachment.pdf"));
3877 FPDF_PAGE page = LoadPage(0);
3878 ASSERT_TRUE(page);
3879 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3880
3881 {
3882 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3883 ASSERT_TRUE(annot);
3884 EXPECT_EQ(FPDF_ANNOT_FILEATTACHMENT, FPDFAnnot_GetSubtype(annot.get()));
3885
3886 FPDF_ATTACHMENT attachment = FPDFAnnot_GetFileAttachment(annot.get());
3887 ASSERT_TRUE(attachment);
3888
3889 // Check that the name of the attachment is correct.
3890 unsigned long length_bytes = FPDFAttachment_GetName(attachment, nullptr, 0);
3891 ASSERT_EQ(18u, length_bytes);
3892 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3893 EXPECT_EQ(18u,
3894 FPDFAttachment_GetName(attachment, buf.data(), length_bytes));
3895 EXPECT_EQ(L"test.txt", GetPlatformWString(buf.data()));
3896
3897 // Check that the content of the attachment is correct.
3898 ASSERT_TRUE(FPDFAttachment_GetFile(attachment, nullptr, 0, &length_bytes));
3899 std::vector<uint8_t> content_buf(length_bytes);
3900 unsigned long actual_length_bytes;
3901 ASSERT_TRUE(FPDFAttachment_GetFile(attachment, content_buf.data(),
3902 length_bytes, &actual_length_bytes));
3903 ASSERT_THAT(content_buf, testing::ElementsAre('t', 'e', 's', 't', ' ', 't',
3904 'e', 'x', 't'));
3905 }
3906
3907 {
3908 // Add a file attachment annotation to the page.
3909 ScopedFPDFAnnotation annot(
3911 ASSERT_TRUE(annot);
3912
3913 // Check that there is now 2 annotations on this page.
3914 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
3915
3916 ScopedFPDFWideString file_name = GetFPDFWideString(L"0.txt");
3917 FPDF_ATTACHMENT attachment =
3918 FPDFAnnot_AddFileAttachment(annot.get(), file_name.get());
3919 ASSERT_TRUE(attachment);
3920
3921 // The filling of the FPDF_ATTACHMENT has been tested in
3922 // fpdf_attachment_embeddertest.cpp
3923 }
3924
3925 {
3926 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
3927 ASSERT_TRUE(annot);
3928 EXPECT_EQ(FPDF_ANNOT_FILEATTACHMENT, FPDFAnnot_GetSubtype(annot.get()));
3929
3930 // Check that we can read newly created file spec
3931 FPDF_ATTACHMENT attachment = FPDFAnnot_GetFileAttachment(annot.get());
3932 ASSERT_TRUE(attachment);
3933
3934 // Verify the name of the new attachment.
3935 unsigned long length_bytes = FPDFAttachment_GetName(attachment, nullptr, 0);
3936 ASSERT_EQ(12u, length_bytes);
3937 std::vector<FPDF_WCHAR> buf = GetFPDFWideStringBuffer(length_bytes);
3938 EXPECT_EQ(12u,
3939 FPDFAttachment_GetName(attachment, buf.data(), length_bytes));
3940 EXPECT_EQ(L"0.txt", GetPlatformWString(buf.data()));
3941 }
3942
3943 UnloadPage(page);
3944}
3945
3947 ASSERT_TRUE(OpenDocument("annotation_fileattachment.pdf"));
3948 FPDF_PAGE page = LoadPage(0);
3949 ASSERT_TRUE(page);
3950 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
3951
3952 {
3953 ASSERT_FALSE(FPDFAnnot_GetFileAttachment(nullptr));
3954
3955 ScopedFPDFAnnotation text_annot(
3957 ASSERT_TRUE(text_annot);
3958 ASSERT_FALSE(FPDFAnnot_GetFileAttachment(text_annot.get()));
3959
3960 ScopedFPDFAnnotation newly_file_annot(
3962 ASSERT_TRUE(newly_file_annot);
3963 ASSERT_FALSE(FPDFAnnot_GetFileAttachment(newly_file_annot.get()));
3964 }
3965
3966 {
3967 ScopedFPDFWideString empty_name = GetFPDFWideString(L"");
3968 ScopedFPDFWideString not_empty_name = GetFPDFWideString(L"0.txt");
3969
3970 ASSERT_FALSE(FPDFAnnot_AddFileAttachment(nullptr, nullptr));
3971 ASSERT_FALSE(FPDFAnnot_AddFileAttachment(nullptr, empty_name.get()));
3972 ASSERT_FALSE(FPDFAnnot_AddFileAttachment(nullptr, not_empty_name.get()));
3973
3974 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
3975 ASSERT_TRUE(annot);
3976
3977 ASSERT_FALSE(FPDFAnnot_AddFileAttachment(annot.get(), nullptr));
3978 ASSERT_FALSE(FPDFAnnot_AddFileAttachment(annot.get(), empty_name.get()));
3979
3980 FPDF_ATTACHMENT old_attachment = FPDFAnnot_GetFileAttachment(annot.get());
3981 EXPECT_NE(old_attachment,
3982 FPDFAnnot_AddFileAttachment(annot.get(), not_empty_name.get()));
3983 }
3984
3985 UnloadPage(page);
3986}
fxcrt::ByteString ByteString
Definition bytestring.h:180
const CPDF_Dictionary * GetAnnotDict() const
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
bool KeyExist(const ByteString &key) const
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
#define UNSAFE_TODO(...)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetFontSize(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot, float *value)
#define FPDF_ANNOT_FILEATTACHMENT
Definition fpdf_annot.h:37
#define FPDF_ANNOT_STAMP
Definition fpdf_annot.h:33
#define FPDF_ANNOT_FLAG_INVISIBLE
Definition fpdf_annot.h:52
#define FPDF_ANNOT_FLAG_NOROTATE
Definition fpdf_annot.h:56
#define FPDF_ANNOT_FLAG_NOZOOM
Definition fpdf_annot.h:55
#define FPDF_ANNOT_FLAG_HIDDEN
Definition fpdf_annot.h:53
#define FPDF_ANNOT_FLAG_PRINT
Definition fpdf_annot.h:54
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_RemoveAnnot(FPDF_PAGE page, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetBorder(FPDF_ANNOTATION annot, float *horizontal_radius, float *vertical_radius, float *border_width)
#define FPDF_ANNOT_APPEARANCEMODE_COUNT
Definition fpdf_annot.h:65
#define FPDF_ANNOT_APPEARANCEMODE_ROLLOVER
Definition fpdf_annot.h:63
FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetFocusableSubtypesCount(FPDF_FORMHANDLE hHandle)
FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetOptionCount(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetVertices(FPDF_ANNOTATION annot, FS_POINTF *buffer, unsigned long length)
#define FPDF_ANNOT_UNDERLINE
Definition fpdf_annot.h:30
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFAnnot_GetObject(FPDF_ANNOTATION annot, int index)
#define FPDF_ANNOT_UNKNOWN
Definition fpdf_annot.h:20
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot, FPDF_BYTESTRING key)
#define FPDF_FORMFLAG_TEXT_MULTILINE
Definition fpdf_annot.h:76
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFAnnot_AddFileAttachment(FPDF_ANNOTATION annot, FPDF_WIDESTRING name)
FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetObjectCount(FPDF_ANNOTATION annot)
#define FPDF_ANNOT_FLAG_READONLY
Definition fpdf_annot.h:58
FPDF_EXPORT int FPDF_CALLCONV FPDFPage_GetAnnotCount(FPDF_PAGE page)
FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV FPDFPage_CreateAnnot(FPDF_PAGE page, FPDF_ANNOTATION_SUBTYPE subtype)
#define FPDF_FORMFLAG_NOEXPORT
Definition fpdf_annot.h:72
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_IsChecked(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot)
FPDF_EXPORT FPDF_LINK FPDF_CALLCONV FPDFAnnot_GetLink(FPDF_ANNOTATION annot)
#define FPDF_FORMFLAG_CHOICE_COMBO
Definition fpdf_annot.h:81
#define FPDF_FORMFLAG_CHOICE_EDIT
Definition fpdf_annot.h:82
FPDF_EXPORT FPDF_ANNOTATION_SUBTYPE FPDF_CALLCONV FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot)
#define FPDF_ANNOT_WIDGET
Definition fpdf_annot.h:40
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_AppendAttachmentPoints(FPDF_ANNOTATION annot, const FS_QUADPOINTSF *quad_points)
#define FPDF_ANNOT_HIGHLIGHT
Definition fpdf_annot.h:29
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetFontColor(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot, unsigned int *R, unsigned int *G, unsigned int *B)
#define FPDF_FORMFLAG_READONLY
Definition fpdf_annot.h:70
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetOptionLabel(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot, int index, FPDF_WCHAR *buffer, unsigned long buflen)
#define FPDF_ANNOT_INK
Definition fpdf_annot.h:35
#define FPDF_ANNOT_APPEARANCEMODE_NORMAL
Definition fpdf_annot.h:62
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetInkListPath(FPDF_ANNOTATION annot, unsigned long path_index, FS_POINTF *buffer, unsigned long length)
#define FPDF_FORMFLAG_REQUIRED
Definition fpdf_annot.h:71
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_IsOptionSelected(FPDF_FORMHANDLE handle, FPDF_ANNOTATION annot, int index)
FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV FPDFPage_GetAnnot(FPDF_PAGE page, int index)
#define FPDF_ANNOT_FLAG_LOCKED
Definition fpdf_annot.h:59
#define FPDF_ANNOT_SQUARE
Definition fpdf_annot.h:25
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, FPDF_BYTESTRING key, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT void FPDF_CALLCONV FPDFPage_CloseAnnot(FPDF_ANNOTATION annot)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetNumberValue(FPDF_ANNOTATION annot, FPDF_BYTESTRING key, float *value)
#define FPDF_ANNOT_AACTION_FORMAT
Definition fpdf_annot.h:91
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetURI(FPDF_ANNOTATION annot, const char *uri)
#define FPDF_ANNOT_APPEARANCEMODE_DOWN
Definition fpdf_annot.h:64
#define FPDF_ANNOT_FLAG_NONE
Definition fpdf_annot.h:51
#define FPDF_ANNOT_FLAG_NOVIEW
Definition fpdf_annot.h:57
#define FPDF_ANNOT_REDACT
Definition fpdf_annot.h:48
#define FPDF_ANNOT_POLYGON
Definition fpdf_annot.h:27
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetLine(FPDF_ANNOTATION annot, FS_POINTF *start, FS_POINTF *end)
FPDF_EXPORT int FPDF_CALLCONV FPDFPage_GetAnnotIndex(FPDF_PAGE page, FPDF_ANNOTATION annot)
#define FPDF_ANNOT_POPUP
Definition fpdf_annot.h:36
#define FPDF_FORMFLAG_TEXT_PASSWORD
Definition fpdf_annot.h:77
FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV FPDFAnnot_GetFormFieldAtPoint(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, const FS_POINTF *point)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetInkListCount(FPDF_ANNOTATION annot)
#define FPDF_FORMFLAG_CHOICE_MULTI_SELECT
Definition fpdf_annot.h:83
@ FPDFANNOT_COLORTYPE_Color
Definition fpdf_annot.h:96
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetBorder(FPDF_ANNOTATION annot, float horizontal_radius, float vertical_radius, float border_width)
#define FPDF_ANNOT_FLAG_TOGGLENOVIEW
Definition fpdf_annot.h:60
#define FPDF_ANNOT_AACTION_KEY_STROKE
Definition fpdf_annot.h:90
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFAnnot_GetFileAttachment(FPDF_ANNOTATION annot)
#define FPDF_ANNOT_TEXT
Definition fpdf_annot.h:21
#define FPDF_ANNOT_LINK
Definition fpdf_annot.h:22
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_RemoveInkList(FPDF_ANNOTATION annot)
TEST_F(FPDFAnnotEmbedderTest, SetAP)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment, void *buffer, unsigned long buflen, unsigned long *out_buflen)
FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV FPDFLink_GetAction(FPDF_LINK link)
Definition fpdf_doc.cpp:361
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAction_GetType(FPDF_ACTION action)
Definition fpdf_doc.cpp:180
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAction_GetURIPath(FPDF_DOCUMENT document, FPDF_ACTION action, void *buffer, unsigned long buflen)
Definition fpdf_doc.cpp:231
FPDF_EXPORT FPDF_LINK FPDF_CALLCONV FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y)
Definition fpdf_doc.cpp:307
#define PDFACTION_URI
Definition fpdf_doc.h:24
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFImageObj_SetBitmap(FPDF_PAGE *pages, int count, FPDF_PAGEOBJECT image_object, FPDF_BITMAP bitmap)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_SetStrokeColor(FPDF_PAGEOBJECT page_object, unsigned int R, unsigned int G, unsigned int B, unsigned int A)
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_CreateNewPath(float x, float y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetDrawMode(FPDF_PAGEOBJECT path, int fillmode, FPDF_BOOL stroke)
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_CreateNewDocument()
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object, unsigned int R, unsigned int G, unsigned int B, unsigned int A)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_LineTo(FPDF_PAGEOBJECT path, float x, float y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_BezierTo(FPDF_PAGEOBJECT path, float x1, float y1, float x2, float y2, float x3, float y3)
FPDF_EXPORT int FPDF_CALLCONV FPDFPageObj_GetType(FPDF_PAGEOBJECT page_object)
#define FPDF_PAGEOBJ_PATH
Definition fpdf_edit.h:40
#define FPDF_PAGEOBJ_TEXT
Definition fpdf_edit.h:39
#define FPDF_PAGEOBJ_IMAGE
Definition fpdf_edit.h:41
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_MoveTo(FPDF_PAGEOBJECT path, float x, float y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_SetMatrix(FPDF_PAGEOBJECT page_object, const FS_MATRIX *matrix)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_SetStrokeWidth(FPDF_PAGEOBJECT page_object, float width)
FPDF_EXPORT int FPDF_CALLCONV FPDFPage_CountObjects(FPDF_PAGE page)
FPDF_EXPORT void FPDF_CALLCONV FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object, double a, double b, double c, double d, double e, double f)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle)
#define FPDF_FORMFIELD_CHECKBOX
#define FPDF_FORMFIELD_COMBOBOX
#define FPDF_FORMFIELD_TEXTFIELD
#define FPDF_FORMFIELD_RADIOBUTTON
#define FPDF_FORMFIELD_LISTBOX
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_LoadCustomDocument(FPDF_FILEACCESS *pFileAccess, FPDF_BYTESTRING password)
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap)
FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap)
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width, int height, int alpha)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, FPDF_DWORD color)
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap)
#define FPDF_OBJECT_REFERENCE
Definition fpdfview.h:45
#define FPDF_OBJECT_NAME
Definition fpdfview.h:40
#define FPDF_OBJECT_STRING
Definition fpdfview.h:39
#define FPDF_ANNOT
Definition fpdfview.h:790
std::unique_ptr< FPDF_WCHAR, pdfium::FreeDeleter > ScopedFPDFWideString
const char kBlankPage612By792Checksum[]
const char * AnnotationStampWithApChecksum()
const char * HelloWorldChecksum()