blob: 4c9a7f1b94ea01decceb4ba026ac011f06117d41 [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
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00005#include <algorithm>
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00006#include <cwchar>
Jane Liu20eafda2017-06-07 10:33:24 -04007#include <memory>
8#include <string>
Jane Liu4fd9a472017-06-01 18:56:09 -04009#include <vector>
10
Jane Liubaa7ff42017-06-29 19:18:23 -040011#include "core/fxcrt/fx_system.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000012#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040013#include "public/fpdf_annot.h"
Jane Liubaa7ff42017-06-29 19:18:23 -040014#include "public/fpdf_edit.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040015#include "public/fpdfview.h"
16#include "testing/embedder_test.h"
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000017#include "testing/gmock/include/gmock/gmock-matchers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040018#include "testing/gtest/include/gtest/gtest.h"
19
Lei Zhangdf064df2017-08-31 02:33:27 -070020static constexpr char kContentsKey[] = "Contents";
21
Lei Zhangab41f252018-12-23 03:10:50 +000022class FPDFAnnotEmbedderTest : public EmbedderTest {};
Jane Liu4fd9a472017-06-01 18:56:09 -040023
Lei Zhang4afee172018-01-10 20:57:15 +000024std::wstring BufferToWString(const std::vector<char>& buf) {
25 return GetPlatformWString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000026}
27
Lei Zhang4afee172018-01-10 20:57:15 +000028std::string BufferToString(const std::vector<char>& buf) {
29 return GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buf.data()));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +000030}
31
Lei Zhangab41f252018-12-23 03:10:50 +000032TEST_F(FPDFAnnotEmbedderTest, BadParams) {
Lei Zhang7557e7b2018-09-14 17:02:40 +000033 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
34 FPDF_PAGE page = LoadPage(0);
35 ASSERT_TRUE(page);
36
37 EXPECT_EQ(0, FPDFPage_GetAnnotCount(nullptr));
38
39 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 0));
40 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, -1));
41 EXPECT_FALSE(FPDFPage_GetAnnot(nullptr, 1));
42 EXPECT_FALSE(FPDFPage_GetAnnot(page, -1));
43 EXPECT_FALSE(FPDFPage_GetAnnot(page, 1));
44
45 EXPECT_EQ(FPDF_ANNOT_UNKNOWN, FPDFAnnot_GetSubtype(nullptr));
46
47 EXPECT_EQ(0, FPDFAnnot_GetObjectCount(nullptr));
48
49 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 0));
50 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, -1));
51 EXPECT_FALSE(FPDFAnnot_GetObject(nullptr, 1));
52
53 EXPECT_FALSE(FPDFAnnot_HasKey(nullptr, "foo"));
54
55 static constexpr wchar_t kContents[] = L"Bar";
56 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
57 GetFPDFWideString(kContents);
58 EXPECT_FALSE(FPDFAnnot_SetStringValue(nullptr, "foo", text.get()));
59
60 char buffer[128];
61 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", nullptr, 0));
62 EXPECT_EQ(0u, FPDFAnnot_GetStringValue(nullptr, "foo", buffer, 0));
63 EXPECT_EQ(0u,
64 FPDFAnnot_GetStringValue(nullptr, "foo", buffer, sizeof(buffer)));
65
66 UnloadPage(page);
67}
68
Lei Zhangab41f252018-12-23 03:10:50 +000069TEST_F(FPDFAnnotEmbedderTest, RenderAnnotWithOnlyRolloverAP) {
Jane Liue17011d2017-06-21 12:18:37 -040070 // Open a file with one annotation and load its first page.
71 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +000072 FPDF_PAGE page = LoadPage(0);
Jane Liue17011d2017-06-21 12:18:37 -040073 ASSERT_TRUE(page);
74
75 // This annotation has a malformed appearance stream, which does not have its
76 // normal appearance defined, only its rollover appearance. In this case, its
77 // normal appearance should be generated, allowing the highlight annotation to
78 // still display.
Tom Sepeze08d2b12018-04-25 18:49:32 +000079 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhanga98e3662018-02-07 20:28:35 +000080 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
Jane Liue17011d2017-06-21 12:18:37 -040081
82 UnloadPage(page);
83}
84
Lei Zhangab41f252018-12-23 03:10:50 +000085TEST_F(FPDFAnnotEmbedderTest, RenderMultilineMarkupAnnotWithoutAP) {
Ralf Sipplb3a52402018-03-19 23:30:28 +000086 const char md5_hash[] = "76512832d88017668d9acc7aacd13dae";
Henrique Nakashima5098b252018-03-26 21:46:00 +000087 // Open a file with multiline markup annotations.
Ralf Sipplb3a52402018-03-19 23:30:28 +000088 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
89 FPDF_PAGE page = LoadPage(0);
90 ASSERT_TRUE(page);
91
Tom Sepeze08d2b12018-04-25 18:49:32 +000092 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Ralf Sipplb3a52402018-03-19 23:30:28 +000093 CompareBitmap(bitmap.get(), 595, 842, md5_hash);
94
95 UnloadPage(page);
96}
97
Lei Zhangab41f252018-12-23 03:10:50 +000098TEST_F(FPDFAnnotEmbedderTest, ExtractHighlightLongContent) {
Jane Liu4fd9a472017-06-01 18:56:09 -040099 // Open a file with one annotation and load its first page.
100 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000101 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400102 ASSERT_TRUE(page);
103
104 // Check that there is a total of 1 annotation on its first page.
105 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
106
107 // Check that the annotation is of type "highlight".
Lei Zhanga21d5932018-02-05 18:28:38 +0000108 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000109 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000110 ASSERT_TRUE(annot);
111 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400112
Lei Zhanga21d5932018-02-05 18:28:38 +0000113 // Check that the annotation color is yellow.
114 unsigned int R;
115 unsigned int G;
116 unsigned int B;
117 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000118 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000119 &G, &B, &A));
120 EXPECT_EQ(255u, R);
121 EXPECT_EQ(255u, G);
122 EXPECT_EQ(0u, B);
123 EXPECT_EQ(255u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400124
Lei Zhanga21d5932018-02-05 18:28:38 +0000125 // Check that the author is correct.
126 static constexpr char kAuthorKey[] = "T";
127 EXPECT_EQ(FPDF_OBJECT_STRING,
128 FPDFAnnot_GetValueType(annot.get(), kAuthorKey));
129 unsigned long len =
130 FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, nullptr, 0);
131 std::vector<char> buf(len);
132 EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot.get(), kAuthorKey, buf.data(),
133 len));
134 EXPECT_STREQ(L"Jae Hyun Park", BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400135
Lei Zhanga21d5932018-02-05 18:28:38 +0000136 // Check that the content is correct.
137 EXPECT_EQ(FPDF_OBJECT_STRING,
138 FPDFAnnot_GetValueType(annot.get(), kContentsKey));
139 len = FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
140 buf.clear();
141 buf.resize(len);
142 EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
143 buf.data(), len));
144 const wchar_t contents[] =
145 L"This is a note for that highlight annotation. Very long highlight "
146 "annotation. Long long long Long 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 longLong long longLong long longLong long longLong long "
164 "longLong long long. END";
165 EXPECT_STREQ(contents, BufferToWString(buf).c_str());
Jane Liu4fd9a472017-06-01 18:56:09 -0400166
Lei Zhanga21d5932018-02-05 18:28:38 +0000167 // Check that the quadpoints are correct.
168 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000169 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000170 EXPECT_EQ(115.802643f, quadpoints.x1);
171 EXPECT_EQ(718.913940f, quadpoints.y1);
172 EXPECT_EQ(157.211182f, quadpoints.x4);
173 EXPECT_EQ(706.264465f, quadpoints.y4);
174 }
Tom Sepez507d0192018-11-07 16:37:51 +0000175 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400176}
177
Lei Zhangab41f252018-12-23 03:10:50 +0000178TEST_F(FPDFAnnotEmbedderTest, ExtractInkMultiple) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400179 // Open a file with three annotations and load its first page.
180 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000181 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu4fd9a472017-06-01 18:56:09 -0400182 ASSERT_TRUE(page);
183
184 // Check that there is a total of 3 annotation on its first page.
185 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
186
Lei Zhanga21d5932018-02-05 18:28:38 +0000187 {
188 // Check that the third annotation is of type "ink".
Tom Sepeze08d2b12018-04-25 18:49:32 +0000189 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000190 ASSERT_TRUE(annot);
191 EXPECT_EQ(FPDF_ANNOT_INK, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu4fd9a472017-06-01 18:56:09 -0400192
Lei Zhanga21d5932018-02-05 18:28:38 +0000193 // Check that the annotation color is blue with opacity.
194 unsigned int R;
195 unsigned int G;
196 unsigned int B;
197 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000198 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000199 &G, &B, &A));
200 EXPECT_EQ(0u, R);
201 EXPECT_EQ(0u, G);
202 EXPECT_EQ(255u, B);
203 EXPECT_EQ(76u, A);
Jane Liu4fd9a472017-06-01 18:56:09 -0400204
Lei Zhanga21d5932018-02-05 18:28:38 +0000205 // Check that there is no content.
206 EXPECT_EQ(2u,
207 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0));
Jane Liu4fd9a472017-06-01 18:56:09 -0400208
Lei Zhanga21d5932018-02-05 18:28:38 +0000209 // Check that the rectange coordinates are correct.
210 // Note that upon rendering, the rectangle coordinates will be adjusted.
211 FS_RECTF rect;
212 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
213 EXPECT_EQ(351.820404f, rect.left);
214 EXPECT_EQ(583.830688f, rect.bottom);
215 EXPECT_EQ(475.336090f, rect.right);
216 EXPECT_EQ(681.535034f, rect.top);
217 }
Tom Sepezef43c262018-11-07 16:41:32 +0000218 {
219 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
220 CompareBitmap(bitmap.get(), 612, 792, "354002e1c4386d38fdde29ef8d61074a");
221 }
Tom Sepez507d0192018-11-07 16:37:51 +0000222 UnloadPageNoEvents(page);
Jane Liu4fd9a472017-06-01 18:56:09 -0400223}
Jane Liu20eafda2017-06-07 10:33:24 -0400224
Lei Zhangab41f252018-12-23 03:10:50 +0000225TEST_F(FPDFAnnotEmbedderTest, AddIllegalSubtypeAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400226 // Open a file with one annotation and load its first page.
227 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000228 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400229 ASSERT_TRUE(page);
230
231 // Add an annotation with an illegal subtype.
Jane Liud60e9ad2017-06-26 11:28:36 -0400232 ASSERT_FALSE(FPDFPage_CreateAnnot(page, -1));
Jane Liu20eafda2017-06-07 10:33:24 -0400233
234 UnloadPage(page);
235}
236
Lei Zhangab41f252018-12-23 03:10:50 +0000237TEST_F(FPDFAnnotEmbedderTest, AddFirstTextAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400238 // Open a file with no annotation and load its first page.
239 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000240 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400241 ASSERT_TRUE(page);
242 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
243
Lei Zhanga21d5932018-02-05 18:28:38 +0000244 {
245 // Add a text annotation to the page.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000246 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
Lei Zhanga21d5932018-02-05 18:28:38 +0000247 ASSERT_TRUE(annot);
Jane Liu20eafda2017-06-07 10:33:24 -0400248
Lei Zhanga21d5932018-02-05 18:28:38 +0000249 // Check that there is now 1 annotations on this page.
250 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400251
Lei Zhanga21d5932018-02-05 18:28:38 +0000252 // Check that the subtype of the annotation is correct.
253 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
254 }
Jane Liue10509a2017-06-20 16:47:41 -0400255
Lei Zhanga21d5932018-02-05 18:28:38 +0000256 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000257 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000258 ASSERT_TRUE(annot);
259 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu20eafda2017-06-07 10:33:24 -0400260
Lei Zhanga21d5932018-02-05 18:28:38 +0000261 // Set the color of the annotation.
262 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
263 102, 153, 204));
264 // Check that the color has been set correctly.
265 unsigned int R;
266 unsigned int G;
267 unsigned int B;
268 unsigned int A;
Lei Zhang75c81712018-02-08 17:22:39 +0000269 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000270 &G, &B, &A));
271 EXPECT_EQ(51u, R);
272 EXPECT_EQ(102u, G);
273 EXPECT_EQ(153u, B);
274 EXPECT_EQ(204u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400275
Lei Zhanga21d5932018-02-05 18:28:38 +0000276 // Change the color of the annotation.
277 ASSERT_TRUE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 204,
278 153, 102, 51));
279 // Check that the color has been set correctly.
Lei Zhang75c81712018-02-08 17:22:39 +0000280 ASSERT_TRUE(FPDFAnnot_GetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, &R,
Lei Zhanga21d5932018-02-05 18:28:38 +0000281 &G, &B, &A));
282 EXPECT_EQ(204u, R);
283 EXPECT_EQ(153u, G);
284 EXPECT_EQ(102u, B);
285 EXPECT_EQ(51u, A);
Jane Liu20eafda2017-06-07 10:33:24 -0400286
Lei Zhanga21d5932018-02-05 18:28:38 +0000287 // Set the annotation rectangle.
288 FS_RECTF rect;
289 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
290 EXPECT_EQ(0.f, rect.left);
291 EXPECT_EQ(0.f, rect.right);
292 rect.left = 35;
293 rect.bottom = 150;
294 rect.right = 53;
295 rect.top = 165;
296 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
297 // Check that the annotation rectangle has been set correctly.
298 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
299 EXPECT_EQ(35.f, rect.left);
300 EXPECT_EQ(150.f, rect.bottom);
301 EXPECT_EQ(53.f, rect.right);
302 EXPECT_EQ(165.f, rect.top);
Jane Liu20eafda2017-06-07 10:33:24 -0400303
Lei Zhanga21d5932018-02-05 18:28:38 +0000304 // Set the content of the annotation.
305 static constexpr wchar_t kContents[] =
306 L"Hello! This is a customized content.";
307 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
308 GetFPDFWideString(kContents);
309 ASSERT_TRUE(
310 FPDFAnnot_SetStringValue(annot.get(), kContentsKey, text.get()));
311 // Check that the content has been set correctly.
312 unsigned long len =
313 FPDFAnnot_GetStringValue(annot.get(), kContentsKey, nullptr, 0);
314 std::vector<char> buf(len);
315 EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot.get(), kContentsKey,
316 buf.data(), len));
317 EXPECT_STREQ(kContents, BufferToWString(buf).c_str());
318 }
Jane Liu20eafda2017-06-07 10:33:24 -0400319 UnloadPage(page);
320}
321
Lei Zhangab41f252018-12-23 03:10:50 +0000322TEST_F(FPDFAnnotEmbedderTest, AddAndSaveUnderlineAnnotation) {
Jane Liu20eafda2017-06-07 10:33:24 -0400323 // Open a file with one annotation and load its first page.
324 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000325 FPDF_PAGE page = LoadPage(0);
Jane Liu20eafda2017-06-07 10:33:24 -0400326 ASSERT_TRUE(page);
327
328 // Check that there is a total of one annotation on its first page, and verify
329 // its quadpoints.
330 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
Jane Liu0c6b07d2017-08-15 10:50:22 -0400331 FS_QUADPOINTSF quadpoints;
Lei Zhanga21d5932018-02-05 18:28:38 +0000332 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000333 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000334 ASSERT_TRUE(annot);
Ralf Sippl16381792018-04-12 21:20:26 +0000335 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000336 EXPECT_EQ(115.802643f, quadpoints.x1);
337 EXPECT_EQ(718.913940f, quadpoints.y1);
338 EXPECT_EQ(157.211182f, quadpoints.x4);
339 EXPECT_EQ(706.264465f, quadpoints.y4);
340 }
Jane Liu20eafda2017-06-07 10:33:24 -0400341
342 // Add an underline annotation to the page and set its quadpoints.
Lei Zhanga21d5932018-02-05 18:28:38 +0000343 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000344 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +0000345 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
346 ASSERT_TRUE(annot);
347 quadpoints.x1 = 140.802643f;
348 quadpoints.x3 = 140.802643f;
Ralf Sippl16381792018-04-12 21:20:26 +0000349 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot.get(), &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000350 }
Jane Liu20eafda2017-06-07 10:33:24 -0400351
352 // Save the document, closing the page and document.
353 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000354 UnloadPage(page);
Jane Liu20eafda2017-06-07 10:33:24 -0400355
356 // Open the saved document.
Henrique Nakashima09b41922017-10-27 20:39:29 +0000357 const char md5[] = "dba153419f67b7c0c0e3d22d3e8910d5";
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400358
Tom Sepezb9c3e272018-08-14 18:22:06 +0000359 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000360 page = LoadSavedPage(0);
361 VerifySavedRendering(page, 612, 792, md5);
Jane Liu20eafda2017-06-07 10:33:24 -0400362
363 // Check that the saved document has 2 annotations on the first page
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000364 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
Jane Liu20eafda2017-06-07 10:33:24 -0400365
Lei Zhanga21d5932018-02-05 18:28:38 +0000366 {
367 // Check that the second annotation is an underline annotation and verify
368 // its quadpoints.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000369 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000370 ASSERT_TRUE(new_annot);
371 EXPECT_EQ(FPDF_ANNOT_UNDERLINE, FPDFAnnot_GetSubtype(new_annot.get()));
372 FS_QUADPOINTSF new_quadpoints;
373 ASSERT_TRUE(
Ralf Sippl16381792018-04-12 21:20:26 +0000374 FPDFAnnot_GetAttachmentPoints(new_annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000375 EXPECT_NEAR(quadpoints.x1, new_quadpoints.x1, 0.001f);
376 EXPECT_NEAR(quadpoints.y1, new_quadpoints.y1, 0.001f);
377 EXPECT_NEAR(quadpoints.x4, new_quadpoints.x4, 0.001f);
378 EXPECT_NEAR(quadpoints.y4, new_quadpoints.y4, 0.001f);
379 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400380
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000381 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400382 CloseSavedDocument();
Jane Liu20eafda2017-06-07 10:33:24 -0400383}
Jane Liu06462752017-06-27 16:41:14 -0400384
Lei Zhangab41f252018-12-23 03:10:50 +0000385TEST_F(FPDFAnnotEmbedderTest, GetAndSetQuadPoints) {
Ralf Sippl16381792018-04-12 21:20:26 +0000386 // Open a file with four annotations and load its first page.
387 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
388 FPDF_PAGE page = LoadPage(0);
389 ASSERT_TRUE(page);
390 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
391
392 // Retrieve the highlight annotation.
393 FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0);
394 ASSERT_TRUE(annot);
395 ASSERT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot));
396
397 FS_QUADPOINTSF quadpoints;
398 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
399
400 {
401 // Verify the current one set of quadpoints.
402 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
403
404 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
405 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
406 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
407 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
408 }
409
410 {
411 // Update the quadpoints.
412 FS_QUADPOINTSF new_quadpoints = quadpoints;
413 new_quadpoints.y1 -= 20.f;
414 new_quadpoints.y2 -= 20.f;
415 new_quadpoints.y3 -= 20.f;
416 new_quadpoints.y4 -= 20.f;
417 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot, 0, &new_quadpoints));
418
419 // Verify added quadpoint set
420 ASSERT_EQ(1u, FPDFAnnot_CountAttachmentPoints(annot));
421 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 0, &quadpoints));
422 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
423 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
424 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
425 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
426 }
427
428 {
429 // Append a new set of quadpoints.
430 FS_QUADPOINTSF new_quadpoints = quadpoints;
431 new_quadpoints.y1 += 20.f;
432 new_quadpoints.y2 += 20.f;
433 new_quadpoints.y3 += 20.f;
434 new_quadpoints.y4 += 20.f;
435 ASSERT_TRUE(FPDFAnnot_AppendAttachmentPoints(annot, &new_quadpoints));
436
437 // Verify added quadpoint set
438 ASSERT_EQ(2u, FPDFAnnot_CountAttachmentPoints(annot));
439 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot, 1, &quadpoints));
440 EXPECT_NEAR(new_quadpoints.x1, quadpoints.x1, 0.001f);
441 EXPECT_NEAR(new_quadpoints.y1, quadpoints.y1, 0.001f);
442 EXPECT_NEAR(new_quadpoints.x4, quadpoints.x4, 0.001f);
443 EXPECT_NEAR(new_quadpoints.y4, quadpoints.y4, 0.001f);
444 }
445
446 {
447 // Setting and getting quadpoints at out-of-bound index should fail
448 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(annot, 300000, &quadpoints));
449 EXPECT_FALSE(FPDFAnnot_GetAttachmentPoints(annot, 300000, &quadpoints));
450 }
451
452 FPDFPage_CloseAnnot(annot);
453
454 // Retrieve the square annotation
455 FPDF_ANNOTATION squareAnnot = FPDFPage_GetAnnot(page, 2);
456
457 {
458 // Check that attempting to set its quadpoints would fail
459 ASSERT_TRUE(squareAnnot);
460 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(squareAnnot));
461 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(squareAnnot));
462 EXPECT_FALSE(FPDFAnnot_SetAttachmentPoints(squareAnnot, 0, &quadpoints));
463 }
464
465 FPDFPage_CloseAnnot(squareAnnot);
Ralf Sippl16381792018-04-12 21:20:26 +0000466 UnloadPage(page);
467}
468
Lei Zhangab41f252018-12-23 03:10:50 +0000469TEST_F(FPDFAnnotEmbedderTest, ModifyRectQuadpointsWithAP) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400470#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liub370e5a2017-08-16 13:24:58 -0400471 const char md5_original[] = "63af8432fab95a67cdebb7cd0e514941";
472 const char md5_modified_highlight[] = "aec26075011349dec9bace891856b5f2";
473 const char md5_modified_square[] = "057f57a32be95975775e5ec513fdcb56";
Dan Sinclair698aed72017-09-26 16:24:49 -0400474#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000475 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
476 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
477 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400478#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000479 const char md5_original[] = "0e27376094f11490f74c65f3dc3a42c5";
480 const char md5_modified_highlight[] = "66f3caef3a7d488a4fa1ad37fc06310e";
481 const char md5_modified_square[] = "a456dad0bc6801ee2d6408a4394af563";
Jane Liub370e5a2017-08-16 13:24:58 -0400482#endif
483
Jane Liu06462752017-06-27 16:41:14 -0400484 // Open a file with four annotations and load its first page.
485 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000486 FPDF_PAGE page = LoadPage(0);
Jane Liu06462752017-06-27 16:41:14 -0400487 ASSERT_TRUE(page);
488 EXPECT_EQ(4, FPDFPage_GetAnnotCount(page));
489
Jane Liub370e5a2017-08-16 13:24:58 -0400490 // Check that the original file renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000491 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000492 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000493 CompareBitmap(bitmap.get(), 612, 792, md5_original);
494 }
Jane Liub370e5a2017-08-16 13:24:58 -0400495
Jane Liu0c6b07d2017-08-15 10:50:22 -0400496 FS_RECTF rect;
Jane Liu0c6b07d2017-08-15 10:50:22 -0400497 FS_RECTF new_rect;
Lei Zhanga21d5932018-02-05 18:28:38 +0000498
499 // Retrieve the highlight annotation which has its AP stream already defined.
500 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000501 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000502 ASSERT_TRUE(annot);
503 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
504
505 // Check that color cannot be set when an AP stream is defined already.
506 EXPECT_FALSE(FPDFAnnot_SetColor(annot.get(), FPDFANNOT_COLORTYPE_Color, 51,
507 102, 153, 204));
508
509 // Verify its attachment points.
510 FS_QUADPOINTSF quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000511 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000512 EXPECT_NEAR(72.0000f, quadpoints.x1, 0.001f);
513 EXPECT_NEAR(720.792f, quadpoints.y1, 0.001f);
514 EXPECT_NEAR(132.055f, quadpoints.x4, 0.001f);
515 EXPECT_NEAR(704.796f, quadpoints.y4, 0.001f);
516
517 // Check that updating the attachment points would succeed.
518 quadpoints.x1 -= 50.f;
519 quadpoints.x2 -= 50.f;
520 quadpoints.x3 -= 50.f;
521 quadpoints.x4 -= 50.f;
Ralf Sippl16381792018-04-12 21:20:26 +0000522 ASSERT_TRUE(FPDFAnnot_SetAttachmentPoints(annot.get(), 0, &quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000523 FS_QUADPOINTSF new_quadpoints;
Ralf Sippl16381792018-04-12 21:20:26 +0000524 ASSERT_TRUE(FPDFAnnot_GetAttachmentPoints(annot.get(), 0, &new_quadpoints));
Lei Zhanga21d5932018-02-05 18:28:38 +0000525 EXPECT_EQ(quadpoints.x1, new_quadpoints.x1);
526 EXPECT_EQ(quadpoints.y1, new_quadpoints.y1);
527 EXPECT_EQ(quadpoints.x4, new_quadpoints.x4);
528 EXPECT_EQ(quadpoints.y4, new_quadpoints.y4);
529
530 // Check that updating quadpoints does not change the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000531 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000532 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000533 CompareBitmap(bitmap.get(), 612, 792, md5_original);
534 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000535
536 // Verify its annotation rectangle.
537 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
538 EXPECT_NEAR(67.7299f, rect.left, 0.001f);
539 EXPECT_NEAR(704.296f, rect.bottom, 0.001f);
540 EXPECT_NEAR(136.325f, rect.right, 0.001f);
541 EXPECT_NEAR(721.292f, rect.top, 0.001f);
542
543 // Check that updating the rectangle would succeed.
544 rect.left -= 60.f;
545 rect.right -= 60.f;
546 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
547 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
548 EXPECT_EQ(rect.right, new_rect.right);
549 }
Jane Liu06462752017-06-27 16:41:14 -0400550
Jane Liub370e5a2017-08-16 13:24:58 -0400551 // Check that updating the rectangle changes the annotation's position.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000552 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000553 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000554 CompareBitmap(bitmap.get(), 612, 792, md5_modified_highlight);
555 }
Jane Liub370e5a2017-08-16 13:24:58 -0400556
Lei Zhanga21d5932018-02-05 18:28:38 +0000557 {
558 // Retrieve the square annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000559 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000560 ASSERT_TRUE(annot);
561 EXPECT_EQ(FPDF_ANNOT_SQUARE, FPDFAnnot_GetSubtype(annot.get()));
Jane Liu06462752017-06-27 16:41:14 -0400562
Lei Zhanga21d5932018-02-05 18:28:38 +0000563 // Check that updating the rectangle would succeed.
564 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
565 rect.left += 70.f;
566 rect.right += 70.f;
567 ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
568 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
569 EXPECT_EQ(rect.right, new_rect.right);
Jane Liu06462752017-06-27 16:41:14 -0400570
Lei Zhanga21d5932018-02-05 18:28:38 +0000571 // Check that updating the rectangle changes the square annotation's
572 // position.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000573 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000574 CompareBitmap(bitmap.get(), 612, 792, md5_modified_square);
Lei Zhanga21d5932018-02-05 18:28:38 +0000575 }
Jane Liub370e5a2017-08-16 13:24:58 -0400576
Jane Liu06462752017-06-27 16:41:14 -0400577 UnloadPage(page);
578}
Jane Liu8ce58f52017-06-29 13:40:22 -0400579
Lei Zhangab41f252018-12-23 03:10:50 +0000580TEST_F(FPDFAnnotEmbedderTest, CountAttachmentPoints) {
Henrique Nakashima5098b252018-03-26 21:46:00 +0000581 // Open a file with multiline markup annotations.
582 ASSERT_TRUE(OpenDocument("annotation_markup_multiline_no_ap.pdf"));
583 FPDF_PAGE page = LoadPage(0);
584 ASSERT_TRUE(page);
585 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000586 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5098b252018-03-26 21:46:00 +0000587 ASSERT_TRUE(annot);
588
589 // This is a three line annotation.
590 EXPECT_EQ(3u, FPDFAnnot_CountAttachmentPoints(annot.get()));
591 }
592 UnloadPage(page);
593
594 // null annotation should return 0
595 EXPECT_EQ(0u, FPDFAnnot_CountAttachmentPoints(nullptr));
596}
597
Lei Zhangab41f252018-12-23 03:10:50 +0000598TEST_F(FPDFAnnotEmbedderTest, RemoveAnnotation) {
Jane Liu8ce58f52017-06-29 13:40:22 -0400599 // Open a file with 3 annotations on its first page.
600 ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf"));
Tom Sepez507d0192018-11-07 16:37:51 +0000601 FPDF_PAGE page = LoadPageNoEvents(0);
Jane Liu8ce58f52017-06-29 13:40:22 -0400602 ASSERT_TRUE(page);
603 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
604
Jane Liu0c6b07d2017-08-15 10:50:22 -0400605 FS_RECTF rect;
Jane Liu8ce58f52017-06-29 13:40:22 -0400606
Lei Zhanga21d5932018-02-05 18:28:38 +0000607 // Check that the annotations have the expected rectangle coordinates.
608 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000609 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000610 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
611 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
612 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400613
Lei Zhanga21d5932018-02-05 18:28:38 +0000614 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000615 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000616 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
617 EXPECT_NEAR(149.8127f, rect.left, 0.001f);
618 }
619
620 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000621 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000622 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
623 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
624 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400625
626 // Check that nothing happens when attempting to remove an annotation with an
627 // out-of-bound index.
628 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4));
629 EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1));
630 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
631
632 // Remove the second annotation.
633 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1));
634 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
635 EXPECT_FALSE(FPDFPage_GetAnnot(page, 2));
636
637 // Save the document, closing the page and document.
638 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Tom Sepez507d0192018-11-07 16:37:51 +0000639 UnloadPageNoEvents(page);
Jane Liu8ce58f52017-06-29 13:40:22 -0400640
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400641 // TODO(npm): VerifySavedRendering changes annot rect dimensions by 1??
Jane Liu8ce58f52017-06-29 13:40:22 -0400642 // Open the saved document.
643 std::string new_file = GetString();
644 FPDF_FILEACCESS file_access;
645 memset(&file_access, 0, sizeof(file_access));
646 file_access.m_FileLen = new_file.size();
647 file_access.m_GetBlock = GetBlockFromString;
648 file_access.m_Param = &new_file;
649 FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr);
650 ASSERT_TRUE(new_doc);
651 FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0);
652 ASSERT_TRUE(new_page);
653
654 // Check that the saved document has 2 annotations on the first page.
655 EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page));
656
Lei Zhanga21d5932018-02-05 18:28:38 +0000657 // Check that the remaining 2 annotations are the original 1st and 3rd ones
658 // by verifying their rectangle coordinates.
659 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000660 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000661 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
662 EXPECT_NEAR(86.1971f, rect.left, 0.001f);
663 }
Jane Liu8ce58f52017-06-29 13:40:22 -0400664
Lei Zhanga21d5932018-02-05 18:28:38 +0000665 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000666 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(new_page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +0000667 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &rect));
668 EXPECT_NEAR(351.8204f, rect.left, 0.001f);
669 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400670 FPDF_ClosePage(new_page);
671 FPDF_CloseDocument(new_doc);
672}
Jane Liu8ce58f52017-06-29 13:40:22 -0400673
Lei Zhangab41f252018-12-23 03:10:50 +0000674TEST_F(FPDFAnnotEmbedderTest, AddAndModifyPath) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400675#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400676 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000677 const char md5_modified_path[] = "873b92ea83ccf006e58415d866ce145b";
678 const char md5_two_paths[] = "6f1f1c91f50240e9cc9d7c87c48b93a7";
679 const char md5_new_annot[] = "078bf58f939645ac305854f31ee9a828";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000680#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
681 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
682 const char md5_modified_path[] = "c66293426cbf1f568502d1f7c0728fc7";
683 const char md5_two_paths[] = "122ac4625393b1dfe4be8707b73cbb13";
684 const char md5_new_annot[] = "253c7fd739646ba2568e1e8bfc782336";
Jane Liubaa7ff42017-06-29 19:18:23 -0400685#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000686 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
Dan Sinclair844d79e2018-02-16 03:46:26 +0000687 const char md5_modified_path[] = "5a4a6091cff648a4ece3ce7e245e3e38";
688 const char md5_two_paths[] = "d6e4072a4415cfc6ec17201fb6be0ee0";
689 const char md5_new_annot[] = "fc338b97bf66a656916c6198697a8a28";
Jane Liubaa7ff42017-06-29 19:18:23 -0400690#endif
691
692 // Open a file with two annotations and load its first page.
693 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000694 FPDF_PAGE page = LoadPage(0);
Jane Liubaa7ff42017-06-29 19:18:23 -0400695 ASSERT_TRUE(page);
696 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
697
698 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000699 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000700 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000701 CompareBitmap(bitmap.get(), 595, 842, md5_original);
702 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400703
Lei Zhanga21d5932018-02-05 18:28:38 +0000704 {
705 // Retrieve the stamp annotation which has its AP stream already defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000706 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000707 ASSERT_TRUE(annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400708
Lei Zhanga21d5932018-02-05 18:28:38 +0000709 // Check that this annotation has one path object and retrieve it.
710 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000711 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000712 FPDF_PAGEOBJECT path = FPDFAnnot_GetObject(annot.get(), 1);
713 EXPECT_FALSE(path);
714 path = FPDFAnnot_GetObject(annot.get(), 0);
715 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(path));
716 EXPECT_TRUE(path);
Jane Liubaa7ff42017-06-29 19:18:23 -0400717
Lei Zhanga21d5932018-02-05 18:28:38 +0000718 // Modify the color of the path object.
719 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 0, 0, 0, 255));
720 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), path));
Jane Liubaa7ff42017-06-29 19:18:23 -0400721
Lei Zhanga21d5932018-02-05 18:28:38 +0000722 // Check that the page with the modified annotation renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000723 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000724 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000725 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
726 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400727
Lei Zhanga21d5932018-02-05 18:28:38 +0000728 // Add a second path object to the same annotation.
729 FPDF_PAGEOBJECT dot = FPDFPageObj_CreateNewPath(7, 84);
730 EXPECT_TRUE(FPDFPath_BezierTo(dot, 9, 86, 10, 87, 11, 88));
731 EXPECT_TRUE(FPDFPath_SetStrokeColor(dot, 255, 0, 0, 100));
732 EXPECT_TRUE(FPDFPath_SetStrokeWidth(dot, 14));
733 EXPECT_TRUE(FPDFPath_SetDrawMode(dot, 0, 1));
734 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), dot));
735 EXPECT_EQ(2, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liu7a9a38b2017-07-11 13:47:37 -0400736
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000737 // The object is in the annontation, not in the page, so the page object
738 // array should not change.
739 ASSERT_EQ(32, FPDFPage_CountObjects(page));
740
Lei Zhanga21d5932018-02-05 18:28:38 +0000741 // Check that the page with an annotation with two paths renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000742 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000743 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000744 CompareBitmap(bitmap.get(), 595, 842, md5_two_paths);
745 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400746
Lei Zhanga21d5932018-02-05 18:28:38 +0000747 // Delete the newly added path object.
748 EXPECT_TRUE(FPDFAnnot_RemoveObject(annot.get(), 1));
749 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000750 ASSERT_EQ(32, FPDFPage_CountObjects(page));
Lei Zhanga21d5932018-02-05 18:28:38 +0000751 }
Jane Liu7a9a38b2017-07-11 13:47:37 -0400752
753 // Check that the page renders the same as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000754 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000755 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000756 CompareBitmap(bitmap.get(), 595, 842, md5_modified_path);
757 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400758
Jane Liubaa7ff42017-06-29 19:18:23 -0400759 FS_RECTF rect;
Jane Liubaa7ff42017-06-29 19:18:23 -0400760
Lei Zhanga21d5932018-02-05 18:28:38 +0000761 {
762 // Create another stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000763 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000764 ASSERT_TRUE(annot);
765 rect.left = 200.f;
766 rect.bottom = 400.f;
767 rect.right = 500.f;
768 rect.top = 600.f;
769 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liubaa7ff42017-06-29 19:18:23 -0400770
Lei Zhanga21d5932018-02-05 18:28:38 +0000771 // Add a new path to the annotation.
772 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(200, 500);
773 EXPECT_TRUE(FPDFPath_LineTo(check, 300, 400));
774 EXPECT_TRUE(FPDFPath_LineTo(check, 500, 600));
775 EXPECT_TRUE(FPDFPath_MoveTo(check, 350, 550));
776 EXPECT_TRUE(FPDFPath_LineTo(check, 450, 450));
777 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 0, 255, 255, 180));
778 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
779 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
780 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), check));
781 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
782
783 // Check that the annotation's bounding box came from its rectangle.
784 FS_RECTF new_rect;
785 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
786 EXPECT_EQ(rect.left, new_rect.left);
787 EXPECT_EQ(rect.bottom, new_rect.bottom);
788 EXPECT_EQ(rect.right, new_rect.right);
789 EXPECT_EQ(rect.top, new_rect.top);
790 }
Jane Liubaa7ff42017-06-29 19:18:23 -0400791
792 // Save the document, closing the page and document.
Jane Liubaa7ff42017-06-29 19:18:23 -0400793 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000794 UnloadPage(page);
Jane Liubaa7ff42017-06-29 19:18:23 -0400795
796 // Open the saved document.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000797 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000798 page = LoadSavedPage(0);
799 VerifySavedRendering(page, 595, 842, md5_new_annot);
Jane Liubaa7ff42017-06-29 19:18:23 -0400800
Jane Liu36567742017-07-06 11:13:35 -0400801 // Check that the document has a correct count of annotations and objects.
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000802 EXPECT_EQ(3, FPDFPage_GetAnnotCount(page));
Jane Liubaa7ff42017-06-29 19:18:23 -0400803
Lei Zhanga21d5932018-02-05 18:28:38 +0000804 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000805 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000806 ASSERT_TRUE(annot);
807 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
Jane Liubaa7ff42017-06-29 19:18:23 -0400808
Lei Zhanga21d5932018-02-05 18:28:38 +0000809 // Check that the new annotation's rectangle is as defined.
810 FS_RECTF new_rect;
811 ASSERT_TRUE(FPDFAnnot_GetRect(annot.get(), &new_rect));
812 EXPECT_EQ(rect.left, new_rect.left);
813 EXPECT_EQ(rect.bottom, new_rect.bottom);
814 EXPECT_EQ(rect.right, new_rect.right);
815 EXPECT_EQ(rect.top, new_rect.top);
816 }
817
Henrique Nakashima8baea3c2017-11-10 20:27:23 +0000818 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400819 CloseSavedDocument();
Jane Liu8ce58f52017-06-29 13:40:22 -0400820}
Jane Liub137e752017-07-05 15:04:33 -0400821
Lei Zhangab41f252018-12-23 03:10:50 +0000822TEST_F(FPDFAnnotEmbedderTest, ModifyAnnotationFlags) {
Jane Liub137e752017-07-05 15:04:33 -0400823 // Open a file with an annotation and load its first page.
824 ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000825 FPDF_PAGE page = LoadPage(0);
Jane Liub137e752017-07-05 15:04:33 -0400826 ASSERT_TRUE(page);
827
828 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000829 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000830 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000831 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
832 }
Jane Liub137e752017-07-05 15:04:33 -0400833
Lei Zhanga21d5932018-02-05 18:28:38 +0000834 {
835 // Retrieve the annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000836 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +0000837 ASSERT_TRUE(annot);
Jane Liub137e752017-07-05 15:04:33 -0400838
Lei Zhanga21d5932018-02-05 18:28:38 +0000839 // Check that the original flag values are as expected.
840 int flags = FPDFAnnot_GetFlags(annot.get());
841 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
842 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400843
Lei Zhanga21d5932018-02-05 18:28:38 +0000844 // Set the HIDDEN flag.
845 flags |= FPDF_ANNOT_FLAG_HIDDEN;
846 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
847 flags = FPDFAnnot_GetFlags(annot.get());
848 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN);
849 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400850
Lei Zhanga21d5932018-02-05 18:28:38 +0000851 // Check that the page renders correctly without rendering the annotation.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000852 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000853 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000854 CompareBitmap(bitmap.get(), 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3");
855 }
Jane Liub137e752017-07-05 15:04:33 -0400856
Lei Zhanga21d5932018-02-05 18:28:38 +0000857 // Unset the HIDDEN flag.
858 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), FPDF_ANNOT_FLAG_NONE));
859 EXPECT_FALSE(FPDFAnnot_GetFlags(annot.get()));
860 flags &= ~FPDF_ANNOT_FLAG_HIDDEN;
861 EXPECT_TRUE(FPDFAnnot_SetFlags(annot.get(), flags));
862 flags = FPDFAnnot_GetFlags(annot.get());
863 EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN);
864 EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT);
Jane Liub137e752017-07-05 15:04:33 -0400865
Lei Zhanga21d5932018-02-05 18:28:38 +0000866 // Check that the page renders correctly as before.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000867 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000868 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000869 CompareBitmap(bitmap.get(), 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e");
870 }
Lei Zhanga21d5932018-02-05 18:28:38 +0000871 }
Jane Liub137e752017-07-05 15:04:33 -0400872
Jane Liub137e752017-07-05 15:04:33 -0400873 UnloadPage(page);
874}
Jane Liu36567742017-07-06 11:13:35 -0400875
Lei Zhangab41f252018-12-23 03:10:50 +0000876TEST_F(FPDFAnnotEmbedderTest, AddAndModifyImage) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400877#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400878 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
879 const char md5_new_image[] = "ff012f5697436dfcaec25b32d1333596";
880 const char md5_modified_image[] = "86cf8cb2755a7a2046a543e66d9c1e61";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000881#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
882 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
883 const char md5_new_image[] = "d19c6fcfd9a170802fcfb9adfa13557e";
884 const char md5_modified_image[] = "1273cf2363570a50d1aa0c95b1318197";
Jane Liu36567742017-07-06 11:13:35 -0400885#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000886 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
887 const char md5_new_image[] = "9ea8732dc9d579f68853f16892856208";
888 const char md5_modified_image[] = "74239d2a8c55c9de1dbb9cd8781895aa";
Jane Liu36567742017-07-06 11:13:35 -0400889#endif
890
891 // Open a file with two annotations and load its first page.
892 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000893 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400894 ASSERT_TRUE(page);
895 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
896
897 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000898 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000899 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000900 CompareBitmap(bitmap.get(), 595, 842, md5_original);
901 }
Jane Liu36567742017-07-06 11:13:35 -0400902
Jane Liu36567742017-07-06 11:13:35 -0400903 constexpr int kBitmapSize = 200;
Lei Zhanga21d5932018-02-05 18:28:38 +0000904 FPDF_BITMAP image_bitmap;
905
906 {
907 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000908 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000909 ASSERT_TRUE(annot);
910 FS_RECTF rect;
911 rect.left = 200.f;
912 rect.bottom = 600.f;
913 rect.right = 400.f;
914 rect.top = 800.f;
915 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
916
917 // Add a solid-color translucent image object to the new annotation.
918 image_bitmap = FPDFBitmap_Create(kBitmapSize, kBitmapSize, 1);
919 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
920 0xeeeecccc);
921 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(image_bitmap));
922 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(image_bitmap));
923 FPDF_PAGEOBJECT image_object = FPDFPageObj_NewImageObj(document());
924 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
925 ASSERT_TRUE(FPDFImageObj_SetMatrix(image_object, kBitmapSize, 0, 0,
926 kBitmapSize, 0, 0));
927 FPDFPageObj_Transform(image_object, 1, 0, 0, 1, 200, 600);
928 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), image_object));
929 }
Jane Liu36567742017-07-06 11:13:35 -0400930
931 // Check that the page renders correctly with the new image object.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000932 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000933 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000934 CompareBitmap(bitmap.get(), 595, 842, md5_new_image);
935 }
Jane Liu36567742017-07-06 11:13:35 -0400936
Lei Zhanga21d5932018-02-05 18:28:38 +0000937 {
938 // Retrieve the newly added stamp annotation and its image object.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000939 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +0000940 ASSERT_TRUE(annot);
941 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
942 FPDF_PAGEOBJECT image_object = FPDFAnnot_GetObject(annot.get(), 0);
943 EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image_object));
Jane Liu36567742017-07-06 11:13:35 -0400944
Lei Zhanga21d5932018-02-05 18:28:38 +0000945 // Modify the image in the new annotation.
946 FPDFBitmap_FillRect(image_bitmap, 0, 0, kBitmapSize, kBitmapSize,
947 0xff000000);
948 ASSERT_TRUE(FPDFImageObj_SetBitmap(&page, 0, image_object, image_bitmap));
949 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), image_object));
950 }
Jane Liu36567742017-07-06 11:13:35 -0400951
952 // Save the document, closing the page and document.
953 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +0000954 UnloadPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400955 FPDFBitmap_Destroy(image_bitmap);
Jane Liu36567742017-07-06 11:13:35 -0400956
957 // Test that the saved document renders the modified image object correctly.
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400958 VerifySavedDocument(595, 842, md5_modified_image);
Jane Liu36567742017-07-06 11:13:35 -0400959}
960
Lei Zhangab41f252018-12-23 03:10:50 +0000961TEST_F(FPDFAnnotEmbedderTest, AddAndModifyText) {
Dan Sinclair698aed72017-09-26 16:24:49 -0400962#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Jane Liu7a9a38b2017-07-11 13:47:37 -0400963 const char md5_original[] = "c35408717759562d1f8bf33d317483d2";
964 const char md5_new_text[] = "e5680ed048c2cfd9a1d27212cdf41286";
965 const char md5_modified_text[] = "79f5cfb0b07caaf936f65f6a7a57ce77";
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000966#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
967 const char md5_original[] = "6f3cc2dd37479ce7cc072bfb0c63c275";
968 const char md5_new_text[] = "554d625b52144816aaabb0dd66962c55";
969 const char md5_modified_text[] = "26e94fbd3af4b1e65479327507600114";
Jane Liu36567742017-07-06 11:13:35 -0400970#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000971 const char md5_original[] = "964f89bbe8911e540a465cf1a64b7f7e";
972 const char md5_new_text[] = "00b14fa2dc1c90d1b0d034e1608efef5";
973 const char md5_modified_text[] = "076c8f24a09ddc0e49f7e758edead6f0";
Jane Liu36567742017-07-06 11:13:35 -0400974#endif
975
976 // Open a file with two annotations and load its first page.
977 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +0000978 FPDF_PAGE page = LoadPage(0);
Jane Liu36567742017-07-06 11:13:35 -0400979 ASSERT_TRUE(page);
980 EXPECT_EQ(2, FPDFPage_GetAnnotCount(page));
981
982 // Check that the page renders correctly.
Lei Zhangc113c7a2018-02-12 14:58:44 +0000983 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000984 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000985 CompareBitmap(bitmap.get(), 595, 842, md5_original);
986 }
Jane Liu36567742017-07-06 11:13:35 -0400987
Lei Zhanga21d5932018-02-05 18:28:38 +0000988 {
989 // Create a stamp annotation and set its annotation rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000990 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_STAMP));
Lei Zhanga21d5932018-02-05 18:28:38 +0000991 ASSERT_TRUE(annot);
992 FS_RECTF rect;
993 rect.left = 200.f;
994 rect.bottom = 550.f;
995 rect.right = 450.f;
996 rect.top = 650.f;
997 EXPECT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect));
Jane Liu36567742017-07-06 11:13:35 -0400998
Lei Zhanga21d5932018-02-05 18:28:38 +0000999 // Add a translucent text object to the new annotation.
1000 FPDF_PAGEOBJECT text_object =
1001 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1002 EXPECT_TRUE(text_object);
1003 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1004 GetFPDFWideString(L"I'm a translucent text laying on other text.");
1005 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1006 EXPECT_TRUE(FPDFText_SetFillColor(text_object, 0, 0, 255, 150));
1007 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 600);
1008 EXPECT_TRUE(FPDFAnnot_AppendObject(annot.get(), text_object));
1009 }
Jane Liu36567742017-07-06 11:13:35 -04001010
1011 // Check that the page renders correctly with the new text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001012 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001013 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001014 CompareBitmap(bitmap.get(), 595, 842, md5_new_text);
1015 }
Jane Liu36567742017-07-06 11:13:35 -04001016
Lei Zhanga21d5932018-02-05 18:28:38 +00001017 {
1018 // Retrieve the newly added stamp annotation and its text object.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001019 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001020 ASSERT_TRUE(annot);
1021 EXPECT_EQ(1, FPDFAnnot_GetObjectCount(annot.get()));
1022 FPDF_PAGEOBJECT text_object = FPDFAnnot_GetObject(annot.get(), 0);
1023 EXPECT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_object));
Jane Liu36567742017-07-06 11:13:35 -04001024
Lei Zhanga21d5932018-02-05 18:28:38 +00001025 // Modify the text in the new annotation.
1026 std::unique_ptr<unsigned short, pdfium::FreeDeleter> new_text =
1027 GetFPDFWideString(L"New text!");
1028 EXPECT_TRUE(FPDFText_SetText(text_object, new_text.get()));
1029 EXPECT_TRUE(FPDFAnnot_UpdateObject(annot.get(), text_object));
1030 }
Jane Liu36567742017-07-06 11:13:35 -04001031
1032 // Check that the page renders correctly with the modified text object.
Lei Zhangc113c7a2018-02-12 14:58:44 +00001033 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001034 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001035 CompareBitmap(bitmap.get(), 595, 842, md5_modified_text);
1036 }
Jane Liu36567742017-07-06 11:13:35 -04001037
1038 // Remove the new annotation, and check that the page renders as before.
1039 EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 2));
Lei Zhangc113c7a2018-02-12 14:58:44 +00001040 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001041 ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001042 CompareBitmap(bitmap.get(), 595, 842, md5_original);
1043 }
Jane Liu36567742017-07-06 11:13:35 -04001044
1045 UnloadPage(page);
1046}
Jane Liu2e1a32b2017-07-06 12:01:25 -04001047
Lei Zhangab41f252018-12-23 03:10:50 +00001048TEST_F(FPDFAnnotEmbedderTest, GetSetStringValue) {
Jane Liu2e1a32b2017-07-06 12:01:25 -04001049 // Open a file with four annotations and load its first page.
1050 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001051 FPDF_PAGE page = LoadPage(0);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001052 ASSERT_TRUE(page);
1053
Lei Zhangdf064df2017-08-31 02:33:27 -07001054 static constexpr char kDateKey[] = "M";
Lei Zhanga21d5932018-02-05 18:28:38 +00001055 static constexpr wchar_t kNewDate[] = L"D:201706282359Z00'00'";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001056
Lei Zhanga21d5932018-02-05 18:28:38 +00001057 {
1058 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001059 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001060 ASSERT_TRUE(annot);
1061
1062 // Check that a non-existent key does not exist.
1063 EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "none"));
1064
1065 // Check that the string value of a non-string dictionary entry is empty.
1066 static constexpr char kApKey[] = "AP";
1067 EXPECT_TRUE(FPDFAnnot_HasKey(annot.get(), kApKey));
1068 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1069 FPDFAnnot_GetValueType(annot.get(), kApKey));
1070 EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kApKey, nullptr, 0));
1071
1072 // Check that the string value of the hash is correct.
1073 static constexpr char kHashKey[] = "AAPL:Hash";
1074 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1075 unsigned long len =
1076 FPDFAnnot_GetStringValue(annot.get(), kHashKey, nullptr, 0);
1077 std::vector<char> buf(len);
1078 EXPECT_EQ(66u,
1079 FPDFAnnot_GetStringValue(annot.get(), kHashKey, buf.data(), len));
1080 EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad",
1081 BufferToWString(buf).c_str());
1082
1083 // Check that the string value of the modified date is correct.
1084 EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot.get(), kHashKey));
1085 len = FPDFAnnot_GetStringValue(annot.get(), kDateKey, nullptr, 0);
1086 buf.clear();
1087 buf.resize(len);
1088 EXPECT_EQ(44u,
1089 FPDFAnnot_GetStringValue(annot.get(), kDateKey, buf.data(), len));
1090 EXPECT_STREQ(L"D:201706071721Z00'00'", BufferToWString(buf).c_str());
1091
1092 // Update the date entry for the annotation.
1093 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1094 GetFPDFWideString(kNewDate);
1095 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kDateKey, text.get()));
1096 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001097
1098 // Save the document, closing the page and document.
Jane Liu2e1a32b2017-07-06 12:01:25 -04001099 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001100 UnloadPage(page);
Jane Liu2e1a32b2017-07-06 12:01:25 -04001101
Dan Sinclair698aed72017-09-26 16:24:49 -04001102#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Artem Strygind24b97e2017-08-09 18:50:59 +03001103 const char md5[] = "4d64e61c9c0f8c60ab3cc3234bb73b1c";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001104#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1105 const char md5[] = "9ee141f698c3fcb56c050dffd6c82624";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001106#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001107 const char md5[] = "c96ee1f316d7f5a1b154de9f9d467f01";
Jane Liu2e1a32b2017-07-06 12:01:25 -04001108#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001109
1110 // Open the saved annotation.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001111 OpenSavedDocument(nullptr);
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001112 page = LoadSavedPage(0);
1113 VerifySavedRendering(page, 595, 842, md5);
Lei Zhanga21d5932018-02-05 18:28:38 +00001114 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001115 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Jane Liu2e1a32b2017-07-06 12:01:25 -04001116
Lei Zhanga21d5932018-02-05 18:28:38 +00001117 // Check that the string value of the modified date is the newly-set value.
1118 EXPECT_EQ(FPDF_OBJECT_STRING,
1119 FPDFAnnot_GetValueType(new_annot.get(), kDateKey));
1120 unsigned long len =
1121 FPDFAnnot_GetStringValue(new_annot.get(), kDateKey, nullptr, 0);
1122 std::vector<char> buf(len);
1123 EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot.get(), kDateKey,
1124 buf.data(), len));
1125 EXPECT_STREQ(kNewDate, BufferToWString(buf).c_str());
1126 }
Jane Liu2e1a32b2017-07-06 12:01:25 -04001127
Henrique Nakashima8baea3c2017-11-10 20:27:23 +00001128 CloseSavedPage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001129 CloseSavedDocument();
Jane Liu2e1a32b2017-07-06 12:01:25 -04001130}
Diana Gage7e0c05d2017-07-19 17:33:33 -07001131
Lei Zhangab41f252018-12-23 03:10:50 +00001132TEST_F(FPDFAnnotEmbedderTest, GetSetAP) {
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001133 // Open a file with four annotations and load its first page.
1134 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001135 FPDF_PAGE page = LoadPage(0);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001136 ASSERT_TRUE(page);
1137
Lei Zhanga21d5932018-02-05 18:28:38 +00001138 {
1139 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001140 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001141 ASSERT_TRUE(annot);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001142
Lei Zhanga21d5932018-02-05 18:28:38 +00001143 // Check that the string value of an AP returns the expected length.
1144 unsigned long normal_len = FPDFAnnot_GetAP(
1145 annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr, 0);
1146 EXPECT_EQ(73970u, normal_len);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001147
Lei Zhanga21d5932018-02-05 18:28:38 +00001148 // Check that the string value of an AP is not returned if the buffer is too
1149 // small. The result buffer should be overwritten with an empty string.
1150 std::vector<char> buf(normal_len - 1);
1151 // Write L"z" in the buffer to verify it's not overwritten.
1152 wcscpy(reinterpret_cast<wchar_t*>(buf.data()), L"z");
1153 EXPECT_EQ(73970u,
1154 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1155 buf.data(), buf.size()));
1156 std::string ap = BufferToString(buf);
1157 EXPECT_STREQ("z", ap.c_str());
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001158
Lei Zhanga21d5932018-02-05 18:28:38 +00001159 // Check that the string value of an AP is returned through a buffer that is
1160 // the right size.
1161 buf.clear();
1162 buf.resize(normal_len);
1163 EXPECT_EQ(73970u,
1164 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1165 buf.data(), buf.size()));
1166 ap = BufferToString(buf);
1167 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1168 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001169
Lei Zhanga21d5932018-02-05 18:28:38 +00001170 // Check that the string value of an AP is returned through a buffer that is
1171 // larger than necessary.
1172 buf.clear();
1173 buf.resize(normal_len + 1);
1174 EXPECT_EQ(73970u,
1175 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1176 buf.data(), buf.size()));
1177 ap = BufferToString(buf);
1178 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1179 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001180
Lei Zhanga21d5932018-02-05 18:28:38 +00001181 // Check that getting an AP for a mode that does not have an AP returns an
1182 // empty string.
1183 unsigned long rollover_len = FPDFAnnot_GetAP(
1184 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1185 EXPECT_EQ(2u, rollover_len);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001186
Lei Zhanga21d5932018-02-05 18:28:38 +00001187 buf.clear();
1188 buf.resize(1000);
1189 EXPECT_EQ(2u,
1190 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1191 buf.data(), buf.size()));
1192 EXPECT_STREQ("", BufferToString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001193
Lei Zhanga21d5932018-02-05 18:28:38 +00001194 // Check that setting the AP for an invalid appearance mode fails.
1195 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1196 GetFPDFWideString(L"new test ap");
1197 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), -1, apText.get()));
1198 EXPECT_FALSE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT,
1199 apText.get()));
1200 EXPECT_FALSE(FPDFAnnot_SetAP(
1201 annot.get(), FPDF_ANNOT_APPEARANCEMODE_COUNT + 1, apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001202
Lei Zhanga21d5932018-02-05 18:28:38 +00001203 // Set the AP correctly now.
1204 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1205 apText.get()));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001206
Lei Zhanga21d5932018-02-05 18:28:38 +00001207 // Check that the new annotation value is equal to the value we just set.
1208 rollover_len = FPDFAnnot_GetAP(
1209 annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1210 EXPECT_EQ(24u, rollover_len);
1211 buf.clear();
1212 buf.resize(rollover_len);
1213 EXPECT_EQ(24u,
1214 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1215 buf.data(), buf.size()));
1216 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
Henrique Nakashima5970a472018-01-11 22:40:59 +00001217
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 // Check that the Normal AP was not touched when the Rollover AP was set.
1219 buf.clear();
1220 buf.resize(normal_len);
1221 EXPECT_EQ(73970u,
1222 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1223 buf.data(), buf.size()));
1224 ap = BufferToString(buf);
1225 EXPECT_THAT(ap, testing::StartsWith("q Q q 7.442786 w 2 J"));
1226 EXPECT_THAT(ap, testing::EndsWith("c 716.5381 327.7156 l S Q Q"));
1227 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001228
1229 // Save the modified document, then reopen it.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001230 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Lei Zhang75c81712018-02-08 17:22:39 +00001231 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001232
Tom Sepezb9c3e272018-08-14 18:22:06 +00001233 OpenSavedDocument(nullptr);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001234 page = LoadSavedPage(0);
Lei Zhanga21d5932018-02-05 18:28:38 +00001235 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001236 ScopedFPDFAnnotation new_annot(FPDFPage_GetAnnot(page, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001237
Lei Zhanga21d5932018-02-05 18:28:38 +00001238 // Check that the new annotation value is equal to the value we set before
1239 // saving.
1240 unsigned long rollover_len = FPDFAnnot_GetAP(
1241 new_annot.get(), FPDF_ANNOT_APPEARANCEMODE_ROLLOVER, nullptr, 0);
1242 EXPECT_EQ(24u, rollover_len);
1243 std::vector<char> buf(rollover_len);
1244 EXPECT_EQ(24u, FPDFAnnot_GetAP(new_annot.get(),
1245 FPDF_ANNOT_APPEARANCEMODE_ROLLOVER,
1246 buf.data(), buf.size()));
1247 EXPECT_STREQ(L"new test ap", BufferToWString(buf).c_str());
1248 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001249
1250 // Close saved document.
Henrique Nakashima5970a472018-01-11 22:40:59 +00001251 CloseSavedPage(page);
1252 CloseSavedDocument();
1253}
1254
Lei Zhangab41f252018-12-23 03:10:50 +00001255TEST_F(FPDFAnnotEmbedderTest, RemoveOptionalAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001256 // Open a file with four annotations and load its first page.
1257 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001258 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001259 ASSERT_TRUE(page);
1260
Lei Zhanga21d5932018-02-05 18:28:38 +00001261 {
1262 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001263 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001264 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001265
Lei Zhanga21d5932018-02-05 18:28:38 +00001266 // Set Down AP. Normal AP is already set.
1267 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1268 GetFPDFWideString(L"new test ap");
1269 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1270 apText.get()));
1271 EXPECT_EQ(73970u,
1272 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1273 nullptr, 0));
1274 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1275 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001276
Lei Zhanga21d5932018-02-05 18:28:38 +00001277 // Check that setting the Down AP to null removes the Down entry but keeps
1278 // Normal intact.
1279 EXPECT_TRUE(
1280 FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN, nullptr));
1281 EXPECT_EQ(73970u,
1282 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1283 nullptr, 0));
1284 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1285 nullptr, 0));
1286 }
Henrique Nakashima5970a472018-01-11 22:40:59 +00001287
Lei Zhang75c81712018-02-08 17:22:39 +00001288 UnloadPage(page);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001289}
1290
Lei Zhangab41f252018-12-23 03:10:50 +00001291TEST_F(FPDFAnnotEmbedderTest, RemoveRequiredAP) {
Henrique Nakashima5970a472018-01-11 22:40:59 +00001292 // Open a file with four annotations and load its first page.
1293 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001294 FPDF_PAGE page = LoadPage(0);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001295 ASSERT_TRUE(page);
1296
Lei Zhanga21d5932018-02-05 18:28:38 +00001297 {
1298 // Retrieve the first annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001299 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001300 ASSERT_TRUE(annot);
Henrique Nakashima5970a472018-01-11 22:40:59 +00001301
Lei Zhanga21d5932018-02-05 18:28:38 +00001302 // Set Down AP. Normal AP is already set.
1303 std::unique_ptr<unsigned short, pdfium::FreeDeleter> apText =
1304 GetFPDFWideString(L"new test ap");
1305 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1306 apText.get()));
1307 EXPECT_EQ(73970u,
1308 FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1309 nullptr, 0));
1310 EXPECT_EQ(24u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1311 nullptr, 0));
Henrique Nakashima5970a472018-01-11 22:40:59 +00001312
Lei Zhanga21d5932018-02-05 18:28:38 +00001313 // Check that setting the Normal AP to null removes the whole AP dictionary.
1314 EXPECT_TRUE(FPDFAnnot_SetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1315 nullptr));
1316 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_NORMAL,
1317 nullptr, 0));
1318 EXPECT_EQ(2u, FPDFAnnot_GetAP(annot.get(), FPDF_ANNOT_APPEARANCEMODE_DOWN,
1319 nullptr, 0));
1320 }
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001321
Lei Zhang75c81712018-02-08 17:22:39 +00001322 UnloadPage(page);
Henrique Nakashimaa74e75d2018-01-10 18:06:55 +00001323}
1324
Lei Zhangab41f252018-12-23 03:10:50 +00001325TEST_F(FPDFAnnotEmbedderTest, ExtractLinkedAnnotations) {
Jane Liu300bb272017-08-21 14:37:53 -04001326 // Open a file with annotations and load its first page.
1327 ASSERT_TRUE(OpenDocument("annotation_highlight_square_with_ap.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001328 FPDF_PAGE page = LoadPage(0);
Jane Liu300bb272017-08-21 14:37:53 -04001329 ASSERT_TRUE(page);
Jane Liud1ed1ce2017-08-24 12:31:10 -04001330 EXPECT_EQ(-1, FPDFPage_GetAnnotIndex(page, nullptr));
Jane Liu300bb272017-08-21 14:37:53 -04001331
Lei Zhanga21d5932018-02-05 18:28:38 +00001332 {
1333 // Retrieve the highlight annotation which has its popup defined.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001334 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001335 ASSERT_TRUE(annot);
1336 EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot.get()));
1337 EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot.get()));
1338 static constexpr char kPopupKey[] = "Popup";
1339 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPopupKey));
1340 ASSERT_EQ(FPDF_OBJECT_REFERENCE,
1341 FPDFAnnot_GetValueType(annot.get(), kPopupKey));
Jane Liu300bb272017-08-21 14:37:53 -04001342
Lei Zhanga21d5932018-02-05 18:28:38 +00001343 // Retrieve and verify the popup of the highlight annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001344 ScopedFPDFAnnotation popup(
Lei Zhanga21d5932018-02-05 18:28:38 +00001345 FPDFAnnot_GetLinkedAnnot(annot.get(), kPopupKey));
1346 ASSERT_TRUE(popup);
1347 EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup.get()));
1348 EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup.get()));
1349 FS_RECTF rect;
1350 ASSERT_TRUE(FPDFAnnot_GetRect(popup.get(), &rect));
1351 EXPECT_NEAR(612.0f, rect.left, 0.001f);
1352 EXPECT_NEAR(578.792, rect.bottom, 0.001f);
Jane Liu300bb272017-08-21 14:37:53 -04001353
Lei Zhanga21d5932018-02-05 18:28:38 +00001354 // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
1355 // since "IRT" is not a key in |annot|'s dictionary.
1356 static constexpr char kIRTKey[] = "IRT";
1357 ASSERT_FALSE(FPDFAnnot_HasKey(annot.get(), kIRTKey));
1358 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kIRTKey));
Jane Liu300bb272017-08-21 14:37:53 -04001359
Lei Zhanga21d5932018-02-05 18:28:38 +00001360 // Attempting to retrieve |annot|'s parent dictionary as an annotation
1361 // would fail, since its parent is not an annotation.
1362 static constexpr char kPKey[] = "P";
1363 ASSERT_TRUE(FPDFAnnot_HasKey(annot.get(), kPKey));
1364 EXPECT_EQ(FPDF_OBJECT_REFERENCE,
1365 FPDFAnnot_GetValueType(annot.get(), kPKey));
1366 EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot.get(), kPKey));
1367 }
Jane Liu300bb272017-08-21 14:37:53 -04001368
Jane Liu300bb272017-08-21 14:37:53 -04001369 UnloadPage(page);
1370}
1371
Lei Zhangab41f252018-12-23 03:10:50 +00001372TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsTextField) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001373 // Open file with form text fields.
1374 ASSERT_TRUE(OpenDocument("text_form_multiple.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001375 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001376 ASSERT_TRUE(page);
1377
Lei Zhanga21d5932018-02-05 18:28:38 +00001378 {
1379 // Retrieve the first annotation: user-editable text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001380 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001381 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001382
Lei Zhanga21d5932018-02-05 18:28:38 +00001383 // Check that the flag values are as expected.
1384 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1385 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1386 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001387
Lei Zhanga21d5932018-02-05 18:28:38 +00001388 {
1389 // Retrieve the second annotation: read-only text field.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001390 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001391 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001392
Lei Zhanga21d5932018-02-05 18:28:38 +00001393 // Check that the flag values are as expected.
1394 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1395 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1396 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001397
1398 UnloadPage(page);
1399}
1400
Lei Zhangab41f252018-12-23 03:10:50 +00001401TEST_F(FPDFAnnotEmbedderTest, GetFormFieldFlagsComboBox) {
Diana Gage7e0c05d2017-07-19 17:33:33 -07001402 // Open file with form text fields.
1403 ASSERT_TRUE(OpenDocument("combobox_form.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001404 FPDF_PAGE page = LoadPage(0);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001405 ASSERT_TRUE(page);
1406
Lei Zhanga21d5932018-02-05 18:28:38 +00001407 {
1408 // Retrieve the first annotation: user-editable combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001409 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 0));
Lei Zhanga21d5932018-02-05 18:28:38 +00001410 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001411
Lei Zhanga21d5932018-02-05 18:28:38 +00001412 // Check that the flag values are as expected.
1413 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1414 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1415 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1416 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1417 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001418
Lei Zhanga21d5932018-02-05 18:28:38 +00001419 {
1420 // Retrieve the second annotation: regular combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001421 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 1));
Lei Zhanga21d5932018-02-05 18:28:38 +00001422 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001423
Lei Zhanga21d5932018-02-05 18:28:38 +00001424 // Check that the flag values are as expected.
1425 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1426 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1427 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1428 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1429 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001430
Lei Zhanga21d5932018-02-05 18:28:38 +00001431 {
1432 // Retrieve the third annotation: read-only combobox.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001433 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, 2));
Lei Zhanga21d5932018-02-05 18:28:38 +00001434 ASSERT_TRUE(annot);
Diana Gage7e0c05d2017-07-19 17:33:33 -07001435
Lei Zhanga21d5932018-02-05 18:28:38 +00001436 // Check that the flag values are as expected.
1437 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1438 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1439 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1440 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1441 }
Diana Gage7e0c05d2017-07-19 17:33:33 -07001442
1443 UnloadPage(page);
1444}
Diana Gage40870db2017-07-19 18:16:03 -07001445
Lei Zhangab41f252018-12-23 03:10:50 +00001446TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotNull) {
Diana Gage40870db2017-07-19 18:16:03 -07001447 // Open file with form text fields.
1448 EXPECT_TRUE(OpenDocument("text_form.pdf"));
1449 FPDF_PAGE page = LoadPage(0);
1450 ASSERT_TRUE(page);
1451
1452 // Attempt to get an annotation where no annotation exists on page.
Lei Zhang7557e7b2018-09-14 17:02:40 +00001453 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 0, 0));
1454
1455 {
1456 // Verify there is an annotation.
1457 ScopedFPDFAnnotation annot(
1458 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 120, 120));
1459 EXPECT_TRUE(annot);
1460 }
1461
1462 // Try other bad inputs at a valid location.
1463 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, nullptr, 120, 120));
1464 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(nullptr, page, 120, 120));
1465 EXPECT_FALSE(FPDFAnnot_GetFormFieldAtPoint(form_handle(), nullptr, 120, 120));
Diana Gage40870db2017-07-19 18:16:03 -07001466
1467 UnloadPage(page);
1468}
1469
Lei Zhangab41f252018-12-23 03:10:50 +00001470TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsTextField) {
Diana Gage40870db2017-07-19 18:16:03 -07001471 // Open file with form text fields.
1472 EXPECT_TRUE(OpenDocument("text_form_multiple.pdf"));
1473 FPDF_PAGE page = LoadPage(0);
1474 ASSERT_TRUE(page);
1475
Lei Zhanga21d5932018-02-05 18:28:38 +00001476 {
1477 // Retrieve user-editable text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001478 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001479 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 118));
1480 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001481
Lei Zhanga21d5932018-02-05 18:28:38 +00001482 // Check that interactive form annotation flag values are as expected.
1483 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1484 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1485 }
Diana Gage40870db2017-07-19 18:16:03 -07001486
Lei Zhanga21d5932018-02-05 18:28:38 +00001487 {
1488 // Retrieve read-only text field annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001489 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001490 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 105, 202));
1491 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001492
Lei Zhanga21d5932018-02-05 18:28:38 +00001493 // Check that interactive form annotation flag values are as expected.
1494 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1495 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1496 }
Diana Gage40870db2017-07-19 18:16:03 -07001497
1498 UnloadPage(page);
1499}
1500
Lei Zhangab41f252018-12-23 03:10:50 +00001501TEST_F(FPDFAnnotEmbedderTest, GetFormAnnotAndCheckFlagsComboBox) {
Diana Gage40870db2017-07-19 18:16:03 -07001502 // Open file with form comboboxes.
1503 EXPECT_TRUE(OpenDocument("combobox_form.pdf"));
1504 FPDF_PAGE page = LoadPage(0);
1505 ASSERT_TRUE(page);
1506
Lei Zhanga21d5932018-02-05 18:28:38 +00001507 {
1508 // Retrieve user-editable combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001509 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001510 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 363));
Lei Zhanga21d5932018-02-05 18:28:38 +00001511 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001512
Lei Zhanga21d5932018-02-05 18:28:38 +00001513 // Check that interactive form annotation flag values are as expected.
1514 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1515 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1516 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1517 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1518 }
Diana Gage40870db2017-07-19 18:16:03 -07001519
Lei Zhanga21d5932018-02-05 18:28:38 +00001520 {
1521 // Retrieve regular combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001522 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001523 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 413));
Lei Zhanga21d5932018-02-05 18:28:38 +00001524 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001525
Lei Zhanga21d5932018-02-05 18:28:38 +00001526 // Check that interactive form annotation flag values are as expected.
1527 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1528 EXPECT_FALSE(flags & FPDF_FORMFLAG_READONLY);
1529 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1530 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1531 }
Diana Gage40870db2017-07-19 18:16:03 -07001532
Lei Zhanga21d5932018-02-05 18:28:38 +00001533 {
1534 // Retrieve read-only combobox annotation.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001535 ScopedFPDFAnnotation annot(
Henrique Nakashima20830922018-03-19 21:21:46 +00001536 FPDFAnnot_GetFormFieldAtPoint(form_handle(), page, 102, 513));
Lei Zhanga21d5932018-02-05 18:28:38 +00001537 ASSERT_TRUE(annot);
Diana Gage40870db2017-07-19 18:16:03 -07001538
Lei Zhanga21d5932018-02-05 18:28:38 +00001539 // Check that interactive form annotation flag values are as expected.
1540 int flags = FPDFAnnot_GetFormFieldFlags(page, annot.get());
1541 EXPECT_TRUE(flags & FPDF_FORMFLAG_READONLY);
1542 EXPECT_TRUE(flags & FPDF_FORMFLAG_CHOICE_COMBO);
1543 EXPECT_FALSE(flags & FPDF_FORMFLAG_CHOICE_EDIT);
1544 }
Diana Gage40870db2017-07-19 18:16:03 -07001545
1546 UnloadPage(page);
1547}
Lei Zhangf5fcd9e2018-12-23 03:11:50 +00001548
1549TEST_F(FPDFAnnotEmbedderTest, BUG_1212) {
1550 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1551 FPDF_PAGE page = LoadPage(0);
1552 ASSERT_TRUE(page);
1553 EXPECT_EQ(0, FPDFPage_GetAnnotCount(page));
1554
1555 static const char kTestKey[] = "test";
1556 static constexpr wchar_t kData[] = L"\xf6\xe4";
1557 std::vector<char> buf(12);
1558
1559 {
1560 // Add a text annotation to the page.
1561 ScopedFPDFAnnotation annot(FPDFPage_CreateAnnot(page, FPDF_ANNOT_TEXT));
1562 ASSERT_TRUE(annot);
1563 EXPECT_EQ(1, FPDFPage_GetAnnotCount(page));
1564 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1565
1566 // Make sure there is no test key, add set a value there, and read it back.
1567 std::fill(buf.begin(), buf.end(), 'x');
1568 ASSERT_EQ(2u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1569 buf.size()));
1570 EXPECT_STREQ(L"", BufferToWString(buf).c_str());
1571
1572 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1573 GetFPDFWideString(kData);
1574 EXPECT_TRUE(FPDFAnnot_SetStringValue(annot.get(), kTestKey, text.get()));
1575
1576 std::fill(buf.begin(), buf.end(), 'x');
1577 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1578 buf.size()));
1579 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1580 }
1581
1582 UnloadPage(page);
1583
1584 {
1585 // Save a copy, open the copy, and check the annotation again.
1586 // Note that it renders the rotation.
1587 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1588 OpenSavedDocument(nullptr);
1589 FPDF_PAGE saved_page = LoadSavedPage(0);
1590 ASSERT_TRUE(saved_page);
1591
1592 EXPECT_EQ(1, FPDFPage_GetAnnotCount(saved_page));
1593 {
1594 ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(saved_page, 0));
1595 ASSERT_TRUE(annot);
1596 EXPECT_EQ(FPDF_ANNOT_TEXT, FPDFAnnot_GetSubtype(annot.get()));
1597
1598 std::fill(buf.begin(), buf.end(), 'x');
1599 ASSERT_EQ(6u, FPDFAnnot_GetStringValue(annot.get(), kTestKey, buf.data(),
1600 buf.size()));
1601 EXPECT_STREQ(kData, BufferToWString(buf).c_str());
1602 }
1603
1604 CloseSavedPage(saved_page);
1605 CloseSavedDocument();
1606 }
1607}