blob: 32edd7a5039023c1172e1c4f260db2014b9a0e65 [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
Jane Liu20eafda2017-06-07 10:33:24 -04005#include <memory>
6#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04007#include <vector>
8
Jane Liubaa7ff42017-06-29 19:18:23 -04009#include "core/fxcrt/fx_system.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040010#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040011#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040012#include "public/fpdfview.h"
13#include "testing/embedder_test.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
Nicolas Pena3ff54002017-07-05 11:55:35 -040016class FPDFAnnotEmbeddertest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040017
Jane Liue17011d2017-06-21 12:18:37 -040018TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
19 // Open a file with one annotation and load its first page.
20 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
21 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
22 ASSERT_TRUE(page);
23
24 // This annotation has a malformed appearance stream, which does not have its
25 // normal appearance defined, only its rollover appearance. In this case, its
26 // normal appearance should be generated, allowing the highlight annotation to
27 // still display.
Nicolas Pena3ff54002017-07-05 11:55:35 -040028 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle(), FPDF_ANNOT);
Jane Liue17011d2017-06-21 12:18:37 -040029 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
30 FPDFBitmap_Destroy(bitmap);
31
32 UnloadPage(page);
33}
34
Jane Liu4fd9a472017-06-01 18:56:09 -040035TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
36 // Open a file with one annotation and load its first page.
37 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
38 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
39 ASSERT_TRUE(page);
40
41 // Check that there is a total of 1 annotation on its first page.
42 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
43
44 // Check that the annotation is of type "highlight".
Jane Liud60e9ad2017-06-26 11:28:36 -040045 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
46 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -040047 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
48
49 // Check that the annotation color is yellow.
50 unsigned int R;
51 unsigned int G;
52 unsigned int B;
53 unsigned int A;
54 EXPECT_TRUE(
55 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
56 EXPECT_EQ(255u, R);
57 EXPECT_EQ(255u, G);
58 EXPECT_EQ(0u, B);
59 EXPECT_EQ(255u, A);
60
61 // Check that the author is correct.
Jane Liu2e1a32b2017-07-06 12:01:25 -040062 std::unique_ptr<unsigned short, pdfium::FreeDeleter> author_key =
63 GetFPDFWideString(L"T");
64 EXPECT_EQ(FPDF_OBJECT_STRING,
65 FPDFAnnot_GetValueType(annot, author_key.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -040066 unsigned long len =
Jane Liu2e1a32b2017-07-06 12:01:25 -040067 FPDFAnnot_GetStringValue(annot, author_key.get(), nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -040068 std::vector<char> buf(len);
Jane Liu2e1a32b2017-07-06 12:01:25 -040069 EXPECT_EQ(28u,
70 FPDFAnnot_GetStringValue(annot, author_key.get(), buf.data(), len));
Jane Liu4fd9a472017-06-01 18:56:09 -040071 EXPECT_STREQ(L"Jae Hyun Park",
72 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
73 .c_str());
74
75 // Check that the content is correct.
Jane Liu2e1a32b2017-07-06 12:01:25 -040076 std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key =
77 GetFPDFWideString(L"Contents");
78 EXPECT_EQ(FPDF_OBJECT_STRING,
79 FPDFAnnot_GetValueType(annot, contents_key.get()));
80 len = FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0);
Jane Liu4fd9a472017-06-01 18:56:09 -040081 buf.clear();
82 buf.resize(len);
Jane Liu2e1a32b2017-07-06 12:01:25 -040083 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot, contents_key.get(),
84 buf.data(), len));
Jane Liu4fd9a472017-06-01 18:56:09 -040085 const wchar_t contents[] =
86 L"This is a note for that highlight annotation. Very long highlight "
87 "annotation. Long long long Long long longLong long longLong long "
88 "longLong long longLong long longLong long longLong long longLong long "
89 "longLong long longLong long longLong long longLong long longLong long "
90 "longLong long longLong long longLong long longLong long longLong long "
91 "longLong long longLong long longLong long longLong long longLong long "
92 "longLong long longLong long longLong long longLong long longLong long "
93 "longLong long longLong long longLong long longLong long longLong long "
94 "longLong long longLong long longLong long longLong long longLong long "
95 "longLong long longLong long longLong long longLong long longLong long "
96 "longLong long longLong long longLong long longLong long longLong long "
97 "longLong long longLong long longLong long longLong long longLong long "
98 "longLong long longLong long longLong long longLong long longLong long "
99 "longLong long longLong long longLong long longLong long longLong long "
100 "longLong long longLong long longLong long longLong long longLong long "
101 "longLong long longLong long longLong long longLong long longLong long "
102 "longLong long longLong long longLong long longLong long longLong long "
103 "longLong long longLong long longLong long longLong long longLong long "
104 "longLong long longLong long longLong long longLong long longLong long "
105 "longLong long long. END";
106 EXPECT_STREQ(contents,
107 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
108 .c_str());
109
110 // Check that the quadpoints are correct.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400111 FS_QUADPOINTSF quadpoints;
112 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liu4fd9a472017-06-01 18:56:09 -0400113 EXPECT_EQ(115.802643f, quadpoints.x1);
114 EXPECT_EQ(718.913940f, quadpoints.y1);
115 EXPECT_EQ(157.211182f, quadpoints.x4);
116 EXPECT_EQ(706.264465f, quadpoints.y4);
117
Jane Liue10509a2017-06-20 16:47:41 -0400118 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400119 UnloadPage(page);
120}
121
122TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
123 // Open a file with three annotations and load its first page.
124 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
125 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
126 ASSERT_TRUE(page);
127
128 // Check that there is a total of 3 annotation on its first page.
129 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
130
Jane Liu20eafda2017-06-07 10:33:24 -0400131 // Check that the third annotation is of type "ink".
Jane Liud60e9ad2017-06-26 11:28:36 -0400132 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 2);
133 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400134 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
135
136 // Check that the annotation color is blue with opacity.
137 unsigned int R;
138 unsigned int G;
139 unsigned int B;
140 unsigned int A;
141 EXPECT_TRUE(
142 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
143 EXPECT_EQ(0u, R);
144 EXPECT_EQ(0u, G);
145 EXPECT_EQ(255u, B);
146 EXPECT_EQ(76u, A);
147
148 // Check that there is no content.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400149 std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key =
150 GetFPDFWideString(L"Contents");
Jane Liu4fd9a472017-06-01 18:56:09 -0400151 EXPECT_EQ(2u,
Jane Liu2e1a32b2017-07-06 12:01:25 -0400152 FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400153
154 // Check that the rectange coordinates are correct.
155 // Note that upon rendering, the rectangle coordinates will be adjusted.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400156 FS_RECTF rect;
157 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu4fd9a472017-06-01 18:56:09 -0400158 EXPECT_EQ(351.820404f, rect.left);
159 EXPECT_EQ(583.830688f, rect.bottom);
160 EXPECT_EQ(475.336090f, rect.right);
161 EXPECT_EQ(681.535034f, rect.top);
162
Jane Liue10509a2017-06-20 16:47:41 -0400163 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400164 UnloadPage(page);
165}
Jane Liu20eafda2017-06-07 10:33:24 -0400166
167TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
168 // Open a file with one annotation and load its first page.
169 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
170 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
171 ASSERT_TRUE(page);
172
173 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400174 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400175
176 UnloadPage(page);
177}
178
Jane Liud321ef92017-06-14 09:56:22 -0400179TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400180 // Open a file with no annotation and load its first page.
181 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
182 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
183 ASSERT_TRUE(page);
184 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
185
Jane Liueda65252017-06-07 11:31:27 -0400186 // Add a text annotation to the page.
Jane Liud60e9ad2017-06-26 11:28:36 -0400187 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT);
188 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400189
190 // Check that there is now 1 annotations on this page.
191 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
192
193 // Check that the subtype of the annotation is correct.
194 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
Jane Liue10509a2017-06-20 16:47:41 -0400195 FPDFPage_CloseAnnot(annot);
196
Jane Liud60e9ad2017-06-26 11:28:36 -0400197 annot = FPDFPage_GetAnnot(page, 0);
198 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400199 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
200
201 // Set the color of the annotation.
202 ASSERT_TRUE(
203 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
204 // Check that the color has been set correctly.
205 unsigned int R;
206 unsigned int G;
207 unsigned int B;
208 unsigned int A;
209 EXPECT_TRUE(
210 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
211 EXPECT_EQ(51u, R);
212 EXPECT_EQ(102u, G);
213 EXPECT_EQ(153u, B);
214 EXPECT_EQ(204u, A);
215
216 // Change the color of the annotation.
217 ASSERT_TRUE(
218 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51));
219 // Check that the color has been set correctly.
220 EXPECT_TRUE(
221 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
222 EXPECT_EQ(204u, R);
223 EXPECT_EQ(153u, G);
224 EXPECT_EQ(102u, B);
225 EXPECT_EQ(51u, A);
226
227 // Set the annotation rectangle.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400228 FS_RECTF rect;
229 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liud60e9ad2017-06-26 11:28:36 -0400230 EXPECT_EQ(0.f, rect.left);
231 EXPECT_EQ(0.f, rect.right);
Jane Liu20eafda2017-06-07 10:33:24 -0400232 rect.left = 35;
233 rect.bottom = 150;
234 rect.right = 53;
235 rect.top = 165;
Jane Liu06462752017-06-27 16:41:14 -0400236 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu20eafda2017-06-07 10:33:24 -0400237 // Check that the annotation rectangle has been set correctly.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400238 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu20eafda2017-06-07 10:33:24 -0400239 EXPECT_EQ(35.f, rect.left);
240 EXPECT_EQ(150.f, rect.bottom);
241 EXPECT_EQ(53.f, rect.right);
242 EXPECT_EQ(165.f, rect.top);
243
244 // Set the content of the annotation.
Jane Liu2e1a32b2017-07-06 12:01:25 -0400245 std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key =
246 GetFPDFWideString(L"Contents");
Jane Liu20eafda2017-06-07 10:33:24 -0400247 const wchar_t contents[] = L"Hello! This is a customized content.";
248 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
249 GetFPDFWideString(contents);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400250 ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, contents_key.get(), text.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400251 // Check that the content has been set correctly.
252 unsigned long len =
Jane Liu2e1a32b2017-07-06 12:01:25 -0400253 FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0);
Jane Liu20eafda2017-06-07 10:33:24 -0400254 std::vector<char> buf(len);
Jane Liu2e1a32b2017-07-06 12:01:25 -0400255 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot, contents_key.get(), buf.data(),
256 len));
Jane Liu20eafda2017-06-07 10:33:24 -0400257 EXPECT_STREQ(contents,
258 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
259 .c_str());
260
Jane Liue10509a2017-06-20 16:47:41 -0400261 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400262 UnloadPage(page);
263}
264
265TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
266 // Open a file with one annotation and load its first page.
267 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
268 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
269 ASSERT_TRUE(page);
270
271 // Check that there is a total of one annotation on its first page, and verify
272 // its quadpoints.
273 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liud60e9ad2017-06-26 11:28:36 -0400274 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
275 ASSERT_TRUE(annot);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400276 FS_QUADPOINTSF quadpoints;
277 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liu20eafda2017-06-07 10:33:24 -0400278 EXPECT_EQ(115.802643f, quadpoints.x1);
279 EXPECT_EQ(718.913940f, quadpoints.y1);
280 EXPECT_EQ(157.211182f, quadpoints.x4);
281 EXPECT_EQ(706.264465f, quadpoints.y4);
Jane Liue10509a2017-06-20 16:47:41 -0400282 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400283
284 // Add an underline annotation to the page and set its quadpoints.
Jane Liud60e9ad2017-06-26 11:28:36 -0400285 annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
286 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400287 quadpoints.x1 = 140.802643f;
288 quadpoints.x3 = 140.802643f;
Jane Liu06462752017-06-27 16:41:14 -0400289 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liue10509a2017-06-20 16:47:41 -0400290 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400291
292 // Save the document, closing the page and document.
293 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
294 FPDF_ClosePage(page);
295
296 // Open the saved document.
Nicolas Pena3ff54002017-07-05 11:55:35 -0400297 const char md5[] = "184b67b322edaee27994b3232544b8b3";
298 TestSaved(612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400299
300 // Check that the saved document has 2 annotations on the first page
Nicolas Pena3ff54002017-07-05 11:55:35 -0400301 EXPECT_EQ(2, FPDFPage_GetAnnotCount(m_SavedPage));
Jane Liu20eafda2017-06-07 10:33:24 -0400302
303 // Check that the second annotation is an underline annotation and verify
304 // its quadpoints.
Nicolas Pena3ff54002017-07-05 11:55:35 -0400305 FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 1);
Jane Liud60e9ad2017-06-26 11:28:36 -0400306 ASSERT_TRUE(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400307 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400308 FS_QUADPOINTSF new_quadpoints;
309 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(new_annot, &new_quadpoints));
Jane Liu20eafda2017-06-07 10:33:24 -0400310 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
311 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
312 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
313 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
314
Jane Liue10509a2017-06-20 16:47:41 -0400315 FPDFPage_CloseAnnot(new_annot);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400316 CloseSaved();
Jane Liu20eafda2017-06-07 10:33:24 -0400317}
Jane Liu06462752017-06-27 16:41:14 -0400318
319TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
320 // Open a file with four annotations and load its first page.
321 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
322 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
323 ASSERT_TRUE(page);
324 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
325
326 // Retrieve the highlight annotation which has its AP stream already defined.
327 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
328 ASSERT_TRUE(annot);
329 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
330
331 // Check that color cannot be set when an AP stream is defined already.
332 EXPECT_FALSE(
333 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
334
335 // Check that when getting the attachment points, bounding box points are
336 // returned since this is a markup annotation with AP defined.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400337 FS_QUADPOINTSF quadpoints;
338 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &quadpoints));
Jane Liu06462752017-06-27 16:41:14 -0400339 EXPECT_NEAR(0.f, quadpoints.x1, 0.001f);
340 EXPECT_NEAR(16.9955f, quadpoints.y1, 0.001f);
341 EXPECT_NEAR(68.5953f, quadpoints.x4, 0.001f);
342 EXPECT_NEAR(0.f, quadpoints.y4, 0.001f);
343
344 // Check that when new attachment points define a smaller bounding box, the
345 // bounding box does not get updated.
346 quadpoints.x1 = 1.0f;
347 quadpoints.x3 = 1.0f;
348 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400349 FS_QUADPOINTSF new_quadpoints;
350 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints));
Jane Liu06462752017-06-27 16:41:14 -0400351 EXPECT_NE(quadpoints.x1, new_quadpoints.x1);
352
353 // Check that the bounding box gets updated successfully when valid attachment
354 // points are set.
355 quadpoints.x1 = 0.f;
356 quadpoints.y1 = 721.792f;
357 quadpoints.x2 = 133.055f;
358 quadpoints.y2 = 721.792f;
359 quadpoints.x3 = 0.f;
360 quadpoints.x4 = 133.055f;
361 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400362 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints));
Jane Liu06462752017-06-27 16:41:14 -0400363 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
364 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
365 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
366 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
367
368 // Check that when getting the annotation rectangle, rectangle points are
369 // returned, but not bounding box points.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400370 FS_RECTF rect;
371 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu06462752017-06-27 16:41:14 -0400372 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
373 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
374 EXPECT_NEAR(136.325f, rect.right, 0.001f);
375 EXPECT_NEAR(721.292f, rect.top, 0.001f);
376
377 // Check that the rectangle gets updated successfully when a valid rectangle
378 // is set, and that the bounding box is not modified.
379 rect.left = 0.f;
380 rect.bottom = 0.f;
381 rect.right = 134.055f;
382 rect.top = 722.792f;
383 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400384 FS_RECTF new_rect;
385 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liu06462752017-06-27 16:41:14 -0400386 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400387 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, &new_quadpoints));
Jane Liu06462752017-06-27 16:41:14 -0400388 EXPECT_NE(rect.right, new_quadpoints.x2);
389
390 FPDFPage_CloseAnnot(annot);
391
392 // Retrieve the square annotation which has its AP stream already defined.
393 annot = FPDFPage_GetAnnot(page, 2);
394 ASSERT_TRUE(annot);
395 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot));
396
397 // Check that the rectangle and the bounding box get updated successfully when
398 // a valid rectangle is set, since this is not a markup annotation.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400399 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu06462752017-06-27 16:41:14 -0400400 rect.right += 1.f;
401 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400402 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liu06462752017-06-27 16:41:14 -0400403 EXPECT_EQ(rect.right, new_rect.right);
404
405 FPDFPage_CloseAnnot(annot);
406 UnloadPage(page);
407}
Jane Liu8ce58f52017-06-29 13:40:22 -0400408
409TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) {
410 // Open a file with 3 annotations on its first page.
411 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
412 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
413 ASSERT_TRUE(page);
414 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
415
416 // Check that the annotations have the expected rectangle coordinates.
417 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400418 FS_RECTF rect;
419 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400420 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
421 FPDFPage_CloseAnnot(annot);
422
423 annot = FPDFPage_GetAnnot(page, 1);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400424 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400425 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
426 FPDFPage_CloseAnnot(annot);
427
428 annot = FPDFPage_GetAnnot(page, 2);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400429 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400430 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
431 FPDFPage_CloseAnnot(annot);
432
433 // Check that nothing happens when attempting to remove an annotation with an
434 // out-of-bound index.
435 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
436 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
437 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
438
439 // Remove the second annotation.
440 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
441 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
442 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
443
444 // Save the document, closing the page and document.
445 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
446 FPDF_ClosePage(page);
447
Nicolas Pena3ff54002017-07-05 11:55:35 -0400448 // TODO(npm): TestSaved changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400449 // Open the saved document.
450 std::string new_file = GetString();
451 FPDF_FILEACCESS file_access;
452 memset(&file_access, 0, sizeof(file_access));
453 file_access.m_FileLen = new_file.size();
454 file_access.m_GetBlock = GetBlockFromString;
455 file_access.m_Param = &new_file;
456 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
457 ASSERT_TRUE(new_doc);
458 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
459 ASSERT_TRUE(new_page);
460
461 // Check that the saved document has 2 annotations on the first page.
462 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
463
464 // Check that the remaining 2 annotations are the original 1st and 3rd ones by
465 // verifying their rectangle coordinates.
466 annot = FPDFPage_GetAnnot(new_page, 0);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400467 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400468 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
469 FPDFPage_CloseAnnot(annot);
470
471 annot = FPDFPage_GetAnnot(new_page, 1);
Jane Liu0c6b07d2017-08-15 10:50:22 -0400472 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &rect));
Jane Liu8ce58f52017-06-29 13:40:22 -0400473 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
474 FPDFPage_CloseAnnot(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400475 FPDF_ClosePage(new_page);
476 FPDF_CloseDocument(new_doc);
477}
Jane Liu8ce58f52017-06-29 13:40:22 -0400478
Jane Liubaa7ff42017-06-29 19:18:23 -0400479TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) {
480#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400481 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
482 const char md5_modified_path[] = "cf3cea74bd46497520ff6c4d1ea228c8";
483 const char md5_two_paths[] = "e8994452fc4385337bae5522354e10ff";
484 const char md5_new_annot[] = "ee5372b31fede117fc83b9384598aa25";
Jane Liubaa7ff42017-06-29 19:18:23 -0400485#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400486 const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb";
487 const char md5_modified_path[] = "681f0d0738dded0722e146f6c219bfac";
488 const char md5_two_paths[] = "67c7e90fc3b64e20f6b69a1744f7f4f0";
489 const char md5_new_annot[] = "262187984451bae2fe826067d68623ff";
Jane Liubaa7ff42017-06-29 19:18:23 -0400490#else
Jane Liu7a9a38b2017-07-11 13:47:37 -0400491 const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43";
492 const char md5_modified_path[] = "87a78cbacd8509b961a67be56b5665a2";
493 const char md5_two_paths[] = "76e985c18b73ceacf409f77f978176d4";
494 const char md5_new_annot[] = "c95de7a9a1f61faca03d953961a319b9";
Jane Liubaa7ff42017-06-29 19:18:23 -0400495#endif
496
497 // Open a file with two annotations and load its first page.
498 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
499 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
500 ASSERT_TRUE(page);
501 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
502
503 // Check that the page renders correctly.
504 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400505 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liubaa7ff42017-06-29 19:18:23 -0400506 FPDFBitmap_Destroy(bitmap);
507
508 // Retrieve the stamp annotation which has its AP stream already defined.
509 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
510 ASSERT_TRUE(annot);
511
512 // Check that this annotation has one path object and retrieve it.
Jane Liu36567742017-07-06 11:13:35 -0400513 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
514 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot, 1);
Jane Liubaa7ff42017-06-29 19:18:23 -0400515 EXPECT_FALSE(path);
Jane Liu36567742017-07-06 11:13:35 -0400516 path = FPDFAnnot_GetObject(annot, 0);
517 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400518 EXPECT_TRUE(path);
519
520 // Modify the color of the path object.
521 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
Jane Liu36567742017-07-06 11:13:35 -0400522 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400523
524 // Check that the page with the modified annotation renders correctly.
525 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400526 CompareBitmap(bitmap, 595, 842, md5_modified_path);
527 FPDFBitmap_Destroy(bitmap);
528
529 // Add a second path object to the same annotation.
530 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
531 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
532 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
533 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
534 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
535 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, dot));
536 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot));
537
538 // Check that the page with an annotation with two paths renders correctly.
539 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
540 CompareBitmap(bitmap, 595, 842, md5_two_paths);
541 FPDFBitmap_Destroy(bitmap);
542
543 // Delete the newly added path object.
544 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot, 1));
545 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
546 FPDFPage_CloseAnnot(annot);
547
548 // Check that the page renders the same as before.
549 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
550 CompareBitmap(bitmap, 595, 842, md5_modified_path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400551 FPDFBitmap_Destroy(bitmap);
552
553 // Create another stamp annotation and set its annotation rectangle.
554 annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
555 ASSERT_TRUE(annot);
556 FS_RECTF rect;
557 rect.left = 200.f;
558 rect.bottom = 400.f;
559 rect.right = 500.f;
560 rect.top = 600.f;
561 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
562
563 // Add a new path to the annotation.
564 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
565 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
566 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
567 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
568 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
569 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
570 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
571 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
Jane Liu36567742017-07-06 11:13:35 -0400572 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, check));
573 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
Jane Liubaa7ff42017-06-29 19:18:23 -0400574
575 // Check that the annotation's bounding box came from its rectangle.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400576 FS_RECTF new_rect;
577 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400578 EXPECT_EQ(rect.left, new_rect.left);
579 EXPECT_EQ(rect.bottom, new_rect.bottom);
580 EXPECT_EQ(rect.right, new_rect.right);
581 EXPECT_EQ(rect.top, new_rect.top);
582
583 // Save the document, closing the page and document.
584 FPDFPage_CloseAnnot(annot);
585 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
586 FPDF_ClosePage(page);
587
588 // Open the saved document.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400589 TestSaved(595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400590
Jane Liu36567742017-07-06 11:13:35 -0400591 // Check that the document has a correct count of annotations and objects.
Nicolas Pena3ff54002017-07-05 11:55:35 -0400592 EXPECT_EQ(3, FPDFPage_GetAnnotCount(m_SavedPage));
593 annot = FPDFPage_GetAnnot(m_SavedPage, 2);
Jane Liubaa7ff42017-06-29 19:18:23 -0400594 ASSERT_TRUE(annot);
Jane Liu36567742017-07-06 11:13:35 -0400595 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
Jane Liubaa7ff42017-06-29 19:18:23 -0400596
597 // Check that the new annotation's rectangle is as defined.
Jane Liu0c6b07d2017-08-15 10:50:22 -0400598 ASSERT_TRUE(FPDFAnnot_GetRect(annot, &new_rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400599 EXPECT_EQ(rect.left, new_rect.left);
600 EXPECT_EQ(rect.bottom, new_rect.bottom);
601 EXPECT_EQ(rect.right, new_rect.right);
602 EXPECT_EQ(rect.top, new_rect.top);
603
Jane Liubaa7ff42017-06-29 19:18:23 -0400604 FPDFPage_CloseAnnot(annot);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400605 CloseSaved();
Jane Liu8ce58f52017-06-29 13:40:22 -0400606}
Jane Liub137e752017-07-05 15:04:33 -0400607
608TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) {
609 // Open a file with an annotation and load its first page.
610 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
611 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
612 ASSERT_TRUE(page);
613
614 // Check that the page renders correctly.
615 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
616 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
617 FPDFBitmap_Destroy(bitmap);
618
619 // Retrieve the annotation.
620 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
621 ASSERT_TRUE(annot);
622
623 // Check that the original flag values are as expected.
624 int flags = FPDFAnnot_GetFlags(annot);
625 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
626 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
627
628 // Set the HIDDEN flag.
629 flags |= FPDF_ANNOT_FLAG_HIDDEN;
630 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags));
631 flags = FPDFAnnot_GetFlags(annot);
632 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
633 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
634
635 // Check that the page renders correctly without rendering the annotation.
636 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
637 CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
638 FPDFBitmap_Destroy(bitmap);
639
640 // Unset the HIDDEN flag.
641 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, FPDF_ANNOT_FLAG_NONE));
642 EXPECT_FALSE(FPDFAnnot_GetFlags(annot));
643 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
644 EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags));
645 flags = FPDFAnnot_GetFlags(annot);
646 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
647 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
648
649 // Check that the page renders correctly as before.
650 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
651 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
652 FPDFBitmap_Destroy(bitmap);
653
654 FPDFPage_CloseAnnot(annot);
655 UnloadPage(page);
656}
Jane Liu36567742017-07-06 11:13:35 -0400657
658TEST_F(FPDFAnnotEmbeddertest, AddAndModifyImage) {
659#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400660 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
661 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
662 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Jane Liu36567742017-07-06 11:13:35 -0400663#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400664 const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb";
665 const char md5_new_image[] = "6fb176c20996cc554d0210d8c8b6138f";
666 const char md5_modified_image[] = "546959714dfb0dcd7e7b00259e8d178c";
Jane Liu36567742017-07-06 11:13:35 -0400667#else
Jane Liu7a9a38b2017-07-11 13:47:37 -0400668 const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43";
669 const char md5_new_image[] = "e7658232abd8977cdc3367dd02aee04a";
670 const char md5_modified_image[] = "f393432b9a9b452ea69022f46c8b3f75";
Jane Liu36567742017-07-06 11:13:35 -0400671#endif
672
673 // Open a file with two annotations and load its first page.
674 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
675 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
676 ASSERT_TRUE(page);
677 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
678
679 // Check that the page renders correctly.
680 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400681 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400682 FPDFBitmap_Destroy(bitmap);
683
684 // Create a stamp annotation and set its annotation rectangle.
685 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
686 ASSERT_TRUE(annot);
687 FS_RECTF rect;
688 rect.left = 200.f;
689 rect.bottom = 600.f;
690 rect.right = 400.f;
691 rect.top = 800.f;
692 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
693
694 // Add a solid-color translucent image object to the new annotation.
695 constexpr int kBitmapSize = 200;
696 FPDF_BITMAP image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
697 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xeeeecccc);
698 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
699 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
700 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
701 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
702 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
703 kBitmapSize, 0, 0));
704 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
705 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, image_object));
706 FPDFPage_CloseAnnot(annot);
707
708 // Check that the page renders correctly with the new image object.
709 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400710 CompareBitmap(bitmap, 595, 842, md5_new_image);
Jane Liu36567742017-07-06 11:13:35 -0400711 FPDFBitmap_Destroy(bitmap);
712
713 // Retrieve the newly added stamp annotation and its image object.
714 annot = FPDFPage_GetAnnot(page, 2);
715 ASSERT_TRUE(annot);
716 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
717 image_object = FPDFAnnot_GetObject(annot, 0);
718 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
719
720 // Modify the image in the new annotation.
721 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize, 0xff000000);
722 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
723 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, image_object));
724 FPDFPage_CloseAnnot(annot);
725
726 // Save the document, closing the page and document.
727 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
728 FPDF_ClosePage(page);
729
730 // Test that the saved document renders the modified image object correctly.
Jane Liu7a9a38b2017-07-11 13:47:37 -0400731 TestSaved(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400732
733 FPDFBitmap_Destroy(image_bitmap);
734 CloseSaved();
735}
736
737TEST_F(FPDFAnnotEmbeddertest, AddAndModifyText) {
738#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400739 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
740 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
741 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Jane Liu36567742017-07-06 11:13:35 -0400742#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400743 const char md5_original[] = "4f64add0190ede63f7bb9eb1e2e83edb";
744 const char md5_new_text[] = "998abae4962f8f41e094e7612d8339fc";
745 const char md5_modified_text[] = "e89b82ca4589b8f0b45fff42ca3a96a4";
Jane Liu36567742017-07-06 11:13:35 -0400746#else
Jane Liu7a9a38b2017-07-11 13:47:37 -0400747 const char md5_original[] = "02e1c6adff8fee4aeabd91c2c2e4be43";
748 const char md5_new_text[] = "3fbbaec4d846ccf2be89e09daae0273d";
749 const char md5_modified_text[] = "2ad0acaf2d8990bcdf48e1d12e6c44ad";
Jane Liu36567742017-07-06 11:13:35 -0400750#endif
751
752 // Open a file with two annotations and load its first page.
753 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
754 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
755 ASSERT_TRUE(page);
756 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
757
758 // Check that the page renders correctly.
759 FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400760 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400761 FPDFBitmap_Destroy(bitmap);
762
763 // Create a stamp annotation and set its annotation rectangle.
764 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP);
765 ASSERT_TRUE(annot);
766 FS_RECTF rect;
767 rect.left = 200.f;
768 rect.bottom = 550.f;
769 rect.right = 450.f;
770 rect.top = 650.f;
771 EXPECT_TRUE(FPDFAnnot_SetRect(annot, &rect));
772
773 // Add a translucent text object to the new annotation.
774 FPDF_PAGEOBJECT text_object =
775 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
776 EXPECT_TRUE(text_object);
777 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
778 GetFPDFWideString(L"I'm a translucent text laying on other text.");
779 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
780 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
781 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
782 EXPECT_TRUE(FPDFAnnot_AppendObject(annot, text_object));
783 FPDFPage_CloseAnnot(annot);
784
785 // Check that the page renders correctly with the new text object.
786 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400787 CompareBitmap(bitmap, 595, 842, md5_new_text);
Jane Liu36567742017-07-06 11:13:35 -0400788 FPDFBitmap_Destroy(bitmap);
789
790 // Retrieve the newly added stamp annotation and its text object.
791 annot = FPDFPage_GetAnnot(page, 2);
792 ASSERT_TRUE(annot);
793 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot));
794 text_object = FPDFAnnot_GetObject(annot, 0);
795 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
796
797 // Modify the text in the new annotation.
798 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
799 GetFPDFWideString(L"New text!");
800 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
801 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot, text_object));
802 FPDFPage_CloseAnnot(annot);
803
804 // Check that the page renders correctly with the modified text object.
805 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400806 CompareBitmap(bitmap, 595, 842, md5_modified_text);
Jane Liu36567742017-07-06 11:13:35 -0400807 FPDFBitmap_Destroy(bitmap);
808
809 // Remove the new annotation, and check that the page renders as before.
810 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
811 bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT);
Jane Liu7a9a38b2017-07-11 13:47:37 -0400812 CompareBitmap(bitmap, 595, 842, md5_original);
Jane Liu36567742017-07-06 11:13:35 -0400813 FPDFBitmap_Destroy(bitmap);
814
815 UnloadPage(page);
816}
Jane Liu2e1a32b2017-07-06 12:01:25 -0400817
818TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
819 // Open a file with four annotations and load its first page.
820 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
821 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
822 ASSERT_TRUE(page);
823
824 // Retrieve the first annotation.
825 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
826 ASSERT_TRUE(annot);
827
828 // Check that a non-existent key does not exist.
829 EXPECT_FALSE(FPDFAnnot_HasKey(annot, GetFPDFWideString(L"none").get()));
830
831 // Check that the string value of a non-string dictionary entry is empty.
832 std::unique_ptr<unsigned short, pdfium::FreeDeleter> ap_key =
833 GetFPDFWideString(L"AP");
834 EXPECT_TRUE(FPDFAnnot_HasKey(annot, ap_key.get()));
835 EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, ap_key.get()));
836 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, ap_key.get(), nullptr, 0));
837
838 // Check that the string value of the hash is correct.
839 std::unique_ptr<unsigned short, pdfium::FreeDeleter> hash_key =
840 GetFPDFWideString(L"AAPL:Hash");
841 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get()));
842 unsigned long len =
843 FPDFAnnot_GetStringValue(annot, hash_key.get(), nullptr, 0);
844 std::vector<char> buf(len);
845 EXPECT_EQ(66u,
846 FPDFAnnot_GetStringValue(annot, hash_key.get(), buf.data(), len));
847 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
848 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
849 .c_str());
850
851 // Check that the string value of the modified date is correct.
852 std::unique_ptr<unsigned short, pdfium::FreeDeleter> date_key =
853 GetFPDFWideString(L"M");
854 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get()));
855 len = FPDFAnnot_GetStringValue(annot, date_key.get(), nullptr, 0);
856 buf.clear();
857 buf.resize(len);
858 EXPECT_EQ(44u,
859 FPDFAnnot_GetStringValue(annot, date_key.get(), buf.data(), len));
860 EXPECT_STREQ(L"D:201706071721Z00'00'",
861 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
862 .c_str());
863
864 // Update the date entry for the annotation.
865 const wchar_t new_date[] = L"D:201706282359Z00'00'";
866 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
867 GetFPDFWideString(new_date);
868 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, date_key.get(), text.get()));
869
870 // Save the document, closing the page and document.
871 FPDFPage_CloseAnnot(annot);
872 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
873 FPDF_ClosePage(page);
874
875 // Open the saved annotation.
876#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +0300877 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400878#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Artem Strygind24b97e2017-08-09 18:50:59 +0300879 const char md5[] = "0e3710ea6476f5bcba2cd39eb42d54e2";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400880#else
Artem Strygind24b97e2017-08-09 18:50:59 +0300881 const char md5[] = "831a3c465a56d2e0c89aef7bdf15306a";
Jane Liu2e1a32b2017-07-06 12:01:25 -0400882#endif
883 TestSaved(595, 842, md5);
884 FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 0);
885
886 // Check that the string value of the modified date is the newly-set value.
887 EXPECT_EQ(FPDF_OBJECT_STRING,
888 FPDFAnnot_GetValueType(new_annot, date_key.get()));
889 len = FPDFAnnot_GetStringValue(new_annot, date_key.get(), nullptr, 0);
890 buf.clear();
891 buf.resize(len);
892 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot, date_key.get(), buf.data(),
893 len));
894 EXPECT_STREQ(new_date,
895 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
896 .c_str());
897
898 FPDFPage_CloseAnnot(new_annot);
899 CloseSaved();
900}
Diana Gage7e0c05d2017-07-19 17:33:33 -0700901
902TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsTextField) {
903 // Open file with form text fields.
904 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
905 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
906 ASSERT_TRUE(page);
907
908 // Retrieve the first annotation: user-editable text field.
909 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
910 ASSERT_TRUE(annot);
911
912 // Check that the flag values are as expected.
913 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
914 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
915 FPDFPage_CloseAnnot(annot);
916
917 // Retrieve the second annotation: read-only text field.
918 annot = FPDFPage_GetAnnot(page, 1);
919 ASSERT_TRUE(annot);
920
921 // Check that the flag values are as expected.
922 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
923 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
924 FPDFPage_CloseAnnot(annot);
925
926 UnloadPage(page);
927}
928
929TEST_F(FPDFAnnotEmbeddertest, GetFormFieldFlagsComboBox) {
930 // Open file with form text fields.
931 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
932 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
933 ASSERT_TRUE(page);
934
935 // Retrieve the first annotation: user-editable combobox.
936 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
937 ASSERT_TRUE(annot);
938
939 // Check that the flag values are as expected.
940 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
941 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
942 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
943 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
944 FPDFPage_CloseAnnot(annot);
945
946 // Retrieve the second annotation: regular combobox.
947 annot = FPDFPage_GetAnnot(page, 1);
948 ASSERT_TRUE(annot);
949
950 // Check that the flag values are as expected.
951 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
952 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
953 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
954 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
955 FPDFPage_CloseAnnot(annot);
956
957 // Retrieve the third annotation: read-only combobox.
958 annot = FPDFPage_GetAnnot(page, 2);
959 ASSERT_TRUE(annot);
960
961 // Check that the flag values are as expected.
962 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
963 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
964 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
965 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
966 FPDFPage_CloseAnnot(annot);
967
968 UnloadPage(page);
969}
Diana Gage40870db2017-07-19 18:16:03 -0700970
971TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotNull) {
972 // Open file with form text fields.
973 EXPECT_TRUE(OpenDocument("text_form.pdf"));
974 FPDF_PAGE page = LoadPage(0);
975 ASSERT_TRUE(page);
976
977 // Attempt to get an annotation where no annotation exists on page.
978 FPDF_ANNOTATION annot =
979 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0);
980 EXPECT_FALSE(annot);
981
982 UnloadPage(page);
983}
984
985TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsTextField) {
986 // Open file with form text fields.
987 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
988 FPDF_PAGE page = LoadPage(0);
989 ASSERT_TRUE(page);
990
991 // Retrieve user-editable text field annotation.
992 FPDF_ANNOTATION annot =
993 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118);
994 ASSERT_TRUE(annot);
995
996 // Check that interactive form annotation flag values are as expected.
997 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
998 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
999 FPDFPage_CloseAnnot(annot);
1000
1001 // Retrieve read-only text field annotation.
1002 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202);
1003 ASSERT_TRUE(annot);
1004
1005 // Check that interactive form annotation flag values are as expected.
1006 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1007 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1008 FPDFPage_CloseAnnot(annot);
1009
1010 UnloadPage(page);
1011}
1012
1013TEST_F(FPDFAnnotEmbeddertest, GetFormAnnotAndCheckFlagsComboBox) {
1014 // Open file with form comboboxes.
1015 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1016 FPDF_PAGE page = LoadPage(0);
1017 ASSERT_TRUE(page);
1018
1019 // Retrieve user-editable combobox annotation.
1020 FPDF_ANNOTATION annot =
1021 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 63);
1022 ASSERT_TRUE(annot);
1023
1024 // Check that interactive form annotation flag values are as expected.
1025 int flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1026 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1027 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1028 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1029 FPDFPage_CloseAnnot(annot);
1030
1031 // Retrieve regular combobox annotation.
1032 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 113);
1033 ASSERT_TRUE(annot);
1034
1035 // Check that interactive form annotation flag values are as expected.
1036 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1037 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1038 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1039 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1040 FPDFPage_CloseAnnot(annot);
1041
1042 // Retrieve read-only combobox annotation.
1043 annot = FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 213);
1044 ASSERT_TRUE(annot);
1045
1046 // Check that interactive form annotation flag values are as expected.
1047 flags = FPDFAnnot_GetFormFieldFlags(page, annot);
1048 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1049 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1050 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1051 FPDFPage_CloseAnnot(annot);
1052
1053 UnloadPage(page);
1054}