blob: 1dc76fee5baaf34e96e5ff50a72714fc690eb3e6 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright (c) 2010 The Chromium 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
5#include <limits.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
tonikitoo3e981582016-08-26 08:37:10 -070010#include <map>
Tom Sepezdaa2e842015-01-29 15:44:37 -080011#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012#include <string>
13#include <utility>
Tom Sepez5ee12d72014-12-17 16:24:01 -080014#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Cary Clark399be5b2016-03-14 16:51:29 -040016#if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
17#define _SKIA_SUPPORT_
18#endif
19
stephanafa05e972017-01-02 06:19:41 -080020#include "core/fdrm/crypto/fx_crypt.h"
Tom Sepez1d17a042017-03-16 13:22:47 -070021#include "public/cpp/fpdf_deleters.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080022#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080023#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080024#include "public/fpdf_ext.h"
25#include "public/fpdf_formfill.h"
Dan Sinclairddcb6e72017-04-05 10:30:33 -040026#include "public/fpdf_structtree.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080027#include "public/fpdf_text.h"
28#include "public/fpdfview.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050029#include "samples/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080030#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070031
thestigf2b940c2016-10-13 06:48:47 -070032#ifdef _WIN32
33#include <io.h>
34#else
35#include <unistd.h>
36#endif
37
Tom Sepez452b4f32015-10-13 09:27:27 -070038#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080039#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070040#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080041#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Cary Clark399be5b2016-03-14 16:51:29 -040043#ifdef PDF_ENABLE_SKIA
44#include "third_party/skia/include/core/SkPictureRecorder.h"
45#include "third_party/skia/include/core/SkStream.h"
46#endif
47
thestigf2b940c2016-10-13 06:48:47 -070048#ifdef _WIN32
49#define access _access
50#define snprintf _snprintf
51#define R_OK 4
52#endif
53
Vitaly Buka9e0177a2014-07-22 18:15:42 -070054enum OutputFormat {
55 OUTPUT_NONE,
Dan Sinclair3c67fbd2017-04-05 16:07:50 -040056 OUTPUT_STRUCTURE,
dsinclairb63068f2016-06-16 07:58:09 -070057 OUTPUT_TEXT,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070058 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080059 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070060#ifdef _WIN32
61 OUTPUT_BMP,
62 OUTPUT_EMF,
63#endif
Cary Clark399be5b2016-03-14 16:51:29 -040064#ifdef PDF_ENABLE_SKIA
65 OUTPUT_SKP,
66#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070067};
68
Tom Sepez5ee12d72014-12-17 16:24:01 -080069struct Options {
tsepezf09bdfa2016-04-18 16:08:26 -070070 Options()
Dan Sinclair3c67fbd2017-04-05 16:07:50 -040071 : show_config(false),
npme3c73152016-11-14 09:14:52 -080072 send_events(false),
73 pages(false),
stephanafa05e972017-01-02 06:19:41 -080074 md5(false),
npme3c73152016-11-14 09:14:52 -080075 output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080076
Tom Sepez2991d8d2016-01-15 16:02:48 -080077 bool show_config;
tsepezf09bdfa2016-04-18 16:08:26 -070078 bool send_events;
npme3c73152016-11-14 09:14:52 -080079 bool pages;
stephanafa05e972017-01-02 06:19:41 -080080 bool md5;
Tom Sepez5ee12d72014-12-17 16:24:01 -080081 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080082 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080083 std::string exe_path;
84 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070085 std::string font_directory;
npmfa20cd52016-11-14 13:33:40 -080086 // 0-based page numbers to be rendered.
87 int first_page;
88 int last_page;
Tom Sepez5ee12d72014-12-17 16:24:01 -080089};
90
tonikitoo81d92f82016-09-21 12:44:56 -070091struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
92 // Hold a map of the currently loaded pages in order to avoid them
93 // to get loaded twice.
npmfa20cd52016-11-14 13:33:40 -080094 std::map<int, FPDF_PAGE> loaded_pages;
tonikitoo81d92f82016-09-21 12:44:56 -070095
96 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
97 // make use of it.
npmfa20cd52016-11-14 13:33:40 -080098 FPDF_FORMHANDLE form_handle;
tonikitoo81d92f82016-09-21 12:44:56 -070099};
100
101static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
npmfa20cd52016-11-14 13:33:40 -0800102 FPDF_FORMFILLINFO* form_fill_info) {
103 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info);
tonikitoo81d92f82016-09-21 12:44:56 -0700104}
105
Tom Sepezaf18cb32015-02-05 15:06:01 -0800106static bool CheckDimensions(int stride, int width, int height) {
107 if (stride < 0 || width < 0 || height < 0)
108 return false;
109 if (height > 0 && width > INT_MAX / height)
110 return false;
111 return true;
112}
113
stephanafa05e972017-01-02 06:19:41 -0800114static void OutputMD5Hash(const char* file_name, const char* buffer, int len) {
115 // Get the MD5 hash and write it to stdout.
116 uint8_t digest[16];
117 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(buffer), len, digest);
118 printf("MD5:%s:", file_name);
119 for (int i = 0; i < 16; i++)
120 printf("%02x", digest[i]);
121 printf("\n");
122}
123
124static std::string WritePpm(const char* pdf_name,
125 int num,
126 const void* buffer_void,
127 int stride,
128 int width,
129 int height) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700130 const char* buffer = reinterpret_cast<const char*>(buffer_void);
131
Tom Sepezaf18cb32015-02-05 15:06:01 -0800132 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800133 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800134
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135 int out_len = width * height;
136 if (out_len > INT_MAX / 3)
stephanafa05e972017-01-02 06:19:41 -0800137 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700138 out_len *= 3;
139
140 char filename[256];
141 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700142 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143 if (!fp)
stephanafa05e972017-01-02 06:19:41 -0800144 return "";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
146 // Source data is B, G, R, unused.
147 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -0700148 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -0700149 for (int h = 0; h < height; ++h) {
150 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700151 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700152 for (int w = 0; w < width; ++w) {
153 // R
154 dest_line[w * 3] = src_line[(w * 4) + 2];
155 // G
156 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
157 // B
158 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160 }
thestig514e8c92016-07-15 17:57:54 -0700161 fwrite(result.data(), out_len, 1, fp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162 fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800163 return std::string(filename);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164}
165
dsinclairb63068f2016-06-16 07:58:09 -0700166void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
167 char filename[256];
168 int chars_formatted =
169 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
170 if (chars_formatted < 0 ||
171 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
172 fprintf(stderr, "Filename %s is too long\n", filename);
173 return;
174 }
175
176 FILE* fp = fopen(filename, "w");
177 if (!fp) {
178 fprintf(stderr, "Failed to open %s for output\n", filename);
179 return;
180 }
181
182 // Output in UTF32-LE.
183 uint32_t bom = 0x0000FEFF;
184 fwrite(&bom, sizeof(bom), 1, fp);
185
Tom Sepez1d17a042017-03-16 13:22:47 -0700186 std::unique_ptr<void, FPDFTextPageDeleter> textpage(FPDFText_LoadPage(page));
187 for (int i = 0; i < FPDFText_CountChars(textpage.get()); i++) {
188 uint32_t c = FPDFText_GetUnicode(textpage.get(), i);
dsinclairb63068f2016-06-16 07:58:09 -0700189 fwrite(&c, sizeof(c), 1, fp);
190 }
dsinclairb63068f2016-06-16 07:58:09 -0700191 (void)fclose(fp);
192}
193
stephanafa05e972017-01-02 06:19:41 -0800194static std::string WritePng(const char* pdf_name,
195 int num,
196 const void* buffer_void,
197 int stride,
198 int width,
199 int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800200 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800201 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800202
203 std::vector<unsigned char> png_encoding;
204 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
205 if (!image_diff_png::EncodeBGRAPNG(
206 buffer, width, height, stride, false, &png_encoding)) {
207 fprintf(stderr, "Failed to convert bitmap to PNG\n");
stephanafa05e972017-01-02 06:19:41 -0800208 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800209 }
210
211 char filename[256];
212 int chars_formatted = snprintf(
213 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
214 if (chars_formatted < 0 ||
215 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400216 fprintf(stderr, "Filename %s is too long\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800217 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800218 }
219
220 FILE* fp = fopen(filename, "wb");
221 if (!fp) {
222 fprintf(stderr, "Failed to open %s for output\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800223 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800224 }
225
226 size_t bytes_written = fwrite(
227 &png_encoding.front(), 1, png_encoding.size(), fp);
228 if (bytes_written != png_encoding.size())
229 fprintf(stderr, "Failed to write to %s\n", filename);
230
Lei Zhang5377ebf2015-09-23 14:52:53 -0700231 (void)fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800232 return std::string(filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800233}
234
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700235#ifdef _WIN32
stephanafa05e972017-01-02 06:19:41 -0800236static std::string WriteBmp(const char* pdf_name,
237 int num,
238 const void* buffer,
239 int stride,
240 int width,
241 int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800242 if (!CheckDimensions(stride, width, height))
stephanafa05e972017-01-02 06:19:41 -0800243 return "";
Tom Sepezaf18cb32015-02-05 15:06:01 -0800244
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700245 int out_len = stride * height;
246 if (out_len > INT_MAX / 3)
stephanafa05e972017-01-02 06:19:41 -0800247 return "";
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700248
249 char filename[256];
250 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
251 FILE* fp = fopen(filename, "wb");
252 if (!fp)
stephanafa05e972017-01-02 06:19:41 -0800253 return "";
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700254
Nico Weber2827bdd2015-07-01 14:08:08 -0700255 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700256 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
257 bmi.bmiHeader.biWidth = width;
258 bmi.bmiHeader.biHeight = -height; // top-down image
259 bmi.bmiHeader.biPlanes = 1;
260 bmi.bmiHeader.biBitCount = 32;
261 bmi.bmiHeader.biCompression = BI_RGB;
262 bmi.bmiHeader.biSizeImage = 0;
263
Nico Weber2827bdd2015-07-01 14:08:08 -0700264 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700265 file_header.bfType = 0x4d42;
266 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
267 file_header.bfOffBits = file_header.bfSize - out_len;
268
269 fwrite(&file_header, sizeof(file_header), 1, fp);
270 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
271 fwrite(buffer, out_len, 1, fp);
272 fclose(fp);
stephanafa05e972017-01-02 06:19:41 -0800273 return std::string(filename);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700274}
275
276void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
277 int width = static_cast<int>(FPDF_GetPageWidth(page));
278 int height = static_cast<int>(FPDF_GetPageHeight(page));
279
280 char filename[256];
281 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
282
Lei Zhang5377ebf2015-09-23 14:52:53 -0700283 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800284
285 HRGN rgn = CreateRectRgn(0, 0, width, height);
286 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700287 DeleteObject(rgn);
288
289 SelectObject(dc, GetStockObject(NULL_PEN));
290 SelectObject(dc, GetStockObject(WHITE_BRUSH));
291 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
292 Rectangle(dc, 0, 0, width + 1, height + 1);
293
294 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
295 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
296
297 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
298}
299#endif
300
Cary Clark399be5b2016-03-14 16:51:29 -0400301#ifdef PDF_ENABLE_SKIA
stephanafa05e972017-01-02 06:19:41 -0800302static std::string WriteSkp(const char* pdf_name,
303 int num,
304 SkPictureRecorder* recorder) {
Cary Clark399be5b2016-03-14 16:51:29 -0400305 char filename[256];
306 int chars_formatted =
307 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
308
309 if (chars_formatted < 0 ||
310 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
311 fprintf(stderr, "Filename %s is too long\n", filename);
stephanafa05e972017-01-02 06:19:41 -0800312 return "";
Cary Clark399be5b2016-03-14 16:51:29 -0400313 }
314
thestigb97e07e2016-09-28 22:16:40 -0700315 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400316 SkFILEWStream wStream(filename);
317 picture->serialize(&wStream);
stephanafa05e972017-01-02 06:19:41 -0800318 return std::string(filename);
Cary Clark399be5b2016-03-14 16:51:29 -0400319}
320#endif
321
Tom Sepez58fb36a2016-02-01 10:32:14 -0800322// These example JS platform callback handlers are entirely optional,
323// and exist here to show the flow of information from a document back
324// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800325int ExampleAppAlert(IPDF_JSPLATFORM*,
326 FPDF_WIDESTRING msg,
327 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800328 int type,
329 int icon) {
Tom Sepezbd932572016-01-29 09:10:41 -0800330 printf("%ls", GetPlatformWString(title).c_str());
npmfa20cd52016-11-14 13:33:40 -0800331 if (icon || type)
332 printf("[icon=%d,type=%d]", icon, type);
Tom Sepezbd932572016-01-29 09:10:41 -0800333 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334 return 0;
335}
336
Tom Sepez58fb36a2016-02-01 10:32:14 -0800337int ExampleAppResponse(IPDF_JSPLATFORM*,
338 FPDF_WIDESTRING question,
339 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800340 FPDF_WIDESTRING default_value,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800341 FPDF_WIDESTRING label,
npmfa20cd52016-11-14 13:33:40 -0800342 FPDF_BOOL is_password,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800343 void* response,
344 int length) {
345 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
346 GetPlatformWString(title).c_str(),
347 GetPlatformWString(question).c_str(),
npmfa20cd52016-11-14 13:33:40 -0800348 GetPlatformWString(default_value).c_str(),
349 GetPlatformWString(label).c_str(), is_password, length);
Tom Sepez58fb36a2016-02-01 10:32:14 -0800350
351 // UTF-16, always LE regardless of platform.
352 uint8_t* ptr = static_cast<uint8_t*>(response);
353 ptr[0] = 'N';
354 ptr[1] = 0;
355 ptr[2] = 'o';
356 ptr[3] = 0;
357 return 4;
358}
359
npmfa20cd52016-11-14 13:33:40 -0800360void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) {
361 printf("Goto Page: %d\n", page_number);
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800362}
363
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800364void ExampleDocMail(IPDF_JSPLATFORM*,
365 void* mailData,
366 int length,
npmfa20cd52016-11-14 13:33:40 -0800367 FPDF_BOOL UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800368 FPDF_WIDESTRING To,
369 FPDF_WIDESTRING Subject,
370 FPDF_WIDESTRING CC,
371 FPDF_WIDESTRING BCC,
372 FPDF_WIDESTRING Msg) {
npmfa20cd52016-11-14 13:33:40 -0800373 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800374 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
375 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
376 GetPlatformWString(Msg).c_str());
377}
378
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800379void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380 std::string feature = "Unknown";
381 switch (type) {
382 case FPDF_UNSP_DOC_XFAFORM:
383 feature = "XFA";
384 break;
385 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
386 feature = "Portfolios_Packages";
387 break;
388 case FPDF_UNSP_DOC_ATTACHMENT:
389 case FPDF_UNSP_ANNOT_ATTACHMENT:
390 feature = "Attachment";
391 break;
392 case FPDF_UNSP_DOC_SECURITY:
393 feature = "Rights_Management";
394 break;
395 case FPDF_UNSP_DOC_SHAREDREVIEW:
396 feature = "Shared_Review";
397 break;
398 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
399 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
400 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
401 feature = "Shared_Form";
402 break;
403 case FPDF_UNSP_ANNOT_3DANNOT:
404 feature = "3D";
405 break;
406 case FPDF_UNSP_ANNOT_MOVIE:
407 feature = "Movie";
408 break;
409 case FPDF_UNSP_ANNOT_SOUND:
410 feature = "Sound";
411 break;
412 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
413 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
414 feature = "Screen";
415 break;
416 case FPDF_UNSP_ANNOT_SIG:
417 feature = "Digital_Signature";
418 break;
419 }
420 printf("Unsupported feature: %s.\n", feature.c_str());
421}
422
Tom Sepez5ee12d72014-12-17 16:24:01 -0800423bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700424 Options* options,
425 std::vector<std::string>* files) {
426 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800427 return false;
thestig514e8c92016-07-15 17:57:54 -0700428
Tom Sepez5ee12d72014-12-17 16:24:01 -0800429 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800430 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800431 for (; cur_idx < args.size(); ++cur_idx) {
432 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800433 if (cur_arg == "--show-config") {
434 options->show_config = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700435 } else if (cur_arg == "--send-events") {
436 options->send_events = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800437 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800438 if (options->output_format != OUTPUT_NONE) {
439 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
440 return false;
441 }
442 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800443 } else if (cur_arg == "--png") {
444 if (options->output_format != OUTPUT_NONE) {
445 fprintf(stderr, "Duplicate or conflicting --png argument\n");
446 return false;
447 }
448 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700449 } else if (cur_arg == "--txt") {
450 if (options->output_format != OUTPUT_NONE) {
451 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
452 return false;
453 }
454 options->output_format = OUTPUT_TEXT;
Cary Clark399be5b2016-03-14 16:51:29 -0400455#ifdef PDF_ENABLE_SKIA
456 } else if (cur_arg == "--skp") {
457 if (options->output_format != OUTPUT_NONE) {
458 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
459 return false;
460 }
461 options->output_format = OUTPUT_SKP;
462#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700463 } else if (cur_arg.size() > 11 &&
464 cur_arg.compare(0, 11, "--font-dir=") == 0) {
465 if (!options->font_directory.empty()) {
466 fprintf(stderr, "Duplicate --font-dir argument\n");
467 return false;
468 }
469 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700470#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500471 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800472 if (options->output_format != OUTPUT_NONE) {
473 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
474 return false;
475 }
476 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500477 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800478 if (options->output_format != OUTPUT_NONE) {
479 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
480 return false;
481 }
482 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800483#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500484
Tom Sepez452b4f32015-10-13 09:27:27 -0700485#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800486#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500487 } else if (cur_arg.size() > 10 &&
488 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800489 if (!options->bin_directory.empty()) {
490 fprintf(stderr, "Duplicate --bin-dir argument\n");
491 return false;
492 }
493 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800494#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700495#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500496
497 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800498 if (!options->scale_factor_as_string.empty()) {
499 fprintf(stderr, "Duplicate --scale argument\n");
500 return false;
501 }
502 options->scale_factor_as_string = cur_arg.substr(8);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400503 } else if (cur_arg == "--show-structure") {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400504 if (options->output_format != OUTPUT_NONE) {
505 fprintf(stderr, "Duplicate or conflicting --show-structure argument\n");
506 return false;
507 }
508 options->output_format = OUTPUT_STRUCTURE;
npme3c73152016-11-14 09:14:52 -0800509 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--pages=") == 0) {
510 if (options->pages) {
511 fprintf(stderr, "Duplicate --pages argument\n");
512 return false;
513 }
514 options->pages = true;
npmfa20cd52016-11-14 13:33:40 -0800515 const std::string pages_string = cur_arg.substr(8);
516 size_t first_dash = pages_string.find("-");
517 if (first_dash == std::string::npos) {
518 std::stringstream(pages_string) >> options->first_page;
519 options->last_page = options->first_page;
npme3c73152016-11-14 09:14:52 -0800520 } else {
npmfa20cd52016-11-14 13:33:40 -0800521 std::stringstream(pages_string.substr(0, first_dash)) >>
522 options->first_page;
523 std::stringstream(pages_string.substr(first_dash + 1)) >>
524 options->last_page;
npme3c73152016-11-14 09:14:52 -0800525 }
stephanafa05e972017-01-02 06:19:41 -0800526 } else if (cur_arg == "--md5") {
527 options->md5 = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800528 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
529 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
530 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500531 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700532 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500533 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700534 }
thestig514e8c92016-07-15 17:57:54 -0700535 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800536 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700537
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538 return true;
539}
540
npmfa20cd52016-11-14 13:33:40 -0800541FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542 return true;
543}
544
npmfa20cd52016-11-14 13:33:40 -0800545void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700546
tsepezf09bdfa2016-04-18 16:08:26 -0700547void SendPageEvents(const FPDF_FORMHANDLE& form,
548 const FPDF_PAGE& page,
549 const std::string& events) {
550 auto lines = StringSplit(events, '\n');
551 for (auto line : lines) {
552 auto command = StringSplit(line, '#');
553 if (command[0].empty())
554 continue;
555 auto tokens = StringSplit(command[0], ',');
556 if (tokens[0] == "keycode") {
557 if (tokens.size() == 2) {
558 int keycode = atoi(tokens[1].c_str());
559 FORM_OnKeyDown(form, page, keycode, 0);
560 FORM_OnKeyUp(form, page, keycode, 0);
561 } else {
562 fprintf(stderr, "keycode: bad args\n");
563 }
564 } else if (tokens[0] == "mousedown") {
565 if (tokens.size() == 4) {
566 int x = atoi(tokens[2].c_str());
567 int y = atoi(tokens[3].c_str());
568 if (tokens[1] == "left")
569 FORM_OnLButtonDown(form, page, 0, x, y);
570#ifdef PDF_ENABLE_XFA
571 else if (tokens[1] == "right")
572 FORM_OnRButtonDown(form, page, 0, x, y);
573#endif
574 else
575 fprintf(stderr, "mousedown: bad button name\n");
576 } else {
577 fprintf(stderr, "mousedown: bad args\n");
578 }
579 } else if (tokens[0] == "mouseup") {
580 if (tokens.size() == 4) {
581 int x = atoi(tokens[2].c_str());
582 int y = atoi(tokens[3].c_str());
583 if (tokens[1] == "left")
584 FORM_OnLButtonUp(form, page, 0, x, y);
585#ifdef PDF_ENABLE_XFA
586 else if (tokens[1] == "right")
587 FORM_OnRButtonUp(form, page, 0, x, y);
588#endif
589 else
590 fprintf(stderr, "mouseup: bad button name\n");
591 } else {
592 fprintf(stderr, "mouseup: bad args\n");
593 }
594 } else if (tokens[0] == "mousemove") {
595 if (tokens.size() == 3) {
596 int x = atoi(tokens[1].c_str());
597 int y = atoi(tokens[2].c_str());
598 FORM_OnMouseMove(form, page, 0, x, y);
599 } else {
600 fprintf(stderr, "mousemove: bad args\n");
601 }
602 } else {
603 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
604 }
605 }
606}
607
tonikitoo3e981582016-08-26 08:37:10 -0700608FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
609 FPDF_DOCUMENT doc,
610 int index) {
npmfa20cd52016-11-14 13:33:40 -0800611 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info =
612 ToPDFiumTestFormFillInfo(param);
613 auto& loaded_pages = form_fill_info->loaded_pages;
npmfa20cd52016-11-14 13:33:40 -0800614 auto iter = loaded_pages.find(index);
615 if (iter != loaded_pages.end())
tonikitoo3e981582016-08-26 08:37:10 -0700616 return iter->second;
617
618 FPDF_PAGE page = FPDF_LoadPage(doc, index);
619 if (!page)
620 return nullptr;
621
npmfa20cd52016-11-14 13:33:40 -0800622 FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle;
npmfa20cd52016-11-14 13:33:40 -0800623 FORM_OnAfterLoadPage(page, form_handle);
624 FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN);
npmfa20cd52016-11-14 13:33:40 -0800625 loaded_pages[index] = page;
tonikitoo3e981582016-08-26 08:37:10 -0700626 return page;
627}
628
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400629std::wstring ConvertToWString(const unsigned short* buf,
630 unsigned long buf_size) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400631 std::wstring result;
632 result.reserve(buf_size);
633 std::copy(buf, buf + buf_size, std::back_inserter(result));
634 return result;
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400635}
636
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400637void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent) {
638 static const size_t kBufSize = 1024;
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400639 unsigned short buf[kBufSize];
640 unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize);
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400641 printf("%*s%ls", indent * 2, "", ConvertToWString(buf, len).c_str());
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400642
643 memset(buf, 0, sizeof(buf));
644 len = FPDF_StructElement_GetAltText(child, buf, kBufSize);
645 if (len > 0)
646 printf(" (%ls)", ConvertToWString(buf, len).c_str());
647 printf("\n");
648
649 for (int i = 0; i < FPDF_StructElement_CountChildren(child); ++i) {
650 FPDF_STRUCTELEMENT sub_child = FPDF_StructElement_GetChildAtIndex(child, i);
651 // If the child is not an Element then this will return null. This can
652 // happen if the element is things like an object reference or a stream.
653 if (!sub_child)
654 continue;
655
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400656 DumpChildStructure(sub_child, indent + 1);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400657 }
658}
659
660void DumpPageStructure(FPDF_PAGE page, const int page_idx) {
661 std::unique_ptr<void, FPDFStructTreeDeleter> tree(
662 FPDF_StructTree_GetForPage(page));
663 if (!tree) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400664 fprintf(stderr, "Failed to load struct tree for page %d\n", page_idx);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400665 return;
666 }
667
668 printf("Structure Tree for Page %d\n", page_idx);
669 for (int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) {
670 FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i);
671 if (!child) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400672 fprintf(stderr, "Failed to load child %d for page %d\n", i, page_idx);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400673 continue;
674 }
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400675 DumpChildStructure(child, 0);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400676 }
677 printf("\n\n");
678}
679
Jun Fangb553bcb2015-11-10 18:49:04 +0800680bool RenderPage(const std::string& name,
tonikitoo3e981582016-08-26 08:37:10 -0700681 FPDF_DOCUMENT doc,
Tom Sepez1d17a042017-03-16 13:22:47 -0700682 FPDF_FORMHANDLE form,
npmfa20cd52016-11-14 13:33:40 -0800683 FPDF_FORMFILLINFO_PDFiumTest& form_fill_info,
Jun Fangb553bcb2015-11-10 18:49:04 +0800684 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -0700685 const Options& options,
686 const std::string& events) {
Tom Sepez1d17a042017-03-16 13:22:47 -0700687 std::unique_ptr<void, FPDFPageDeleter> page(
688 GetPageForIndex(&form_fill_info, doc, page_index));
689 if (!page.get())
Jun Fangb553bcb2015-11-10 18:49:04 +0800690 return false;
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400691 if (options.send_events)
692 SendPageEvents(form, page.get(), events);
693 if (options.output_format == OUTPUT_STRUCTURE) {
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400694 DumpPageStructure(page.get(), page_index);
695 return true;
696 }
697
Tom Sepez1d17a042017-03-16 13:22:47 -0700698 std::unique_ptr<void, FPDFTextPageDeleter> text_page(
699 FPDFText_LoadPage(page.get()));
tsepezf09bdfa2016-04-18 16:08:26 -0700700
Jun Fangdf7f3662015-11-10 18:29:18 +0800701 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700702 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800703 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700704
Tom Sepez1d17a042017-03-16 13:22:47 -0700705 int width = static_cast<int>(FPDF_GetPageWidth(page.get()) * scale);
706 int height = static_cast<int>(FPDF_GetPageHeight(page.get()) * scale);
707 int alpha = FPDFPage_HasTransparency(page.get()) ? 1 : 0;
708 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
709 FPDFBitmap_Create(width, height, alpha));
710
thestige97ea032016-05-19 10:59:15 -0700711 if (bitmap) {
712 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
Tom Sepez1d17a042017-03-16 13:22:47 -0700713 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
714 FPDF_RenderPageBitmap(bitmap.get(), page.get(), 0, 0, width, height, 0,
715 FPDF_ANNOT);
Jun Fangdf7f3662015-11-10 18:29:18 +0800716
Tom Sepez1d17a042017-03-16 13:22:47 -0700717 FPDF_FFLDraw(form, bitmap.get(), page.get(), 0, 0, width, height, 0,
718 FPDF_ANNOT);
719 int stride = FPDFBitmap_GetStride(bitmap.get());
thestige97ea032016-05-19 10:59:15 -0700720 const char* buffer =
Tom Sepez1d17a042017-03-16 13:22:47 -0700721 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get()));
Jun Fangdf7f3662015-11-10 18:29:18 +0800722
stephanafa05e972017-01-02 06:19:41 -0800723 std::string&& image_file_name = "";
thestige97ea032016-05-19 10:59:15 -0700724 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800725#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -0700726 case OUTPUT_BMP:
stephanafa05e972017-01-02 06:19:41 -0800727 image_file_name =
728 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700729 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800730
thestige97ea032016-05-19 10:59:15 -0700731 case OUTPUT_EMF:
Tom Sepez1d17a042017-03-16 13:22:47 -0700732 WriteEmf(page.get(), name.c_str(), page_index);
thestige97ea032016-05-19 10:59:15 -0700733 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800734#endif
dsinclairb63068f2016-06-16 07:58:09 -0700735 case OUTPUT_TEXT:
Tom Sepez1d17a042017-03-16 13:22:47 -0700736 WriteText(page.get(), name.c_str(), page_index);
dsinclairb63068f2016-06-16 07:58:09 -0700737 break;
738
thestige97ea032016-05-19 10:59:15 -0700739 case OUTPUT_PNG:
stephanafa05e972017-01-02 06:19:41 -0800740 image_file_name =
741 WritePng(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700742 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800743
thestige97ea032016-05-19 10:59:15 -0700744 case OUTPUT_PPM:
stephanafa05e972017-01-02 06:19:41 -0800745 image_file_name =
746 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700747 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800748
Cary Clark399be5b2016-03-14 16:51:29 -0400749#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -0700750 case OUTPUT_SKP: {
751 std::unique_ptr<SkPictureRecorder> recorder(
thestigb97e07e2016-09-28 22:16:40 -0700752 reinterpret_cast<SkPictureRecorder*>(
Tom Sepez1d17a042017-03-16 13:22:47 -0700753 FPDF_RenderPageSkp(page.get(), width, height)));
754 FPDF_FFLRecord(form, recorder.get(), page.get(), 0, 0, width, height, 0,
755 0);
stephanafa05e972017-01-02 06:19:41 -0800756 image_file_name = WriteSkp(name.c_str(), page_index, recorder.get());
thestige97ea032016-05-19 10:59:15 -0700757 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400758#endif
thestige97ea032016-05-19 10:59:15 -0700759 default:
760 break;
761 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800762
stephanafa05e972017-01-02 06:19:41 -0800763 // Write the filename and the MD5 of the buffer to stdout if we wrote a
764 // file.
765 if (options.md5 && image_file_name != "")
766 OutputMD5Hash(image_file_name.c_str(), buffer, stride * height);
thestige97ea032016-05-19 10:59:15 -0700767 } else {
768 fprintf(stderr, "Page was too large to be rendered.\n");
769 }
tonikitoo3e981582016-08-26 08:37:10 -0700770
npmfa20cd52016-11-14 13:33:40 -0800771 form_fill_info.loaded_pages.erase(page_index);
Tom Sepez1d17a042017-03-16 13:22:47 -0700772 FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_CLOSE);
773 FORM_OnBeforeClosePage(page.get(), form);
thestige97ea032016-05-19 10:59:15 -0700774 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800775}
776
tsepezf09bdfa2016-04-18 16:08:26 -0700777void RenderPdf(const std::string& name,
778 const char* pBuf,
779 size_t len,
780 const Options& options,
781 const std::string& events) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700782 IPDF_JSPLATFORM platform_callbacks;
783 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700784 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800785 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800786 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800787 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800788 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700789
tonikitoo81d92f82016-09-21 12:44:56 -0700790 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
Tom Sepezc46d0002015-11-30 15:46:36 -0800791#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800792 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800793#else // PDF_ENABLE_XFA
794 form_callbacks.version = 1;
795#endif // PDF_ENABLE_XFA
tonikitoo3e981582016-08-26 08:37:10 -0700796 form_callbacks.FFI_GetPage = GetPageForIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700797 form_callbacks.m_pJsPlatform = &platform_callbacks;
798
799 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700800 FPDF_FILEACCESS file_access;
801 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700802 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700803 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700804 file_access.m_Param = &loader;
805
806 FX_FILEAVAIL file_avail;
807 memset(&file_avail, '\0', sizeof(file_avail));
808 file_avail.version = 1;
809 file_avail.IsDataAvail = Is_Data_Avail;
810
811 FX_DOWNLOADHINTS hints;
812 memset(&hints, '\0', sizeof(hints));
813 hints.version = 1;
814 hints.AddSegment = Add_Segment;
815
Jun Fangdf7f3662015-11-10 18:29:18 +0800816 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800817 bool bIsLinearized = false;
Tom Sepez1d17a042017-03-16 13:22:47 -0700818 std::unique_ptr<void, FPDFDocumentDeleter> doc;
819 std::unique_ptr<void, FPDFAvailDeleter> pdf_avail(
820 FPDFAvail_Create(&file_avail, &file_access));
Tom Sepezc98895c2015-11-24 15:30:36 -0800821
Tom Sepez1d17a042017-03-16 13:22:47 -0700822 if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) {
823 doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr));
Jun Fangdf7f3662015-11-10 18:29:18 +0800824 if (doc) {
thestig514e8c92016-07-15 17:57:54 -0700825 while (nRet == PDF_DATA_NOTAVAIL)
Tom Sepez1d17a042017-03-16 13:22:47 -0700826 nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
thestig514e8c92016-07-15 17:57:54 -0700827
Jun Fangdf7f3662015-11-10 18:29:18 +0800828 if (nRet == PDF_DATA_ERROR) {
829 fprintf(stderr, "Unknown error in checking if doc was available.\n");
830 return;
831 }
Tom Sepez1d17a042017-03-16 13:22:47 -0700832 nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
Jun Fangdf7f3662015-11-10 18:29:18 +0800833 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
834 fprintf(stderr,
835 "Error %d was returned in checking if form was available.\n",
836 nRet);
837 return;
838 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800839 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800840 }
841 } else {
Tom Sepez1d17a042017-03-16 13:22:47 -0700842 doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr));
Jun Fangdf7f3662015-11-10 18:29:18 +0800843 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700844
Lei Zhang5377ebf2015-09-23 14:52:53 -0700845 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400846 unsigned long err = FPDF_GetLastError();
847 fprintf(stderr, "Load pdf docs unsuccessful: ");
848 switch (err) {
849 case FPDF_ERR_SUCCESS:
850 fprintf(stderr, "Success");
851 break;
852 case FPDF_ERR_UNKNOWN:
853 fprintf(stderr, "Unknown error");
854 break;
855 case FPDF_ERR_FILE:
856 fprintf(stderr, "File not found or could not be opened");
857 break;
858 case FPDF_ERR_FORMAT:
859 fprintf(stderr, "File not in PDF format or corrupted");
860 break;
861 case FPDF_ERR_PASSWORD:
862 fprintf(stderr, "Password required or incorrect password");
863 break;
864 case FPDF_ERR_SECURITY:
865 fprintf(stderr, "Unsupported security scheme");
866 break;
867 case FPDF_ERR_PAGE:
868 fprintf(stderr, "Page not found or content error");
869 break;
870 default:
871 fprintf(stderr, "Unknown error %ld", err);
872 }
873 fprintf(stderr, ".\n");
JUN FANG827a1722015-03-05 13:39:21 -0800874 return;
875 }
876
Tom Sepez1d17a042017-03-16 13:22:47 -0700877 (void)FPDF_GetDocPermissions(doc.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700878
Tom Sepez1d17a042017-03-16 13:22:47 -0700879 std::unique_ptr<void, FPDFFormHandleDeleter> form(
880 FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
881 form_callbacks.form_handle = form.get();
tonikitoo81d92f82016-09-21 12:44:56 -0700882
Tom Sepezc46d0002015-11-30 15:46:36 -0800883#ifdef PDF_ENABLE_XFA
thestigb97e07e2016-09-28 22:16:40 -0700884 int doc_type = DOCTYPE_PDF;
Tom Sepez1d17a042017-03-16 13:22:47 -0700885 if (FPDF_HasXFAField(doc.get(), &doc_type) && doc_type != DOCTYPE_PDF &&
886 !FPDF_LoadXFA(doc.get())) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700887 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800888 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800889#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700890
Tom Sepez1d17a042017-03-16 13:22:47 -0700891 FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD);
892 FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
893 FORM_DoDocumentJSAction(form.get());
894 FORM_DoDocumentOpenAction(form.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700895
Tom Sepez1d17a042017-03-16 13:22:47 -0700896 int page_count = FPDF_GetPageCount(doc.get());
Tom Sepez1ed8a212015-05-11 15:25:39 -0700897 int rendered_pages = 0;
898 int bad_pages = 0;
npmfa20cd52016-11-14 13:33:40 -0800899 int first_page = options.pages ? options.first_page : 0;
900 int last_page = options.pages ? options.last_page + 1 : page_count;
901 for (int i = first_page; i < last_page; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800902 if (bIsLinearized) {
903 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -0700904 while (nRet == PDF_DATA_NOTAVAIL)
Tom Sepez1d17a042017-03-16 13:22:47 -0700905 nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
thestig514e8c92016-07-15 17:57:54 -0700906
Jun Fangdf7f3662015-11-10 18:29:18 +0800907 if (nRet == PDF_DATA_ERROR) {
908 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
909 i);
910 return;
911 }
912 }
Tom Sepez1d17a042017-03-16 13:22:47 -0700913 if (RenderPage(name, doc.get(), form.get(), form_callbacks, i, options,
914 events))
Jun Fangdf7f3662015-11-10 18:29:18 +0800915 ++rendered_pages;
thestig514e8c92016-07-15 17:57:54 -0700916 else
Lei Zhang5377ebf2015-09-23 14:52:53 -0700917 ++bad_pages;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700918 }
919
Tom Sepez1d17a042017-03-16 13:22:47 -0700920 FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700921 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -0700922 if (bad_pages)
923 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700924}
925
Tom Sepez2991d8d2016-01-15 16:02:48 -0800926static void ShowConfig() {
927 std::string config;
928 std::string maybe_comma;
929#if PDF_ENABLE_V8
930 config.append(maybe_comma);
931 config.append("V8");
932 maybe_comma = ",";
933#endif // PDF_ENABLE_V8
934#ifdef V8_USE_EXTERNAL_STARTUP_DATA
935 config.append(maybe_comma);
936 config.append("V8_EXTERNAL");
937 maybe_comma = ",";
938#endif // V8_USE_EXTERNAL_STARTUP_DATA
939#ifdef PDF_ENABLE_XFA
940 config.append(maybe_comma);
941 config.append("XFA");
942 maybe_comma = ",";
943#endif // PDF_ENABLE_XFA
dan sinclair00d40642017-01-30 19:48:54 -0800944#ifdef PDF_ENABLE_ASAN
945 config.append(maybe_comma);
946 config.append("ASAN");
947 maybe_comma = ",";
948#endif // PDF_ENABLE_ASAN
Tom Sepez2991d8d2016-01-15 16:02:48 -0800949 printf("%s\n", config.c_str());
950}
951
thestig514e8c92016-07-15 17:57:54 -0700952static const char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800953 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800954 " --show-config - print build options and exit\n"
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400955 " --show-structure - print the structure elements from the document\n"
tsepez10b01bf2016-05-04 12:52:42 -0700956 " --send-events - send input described by .evt file\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700957 " --bin-dir=<path> - override path to v8 external data\n"
958 " --font-dir=<path> - override path to external fonts\n"
959 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
npmfa20cd52016-11-14 13:33:40 -0800960 " --pages=<number>(-<number>) - only render the given 0-based page(s)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800961#ifdef _WIN32
962 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
963 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800964#endif // _WIN32
thestig514e8c92016-07-15 17:57:54 -0700965 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800966 " --png - write page images <pdf-name>.<page-number>.png\n"
thestig514e8c92016-07-15 17:57:54 -0700967 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400968#ifdef PDF_ENABLE_SKIA
969 " --skp - write page images <pdf-name>.<page-number>.skp\n"
970#endif
stephanafa05e972017-01-02 06:19:41 -0800971 " --md5 - write output image paths and their md5 hashes to stdout.\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400972 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800973
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700974int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800975 std::vector<std::string> args(argv, argv + argc);
976 Options options;
thestig514e8c92016-07-15 17:57:54 -0700977 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800978 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -0700979 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700980 return 1;
981 }
982
Tom Sepez2991d8d2016-01-15 16:02:48 -0800983 if (options.show_config) {
984 ShowConfig();
985 return 0;
986 }
987
988 if (files.empty()) {
989 fprintf(stderr, "No input files.\n");
990 return 1;
991 }
992
Tom Sepez452b4f32015-10-13 09:27:27 -0700993#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700994 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800995#ifdef V8_USE_EXTERNAL_STARTUP_DATA
996 v8::StartupData natives;
997 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700998 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
999 &snapshot, &platform);
1000#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -07001001 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -08001002#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -07001003#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -08001004
Tom Sepeza72e8e22015-10-07 10:17:53 -07001005 FPDF_LIBRARY_CONFIG config;
1006 config.version = 2;
1007 config.m_pUserFontPaths = nullptr;
1008 config.m_pIsolate = nullptr;
1009 config.m_v8EmbedderSlot = 0;
1010
1011 const char* path_array[2];
1012 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -07001013 path_array[0] = options.font_directory.c_str();
1014 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -07001015 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -07001016 }
Tom Sepeza72e8e22015-10-07 10:17:53 -07001017 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001018
npmfa20cd52016-11-14 13:33:40 -08001019 UNSUPPORT_INFO unsupported_info;
1020 memset(&unsupported_info, '\0', sizeof(unsupported_info));
1021 unsupported_info.version = 1;
1022 unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001023
npmfa20cd52016-11-14 13:33:40 -08001024 FSDK_SetUnSpObjProcessHandler(&unsupported_info);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001025
thestig514e8c92016-07-15 17:57:54 -07001026 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -08001027 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -08001028 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
1029 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -07001030 if (!file_contents)
1031 continue;
tsepez10b01bf2016-05-04 12:52:42 -07001032 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -07001033 std::string events;
1034 if (options.send_events) {
1035 std::string event_filename = filename;
1036 size_t event_length = 0;
1037 size_t extension_pos = event_filename.find(".pdf");
1038 if (extension_pos != std::string::npos) {
1039 event_filename.replace(extension_pos, 4, ".evt");
thestigf2b940c2016-10-13 06:48:47 -07001040 if (access(event_filename.c_str(), R_OK) == 0) {
1041 fprintf(stderr, "Using event file %s.\n", event_filename.c_str());
1042 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
1043 GetFileContents(event_filename.c_str(), &event_length);
1044 if (event_contents) {
1045 fprintf(stderr, "Sending events from: %s\n",
1046 event_filename.c_str());
1047 events = std::string(event_contents.get(), event_length);
1048 }
tsepezf09bdfa2016-04-18 16:08:26 -07001049 }
1050 }
1051 }
1052 RenderPdf(filename, file_contents.get(), file_length, options, events);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001053 }
1054
1055 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -07001056#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -08001057 v8::V8::ShutdownPlatform();
1058 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -07001059
1060#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1061 free(const_cast<char*>(natives.data));
1062 free(const_cast<char*>(snapshot.data));
1063#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -07001064#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001065
1066 return 0;
1067}