blob: 421df39bf3cdfe30d56326dad52a585e444ea490 [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
tonikitoo3e981582016-08-26 08:37:10 -070057// Hold a map of the currently loaded pages in order to avoid them
58// to get loaded twice.
59std::map<int, FPDF_PAGE> g_loadedPages;
60
61// Hold a global pointer of FPDF_FORMHANDLE so that PDFium
62// app hooks can made use of it.
63FPDF_FORMHANDLE g_formHandle;
64
Tom Sepez5ee12d72014-12-17 16:24:01 -080065struct Options {
tsepezf09bdfa2016-04-18 16:08:26 -070066 Options()
67 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080068
Tom Sepez2991d8d2016-01-15 16:02:48 -080069 bool show_config;
tsepezf09bdfa2016-04-18 16:08:26 -070070 bool send_events;
Tom Sepez5ee12d72014-12-17 16:24:01 -080071 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080072 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080073 std::string exe_path;
74 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070075 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080076};
77
Tom Sepezaf18cb32015-02-05 15:06:01 -080078static bool CheckDimensions(int stride, int width, int height) {
79 if (stride < 0 || width < 0 || height < 0)
80 return false;
81 if (height > 0 && width > INT_MAX / height)
82 return false;
83 return true;
84}
85
Vitaly Buka9e0177a2014-07-22 18:15:42 -070086static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
87 int stride, int width, int height) {
88 const char* buffer = reinterpret_cast<const char*>(buffer_void);
89
Tom Sepezaf18cb32015-02-05 15:06:01 -080090 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080092
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093 int out_len = width * height;
94 if (out_len > INT_MAX / 3)
95 return;
96 out_len *= 3;
97
98 char filename[256];
99 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700100 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101 if (!fp)
102 return;
103 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
104 // Source data is B, G, R, unused.
105 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -0700106 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -0700107 for (int h = 0; h < height; ++h) {
108 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700109 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700110 for (int w = 0; w < width; ++w) {
111 // R
112 dest_line[w * 3] = src_line[(w * 4) + 2];
113 // G
114 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
115 // B
116 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118 }
thestig514e8c92016-07-15 17:57:54 -0700119 fwrite(result.data(), out_len, 1, fp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120 fclose(fp);
121}
122
dsinclairb63068f2016-06-16 07:58:09 -0700123void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
124 char filename[256];
125 int chars_formatted =
126 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
127 if (chars_formatted < 0 ||
128 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
129 fprintf(stderr, "Filename %s is too long\n", filename);
130 return;
131 }
132
133 FILE* fp = fopen(filename, "w");
134 if (!fp) {
135 fprintf(stderr, "Failed to open %s for output\n", filename);
136 return;
137 }
138
139 // Output in UTF32-LE.
140 uint32_t bom = 0x0000FEFF;
141 fwrite(&bom, sizeof(bom), 1, fp);
142
143 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
144 for (int i = 0; i < FPDFText_CountChars(textpage); i++) {
145 uint32_t c = FPDFText_GetUnicode(textpage, i);
146 fwrite(&c, sizeof(c), 1, fp);
147 }
148
149 FPDFText_ClosePage(textpage);
150
151 (void)fclose(fp);
152}
153
Tom Sepezaf18cb32015-02-05 15:06:01 -0800154static void WritePng(const char* pdf_name, int num, const void* buffer_void,
155 int stride, int width, int height) {
156 if (!CheckDimensions(stride, width, height))
157 return;
158
159 std::vector<unsigned char> png_encoding;
160 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
161 if (!image_diff_png::EncodeBGRAPNG(
162 buffer, width, height, stride, false, &png_encoding)) {
163 fprintf(stderr, "Failed to convert bitmap to PNG\n");
164 return;
165 }
166
167 char filename[256];
168 int chars_formatted = snprintf(
169 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
170 if (chars_formatted < 0 ||
171 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400172 fprintf(stderr, "Filename %s is too long\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800173 return;
174 }
175
176 FILE* fp = fopen(filename, "wb");
177 if (!fp) {
178 fprintf(stderr, "Failed to open %s for output\n", filename);
179 return;
180 }
181
182 size_t bytes_written = fwrite(
183 &png_encoding.front(), 1, png_encoding.size(), fp);
184 if (bytes_written != png_encoding.size())
185 fprintf(stderr, "Failed to write to %s\n", filename);
186
Lei Zhang5377ebf2015-09-23 14:52:53 -0700187 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800188}
189
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700190#ifdef _WIN32
191static void WriteBmp(const char* pdf_name, int num, const void* buffer,
192 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800193 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700194 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800195
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700196 int out_len = stride * height;
197 if (out_len > INT_MAX / 3)
198 return;
199
200 char filename[256];
201 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
202 FILE* fp = fopen(filename, "wb");
203 if (!fp)
204 return;
205
Nico Weber2827bdd2015-07-01 14:08:08 -0700206 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700207 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
208 bmi.bmiHeader.biWidth = width;
209 bmi.bmiHeader.biHeight = -height; // top-down image
210 bmi.bmiHeader.biPlanes = 1;
211 bmi.bmiHeader.biBitCount = 32;
212 bmi.bmiHeader.biCompression = BI_RGB;
213 bmi.bmiHeader.biSizeImage = 0;
214
Nico Weber2827bdd2015-07-01 14:08:08 -0700215 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700216 file_header.bfType = 0x4d42;
217 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
218 file_header.bfOffBits = file_header.bfSize - out_len;
219
220 fwrite(&file_header, sizeof(file_header), 1, fp);
221 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
222 fwrite(buffer, out_len, 1, fp);
223 fclose(fp);
224}
225
226void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
227 int width = static_cast<int>(FPDF_GetPageWidth(page));
228 int height = static_cast<int>(FPDF_GetPageHeight(page));
229
230 char filename[256];
231 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
232
Lei Zhang5377ebf2015-09-23 14:52:53 -0700233 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800234
235 HRGN rgn = CreateRectRgn(0, 0, width, height);
236 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700237 DeleteObject(rgn);
238
239 SelectObject(dc, GetStockObject(NULL_PEN));
240 SelectObject(dc, GetStockObject(WHITE_BRUSH));
241 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
242 Rectangle(dc, 0, 0, width + 1, height + 1);
243
244 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
245 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
246
247 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
248}
249#endif
250
Cary Clark399be5b2016-03-14 16:51:29 -0400251#ifdef PDF_ENABLE_SKIA
252void WriteSkp(const char* pdf_name, int num, const void* recorder) {
253 char filename[256];
254 int chars_formatted =
255 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
256
257 if (chars_formatted < 0 ||
258 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
259 fprintf(stderr, "Filename %s is too long\n", filename);
260 return;
261 }
262
263 SkPictureRecorder* r = (SkPictureRecorder*)recorder;
caryclark31735432016-05-11 10:52:53 -0700264 sk_sp<SkPicture> picture(r->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400265 SkFILEWStream wStream(filename);
266 picture->serialize(&wStream);
267}
268#endif
269
Tom Sepez58fb36a2016-02-01 10:32:14 -0800270// These example JS platform callback handlers are entirely optional,
271// and exist here to show the flow of information from a document back
272// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800273int ExampleAppAlert(IPDF_JSPLATFORM*,
274 FPDF_WIDESTRING msg,
275 FPDF_WIDESTRING title,
276 int nType,
277 int nIcon) {
278 printf("%ls", GetPlatformWString(title).c_str());
279 if (nIcon || nType)
280 printf("[icon=%d,type=%d]", nIcon, nType);
281 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282 return 0;
283}
284
Tom Sepez58fb36a2016-02-01 10:32:14 -0800285int ExampleAppResponse(IPDF_JSPLATFORM*,
286 FPDF_WIDESTRING question,
287 FPDF_WIDESTRING title,
288 FPDF_WIDESTRING defaultValue,
289 FPDF_WIDESTRING label,
290 FPDF_BOOL isPassword,
291 void* response,
292 int length) {
293 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
294 GetPlatformWString(title).c_str(),
295 GetPlatformWString(question).c_str(),
296 GetPlatformWString(defaultValue).c_str(),
297 GetPlatformWString(label).c_str(), isPassword, length);
298
299 // UTF-16, always LE regardless of platform.
300 uint8_t* ptr = static_cast<uint8_t*>(response);
301 ptr[0] = 'N';
302 ptr[1] = 0;
303 ptr[2] = 'o';
304 ptr[3] = 0;
305 return 4;
306}
307
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800308void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
309 printf("Goto Page: %d\n", pageNumber);
310}
311
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800312void ExampleDocMail(IPDF_JSPLATFORM*,
313 void* mailData,
314 int length,
315 FPDF_BOOL bUI,
316 FPDF_WIDESTRING To,
317 FPDF_WIDESTRING Subject,
318 FPDF_WIDESTRING CC,
319 FPDF_WIDESTRING BCC,
320 FPDF_WIDESTRING Msg) {
321 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
322 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
323 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
324 GetPlatformWString(Msg).c_str());
325}
326
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800327void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328 std::string feature = "Unknown";
329 switch (type) {
330 case FPDF_UNSP_DOC_XFAFORM:
331 feature = "XFA";
332 break;
333 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
334 feature = "Portfolios_Packages";
335 break;
336 case FPDF_UNSP_DOC_ATTACHMENT:
337 case FPDF_UNSP_ANNOT_ATTACHMENT:
338 feature = "Attachment";
339 break;
340 case FPDF_UNSP_DOC_SECURITY:
341 feature = "Rights_Management";
342 break;
343 case FPDF_UNSP_DOC_SHAREDREVIEW:
344 feature = "Shared_Review";
345 break;
346 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
347 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
348 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
349 feature = "Shared_Form";
350 break;
351 case FPDF_UNSP_ANNOT_3DANNOT:
352 feature = "3D";
353 break;
354 case FPDF_UNSP_ANNOT_MOVIE:
355 feature = "Movie";
356 break;
357 case FPDF_UNSP_ANNOT_SOUND:
358 feature = "Sound";
359 break;
360 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
361 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
362 feature = "Screen";
363 break;
364 case FPDF_UNSP_ANNOT_SIG:
365 feature = "Digital_Signature";
366 break;
367 }
368 printf("Unsupported feature: %s.\n", feature.c_str());
369}
370
Tom Sepez5ee12d72014-12-17 16:24:01 -0800371bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700372 Options* options,
373 std::vector<std::string>* files) {
374 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800375 return false;
thestig514e8c92016-07-15 17:57:54 -0700376
Tom Sepez5ee12d72014-12-17 16:24:01 -0800377 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800378 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800379 for (; cur_idx < args.size(); ++cur_idx) {
380 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800381 if (cur_arg == "--show-config") {
382 options->show_config = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700383 } else if (cur_arg == "--send-events") {
384 options->send_events = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800385 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800386 if (options->output_format != OUTPUT_NONE) {
387 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
388 return false;
389 }
390 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800391 } else if (cur_arg == "--png") {
392 if (options->output_format != OUTPUT_NONE) {
393 fprintf(stderr, "Duplicate or conflicting --png argument\n");
394 return false;
395 }
396 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700397 } else if (cur_arg == "--txt") {
398 if (options->output_format != OUTPUT_NONE) {
399 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
400 return false;
401 }
402 options->output_format = OUTPUT_TEXT;
Cary Clark399be5b2016-03-14 16:51:29 -0400403#ifdef PDF_ENABLE_SKIA
404 } else if (cur_arg == "--skp") {
405 if (options->output_format != OUTPUT_NONE) {
406 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
407 return false;
408 }
409 options->output_format = OUTPUT_SKP;
410#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700411 } else if (cur_arg.size() > 11 &&
412 cur_arg.compare(0, 11, "--font-dir=") == 0) {
413 if (!options->font_directory.empty()) {
414 fprintf(stderr, "Duplicate --font-dir argument\n");
415 return false;
416 }
417 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700418#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500419 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800420 if (options->output_format != OUTPUT_NONE) {
421 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
422 return false;
423 }
424 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500425 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800426 if (options->output_format != OUTPUT_NONE) {
427 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
428 return false;
429 }
430 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800431#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500432
Tom Sepez452b4f32015-10-13 09:27:27 -0700433#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800434#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500435 } else if (cur_arg.size() > 10 &&
436 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800437 if (!options->bin_directory.empty()) {
438 fprintf(stderr, "Duplicate --bin-dir argument\n");
439 return false;
440 }
441 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800442#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700443#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500444
445 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800446 if (!options->scale_factor_as_string.empty()) {
447 fprintf(stderr, "Duplicate --scale argument\n");
448 return false;
449 }
450 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800451 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
452 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
453 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500454 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700455 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500456 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700457 }
thestig514e8c92016-07-15 17:57:54 -0700458 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800459 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700460
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461 return true;
462}
463
Tom Sepezcf22eb82015-05-12 17:28:08 -0700464FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465 return true;
466}
467
468void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
469}
470
tsepezf09bdfa2016-04-18 16:08:26 -0700471void SendPageEvents(const FPDF_FORMHANDLE& form,
472 const FPDF_PAGE& page,
473 const std::string& events) {
474 auto lines = StringSplit(events, '\n');
475 for (auto line : lines) {
476 auto command = StringSplit(line, '#');
477 if (command[0].empty())
478 continue;
479 auto tokens = StringSplit(command[0], ',');
480 if (tokens[0] == "keycode") {
481 if (tokens.size() == 2) {
482 int keycode = atoi(tokens[1].c_str());
483 FORM_OnKeyDown(form, page, keycode, 0);
484 FORM_OnKeyUp(form, page, keycode, 0);
485 } else {
486 fprintf(stderr, "keycode: bad args\n");
487 }
488 } else if (tokens[0] == "mousedown") {
489 if (tokens.size() == 4) {
490 int x = atoi(tokens[2].c_str());
491 int y = atoi(tokens[3].c_str());
492 if (tokens[1] == "left")
493 FORM_OnLButtonDown(form, page, 0, x, y);
494#ifdef PDF_ENABLE_XFA
495 else if (tokens[1] == "right")
496 FORM_OnRButtonDown(form, page, 0, x, y);
497#endif
498 else
499 fprintf(stderr, "mousedown: bad button name\n");
500 } else {
501 fprintf(stderr, "mousedown: bad args\n");
502 }
503 } else if (tokens[0] == "mouseup") {
504 if (tokens.size() == 4) {
505 int x = atoi(tokens[2].c_str());
506 int y = atoi(tokens[3].c_str());
507 if (tokens[1] == "left")
508 FORM_OnLButtonUp(form, page, 0, x, y);
509#ifdef PDF_ENABLE_XFA
510 else if (tokens[1] == "right")
511 FORM_OnRButtonUp(form, page, 0, x, y);
512#endif
513 else
514 fprintf(stderr, "mouseup: bad button name\n");
515 } else {
516 fprintf(stderr, "mouseup: bad args\n");
517 }
518 } else if (tokens[0] == "mousemove") {
519 if (tokens.size() == 3) {
520 int x = atoi(tokens[1].c_str());
521 int y = atoi(tokens[2].c_str());
522 FORM_OnMouseMove(form, page, 0, x, y);
523 } else {
524 fprintf(stderr, "mousemove: bad args\n");
525 }
526 } else {
527 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
528 }
529 }
530}
531
tonikitoo3e981582016-08-26 08:37:10 -0700532FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
533 FPDF_DOCUMENT doc,
534 int index) {
535 auto iter = g_loadedPages.find(index);
536 if (iter != g_loadedPages.end())
537 return iter->second;
538
539 FPDF_PAGE page = FPDF_LoadPage(doc, index);
540 if (!page)
541 return nullptr;
542
543 FORM_OnAfterLoadPage(page, g_formHandle);
544 FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
545
546 g_loadedPages[index] = page;
547 return page;
548}
549
Jun Fangb553bcb2015-11-10 18:49:04 +0800550bool RenderPage(const std::string& name,
tonikitoo3e981582016-08-26 08:37:10 -0700551 FPDF_DOCUMENT doc,
552 FPDF_FORMHANDLE& form,
Jun Fangb553bcb2015-11-10 18:49:04 +0800553 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -0700554 const Options& options,
555 const std::string& events) {
tonikitoo3e981582016-08-26 08:37:10 -0700556 FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index);
thestig514e8c92016-07-15 17:57:54 -0700557 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +0800558 return false;
thestig514e8c92016-07-15 17:57:54 -0700559
Jun Fangdf7f3662015-11-10 18:29:18 +0800560 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
Jun Fangdf7f3662015-11-10 18:29:18 +0800561
tsepezf09bdfa2016-04-18 16:08:26 -0700562 if (options.send_events)
tsepez10b01bf2016-05-04 12:52:42 -0700563 SendPageEvents(form, page, events);
tsepezf09bdfa2016-04-18 16:08:26 -0700564
Jun Fangdf7f3662015-11-10 18:29:18 +0800565 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700566 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800567 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700568
Jun Fangdf7f3662015-11-10 18:29:18 +0800569 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
570 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800571 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
572 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
thestige97ea032016-05-19 10:59:15 -0700573 if (bitmap) {
574 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
575 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
576 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangdf7f3662015-11-10 18:29:18 +0800577
thestige97ea032016-05-19 10:59:15 -0700578 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
579 int stride = FPDFBitmap_GetStride(bitmap);
580 const char* buffer =
581 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
Jun Fangdf7f3662015-11-10 18:29:18 +0800582
thestige97ea032016-05-19 10:59:15 -0700583 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800584#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -0700585 case OUTPUT_BMP:
586 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
587 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800588
thestige97ea032016-05-19 10:59:15 -0700589 case OUTPUT_EMF:
590 WriteEmf(page, name.c_str(), page_index);
591 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800592#endif
dsinclairb63068f2016-06-16 07:58:09 -0700593 case OUTPUT_TEXT:
594 WriteText(page, name.c_str(), page_index);
595 break;
596
thestige97ea032016-05-19 10:59:15 -0700597 case OUTPUT_PNG:
598 WritePng(name.c_str(), page_index, buffer, stride, width, height);
599 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800600
thestige97ea032016-05-19 10:59:15 -0700601 case OUTPUT_PPM:
602 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
603 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800604
Cary Clark399be5b2016-03-14 16:51:29 -0400605#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -0700606 case OUTPUT_SKP: {
607 std::unique_ptr<SkPictureRecorder> recorder(
608 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height));
609 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
610 WriteSkp(name.c_str(), page_index, recorder.get());
611 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400612#endif
thestige97ea032016-05-19 10:59:15 -0700613 default:
614 break;
615 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800616
thestige97ea032016-05-19 10:59:15 -0700617 FPDFBitmap_Destroy(bitmap);
618 } else {
619 fprintf(stderr, "Page was too large to be rendered.\n");
620 }
tonikitoo3e981582016-08-26 08:37:10 -0700621
622 g_loadedPages.erase(page_index);
Jun Fangdf7f3662015-11-10 18:29:18 +0800623 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
624 FORM_OnBeforeClosePage(page, form);
625 FPDFText_ClosePage(text_page);
626 FPDF_ClosePage(page);
thestige97ea032016-05-19 10:59:15 -0700627 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800628}
629
tsepezf09bdfa2016-04-18 16:08:26 -0700630void RenderPdf(const std::string& name,
631 const char* pBuf,
632 size_t len,
633 const Options& options,
634 const std::string& events) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635 IPDF_JSPLATFORM platform_callbacks;
636 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700637 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800638 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800639 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800640 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800641 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700642
643 FPDF_FORMFILLINFO form_callbacks;
644 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezc46d0002015-11-30 15:46:36 -0800645#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800646 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800647#else // PDF_ENABLE_XFA
648 form_callbacks.version = 1;
649#endif // PDF_ENABLE_XFA
tonikitoo3e981582016-08-26 08:37:10 -0700650 form_callbacks.FFI_GetPage = GetPageForIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700651 form_callbacks.m_pJsPlatform = &platform_callbacks;
652
653 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700654 FPDF_FILEACCESS file_access;
655 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700656 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700657 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700658 file_access.m_Param = &loader;
659
660 FX_FILEAVAIL file_avail;
661 memset(&file_avail, '\0', sizeof(file_avail));
662 file_avail.version = 1;
663 file_avail.IsDataAvail = Is_Data_Avail;
664
665 FX_DOWNLOADHINTS hints;
666 memset(&hints, '\0', sizeof(hints));
667 hints.version = 1;
668 hints.AddSegment = Add_Segment;
669
670 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800671 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800672 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800674
Jun Fangdf7f3662015-11-10 18:29:18 +0800675 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700676 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800677 if (doc) {
thestig514e8c92016-07-15 17:57:54 -0700678 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800679 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
thestig514e8c92016-07-15 17:57:54 -0700680
Jun Fangdf7f3662015-11-10 18:29:18 +0800681 if (nRet == PDF_DATA_ERROR) {
682 fprintf(stderr, "Unknown error in checking if doc was available.\n");
683 return;
684 }
685 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
686 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
687 fprintf(stderr,
688 "Error %d was returned in checking if form was available.\n",
689 nRet);
690 return;
691 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800692 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800693 }
694 } else {
Lei Zhang600d4072015-09-23 15:35:25 -0700695 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800696 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700697
Lei Zhang5377ebf2015-09-23 14:52:53 -0700698 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400699 unsigned long err = FPDF_GetLastError();
700 fprintf(stderr, "Load pdf docs unsuccessful: ");
701 switch (err) {
702 case FPDF_ERR_SUCCESS:
703 fprintf(stderr, "Success");
704 break;
705 case FPDF_ERR_UNKNOWN:
706 fprintf(stderr, "Unknown error");
707 break;
708 case FPDF_ERR_FILE:
709 fprintf(stderr, "File not found or could not be opened");
710 break;
711 case FPDF_ERR_FORMAT:
712 fprintf(stderr, "File not in PDF format or corrupted");
713 break;
714 case FPDF_ERR_PASSWORD:
715 fprintf(stderr, "Password required or incorrect password");
716 break;
717 case FPDF_ERR_SECURITY:
718 fprintf(stderr, "Unsupported security scheme");
719 break;
720 case FPDF_ERR_PAGE:
721 fprintf(stderr, "Page not found or content error");
722 break;
723 default:
724 fprintf(stderr, "Unknown error %ld", err);
725 }
726 fprintf(stderr, ".\n");
727
Qin Zhao05224972015-10-20 18:31:06 -0400728 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800729 return;
730 }
731
Lei Zhang5377ebf2015-09-23 14:52:53 -0700732 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700733
tonikitoo3e981582016-08-26 08:37:10 -0700734 FPDF_FORMHANDLE form = g_formHandle =
735 FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
Tom Sepezc46d0002015-11-30 15:46:36 -0800736#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800737 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700738 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
739 !FPDF_LoadXFA(doc)) {
740 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800741 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800742#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
744 FPDF_SetFormFieldHighlightAlpha(form, 100);
745
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700746 FORM_DoDocumentJSAction(form);
747 FORM_DoDocumentOpenAction(form);
748
Jun Fangdf7f3662015-11-10 18:29:18 +0800749 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700750 int rendered_pages = 0;
751 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700752 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800753 if (bIsLinearized) {
754 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -0700755 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800756 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
thestig514e8c92016-07-15 17:57:54 -0700757
Jun Fangdf7f3662015-11-10 18:29:18 +0800758 if (nRet == PDF_DATA_ERROR) {
759 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
760 i);
761 return;
762 }
763 }
thestig514e8c92016-07-15 17:57:54 -0700764 if (RenderPage(name, doc, form, i, options, events))
Jun Fangdf7f3662015-11-10 18:29:18 +0800765 ++rendered_pages;
thestig514e8c92016-07-15 17:57:54 -0700766 else
Lei Zhang5377ebf2015-09-23 14:52:53 -0700767 ++bad_pages;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700768 }
769
770 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700771
Tom Sepezc46d0002015-11-30 15:46:36 -0800772#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700773 // Note: The shut down order here is the reverse of the non-XFA branch order.
774 // Need to work out if this is required, and if it is, the lifetimes of
775 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700776 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800777 FPDFDOC_ExitFormFillEnvironment(form);
Tom Sepezc46d0002015-11-30 15:46:36 -0800778#else // PDF_ENABLE_XFA
779 FPDFDOC_ExitFormFillEnvironment(form);
780 FPDF_CloseDocument(doc);
781#endif // PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700782
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700783 FPDFAvail_Destroy(pdf_avail);
784
Tom Sepez1ed8a212015-05-11 15:25:39 -0700785 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -0700786 if (bad_pages)
787 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700788}
789
Tom Sepez2991d8d2016-01-15 16:02:48 -0800790static void ShowConfig() {
791 std::string config;
792 std::string maybe_comma;
793#if PDF_ENABLE_V8
794 config.append(maybe_comma);
795 config.append("V8");
796 maybe_comma = ",";
797#endif // PDF_ENABLE_V8
798#ifdef V8_USE_EXTERNAL_STARTUP_DATA
799 config.append(maybe_comma);
800 config.append("V8_EXTERNAL");
801 maybe_comma = ",";
802#endif // V8_USE_EXTERNAL_STARTUP_DATA
803#ifdef PDF_ENABLE_XFA
804 config.append(maybe_comma);
805 config.append("XFA");
806 maybe_comma = ",";
807#endif // PDF_ENABLE_XFA
808 printf("%s\n", config.c_str());
809}
810
thestig514e8c92016-07-15 17:57:54 -0700811static const char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800812 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800813 " --show-config - print build options and exit\n"
tsepez10b01bf2016-05-04 12:52:42 -0700814 " --send-events - send input described by .evt file\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700815 " --bin-dir=<path> - override path to v8 external data\n"
816 " --font-dir=<path> - override path to external fonts\n"
817 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800818#ifdef _WIN32
819 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
820 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800821#endif // _WIN32
thestig514e8c92016-07-15 17:57:54 -0700822 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800823 " --png - write page images <pdf-name>.<page-number>.png\n"
thestig514e8c92016-07-15 17:57:54 -0700824 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400825#ifdef PDF_ENABLE_SKIA
826 " --skp - write page images <pdf-name>.<page-number>.skp\n"
827#endif
828 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800829
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700830int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800831 std::vector<std::string> args(argv, argv + argc);
832 Options options;
thestig514e8c92016-07-15 17:57:54 -0700833 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800834 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -0700835 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700836 return 1;
837 }
838
Tom Sepez2991d8d2016-01-15 16:02:48 -0800839 if (options.show_config) {
840 ShowConfig();
841 return 0;
842 }
843
844 if (files.empty()) {
845 fprintf(stderr, "No input files.\n");
846 return 1;
847 }
848
Tom Sepez452b4f32015-10-13 09:27:27 -0700849#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700850 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800851#ifdef V8_USE_EXTERNAL_STARTUP_DATA
852 v8::StartupData natives;
853 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700854 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
855 &snapshot, &platform);
856#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700857 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800858#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700859#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800860
Tom Sepeza72e8e22015-10-07 10:17:53 -0700861 FPDF_LIBRARY_CONFIG config;
862 config.version = 2;
863 config.m_pUserFontPaths = nullptr;
864 config.m_pIsolate = nullptr;
865 config.m_v8EmbedderSlot = 0;
866
867 const char* path_array[2];
868 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700869 path_array[0] = options.font_directory.c_str();
870 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700871 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700872 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700873 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700874
875 UNSUPPORT_INFO unsuppored_info;
876 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
877 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800878 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700879
880 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
881
thestig514e8c92016-07-15 17:57:54 -0700882 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800883 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800884 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
885 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700886 if (!file_contents)
887 continue;
tsepez10b01bf2016-05-04 12:52:42 -0700888 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700889 std::string events;
890 if (options.send_events) {
891 std::string event_filename = filename;
892 size_t event_length = 0;
893 size_t extension_pos = event_filename.find(".pdf");
894 if (extension_pos != std::string::npos) {
895 event_filename.replace(extension_pos, 4, ".evt");
896 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
897 GetFileContents(event_filename.c_str(), &event_length);
898 if (event_contents) {
tsepez10b01bf2016-05-04 12:52:42 -0700899 fprintf(stderr, "Sending events from: %s\n", event_filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700900 events = std::string(event_contents.get(), event_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700901 }
902 }
903 }
904 RenderPdf(filename, file_contents.get(), file_length, options, events);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905 }
906
907 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700908#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800909 v8::V8::ShutdownPlatform();
910 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -0700911
912#ifdef V8_USE_EXTERNAL_STARTUP_DATA
913 free(const_cast<char*>(natives.data));
914 free(const_cast<char*>(snapshot.data));
915#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700916#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700917
918 return 0;
919}