blob: a930f9435035dc05313b758657fffe8026c1f4f5 [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
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080021#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080022#include "public/fpdf_ext.h"
23#include "public/fpdf_formfill.h"
24#include "public/fpdf_text.h"
25#include "public/fpdfview.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050026#include "samples/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080027#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070028
Tom Sepez452b4f32015-10-13 09:27:27 -070029#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080030#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070031#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080032#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
34#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080035#define snprintf _snprintf
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036#endif
37
Cary Clark399be5b2016-03-14 16:51:29 -040038#ifdef PDF_ENABLE_SKIA
39#include "third_party/skia/include/core/SkPictureRecorder.h"
40#include "third_party/skia/include/core/SkStream.h"
41#endif
42
Vitaly Buka9e0177a2014-07-22 18:15:42 -070043enum OutputFormat {
44 OUTPUT_NONE,
dsinclairb63068f2016-06-16 07:58:09 -070045 OUTPUT_TEXT,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070046 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080047 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070048#ifdef _WIN32
49 OUTPUT_BMP,
50 OUTPUT_EMF,
51#endif
Cary Clark399be5b2016-03-14 16:51:29 -040052#ifdef PDF_ENABLE_SKIA
53 OUTPUT_SKP,
54#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070055};
56
Tom Sepez5ee12d72014-12-17 16:24:01 -080057struct Options {
tsepezf09bdfa2016-04-18 16:08:26 -070058 Options()
59 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080060
Tom Sepez2991d8d2016-01-15 16:02:48 -080061 bool show_config;
tsepezf09bdfa2016-04-18 16:08:26 -070062 bool send_events;
Tom Sepez5ee12d72014-12-17 16:24:01 -080063 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080064 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080065 std::string exe_path;
66 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070067 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080068};
69
tonikitoo81d92f82016-09-21 12:44:56 -070070struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
71 // Hold a map of the currently loaded pages in order to avoid them
72 // to get loaded twice.
73 std::map<int, FPDF_PAGE> loadedPages;
74
75 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
76 // make use of it.
77 FPDF_FORMHANDLE formHandle;
78};
79
80static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
81 FPDF_FORMFILLINFO* formFillInfo) {
82 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo);
83}
84
Tom Sepezaf18cb32015-02-05 15:06:01 -080085static bool CheckDimensions(int stride, int width, int height) {
86 if (stride < 0 || width < 0 || height < 0)
87 return false;
88 if (height > 0 && width > INT_MAX / height)
89 return false;
90 return true;
91}
92
Vitaly Buka9e0177a2014-07-22 18:15:42 -070093static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
94 int stride, int width, int height) {
95 const char* buffer = reinterpret_cast<const char*>(buffer_void);
96
Tom Sepezaf18cb32015-02-05 15:06:01 -080097 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080099
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100 int out_len = width * height;
101 if (out_len > INT_MAX / 3)
102 return;
103 out_len *= 3;
104
105 char filename[256];
106 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700107 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108 if (!fp)
109 return;
110 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
111 // Source data is B, G, R, unused.
112 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -0700113 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -0700114 for (int h = 0; h < height; ++h) {
115 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700116 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700117 for (int w = 0; w < width; ++w) {
118 // R
119 dest_line[w * 3] = src_line[(w * 4) + 2];
120 // G
121 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
122 // B
123 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125 }
thestig514e8c92016-07-15 17:57:54 -0700126 fwrite(result.data(), out_len, 1, fp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127 fclose(fp);
128}
129
dsinclairb63068f2016-06-16 07:58:09 -0700130void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
131 char filename[256];
132 int chars_formatted =
133 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
134 if (chars_formatted < 0 ||
135 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
136 fprintf(stderr, "Filename %s is too long\n", filename);
137 return;
138 }
139
140 FILE* fp = fopen(filename, "w");
141 if (!fp) {
142 fprintf(stderr, "Failed to open %s for output\n", filename);
143 return;
144 }
145
146 // Output in UTF32-LE.
147 uint32_t bom = 0x0000FEFF;
148 fwrite(&bom, sizeof(bom), 1, fp);
149
150 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
151 for (int i = 0; i < FPDFText_CountChars(textpage); i++) {
152 uint32_t c = FPDFText_GetUnicode(textpage, i);
153 fwrite(&c, sizeof(c), 1, fp);
154 }
155
156 FPDFText_ClosePage(textpage);
157
158 (void)fclose(fp);
159}
160
Tom Sepezaf18cb32015-02-05 15:06:01 -0800161static void WritePng(const char* pdf_name, int num, const void* buffer_void,
162 int stride, int width, int height) {
163 if (!CheckDimensions(stride, width, height))
164 return;
165
166 std::vector<unsigned char> png_encoding;
167 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
168 if (!image_diff_png::EncodeBGRAPNG(
169 buffer, width, height, stride, false, &png_encoding)) {
170 fprintf(stderr, "Failed to convert bitmap to PNG\n");
171 return;
172 }
173
174 char filename[256];
175 int chars_formatted = snprintf(
176 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
177 if (chars_formatted < 0 ||
178 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400179 fprintf(stderr, "Filename %s is too long\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800180 return;
181 }
182
183 FILE* fp = fopen(filename, "wb");
184 if (!fp) {
185 fprintf(stderr, "Failed to open %s for output\n", filename);
186 return;
187 }
188
189 size_t bytes_written = fwrite(
190 &png_encoding.front(), 1, png_encoding.size(), fp);
191 if (bytes_written != png_encoding.size())
192 fprintf(stderr, "Failed to write to %s\n", filename);
193
Lei Zhang5377ebf2015-09-23 14:52:53 -0700194 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800195}
196
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700197#ifdef _WIN32
198static void WriteBmp(const char* pdf_name, int num, const void* buffer,
199 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800200 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700201 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800202
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700203 int out_len = stride * height;
204 if (out_len > INT_MAX / 3)
205 return;
206
207 char filename[256];
208 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
209 FILE* fp = fopen(filename, "wb");
210 if (!fp)
211 return;
212
Nico Weber2827bdd2015-07-01 14:08:08 -0700213 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700214 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
215 bmi.bmiHeader.biWidth = width;
216 bmi.bmiHeader.biHeight = -height; // top-down image
217 bmi.bmiHeader.biPlanes = 1;
218 bmi.bmiHeader.biBitCount = 32;
219 bmi.bmiHeader.biCompression = BI_RGB;
220 bmi.bmiHeader.biSizeImage = 0;
221
Nico Weber2827bdd2015-07-01 14:08:08 -0700222 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700223 file_header.bfType = 0x4d42;
224 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
225 file_header.bfOffBits = file_header.bfSize - out_len;
226
227 fwrite(&file_header, sizeof(file_header), 1, fp);
228 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
229 fwrite(buffer, out_len, 1, fp);
230 fclose(fp);
231}
232
233void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
234 int width = static_cast<int>(FPDF_GetPageWidth(page));
235 int height = static_cast<int>(FPDF_GetPageHeight(page));
236
237 char filename[256];
238 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
239
Lei Zhang5377ebf2015-09-23 14:52:53 -0700240 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800241
242 HRGN rgn = CreateRectRgn(0, 0, width, height);
243 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700244 DeleteObject(rgn);
245
246 SelectObject(dc, GetStockObject(NULL_PEN));
247 SelectObject(dc, GetStockObject(WHITE_BRUSH));
248 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
249 Rectangle(dc, 0, 0, width + 1, height + 1);
250
251 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
252 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
253
254 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
255}
256#endif
257
Cary Clark399be5b2016-03-14 16:51:29 -0400258#ifdef PDF_ENABLE_SKIA
259void WriteSkp(const char* pdf_name, int num, const void* recorder) {
260 char filename[256];
261 int chars_formatted =
262 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
263
264 if (chars_formatted < 0 ||
265 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
266 fprintf(stderr, "Filename %s is too long\n", filename);
267 return;
268 }
269
270 SkPictureRecorder* r = (SkPictureRecorder*)recorder;
caryclark31735432016-05-11 10:52:53 -0700271 sk_sp<SkPicture> picture(r->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400272 SkFILEWStream wStream(filename);
273 picture->serialize(&wStream);
274}
275#endif
276
Tom Sepez58fb36a2016-02-01 10:32:14 -0800277// These example JS platform callback handlers are entirely optional,
278// and exist here to show the flow of information from a document back
279// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800280int ExampleAppAlert(IPDF_JSPLATFORM*,
281 FPDF_WIDESTRING msg,
282 FPDF_WIDESTRING title,
283 int nType,
284 int nIcon) {
285 printf("%ls", GetPlatformWString(title).c_str());
286 if (nIcon || nType)
287 printf("[icon=%d,type=%d]", nIcon, nType);
288 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289 return 0;
290}
291
Tom Sepez58fb36a2016-02-01 10:32:14 -0800292int ExampleAppResponse(IPDF_JSPLATFORM*,
293 FPDF_WIDESTRING question,
294 FPDF_WIDESTRING title,
295 FPDF_WIDESTRING defaultValue,
296 FPDF_WIDESTRING label,
297 FPDF_BOOL isPassword,
298 void* response,
299 int length) {
300 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
301 GetPlatformWString(title).c_str(),
302 GetPlatformWString(question).c_str(),
303 GetPlatformWString(defaultValue).c_str(),
304 GetPlatformWString(label).c_str(), isPassword, length);
305
306 // UTF-16, always LE regardless of platform.
307 uint8_t* ptr = static_cast<uint8_t*>(response);
308 ptr[0] = 'N';
309 ptr[1] = 0;
310 ptr[2] = 'o';
311 ptr[3] = 0;
312 return 4;
313}
314
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800315void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
316 printf("Goto Page: %d\n", pageNumber);
317}
318
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800319void ExampleDocMail(IPDF_JSPLATFORM*,
320 void* mailData,
321 int length,
322 FPDF_BOOL bUI,
323 FPDF_WIDESTRING To,
324 FPDF_WIDESTRING Subject,
325 FPDF_WIDESTRING CC,
326 FPDF_WIDESTRING BCC,
327 FPDF_WIDESTRING Msg) {
328 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
329 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
330 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
331 GetPlatformWString(Msg).c_str());
332}
333
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800334void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700335 std::string feature = "Unknown";
336 switch (type) {
337 case FPDF_UNSP_DOC_XFAFORM:
338 feature = "XFA";
339 break;
340 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
341 feature = "Portfolios_Packages";
342 break;
343 case FPDF_UNSP_DOC_ATTACHMENT:
344 case FPDF_UNSP_ANNOT_ATTACHMENT:
345 feature = "Attachment";
346 break;
347 case FPDF_UNSP_DOC_SECURITY:
348 feature = "Rights_Management";
349 break;
350 case FPDF_UNSP_DOC_SHAREDREVIEW:
351 feature = "Shared_Review";
352 break;
353 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
354 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
355 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
356 feature = "Shared_Form";
357 break;
358 case FPDF_UNSP_ANNOT_3DANNOT:
359 feature = "3D";
360 break;
361 case FPDF_UNSP_ANNOT_MOVIE:
362 feature = "Movie";
363 break;
364 case FPDF_UNSP_ANNOT_SOUND:
365 feature = "Sound";
366 break;
367 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
368 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
369 feature = "Screen";
370 break;
371 case FPDF_UNSP_ANNOT_SIG:
372 feature = "Digital_Signature";
373 break;
374 }
375 printf("Unsupported feature: %s.\n", feature.c_str());
376}
377
Tom Sepez5ee12d72014-12-17 16:24:01 -0800378bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700379 Options* options,
380 std::vector<std::string>* files) {
381 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800382 return false;
thestig514e8c92016-07-15 17:57:54 -0700383
Tom Sepez5ee12d72014-12-17 16:24:01 -0800384 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800385 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800386 for (; cur_idx < args.size(); ++cur_idx) {
387 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800388 if (cur_arg == "--show-config") {
389 options->show_config = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700390 } else if (cur_arg == "--send-events") {
391 options->send_events = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800392 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800393 if (options->output_format != OUTPUT_NONE) {
394 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
395 return false;
396 }
397 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800398 } else if (cur_arg == "--png") {
399 if (options->output_format != OUTPUT_NONE) {
400 fprintf(stderr, "Duplicate or conflicting --png argument\n");
401 return false;
402 }
403 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700404 } else if (cur_arg == "--txt") {
405 if (options->output_format != OUTPUT_NONE) {
406 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
407 return false;
408 }
409 options->output_format = OUTPUT_TEXT;
Cary Clark399be5b2016-03-14 16:51:29 -0400410#ifdef PDF_ENABLE_SKIA
411 } else if (cur_arg == "--skp") {
412 if (options->output_format != OUTPUT_NONE) {
413 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
414 return false;
415 }
416 options->output_format = OUTPUT_SKP;
417#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700418 } else if (cur_arg.size() > 11 &&
419 cur_arg.compare(0, 11, "--font-dir=") == 0) {
420 if (!options->font_directory.empty()) {
421 fprintf(stderr, "Duplicate --font-dir argument\n");
422 return false;
423 }
424 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700425#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500426 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800427 if (options->output_format != OUTPUT_NONE) {
428 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
429 return false;
430 }
431 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500432 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800433 if (options->output_format != OUTPUT_NONE) {
434 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
435 return false;
436 }
437 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800438#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500439
Tom Sepez452b4f32015-10-13 09:27:27 -0700440#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800441#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500442 } else if (cur_arg.size() > 10 &&
443 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800444 if (!options->bin_directory.empty()) {
445 fprintf(stderr, "Duplicate --bin-dir argument\n");
446 return false;
447 }
448 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800449#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700450#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500451
452 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800453 if (!options->scale_factor_as_string.empty()) {
454 fprintf(stderr, "Duplicate --scale argument\n");
455 return false;
456 }
457 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800458 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
459 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
460 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500461 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700462 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500463 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464 }
thestig514e8c92016-07-15 17:57:54 -0700465 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800466 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700467
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468 return true;
469}
470
Tom Sepezcf22eb82015-05-12 17:28:08 -0700471FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472 return true;
473}
474
475void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
476}
477
tsepezf09bdfa2016-04-18 16:08:26 -0700478void SendPageEvents(const FPDF_FORMHANDLE& form,
479 const FPDF_PAGE& page,
480 const std::string& events) {
481 auto lines = StringSplit(events, '\n');
482 for (auto line : lines) {
483 auto command = StringSplit(line, '#');
484 if (command[0].empty())
485 continue;
486 auto tokens = StringSplit(command[0], ',');
487 if (tokens[0] == "keycode") {
488 if (tokens.size() == 2) {
489 int keycode = atoi(tokens[1].c_str());
490 FORM_OnKeyDown(form, page, keycode, 0);
491 FORM_OnKeyUp(form, page, keycode, 0);
492 } else {
493 fprintf(stderr, "keycode: bad args\n");
494 }
495 } else if (tokens[0] == "mousedown") {
496 if (tokens.size() == 4) {
497 int x = atoi(tokens[2].c_str());
498 int y = atoi(tokens[3].c_str());
499 if (tokens[1] == "left")
500 FORM_OnLButtonDown(form, page, 0, x, y);
501#ifdef PDF_ENABLE_XFA
502 else if (tokens[1] == "right")
503 FORM_OnRButtonDown(form, page, 0, x, y);
504#endif
505 else
506 fprintf(stderr, "mousedown: bad button name\n");
507 } else {
508 fprintf(stderr, "mousedown: bad args\n");
509 }
510 } else if (tokens[0] == "mouseup") {
511 if (tokens.size() == 4) {
512 int x = atoi(tokens[2].c_str());
513 int y = atoi(tokens[3].c_str());
514 if (tokens[1] == "left")
515 FORM_OnLButtonUp(form, page, 0, x, y);
516#ifdef PDF_ENABLE_XFA
517 else if (tokens[1] == "right")
518 FORM_OnRButtonUp(form, page, 0, x, y);
519#endif
520 else
521 fprintf(stderr, "mouseup: bad button name\n");
522 } else {
523 fprintf(stderr, "mouseup: bad args\n");
524 }
525 } else if (tokens[0] == "mousemove") {
526 if (tokens.size() == 3) {
527 int x = atoi(tokens[1].c_str());
528 int y = atoi(tokens[2].c_str());
529 FORM_OnMouseMove(form, page, 0, x, y);
530 } else {
531 fprintf(stderr, "mousemove: bad args\n");
532 }
533 } else {
534 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
535 }
536 }
537}
538
tonikitoo3e981582016-08-26 08:37:10 -0700539FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
540 FPDF_DOCUMENT doc,
541 int index) {
tonikitoo81d92f82016-09-21 12:44:56 -0700542 FPDF_FORMFILLINFO_PDFiumTest* formFillInfo = ToPDFiumTestFormFillInfo(param);
543 auto& loadedPages = formFillInfo->loadedPages;
544
545 auto iter = loadedPages.find(index);
546 if (iter != loadedPages.end())
tonikitoo3e981582016-08-26 08:37:10 -0700547 return iter->second;
548
549 FPDF_PAGE page = FPDF_LoadPage(doc, index);
550 if (!page)
551 return nullptr;
552
tonikitoo81d92f82016-09-21 12:44:56 -0700553 FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
tonikitoo3e981582016-08-26 08:37:10 -0700554
tonikitoo81d92f82016-09-21 12:44:56 -0700555 FORM_OnAfterLoadPage(page, formHandle);
556 FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
557
558 loadedPages[index] = page;
tonikitoo3e981582016-08-26 08:37:10 -0700559 return page;
560}
561
Jun Fangb553bcb2015-11-10 18:49:04 +0800562bool RenderPage(const std::string& name,
tonikitoo3e981582016-08-26 08:37:10 -0700563 FPDF_DOCUMENT doc,
564 FPDF_FORMHANDLE& form,
tonikitoo81d92f82016-09-21 12:44:56 -0700565 FPDF_FORMFILLINFO_PDFiumTest& formFillInfo,
Jun Fangb553bcb2015-11-10 18:49:04 +0800566 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -0700567 const Options& options,
568 const std::string& events) {
tonikitoo81d92f82016-09-21 12:44:56 -0700569 FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
thestig514e8c92016-07-15 17:57:54 -0700570 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +0800571 return false;
thestig514e8c92016-07-15 17:57:54 -0700572
Jun Fangdf7f3662015-11-10 18:29:18 +0800573 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
Jun Fangdf7f3662015-11-10 18:29:18 +0800574
tsepezf09bdfa2016-04-18 16:08:26 -0700575 if (options.send_events)
tsepez10b01bf2016-05-04 12:52:42 -0700576 SendPageEvents(form, page, events);
tsepezf09bdfa2016-04-18 16:08:26 -0700577
Jun Fangdf7f3662015-11-10 18:29:18 +0800578 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700579 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800580 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700581
Jun Fangdf7f3662015-11-10 18:29:18 +0800582 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
583 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800584 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
585 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
thestige97ea032016-05-19 10:59:15 -0700586 if (bitmap) {
587 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
588 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
jaepark75f84a52016-09-09 15:39:09 -0700589 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
Jun Fangdf7f3662015-11-10 18:29:18 +0800590
jaepark75f84a52016-09-09 15:39:09 -0700591 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
thestige97ea032016-05-19 10:59:15 -0700592 int stride = FPDFBitmap_GetStride(bitmap);
593 const char* buffer =
594 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
Jun Fangdf7f3662015-11-10 18:29:18 +0800595
thestige97ea032016-05-19 10:59:15 -0700596 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800597#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -0700598 case OUTPUT_BMP:
599 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
600 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800601
thestige97ea032016-05-19 10:59:15 -0700602 case OUTPUT_EMF:
603 WriteEmf(page, name.c_str(), page_index);
604 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800605#endif
dsinclairb63068f2016-06-16 07:58:09 -0700606 case OUTPUT_TEXT:
607 WriteText(page, name.c_str(), page_index);
608 break;
609
thestige97ea032016-05-19 10:59:15 -0700610 case OUTPUT_PNG:
611 WritePng(name.c_str(), page_index, buffer, stride, width, height);
612 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800613
thestige97ea032016-05-19 10:59:15 -0700614 case OUTPUT_PPM:
615 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
616 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800617
Cary Clark399be5b2016-03-14 16:51:29 -0400618#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -0700619 case OUTPUT_SKP: {
620 std::unique_ptr<SkPictureRecorder> recorder(
621 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height));
622 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
623 WriteSkp(name.c_str(), page_index, recorder.get());
624 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400625#endif
thestige97ea032016-05-19 10:59:15 -0700626 default:
627 break;
628 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800629
thestige97ea032016-05-19 10:59:15 -0700630 FPDFBitmap_Destroy(bitmap);
631 } else {
632 fprintf(stderr, "Page was too large to be rendered.\n");
633 }
tonikitoo3e981582016-08-26 08:37:10 -0700634
tonikitoo81d92f82016-09-21 12:44:56 -0700635 formFillInfo.loadedPages.erase(page_index);
636
Jun Fangdf7f3662015-11-10 18:29:18 +0800637 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
638 FORM_OnBeforeClosePage(page, form);
639 FPDFText_ClosePage(text_page);
640 FPDF_ClosePage(page);
thestige97ea032016-05-19 10:59:15 -0700641 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800642}
643
tsepezf09bdfa2016-04-18 16:08:26 -0700644void RenderPdf(const std::string& name,
645 const char* pBuf,
646 size_t len,
647 const Options& options,
648 const std::string& events) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700649 IPDF_JSPLATFORM platform_callbacks;
650 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700651 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800652 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800653 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800654 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800655 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700656
tonikitoo81d92f82016-09-21 12:44:56 -0700657 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
Tom Sepezc46d0002015-11-30 15:46:36 -0800658#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800659 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800660#else // PDF_ENABLE_XFA
661 form_callbacks.version = 1;
662#endif // PDF_ENABLE_XFA
tonikitoo3e981582016-08-26 08:37:10 -0700663 form_callbacks.FFI_GetPage = GetPageForIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700664 form_callbacks.m_pJsPlatform = &platform_callbacks;
665
666 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700667 FPDF_FILEACCESS file_access;
668 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700669 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700670 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700671 file_access.m_Param = &loader;
672
673 FX_FILEAVAIL file_avail;
674 memset(&file_avail, '\0', sizeof(file_avail));
675 file_avail.version = 1;
676 file_avail.IsDataAvail = Is_Data_Avail;
677
678 FX_DOWNLOADHINTS hints;
679 memset(&hints, '\0', sizeof(hints));
680 hints.version = 1;
681 hints.AddSegment = Add_Segment;
682
683 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800684 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800685 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700686 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800687
Jun Fangdf7f3662015-11-10 18:29:18 +0800688 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700689 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800690 if (doc) {
thestig514e8c92016-07-15 17:57:54 -0700691 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800692 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
thestig514e8c92016-07-15 17:57:54 -0700693
Jun Fangdf7f3662015-11-10 18:29:18 +0800694 if (nRet == PDF_DATA_ERROR) {
695 fprintf(stderr, "Unknown error in checking if doc was available.\n");
696 return;
697 }
698 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
699 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
700 fprintf(stderr,
701 "Error %d was returned in checking if form was available.\n",
702 nRet);
703 return;
704 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800705 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800706 }
707 } else {
Lei Zhang600d4072015-09-23 15:35:25 -0700708 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800709 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700710
Lei Zhang5377ebf2015-09-23 14:52:53 -0700711 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400712 unsigned long err = FPDF_GetLastError();
713 fprintf(stderr, "Load pdf docs unsuccessful: ");
714 switch (err) {
715 case FPDF_ERR_SUCCESS:
716 fprintf(stderr, "Success");
717 break;
718 case FPDF_ERR_UNKNOWN:
719 fprintf(stderr, "Unknown error");
720 break;
721 case FPDF_ERR_FILE:
722 fprintf(stderr, "File not found or could not be opened");
723 break;
724 case FPDF_ERR_FORMAT:
725 fprintf(stderr, "File not in PDF format or corrupted");
726 break;
727 case FPDF_ERR_PASSWORD:
728 fprintf(stderr, "Password required or incorrect password");
729 break;
730 case FPDF_ERR_SECURITY:
731 fprintf(stderr, "Unsupported security scheme");
732 break;
733 case FPDF_ERR_PAGE:
734 fprintf(stderr, "Page not found or content error");
735 break;
736 default:
737 fprintf(stderr, "Unknown error %ld", err);
738 }
739 fprintf(stderr, ".\n");
740
Qin Zhao05224972015-10-20 18:31:06 -0400741 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800742 return;
743 }
744
Lei Zhang5377ebf2015-09-23 14:52:53 -0700745 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700746
tonikitoo81d92f82016-09-21 12:44:56 -0700747 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
748 form_callbacks.formHandle = form;
749
Tom Sepezc46d0002015-11-30 15:46:36 -0800750#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800751 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700752 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
753 !FPDF_LoadXFA(doc)) {
754 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800755 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800756#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700757 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
758 FPDF_SetFormFieldHighlightAlpha(form, 100);
759
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700760 FORM_DoDocumentJSAction(form);
761 FORM_DoDocumentOpenAction(form);
762
Jun Fangdf7f3662015-11-10 18:29:18 +0800763 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700764 int rendered_pages = 0;
765 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700766 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800767 if (bIsLinearized) {
768 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -0700769 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800770 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
thestig514e8c92016-07-15 17:57:54 -0700771
Jun Fangdf7f3662015-11-10 18:29:18 +0800772 if (nRet == PDF_DATA_ERROR) {
773 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
774 i);
775 return;
776 }
777 }
tonikitoo81d92f82016-09-21 12:44:56 -0700778 if (RenderPage(name, doc, form, form_callbacks, i, options, events))
Jun Fangdf7f3662015-11-10 18:29:18 +0800779 ++rendered_pages;
thestig514e8c92016-07-15 17:57:54 -0700780 else
Lei Zhang5377ebf2015-09-23 14:52:53 -0700781 ++bad_pages;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700782 }
783
784 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700785
Tom Sepezc46d0002015-11-30 15:46:36 -0800786#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700787 // Note: The shut down order here is the reverse of the non-XFA branch order.
788 // Need to work out if this is required, and if it is, the lifetimes of
789 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700790 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800791 FPDFDOC_ExitFormFillEnvironment(form);
Tom Sepezc46d0002015-11-30 15:46:36 -0800792#else // PDF_ENABLE_XFA
793 FPDFDOC_ExitFormFillEnvironment(form);
794 FPDF_CloseDocument(doc);
795#endif // PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700796
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700797 FPDFAvail_Destroy(pdf_avail);
798
Tom Sepez1ed8a212015-05-11 15:25:39 -0700799 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -0700800 if (bad_pages)
801 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700802}
803
Tom Sepez2991d8d2016-01-15 16:02:48 -0800804static void ShowConfig() {
805 std::string config;
806 std::string maybe_comma;
807#if PDF_ENABLE_V8
808 config.append(maybe_comma);
809 config.append("V8");
810 maybe_comma = ",";
811#endif // PDF_ENABLE_V8
812#ifdef V8_USE_EXTERNAL_STARTUP_DATA
813 config.append(maybe_comma);
814 config.append("V8_EXTERNAL");
815 maybe_comma = ",";
816#endif // V8_USE_EXTERNAL_STARTUP_DATA
817#ifdef PDF_ENABLE_XFA
818 config.append(maybe_comma);
819 config.append("XFA");
820 maybe_comma = ",";
821#endif // PDF_ENABLE_XFA
822 printf("%s\n", config.c_str());
823}
824
thestig514e8c92016-07-15 17:57:54 -0700825static const char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800826 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800827 " --show-config - print build options and exit\n"
tsepez10b01bf2016-05-04 12:52:42 -0700828 " --send-events - send input described by .evt file\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700829 " --bin-dir=<path> - override path to v8 external data\n"
830 " --font-dir=<path> - override path to external fonts\n"
831 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800832#ifdef _WIN32
833 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
834 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800835#endif // _WIN32
thestig514e8c92016-07-15 17:57:54 -0700836 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800837 " --png - write page images <pdf-name>.<page-number>.png\n"
thestig514e8c92016-07-15 17:57:54 -0700838 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400839#ifdef PDF_ENABLE_SKIA
840 " --skp - write page images <pdf-name>.<page-number>.skp\n"
841#endif
842 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800843
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700844int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800845 std::vector<std::string> args(argv, argv + argc);
846 Options options;
thestig514e8c92016-07-15 17:57:54 -0700847 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800848 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -0700849 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700850 return 1;
851 }
852
Tom Sepez2991d8d2016-01-15 16:02:48 -0800853 if (options.show_config) {
854 ShowConfig();
855 return 0;
856 }
857
858 if (files.empty()) {
859 fprintf(stderr, "No input files.\n");
860 return 1;
861 }
862
Tom Sepez452b4f32015-10-13 09:27:27 -0700863#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700864 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800865#ifdef V8_USE_EXTERNAL_STARTUP_DATA
866 v8::StartupData natives;
867 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700868 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
869 &snapshot, &platform);
870#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700871 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800872#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700873#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800874
Tom Sepeza72e8e22015-10-07 10:17:53 -0700875 FPDF_LIBRARY_CONFIG config;
876 config.version = 2;
877 config.m_pUserFontPaths = nullptr;
878 config.m_pIsolate = nullptr;
879 config.m_v8EmbedderSlot = 0;
880
881 const char* path_array[2];
882 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700883 path_array[0] = options.font_directory.c_str();
884 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700885 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700886 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700887 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888
889 UNSUPPORT_INFO unsuppored_info;
890 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
891 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800892 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700893
894 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
895
thestig514e8c92016-07-15 17:57:54 -0700896 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800897 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800898 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
899 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700900 if (!file_contents)
901 continue;
tsepez10b01bf2016-05-04 12:52:42 -0700902 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700903 std::string events;
904 if (options.send_events) {
905 std::string event_filename = filename;
906 size_t event_length = 0;
907 size_t extension_pos = event_filename.find(".pdf");
908 if (extension_pos != std::string::npos) {
909 event_filename.replace(extension_pos, 4, ".evt");
910 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
911 GetFileContents(event_filename.c_str(), &event_length);
912 if (event_contents) {
tsepez10b01bf2016-05-04 12:52:42 -0700913 fprintf(stderr, "Sending events from: %s\n", event_filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700914 events = std::string(event_contents.get(), event_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700915 }
916 }
917 }
918 RenderPdf(filename, file_contents.get(), file_length, options, events);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700919 }
920
921 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700922#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800923 v8::V8::ShutdownPlatform();
924 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -0700925
926#ifdef V8_USE_EXTERNAL_STARTUP_DATA
927 free(const_cast<char*>(natives.data));
928 free(const_cast<char*>(snapshot.data));
929#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700930#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700931
932 return 0;
933}