1 // Copyright 2015 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <math.h>
6
7 #include <limits>
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "build/build_config.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fxge/cfx_defaultrenderdevice.h"
16 #include "fpdfsdk/cpdfsdk_helpers.h"
17 #include "fpdfsdk/fpdf_view_c_api_test.h"
18 #include "public/cpp/fpdf_scopers.h"
19 #include "public/fpdfview.h"
20 #include "testing/embedder_test.h"
21 #include "testing/embedder_test_constants.h"
22 #include "testing/embedder_test_environment.h"
23 #include "testing/fx_string_testhelpers.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/utils/file_util.h"
26 #include "testing/utils/hash.h"
27 #include "testing/utils/path_service.h"
28 #include "third_party/base/check.h"
29
30 #if defined(_SKIA_SUPPORT_)
31 #include "third_party/skia/include/core/SkCanvas.h" // nogncheck
32 #include "third_party/skia/include/core/SkColor.h" // nogncheck
33 #include "third_party/skia/include/core/SkColorType.h" // nogncheck
34 #include "third_party/skia/include/core/SkImage.h" // nogncheck
35 #include "third_party/skia/include/core/SkImageInfo.h" // nogncheck
36 #include "third_party/skia/include/core/SkPicture.h" // nogncheck
37 #include "third_party/skia/include/core/SkPictureRecorder.h" // nogncheck
38 #include "third_party/skia/include/core/SkRefCnt.h" // nogncheck
39 #include "third_party/skia/include/core/SkSize.h" // nogncheck
40 #include "third_party/skia/include/core/SkSurface.h" // nogncheck
41 #endif // defined(_SKIA_SUPPORT_)
42
43 using pdfium::ManyRectanglesChecksum;
44
45 namespace {
46
47 constexpr char kFirstAlternate[] = "FirstAlternate";
48 constexpr char kLastAlternate[] = "LastAlternate";
49
50 #if BUILDFLAG(IS_WIN)
51 const char kExpectedRectanglePostScript[] = R"(
52 save
53 /im/initmatrix load def
54 /n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load def/h/closepath load def
55 /f/fill load def/F/eofill load def/s/stroke load def/W/clip load def/W*/eoclip load def
56 /rg/setrgbcolor load def/k/setcmykcolor load def
57 /J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load def/M/setmiterlimit load def/d/setdash load def
58 /q/gsave load def/Q/grestore load def/iM/imagemask load def
59 /Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont load def
60 /cm/concat load def/Cm/currentmatrix load def/mx/matrix load def/sm/setmatrix load def
61 0 300 m 0 0 l 200 0 l 200 300 l 0 300 l h W n
62 q
63 0 300 m 0 0 l 200 0 l 200 300 l 0 300 l h W n
64 q
65 0 J
66 []0 d
67 0 j
68 1 w
69 10 M
70 mx Cm [1 0 0 -1 0 300]cm 0 290 m 10 290 l 10 300 l 0 300 l 0 290 l h 0 0 0 rg
71 q F Q s sm
72 mx Cm [1 0 0 -1 0 300]cm 10 150 m 60 150 l 60 180 l 10 180 l 10 150 l h q F Q s sm
73 mx Cm [1 0 0 -1 0 300]cm 190 290 m 200 290 l 200 300 l 190 300 l 190 290 l h 0 0 1 rg
74 q F Q 0 0 0 rg
75 s sm
76 mx Cm [1 0 0 -1 0 300]cm 70 232 m 120 232 l 120 262 l 70 262 l 70 232 l h 0 0 1 rg
77 q F Q 0 0 0 rg
78 s sm
79 mx Cm [1 0 0 -1 0 300]cm 190 0 m 200 0 l 200 10 l 190 10 l 190 0 l h 0 1 0 rg
80 q F Q 0 0 0 rg
81 s sm
82 mx Cm [1 0 0 -1 0 300]cm 130 150 m 180 150 l 180 180 l 130 180 l 130 150 l h 0 1 0 rg
83 q F Q 0 0 0 rg
84 s sm
85 mx Cm [1 0 0 -1 0 300]cm 0 0 m 10 0 l 10 10 l 0 10 l 0 0 l h 1 0 0 rg
86 q F Q 0 0 0 rg
87 s sm
88 mx Cm [1 0 0 -1 0 300]cm 70 67 m 120 67 l 120 97 l 70 97 l 70 67 l h 1 0 0 rg
89 q F Q 0 0 0 rg
90 s sm
91 Q
92 Q
93 Q
94
95 restore
96 )";
97 #endif // BUILDFLAG(IS_WIN)
98
99 class MockDownloadHints final : public FX_DOWNLOADHINTS {
100 public:
SAddSegment(FX_DOWNLOADHINTS * pThis,size_t offset,size_t size)101 static void SAddSegment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
102 }
103
MockDownloadHints()104 MockDownloadHints() {
105 FX_DOWNLOADHINTS::version = 1;
106 FX_DOWNLOADHINTS::AddSegment = SAddSegment;
107 }
108
109 ~MockDownloadHints() = default;
110 };
111
112 #if defined(_SKIA_SUPPORT_)
SkImageToPdfiumBitmap(const SkImage & image)113 ScopedFPDFBitmap SkImageToPdfiumBitmap(const SkImage& image) {
114 ScopedFPDFBitmap bitmap(
115 FPDFBitmap_Create(image.width(), image.height(), /*alpha=*/1));
116 if (!bitmap) {
117 ADD_FAILURE() << "Could not create FPDF_BITMAP";
118 return nullptr;
119 }
120
121 if (!image.readPixels(/*context=*/nullptr,
122 image.imageInfo().makeColorType(kBGRA_8888_SkColorType),
123 FPDFBitmap_GetBuffer(bitmap.get()),
124 FPDFBitmap_GetStride(bitmap.get()),
125 /*srcX=*/0, /*srcY=*/0)) {
126 ADD_FAILURE() << "Could not read pixels from SkImage";
127 return nullptr;
128 }
129
130 return bitmap;
131 }
132
SkPictureToPdfiumBitmap(sk_sp<SkPicture> picture,const SkISize & size)133 ScopedFPDFBitmap SkPictureToPdfiumBitmap(sk_sp<SkPicture> picture,
134 const SkISize& size) {
135 sk_sp<SkSurface> surface =
136 SkSurfaces::Raster(SkImageInfo::MakeN32Premul(size));
137 if (!surface) {
138 ADD_FAILURE() << "Could not create SkSurface";
139 return nullptr;
140 }
141
142 surface->getCanvas()->clear(SK_ColorWHITE);
143 surface->getCanvas()->drawPicture(picture);
144 sk_sp<SkImage> image = surface->makeImageSnapshot();
145 if (!image) {
146 ADD_FAILURE() << "Could not snapshot SkSurface";
147 return nullptr;
148 }
149
150 return SkImageToPdfiumBitmap(*image);
151 }
152 #endif // defined(_SKIA_SUPPORT_)
153
154 } // namespace
155
TEST(fpdf,CApiTest)156 TEST(fpdf, CApiTest) {
157 EXPECT_TRUE(CheckPDFiumCApi());
158 }
159
160 class FPDFViewEmbedderTest : public EmbedderTest {
161 protected:
TestRenderPageBitmapWithMatrix(FPDF_PAGE page,int bitmap_width,int bitmap_height,const FS_MATRIX & matrix,const FS_RECTF & rect,const char * expected_checksum)162 void TestRenderPageBitmapWithMatrix(FPDF_PAGE page,
163 int bitmap_width,
164 int bitmap_height,
165 const FS_MATRIX& matrix,
166 const FS_RECTF& rect,
167 const char* expected_checksum) {
168 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(bitmap_width, bitmap_height, 0));
169 FPDFBitmap_FillRect(bitmap.get(), 0, 0, bitmap_width, bitmap_height,
170 0xFFFFFFFF);
171 FPDF_RenderPageBitmapWithMatrix(bitmap.get(), page, &matrix, &rect, 0);
172 CompareBitmap(bitmap.get(), bitmap_width, bitmap_height, expected_checksum);
173 }
174
TestRenderPageBitmapWithFlags(FPDF_PAGE page,int flags,const char * expected_checksum)175 void TestRenderPageBitmapWithFlags(FPDF_PAGE page,
176 int flags,
177 const char* expected_checksum) {
178 int bitmap_width = static_cast<int>(FPDF_GetPageWidth(page));
179 int bitmap_height = static_cast<int>(FPDF_GetPageHeight(page));
180 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(bitmap_width, bitmap_height, 0));
181 FPDFBitmap_FillRect(bitmap.get(), 0, 0, bitmap_width, bitmap_height,
182 0xFFFFFFFF);
183 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, bitmap_width, bitmap_height,
184 0, flags);
185 CompareBitmap(bitmap.get(), bitmap_width, bitmap_height, expected_checksum);
186 }
187
TestRenderPageBitmapWithInternalMemory(FPDF_PAGE page,int format,const char * expected_checksum)188 void TestRenderPageBitmapWithInternalMemory(FPDF_PAGE page,
189 int format,
190 const char* expected_checksum) {
191 TestRenderPageBitmapWithInternalMemoryAndStride(
192 page, format, /*bitmap_stride=*/0, expected_checksum);
193 }
194
TestRenderPageBitmapWithInternalMemoryAndStride(FPDF_PAGE page,int format,int bitmap_stride,const char * expected_checksum)195 void TestRenderPageBitmapWithInternalMemoryAndStride(
196 FPDF_PAGE page,
197 int format,
198 int bitmap_stride,
199 const char* expected_checksum) {
200 int bitmap_width = static_cast<int>(FPDF_GetPageWidth(page));
201 int bitmap_height = static_cast<int>(FPDF_GetPageHeight(page));
202 int bytes_per_pixel = BytesPerPixelForFormat(format);
203 ASSERT_NE(0, bytes_per_pixel);
204
205 ScopedFPDFBitmap bitmap(FPDFBitmap_CreateEx(
206 bitmap_width, bitmap_height, format, nullptr, bitmap_stride));
207 RenderPageToBitmapAndCheck(page, bitmap.get(), expected_checksum);
208 }
209
TestRenderPageBitmapWithExternalMemory(FPDF_PAGE page,int format,const char * expected_checksum)210 void TestRenderPageBitmapWithExternalMemory(FPDF_PAGE page,
211 int format,
212 const char* expected_checksum) {
213 int bitmap_width = static_cast<int>(FPDF_GetPageWidth(page));
214 int bytes_per_pixel = BytesPerPixelForFormat(format);
215 ASSERT_NE(0, bytes_per_pixel);
216
217 int bitmap_stride = bytes_per_pixel * bitmap_width;
218 return TestRenderPageBitmapWithExternalMemoryImpl(
219 page, format, bitmap_stride, expected_checksum);
220 }
221
TestRenderPageBitmapWithExternalMemoryAndNoStride(FPDF_PAGE page,int format,const char * expected_checksum)222 void TestRenderPageBitmapWithExternalMemoryAndNoStride(
223 FPDF_PAGE page,
224 int format,
225 const char* expected_checksum) {
226 return TestRenderPageBitmapWithExternalMemoryImpl(
227 page, format, /*bitmap_stride=*/0, expected_checksum);
228 }
229
230 #if defined(_SKIA_SUPPORT_)
TestRenderPageSkp(FPDF_PAGE page,const char * expected_checksum)231 void TestRenderPageSkp(FPDF_PAGE page, const char* expected_checksum) {
232 int width = static_cast<int>(FPDF_GetPageWidth(page));
233 int height = static_cast<int>(FPDF_GetPageHeight(page));
234
235 sk_sp<SkPicture> picture;
236 {
237 auto recorder = std::make_unique<SkPictureRecorder>();
238 recorder->beginRecording(width, height);
239
240 FPDF_RenderPageSkia(
241 reinterpret_cast<FPDF_SKIA_CANVAS>(recorder->getRecordingCanvas()),
242 page, width, height);
243 picture = recorder->finishRecordingAsPicture();
244 ASSERT_TRUE(picture);
245 }
246
247 ScopedFPDFBitmap bitmap = SkPictureToPdfiumBitmap(
248 std::move(picture), SkISize::Make(width, height));
249 CompareBitmap(bitmap.get(), width, height, expected_checksum);
250 }
251 #endif // defined(_SKIA_SUPPORT_)
252
253 private:
TestRenderPageBitmapWithExternalMemoryImpl(FPDF_PAGE page,int format,int bitmap_stride,const char * expected_checksum)254 void TestRenderPageBitmapWithExternalMemoryImpl(
255 FPDF_PAGE page,
256 int format,
257 int bitmap_stride,
258 const char* expected_checksum) {
259 int bitmap_width = static_cast<int>(FPDF_GetPageWidth(page));
260 int bitmap_height = static_cast<int>(FPDF_GetPageHeight(page));
261
262 std::vector<uint8_t> external_memory(bitmap_stride * bitmap_height);
263 ScopedFPDFBitmap bitmap(FPDFBitmap_CreateEx(bitmap_width, bitmap_height,
264 format, external_memory.data(),
265 bitmap_stride));
266 RenderPageToBitmapAndCheck(page, bitmap.get(), expected_checksum);
267 }
268
RenderPageToBitmapAndCheck(FPDF_PAGE page,FPDF_BITMAP bitmap,const char * expected_checksum)269 void RenderPageToBitmapAndCheck(FPDF_PAGE page,
270 FPDF_BITMAP bitmap,
271 const char* expected_checksum) {
272 int bitmap_width = FPDFBitmap_GetWidth(bitmap);
273 int bitmap_height = FPDFBitmap_GetHeight(bitmap);
274 EXPECT_EQ(bitmap_width, static_cast<int>(FPDF_GetPageWidth(page)));
275 EXPECT_EQ(bitmap_height, static_cast<int>(FPDF_GetPageHeight(page)));
276 FPDFBitmap_FillRect(bitmap, 0, 0, bitmap_width, bitmap_height, 0xFFFFFFFF);
277 FPDF_RenderPageBitmap(bitmap, page, 0, 0, bitmap_width, bitmap_height, 0,
278 0);
279 CompareBitmap(bitmap, bitmap_width, bitmap_height, expected_checksum);
280 }
281 };
282
283 // Test for conversion of a point in device coordinates to page coordinates
TEST_F(FPDFViewEmbedderTest,DeviceCoordinatesToPageCoordinates)284 TEST_F(FPDFViewEmbedderTest, DeviceCoordinatesToPageCoordinates) {
285 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
286 FPDF_PAGE page = LoadPage(0);
287 EXPECT_TRUE(page);
288
289 // Error tolerance for floating point comparison
290 const double kTolerance = 0.0001;
291
292 // Display bounds in device coordinates
293 int start_x = 0;
294 int start_y = 0;
295 int size_x = 640;
296 int size_y = 480;
297
298 // Page Orientation normal
299 int rotate = 0;
300
301 // Device coordinate to be converted
302 int device_x = 10;
303 int device_y = 10;
304
305 double page_x = 0.0;
306 double page_y = 0.0;
307 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
308 device_x, device_y, &page_x, &page_y));
309 EXPECT_NEAR(9.5625, page_x, kTolerance);
310 EXPECT_NEAR(775.5, page_y, kTolerance);
311
312 // Rotate 90 degrees clockwise
313 rotate = 1;
314 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
315 device_x, device_y, &page_x, &page_y));
316 EXPECT_NEAR(12.75, page_x, kTolerance);
317 EXPECT_NEAR(12.375, page_y, kTolerance);
318
319 // Rotate 180 degrees
320 rotate = 2;
321 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
322 device_x, device_y, &page_x, &page_y));
323 EXPECT_NEAR(602.4374, page_x, kTolerance);
324 EXPECT_NEAR(16.5, page_y, kTolerance);
325
326 // Rotate 90 degrees counter-clockwise
327 rotate = 3;
328 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
329 device_x, device_y, &page_x, &page_y));
330 EXPECT_NEAR(599.25, page_x, kTolerance);
331 EXPECT_NEAR(779.625, page_y, kTolerance);
332
333 // FPDF_DeviceToPage() converts |rotate| into legal rotation by taking
334 // modulo by 4. A value of 4 is expected to be converted into 0 (normal
335 // rotation)
336 rotate = 4;
337 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
338 device_x, device_y, &page_x, &page_y));
339 EXPECT_NEAR(9.5625, page_x, kTolerance);
340 EXPECT_NEAR(775.5, page_y, kTolerance);
341
342 // FPDF_DeviceToPage returns untransformed coordinates if |rotate| % 4 is
343 // negative.
344 rotate = -1;
345 EXPECT_TRUE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
346 device_x, device_y, &page_x, &page_y));
347 EXPECT_NEAR(device_x, page_x, kTolerance);
348 EXPECT_NEAR(device_y, page_y, kTolerance);
349
350 // Negative case - invalid page
351 page_x = 1234.0;
352 page_y = 5678.0;
353 EXPECT_FALSE(FPDF_DeviceToPage(nullptr, start_x, start_y, size_x, size_y,
354 rotate, device_x, device_y, &page_x, &page_y));
355 // Out parameters are expected to remain unchanged
356 EXPECT_NEAR(1234.0, page_x, kTolerance);
357 EXPECT_NEAR(5678.0, page_y, kTolerance);
358
359 // Negative case - invalid output parameters
360 EXPECT_FALSE(FPDF_DeviceToPage(page, start_x, start_y, size_x, size_y, rotate,
361 device_x, device_y, nullptr, nullptr));
362
363 UnloadPage(page);
364 }
365
366 // Test for conversion of a point in page coordinates to device coordinates.
TEST_F(FPDFViewEmbedderTest,PageCoordinatesToDeviceCoordinates)367 TEST_F(FPDFViewEmbedderTest, PageCoordinatesToDeviceCoordinates) {
368 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
369 FPDF_PAGE page = LoadPage(0);
370 EXPECT_TRUE(page);
371
372 // Display bounds in device coordinates
373 int start_x = 0;
374 int start_y = 0;
375 int size_x = 640;
376 int size_y = 480;
377
378 // Page Orientation normal
379 int rotate = 0;
380
381 // Page coordinate to be converted
382 double page_x = 9.0;
383 double page_y = 775.0;
384
385 int device_x = 0;
386 int device_y = 0;
387 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
388 page_x, page_y, &device_x, &device_y));
389
390 EXPECT_EQ(9, device_x);
391 EXPECT_EQ(10, device_y);
392
393 // Rotate 90 degrees clockwise
394 rotate = 1;
395 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
396 page_x, page_y, &device_x, &device_y));
397 EXPECT_EQ(626, device_x);
398 EXPECT_EQ(7, device_y);
399
400 // Rotate 180 degrees
401 rotate = 2;
402 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
403 page_x, page_y, &device_x, &device_y));
404 EXPECT_EQ(631, device_x);
405 EXPECT_EQ(470, device_y);
406
407 // Rotate 90 degrees counter-clockwise
408 rotate = 3;
409 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
410 page_x, page_y, &device_x, &device_y));
411 EXPECT_EQ(14, device_x);
412 EXPECT_EQ(473, device_y);
413
414 // FPDF_PageToDevice() converts |rotate| into legal rotation by taking
415 // modulo by 4. A value of 4 is expected to be converted into 0 (normal
416 // rotation)
417 rotate = 4;
418 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
419 page_x, page_y, &device_x, &device_y));
420 EXPECT_EQ(9, device_x);
421 EXPECT_EQ(10, device_y);
422
423 // FPDF_PageToDevice() returns untransformed coordinates if |rotate| % 4 is
424 // negative.
425 rotate = -1;
426 EXPECT_TRUE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
427 page_x, page_y, &device_x, &device_y));
428 EXPECT_EQ(start_x, device_x);
429 EXPECT_EQ(start_y, device_y);
430
431 // Negative case - invalid page
432 device_x = 1234;
433 device_y = 5678;
434 EXPECT_FALSE(FPDF_PageToDevice(nullptr, start_x, start_y, size_x, size_y,
435 rotate, page_x, page_y, &device_x, &device_y));
436 // Out parameters are expected to remain unchanged
437 EXPECT_EQ(1234, device_x);
438 EXPECT_EQ(5678, device_y);
439
440 // Negative case - invalid output parameters
441 EXPECT_FALSE(FPDF_PageToDevice(page, start_x, start_y, size_x, size_y, rotate,
442 page_x, page_y, nullptr, nullptr));
443
444 UnloadPage(page);
445 }
446
TEST_F(FPDFViewEmbedderTest,MultipleInitDestroy)447 TEST_F(FPDFViewEmbedderTest, MultipleInitDestroy) {
448 FPDF_InitLibrary(); // Redundant given SetUp() in environment, but safe.
449 FPDF_InitLibrary(); // Doubly-redundant even, but safe.
450
451 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
452 CloseDocument();
453 CloseDocument(); // Redundant given above, but safe.
454 CloseDocument(); // Doubly-redundant even, but safe.
455
456 FPDF_DestroyLibrary(); // Doubly-Redundant even, but safe.
457 FPDF_DestroyLibrary(); // Redundant given call to TearDown(), but safe.
458
459 EmbedderTestEnvironment::GetInstance()->TearDown();
460 EmbedderTestEnvironment::GetInstance()->SetUp();
461 }
462
TEST_F(FPDFViewEmbedderTest,RepeatedInitDestroy)463 TEST_F(FPDFViewEmbedderTest, RepeatedInitDestroy) {
464 for (int i = 0; i < 3; ++i) {
465 if (!OpenDocument("about_blank.pdf"))
466 ADD_FAILURE();
467 CloseDocument();
468
469 FPDF_DestroyLibrary();
470 FPDF_InitLibrary();
471 }
472
473 // Puts the test environment back the way it was.
474 EmbedderTestEnvironment::GetInstance()->TearDown();
475 EmbedderTestEnvironment::GetInstance()->SetUp();
476 }
477
TEST_F(FPDFViewEmbedderTest,Document)478 TEST_F(FPDFViewEmbedderTest, Document) {
479 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
480 EXPECT_EQ(1, GetPageCount());
481 EXPECT_EQ(0, GetFirstPageNum());
482
483 int version;
484 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
485 EXPECT_EQ(14, version);
486
487 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
488 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
489 CloseDocument();
490
491 // Safe to open again and do the same things all over again.
492 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
493 EXPECT_EQ(1, GetPageCount());
494 EXPECT_EQ(0, GetFirstPageNum());
495
496 version = 42;
497 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
498 EXPECT_EQ(14, version);
499
500 EXPECT_EQ(0xFFFFFFFF, FPDF_GetDocPermissions(document()));
501 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
502 // CloseDocument() called by TearDown().
503 }
504
TEST_F(FPDFViewEmbedderTest,LoadDocument64)505 TEST_F(FPDFViewEmbedderTest, LoadDocument64) {
506 std::string file_path;
507 ASSERT_TRUE(PathService::GetTestFilePath("about_blank.pdf", &file_path));
508
509 size_t file_length = 0;
510 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
511 GetFileContents(file_path.c_str(), &file_length);
512 DCHECK(file_contents);
513 ScopedFPDFDocument doc(
514 FPDF_LoadMemDocument64(file_contents.get(), file_length, nullptr));
515 ASSERT_TRUE(doc);
516
517 int version;
518 EXPECT_TRUE(FPDF_GetFileVersion(doc.get(), &version));
519 EXPECT_EQ(14, version);
520 }
521
TEST_F(FPDFViewEmbedderTest,LoadNonexistentDocument)522 TEST_F(FPDFViewEmbedderTest, LoadNonexistentDocument) {
523 FPDF_DOCUMENT doc = FPDF_LoadDocument("nonexistent_document.pdf", "");
524 ASSERT_FALSE(doc);
525 EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
526 }
527
TEST_F(FPDFViewEmbedderTest,DocumentWithNoPageCount)528 TEST_F(FPDFViewEmbedderTest, DocumentWithNoPageCount) {
529 ASSERT_TRUE(OpenDocument("no_page_count.pdf"));
530 ASSERT_EQ(6, FPDF_GetPageCount(document()));
531 }
532
TEST_F(FPDFViewEmbedderTest,DocumentWithEmptyPageTreeNode)533 TEST_F(FPDFViewEmbedderTest, DocumentWithEmptyPageTreeNode) {
534 ASSERT_TRUE(OpenDocument("page_tree_empty_node.pdf"));
535 ASSERT_EQ(2, FPDF_GetPageCount(document()));
536 }
537
538 // See https://crbug.com/pdfium/465
TEST_F(FPDFViewEmbedderTest,EmptyDocument)539 TEST_F(FPDFViewEmbedderTest, EmptyDocument) {
540 CreateEmptyDocument();
541 {
542 int version = 42;
543 EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
544 EXPECT_EQ(0, version);
545 }
546 {
547 #ifdef PDF_ENABLE_XFA
548 const unsigned long kExpected = static_cast<uint32_t>(-1);
549 #else // PDF_ENABLE_XFA
550 const unsigned long kExpected = 0;
551 #endif // PDF_ENABLE_XFA
552 EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
553 }
554 EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
555 EXPECT_EQ(0, FPDF_GetPageCount(document()));
556 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
557 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
558 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
559
560 char buf[100];
561 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
562 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
563 EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
564 }
565
TEST_F(FPDFViewEmbedderTest,SandboxDocument)566 TEST_F(FPDFViewEmbedderTest, SandboxDocument) {
567 uint16_t buf[200];
568 unsigned long len;
569
570 CreateEmptyDocument();
571 len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
572 EXPECT_GT(len, 2u); // Not just "double NUL" end-of-string indicator.
573 EXPECT_NE(0u, buf[0]);
574 CloseDocument();
575
576 FPDF_SetSandBoxPolicy(FPDF_POLICY_MACHINETIME_ACCESS, false);
577 CreateEmptyDocument();
578 len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
579 EXPECT_EQ(2u, len); // Only a "double NUL" end-of-string indicator.
580 EXPECT_EQ(0u, buf[0]);
581 CloseDocument();
582
583 constexpr unsigned long kNoSuchPolicy = 102;
584 FPDF_SetSandBoxPolicy(kNoSuchPolicy, true);
585 CreateEmptyDocument();
586 len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
587 EXPECT_EQ(2u, len); // Only a "double NUL" end-of-string indicator.
588 EXPECT_EQ(0u, buf[0]);
589 CloseDocument();
590
591 FPDF_SetSandBoxPolicy(FPDF_POLICY_MACHINETIME_ACCESS, true);
592 CreateEmptyDocument();
593 len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
594 EXPECT_GT(len, 2u); // Not just "double NUL" end-of-string indicator.
595 EXPECT_NE(0u, buf[0]);
596 CloseDocument();
597 }
598
TEST_F(FPDFViewEmbedderTest,LinearizedDocument)599 TEST_F(FPDFViewEmbedderTest, LinearizedDocument) {
600 ASSERT_TRUE(OpenDocumentLinearized("feature_linearized_loading.pdf"));
601 int version;
602 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
603 EXPECT_EQ(16, version);
604 }
605
TEST_F(FPDFViewEmbedderTest,LoadCustomDocumentWithoutFileAccess)606 TEST_F(FPDFViewEmbedderTest, LoadCustomDocumentWithoutFileAccess) {
607 EXPECT_FALSE(FPDF_LoadCustomDocument(nullptr, ""));
608 }
609
610 // See https://crbug.com/pdfium/1261
TEST_F(FPDFViewEmbedderTest,LoadCustomDocumentWithShortLivedFileAccess)611 TEST_F(FPDFViewEmbedderTest, LoadCustomDocumentWithShortLivedFileAccess) {
612 std::string file_contents_string; // Must outlive |doc|.
613 ScopedFPDFDocument doc;
614 {
615 // Read a PDF, and copy it into |file_contents_string|.
616 std::string pdf_path;
617 size_t pdf_length;
618 ASSERT_TRUE(PathService::GetTestFilePath("rectangles.pdf", &pdf_path));
619 auto file_contents = GetFileContents(pdf_path.c_str(), &pdf_length);
620 ASSERT_TRUE(file_contents);
621 for (size_t i = 0; i < pdf_length; ++i)
622 file_contents_string.push_back(file_contents.get()[i]);
623
624 // Define a FPDF_FILEACCESS object that will go out of scope, while the
625 // loaded document in |doc| remains valid.
626 FPDF_FILEACCESS file_access = {};
627 file_access.m_FileLen = pdf_length;
628 file_access.m_GetBlock = GetBlockFromString;
629 file_access.m_Param = &file_contents_string;
630 doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr));
631 ASSERT_TRUE(doc);
632 }
633
634 // Now try to access |doc| and make sure it still works.
635 ScopedFPDFPage page(FPDF_LoadPage(doc.get(), 0));
636 ASSERT_TRUE(page);
637 EXPECT_FLOAT_EQ(200.0f, FPDF_GetPageWidthF(page.get()));
638 EXPECT_FLOAT_EQ(300.0f, FPDF_GetPageHeightF(page.get()));
639 }
640
TEST_F(FPDFViewEmbedderTest,Page)641 TEST_F(FPDFViewEmbedderTest, Page) {
642 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
643 FPDF_PAGE page = LoadPage(0);
644 EXPECT_TRUE(page);
645
646 EXPECT_FLOAT_EQ(612.0f, FPDF_GetPageWidthF(page));
647 EXPECT_FLOAT_EQ(792.0f, FPDF_GetPageHeightF(page));
648
649 FS_RECTF rect;
650 EXPECT_TRUE(FPDF_GetPageBoundingBox(page, &rect));
651 EXPECT_EQ(0.0, rect.left);
652 EXPECT_EQ(0.0, rect.bottom);
653 EXPECT_EQ(612.0, rect.right);
654 EXPECT_EQ(792.0, rect.top);
655
656 // Null arguments return errors rather than crashing,
657 EXPECT_EQ(0.0, FPDF_GetPageWidth(nullptr));
658 EXPECT_EQ(0.0, FPDF_GetPageHeight(nullptr));
659 EXPECT_FALSE(FPDF_GetPageBoundingBox(nullptr, &rect));
660 EXPECT_FALSE(FPDF_GetPageBoundingBox(page, nullptr));
661
662 UnloadPage(page);
663 EXPECT_FALSE(LoadPage(1));
664 }
665
TEST_F(FPDFViewEmbedderTest,ViewerRefDummy)666 TEST_F(FPDFViewEmbedderTest, ViewerRefDummy) {
667 ASSERT_TRUE(OpenDocument("about_blank.pdf"));
668 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
669 EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
670 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
671
672 char buf[100];
673 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
674 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
675
676 FPDF_PAGERANGE page_range = FPDF_VIEWERREF_GetPrintPageRange(document());
677 EXPECT_FALSE(page_range);
678 EXPECT_EQ(0U, FPDF_VIEWERREF_GetPrintPageRangeCount(page_range));
679 EXPECT_EQ(-1, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 0));
680 EXPECT_EQ(-1, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 1));
681 }
682
TEST_F(FPDFViewEmbedderTest,ViewerRef)683 TEST_F(FPDFViewEmbedderTest, ViewerRef) {
684 ASSERT_TRUE(OpenDocument("viewer_ref.pdf"));
685 EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
686 EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
687 EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
688
689 // Test some corner cases.
690 char buf[100];
691 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
692 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
693 EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
694
695 // Make sure |buf| does not get written into when it appears to be too small.
696 // NOLINTNEXTLINE(runtime/printf)
697 strcpy(buf, "ABCD");
698 EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
699 EXPECT_STREQ("ABCD", buf);
700
701 // Note "Foo" is a different key from "foo".
702 EXPECT_EQ(4U,
703 FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
704 ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
705 EXPECT_STREQ("foo", buf);
706
707 // Try to retrieve a boolean and an integer.
708 EXPECT_EQ(
709 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
710 EXPECT_EQ(0U,
711 FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
712
713 // Try more valid cases.
714 ASSERT_EQ(4U,
715 FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
716 EXPECT_STREQ("R2L", buf);
717 ASSERT_EQ(8U,
718 FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
719 EXPECT_STREQ("CropBox", buf);
720
721 FPDF_PAGERANGE page_range = FPDF_VIEWERREF_GetPrintPageRange(document());
722 EXPECT_TRUE(page_range);
723 EXPECT_EQ(4U, FPDF_VIEWERREF_GetPrintPageRangeCount(page_range));
724 EXPECT_EQ(0, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 0));
725 EXPECT_EQ(2, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 1));
726 EXPECT_EQ(4, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 2));
727 EXPECT_EQ(4, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 3));
728 EXPECT_EQ(-1, FPDF_VIEWERREF_GetPrintPageRangeElement(page_range, 4));
729 }
730
TEST_F(FPDFViewEmbedderTest,NamedDests)731 TEST_F(FPDFViewEmbedderTest, NamedDests) {
732 ASSERT_TRUE(OpenDocument("named_dests.pdf"));
733 EXPECT_EQ(6u, FPDF_CountNamedDests(document()));
734
735 long buffer_size;
736 char fixed_buffer[512];
737 FPDF_DEST dest;
738
739 // Query the size of the first item.
740 buffer_size = 2000000; // Absurdly large, check not used for this case.
741 dest = FPDF_GetNamedDest(document(), 0, nullptr, &buffer_size);
742 EXPECT_TRUE(dest);
743 EXPECT_EQ(12, buffer_size);
744
745 // Try to retrieve the first item with too small a buffer.
746 buffer_size = 10;
747 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
748 EXPECT_TRUE(dest);
749 EXPECT_EQ(-1, buffer_size);
750
751 // Try to retrieve the first item with correctly sized buffer. Item is
752 // taken from Dests NameTree in named_dests.pdf.
753 buffer_size = 12;
754 dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, &buffer_size);
755 EXPECT_TRUE(dest);
756 EXPECT_EQ(12, buffer_size);
757 EXPECT_EQ("First",
758 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(fixed_buffer)));
759
760 // Try to retrieve the second item with ample buffer. Item is taken
761 // from Dests NameTree but has a sub-dictionary in named_dests.pdf.
762 buffer_size = sizeof(fixed_buffer);
763 dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, &buffer_size);
764 EXPECT_TRUE(dest);
765 EXPECT_EQ(10, buffer_size);
766 EXPECT_EQ("Next",
767 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(fixed_buffer)));
768
769 // Try to retrieve third item with ample buffer. Item is taken
770 // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf.
771 // in named_dests.pdf).
772 buffer_size = sizeof(fixed_buffer);
773 dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, &buffer_size);
774 EXPECT_FALSE(dest);
775 EXPECT_EQ(sizeof(fixed_buffer),
776 static_cast<size_t>(buffer_size)); // unmodified.
777
778 // Try to retrieve the forth item with ample buffer. Item is taken
779 // from Dests NameTree but has a vale of the wrong type in named_dests.pdf.
780 buffer_size = sizeof(fixed_buffer);
781 dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, &buffer_size);
782 EXPECT_FALSE(dest);
783 EXPECT_EQ(sizeof(fixed_buffer),
784 static_cast<size_t>(buffer_size)); // unmodified.
785
786 // Try to retrieve fifth item with ample buffer. Item taken from the
787 // old-style Dests dictionary object in named_dests.pdf.
788 buffer_size = sizeof(fixed_buffer);
789 dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, &buffer_size);
790 EXPECT_TRUE(dest);
791 EXPECT_EQ(30, buffer_size);
792 EXPECT_EQ(kFirstAlternate,
793 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(fixed_buffer)));
794
795 // Try to retrieve sixth item with ample buffer. Item istaken from the
796 // old-style Dests dictionary object but has a sub-dictionary in
797 // named_dests.pdf.
798 buffer_size = sizeof(fixed_buffer);
799 dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, &buffer_size);
800 EXPECT_TRUE(dest);
801 EXPECT_EQ(28, buffer_size);
802 EXPECT_EQ(kLastAlternate,
803 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(fixed_buffer)));
804
805 // Try to retrieve non-existent item with ample buffer.
806 buffer_size = sizeof(fixed_buffer);
807 dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, &buffer_size);
808 EXPECT_FALSE(dest);
809 EXPECT_EQ(sizeof(fixed_buffer),
810 static_cast<size_t>(buffer_size)); // unmodified.
811
812 // Try to underflow/overflow the integer index.
813 buffer_size = sizeof(fixed_buffer);
814 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::max(),
815 fixed_buffer, &buffer_size);
816 EXPECT_FALSE(dest);
817 EXPECT_EQ(sizeof(fixed_buffer),
818 static_cast<size_t>(buffer_size)); // unmodified.
819
820 buffer_size = sizeof(fixed_buffer);
821 dest = FPDF_GetNamedDest(document(), std::numeric_limits<int>::min(),
822 fixed_buffer, &buffer_size);
823 EXPECT_FALSE(dest);
824 EXPECT_EQ(sizeof(fixed_buffer),
825 static_cast<size_t>(buffer_size)); // unmodified.
826
827 buffer_size = sizeof(fixed_buffer);
828 dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, &buffer_size);
829 EXPECT_FALSE(dest);
830 EXPECT_EQ(sizeof(fixed_buffer),
831 static_cast<size_t>(buffer_size)); // unmodified.
832 }
833
TEST_F(FPDFViewEmbedderTest,NamedDestsByName)834 TEST_F(FPDFViewEmbedderTest, NamedDestsByName) {
835 ASSERT_TRUE(OpenDocument("named_dests.pdf"));
836
837 // Null pointer returns nullptr.
838 FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr);
839 EXPECT_FALSE(dest);
840
841 // Empty string returns nullptr.
842 dest = FPDF_GetNamedDestByName(document(), "");
843 EXPECT_FALSE(dest);
844
845 // Item from Dests NameTree.
846 dest = FPDF_GetNamedDestByName(document(), "First");
847 EXPECT_TRUE(dest);
848
849 long ignore_len = 0;
850 FPDF_DEST dest_by_index =
851 FPDF_GetNamedDest(document(), 0, nullptr, &ignore_len);
852 EXPECT_EQ(dest_by_index, dest);
853
854 // Item from Dests dictionary.
855 dest = FPDF_GetNamedDestByName(document(), kFirstAlternate);
856 EXPECT_TRUE(dest);
857
858 ignore_len = 0;
859 dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, &ignore_len);
860 EXPECT_EQ(dest_by_index, dest);
861
862 // Bad value type for item from Dests NameTree array.
863 dest = FPDF_GetNamedDestByName(document(), "WrongType");
864 EXPECT_FALSE(dest);
865
866 // No such destination in either Dest NameTree or dictionary.
867 dest = FPDF_GetNamedDestByName(document(), "Bogus");
868 EXPECT_FALSE(dest);
869 }
870
TEST_F(FPDFViewEmbedderTest,NamedDestsOldStyle)871 TEST_F(FPDFViewEmbedderTest, NamedDestsOldStyle) {
872 ASSERT_TRUE(OpenDocument("named_dests_old_style.pdf"));
873 EXPECT_EQ(2u, FPDF_CountNamedDests(document()));
874
875 // Test bad parameters.
876 EXPECT_FALSE(FPDF_GetNamedDestByName(document(), nullptr));
877 EXPECT_FALSE(FPDF_GetNamedDestByName(document(), ""));
878 EXPECT_FALSE(FPDF_GetNamedDestByName(document(), "NoSuchName"));
879
880 // These should return a valid destination.
881 EXPECT_TRUE(FPDF_GetNamedDestByName(document(), kFirstAlternate));
882 EXPECT_TRUE(FPDF_GetNamedDestByName(document(), kLastAlternate));
883
884 char buffer[512];
885 constexpr long kBufferSize = sizeof(buffer);
886 long size = kBufferSize;
887
888 // Test bad indices.
889 EXPECT_FALSE(FPDF_GetNamedDest(document(), -1, buffer, &size));
890 EXPECT_EQ(kBufferSize, size);
891 size = kBufferSize;
892 EXPECT_FALSE(FPDF_GetNamedDest(document(), 2, buffer, &size));
893 EXPECT_EQ(kBufferSize, size);
894
895 // These should return a valid destination.
896 size = kBufferSize;
897 ASSERT_TRUE(FPDF_GetNamedDest(document(), 0, buffer, &size));
898 ASSERT_EQ(static_cast<int>(sizeof(kFirstAlternate) * 2), size);
899 EXPECT_EQ(kFirstAlternate,
900 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buffer)));
901 size = kBufferSize;
902 ASSERT_TRUE(FPDF_GetNamedDest(document(), 1, buffer, &size));
903 ASSERT_EQ(static_cast<int>(sizeof(kLastAlternate) * 2), size);
904 EXPECT_EQ(kLastAlternate,
905 GetPlatformString(reinterpret_cast<FPDF_WIDESTRING>(buffer)));
906 }
907
908 // The following tests pass if the document opens without crashing.
TEST_F(FPDFViewEmbedderTest,Crasher_113)909 TEST_F(FPDFViewEmbedderTest, Crasher_113) {
910 ASSERT_TRUE(OpenDocument("bug_113.pdf"));
911 }
912
TEST_F(FPDFViewEmbedderTest,Crasher_451830)913 TEST_F(FPDFViewEmbedderTest, Crasher_451830) {
914 // Document is damaged and can't be opened.
915 EXPECT_FALSE(OpenDocument("bug_451830.pdf"));
916 }
917
TEST_F(FPDFViewEmbedderTest,Crasher_452455)918 TEST_F(FPDFViewEmbedderTest, Crasher_452455) {
919 ASSERT_TRUE(OpenDocument("bug_452455.pdf"));
920 FPDF_PAGE page = LoadPage(0);
921 EXPECT_TRUE(page);
922 UnloadPage(page);
923 }
924
TEST_F(FPDFViewEmbedderTest,Crasher_454695)925 TEST_F(FPDFViewEmbedderTest, Crasher_454695) {
926 // Document is damaged and can't be opened.
927 EXPECT_FALSE(OpenDocument("bug_454695.pdf"));
928 }
929
TEST_F(FPDFViewEmbedderTest,Crasher_572871)930 TEST_F(FPDFViewEmbedderTest, Crasher_572871) {
931 ASSERT_TRUE(OpenDocument("bug_572871.pdf"));
932 }
933
934 // It tests that document can still be loaded even the trailer has no 'Size'
935 // field if other information is right.
TEST_F(FPDFViewEmbedderTest,Failed_213)936 TEST_F(FPDFViewEmbedderTest, Failed_213) {
937 ASSERT_TRUE(OpenDocument("bug_213.pdf"));
938 }
939
940 // The following tests pass if the document opens without infinite looping.
TEST_F(FPDFViewEmbedderTest,Hang_298)941 TEST_F(FPDFViewEmbedderTest, Hang_298) {
942 EXPECT_FALSE(OpenDocument("bug_298.pdf"));
943 }
944
TEST_F(FPDFViewEmbedderTest,Crasher_773229)945 TEST_F(FPDFViewEmbedderTest, Crasher_773229) {
946 ASSERT_TRUE(OpenDocument("bug_773229.pdf"));
947 }
948
949 // Test if the document opens without infinite looping.
950 // Previously this test will hang in a loop inside LoadAllCrossRefV4. After
951 // the fix, LoadAllCrossRefV4 will return false after detecting a cross
952 // reference loop. Cross references will be rebuilt successfully.
TEST_F(FPDFViewEmbedderTest,CrossRefV4Loop)953 TEST_F(FPDFViewEmbedderTest, CrossRefV4Loop) {
954 ASSERT_TRUE(OpenDocument("bug_xrefv4_loop.pdf"));
955 MockDownloadHints hints;
956
957 // Make sure calling FPDFAvail_IsDocAvail() on this file does not infinite
958 // loop either. See bug 875.
959 int ret = PDF_DATA_NOTAVAIL;
960 while (ret == PDF_DATA_NOTAVAIL)
961 ret = FPDFAvail_IsDocAvail(avail(), &hints);
962 EXPECT_EQ(PDF_DATA_AVAIL, ret);
963 }
964
965 // The test should pass when circular references to ParseIndirectObject will not
966 // cause infinite loop.
TEST_F(FPDFViewEmbedderTest,Hang_343)967 TEST_F(FPDFViewEmbedderTest, Hang_343) {
968 EXPECT_FALSE(OpenDocument("bug_343.pdf"));
969 }
970
971 // The test should pass when the absence of 'Contents' field in a signature
972 // dictionary will not cause an infinite loop in CPDF_SyntaxParser::GetObject().
TEST_F(FPDFViewEmbedderTest,Hang_344)973 TEST_F(FPDFViewEmbedderTest, Hang_344) {
974 EXPECT_FALSE(OpenDocument("bug_344.pdf"));
975 }
976
977 // The test should pass when there is no infinite recursion in
978 // CPDF_SyntaxParser::GetString().
TEST_F(FPDFViewEmbedderTest,Hang_355)979 TEST_F(FPDFViewEmbedderTest, Hang_355) {
980 EXPECT_FALSE(OpenDocument("bug_355.pdf"));
981 }
982 // The test should pass even when the file has circular references to pages.
TEST_F(FPDFViewEmbedderTest,Hang_360)983 TEST_F(FPDFViewEmbedderTest, Hang_360) {
984 EXPECT_FALSE(OpenDocument("bug_360.pdf"));
985 }
986
987 // Deliberately damaged version of linearized.pdf with bad data in the shared
988 // object hint table.
TEST_F(FPDFViewEmbedderTest,Hang_1055)989 TEST_F(FPDFViewEmbedderTest, Hang_1055) {
990 ASSERT_TRUE(OpenDocumentLinearized("linearized_bug_1055.pdf"));
991 int version;
992 EXPECT_TRUE(FPDF_GetFileVersion(document(), &version));
993 EXPECT_EQ(16, version);
994 }
995
TEST_F(FPDFViewEmbedderTest,FPDF_RenderPageBitmapWithMatrix)996 TEST_F(FPDFViewEmbedderTest, FPDF_RenderPageBitmapWithMatrix) {
997 const char* clipped_checksum = []() {
998 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
999 return "d2929fae285593cd1c1d446750d47d60";
1000 return "a84cab93c102b9b9290fba3047ba702c";
1001 }();
1002 const char* top_left_quarter_checksum = []() {
1003 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1004 return "31d24d8c6a2bac380b2f5c393e77ecc9";
1005 return "f11a11137c8834389e31cf555a4a6979";
1006 }();
1007 const char* hori_stretched_checksum = []() {
1008 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1009 return "6d3776d7bb21cbb7195126b8e95dfba2";
1010 return "48ef9205941ed19691ccfa00d717187e";
1011 }();
1012 const char* rotated_90_clockwise_checksum = []() {
1013 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1014 return "b4baa001d201baed576cd6d5d0d5a160";
1015 return "d8da2c7bf77521550d0f2752b9cf3482";
1016 }();
1017 const char* rotated_180_clockwise_checksum = []() {
1018 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1019 return "51819227d0863222aed366d5d7c5d9c8";
1020 return "0113386bb0bd45125bacc6dee78bfe78";
1021 }();
1022 const char* rotated_270_clockwise_checksum = []() {
1023 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1024 return "f2b046e46c2751cebc777a9725ae2f3e";
1025 return "a287e0f74ce203699cda89f9cc97a240";
1026 }();
1027 const char* mirror_hori_checksum = []() {
1028 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1029 return "c7fbec322b4fc6bcf46ec1eb89661c41";
1030 return "6e8d7a6fde39d8e720fb9e620102918c";
1031 }();
1032 const char* mirror_vert_checksum = []() {
1033 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1034 return "a8b00bc40677a73c15a08b9769d1b576";
1035 return "8f3a555ef9c0d5031831ae3715273707";
1036 }();
1037 const char* larger_top_left_quarter_checksum = []() {
1038 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1039 return "35deb5ed4b73675ce33f68328a33c687";
1040 return "172a2f4adafbadbe98017b1c025b9e27";
1041 }();
1042 const char* larger_rotated_diagonal_checksum = []() {
1043 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1044 return "85c41bb892c1a09882f432aa2f4a5ef6";
1045 return "3d62417468bdaff0eb14391a0c30a3b1";
1046 }();
1047 const char* tile_checksum = []() {
1048 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1049 return "387be3a84774f39aaa955314d2fe7106";
1050 return "0a190003c97220bf8877684c8d7e89cf";
1051 }();
1052 const char kLargerChecksum[] = "c806145641c3e6fc4e022c7065343749";
1053 const char kLargerClippedChecksum[] = "091d3b1c7933c8f6945eb2cb41e588e9";
1054 const char kLargerRotatedChecksum[] = "115f13353ebfc82ddb392d1f0059eb12";
1055 const char kLargerRotatedLandscapeChecksum[] =
1056 "c901239d17d84ac84cb6f2124da71b0d";
1057
1058 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1059 FPDF_PAGE page = LoadPage(0);
1060 ASSERT_TRUE(page);
1061 const float page_width = FPDF_GetPageWidthF(page);
1062 const float page_height = FPDF_GetPageHeightF(page);
1063 EXPECT_FLOAT_EQ(200, page_width);
1064 EXPECT_FLOAT_EQ(300, page_height);
1065
1066 using pdfium::RectanglesChecksum;
1067 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
1068 CompareBitmap(bitmap.get(), page_width, page_height, RectanglesChecksum());
1069
1070 FS_RECTF page_rect{0, 0, page_width, page_height};
1071
1072 // Try rendering with an identity matrix. The output should be the same as
1073 // the RenderLoadedPage() output.
1074 FS_MATRIX identity_matrix{1, 0, 0, 1, 0, 0};
1075 TestRenderPageBitmapWithMatrix(page, page_width, page_height, identity_matrix,
1076 page_rect, RectanglesChecksum());
1077
1078 // Again render with an identity matrix but with a smaller clipping rect.
1079 FS_RECTF middle_of_page_rect{page_width / 4, page_height / 4,
1080 page_width * 3 / 4, page_height * 3 / 4};
1081 TestRenderPageBitmapWithMatrix(page, page_width, page_height, identity_matrix,
1082 middle_of_page_rect, clipped_checksum);
1083
1084 // Now render again with the image scaled smaller.
1085 FS_MATRIX half_scale_matrix{0.5, 0, 0, 0.5, 0, 0};
1086 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1087 half_scale_matrix, page_rect,
1088 top_left_quarter_checksum);
1089
1090 // Now render again with the image scaled larger horizontally (the right half
1091 // will be clipped).
1092 FS_MATRIX stretch_x_matrix{2, 0, 0, 1, 0, 0};
1093 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1094 stretch_x_matrix, page_rect,
1095 hori_stretched_checksum);
1096
1097 // Try a 90 degree rotation clockwise but with the same bitmap size, so part
1098 // will be clipped.
1099 FS_MATRIX rotate_90_matrix{0, 1, -1, 0, page_width, 0};
1100 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1101 rotate_90_matrix, page_rect,
1102 rotated_90_clockwise_checksum);
1103
1104 // 180 degree rotation clockwise.
1105 FS_MATRIX rotate_180_matrix{-1, 0, 0, -1, page_width, page_height};
1106 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1107 rotate_180_matrix, page_rect,
1108 rotated_180_clockwise_checksum);
1109
1110 // 270 degree rotation clockwise.
1111 FS_MATRIX rotate_270_matrix{0, -1, 1, 0, 0, page_width};
1112 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1113 rotate_270_matrix, page_rect,
1114 rotated_270_clockwise_checksum);
1115
1116 // Mirror horizontally.
1117 FS_MATRIX mirror_hori_matrix{-1, 0, 0, 1, page_width, 0};
1118 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1119 mirror_hori_matrix, page_rect,
1120 mirror_hori_checksum);
1121
1122 // Mirror vertically.
1123 FS_MATRIX mirror_vert_matrix{1, 0, 0, -1, 0, page_height};
1124 TestRenderPageBitmapWithMatrix(page, page_width, page_height,
1125 mirror_vert_matrix, page_rect,
1126 mirror_vert_checksum);
1127
1128 // Tests rendering to a larger bitmap
1129 const float bitmap_width = page_width * 2;
1130 const float bitmap_height = page_height * 2;
1131
1132 // Render using an identity matrix and the whole bitmap area as clipping rect.
1133 FS_RECTF bitmap_rect{0, 0, bitmap_width, bitmap_height};
1134 TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
1135 identity_matrix, bitmap_rect,
1136 larger_top_left_quarter_checksum);
1137
1138 // Render using a scaling matrix to fill the larger bitmap.
1139 FS_MATRIX double_scale_matrix{2, 0, 0, 2, 0, 0};
1140 TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
1141 double_scale_matrix, bitmap_rect,
1142 kLargerChecksum);
1143
1144 // Render the larger image again but with clipping.
1145 FS_RECTF middle_of_bitmap_rect{bitmap_width / 4, bitmap_height / 4,
1146 bitmap_width * 3 / 4, bitmap_height * 3 / 4};
1147 TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
1148 double_scale_matrix, middle_of_bitmap_rect,
1149 kLargerClippedChecksum);
1150
1151 // On the larger bitmap, try a 90 degree rotation but with the same bitmap
1152 // size, so part will be clipped.
1153 FS_MATRIX rotate_90_scale_2_matrix{0, 2, -2, 0, bitmap_width, 0};
1154 TestRenderPageBitmapWithMatrix(page, bitmap_width, bitmap_height,
1155 rotate_90_scale_2_matrix, bitmap_rect,
1156 kLargerRotatedChecksum);
1157
1158 // On the larger bitmap, apply 90 degree rotation to a bitmap with the
1159 // appropriate dimensions.
1160 const float landscape_bitmap_width = bitmap_height;
1161 const float landscape_bitmap_height = bitmap_width;
1162 FS_RECTF landscape_bitmap_rect{0, 0, landscape_bitmap_width,
1163 landscape_bitmap_height};
1164 FS_MATRIX landscape_rotate_90_scale_2_matrix{
1165 0, 2, -2, 0, landscape_bitmap_width, 0};
1166 TestRenderPageBitmapWithMatrix(
1167 page, landscape_bitmap_width, landscape_bitmap_height,
1168 landscape_rotate_90_scale_2_matrix, landscape_bitmap_rect,
1169 kLargerRotatedLandscapeChecksum);
1170
1171 // On the larger bitmap, apply 45 degree rotation to a bitmap with the
1172 // appropriate dimensions.
1173 const float sqrt2 = 1.41421356f;
1174 const float diagonal_bitmap_size =
1175 ceil((bitmap_width + bitmap_height) / sqrt2);
1176 FS_RECTF diagonal_bitmap_rect{0, 0, diagonal_bitmap_size,
1177 diagonal_bitmap_size};
1178 FS_MATRIX rotate_45_scale_2_matrix{
1179 sqrt2, sqrt2, -sqrt2, sqrt2, bitmap_height / sqrt2, 0};
1180 TestRenderPageBitmapWithMatrix(page, diagonal_bitmap_size,
1181 diagonal_bitmap_size, rotate_45_scale_2_matrix,
1182 diagonal_bitmap_rect,
1183 larger_rotated_diagonal_checksum);
1184
1185 // Render the (2, 1) tile of the page (third column, second row) when the page
1186 // is divided in 50x50 pixel tiles. The tile is scaled by a factor of 7.
1187 const float scale = 7.0;
1188 const int tile_size = 50;
1189 const int tile_x = 2;
1190 const int tile_y = 1;
1191 float tile_bitmap_size = scale * tile_size;
1192 FS_RECTF tile_bitmap_rect{0, 0, tile_bitmap_size, tile_bitmap_size};
1193 FS_MATRIX tile_2_1_matrix{scale,
1194 0,
1195 0,
1196 scale,
1197 -tile_x * tile_bitmap_size,
1198 -tile_y * tile_bitmap_size};
1199 TestRenderPageBitmapWithMatrix(page, tile_bitmap_size, tile_bitmap_size,
1200 tile_2_1_matrix, tile_bitmap_rect,
1201 tile_checksum);
1202
1203 UnloadPage(page);
1204 }
1205
TEST_F(FPDFViewEmbedderTest,FPDF_GetPageSizeByIndexF)1206 TEST_F(FPDFViewEmbedderTest, FPDF_GetPageSizeByIndexF) {
1207 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1208
1209 FS_SIZEF size;
1210 EXPECT_FALSE(FPDF_GetPageSizeByIndexF(nullptr, 0, &size));
1211 EXPECT_FALSE(FPDF_GetPageSizeByIndexF(document(), 0, nullptr));
1212
1213 // Page -1 doesn't exist.
1214 EXPECT_FALSE(FPDF_GetPageSizeByIndexF(document(), -1, &size));
1215
1216 // Page 1 doesn't exist.
1217 EXPECT_FALSE(FPDF_GetPageSizeByIndexF(document(), 1, &size));
1218
1219 // Page 0 exists.
1220 EXPECT_TRUE(FPDF_GetPageSizeByIndexF(document(), 0, &size));
1221 EXPECT_FLOAT_EQ(200.0f, size.width);
1222 EXPECT_FLOAT_EQ(300.0f, size.height);
1223
1224 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document());
1225 #ifdef PDF_ENABLE_XFA
1226 // TODO(tsepez): XFA must obtain this size without parsing.
1227 EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting());
1228 #else // PDF_ENABLE_XFA
1229 EXPECT_EQ(0u, pDoc->GetParsedPageCountForTesting());
1230 #endif // PDF_ENABLE_XFA
1231
1232 // Double-check against values from when page is actually parsed.
1233 FPDF_PAGE page = LoadPage(0);
1234 ASSERT_TRUE(page);
1235 EXPECT_FLOAT_EQ(size.width, FPDF_GetPageWidthF(page));
1236 EXPECT_FLOAT_EQ(size.height, FPDF_GetPageHeightF(page));
1237 EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting());
1238 UnloadPage(page);
1239 }
1240
TEST_F(FPDFViewEmbedderTest,FPDF_GetPageSizeByIndex)1241 TEST_F(FPDFViewEmbedderTest, FPDF_GetPageSizeByIndex) {
1242 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1243
1244 double width = 0;
1245 double height = 0;
1246
1247 EXPECT_FALSE(FPDF_GetPageSizeByIndex(nullptr, 0, &width, &height));
1248 EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), 0, nullptr, &height));
1249 EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), 0, &width, nullptr));
1250
1251 // Page -1 doesn't exist.
1252 EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), -1, &width, &height));
1253
1254 // Page 1 doesn't exist.
1255 EXPECT_FALSE(FPDF_GetPageSizeByIndex(document(), 1, &width, &height));
1256
1257 // Page 0 exists.
1258 EXPECT_TRUE(FPDF_GetPageSizeByIndex(document(), 0, &width, &height));
1259 EXPECT_EQ(200.0, width);
1260 EXPECT_EQ(300.0, height);
1261
1262 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document());
1263 #ifdef PDF_ENABLE_XFA
1264 // TODO(tsepez): XFA must obtain this size without parsing.
1265 EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting());
1266 #else // PDF_ENABLE_XFA
1267 EXPECT_EQ(0u, pDoc->GetParsedPageCountForTesting());
1268 #endif // PDF_ENABLE_XFA
1269
1270 // Double-check against values from when page is actually parsed.
1271 FPDF_PAGE page = LoadPage(0);
1272 ASSERT_TRUE(page);
1273 EXPECT_EQ(width, FPDF_GetPageWidth(page));
1274 EXPECT_EQ(height, FPDF_GetPageHeight(page));
1275 EXPECT_EQ(1u, pDoc->GetParsedPageCountForTesting());
1276 UnloadPage(page);
1277 }
1278
TEST_F(FPDFViewEmbedderTest,GetXFAArrayData)1279 TEST_F(FPDFViewEmbedderTest, GetXFAArrayData) {
1280 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1281
1282 static constexpr struct {
1283 const char* name;
1284 size_t content_length;
1285 const char* content_checksum;
1286 } kExpectedResults[]{
1287 {"preamble", 124u, "71be364e53292596412242bfcdb46eab"},
1288 {"config", 642u, "bcd1ca1d420ee31a561273a54a06435f"},
1289 {"template", 541u, "0f48cb2fa1bb9cbf9eee802d66e81bf4"},
1290 {"localeSet", 3455u, "bb1f253d3e5c719ac0da87d055bc164e"},
1291 {"postamble", 11u, "6b79e25da35d86634ea27c38f64cf243"},
1292 };
1293
1294 ASSERT_EQ(static_cast<int>(std::size(kExpectedResults)),
1295 FPDF_GetXFAPacketCount(document()));
1296 for (size_t i = 0; i < std::size(kExpectedResults); ++i) {
1297 char name_buffer[20] = {};
1298 ASSERT_EQ(strlen(kExpectedResults[i].name) + 1,
1299 FPDF_GetXFAPacketName(document(), i, nullptr, 0));
1300 EXPECT_EQ(
1301 strlen(kExpectedResults[i].name) + 1,
1302 FPDF_GetXFAPacketName(document(), i, name_buffer, sizeof(name_buffer)));
1303 EXPECT_STREQ(kExpectedResults[i].name, name_buffer);
1304
1305 unsigned long buflen;
1306 ASSERT_TRUE(FPDF_GetXFAPacketContent(document(), i, nullptr, 0, &buflen));
1307 ASSERT_EQ(kExpectedResults[i].content_length, buflen);
1308 std::vector<uint8_t> data_buffer(buflen);
1309 EXPECT_TRUE(FPDF_GetXFAPacketContent(document(), i, data_buffer.data(),
1310 data_buffer.size(), &buflen));
1311 EXPECT_EQ(kExpectedResults[i].content_length, buflen);
1312 EXPECT_STREQ(kExpectedResults[i].content_checksum,
1313 GenerateMD5Base16(data_buffer).c_str());
1314 }
1315
1316 // Test bad parameters.
1317 EXPECT_EQ(-1, FPDF_GetXFAPacketCount(nullptr));
1318
1319 EXPECT_EQ(0u, FPDF_GetXFAPacketName(nullptr, 0, nullptr, 0));
1320 EXPECT_EQ(0u, FPDF_GetXFAPacketName(document(), -1, nullptr, 0));
1321 EXPECT_EQ(0u, FPDF_GetXFAPacketName(document(), std::size(kExpectedResults),
1322 nullptr, 0));
1323
1324 unsigned long buflen = 123;
1325 EXPECT_FALSE(FPDF_GetXFAPacketContent(nullptr, 0, nullptr, 0, &buflen));
1326 EXPECT_EQ(123u, buflen);
1327 EXPECT_FALSE(FPDF_GetXFAPacketContent(document(), -1, nullptr, 0, &buflen));
1328 EXPECT_EQ(123u, buflen);
1329 EXPECT_FALSE(FPDF_GetXFAPacketContent(document(), std::size(kExpectedResults),
1330 nullptr, 0, &buflen));
1331 EXPECT_EQ(123u, buflen);
1332 EXPECT_FALSE(FPDF_GetXFAPacketContent(document(), 0, nullptr, 0, nullptr));
1333 }
1334
TEST_F(FPDFViewEmbedderTest,GetXFAStreamData)1335 TEST_F(FPDFViewEmbedderTest, GetXFAStreamData) {
1336 ASSERT_TRUE(OpenDocument("bug_1265.pdf"));
1337
1338 ASSERT_EQ(1, FPDF_GetXFAPacketCount(document()));
1339
1340 char name_buffer[20] = {};
1341 ASSERT_EQ(1u, FPDF_GetXFAPacketName(document(), 0, nullptr, 0));
1342 EXPECT_EQ(1u, FPDF_GetXFAPacketName(document(), 0, name_buffer,
1343 sizeof(name_buffer)));
1344 EXPECT_STREQ("", name_buffer);
1345
1346 unsigned long buflen;
1347 ASSERT_TRUE(FPDF_GetXFAPacketContent(document(), 0, nullptr, 0, &buflen));
1348 ASSERT_EQ(121u, buflen);
1349 std::vector<uint8_t> data_buffer(buflen);
1350 EXPECT_TRUE(FPDF_GetXFAPacketContent(document(), 0, data_buffer.data(),
1351 data_buffer.size(), &buflen));
1352 EXPECT_EQ(121u, buflen);
1353 EXPECT_STREQ("8f912eaa1e66c9341cb3032ede71e147",
1354 GenerateMD5Base16(data_buffer).c_str());
1355 }
1356
TEST_F(FPDFViewEmbedderTest,GetXFADataForNoForm)1357 TEST_F(FPDFViewEmbedderTest, GetXFADataForNoForm) {
1358 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1359
1360 EXPECT_EQ(0, FPDF_GetXFAPacketCount(document()));
1361 }
1362
TEST_F(FPDFViewEmbedderTest,GetXFADataForAcroForm)1363 TEST_F(FPDFViewEmbedderTest, GetXFADataForAcroForm) {
1364 ASSERT_TRUE(OpenDocument("text_form.pdf"));
1365
1366 EXPECT_EQ(0, FPDF_GetXFAPacketCount(document()));
1367 }
1368
1369 class RecordUnsupportedErrorDelegate final : public EmbedderTest::Delegate {
1370 public:
1371 RecordUnsupportedErrorDelegate() = default;
1372 ~RecordUnsupportedErrorDelegate() override = default;
1373
UnsupportedHandler(int type)1374 void UnsupportedHandler(int type) override { type_ = type; }
1375
1376 int type_ = -1;
1377 };
1378
TEST_F(FPDFViewEmbedderTest,UnSupportedOperations_NotFound)1379 TEST_F(FPDFViewEmbedderTest, UnSupportedOperations_NotFound) {
1380 RecordUnsupportedErrorDelegate delegate;
1381 SetDelegate(&delegate);
1382 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1383 EXPECT_EQ(delegate.type_, -1);
1384 SetDelegate(nullptr);
1385 }
1386
TEST_F(FPDFViewEmbedderTest,UnSupportedOperations_LoadCustomDocument)1387 TEST_F(FPDFViewEmbedderTest, UnSupportedOperations_LoadCustomDocument) {
1388 RecordUnsupportedErrorDelegate delegate;
1389 SetDelegate(&delegate);
1390 ASSERT_TRUE(OpenDocument("unsupported_feature.pdf"));
1391 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
1392 SetDelegate(nullptr);
1393 }
1394
TEST_F(FPDFViewEmbedderTest,UnSupportedOperations_LoadDocument)1395 TEST_F(FPDFViewEmbedderTest, UnSupportedOperations_LoadDocument) {
1396 std::string file_path;
1397 ASSERT_TRUE(
1398 PathService::GetTestFilePath("unsupported_feature.pdf", &file_path));
1399
1400 RecordUnsupportedErrorDelegate delegate;
1401 SetDelegate(&delegate);
1402 FPDF_DOCUMENT doc = FPDF_LoadDocument(file_path.c_str(), "");
1403 EXPECT_TRUE(doc != nullptr);
1404 EXPECT_EQ(FPDF_UNSP_DOC_PORTABLECOLLECTION, delegate.type_);
1405 FPDF_CloseDocument(doc);
1406 SetDelegate(nullptr);
1407 }
1408
TEST_F(FPDFViewEmbedderTest,DocumentHasValidCrossReferenceTable)1409 TEST_F(FPDFViewEmbedderTest, DocumentHasValidCrossReferenceTable) {
1410 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1411 EXPECT_TRUE(FPDF_DocumentHasValidCrossReferenceTable(document()));
1412 }
1413
TEST_F(FPDFViewEmbedderTest,DocumentHasInvalidCrossReferenceTable)1414 TEST_F(FPDFViewEmbedderTest, DocumentHasInvalidCrossReferenceTable) {
1415 EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(nullptr));
1416
1417 ASSERT_TRUE(OpenDocument("bug_664284.pdf"));
1418 EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(document()));
1419 }
1420
1421 // Related to https://crbug.com/pdfium/1197
TEST_F(FPDFViewEmbedderTest,LoadDocumentWithEmptyXRefConsistently)1422 TEST_F(FPDFViewEmbedderTest, LoadDocumentWithEmptyXRefConsistently) {
1423 ASSERT_TRUE(OpenDocument("empty_xref.pdf"));
1424 EXPECT_TRUE(FPDF_DocumentHasValidCrossReferenceTable(document()));
1425
1426 std::string file_path;
1427 ASSERT_TRUE(PathService::GetTestFilePath("empty_xref.pdf", &file_path));
1428 {
1429 ScopedFPDFDocument doc(FPDF_LoadDocument(file_path.c_str(), ""));
1430 ASSERT_TRUE(doc);
1431 EXPECT_TRUE(FPDF_DocumentHasValidCrossReferenceTable(doc.get()));
1432 }
1433 {
1434 size_t file_length = 0;
1435 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
1436 GetFileContents(file_path.c_str(), &file_length);
1437 DCHECK(file_contents);
1438 ScopedFPDFDocument doc(
1439 FPDF_LoadMemDocument(file_contents.get(), file_length, ""));
1440 ASSERT_TRUE(doc);
1441 EXPECT_TRUE(FPDF_DocumentHasValidCrossReferenceTable(doc.get()));
1442 }
1443 }
1444
TEST_F(FPDFViewEmbedderTest,RenderBug664284WithNoNativeText)1445 TEST_F(FPDFViewEmbedderTest, RenderBug664284WithNoNativeText) {
1446 // For Skia, since the font used in bug_664284.pdf is not a CID font,
1447 // ShouldDrawDeviceText() will always return true. Therefore
1448 // FPDF_NO_NATIVETEXT and the font widths defined in the PDF determines
1449 // whether to go through the rendering path in
1450 // CFX_SkiaDeviceDriver::DrawDeviceText(). In this case, it returns false and
1451 // affects the rendering results across all platforms.
1452
1453 // For AGG, since CFX_AggDeviceDriver::DrawDeviceText() always returns false,
1454 // FPDF_NO_NATIVETEXT won't affect device-specific rendering path and it will
1455 // only disable native text support on macOS. Therefore Windows and Linux
1456 // rendering results remain the same as rendering with no flags, while the
1457 // macOS rendering result doesn't.
1458
1459 const char* original_checksum = []() {
1460 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
1461 return "29cb8045c21cfa2c920fdf43de70efd8";
1462 }
1463 #if BUILDFLAG(IS_APPLE)
1464 return "0e339d606aafb63077f49e238dc27cb0";
1465 #else
1466 return "288502887ffc63291f35a0573b944375";
1467 #endif
1468 }();
1469 static const char kNoNativeTextChecksum[] =
1470 "288502887ffc63291f35a0573b944375";
1471 ASSERT_TRUE(OpenDocument("bug_664284.pdf"));
1472 FPDF_PAGE page = LoadPage(0);
1473 ASSERT_TRUE(page);
1474
1475 TestRenderPageBitmapWithFlags(page, 0, original_checksum);
1476 TestRenderPageBitmapWithFlags(page, FPDF_NO_NATIVETEXT,
1477 kNoNativeTextChecksum);
1478
1479 UnloadPage(page);
1480 }
1481
TEST_F(FPDFViewEmbedderTest,RenderAnnotationWithPrintingFlag)1482 TEST_F(FPDFViewEmbedderTest, RenderAnnotationWithPrintingFlag) {
1483 const char* annotation_checksum = []() {
1484 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
1485 return "eaece6b8041c0cb9b33398e5b6d5ddda";
1486 }
1487 return "c108ba6e0a9743652f12e4bc223f9b32";
1488 }();
1489 static const char kPrintingChecksum[] = "3e235b9f88f652f2b97b1fc393924849";
1490 ASSERT_TRUE(OpenDocument("bug_1658.pdf"));
1491 FPDF_PAGE page = LoadPage(0);
1492 ASSERT_TRUE(page);
1493
1494 // A yellow highlight is rendered with `FPDF_ANNOT` flag.
1495 TestRenderPageBitmapWithFlags(page, FPDF_ANNOT, annotation_checksum);
1496
1497 // After adding `FPDF_PRINTING` flag, the yellow highlight is not rendered.
1498 TestRenderPageBitmapWithFlags(page, FPDF_PRINTING | FPDF_ANNOT,
1499 kPrintingChecksum);
1500 UnloadPage(page);
1501 }
1502
1503 // TODO(crbug.com/pdfium/1955): Remove this test once pixel tests can pass with
1504 // `reverse-byte-order` option.
TEST_F(FPDFViewEmbedderTest,RenderBlueAndRedImagesWithReverByteOrderFlag)1505 TEST_F(FPDFViewEmbedderTest, RenderBlueAndRedImagesWithReverByteOrderFlag) {
1506 // When rendering with `FPDF_REVERSE_BYTE_ORDER` flag, the blue and red
1507 // channels should be reversed.
1508 ASSERT_TRUE(OpenDocument("bug_1396264.pdf"));
1509 ScopedFPDFPage page(FPDF_LoadPage(document(), 0));
1510 ASSERT_TRUE(page);
1511
1512 TestRenderPageBitmapWithFlags(page.get(), 0,
1513 "81e7f4498090977c848a21b5c6510d3a");
1514 TestRenderPageBitmapWithFlags(page.get(), FPDF_REVERSE_BYTE_ORDER,
1515 "505ba6d1c7f4044c11c91873452a8bde");
1516 }
1517
TEST_F(FPDFViewEmbedderTest,RenderJpxLzwImageWithFlags)1518 TEST_F(FPDFViewEmbedderTest, RenderJpxLzwImageWithFlags) {
1519 static const char kNormalChecksum[] = "4bcd56cae1ca2622403e8af07242e71a";
1520 static const char kGrayscaleChecksum[] = "fe45ad56efe868ba82285fa5ffedc0cb";
1521
1522 ASSERT_TRUE(OpenDocument("jpx_lzw.pdf"));
1523 FPDF_PAGE page = LoadPage(0);
1524 ASSERT_TRUE(page);
1525
1526 TestRenderPageBitmapWithFlags(page, 0, kNormalChecksum);
1527 TestRenderPageBitmapWithFlags(page, FPDF_ANNOT, kNormalChecksum);
1528 TestRenderPageBitmapWithFlags(page, FPDF_LCD_TEXT, kNormalChecksum);
1529 TestRenderPageBitmapWithFlags(page, FPDF_NO_NATIVETEXT, kNormalChecksum);
1530 TestRenderPageBitmapWithFlags(page, FPDF_GRAYSCALE, kGrayscaleChecksum);
1531 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_LIMITEDIMAGECACHE,
1532 kNormalChecksum);
1533 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_FORCEHALFTONE,
1534 kNormalChecksum);
1535 TestRenderPageBitmapWithFlags(page, FPDF_PRINTING, kNormalChecksum);
1536 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHTEXT,
1537 kNormalChecksum);
1538 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHIMAGE,
1539 kNormalChecksum);
1540 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHPATH,
1541 kNormalChecksum);
1542
1543 UnloadPage(page);
1544 }
1545
TEST_F(FPDFViewEmbedderTest,RenderManyRectanglesWithFlags)1546 TEST_F(FPDFViewEmbedderTest, RenderManyRectanglesWithFlags) {
1547 const char* grayscale_checksum = []() {
1548 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1549 return "b596ac8bbe64e7bff31888ab05e4dcf4";
1550 return "7b553f1052069a9c61237a05db0955d6";
1551 }();
1552 const char* no_smoothpath_checksum = []() {
1553 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1554 return "4d71ed53d9f6e6a761876ebb4ff23e19";
1555 return "ff6e5c509d1f6984bcdfd18b26a4203a";
1556 }();
1557
1558 ASSERT_TRUE(OpenDocument("many_rectangles.pdf"));
1559 FPDF_PAGE page = LoadPage(0);
1560 ASSERT_TRUE(page);
1561
1562 TestRenderPageBitmapWithFlags(page, 0, ManyRectanglesChecksum());
1563 TestRenderPageBitmapWithFlags(page, FPDF_ANNOT, ManyRectanglesChecksum());
1564 TestRenderPageBitmapWithFlags(page, FPDF_LCD_TEXT, ManyRectanglesChecksum());
1565 TestRenderPageBitmapWithFlags(page, FPDF_NO_NATIVETEXT,
1566 ManyRectanglesChecksum());
1567 TestRenderPageBitmapWithFlags(page, FPDF_GRAYSCALE, grayscale_checksum);
1568 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_LIMITEDIMAGECACHE,
1569 ManyRectanglesChecksum());
1570 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_FORCEHALFTONE,
1571 ManyRectanglesChecksum());
1572 TestRenderPageBitmapWithFlags(page, FPDF_PRINTING, ManyRectanglesChecksum());
1573 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHTEXT,
1574 ManyRectanglesChecksum());
1575 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHIMAGE,
1576 ManyRectanglesChecksum());
1577 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHPATH,
1578 no_smoothpath_checksum);
1579
1580 UnloadPage(page);
1581 }
1582
TEST_F(FPDFViewEmbedderTest,RenderManyRectanglesWithAndWithoutExternalMemory)1583 TEST_F(FPDFViewEmbedderTest, RenderManyRectanglesWithAndWithoutExternalMemory) {
1584 ASSERT_TRUE(OpenDocument("many_rectangles.pdf"));
1585 FPDF_PAGE page = LoadPage(0);
1586 ASSERT_TRUE(page);
1587
1588 const char* bgr_checksum = []() {
1589 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1590 return "4d52e5cc1d4a8067bf918b85b232fff0";
1591 return "ab6312e04c0d3f4e46fb302a45173d05";
1592 }();
1593 static constexpr int kBgrStride = 600; // Width of 200 * 24 bits per pixel.
1594 TestRenderPageBitmapWithInternalMemory(page, FPDFBitmap_BGR, bgr_checksum);
1595 TestRenderPageBitmapWithInternalMemoryAndStride(page, FPDFBitmap_BGR,
1596 kBgrStride, bgr_checksum);
1597 TestRenderPageBitmapWithExternalMemory(page, FPDFBitmap_BGR, bgr_checksum);
1598 TestRenderPageBitmapWithExternalMemoryAndNoStride(page, FPDFBitmap_BGR,
1599 bgr_checksum);
1600
1601 const char* gray_checksum = []() {
1602 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1603 return "3dfe1fc3889123d68e1748fefac65e72";
1604 return "b561c11edc44dc3972125a9b8744fa2f";
1605 }();
1606
1607 TestRenderPageBitmapWithInternalMemory(page, FPDFBitmap_Gray, gray_checksum);
1608 static constexpr int kGrayStride = 200; // Width of 200 * 8 bits per pixel.
1609 TestRenderPageBitmapWithInternalMemoryAndStride(page, FPDFBitmap_Gray,
1610 kGrayStride, gray_checksum);
1611 TestRenderPageBitmapWithExternalMemory(page, FPDFBitmap_Gray, gray_checksum);
1612 TestRenderPageBitmapWithExternalMemoryAndNoStride(page, FPDFBitmap_Gray,
1613 gray_checksum);
1614
1615 static constexpr int kBgrxStride = 800; // Width of 200 * 32 bits per pixel.
1616 TestRenderPageBitmapWithInternalMemory(page, FPDFBitmap_BGRx,
1617 ManyRectanglesChecksum());
1618 TestRenderPageBitmapWithInternalMemoryAndStride(
1619 page, FPDFBitmap_BGRx, kBgrxStride, ManyRectanglesChecksum());
1620 TestRenderPageBitmapWithExternalMemory(page, FPDFBitmap_BGRx,
1621 ManyRectanglesChecksum());
1622 TestRenderPageBitmapWithExternalMemoryAndNoStride(page, FPDFBitmap_BGRx,
1623 ManyRectanglesChecksum());
1624
1625 TestRenderPageBitmapWithInternalMemory(page, FPDFBitmap_BGRA,
1626 ManyRectanglesChecksum());
1627 TestRenderPageBitmapWithInternalMemoryAndStride(
1628 page, FPDFBitmap_BGRA, kBgrxStride, ManyRectanglesChecksum());
1629 TestRenderPageBitmapWithExternalMemory(page, FPDFBitmap_BGRA,
1630 ManyRectanglesChecksum());
1631 TestRenderPageBitmapWithExternalMemoryAndNoStride(page, FPDFBitmap_BGRA,
1632 ManyRectanglesChecksum());
1633
1634 UnloadPage(page);
1635 }
1636
TEST_F(FPDFViewEmbedderTest,RenderHelloWorldWithFlags)1637 TEST_F(FPDFViewEmbedderTest, RenderHelloWorldWithFlags) {
1638 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1639 FPDF_PAGE page = LoadPage(0);
1640 ASSERT_TRUE(page);
1641
1642 using pdfium::HelloWorldChecksum;
1643 TestRenderPageBitmapWithFlags(page, 0, HelloWorldChecksum());
1644 TestRenderPageBitmapWithFlags(page, FPDF_ANNOT, HelloWorldChecksum());
1645 TestRenderPageBitmapWithFlags(page, FPDF_GRAYSCALE, HelloWorldChecksum());
1646 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_LIMITEDIMAGECACHE,
1647 HelloWorldChecksum());
1648 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_FORCEHALFTONE,
1649 HelloWorldChecksum());
1650 TestRenderPageBitmapWithFlags(page, FPDF_PRINTING, HelloWorldChecksum());
1651 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHIMAGE,
1652 HelloWorldChecksum());
1653 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHPATH,
1654 HelloWorldChecksum());
1655
1656 const char* lcd_text_checksum = []() {
1657 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1658 return "d1decde2de1c07b5274cc8cb44f92427";
1659 #if BUILDFLAG(IS_APPLE)
1660 return "6eef7237f7591f07616e238422086737";
1661 #else
1662 return "09152e25e51fa8ca31fc28d0937bf477";
1663 #endif // BUILDFLAG(IS_APPLE)
1664 }();
1665 const char* no_smoothtext_checksum = []() {
1666 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
1667 return "cd5bbe9407c3fcc85d365172a9a55abd";
1668 }
1669 #if BUILDFLAG(IS_APPLE)
1670 return "6eef7237f7591f07616e238422086737";
1671 #else
1672 return "37d0b34e1762fdda4c05ce7ea357b828";
1673 #endif
1674 }();
1675
1676 TestRenderPageBitmapWithFlags(page, FPDF_LCD_TEXT, lcd_text_checksum);
1677 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHTEXT,
1678 no_smoothtext_checksum);
1679
1680 // For text rendering, When anti-aliasing is disabled, LCD Optimization flag
1681 // will be ignored.
1682 TestRenderPageBitmapWithFlags(page, FPDF_LCD_TEXT | FPDF_RENDER_NO_SMOOTHTEXT,
1683 no_smoothtext_checksum);
1684
1685 UnloadPage(page);
1686 }
1687
1688 // Deliberately disabled because this test case renders a large bitmap, which is
1689 // very slow for debug builds.
1690 #if defined(NDEBUG)
1691 #define MAYBE_LargeImageDoesNotRenderBlank LargeImageDoesNotRenderBlank
1692 #else
1693 #define MAYBE_LargeImageDoesNotRenderBlank DISABLED_LargeImageDoesNotRenderBlank
1694 #endif
TEST_F(FPDFViewEmbedderTest,MAYBE_LargeImageDoesNotRenderBlank)1695 TEST_F(FPDFViewEmbedderTest, MAYBE_LargeImageDoesNotRenderBlank) {
1696 static const char kChecksum[] = "a6056db6961f4e65c42ab2e246171fe1";
1697
1698 ASSERT_TRUE(OpenDocument("bug_1646.pdf"));
1699 FPDF_PAGE page = LoadPage(0);
1700 ASSERT_TRUE(page);
1701
1702 constexpr int kWidth = 40000;
1703 constexpr int kHeight = 100;
1704 TestRenderPageBitmapWithMatrix(page, kWidth, kHeight, {1000, 0, 0, 1, 0, 0},
1705 {0, 0, kWidth, kHeight}, kChecksum);
1706
1707 UnloadPage(page);
1708 }
1709
1710 #if BUILDFLAG(IS_WIN)
TEST_F(FPDFViewEmbedderTest,FPDFRenderPageEmf)1711 TEST_F(FPDFViewEmbedderTest, FPDFRenderPageEmf) {
1712 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1713 FPDF_PAGE page = LoadPage(0);
1714 ASSERT_TRUE(page);
1715
1716 std::vector<uint8_t> emf_normal = RenderPageWithFlagsToEmf(page, 0);
1717 EXPECT_EQ(3772u, emf_normal.size());
1718
1719 // FPDF_REVERSE_BYTE_ORDER is ignored since EMFs are always BGR.
1720 std::vector<uint8_t> emf_reverse_byte_order =
1721 RenderPageWithFlagsToEmf(page, FPDF_REVERSE_BYTE_ORDER);
1722 EXPECT_EQ(emf_normal, emf_reverse_byte_order);
1723
1724 UnloadPage(page);
1725 }
1726
1727 class PostScriptRenderEmbedderTestBase : public FPDFViewEmbedderTest {
1728 protected:
1729 ~PostScriptRenderEmbedderTestBase() override = default;
1730
1731 // FPDFViewEmbedderTest:
TearDown()1732 void TearDown() override {
1733 FPDF_SetPrintMode(FPDF_PRINTMODE_EMF);
1734 FPDFViewEmbedderTest::TearDown();
1735 }
1736 };
1737
1738 class PostScriptLevel2EmbedderTest : public PostScriptRenderEmbedderTestBase {
1739 public:
1740 PostScriptLevel2EmbedderTest() = default;
1741 ~PostScriptLevel2EmbedderTest() override = default;
1742
1743 protected:
1744 // FPDFViewEmbedderTest:
SetUp()1745 void SetUp() override {
1746 FPDFViewEmbedderTest::SetUp();
1747 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT2);
1748 }
1749 };
1750
1751 class PostScriptLevel3EmbedderTest : public PostScriptRenderEmbedderTestBase {
1752 public:
1753 PostScriptLevel3EmbedderTest() = default;
1754 ~PostScriptLevel3EmbedderTest() override = default;
1755
1756 protected:
1757 // FPDFViewEmbedderTest:
SetUp()1758 void SetUp() override {
1759 FPDFViewEmbedderTest::SetUp();
1760 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3);
1761 }
1762 };
1763
TEST_F(PostScriptLevel2EmbedderTest,Rectangles)1764 TEST_F(PostScriptLevel2EmbedderTest, Rectangles) {
1765 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1766 FPDF_PAGE page = LoadPage(0);
1767 ASSERT_TRUE(page);
1768
1769 std::vector<uint8_t> emf_normal = RenderPageWithFlagsToEmf(page, 0);
1770 std::string ps_data = GetPostScriptFromEmf(emf_normal);
1771 EXPECT_STREQ(kExpectedRectanglePostScript, ps_data.c_str());
1772
1773 // FPDF_REVERSE_BYTE_ORDER is ignored since PostScript is not bitmap-based.
1774 std::vector<uint8_t> emf_reverse_byte_order =
1775 RenderPageWithFlagsToEmf(page, FPDF_REVERSE_BYTE_ORDER);
1776 EXPECT_EQ(emf_normal, emf_reverse_byte_order);
1777
1778 UnloadPage(page);
1779 }
1780
TEST_F(PostScriptLevel3EmbedderTest,Rectangles)1781 TEST_F(PostScriptLevel3EmbedderTest, Rectangles) {
1782 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
1783 FPDF_PAGE page = LoadPage(0);
1784 ASSERT_TRUE(page);
1785
1786 std::vector<uint8_t> emf_normal = RenderPageWithFlagsToEmf(page, 0);
1787 std::string ps_data = GetPostScriptFromEmf(emf_normal);
1788 EXPECT_STREQ(kExpectedRectanglePostScript, ps_data.c_str());
1789
1790 // FPDF_REVERSE_BYTE_ORDER is ignored since PostScript is not bitmap-based.
1791 std::vector<uint8_t> emf_reverse_byte_order =
1792 RenderPageWithFlagsToEmf(page, FPDF_REVERSE_BYTE_ORDER);
1793 EXPECT_EQ(emf_normal, emf_reverse_byte_order);
1794
1795 UnloadPage(page);
1796 }
1797
TEST_F(PostScriptLevel2EmbedderTest,Image)1798 TEST_F(PostScriptLevel2EmbedderTest, Image) {
1799 const char kExpected[] =
1800 "\n"
1801 "save\n"
1802 "/im/initmatrix load def\n"
1803 "/n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load "
1804 "def/h/closepath load def\n"
1805 "/f/fill load def/F/eofill load def/s/stroke load def/W/clip load "
1806 "def/W*/eoclip load def\n"
1807 "/rg/setrgbcolor load def/k/setcmykcolor load def\n"
1808 "/J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load "
1809 "def/M/setmiterlimit load def/d/setdash load def\n"
1810 "/q/gsave load def/Q/grestore load def/iM/imagemask load def\n"
1811 "/Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont "
1812 "load def\n"
1813 "/cm/concat load def/Cm/currentmatrix load def/mx/matrix load "
1814 "def/sm/setmatrix load def\n"
1815 "0 792 m 0 0 l 612 0 l 612 792 l 0 792 l h W n\n"
1816 "q\n"
1817 "0 792 m 0 0 l 612 0 l 612 792 l 0 792 l h W n\n"
1818 "q\n"
1819 "Q\n"
1820 "q\n"
1821 "281 106.7 m 331 106.7 l 331 56.7 l 281 56.7 l 281 106.7 l h W* n\n"
1822 "q\n"
1823 "[49.9 0 0 -50 281.1 106.6]cm 50 50 8[50 0 0 -50 0 "
1824 "50]currentfile/ASCII85Decode filter /DCTDecode filter false 3 "
1825 "colorimage\n"
1826 "s4IA0!\"_al8O`[\\!<<*#!!*'\"s4[N@!!ic5#6k>;#6tJ?#m^kH'FbHY$Odmc'+Yct)"
1827 "BU\"@)B9_>\r\n"
1828 ",VCGe+tOrY*%3`p/2/e81c-:%3B]>W4>&EH1B6)/"
1829 "6NIK\"#n.1M(_$ok1*IV\\1,:U?1,:U?1,:U?\r\n"
1830 "1,:U?1,:U?1,:U?1,:U?1,:U?1,:U?1,:U?1,:U?1,:U?1,AmF!\"fJ:1&s'3!?qLF&HMtG!"
1831 "WU(<\r\n"
1832 "*rl9A\"T\\W)!<E3$z!!!!\"!WrQ/\"pYD?$4HmP!4<@<!W`B*!X&T/"
1833 "\"U\"r.!!.KK!WrE*&Hrdj0gQ!W\r\n"
1834 ";.0\\RE>10ZOeE%*6F\"?A;UOtZ1LbBV#mqFa(`=5<-7:2j.Ps\"@2`NfY6UX@47n?3D;"
1835 "cHat='/U/\r\n"
1836 "@q9._B4u!oF*)PJGBeCZK7nr5LPUeEP*;,qQC!u,R\\HRQV5C/"
1837 "hWN*81['d?O\\@K2f_o0O6a2lBF\r\n"
1838 "daQ^rf%8R-g>V&OjQ5OekiqC&o(2MHp@n@XqZ\"J6*ru?D!<E3%!<E3%!<<*\"!!!!\"!"
1839 "WrQ/\"pYD?\r\n"
1840 "$4HmP!4<C=!W`?*\"9Sc3\"U\"r.!<RHF!<N?8\"9fr'\"qj4!#@VTc+u4]T'LIqUZ,$_"
1841 "k1K*]W@WKj'\r\n"
1842 "(*k`q-1Mcg)&ahL-n-W'2E*TU3^Z;(7Rp!@8lJ\\h<``C+>%;)SAnPdkC3+K>G'A1VH@gd&"
1843 "KnbA=\r\n"
1844 "M2II[Pa.Q$R$jD;USO``Vl6SpZEppG[^WcW]#)A'`Q#s>ai`&\\eCE.%f\\,!<j5f="
1845 "akNM0qo(2MH\r\n"
1846 "p@n@XqZ#7L$j-M1!YGMH!'^JZre`+s!fAD!!fAD!!fAD!!fAD!!fAD!!fAD!!fAD!!fAD!!"
1847 "fAD!\r\n"
1848 "!fAD!!fAD!!fAD!!fAD!!fAD!!fAD!&-(;~>\n"
1849 "Q\n"
1850 "Q\n"
1851 "q\n"
1852 "q\n"
1853 "Q\n"
1854 "Q\n"
1855 "Q\n"
1856 "Q\n"
1857 "\n"
1858 "restore\n";
1859
1860 ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
1861 FPDF_PAGE page = LoadPage(0);
1862 ASSERT_TRUE(page);
1863
1864 std::vector<uint8_t> emf = RenderPageWithFlagsToEmf(page, 0);
1865 std::string ps_data = GetPostScriptFromEmf(emf);
1866 EXPECT_STREQ(kExpected, ps_data.c_str());
1867
1868 UnloadPage(page);
1869 }
1870
TEST_F(PostScriptLevel3EmbedderTest,Image)1871 TEST_F(PostScriptLevel3EmbedderTest, Image) {
1872 const char kExpected[] = R"(
1873 save
1874 /im/initmatrix load def
1875 /n/newpath load def/m/moveto load def/l/lineto load def/c/curveto load def/h/closepath load def
1876 /f/fill load def/F/eofill load def/s/stroke load def/W/clip load def/W*/eoclip load def
1877 /rg/setrgbcolor load def/k/setcmykcolor load def
1878 /J/setlinecap load def/j/setlinejoin load def/w/setlinewidth load def/M/setmiterlimit load def/d/setdash load def
1879 /q/gsave load def/Q/grestore load def/iM/imagemask load def
1880 /Tj/show load def/Ff/findfont load def/Fs/scalefont load def/Sf/setfont load def
1881 /cm/concat load def/Cm/currentmatrix load def/mx/matrix load def/sm/setmatrix load def
1882 0 792 m 0 0 l 612 0 l 612 792 l 0 792 l h W n
1883 q
1884 0 792 m 0 0 l 612 0 l 612 792 l 0 792 l h W n
1885 q
1886 Q
1887 q
1888 281 106.7 m 331 106.7 l 331 56.7 l 281 56.7 l 281 106.7 l h W* n
1889 q
1890 [49.9 0 0 -50 281.1 106.6]cm 50 50 8[50 0 0 -50 0 50]currentfile/ASCII85Decode filter /FlateDecode filter false 3 colorimage
1891 Gb"0;0`_7S!5bE%:[N')TE"rlzGQSs[!!*~>
1892 Q
1893 Q
1894 q
1895 q
1896 Q
1897 Q
1898 Q
1899 Q
1900
1901 restore
1902 )";
1903
1904 ASSERT_TRUE(OpenDocument("tagged_alt_text.pdf"));
1905 FPDF_PAGE page = LoadPage(0);
1906 ASSERT_TRUE(page);
1907
1908 std::vector<uint8_t> emf = RenderPageWithFlagsToEmf(page, 0);
1909 std::string ps_data = GetPostScriptFromEmf(emf);
1910 EXPECT_STREQ(kExpected, ps_data.c_str());
1911
1912 UnloadPage(page);
1913 }
1914
1915 TEST_F(FPDFViewEmbedderTest, ImageMask) {
1916 // TODO(crbug.com/pdfium/1500): Fix this test and enable.
1917 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer())
1918 return;
1919
1920 ASSERT_TRUE(OpenDocument("bug_674771.pdf"));
1921 FPDF_PAGE page = LoadPage(0);
1922 ASSERT_TRUE(page);
1923
1924 // Render the page with more efficient processing of image masks.
1925 FPDF_SetPrintMode(FPDF_PRINTMODE_EMF_IMAGE_MASKS);
1926 std::vector<uint8_t> emf_image_masks = RenderPageWithFlagsToEmf(page, 0);
1927
1928 // Render the page normally.
1929 FPDF_SetPrintMode(FPDF_PRINTMODE_EMF);
1930 std::vector<uint8_t> emf_normal = RenderPageWithFlagsToEmf(page, 0);
1931
1932 EXPECT_LT(emf_image_masks.size(), emf_normal.size());
1933
1934 UnloadPage(page);
1935 }
1936 #endif // BUILDFLAG(IS_WIN)
1937
1938 TEST_F(FPDFViewEmbedderTest, GetTrailerEnds) {
1939 ASSERT_TRUE(OpenDocument("two_signatures.pdf"));
1940
1941 // FPDF_GetTrailerEnds() positive testing.
1942 unsigned long size = FPDF_GetTrailerEnds(document(), nullptr, 0);
1943 const std::vector<unsigned int> kExpectedEnds{633, 1703, 2781};
1944 ASSERT_EQ(kExpectedEnds.size(), size);
1945 std::vector<unsigned int> ends(size);
1946 ASSERT_EQ(size, FPDF_GetTrailerEnds(document(), ends.data(), size));
1947 ASSERT_EQ(kExpectedEnds, ends);
1948
1949 // FPDF_GetTrailerEnds() negative testing.
1950 ASSERT_EQ(0U, FPDF_GetTrailerEnds(nullptr, nullptr, 0));
1951
1952 ends.resize(2);
1953 ends[0] = 0;
1954 ends[1] = 1;
1955 size = FPDF_GetTrailerEnds(document(), ends.data(), ends.size());
1956 ASSERT_EQ(kExpectedEnds.size(), size);
1957 EXPECT_EQ(0U, ends[0]);
1958 EXPECT_EQ(1U, ends[1]);
1959 }
1960
1961 TEST_F(FPDFViewEmbedderTest, GetTrailerEndsHelloWorld) {
1962 // Single trailer, \n line ending at the trailer end.
1963 ASSERT_TRUE(OpenDocument("hello_world.pdf"));
1964
1965 // FPDF_GetTrailerEnds() positive testing.
1966 unsigned long size = FPDF_GetTrailerEnds(document(), nullptr, 0);
1967 const std::vector<unsigned int> kExpectedEnds{840};
1968 ASSERT_EQ(kExpectedEnds.size(), size);
1969 std::vector<unsigned int> ends(size);
1970 ASSERT_EQ(size, FPDF_GetTrailerEnds(document(), ends.data(), size));
1971 ASSERT_EQ(kExpectedEnds, ends);
1972 }
1973
1974 TEST_F(FPDFViewEmbedderTest, GetTrailerEndsAnnotationStamp) {
1975 // Multiple trailers, \r\n line ending at the trailer ends.
1976 ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
1977
1978 // FPDF_GetTrailerEnds() positive testing.
1979 unsigned long size = FPDF_GetTrailerEnds(document(), nullptr, 0);
1980 const std::vector<unsigned int> kExpectedEnds{441, 7945, 101719};
1981 ASSERT_EQ(kExpectedEnds.size(), size);
1982 std::vector<unsigned int> ends(size);
1983 ASSERT_EQ(size, FPDF_GetTrailerEnds(document(), ends.data(), size));
1984 ASSERT_EQ(kExpectedEnds, ends);
1985 }
1986
1987 TEST_F(FPDFViewEmbedderTest, GetTrailerEndsLinearized) {
1988 // Set up linearized PDF.
1989 FileAccessForTesting file_acc("linearized.pdf");
1990 FakeFileAccess fake_acc(&file_acc);
1991 CreateAvail(fake_acc.GetFileAvail(), fake_acc.GetFileAccess());
1992 fake_acc.SetWholeFileAvailable();
1993
1994 // Multiple trailers, \r line ending at the trailer ends (no \n).
1995 SetDocumentFromAvail();
1996 ASSERT_TRUE(document());
1997
1998 // FPDF_GetTrailerEnds() positive testing.
1999 unsigned long size = FPDF_GetTrailerEnds(document(), nullptr, 0);
2000 const std::vector<unsigned int> kExpectedEnds{474, 11384};
2001 ASSERT_EQ(kExpectedEnds.size(), size);
2002 std::vector<unsigned int> ends(size);
2003 ASSERT_EQ(size, FPDF_GetTrailerEnds(document(), ends.data(), size));
2004 ASSERT_EQ(kExpectedEnds, ends);
2005 }
2006
2007 TEST_F(FPDFViewEmbedderTest, GetTrailerEndsWhitespace) {
2008 // Whitespace between 'endstream'/'endobj' and the newline.
2009 ASSERT_TRUE(OpenDocument("trailer_end_trailing_space.pdf"));
2010
2011 unsigned long size = FPDF_GetTrailerEnds(document(), nullptr, 0);
2012 const std::vector<unsigned int> kExpectedEnds{1193};
2013 // Without the accompanying fix in place, this test would have failed, as the
2014 // size was 0, not 1, i.e. no trailer ends were found.
2015 ASSERT_EQ(kExpectedEnds.size(), size);
2016 std::vector<unsigned int> ends(size);
2017 ASSERT_EQ(size, FPDF_GetTrailerEnds(document(), ends.data(), size));
2018 EXPECT_EQ(kExpectedEnds, ends);
2019 }
2020
2021 TEST_F(FPDFViewEmbedderTest, RenderXfaPage) {
2022 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
2023
2024 FPDF_PAGE page = LoadPage(0);
2025 ASSERT_TRUE(page);
2026
2027 // Should always be blank, as we're not testing `FPDF_FFLDraw()` here.
2028 TestRenderPageBitmapWithFlags(page, 0, pdfium::kBlankPage612By792Checksum);
2029
2030 UnloadPage(page);
2031 }
2032
2033 #if defined(_SKIA_SUPPORT_)
TEST_F(FPDFViewEmbedderTest,RenderPageToSkp)2034 TEST_F(FPDFViewEmbedderTest, RenderPageToSkp) {
2035 if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
2036 GTEST_SKIP() << "FPDF_RenderPageSkp() only makes sense with Skia";
2037 }
2038
2039 ASSERT_TRUE(OpenDocument("rectangles.pdf"));
2040
2041 FPDF_PAGE page = LoadPage(0);
2042 ASSERT_TRUE(page);
2043
2044 TestRenderPageSkp(page, pdfium::RectanglesChecksum());
2045
2046 UnloadPage(page);
2047 }
2048
TEST_F(FPDFViewEmbedderTest,RenderXfaPageToSkp)2049 TEST_F(FPDFViewEmbedderTest, RenderXfaPageToSkp) {
2050 if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
2051 GTEST_SKIP() << "FPDF_RenderPageSkp() only makes sense with Skia";
2052 }
2053
2054 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
2055
2056 FPDF_PAGE page = LoadPage(0);
2057 ASSERT_TRUE(page);
2058
2059 // Should always be blank, as we're not testing `FPDF_FFLRecord()` here.
2060 TestRenderPageSkp(page, pdfium::kBlankPage612By792Checksum);
2061
2062 UnloadPage(page);
2063 }
2064 #endif // defined(_SKIA_SUPPORT_)
2065
TEST_F(FPDFViewEmbedderTest,NoSmoothTextItalicOverlappingGlyphs)2066 TEST_F(FPDFViewEmbedderTest, NoSmoothTextItalicOverlappingGlyphs) {
2067 ASSERT_TRUE(OpenDocument("bug_1919.pdf"));
2068 FPDF_PAGE page = LoadPage(0);
2069 ASSERT_TRUE(page);
2070
2071 const char* checksum = []() {
2072 #if !BUILDFLAG(IS_APPLE)
2073 if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) {
2074 return "ceeb93d2bcdb586d62c95b33cadcd873";
2075 }
2076 #endif
2077 return "4ef1f65ab1ac76acb97a3540dcb10b4e";
2078 }();
2079
2080 TestRenderPageBitmapWithFlags(page, FPDF_RENDER_NO_SMOOTHTEXT, checksum);
2081 UnloadPage(page);
2082 }
2083