blob: 9d9e2d365f2b2d9e2112bf5ad913f6ace1c1d3be [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
9#include "public/fpdf_annot.h"
10#include "public/fpdfview.h"
11#include "testing/embedder_test.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
Jane Liu20eafda2017-06-07 10:33:24 -040014class FPDFAnnotEmbeddertest : public EmbedderTest, public TestSaver {};
Jane Liu4fd9a472017-06-01 18:56:09 -040015
Jane Liue17011d2017-06-21 12:18:37 -040016TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) {
17 // Open a file with one annotation and load its first page.
18 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
19 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
20 ASSERT_TRUE(page);
21
22 // This annotation has a malformed appearance stream, which does not have its
23 // normal appearance defined, only its rollover appearance. In this case, its
24 // normal appearance should be generated, allowing the highlight annotation to
25 // still display.
26 FPDF_BITMAP bitmap = RenderPageWithFlags(page, FPDF_ANNOT);
27 CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
28 FPDFBitmap_Destroy(bitmap);
29
30 UnloadPage(page);
31}
32
Jane Liu4fd9a472017-06-01 18:56:09 -040033TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) {
34 // Open a file with one annotation and load its first page.
35 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
36 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
37 ASSERT_TRUE(page);
38
39 // Check that there is a total of 1 annotation on its first page.
40 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
41
42 // Check that the annotation is of type "highlight".
Jane Liud60e9ad2017-06-26 11:28:36 -040043 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
44 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -040045 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
46
47 // Check that the annotation color is yellow.
48 unsigned int R;
49 unsigned int G;
50 unsigned int B;
51 unsigned int A;
52 EXPECT_TRUE(
53 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
54 EXPECT_EQ(255u, R);
55 EXPECT_EQ(255u, G);
56 EXPECT_EQ(0u, B);
57 EXPECT_EQ(255u, A);
58
59 // Check that the author is correct.
60 unsigned long len =
61 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, nullptr, 0);
62 std::vector<char> buf(len);
63 EXPECT_EQ(28u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Author, buf.data(),
64 len));
65 EXPECT_STREQ(L"Jae Hyun Park",
66 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
67 .c_str());
68
69 // Check that the content is correct.
70 len = FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
71 buf.clear();
72 buf.resize(len);
73 EXPECT_EQ(2690u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
74 buf.data(), len));
75 const wchar_t contents[] =
76 L"This is a note for that highlight annotation. Very long highlight "
77 "annotation. Long long long Long long longLong long longLong long "
78 "longLong long longLong long longLong long longLong long longLong long "
79 "longLong long longLong long longLong long longLong long longLong long "
80 "longLong long longLong long longLong long longLong long longLong long "
81 "longLong long longLong long longLong long longLong long longLong long "
82 "longLong long longLong long longLong long longLong long longLong long "
83 "longLong long longLong long longLong long longLong long longLong long "
84 "longLong long longLong long longLong long longLong long longLong long "
85 "longLong long longLong long longLong long longLong long longLong long "
86 "longLong long longLong long longLong long longLong long longLong long "
87 "longLong long longLong long longLong 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 long. END";
96 EXPECT_STREQ(contents,
97 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
98 .c_str());
99
100 // Check that the quadpoints are correct.
Jane Liud60e9ad2017-06-26 11:28:36 -0400101 FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400102 EXPECT_EQ(115.802643f, quadpoints.x1);
103 EXPECT_EQ(718.913940f, quadpoints.y1);
104 EXPECT_EQ(157.211182f, quadpoints.x4);
105 EXPECT_EQ(706.264465f, quadpoints.y4);
106
Jane Liue10509a2017-06-20 16:47:41 -0400107 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400108 UnloadPage(page);
109}
110
111TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) {
112 // Open a file with three annotations and load its first page.
113 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
114 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
115 ASSERT_TRUE(page);
116
117 // Check that there is a total of 3 annotation on its first page.
118 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
119
Jane Liu20eafda2017-06-07 10:33:24 -0400120 // Check that the third annotation is of type "ink".
Jane Liud60e9ad2017-06-26 11:28:36 -0400121 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 2);
122 ASSERT_TRUE(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400123 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot));
124
125 // Check that the annotation color is blue with opacity.
126 unsigned int R;
127 unsigned int G;
128 unsigned int B;
129 unsigned int A;
130 EXPECT_TRUE(
131 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
132 EXPECT_EQ(0u, R);
133 EXPECT_EQ(0u, G);
134 EXPECT_EQ(255u, B);
135 EXPECT_EQ(76u, A);
136
137 // Check that there is no content.
138 EXPECT_EQ(2u,
139 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0));
140
141 // Check that the rectange coordinates are correct.
142 // Note that upon rendering, the rectangle coordinates will be adjusted.
Jane Liud60e9ad2017-06-26 11:28:36 -0400143 FS_RECTF rect = FPDFAnnot_GetRect(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400144 EXPECT_EQ(351.820404f, rect.left);
145 EXPECT_EQ(583.830688f, rect.bottom);
146 EXPECT_EQ(475.336090f, rect.right);
147 EXPECT_EQ(681.535034f, rect.top);
148
Jane Liue10509a2017-06-20 16:47:41 -0400149 FPDFPage_CloseAnnot(annot);
Jane Liu4fd9a472017-06-01 18:56:09 -0400150 UnloadPage(page);
151}
Jane Liu20eafda2017-06-07 10:33:24 -0400152
153TEST_F(FPDFAnnotEmbeddertest, AddIllegalSubtypeAnnotation) {
154 // Open a file with one annotation and load its first page.
155 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
156 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
157 ASSERT_TRUE(page);
158
159 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400160 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400161
162 UnloadPage(page);
163}
164
Jane Liud321ef92017-06-14 09:56:22 -0400165TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400166 // Open a file with no annotation and load its first page.
167 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
168 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
169 ASSERT_TRUE(page);
170 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
171
Jane Liueda65252017-06-07 11:31:27 -0400172 // Add a text annotation to the page.
Jane Liud60e9ad2017-06-26 11:28:36 -0400173 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT);
174 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400175
176 // Check that there is now 1 annotations on this page.
177 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
178
179 // Check that the subtype of the annotation is correct.
180 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
Jane Liue10509a2017-06-20 16:47:41 -0400181 FPDFPage_CloseAnnot(annot);
182
Jane Liud60e9ad2017-06-26 11:28:36 -0400183 annot = FPDFPage_GetAnnot(page, 0);
184 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400185 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot));
186
187 // Set the color of the annotation.
188 ASSERT_TRUE(
189 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
190 // Check that the color has been set correctly.
191 unsigned int R;
192 unsigned int G;
193 unsigned int B;
194 unsigned int A;
195 EXPECT_TRUE(
196 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
197 EXPECT_EQ(51u, R);
198 EXPECT_EQ(102u, G);
199 EXPECT_EQ(153u, B);
200 EXPECT_EQ(204u, A);
201
202 // Change the color of the annotation.
203 ASSERT_TRUE(
204 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 204, 153, 102, 51));
205 // Check that the color has been set correctly.
206 EXPECT_TRUE(
207 FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A));
208 EXPECT_EQ(204u, R);
209 EXPECT_EQ(153u, G);
210 EXPECT_EQ(102u, B);
211 EXPECT_EQ(51u, A);
212
213 // Set the annotation rectangle.
Jane Liud60e9ad2017-06-26 11:28:36 -0400214 FS_RECTF rect = FPDFAnnot_GetRect(annot);
215 EXPECT_EQ(0.f, rect.left);
216 EXPECT_EQ(0.f, rect.right);
Jane Liu20eafda2017-06-07 10:33:24 -0400217 rect.left = 35;
218 rect.bottom = 150;
219 rect.right = 53;
220 rect.top = 165;
Jane Liu06462752017-06-27 16:41:14 -0400221 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
Jane Liu20eafda2017-06-07 10:33:24 -0400222 // Check that the annotation rectangle has been set correctly.
Jane Liud60e9ad2017-06-26 11:28:36 -0400223 rect = FPDFAnnot_GetRect(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400224 EXPECT_EQ(35.f, rect.left);
225 EXPECT_EQ(150.f, rect.bottom);
226 EXPECT_EQ(53.f, rect.right);
227 EXPECT_EQ(165.f, rect.top);
228
229 // Set the content of the annotation.
230 const wchar_t contents[] = L"Hello! This is a customized content.";
231 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
232 GetFPDFWideString(contents);
233 ASSERT_TRUE(
234 FPDFAnnot_SetText(annot, FPDFANNOT_TEXTTYPE_Contents, text.get()));
235 // Check that the content has been set correctly.
236 unsigned long len =
237 FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents, nullptr, 0);
238 std::vector<char> buf(len);
239 EXPECT_EQ(74u, FPDFAnnot_GetText(annot, FPDFANNOT_TEXTTYPE_Contents,
240 buf.data(), len));
241 EXPECT_STREQ(contents,
242 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
243 .c_str());
244
Jane Liue10509a2017-06-20 16:47:41 -0400245 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400246 UnloadPage(page);
247}
248
249TEST_F(FPDFAnnotEmbeddertest, AddAndSaveUnderlineAnnotation) {
250 // Open a file with one annotation and load its first page.
251 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
252 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
253 ASSERT_TRUE(page);
254
255 // Check that there is a total of one annotation on its first page, and verify
256 // its quadpoints.
257 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liud60e9ad2017-06-26 11:28:36 -0400258 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
259 ASSERT_TRUE(annot);
260 FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400261 EXPECT_EQ(115.802643f, quadpoints.x1);
262 EXPECT_EQ(718.913940f, quadpoints.y1);
263 EXPECT_EQ(157.211182f, quadpoints.x4);
264 EXPECT_EQ(706.264465f, quadpoints.y4);
Jane Liue10509a2017-06-20 16:47:41 -0400265 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400266
267 // Add an underline annotation to the page and set its quadpoints.
Jane Liud60e9ad2017-06-26 11:28:36 -0400268 annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
269 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400270 quadpoints.x1 = 140.802643f;
271 quadpoints.x3 = 140.802643f;
Jane Liu06462752017-06-27 16:41:14 -0400272 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
Jane Liue10509a2017-06-20 16:47:41 -0400273 FPDFPage_CloseAnnot(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400274
275 // Save the document, closing the page and document.
276 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
277 FPDF_ClosePage(page);
278
279 // Open the saved document.
280 std::string new_file = GetString();
281 FPDF_FILEACCESS file_access;
282 memset(&file_access, 0, sizeof(file_access));
283 file_access.m_FileLen = new_file.size();
284 file_access.m_GetBlock = GetBlockFromString;
285 file_access.m_Param = &new_file;
286 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
287 ASSERT_TRUE(new_doc);
288 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
289 ASSERT_TRUE(new_page);
290
291 // Check that the saved document has 2 annotations on the first page
292 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
293
294 // Check that the second annotation is an underline annotation and verify
295 // its quadpoints.
Jane Liud60e9ad2017-06-26 11:28:36 -0400296 FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(new_page, 1);
297 ASSERT_TRUE(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400298 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot));
Jane Liud60e9ad2017-06-26 11:28:36 -0400299 FS_QUADPOINTSF new_quadpoints = FPDFAnnot_GetAttachmentPoints(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400300 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
301 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
302 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
303 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
304
Jane Liue10509a2017-06-20 16:47:41 -0400305 FPDFPage_CloseAnnot(new_annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400306 FPDF_ClosePage(new_page);
307 FPDF_CloseDocument(new_doc);
308}
Jane Liu06462752017-06-27 16:41:14 -0400309
310TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) {
311 // Open a file with four annotations and load its first page.
312 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
313 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
314 ASSERT_TRUE(page);
315 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
316
317 // Retrieve the highlight annotation which has its AP stream already defined.
318 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
319 ASSERT_TRUE(annot);
320 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
321
322 // Check that color cannot be set when an AP stream is defined already.
323 EXPECT_FALSE(
324 FPDFAnnot_SetColor(annot, FPDFANNOT_COLORTYPE_Color, 51, 102, 153, 204));
325
326 // Check that when getting the attachment points, bounding box points are
327 // returned since this is a markup annotation with AP defined.
328 FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
329 EXPECT_NEAR(0.f, quadpoints.x1, 0.001f);
330 EXPECT_NEAR(16.9955f, quadpoints.y1, 0.001f);
331 EXPECT_NEAR(68.5953f, quadpoints.x4, 0.001f);
332 EXPECT_NEAR(0.f, quadpoints.y4, 0.001f);
333
334 // Check that when new attachment points define a smaller bounding box, the
335 // bounding box does not get updated.
336 quadpoints.x1 = 1.0f;
337 quadpoints.x3 = 1.0f;
338 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
339 FS_QUADPOINTSF new_quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
340 EXPECT_NE(quadpoints.x1, new_quadpoints.x1);
341
342 // Check that the bounding box gets updated successfully when valid attachment
343 // points are set.
344 quadpoints.x1 = 0.f;
345 quadpoints.y1 = 721.792f;
346 quadpoints.x2 = 133.055f;
347 quadpoints.y2 = 721.792f;
348 quadpoints.x3 = 0.f;
349 quadpoints.x4 = 133.055f;
350 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, &quadpoints));
351 new_quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
352 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
353 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
354 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
355 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
356
357 // Check that when getting the annotation rectangle, rectangle points are
358 // returned, but not bounding box points.
359 FS_RECTF rect = FPDFAnnot_GetRect(annot);
360 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
361 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
362 EXPECT_NEAR(136.325f, rect.right, 0.001f);
363 EXPECT_NEAR(721.292f, rect.top, 0.001f);
364
365 // Check that the rectangle gets updated successfully when a valid rectangle
366 // is set, and that the bounding box is not modified.
367 rect.left = 0.f;
368 rect.bottom = 0.f;
369 rect.right = 134.055f;
370 rect.top = 722.792f;
371 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
372 FS_RECTF new_rect = FPDFAnnot_GetRect(annot);
373 EXPECT_EQ(rect.right, new_rect.right);
374 new_quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
375 EXPECT_NE(rect.right, new_quadpoints.x2);
376
377 FPDFPage_CloseAnnot(annot);
378
379 // Retrieve the square annotation which has its AP stream already defined.
380 annot = FPDFPage_GetAnnot(page, 2);
381 ASSERT_TRUE(annot);
382 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot));
383
384 // Check that the rectangle and the bounding box get updated successfully when
385 // a valid rectangle is set, since this is not a markup annotation.
386 rect = FPDFAnnot_GetRect(annot);
387 rect.right += 1.f;
388 ASSERT_TRUE(FPDFAnnot_SetRect(annot, &rect));
389 new_rect = FPDFAnnot_GetRect(annot);
390 EXPECT_EQ(rect.right, new_rect.right);
391
392 FPDFPage_CloseAnnot(annot);
393 UnloadPage(page);
394}