blob: 33788b44cbbcb904158944be09725c077fdf2379 [file] [log] [blame]
Jane Liu4fd9a472017-06-01 18:56:09 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00005#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04006#include <memory>
7#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04008#include <vector>
9
Jane Liubaa7ff42017-06-29 19:18:23 -040010#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000011#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040013#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040014#include "public/fpdfview.h"
15#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000016#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040017#include "testing/gtest/include/gtest/gtest.h"
18
Lei Zhangdf064df2017-08-31 02:33:27 -070019static constexpr char kContentsKey[] = "Contents";
20
Nicolas Pena3ff54002017-07-05 11:55:35 -040021class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040022
Lei Zhang4afee172018-01-10 20:57:15 +000023std::wstring BufferToWString(const std::vector<char>& buf) {
24 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000025}
26
Lei Zhang4afee172018-01-10 20:57:15 +000027std::string BufferToString(const std::vector<char>& buf) {
28 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000029}
30
Lei Zhang7557e7b2018-09-14 17:02:40 +000031TEST_F(FPDFAnnotEmbeddertest, BadParams) {
32 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
33 FPDF_PAGE page = LoadPage(0);
34 ASSERT_TRUE(page);
35
36 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
37
38 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
43
44 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
45
46 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
47
48 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
51
52 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
53
54 static constexpr wchar_t kContents[] = L"Bar";
55 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
56 GetFPDFWideString(kContents);
57 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
58
59 char buffer[128];
60 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
62 EXPECT_EQ(0u,
63 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
64
65 UnloadPage(page);
66}
67
Jane Liue17011d2017-06-21 12:18:37 -040068TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
69 // Open a file with one annotation and load its first page.
70 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000071 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040072 ASSERT_TRUE(page);
73
74 // This annotation has a malformed appearance stream, which does not have its
75 // normal appearance defined, only its rollover appearance. In this case, its
76 // normal appearance should be generated, allowing the highlight annotation to
77 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000078 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000079 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040080
81 UnloadPage(page);
82}
83
Ralf Sipplb3a52402018-03-19 23:30:28 +000084TEST_F(FPDFAnnotEmbeddertest, RenderMultilineMarkupAnnotWithoutAP) {
85 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000086 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000087 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
88 FPDF_PAGE page = LoadPage(0);
89 ASSERT_TRUE(page);
90
Tom Sepeze08d2b12018-04-25 18:49:32 +000091 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000092 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
93
94 UnloadPage(page);
95}
96
Jane Liu4fd9a472017-06-01 18:56:09 -040097TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
98 // Open a file with one annotation and load its first page.
99 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000100 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400101 ASSERT_TRUE(page);
102
103 // Check that there is a total of 1 annotation on its first page.
104 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
105
106 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000107 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000108 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000109 ASSERT_TRUE(annot);
110 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400111
Lei Zhanga21d5932018-02-05 18:28:38 +0000112 // Check that the annotation color is yellow.
113 unsigned int R;
114 unsigned int G;
115 unsigned int B;
116 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000117 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000118 &G, &B, &A));
119 EXPECT_EQ(255u, R);
120 EXPECT_EQ(255u, G);
121 EXPECT_EQ(0u, B);
122 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400123
Lei Zhanga21d5932018-02-05 18:28:38 +0000124 // Check that the author is correct.
125 static constexpr char kAuthorKey[] = "T";
126 EXPECT_EQ(FPDF_OBJECT_STRING,
127 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
128 unsigned long len =
129 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
130 std::vector<char> buf(len);
131 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
132 len));
133 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400134
Lei Zhanga21d5932018-02-05 18:28:38 +0000135 // Check that the content is correct.
136 EXPECT_EQ(FPDF_OBJECT_STRING,
137 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
138 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
139 buf.clear();
140 buf.resize(len);
141 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
142 buf.data(), len));
143 const wchar_t contents[] =
144 L"This is a note for that highlight annotation. Very long highlight "
145 "annotation. Long long long Long long longLong long longLong long "
146 "longLong long longLong long longLong long longLong long longLong long "
147 "longLong long longLong long longLong long longLong long longLong long "
148 "longLong long longLong long longLong long longLong long longLong long "
149 "longLong long longLong long longLong long longLong long longLong long "
150 "longLong long longLong long longLong long longLong long longLong long "
151 "longLong long longLong long longLong long longLong long longLong long "
152 "longLong long longLong long longLong long longLong long longLong long "
153 "longLong long longLong long longLong long longLong long longLong long "
154 "longLong long longLong long longLong long longLong long longLong long "
155 "longLong long longLong long longLong long longLong long longLong long "
156 "longLong long longLong long longLong long longLong long longLong long "
157 "longLong long longLong long longLong long longLong long longLong long "
158 "longLong long longLong long longLong long longLong long longLong long "
159 "longLong long longLong long longLong long longLong long longLong long "
160 "longLong long longLong long longLong long longLong long longLong long "
161 "longLong long longLong long longLong long longLong long longLong long "
162 "longLong long longLong long longLong long longLong long longLong long "
163 "longLong long long. END";
164 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400165
Lei Zhanga21d5932018-02-05 18:28:38 +0000166 // Check that the quadpoints are correct.
167 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000168 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000169 EXPECT_EQ(115.802643f, quadpoints.x1);
170 EXPECT_EQ(718.913940f, quadpoints.y1);
171 EXPECT_EQ(157.211182f, quadpoints.x4);
172 EXPECT_EQ(706.264465f, quadpoints.y4);
173 }
Tom Sepez507d0192018-11-07 16:37:51 +0000174 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400175}
176
177TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
178 // Open a file with three annotations and load its first page.
179 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000180 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400181 ASSERT_TRUE(page);
182
183 // Check that there is a total of 3 annotation on its first page.
184 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
185
Lei Zhanga21d5932018-02-05 18:28:38 +0000186 {
187 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000188 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000189 ASSERT_TRUE(annot);
190 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400191
Lei Zhanga21d5932018-02-05 18:28:38 +0000192 // Check that the annotation color is blue with opacity.
193 unsigned int R;
194 unsigned int G;
195 unsigned int B;
196 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000197 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000198 &G, &B, &A));
199 EXPECT_EQ(0u, R);
200 EXPECT_EQ(0u, G);
201 EXPECT_EQ(255u, B);
202 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400203
Lei Zhanga21d5932018-02-05 18:28:38 +0000204 // Check that there is no content.
205 EXPECT_EQ(2u,
206 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400207
Lei Zhanga21d5932018-02-05 18:28:38 +0000208 // Check that the rectange coordinates are correct.
209 // Note that upon rendering, the rectangle coordinates will be adjusted.
210 FS_RECTF rect;
211 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
212 EXPECT_EQ(351.820404f, rect.left);
213 EXPECT_EQ(583.830688f, rect.bottom);
214 EXPECT_EQ(475.336090f, rect.right);
215 EXPECT_EQ(681.535034f, rect.top);
216 }
Tom Sepezef43c262018-11-07 16:41:32 +0000217 {
218 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
219 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
220 }
Tom Sepez507d0192018-11-07 16:37:51 +0000221 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400222}
Jane Liu20eafda2017-06-07 10:33:24 -0400223
224TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
225 // Open a file with one annotation and load its first page.
226 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000227 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400228 ASSERT_TRUE(page);
229
230 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400231 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400232
233 UnloadPage(page);
234}
235
Jane Liud321ef92017-06-14 09:56:22 -0400236TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400237 // Open a file with no annotation and load its first page.
238 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000239 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400240 ASSERT_TRUE(page);
241 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
242
Lei Zhanga21d5932018-02-05 18:28:38 +0000243 {
244 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000245 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000246 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400247
Lei Zhanga21d5932018-02-05 18:28:38 +0000248 // Check that there is now 1 annotations on this page.
249 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400250
Lei Zhanga21d5932018-02-05 18:28:38 +0000251 // Check that the subtype of the annotation is correct.
252 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
253 }
Jane Liue10509a2017-06-20 16:47:41 -0400254
Lei Zhanga21d5932018-02-05 18:28:38 +0000255 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000256 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000257 ASSERT_TRUE(annot);
258 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400259
Lei Zhanga21d5932018-02-05 18:28:38 +0000260 // Set the color of the annotation.
261 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
262 102, 153, 204));
263 // Check that the color has been set correctly.
264 unsigned int R;
265 unsigned int G;
266 unsigned int B;
267 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000268 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000269 &G, &B, &A));
270 EXPECT_EQ(51u, R);
271 EXPECT_EQ(102u, G);
272 EXPECT_EQ(153u, B);
273 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400274
Lei Zhanga21d5932018-02-05 18:28:38 +0000275 // Change the color of the annotation.
276 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
277 153, 102, 51));
278 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000279 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000280 &G, &B, &A));
281 EXPECT_EQ(204u, R);
282 EXPECT_EQ(153u, G);
283 EXPECT_EQ(102u, B);
284 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400285
Lei Zhanga21d5932018-02-05 18:28:38 +0000286 // Set the annotation rectangle.
287 FS_RECTF rect;
288 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
289 EXPECT_EQ(0.f, rect.left);
290 EXPECT_EQ(0.f, rect.right);
291 rect.left = 35;
292 rect.bottom = 150;
293 rect.right = 53;
294 rect.top = 165;
295 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
296 // Check that the annotation rectangle has been set correctly.
297 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
298 EXPECT_EQ(35.f, rect.left);
299 EXPECT_EQ(150.f, rect.bottom);
300 EXPECT_EQ(53.f, rect.right);
301 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400302
Lei Zhanga21d5932018-02-05 18:28:38 +0000303 // Set the content of the annotation.
304 static constexpr wchar_t kContents[] =
305 L"Hello! This is a customized content.";
306 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
307 GetFPDFWideString(kContents);
308 ASSERT_TRUE(
309 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
310 // Check that the content has been set correctly.
311 unsigned long len =
312 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
313 std::vector<char> buf(len);
314 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
315 buf.data(), len));
316 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
317 }
Jane Liu20eafda2017-06-07 10:33:24 -0400318 UnloadPage(page);
319}
320
321TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
322 // Open a file with one annotation and load its first page.
323 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000324 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400325 ASSERT_TRUE(page);
326
327 // Check that there is a total of one annotation on its first page, and verify
328 // its quadpoints.
329 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400330 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000331 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000332 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000333 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000334 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000335 EXPECT_EQ(115.802643f, quadpoints.x1);
336 EXPECT_EQ(718.913940f, quadpoints.y1);
337 EXPECT_EQ(157.211182f, quadpoints.x4);
338 EXPECT_EQ(706.264465f, quadpoints.y4);
339 }
Jane Liu20eafda2017-06-07 10:33:24 -0400340
341 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000342 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000343 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000344 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
345 ASSERT_TRUE(annot);
346 quadpoints.x1 = 140.802643f;
347 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000348 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000349 }
Jane Liu20eafda2017-06-07 10:33:24 -0400350
351 // Save the document, closing the page and document.
352 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000353 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400354
355 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000356 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400357
Tom Sepezb9c3e272018-08-14 18:22:06 +0000358 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000359 page = LoadSavedPage(0);
360 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400361
362 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000363 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400364
Lei Zhanga21d5932018-02-05 18:28:38 +0000365 {
366 // Check that the second annotation is an underline annotation and verify
367 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000368 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000369 ASSERT_TRUE(new_annot);
370 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
371 FS_QUADPOINTSF new_quadpoints;
372 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000373 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000374 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
375 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
376 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
377 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
378 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400379
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000380 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400381 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400382}
Jane Liu06462752017-06-27 16:41:14 -0400383
Ralf Sippl16381792018-04-12 21:20:26 +0000384TEST_F(FPDFAnnotEmbeddertest, GetAndSetQuadPoints) {
385 // Open a file with four annotations and load its first page.
386 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
387 FPDF_PAGE page = LoadPage(0);
388 ASSERT_TRUE(page);
389 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
390
391 // Retrieve the highlight annotation.
392 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
393 ASSERT_TRUE(annot);
394 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
395
396 FS_QUADPOINTSF quadpoints;
397 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
398
399 {
400 // Verify the current one set of quadpoints.
401 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
402
403 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
404 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
405 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
406 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
407 }
408
409 {
410 // Update the quadpoints.
411 FS_QUADPOINTSF new_quadpoints = quadpoints;
412 new_quadpoints.y1 -= 20.f;
413 new_quadpoints.y2 -= 20.f;
414 new_quadpoints.y3 -= 20.f;
415 new_quadpoints.y4 -= 20.f;
416 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
417
418 // Verify added quadpoint set
419 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
420 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
421 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
422 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
423 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
424 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
425 }
426
427 {
428 // Append a new set of quadpoints.
429 FS_QUADPOINTSF new_quadpoints = quadpoints;
430 new_quadpoints.y1 += 20.f;
431 new_quadpoints.y2 += 20.f;
432 new_quadpoints.y3 += 20.f;
433 new_quadpoints.y4 += 20.f;
434 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
435
436 // Verify added quadpoint set
437 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
438 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
439 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
440 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
441 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
442 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
443 }
444
445 {
446 // Setting and getting quadpoints at out-of-bound index should fail
447 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
448 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
449 }
450
451 FPDFPage_CloseAnnot(annot);
452
453 // Retrieve the square annotation
454 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
455
456 {
457 // Check that attempting to set its quadpoints would fail
458 ASSERT_TRUE(squareAnnot);
459 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
460 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
461 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
462 }
463
464 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000465 UnloadPage(page);
466}
467
Jane Liu06462752017-06-27 16:41:14 -0400468TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400469#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400470 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
471 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
472 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400473#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000474 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
475 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
476 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400477#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000478 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
479 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
480 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400481#endif
482
Jane Liu06462752017-06-27 16:41:14 -0400483 // Open a file with four annotations and load its first page.
484 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000485 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400486 ASSERT_TRUE(page);
487 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
488
Jane Liub370e5a2017-08-16 13:24:58 -0400489 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000490 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000491 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000492 CompareBitmap(bitmap.get(), 612, 792, md5_original);
493 }
Jane Liub370e5a2017-08-16 13:24:58 -0400494
Jane Liu0c6b07d2017-08-15 10:50:22 -0400495 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400496 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000497
498 // Retrieve the highlight annotation which has its AP stream already defined.
499 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000500 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000501 ASSERT_TRUE(annot);
502 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
503
504 // Check that color cannot be set when an AP stream is defined already.
505 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
506 102, 153, 204));
507
508 // Verify its attachment points.
509 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000510 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000511 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
512 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
513 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
514 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
515
516 // Check that updating the attachment points would succeed.
517 quadpoints.x1 -= 50.f;
518 quadpoints.x2 -= 50.f;
519 quadpoints.x3 -= 50.f;
520 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000521 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000522 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000523 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000524 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
525 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
526 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
527 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
528
529 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000530 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000531 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000532 CompareBitmap(bitmap.get(), 612, 792, md5_original);
533 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000534
535 // Verify its annotation rectangle.
536 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
537 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
538 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
539 EXPECT_NEAR(136.325f, rect.right, 0.001f);
540 EXPECT_NEAR(721.292f, rect.top, 0.001f);
541
542 // Check that updating the rectangle would succeed.
543 rect.left -= 60.f;
544 rect.right -= 60.f;
545 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
546 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
547 EXPECT_EQ(rect.right, new_rect.right);
548 }
Jane Liu06462752017-06-27 16:41:14 -0400549
Jane Liub370e5a2017-08-16 13:24:58 -0400550 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000551 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000552 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000553 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
554 }
Jane Liub370e5a2017-08-16 13:24:58 -0400555
Lei Zhanga21d5932018-02-05 18:28:38 +0000556 {
557 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000558 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000559 ASSERT_TRUE(annot);
560 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400561
Lei Zhanga21d5932018-02-05 18:28:38 +0000562 // Check that updating the rectangle would succeed.
563 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
564 rect.left += 70.f;
565 rect.right += 70.f;
566 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
567 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
568 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400569
Lei Zhanga21d5932018-02-05 18:28:38 +0000570 // Check that updating the rectangle changes the square annotation's
571 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000572 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000573 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000574 }
Jane Liub370e5a2017-08-16 13:24:58 -0400575
Jane Liu06462752017-06-27 16:41:14 -0400576 UnloadPage(page);
577}
Jane Liu8ce58f52017-06-29 13:40:22 -0400578
Henrique Nakashima5098b252018-03-26 21:46:00 +0000579TEST_F(FPDFAnnotEmbeddertest, CountAttachmentPoints) {
580 // Open a file with multiline markup annotations.
581 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
582 FPDF_PAGE page = LoadPage(0);
583 ASSERT_TRUE(page);
584 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000585 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000586 ASSERT_TRUE(annot);
587
588 // This is a three line annotation.
589 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
590 }
591 UnloadPage(page);
592
593 // null annotation should return 0
594 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
595}
596
Jane Liu8ce58f52017-06-29 13:40:22 -0400597TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
598 // Open a file with 3 annotations on its first page.
599 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000600 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400601 ASSERT_TRUE(page);
602 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
603
Jane Liu0c6b07d2017-08-15 10:50:22 -0400604 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400605
Lei Zhanga21d5932018-02-05 18:28:38 +0000606 // Check that the annotations have the expected rectangle coordinates.
607 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000608 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000609 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
610 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
611 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400612
Lei Zhanga21d5932018-02-05 18:28:38 +0000613 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000614 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000615 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
616 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
617 }
618
619 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000620 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000621 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
622 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
623 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400624
625 // Check that nothing happens when attempting to remove an annotation with an
626 // out-of-bound index.
627 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
628 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
629 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
630
631 // Remove the second annotation.
632 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
633 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
634 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
635
636 // Save the document, closing the page and document.
637 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000638 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400639
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400640 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400641 // Open the saved document.
642 std::string new_file = GetString();
643 FPDF_FILEACCESS file_access;
644 memset(&file_access, 0, sizeof(file_access));
645 file_access.m_FileLen = new_file.size();
646 file_access.m_GetBlock = GetBlockFromString;
647 file_access.m_Param = &new_file;
648 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
649 ASSERT_TRUE(new_doc);
650 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
651 ASSERT_TRUE(new_page);
652
653 // Check that the saved document has 2 annotations on the first page.
654 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
655
Lei Zhanga21d5932018-02-05 18:28:38 +0000656 // Check that the remaining 2 annotations are the original 1st and 3rd ones
657 // by verifying their rectangle coordinates.
658 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000659 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000660 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
661 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
662 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400663
Lei Zhanga21d5932018-02-05 18:28:38 +0000664 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000665 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000666 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
667 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
668 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400669 FPDF_ClosePage(new_page);
670 FPDF_CloseDocument(new_doc);
671}
Jane Liu8ce58f52017-06-29 13:40:22 -0400672
Jane Liubaa7ff42017-06-29 19:18:23 -0400673TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400674#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400675 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000676 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
677 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
678 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000679#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
680 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
681 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
682 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
683 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400684#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000685 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000686 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
687 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
688 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400689#endif
690
691 // Open a file with two annotations and load its first page.
692 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000693 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400694 ASSERT_TRUE(page);
695 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
696
697 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000698 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000699 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000700 CompareBitmap(bitmap.get(), 595, 842, md5_original);
701 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400702
Lei Zhanga21d5932018-02-05 18:28:38 +0000703 {
704 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000705 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000706 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400707
Lei Zhanga21d5932018-02-05 18:28:38 +0000708 // Check that this annotation has one path object and retrieve it.
709 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000710 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000711 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
712 EXPECT_FALSE(path);
713 path = FPDFAnnot_GetObject(annot.get(), 0);
714 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
715 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400716
Lei Zhanga21d5932018-02-05 18:28:38 +0000717 // Modify the color of the path object.
718 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
719 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400720
Lei Zhanga21d5932018-02-05 18:28:38 +0000721 // Check that the page with the modified annotation renders correctly.
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 Liu7a9a38b2017-07-11 13:47:37 -0400726
Lei Zhanga21d5932018-02-05 18:28:38 +0000727 // Add a second path object to the same annotation.
728 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
729 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
730 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
731 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
732 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
733 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
734 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400735
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000736 // The object is in the annontation, not in the page, so the page object
737 // array should not change.
738 ASSERT_EQ(32, FPDFPage_CountObjects(page));
739
Lei Zhanga21d5932018-02-05 18:28:38 +0000740 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000741 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000742 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000743 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
744 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400745
Lei Zhanga21d5932018-02-05 18:28:38 +0000746 // Delete the newly added path object.
747 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
748 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000749 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000750 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400751
752 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000753 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000754 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000755 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
756 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400757
Jane Liubaa7ff42017-06-29 19:18:23 -0400758 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400759
Lei Zhanga21d5932018-02-05 18:28:38 +0000760 {
761 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000762 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000763 ASSERT_TRUE(annot);
764 rect.left = 200.f;
765 rect.bottom = 400.f;
766 rect.right = 500.f;
767 rect.top = 600.f;
768 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400769
Lei Zhanga21d5932018-02-05 18:28:38 +0000770 // Add a new path to the annotation.
771 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
772 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
773 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
774 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
775 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
776 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
777 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
778 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
779 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
780 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
781
782 // Check that the annotation's bounding box came from its rectangle.
783 FS_RECTF new_rect;
784 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
785 EXPECT_EQ(rect.left, new_rect.left);
786 EXPECT_EQ(rect.bottom, new_rect.bottom);
787 EXPECT_EQ(rect.right, new_rect.right);
788 EXPECT_EQ(rect.top, new_rect.top);
789 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400790
791 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400792 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000793 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400794
795 // Open the saved document.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000796 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000797 page = LoadSavedPage(0);
798 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400799
Jane Liu36567742017-07-06 11:13:35 -0400800 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000801 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400802
Lei Zhanga21d5932018-02-05 18:28:38 +0000803 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000804 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000805 ASSERT_TRUE(annot);
806 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400807
Lei Zhanga21d5932018-02-05 18:28:38 +0000808 // Check that the new annotation's rectangle is as defined.
809 FS_RECTF new_rect;
810 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
811 EXPECT_EQ(rect.left, new_rect.left);
812 EXPECT_EQ(rect.bottom, new_rect.bottom);
813 EXPECT_EQ(rect.right, new_rect.right);
814 EXPECT_EQ(rect.top, new_rect.top);
815 }
816
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000817 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400818 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400819}
Jane Liub137e752017-07-05 15:04:33 -0400820
821TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
822 // Open a file with an annotation and load its first page.
823 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000824 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400825 ASSERT_TRUE(page);
826
827 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000828 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000829 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000830 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
831 }
Jane Liub137e752017-07-05 15:04:33 -0400832
Lei Zhanga21d5932018-02-05 18:28:38 +0000833 {
834 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000835 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000836 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400837
Lei Zhanga21d5932018-02-05 18:28:38 +0000838 // Check that the original flag values are as expected.
839 int flags = FPDFAnnot_GetFlags(annot.get());
840 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
841 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400842
Lei Zhanga21d5932018-02-05 18:28:38 +0000843 // Set the HIDDEN flag.
844 flags |= FPDF_ANNOT_FLAG_HIDDEN;
845 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
846 flags = FPDFAnnot_GetFlags(annot.get());
847 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
848 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400849
Lei Zhanga21d5932018-02-05 18:28:38 +0000850 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000851 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000852 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000853 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
854 }
Jane Liub137e752017-07-05 15:04:33 -0400855
Lei Zhanga21d5932018-02-05 18:28:38 +0000856 // Unset the HIDDEN flag.
857 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
858 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
859 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
860 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
861 flags = FPDFAnnot_GetFlags(annot.get());
862 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
863 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400864
Lei Zhanga21d5932018-02-05 18:28:38 +0000865 // Check that the page renders correctly as before.
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(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
869 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000870 }
Jane Liub137e752017-07-05 15:04:33 -0400871
Jane Liub137e752017-07-05 15:04:33 -0400872 UnloadPage(page);
873}
Jane Liu36567742017-07-06 11:13:35 -0400874
875TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400876#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400877 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
878 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
879 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000880#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
881 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
882 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
883 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400884#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000885 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
886 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
887 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400888#endif
889
890 // Open a file with two annotations and load its first page.
891 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000892 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400893 ASSERT_TRUE(page);
894 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
895
896 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000897 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000898 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000899 CompareBitmap(bitmap.get(), 595, 842, md5_original);
900 }
Jane Liu36567742017-07-06 11:13:35 -0400901
Jane Liu36567742017-07-06 11:13:35 -0400902 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000903 FPDF_BITMAP image_bitmap;
904
905 {
906 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000907 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000908 ASSERT_TRUE(annot);
909 FS_RECTF rect;
910 rect.left = 200.f;
911 rect.bottom = 600.f;
912 rect.right = 400.f;
913 rect.top = 800.f;
914 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
915
916 // Add a solid-color translucent image object to the new annotation.
917 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
918 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
919 0xeeeecccc);
920 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
921 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
922 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
923 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
924 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
925 kBitmapSize, 0, 0));
926 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
927 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
928 }
Jane Liu36567742017-07-06 11:13:35 -0400929
930 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000931 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000932 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000933 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
934 }
Jane Liu36567742017-07-06 11:13:35 -0400935
Lei Zhanga21d5932018-02-05 18:28:38 +0000936 {
937 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000938 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000939 ASSERT_TRUE(annot);
940 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
941 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
942 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400943
Lei Zhanga21d5932018-02-05 18:28:38 +0000944 // Modify the image in the new annotation.
945 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
946 0xff000000);
947 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
948 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
949 }
Jane Liu36567742017-07-06 11:13:35 -0400950
951 // Save the document, closing the page and document.
952 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000953 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400954 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400955
956 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400957 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400958}
959
960TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400961#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400962 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
963 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
964 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000965#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
966 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
967 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
968 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400969#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000970 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
971 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
972 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400973#endif
974
975 // Open a file with two annotations and load its first page.
976 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000977 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400978 ASSERT_TRUE(page);
979 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
980
981 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000982 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000983 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000984 CompareBitmap(bitmap.get(), 595, 842, md5_original);
985 }
Jane Liu36567742017-07-06 11:13:35 -0400986
Lei Zhanga21d5932018-02-05 18:28:38 +0000987 {
988 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000989 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000990 ASSERT_TRUE(annot);
991 FS_RECTF rect;
992 rect.left = 200.f;
993 rect.bottom = 550.f;
994 rect.right = 450.f;
995 rect.top = 650.f;
996 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400997
Lei Zhanga21d5932018-02-05 18:28:38 +0000998 // Add a translucent text object to the new annotation.
999 FPDF_PAGEOBJECT text_object =
1000 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1001 EXPECT_TRUE(text_object);
1002 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1003 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1004 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1005 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1006 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1007 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1008 }
Jane Liu36567742017-07-06 11:13:35 -04001009
1010 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001011 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001012 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001013 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1014 }
Jane Liu36567742017-07-06 11:13:35 -04001015
Lei Zhanga21d5932018-02-05 18:28:38 +00001016 {
1017 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001018 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001019 ASSERT_TRUE(annot);
1020 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1021 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1022 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001023
Lei Zhanga21d5932018-02-05 18:28:38 +00001024 // Modify the text in the new annotation.
1025 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1026 GetFPDFWideString(L"New text!");
1027 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1028 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1029 }
Jane Liu36567742017-07-06 11:13:35 -04001030
1031 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001032 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001033 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001034 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1035 }
Jane Liu36567742017-07-06 11:13:35 -04001036
1037 // Remove the new annotation, and check that the page renders as before.
1038 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001039 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001040 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001041 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1042 }
Jane Liu36567742017-07-06 11:13:35 -04001043
1044 UnloadPage(page);
1045}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001046
1047TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
1048 // Open a file with four annotations and load its first page.
1049 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001050 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001051 ASSERT_TRUE(page);
1052
Lei Zhangdf064df2017-08-31 02:33:27 -07001053 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001054 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001055
Lei Zhanga21d5932018-02-05 18:28:38 +00001056 {
1057 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001058 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001059 ASSERT_TRUE(annot);
1060
1061 // Check that a non-existent key does not exist.
1062 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1063
1064 // Check that the string value of a non-string dictionary entry is empty.
1065 static constexpr char kApKey[] = "AP";
1066 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1067 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1068 FPDFAnnot_GetValueType(annot.get(), kApKey));
1069 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1070
1071 // Check that the string value of the hash is correct.
1072 static constexpr char kHashKey[] = "AAPL:Hash";
1073 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1074 unsigned long len =
1075 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1076 std::vector<char> buf(len);
1077 EXPECT_EQ(66u,
1078 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1079 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1080 BufferToWString(buf).c_str());
1081
1082 // Check that the string value of the modified date is correct.
1083 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1084 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1085 buf.clear();
1086 buf.resize(len);
1087 EXPECT_EQ(44u,
1088 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1089 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1090
1091 // Update the date entry for the annotation.
1092 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1093 GetFPDFWideString(kNewDate);
1094 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1095 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001096
1097 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001098 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001099 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001100
Dan Sinclair698aed72017-09-26 16:24:49 -04001101#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001102 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001103#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1104 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001105#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001106 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001107#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001108
1109 // Open the saved annotation.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001110 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001111 page = LoadSavedPage(0);
1112 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001113 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001114 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001115
Lei Zhanga21d5932018-02-05 18:28:38 +00001116 // Check that the string value of the modified date is the newly-set value.
1117 EXPECT_EQ(FPDF_OBJECT_STRING,
1118 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1119 unsigned long len =
1120 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1121 std::vector<char> buf(len);
1122 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1123 buf.data(), len));
1124 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1125 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001126
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001127 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001128 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001129}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001130
Henrique Nakashima5970a472018-01-11 22:40:59 +00001131TEST_F(FPDFAnnotEmbeddertest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001132 // Open a file with four annotations and load its first page.
1133 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001134 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001135 ASSERT_TRUE(page);
1136
Lei Zhanga21d5932018-02-05 18:28:38 +00001137 {
1138 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001139 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001140 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001141
Lei Zhanga21d5932018-02-05 18:28:38 +00001142 // Check that the string value of an AP returns the expected length.
1143 unsigned long normal_len = FPDFAnnot_GetAP(
1144 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1145 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001146
Lei Zhanga21d5932018-02-05 18:28:38 +00001147 // Check that the string value of an AP is not returned if the buffer is too
1148 // small. The result buffer should be overwritten with an empty string.
1149 std::vector<char> buf(normal_len - 1);
1150 // Write L"z" in the buffer to verify it's not overwritten.
1151 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1152 EXPECT_EQ(73970u,
1153 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1154 buf.data(), buf.size()));
1155 std::string ap = BufferToString(buf);
1156 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001157
Lei Zhanga21d5932018-02-05 18:28:38 +00001158 // Check that the string value of an AP is returned through a buffer that is
1159 // the right size.
1160 buf.clear();
1161 buf.resize(normal_len);
1162 EXPECT_EQ(73970u,
1163 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1164 buf.data(), buf.size()));
1165 ap = BufferToString(buf);
1166 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1167 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001168
Lei Zhanga21d5932018-02-05 18:28:38 +00001169 // Check that the string value of an AP is returned through a buffer that is
1170 // larger than necessary.
1171 buf.clear();
1172 buf.resize(normal_len + 1);
1173 EXPECT_EQ(73970u,
1174 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1175 buf.data(), buf.size()));
1176 ap = BufferToString(buf);
1177 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1178 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001179
Lei Zhanga21d5932018-02-05 18:28:38 +00001180 // Check that getting an AP for a mode that does not have an AP returns an
1181 // empty string.
1182 unsigned long rollover_len = FPDFAnnot_GetAP(
1183 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1184 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001185
Lei Zhanga21d5932018-02-05 18:28:38 +00001186 buf.clear();
1187 buf.resize(1000);
1188 EXPECT_EQ(2u,
1189 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1190 buf.data(), buf.size()));
1191 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001192
Lei Zhanga21d5932018-02-05 18:28:38 +00001193 // Check that setting the AP for an invalid appearance mode fails.
1194 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1195 GetFPDFWideString(L"new test ap");
1196 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1197 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1198 apText.get()));
1199 EXPECT_FALSE(FPDFAnnot_SetAP(
1200 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001201
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 // Set the AP correctly now.
1203 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1204 apText.get()));
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 just set.
1207 rollover_len = FPDFAnnot_GetAP(
1208 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1209 EXPECT_EQ(24u, rollover_len);
1210 buf.clear();
1211 buf.resize(rollover_len);
1212 EXPECT_EQ(24u,
1213 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1214 buf.data(), buf.size()));
1215 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001216
Lei Zhanga21d5932018-02-05 18:28:38 +00001217 // Check that the Normal AP was not touched when the Rollover AP was set.
1218 buf.clear();
1219 buf.resize(normal_len);
1220 EXPECT_EQ(73970u,
1221 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1222 buf.data(), buf.size()));
1223 ap = BufferToString(buf);
1224 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1225 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1226 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001227
1228 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001229 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001230 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001231
Tom Sepezb9c3e272018-08-14 18:22:06 +00001232 OpenSavedDocument(nullptr);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001233 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001234 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001235 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001236
Lei Zhanga21d5932018-02-05 18:28:38 +00001237 // Check that the new annotation value is equal to the value we set before
1238 // saving.
1239 unsigned long rollover_len = FPDFAnnot_GetAP(
1240 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1241 EXPECT_EQ(24u, rollover_len);
1242 std::vector<char> buf(rollover_len);
1243 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1244 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1245 buf.data(), buf.size()));
1246 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1247 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001248
1249 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001250 CloseSavedPage(page);
1251 CloseSavedDocument();
1252}
1253
1254TEST_F(FPDFAnnotEmbeddertest, RemoveOptionalAP) {
1255 // Open a file with four annotations and load its first page.
1256 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001257 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001258 ASSERT_TRUE(page);
1259
Lei Zhanga21d5932018-02-05 18:28:38 +00001260 {
1261 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001262 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001263 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001264
Lei Zhanga21d5932018-02-05 18:28:38 +00001265 // Set Down AP. Normal AP is already set.
1266 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1267 GetFPDFWideString(L"new test ap");
1268 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1269 apText.get()));
1270 EXPECT_EQ(73970u,
1271 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1272 nullptr, 0));
1273 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1274 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001275
Lei Zhanga21d5932018-02-05 18:28:38 +00001276 // Check that setting the Down AP to null removes the Down entry but keeps
1277 // Normal intact.
1278 EXPECT_TRUE(
1279 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1280 EXPECT_EQ(73970u,
1281 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1282 nullptr, 0));
1283 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1284 nullptr, 0));
1285 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001286
Lei Zhang75c81712018-02-08 17:22:39 +00001287 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001288}
1289
1290TEST_F(FPDFAnnotEmbeddertest, RemoveRequiredAP) {
1291 // Open a file with four annotations and load its first page.
1292 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001293 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001294 ASSERT_TRUE(page);
1295
Lei Zhanga21d5932018-02-05 18:28:38 +00001296 {
1297 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001298 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001299 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001300
Lei Zhanga21d5932018-02-05 18:28:38 +00001301 // Set Down AP. Normal AP is already set.
1302 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1303 GetFPDFWideString(L"new test ap");
1304 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1305 apText.get()));
1306 EXPECT_EQ(73970u,
1307 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1308 nullptr, 0));
1309 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1310 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001311
Lei Zhanga21d5932018-02-05 18:28:38 +00001312 // Check that setting the Normal AP to null removes the whole AP dictionary.
1313 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1314 nullptr));
1315 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1316 nullptr, 0));
1317 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1318 nullptr, 0));
1319 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001320
Lei Zhang75c81712018-02-08 17:22:39 +00001321 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001322}
1323
Jane Liu300bb272017-08-21 14:37:53 -04001324TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) {
1325 // Open a file with annotations and load its first page.
1326 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001327 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001328 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001329 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001330
Lei Zhanga21d5932018-02-05 18:28:38 +00001331 {
1332 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001333 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001334 ASSERT_TRUE(annot);
1335 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1336 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1337 static constexpr char kPopupKey[] = "Popup";
1338 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1339 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1340 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001341
Lei Zhanga21d5932018-02-05 18:28:38 +00001342 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001343 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001344 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1345 ASSERT_TRUE(popup);
1346 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1347 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1348 FS_RECTF rect;
1349 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1350 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1351 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001352
Lei Zhanga21d5932018-02-05 18:28:38 +00001353 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1354 // since "IRT" is not a key in |annot|'s dictionary.
1355 static constexpr char kIRTKey[] = "IRT";
1356 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1357 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001358
Lei Zhanga21d5932018-02-05 18:28:38 +00001359 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1360 // would fail, since its parent is not an annotation.
1361 static constexpr char kPKey[] = "P";
1362 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1363 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1364 FPDFAnnot_GetValueType(annot.get(), kPKey));
1365 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1366 }
Jane Liu300bb272017-08-21 14:37:53 -04001367
Jane Liu300bb272017-08-21 14:37:53 -04001368 UnloadPage(page);
1369}
1370
Diana Gage7e0c05d2017-07-19 17:33:33 -07001371TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
1372 // Open file with form text fields.
1373 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001374 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001375 ASSERT_TRUE(page);
1376
Lei Zhanga21d5932018-02-05 18:28:38 +00001377 {
1378 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001379 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001380 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001381
Lei Zhanga21d5932018-02-05 18:28:38 +00001382 // Check that the flag values are as expected.
1383 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1384 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1385 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001386
Lei Zhanga21d5932018-02-05 18:28:38 +00001387 {
1388 // Retrieve the second annotation: read-only text field.
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_TRUE(flags & FPDF_FORMFLAG_READONLY);
1395 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001396
1397 UnloadPage(page);
1398}
1399
1400TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
1401 // Open file with form text fields.
1402 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001403 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001404 ASSERT_TRUE(page);
1405
Lei Zhanga21d5932018-02-05 18:28:38 +00001406 {
1407 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001408 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001409 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001410
Lei Zhanga21d5932018-02-05 18:28:38 +00001411 // Check that the flag values are as expected.
1412 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1413 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1414 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1415 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1416 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001417
Lei Zhanga21d5932018-02-05 18:28:38 +00001418 {
1419 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001420 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001421 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001422
Lei Zhanga21d5932018-02-05 18:28:38 +00001423 // Check that the flag values are as expected.
1424 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1425 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1426 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1427 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1428 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001429
Lei Zhanga21d5932018-02-05 18:28:38 +00001430 {
1431 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001432 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001433 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001434
Lei Zhanga21d5932018-02-05 18:28:38 +00001435 // Check that the flag values are as expected.
1436 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1437 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1438 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1439 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1440 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001441
1442 UnloadPage(page);
1443}
Diana Gage40870db2017-07-19 18:16:03 -07001444
1445TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
1446 // Open file with form text fields.
1447 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1448 FPDF_PAGE page = LoadPage(0);
1449 ASSERT_TRUE(page);
1450
1451 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001452 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1453
1454 {
1455 // Verify there is an annotation.
1456 ScopedFPDFAnnotation annot(
1457 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1458 EXPECT_TRUE(annot);
1459 }
1460
1461 // Try other bad inputs at a valid location.
1462 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1463 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1464 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001465
1466 UnloadPage(page);
1467}
1468
1469TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
1470 // Open file with form text fields.
1471 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1472 FPDF_PAGE page = LoadPage(0);
1473 ASSERT_TRUE(page);
1474
Lei Zhanga21d5932018-02-05 18:28:38 +00001475 {
1476 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001477 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001478 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1479 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001480
Lei Zhanga21d5932018-02-05 18:28:38 +00001481 // Check that interactive form annotation flag values are as expected.
1482 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1483 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1484 }
Diana Gage40870db2017-07-19 18:16:03 -07001485
Lei Zhanga21d5932018-02-05 18:28:38 +00001486 {
1487 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001488 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001489 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1490 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001491
Lei Zhanga21d5932018-02-05 18:28:38 +00001492 // Check that interactive form annotation flag values are as expected.
1493 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1494 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1495 }
Diana Gage40870db2017-07-19 18:16:03 -07001496
1497 UnloadPage(page);
1498}
1499
1500TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1501 // Open file with form comboboxes.
1502 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1503 FPDF_PAGE page = LoadPage(0);
1504 ASSERT_TRUE(page);
1505
Lei Zhanga21d5932018-02-05 18:28:38 +00001506 {
1507 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001508 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001509 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001510 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001511
Lei Zhanga21d5932018-02-05 18:28:38 +00001512 // Check that interactive form annotation flag values are as expected.
1513 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1514 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1515 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1516 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1517 }
Diana Gage40870db2017-07-19 18:16:03 -07001518
Lei Zhanga21d5932018-02-05 18:28:38 +00001519 {
1520 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001521 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001522 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001523 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001524
Lei Zhanga21d5932018-02-05 18:28:38 +00001525 // Check that interactive form annotation flag values are as expected.
1526 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1527 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1528 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1529 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1530 }
Diana Gage40870db2017-07-19 18:16:03 -07001531
Lei Zhanga21d5932018-02-05 18:28:38 +00001532 {
1533 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001534 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001535 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001536 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001537
Lei Zhanga21d5932018-02-05 18:28:38 +00001538 // Check that interactive form annotation flag values are as expected.
1539 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1540 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1541 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1542 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1543 }
Diana Gage40870db2017-07-19 18:16:03 -07001544
1545 UnloadPage(page);
1546}