blob: 767a5c78c04996d038f8dcb3617462c0d4b082b5 [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000011#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Jane Liue17011d2017-06-21 12:18:37 -040031TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
32 // Open a file with one annotation and load its first page.
33 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000034 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040035 ASSERT_TRUE(page);
36
37 // This annotation has a malformed appearance stream, which does not have its
38 // normal appearance defined, only its rollover appearance. In this case, its
39 // normal appearance should be generated, allowing the highlight annotation to
40 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000041 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000042 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040043
44 UnloadPage(page);
45}
46
Ralf Sipplb3a52402018-03-19 23:30:28 +000047TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
48 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000049 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000050 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
51 FPDF_PAGE page = LoadPage(0);
52 ASSERT_TRUE(page);
53
Tom Sepeze08d2b12018-04-25 18:49:32 +000054 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000055 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
56
57 UnloadPage(page);
58}
59
Jane Liu4fd9a472017-06-01 18:56:09 -040060TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
61 // Open a file with one annotation and load its first page.
62 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000063 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
64 // the FORM API calls in LoadPage() makes this test fail. So use
65 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -040066 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
67 ASSERT_TRUE(page);
68
69 // Check that there is a total of 1 annotation on its first page.
70 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
71
72 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +000073 {
Tom Sepeze08d2b12018-04-25 18:49:32 +000074 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +000075 ASSERT_TRUE(annot);
76 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040077
Lei Zhanga21d5932018-02-05 18:28:38 +000078 // Check that the annotation color is yellow.
79 unsigned int R;
80 unsigned int G;
81 unsigned int B;
82 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +000083 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +000084 &G, &B, &A));
85 EXPECT_EQ(255u, R);
86 EXPECT_EQ(255u, G);
87 EXPECT_EQ(0u, B);
88 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -040089
Lei Zhanga21d5932018-02-05 18:28:38 +000090 // Check that the author is correct.
91 static constexpr char kAuthorKey[] = "T";
92 EXPECT_EQ(FPDF_OBJECT_STRING,
93 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
94 unsigned long len =
95 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
96 std::vector<char> buf(len);
97 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
98 len));
99 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400100
Lei Zhanga21d5932018-02-05 18:28:38 +0000101 // Check that the content is correct.
102 EXPECT_EQ(FPDF_OBJECT_STRING,
103 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
104 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
105 buf.clear();
106 buf.resize(len);
107 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
108 buf.data(), len));
109 const wchar_t contents[] =
110 L"This is a note for that highlight annotation. Very long highlight "
111 "annotation. Long long long Long long longLong long longLong long "
112 "longLong long longLong long longLong long longLong long longLong long "
113 "longLong long longLong long longLong long longLong long longLong long "
114 "longLong long longLong long longLong long longLong long longLong long "
115 "longLong long longLong long longLong long longLong long longLong long "
116 "longLong long longLong long longLong long longLong long longLong long "
117 "longLong long longLong long longLong long longLong long longLong long "
118 "longLong long longLong long longLong long longLong long longLong long "
119 "longLong long longLong long longLong long longLong long longLong long "
120 "longLong long longLong long longLong long longLong long longLong long "
121 "longLong long longLong long longLong long longLong long longLong long "
122 "longLong long longLong long longLong long longLong long longLong long "
123 "longLong long longLong long longLong long longLong long longLong long "
124 "longLong long longLong long longLong long longLong long longLong long "
125 "longLong long longLong long longLong long longLong long longLong long "
126 "longLong long longLong long longLong long longLong long longLong long "
127 "longLong long longLong long longLong long longLong long longLong long "
128 "longLong long longLong long longLong long longLong long longLong long "
129 "longLong long long. END";
130 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400131
Lei Zhanga21d5932018-02-05 18:28:38 +0000132 // Check that the quadpoints are correct.
133 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000134 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 EXPECT_EQ(115.802643f, quadpoints.x1);
136 EXPECT_EQ(718.913940f, quadpoints.y1);
137 EXPECT_EQ(157.211182f, quadpoints.x4);
138 EXPECT_EQ(706.264465f, quadpoints.y4);
139 }
Lei Zhang75c81712018-02-08 17:22:39 +0000140 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400141}
142
143TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
144 // Open a file with three annotations and load its first page.
145 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000146 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
147 // the FORM API calls in LoadPage() makes this test fail. So use
148 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu4fd9a472017-06-01 18:56:09 -0400149 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
150 ASSERT_TRUE(page);
151
152 // Check that there is a total of 3 annotation on its first page.
153 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
154
Lei Zhanga21d5932018-02-05 18:28:38 +0000155 {
156 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000157 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000158 ASSERT_TRUE(annot);
159 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400160
Lei Zhanga21d5932018-02-05 18:28:38 +0000161 // Check that the annotation color is blue with opacity.
162 unsigned int R;
163 unsigned int G;
164 unsigned int B;
165 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000166 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000167 &G, &B, &A));
168 EXPECT_EQ(0u, R);
169 EXPECT_EQ(0u, G);
170 EXPECT_EQ(255u, B);
171 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400172
Lei Zhanga21d5932018-02-05 18:28:38 +0000173 // Check that there is no content.
174 EXPECT_EQ(2u,
175 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400176
Lei Zhanga21d5932018-02-05 18:28:38 +0000177 // Check that the rectange coordinates are correct.
178 // Note that upon rendering, the rectangle coordinates will be adjusted.
179 FS_RECTF rect;
180 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
181 EXPECT_EQ(351.820404f, rect.left);
182 EXPECT_EQ(583.830688f, rect.bottom);
183 EXPECT_EQ(475.336090f, rect.right);
184 EXPECT_EQ(681.535034f, rect.top);
185 }
Lei Zhang75c81712018-02-08 17:22:39 +0000186 FPDF_ClosePage(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400187}
Jane Liu20eafda2017-06-07 10:33:24 -0400188
189TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
190 // Open a file with one annotation and load its first page.
191 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000192 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400193 ASSERT_TRUE(page);
194
195 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400196 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400197
198 UnloadPage(page);
199}
200
Jane Liud321ef92017-06-14 09:56:22 -0400201TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400202 // Open a file with no annotation and load its first page.
203 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000204 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400205 ASSERT_TRUE(page);
206 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
207
Lei Zhanga21d5932018-02-05 18:28:38 +0000208 {
209 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000210 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000211 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400212
Lei Zhanga21d5932018-02-05 18:28:38 +0000213 // Check that there is now 1 annotations on this page.
214 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400215
Lei Zhanga21d5932018-02-05 18:28:38 +0000216 // Check that the subtype of the annotation is correct.
217 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
218 }
Jane Liue10509a2017-06-20 16:47:41 -0400219
Lei Zhanga21d5932018-02-05 18:28:38 +0000220 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000221 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000222 ASSERT_TRUE(annot);
223 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400224
Lei Zhanga21d5932018-02-05 18:28:38 +0000225 // Set the color of the annotation.
226 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
227 102, 153, 204));
228 // Check that the color has been set correctly.
229 unsigned int R;
230 unsigned int G;
231 unsigned int B;
232 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000233 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000234 &G, &B, &A));
235 EXPECT_EQ(51u, R);
236 EXPECT_EQ(102u, G);
237 EXPECT_EQ(153u, B);
238 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400239
Lei Zhanga21d5932018-02-05 18:28:38 +0000240 // Change the color of the annotation.
241 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
242 153, 102, 51));
243 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000244 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000245 &G, &B, &A));
246 EXPECT_EQ(204u, R);
247 EXPECT_EQ(153u, G);
248 EXPECT_EQ(102u, B);
249 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 // Set the annotation rectangle.
252 FS_RECTF rect;
253 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
254 EXPECT_EQ(0.f, rect.left);
255 EXPECT_EQ(0.f, rect.right);
256 rect.left = 35;
257 rect.bottom = 150;
258 rect.right = 53;
259 rect.top = 165;
260 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
261 // Check that the annotation rectangle has been set correctly.
262 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
263 EXPECT_EQ(35.f, rect.left);
264 EXPECT_EQ(150.f, rect.bottom);
265 EXPECT_EQ(53.f, rect.right);
266 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400267
Lei Zhanga21d5932018-02-05 18:28:38 +0000268 // Set the content of the annotation.
269 static constexpr wchar_t kContents[] =
270 L"Hello! This is a customized content.";
271 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
272 GetFPDFWideString(kContents);
273 ASSERT_TRUE(
274 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
275 // Check that the content has been set correctly.
276 unsigned long len =
277 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
278 std::vector<char> buf(len);
279 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
280 buf.data(), len));
281 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
282 }
Jane Liu20eafda2017-06-07 10:33:24 -0400283 UnloadPage(page);
284}
285
286TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
287 // Open a file with one annotation and load its first page.
288 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000289 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400290 ASSERT_TRUE(page);
291
292 // Check that there is a total of one annotation on its first page, and verify
293 // its quadpoints.
294 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400295 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000298 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000299 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000300 EXPECT_EQ(115.802643f, quadpoints.x1);
301 EXPECT_EQ(718.913940f, quadpoints.y1);
302 EXPECT_EQ(157.211182f, quadpoints.x4);
303 EXPECT_EQ(706.264465f, quadpoints.y4);
304 }
Jane Liu20eafda2017-06-07 10:33:24 -0400305
306 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000307 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000308 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000309 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
310 ASSERT_TRUE(annot);
311 quadpoints.x1 = 140.802643f;
312 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000313 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000314 }
Jane Liu20eafda2017-06-07 10:33:24 -0400315
316 // Save the document, closing the page and document.
317 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000318 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400319
320 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000321 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400322
Tom Sepezb9c3e272018-08-14 18:22:06 +0000323 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000324 page = LoadSavedPage(0);
325 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400326
327 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000328 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400329
Lei Zhanga21d5932018-02-05 18:28:38 +0000330 {
331 // Check that the second annotation is an underline annotation and verify
332 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000333 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000334 ASSERT_TRUE(new_annot);
335 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
336 FS_QUADPOINTSF new_quadpoints;
337 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000338 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000339 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
340 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
341 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
342 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
343 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400344
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000345 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400346 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400347}
Jane Liu06462752017-06-27 16:41:14 -0400348
Ralf Sippl16381792018-04-12 21:20:26 +0000349TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
350 // Open a file with four annotations and load its first page.
351 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
352 FPDF_PAGE page = LoadPage(0);
353 ASSERT_TRUE(page);
354 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
355
356 // Retrieve the highlight annotation.
357 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
358 ASSERT_TRUE(annot);
359 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
360
361 FS_QUADPOINTSF quadpoints;
362 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
363
364 {
365 // Verify the current one set of quadpoints.
366 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
367
368 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
369 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
370 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
371 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
372 }
373
374 {
375 // Update the quadpoints.
376 FS_QUADPOINTSF new_quadpoints = quadpoints;
377 new_quadpoints.y1 -= 20.f;
378 new_quadpoints.y2 -= 20.f;
379 new_quadpoints.y3 -= 20.f;
380 new_quadpoints.y4 -= 20.f;
381 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
382
383 // Verify added quadpoint set
384 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
385 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
386 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
387 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
388 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
389 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
390 }
391
392 {
393 // Append a new set of quadpoints.
394 FS_QUADPOINTSF new_quadpoints = quadpoints;
395 new_quadpoints.y1 += 20.f;
396 new_quadpoints.y2 += 20.f;
397 new_quadpoints.y3 += 20.f;
398 new_quadpoints.y4 += 20.f;
399 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
400
401 // Verify added quadpoint set
402 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
403 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
404 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
405 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
406 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
407 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
408 }
409
410 {
411 // Setting and getting quadpoints at out-of-bound index should fail
412 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
413 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
414 }
415
416 FPDFPage_CloseAnnot(annot);
417
418 // Retrieve the square annotation
419 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
420
421 {
422 // Check that attempting to set its quadpoints would fail
423 ASSERT_TRUE(squareAnnot);
424 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
425 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
426 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
427 }
428
429 FPDFPage_CloseAnnot(squareAnnot);
430
431 UnloadPage(page);
432}
433
Jane Liu06462752017-06-27 16:41:14 -0400434TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400435#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400436 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
437 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
438 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400439#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000440 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
441 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
442 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400443#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000444 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
445 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
446 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400447#endif
448
Jane Liu06462752017-06-27 16:41:14 -0400449 // Open a file with four annotations and load its first page.
450 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000451 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400452 ASSERT_TRUE(page);
453 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
454
Jane Liub370e5a2017-08-16 13:24:58 -0400455 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000456 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000457 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000458 CompareBitmap(bitmap.get(), 612, 792, md5_original);
459 }
Jane Liub370e5a2017-08-16 13:24:58 -0400460
Jane Liu0c6b07d2017-08-15 10:50:22 -0400461 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400462 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000463
464 // Retrieve the highlight annotation which has its AP stream already defined.
465 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000466 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000467 ASSERT_TRUE(annot);
468 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
469
470 // Check that color cannot be set when an AP stream is defined already.
471 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
472 102, 153, 204));
473
474 // Verify its attachment points.
475 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000476 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000477 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
478 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
479 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
480 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
481
482 // Check that updating the attachment points would succeed.
483 quadpoints.x1 -= 50.f;
484 quadpoints.x2 -= 50.f;
485 quadpoints.x3 -= 50.f;
486 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000487 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000488 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000489 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000490 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
491 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
492 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
493 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
494
495 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000496 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000497 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000498 CompareBitmap(bitmap.get(), 612, 792, md5_original);
499 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000500
501 // Verify its annotation rectangle.
502 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
503 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
504 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
505 EXPECT_NEAR(136.325f, rect.right, 0.001f);
506 EXPECT_NEAR(721.292f, rect.top, 0.001f);
507
508 // Check that updating the rectangle would succeed.
509 rect.left -= 60.f;
510 rect.right -= 60.f;
511 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
512 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
513 EXPECT_EQ(rect.right, new_rect.right);
514 }
Jane Liu06462752017-06-27 16:41:14 -0400515
Jane Liub370e5a2017-08-16 13:24:58 -0400516 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000517 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000518 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000519 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
520 }
Jane Liub370e5a2017-08-16 13:24:58 -0400521
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 {
523 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000524 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 ASSERT_TRUE(annot);
526 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400527
Lei Zhanga21d5932018-02-05 18:28:38 +0000528 // Check that updating the rectangle would succeed.
529 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
530 rect.left += 70.f;
531 rect.right += 70.f;
532 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
533 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
534 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400535
Lei Zhanga21d5932018-02-05 18:28:38 +0000536 // Check that updating the rectangle changes the square annotation's
537 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000538 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000539 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000540 }
Jane Liub370e5a2017-08-16 13:24:58 -0400541
Jane Liu06462752017-06-27 16:41:14 -0400542 UnloadPage(page);
543}
Jane Liu8ce58f52017-06-29 13:40:22 -0400544
Henrique Nakashima5098b252018-03-26 21:46:00 +0000545TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
546 // Open a file with multiline markup annotations.
547 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
548 FPDF_PAGE page = LoadPage(0);
549 ASSERT_TRUE(page);
550 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000551 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000552 ASSERT_TRUE(annot);
553
554 // This is a three line annotation.
555 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
556 }
557 UnloadPage(page);
558
559 // null annotation should return 0
560 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
561}
562
Jane Liu8ce58f52017-06-29 13:40:22 -0400563TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
564 // Open a file with 3 annotations on its first page.
565 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000566 // TODO(thestig): This test should use LoadPage() and UnloadPage(), but one of
567 // the FORM API calls in LoadPage() makes this test fail. So use
568 // FPDF_LoadPage() and FPDF_ClosePage() for now.
Jane Liu8ce58f52017-06-29 13:40:22 -0400569 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
570 ASSERT_TRUE(page);
571 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
572
Jane Liu0c6b07d2017-08-15 10:50:22 -0400573 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400574
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 // Check that the annotations have the expected rectangle coordinates.
576 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000577 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000578 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
579 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
580 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400581
Lei Zhanga21d5932018-02-05 18:28:38 +0000582 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000583 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000584 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
585 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
586 }
587
588 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000589 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000590 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
591 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
592 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400593
594 // Check that nothing happens when attempting to remove an annotation with an
595 // out-of-bound index.
596 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
597 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
598 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
599
600 // Remove the second annotation.
601 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
602 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
603 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
604
605 // Save the document, closing the page and document.
606 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
607 FPDF_ClosePage(page);
608
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400609 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400610 // Open the saved document.
611 std::string new_file = GetString();
612 FPDF_FILEACCESS file_access;
613 memset(&file_access, 0, sizeof(file_access));
614 file_access.m_FileLen = new_file.size();
615 file_access.m_GetBlock = GetBlockFromString;
616 file_access.m_Param = &new_file;
617 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
618 ASSERT_TRUE(new_doc);
619 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
620 ASSERT_TRUE(new_page);
621
622 // Check that the saved document has 2 annotations on the first page.
623 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
624
Lei Zhanga21d5932018-02-05 18:28:38 +0000625 // Check that the remaining 2 annotations are the original 1st and 3rd ones
626 // by verifying their rectangle coordinates.
627 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000628 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000629 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
630 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
631 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400632
Lei Zhanga21d5932018-02-05 18:28:38 +0000633 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000634 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000635 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
636 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
637 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400638 FPDF_ClosePage(new_page);
639 FPDF_CloseDocument(new_doc);
640}
Jane Liu8ce58f52017-06-29 13:40:22 -0400641
Jane Liubaa7ff42017-06-29 19:18:23 -0400642TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400643#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400644 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000645 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
646 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
647 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000648#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
649 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
650 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
651 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
652 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400653#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000654 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000655 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
656 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
657 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400658#endif
659
660 // Open a file with two annotations and load its first page.
661 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000662 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400663 ASSERT_TRUE(page);
664 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
665
666 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000667 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000668 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000669 CompareBitmap(bitmap.get(), 595, 842, md5_original);
670 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400671
Lei Zhanga21d5932018-02-05 18:28:38 +0000672 {
673 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000674 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000675 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400676
Lei Zhanga21d5932018-02-05 18:28:38 +0000677 // Check that this annotation has one path object and retrieve it.
678 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000679 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000680 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
681 EXPECT_FALSE(path);
682 path = FPDFAnnot_GetObject(annot.get(), 0);
683 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
684 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400685
Lei Zhanga21d5932018-02-05 18:28:38 +0000686 // Modify the color of the path object.
687 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
688 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400689
Lei Zhanga21d5932018-02-05 18:28:38 +0000690 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000691 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000692 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000693 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
694 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400695
Lei Zhanga21d5932018-02-05 18:28:38 +0000696 // Add a second path object to the same annotation.
697 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
698 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
699 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
700 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
701 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
702 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
703 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400704
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000705 // The object is in the annontation, not in the page, so the page object
706 // array should not change.
707 ASSERT_EQ(32, FPDFPage_CountObjects(page));
708
Lei Zhanga21d5932018-02-05 18:28:38 +0000709 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000710 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000711 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000712 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
713 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400714
Lei Zhanga21d5932018-02-05 18:28:38 +0000715 // Delete the newly added path object.
716 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
717 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000718 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000719 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400720
721 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000722 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000723 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000724 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
725 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400726
Jane Liubaa7ff42017-06-29 19:18:23 -0400727 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400728
Lei Zhanga21d5932018-02-05 18:28:38 +0000729 {
730 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000731 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000732 ASSERT_TRUE(annot);
733 rect.left = 200.f;
734 rect.bottom = 400.f;
735 rect.right = 500.f;
736 rect.top = 600.f;
737 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400738
Lei Zhanga21d5932018-02-05 18:28:38 +0000739 // Add a new path to the annotation.
740 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
741 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
742 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
743 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
744 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
745 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
746 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
747 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
748 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
749 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
750
751 // Check that the annotation's bounding box came from its rectangle.
752 FS_RECTF new_rect;
753 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
754 EXPECT_EQ(rect.left, new_rect.left);
755 EXPECT_EQ(rect.bottom, new_rect.bottom);
756 EXPECT_EQ(rect.right, new_rect.right);
757 EXPECT_EQ(rect.top, new_rect.top);
758 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400759
760 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400761 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000762 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400763
764 // Open the saved document.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000765 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000766 page = LoadSavedPage(0);
767 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400768
Jane Liu36567742017-07-06 11:13:35 -0400769 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000770 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400771
Lei Zhanga21d5932018-02-05 18:28:38 +0000772 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000773 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000774 ASSERT_TRUE(annot);
775 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400776
Lei Zhanga21d5932018-02-05 18:28:38 +0000777 // Check that the new annotation's rectangle is as defined.
778 FS_RECTF new_rect;
779 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
780 EXPECT_EQ(rect.left, new_rect.left);
781 EXPECT_EQ(rect.bottom, new_rect.bottom);
782 EXPECT_EQ(rect.right, new_rect.right);
783 EXPECT_EQ(rect.top, new_rect.top);
784 }
785
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000786 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400787 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400788}
Jane Liub137e752017-07-05 15:04:33 -0400789
790TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
791 // Open a file with an annotation and load its first page.
792 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000793 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400794 ASSERT_TRUE(page);
795
796 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000797 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000798 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000799 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
800 }
Jane Liub137e752017-07-05 15:04:33 -0400801
Lei Zhanga21d5932018-02-05 18:28:38 +0000802 {
803 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000804 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000805 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400806
Lei Zhanga21d5932018-02-05 18:28:38 +0000807 // Check that the original flag values are as expected.
808 int flags = FPDFAnnot_GetFlags(annot.get());
809 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
810 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400811
Lei Zhanga21d5932018-02-05 18:28:38 +0000812 // Set the HIDDEN flag.
813 flags |= FPDF_ANNOT_FLAG_HIDDEN;
814 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
815 flags = FPDFAnnot_GetFlags(annot.get());
816 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
817 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400818
Lei Zhanga21d5932018-02-05 18:28:38 +0000819 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000820 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000821 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000822 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
823 }
Jane Liub137e752017-07-05 15:04:33 -0400824
Lei Zhanga21d5932018-02-05 18:28:38 +0000825 // Unset the HIDDEN flag.
826 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
827 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
828 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
829 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
830 flags = FPDFAnnot_GetFlags(annot.get());
831 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
832 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400833
Lei Zhanga21d5932018-02-05 18:28:38 +0000834 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000835 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000836 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000837 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
838 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000839 }
Jane Liub137e752017-07-05 15:04:33 -0400840
Jane Liub137e752017-07-05 15:04:33 -0400841 UnloadPage(page);
842}
Jane Liu36567742017-07-06 11:13:35 -0400843
844TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400845#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400846 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
847 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
848 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000849#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
850 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
851 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
852 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400853#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000854 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
855 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
856 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400857#endif
858
859 // Open a file with two annotations and load its first page.
860 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000861 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400862 ASSERT_TRUE(page);
863 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
864
865 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000866 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000867 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000868 CompareBitmap(bitmap.get(), 595, 842, md5_original);
869 }
Jane Liu36567742017-07-06 11:13:35 -0400870
Jane Liu36567742017-07-06 11:13:35 -0400871 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000872 FPDF_BITMAP image_bitmap;
873
874 {
875 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000876 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000877 ASSERT_TRUE(annot);
878 FS_RECTF rect;
879 rect.left = 200.f;
880 rect.bottom = 600.f;
881 rect.right = 400.f;
882 rect.top = 800.f;
883 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
884
885 // Add a solid-color translucent image object to the new annotation.
886 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
887 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
888 0xeeeecccc);
889 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
890 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
891 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
892 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
893 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
894 kBitmapSize, 0, 0));
895 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
896 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
897 }
Jane Liu36567742017-07-06 11:13:35 -0400898
899 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000900 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000901 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000902 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
903 }
Jane Liu36567742017-07-06 11:13:35 -0400904
Lei Zhanga21d5932018-02-05 18:28:38 +0000905 {
906 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000907 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000908 ASSERT_TRUE(annot);
909 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
910 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
911 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400912
Lei Zhanga21d5932018-02-05 18:28:38 +0000913 // Modify the image in the new annotation.
914 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
915 0xff000000);
916 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
917 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
918 }
Jane Liu36567742017-07-06 11:13:35 -0400919
920 // Save the document, closing the page and document.
921 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000922 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400923 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400924
925 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400926 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400927}
928
929TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400930#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400931 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
932 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
933 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000934#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
935 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
936 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
937 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400938#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000939 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
940 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
941 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400942#endif
943
944 // Open a file with two annotations and load its first page.
945 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000946 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400947 ASSERT_TRUE(page);
948 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
949
950 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000951 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000952 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000953 CompareBitmap(bitmap.get(), 595, 842, md5_original);
954 }
Jane Liu36567742017-07-06 11:13:35 -0400955
Lei Zhanga21d5932018-02-05 18:28:38 +0000956 {
957 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000958 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000959 ASSERT_TRUE(annot);
960 FS_RECTF rect;
961 rect.left = 200.f;
962 rect.bottom = 550.f;
963 rect.right = 450.f;
964 rect.top = 650.f;
965 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400966
Lei Zhanga21d5932018-02-05 18:28:38 +0000967 // Add a translucent text object to the new annotation.
968 FPDF_PAGEOBJECT text_object =
969 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
970 EXPECT_TRUE(text_object);
971 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
972 GetFPDFWideString(L"I'm a translucent text laying on other text.");
973 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
974 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
975 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
976 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
977 }
Jane Liu36567742017-07-06 11:13:35 -0400978
979 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000980 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000981 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000982 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
983 }
Jane Liu36567742017-07-06 11:13:35 -0400984
Lei Zhanga21d5932018-02-05 18:28:38 +0000985 {
986 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000987 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000988 ASSERT_TRUE(annot);
989 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
990 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
991 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -0400992
Lei Zhanga21d5932018-02-05 18:28:38 +0000993 // Modify the text in the new annotation.
994 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
995 GetFPDFWideString(L"New text!");
996 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
997 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
998 }
Jane Liu36567742017-07-06 11:13:35 -0400999
1000 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001001 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001002 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001003 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1004 }
Jane Liu36567742017-07-06 11:13:35 -04001005
1006 // Remove the new annotation, and check that the page renders as before.
1007 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001008 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001009 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001010 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1011 }
Jane Liu36567742017-07-06 11:13:35 -04001012
1013 UnloadPage(page);
1014}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001015
1016TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1017 // Open a file with four annotations and load its first page.
1018 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001019 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001020 ASSERT_TRUE(page);
1021
Lei Zhangdf064df2017-08-31 02:33:27 -07001022 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001023 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001024
Lei Zhanga21d5932018-02-05 18:28:38 +00001025 {
1026 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001027 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001028 ASSERT_TRUE(annot);
1029
1030 // Check that a non-existent key does not exist.
1031 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1032
1033 // Check that the string value of a non-string dictionary entry is empty.
1034 static constexpr char kApKey[] = "AP";
1035 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1036 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1037 FPDFAnnot_GetValueType(annot.get(), kApKey));
1038 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1039
1040 // Check that the string value of the hash is correct.
1041 static constexpr char kHashKey[] = "AAPL:Hash";
1042 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1043 unsigned long len =
1044 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1045 std::vector<char> buf(len);
1046 EXPECT_EQ(66u,
1047 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1048 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1049 BufferToWString(buf).c_str());
1050
1051 // Check that the string value of the modified date is correct.
1052 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1053 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1054 buf.clear();
1055 buf.resize(len);
1056 EXPECT_EQ(44u,
1057 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1058 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1059
1060 // Update the date entry for the annotation.
1061 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1062 GetFPDFWideString(kNewDate);
1063 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1064 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001065
1066 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001067 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001068 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001069
Dan Sinclair698aed72017-09-26 16:24:49 -04001070#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001071 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001072#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1073 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001074#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001075 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001076#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001077
1078 // Open the saved annotation.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001079 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001080 page = LoadSavedPage(0);
1081 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001082 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001083 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001084
Lei Zhanga21d5932018-02-05 18:28:38 +00001085 // Check that the string value of the modified date is the newly-set value.
1086 EXPECT_EQ(FPDF_OBJECT_STRING,
1087 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1088 unsigned long len =
1089 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1090 std::vector<char> buf(len);
1091 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1092 buf.data(), len));
1093 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1094 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001095
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001096 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001097 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001098}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001099
Henrique Nakashima5970a472018-01-11 22:40:59 +00001100TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001101 // Open a file with four annotations and load its first page.
1102 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001103 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001104 ASSERT_TRUE(page);
1105
Lei Zhanga21d5932018-02-05 18:28:38 +00001106 {
1107 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001108 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001109 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001110
Lei Zhanga21d5932018-02-05 18:28:38 +00001111 // Check that the string value of an AP returns the expected length.
1112 unsigned long normal_len = FPDFAnnot_GetAP(
1113 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1114 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001115
Lei Zhanga21d5932018-02-05 18:28:38 +00001116 // Check that the string value of an AP is not returned if the buffer is too
1117 // small. The result buffer should be overwritten with an empty string.
1118 std::vector<char> buf(normal_len - 1);
1119 // Write L"z" in the buffer to verify it's not overwritten.
1120 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1121 EXPECT_EQ(73970u,
1122 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1123 buf.data(), buf.size()));
1124 std::string ap = BufferToString(buf);
1125 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001126
Lei Zhanga21d5932018-02-05 18:28:38 +00001127 // Check that the string value of an AP is returned through a buffer that is
1128 // the right size.
1129 buf.clear();
1130 buf.resize(normal_len);
1131 EXPECT_EQ(73970u,
1132 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1133 buf.data(), buf.size()));
1134 ap = BufferToString(buf);
1135 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1136 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001137
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 // Check that the string value of an AP is returned through a buffer that is
1139 // larger than necessary.
1140 buf.clear();
1141 buf.resize(normal_len + 1);
1142 EXPECT_EQ(73970u,
1143 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1144 buf.data(), buf.size()));
1145 ap = BufferToString(buf);
1146 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1147 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001148
Lei Zhanga21d5932018-02-05 18:28:38 +00001149 // Check that getting an AP for a mode that does not have an AP returns an
1150 // empty string.
1151 unsigned long rollover_len = FPDFAnnot_GetAP(
1152 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1153 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001154
Lei Zhanga21d5932018-02-05 18:28:38 +00001155 buf.clear();
1156 buf.resize(1000);
1157 EXPECT_EQ(2u,
1158 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1159 buf.data(), buf.size()));
1160 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001161
Lei Zhanga21d5932018-02-05 18:28:38 +00001162 // Check that setting the AP for an invalid appearance mode fails.
1163 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1164 GetFPDFWideString(L"new test ap");
1165 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1166 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1167 apText.get()));
1168 EXPECT_FALSE(FPDFAnnot_SetAP(
1169 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001170
Lei Zhanga21d5932018-02-05 18:28:38 +00001171 // Set the AP correctly now.
1172 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1173 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001174
Lei Zhanga21d5932018-02-05 18:28:38 +00001175 // Check that the new annotation value is equal to the value we just set.
1176 rollover_len = FPDFAnnot_GetAP(
1177 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1178 EXPECT_EQ(24u, rollover_len);
1179 buf.clear();
1180 buf.resize(rollover_len);
1181 EXPECT_EQ(24u,
1182 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1183 buf.data(), buf.size()));
1184 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 // Check that the Normal AP was not touched when the Rollover AP was set.
1187 buf.clear();
1188 buf.resize(normal_len);
1189 EXPECT_EQ(73970u,
1190 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1191 buf.data(), buf.size()));
1192 ap = BufferToString(buf);
1193 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1194 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1195 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001196
1197 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001198 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001199 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001200
Tom Sepezb9c3e272018-08-14 18:22:06 +00001201 OpenSavedDocument(nullptr);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001202 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001204 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001205
Lei Zhanga21d5932018-02-05 18:28:38 +00001206 // Check that the new annotation value is equal to the value we set before
1207 // saving.
1208 unsigned long rollover_len = FPDFAnnot_GetAP(
1209 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1210 EXPECT_EQ(24u, rollover_len);
1211 std::vector<char> buf(rollover_len);
1212 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1213 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1214 buf.data(), buf.size()));
1215 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1216 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001217
1218 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001219 CloseSavedPage(page);
1220 CloseSavedDocument();
1221}
1222
1223TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1224 // Open a file with four annotations and load its first page.
1225 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001226 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001227 ASSERT_TRUE(page);
1228
Lei Zhanga21d5932018-02-05 18:28:38 +00001229 {
1230 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001231 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001232 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001233
Lei Zhanga21d5932018-02-05 18:28:38 +00001234 // Set Down AP. Normal AP is already set.
1235 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1236 GetFPDFWideString(L"new test ap");
1237 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1238 apText.get()));
1239 EXPECT_EQ(73970u,
1240 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1241 nullptr, 0));
1242 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1243 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001244
Lei Zhanga21d5932018-02-05 18:28:38 +00001245 // Check that setting the Down AP to null removes the Down entry but keeps
1246 // Normal intact.
1247 EXPECT_TRUE(
1248 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1249 EXPECT_EQ(73970u,
1250 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1251 nullptr, 0));
1252 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1253 nullptr, 0));
1254 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001255
Lei Zhang75c81712018-02-08 17:22:39 +00001256 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001257}
1258
1259TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1260 // Open a file with four annotations and load its first page.
1261 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001262 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001263 ASSERT_TRUE(page);
1264
Lei Zhanga21d5932018-02-05 18:28:38 +00001265 {
1266 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001267 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001268 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001269
Lei Zhanga21d5932018-02-05 18:28:38 +00001270 // Set Down AP. Normal AP is already set.
1271 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1272 GetFPDFWideString(L"new test ap");
1273 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1274 apText.get()));
1275 EXPECT_EQ(73970u,
1276 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1277 nullptr, 0));
1278 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1279 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001280
Lei Zhanga21d5932018-02-05 18:28:38 +00001281 // Check that setting the Normal AP to null removes the whole AP dictionary.
1282 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1283 nullptr));
1284 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1285 nullptr, 0));
1286 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1287 nullptr, 0));
1288 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001289
Lei Zhang75c81712018-02-08 17:22:39 +00001290 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001291}
1292
Jane Liu300bb272017-08-21 14:37:53 -04001293TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1294 // Open a file with annotations and load its first page.
1295 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001296 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001297 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001298 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001299
Lei Zhanga21d5932018-02-05 18:28:38 +00001300 {
1301 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001302 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001303 ASSERT_TRUE(annot);
1304 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1305 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1306 static constexpr char kPopupKey[] = "Popup";
1307 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1308 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1309 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001310
Lei Zhanga21d5932018-02-05 18:28:38 +00001311 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001312 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1314 ASSERT_TRUE(popup);
1315 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1316 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1317 FS_RECTF rect;
1318 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1319 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1320 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001321
Lei Zhanga21d5932018-02-05 18:28:38 +00001322 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1323 // since "IRT" is not a key in |annot|'s dictionary.
1324 static constexpr char kIRTKey[] = "IRT";
1325 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1326 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001327
Lei Zhanga21d5932018-02-05 18:28:38 +00001328 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1329 // would fail, since its parent is not an annotation.
1330 static constexpr char kPKey[] = "P";
1331 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1332 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1333 FPDFAnnot_GetValueType(annot.get(), kPKey));
1334 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1335 }
Jane Liu300bb272017-08-21 14:37:53 -04001336
Jane Liu300bb272017-08-21 14:37:53 -04001337 UnloadPage(page);
1338}
1339
Diana Gage7e0c05d2017-07-19 17:33:33 -07001340TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1341 // Open file with form text fields.
1342 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001343 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001344 ASSERT_TRUE(page);
1345
Lei Zhanga21d5932018-02-05 18:28:38 +00001346 {
1347 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001348 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001349 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001350
Lei Zhanga21d5932018-02-05 18:28:38 +00001351 // Check that the flag values are as expected.
1352 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1353 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1354 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001355
Lei Zhanga21d5932018-02-05 18:28:38 +00001356 {
1357 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001358 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001360
Lei Zhanga21d5932018-02-05 18:28:38 +00001361 // Check that the flag values are as expected.
1362 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1363 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1364 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001365
1366 UnloadPage(page);
1367}
1368
1369TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1370 // Open file with form text fields.
1371 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001372 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001373 ASSERT_TRUE(page);
1374
Lei Zhanga21d5932018-02-05 18:28:38 +00001375 {
1376 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001377 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001379
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 // Check that the flag values are as expected.
1381 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1382 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1383 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1384 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1385 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001386
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 {
1388 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001389 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001390 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001391
Lei Zhanga21d5932018-02-05 18:28:38 +00001392 // Check that the flag values are as expected.
1393 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1394 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1395 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1396 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1397 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001398
Lei Zhanga21d5932018-02-05 18:28:38 +00001399 {
1400 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001401 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001402 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001403
Lei Zhanga21d5932018-02-05 18:28:38 +00001404 // Check that the flag values are as expected.
1405 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1406 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1407 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1408 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1409 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001410
1411 UnloadPage(page);
1412}
Diana Gage40870db2017-07-19 18:16:03 -07001413
1414TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1415 // Open file with form text fields.
1416 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1417 FPDF_PAGE page = LoadPage(0);
1418 ASSERT_TRUE(page);
1419
1420 // Attempt to get an annotation where no annotation exists on page.
1421 FPDF_ANNOTATION annot =
1422 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
1423 EXPECT_FALSE(annot);
1424
1425 UnloadPage(page);
1426}
1427
1428TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1429 // Open file with form text fields.
1430 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1431 FPDF_PAGE page = LoadPage(0);
1432 ASSERT_TRUE(page);
1433
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 {
1435 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001436 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001437 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1438 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001439
Lei Zhanga21d5932018-02-05 18:28:38 +00001440 // Check that interactive form annotation flag values are as expected.
1441 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1442 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1443 }
Diana Gage40870db2017-07-19 18:16:03 -07001444
Lei Zhanga21d5932018-02-05 18:28:38 +00001445 {
1446 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001447 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001448 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1449 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001450
Lei Zhanga21d5932018-02-05 18:28:38 +00001451 // Check that interactive form annotation flag values are as expected.
1452 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1453 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1454 }
Diana Gage40870db2017-07-19 18:16:03 -07001455
1456 UnloadPage(page);
1457}
1458
1459TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1460 // Open file with form comboboxes.
1461 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1462 FPDF_PAGE page = LoadPage(0);
1463 ASSERT_TRUE(page);
1464
Lei Zhanga21d5932018-02-05 18:28:38 +00001465 {
1466 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001467 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001468 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001469 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001470
Lei Zhanga21d5932018-02-05 18:28:38 +00001471 // Check that interactive form annotation flag values are as expected.
1472 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1473 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1474 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1475 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1476 }
Diana Gage40870db2017-07-19 18:16:03 -07001477
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 {
1479 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001480 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001481 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001483
Lei Zhanga21d5932018-02-05 18:28:38 +00001484 // Check that interactive form annotation flag values are as expected.
1485 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1486 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1487 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1488 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1489 }
Diana Gage40870db2017-07-19 18:16:03 -07001490
Lei Zhanga21d5932018-02-05 18:28:38 +00001491 {
1492 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001494 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001495 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001496
Lei Zhanga21d5932018-02-05 18:28:38 +00001497 // Check that interactive form annotation flag values are as expected.
1498 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1499 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1500 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1501 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1502 }
Diana Gage40870db2017-07-19 18:16:03 -07001503
1504 UnloadPage(page);
1505}