blob: 35e0d50678baa3592bef364772d5a356ee89a5d8 [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
thestigb97e07e2016-09-28 22:16:40 -070080struct AvailDeleter {
81 inline void operator()(FPDF_AVAIL avail) const { FPDFAvail_Destroy(avail); }
82};
83
tonikitoo81d92f82016-09-21 12:44:56 -070084static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
85 FPDF_FORMFILLINFO* formFillInfo) {
86 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo);
87}
88
thestigb97e07e2016-09-28 22:16:40 -070089static void CloseDocAndForm(FPDF_DOCUMENT doc, FPDF_FORMHANDLE form) {
90#ifdef PDF_ENABLE_XFA
91 // Note: The shut down order here is the reverse of the non-XFA branch order.
92 // Need to work out if this is required, and if it is, the lifetimes of
93 // objects owned by |doc| that |form| reference.
94 FPDF_CloseDocument(doc);
95 FPDFDOC_ExitFormFillEnvironment(form);
96#else // PDF_ENABLE_XFA
97 FPDFDOC_ExitFormFillEnvironment(form);
98 FPDF_CloseDocument(doc);
99#endif // PDF_ENABLE_XFA
100}
101
Tom Sepezaf18cb32015-02-05 15:06:01 -0800102static bool CheckDimensions(int stride, int width, int height) {
103 if (stride < 0 || width < 0 || height < 0)
104 return false;
105 if (height > 0 && width > INT_MAX / height)
106 return false;
107 return true;
108}
109
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700110static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
111 int stride, int width, int height) {
112 const char* buffer = reinterpret_cast<const char*>(buffer_void);
113
Tom Sepezaf18cb32015-02-05 15:06:01 -0800114 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800116
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117 int out_len = width * height;
118 if (out_len > INT_MAX / 3)
119 return;
120 out_len *= 3;
121
122 char filename[256];
123 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700124 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125 if (!fp)
126 return;
127 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
128 // Source data is B, G, R, unused.
129 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -0700130 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -0700131 for (int h = 0; h < height; ++h) {
132 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700133 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700134 for (int w = 0; w < width; ++w) {
135 // R
136 dest_line[w * 3] = src_line[(w * 4) + 2];
137 // G
138 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
139 // B
140 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142 }
thestig514e8c92016-07-15 17:57:54 -0700143 fwrite(result.data(), out_len, 1, fp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144 fclose(fp);
145}
146
dsinclairb63068f2016-06-16 07:58:09 -0700147void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
148 char filename[256];
149 int chars_formatted =
150 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
151 if (chars_formatted < 0 ||
152 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
153 fprintf(stderr, "Filename %s is too long\n", filename);
154 return;
155 }
156
157 FILE* fp = fopen(filename, "w");
158 if (!fp) {
159 fprintf(stderr, "Failed to open %s for output\n", filename);
160 return;
161 }
162
163 // Output in UTF32-LE.
164 uint32_t bom = 0x0000FEFF;
165 fwrite(&bom, sizeof(bom), 1, fp);
166
167 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
168 for (int i = 0; i < FPDFText_CountChars(textpage); i++) {
169 uint32_t c = FPDFText_GetUnicode(textpage, i);
170 fwrite(&c, sizeof(c), 1, fp);
171 }
172
173 FPDFText_ClosePage(textpage);
174
175 (void)fclose(fp);
176}
177
Tom Sepezaf18cb32015-02-05 15:06:01 -0800178static void WritePng(const char* pdf_name, int num, const void* buffer_void,
179 int stride, int width, int height) {
180 if (!CheckDimensions(stride, width, height))
181 return;
182
183 std::vector<unsigned char> png_encoding;
184 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
185 if (!image_diff_png::EncodeBGRAPNG(
186 buffer, width, height, stride, false, &png_encoding)) {
187 fprintf(stderr, "Failed to convert bitmap to PNG\n");
188 return;
189 }
190
191 char filename[256];
192 int chars_formatted = snprintf(
193 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
194 if (chars_formatted < 0 ||
195 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400196 fprintf(stderr, "Filename %s is too long\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800197 return;
198 }
199
200 FILE* fp = fopen(filename, "wb");
201 if (!fp) {
202 fprintf(stderr, "Failed to open %s for output\n", filename);
203 return;
204 }
205
206 size_t bytes_written = fwrite(
207 &png_encoding.front(), 1, png_encoding.size(), fp);
208 if (bytes_written != png_encoding.size())
209 fprintf(stderr, "Failed to write to %s\n", filename);
210
Lei Zhang5377ebf2015-09-23 14:52:53 -0700211 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800212}
213
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700214#ifdef _WIN32
215static void WriteBmp(const char* pdf_name, int num, const void* buffer,
216 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800217 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700218 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800219
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700220 int out_len = stride * height;
221 if (out_len > INT_MAX / 3)
222 return;
223
224 char filename[256];
225 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
226 FILE* fp = fopen(filename, "wb");
227 if (!fp)
228 return;
229
Nico Weber2827bdd2015-07-01 14:08:08 -0700230 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700231 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
232 bmi.bmiHeader.biWidth = width;
233 bmi.bmiHeader.biHeight = -height; // top-down image
234 bmi.bmiHeader.biPlanes = 1;
235 bmi.bmiHeader.biBitCount = 32;
236 bmi.bmiHeader.biCompression = BI_RGB;
237 bmi.bmiHeader.biSizeImage = 0;
238
Nico Weber2827bdd2015-07-01 14:08:08 -0700239 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700240 file_header.bfType = 0x4d42;
241 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
242 file_header.bfOffBits = file_header.bfSize - out_len;
243
244 fwrite(&file_header, sizeof(file_header), 1, fp);
245 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
246 fwrite(buffer, out_len, 1, fp);
247 fclose(fp);
248}
249
250void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
251 int width = static_cast<int>(FPDF_GetPageWidth(page));
252 int height = static_cast<int>(FPDF_GetPageHeight(page));
253
254 char filename[256];
255 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
256
Lei Zhang5377ebf2015-09-23 14:52:53 -0700257 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800258
259 HRGN rgn = CreateRectRgn(0, 0, width, height);
260 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700261 DeleteObject(rgn);
262
263 SelectObject(dc, GetStockObject(NULL_PEN));
264 SelectObject(dc, GetStockObject(WHITE_BRUSH));
265 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
266 Rectangle(dc, 0, 0, width + 1, height + 1);
267
268 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
269 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
270
271 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
272}
273#endif
274
Cary Clark399be5b2016-03-14 16:51:29 -0400275#ifdef PDF_ENABLE_SKIA
thestigb97e07e2016-09-28 22:16:40 -0700276void WriteSkp(const char* pdf_name, int num, SkPictureRecorder* recorder) {
Cary Clark399be5b2016-03-14 16:51:29 -0400277 char filename[256];
278 int chars_formatted =
279 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
280
281 if (chars_formatted < 0 ||
282 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
283 fprintf(stderr, "Filename %s is too long\n", filename);
284 return;
285 }
286
thestigb97e07e2016-09-28 22:16:40 -0700287 sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400288 SkFILEWStream wStream(filename);
289 picture->serialize(&wStream);
290}
291#endif
292
Tom Sepez58fb36a2016-02-01 10:32:14 -0800293// These example JS platform callback handlers are entirely optional,
294// and exist here to show the flow of information from a document back
295// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800296int ExampleAppAlert(IPDF_JSPLATFORM*,
297 FPDF_WIDESTRING msg,
298 FPDF_WIDESTRING title,
299 int nType,
300 int nIcon) {
301 printf("%ls", GetPlatformWString(title).c_str());
302 if (nIcon || nType)
303 printf("[icon=%d,type=%d]", nIcon, nType);
304 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305 return 0;
306}
307
Tom Sepez58fb36a2016-02-01 10:32:14 -0800308int ExampleAppResponse(IPDF_JSPLATFORM*,
309 FPDF_WIDESTRING question,
310 FPDF_WIDESTRING title,
311 FPDF_WIDESTRING defaultValue,
312 FPDF_WIDESTRING label,
313 FPDF_BOOL isPassword,
314 void* response,
315 int length) {
316 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
317 GetPlatformWString(title).c_str(),
318 GetPlatformWString(question).c_str(),
319 GetPlatformWString(defaultValue).c_str(),
320 GetPlatformWString(label).c_str(), isPassword, length);
321
322 // UTF-16, always LE regardless of platform.
323 uint8_t* ptr = static_cast<uint8_t*>(response);
324 ptr[0] = 'N';
325 ptr[1] = 0;
326 ptr[2] = 'o';
327 ptr[3] = 0;
328 return 4;
329}
330
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800331void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
332 printf("Goto Page: %d\n", pageNumber);
333}
334
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800335void ExampleDocMail(IPDF_JSPLATFORM*,
336 void* mailData,
337 int length,
338 FPDF_BOOL bUI,
339 FPDF_WIDESTRING To,
340 FPDF_WIDESTRING Subject,
341 FPDF_WIDESTRING CC,
342 FPDF_WIDESTRING BCC,
343 FPDF_WIDESTRING Msg) {
344 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
345 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
346 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
347 GetPlatformWString(Msg).c_str());
348}
349
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800350void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351 std::string feature = "Unknown";
352 switch (type) {
353 case FPDF_UNSP_DOC_XFAFORM:
354 feature = "XFA";
355 break;
356 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
357 feature = "Portfolios_Packages";
358 break;
359 case FPDF_UNSP_DOC_ATTACHMENT:
360 case FPDF_UNSP_ANNOT_ATTACHMENT:
361 feature = "Attachment";
362 break;
363 case FPDF_UNSP_DOC_SECURITY:
364 feature = "Rights_Management";
365 break;
366 case FPDF_UNSP_DOC_SHAREDREVIEW:
367 feature = "Shared_Review";
368 break;
369 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
370 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
371 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
372 feature = "Shared_Form";
373 break;
374 case FPDF_UNSP_ANNOT_3DANNOT:
375 feature = "3D";
376 break;
377 case FPDF_UNSP_ANNOT_MOVIE:
378 feature = "Movie";
379 break;
380 case FPDF_UNSP_ANNOT_SOUND:
381 feature = "Sound";
382 break;
383 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
384 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
385 feature = "Screen";
386 break;
387 case FPDF_UNSP_ANNOT_SIG:
388 feature = "Digital_Signature";
389 break;
390 }
391 printf("Unsupported feature: %s.\n", feature.c_str());
392}
393
Tom Sepez5ee12d72014-12-17 16:24:01 -0800394bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700395 Options* options,
396 std::vector<std::string>* files) {
397 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800398 return false;
thestig514e8c92016-07-15 17:57:54 -0700399
Tom Sepez5ee12d72014-12-17 16:24:01 -0800400 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800401 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800402 for (; cur_idx < args.size(); ++cur_idx) {
403 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800404 if (cur_arg == "--show-config") {
405 options->show_config = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700406 } else if (cur_arg == "--send-events") {
407 options->send_events = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800408 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800409 if (options->output_format != OUTPUT_NONE) {
410 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
411 return false;
412 }
413 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800414 } else if (cur_arg == "--png") {
415 if (options->output_format != OUTPUT_NONE) {
416 fprintf(stderr, "Duplicate or conflicting --png argument\n");
417 return false;
418 }
419 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700420 } else if (cur_arg == "--txt") {
421 if (options->output_format != OUTPUT_NONE) {
422 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
423 return false;
424 }
425 options->output_format = OUTPUT_TEXT;
Cary Clark399be5b2016-03-14 16:51:29 -0400426#ifdef PDF_ENABLE_SKIA
427 } else if (cur_arg == "--skp") {
428 if (options->output_format != OUTPUT_NONE) {
429 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
430 return false;
431 }
432 options->output_format = OUTPUT_SKP;
433#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700434 } else if (cur_arg.size() > 11 &&
435 cur_arg.compare(0, 11, "--font-dir=") == 0) {
436 if (!options->font_directory.empty()) {
437 fprintf(stderr, "Duplicate --font-dir argument\n");
438 return false;
439 }
440 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700441#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500442 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800443 if (options->output_format != OUTPUT_NONE) {
444 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
445 return false;
446 }
447 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500448 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800449 if (options->output_format != OUTPUT_NONE) {
450 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
451 return false;
452 }
453 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800454#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500455
Tom Sepez452b4f32015-10-13 09:27:27 -0700456#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800457#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500458 } else if (cur_arg.size() > 10 &&
459 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800460 if (!options->bin_directory.empty()) {
461 fprintf(stderr, "Duplicate --bin-dir argument\n");
462 return false;
463 }
464 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800465#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700466#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500467
468 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800469 if (!options->scale_factor_as_string.empty()) {
470 fprintf(stderr, "Duplicate --scale argument\n");
471 return false;
472 }
473 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800474 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
475 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
476 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500477 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700478 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500479 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480 }
thestig514e8c92016-07-15 17:57:54 -0700481 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800482 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700483
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484 return true;
485}
486
Tom Sepezcf22eb82015-05-12 17:28:08 -0700487FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700488 return true;
489}
490
491void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
492}
493
tsepezf09bdfa2016-04-18 16:08:26 -0700494void SendPageEvents(const FPDF_FORMHANDLE& form,
495 const FPDF_PAGE& page,
496 const std::string& events) {
497 auto lines = StringSplit(events, '\n');
498 for (auto line : lines) {
499 auto command = StringSplit(line, '#');
500 if (command[0].empty())
501 continue;
502 auto tokens = StringSplit(command[0], ',');
503 if (tokens[0] == "keycode") {
504 if (tokens.size() == 2) {
505 int keycode = atoi(tokens[1].c_str());
506 FORM_OnKeyDown(form, page, keycode, 0);
507 FORM_OnKeyUp(form, page, keycode, 0);
508 } else {
509 fprintf(stderr, "keycode: bad args\n");
510 }
511 } else if (tokens[0] == "mousedown") {
512 if (tokens.size() == 4) {
513 int x = atoi(tokens[2].c_str());
514 int y = atoi(tokens[3].c_str());
515 if (tokens[1] == "left")
516 FORM_OnLButtonDown(form, page, 0, x, y);
517#ifdef PDF_ENABLE_XFA
518 else if (tokens[1] == "right")
519 FORM_OnRButtonDown(form, page, 0, x, y);
520#endif
521 else
522 fprintf(stderr, "mousedown: bad button name\n");
523 } else {
524 fprintf(stderr, "mousedown: bad args\n");
525 }
526 } else if (tokens[0] == "mouseup") {
527 if (tokens.size() == 4) {
528 int x = atoi(tokens[2].c_str());
529 int y = atoi(tokens[3].c_str());
530 if (tokens[1] == "left")
531 FORM_OnLButtonUp(form, page, 0, x, y);
532#ifdef PDF_ENABLE_XFA
533 else if (tokens[1] == "right")
534 FORM_OnRButtonUp(form, page, 0, x, y);
535#endif
536 else
537 fprintf(stderr, "mouseup: bad button name\n");
538 } else {
539 fprintf(stderr, "mouseup: bad args\n");
540 }
541 } else if (tokens[0] == "mousemove") {
542 if (tokens.size() == 3) {
543 int x = atoi(tokens[1].c_str());
544 int y = atoi(tokens[2].c_str());
545 FORM_OnMouseMove(form, page, 0, x, y);
546 } else {
547 fprintf(stderr, "mousemove: bad args\n");
548 }
549 } else {
550 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
551 }
552 }
553}
554
tonikitoo3e981582016-08-26 08:37:10 -0700555FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
556 FPDF_DOCUMENT doc,
557 int index) {
tonikitoo81d92f82016-09-21 12:44:56 -0700558 FPDF_FORMFILLINFO_PDFiumTest* formFillInfo = ToPDFiumTestFormFillInfo(param);
559 auto& loadedPages = formFillInfo->loadedPages;
560
561 auto iter = loadedPages.find(index);
562 if (iter != loadedPages.end())
tonikitoo3e981582016-08-26 08:37:10 -0700563 return iter->second;
564
565 FPDF_PAGE page = FPDF_LoadPage(doc, index);
566 if (!page)
567 return nullptr;
568
tonikitoo81d92f82016-09-21 12:44:56 -0700569 FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
tonikitoo3e981582016-08-26 08:37:10 -0700570
tonikitoo81d92f82016-09-21 12:44:56 -0700571 FORM_OnAfterLoadPage(page, formHandle);
572 FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
573
574 loadedPages[index] = page;
tonikitoo3e981582016-08-26 08:37:10 -0700575 return page;
576}
577
Jun Fangb553bcb2015-11-10 18:49:04 +0800578bool RenderPage(const std::string& name,
tonikitoo3e981582016-08-26 08:37:10 -0700579 FPDF_DOCUMENT doc,
580 FPDF_FORMHANDLE& form,
tonikitoo81d92f82016-09-21 12:44:56 -0700581 FPDF_FORMFILLINFO_PDFiumTest& formFillInfo,
Jun Fangb553bcb2015-11-10 18:49:04 +0800582 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -0700583 const Options& options,
584 const std::string& events) {
tonikitoo81d92f82016-09-21 12:44:56 -0700585 FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
thestig514e8c92016-07-15 17:57:54 -0700586 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +0800587 return false;
thestig514e8c92016-07-15 17:57:54 -0700588
Jun Fangdf7f3662015-11-10 18:29:18 +0800589 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
Jun Fangdf7f3662015-11-10 18:29:18 +0800590
tsepezf09bdfa2016-04-18 16:08:26 -0700591 if (options.send_events)
tsepez10b01bf2016-05-04 12:52:42 -0700592 SendPageEvents(form, page, events);
tsepezf09bdfa2016-04-18 16:08:26 -0700593
Jun Fangdf7f3662015-11-10 18:29:18 +0800594 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700595 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800596 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700597
Jun Fangdf7f3662015-11-10 18:29:18 +0800598 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
599 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800600 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
601 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
thestige97ea032016-05-19 10:59:15 -0700602 if (bitmap) {
603 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
604 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
jaepark75f84a52016-09-09 15:39:09 -0700605 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
Jun Fangdf7f3662015-11-10 18:29:18 +0800606
jaepark75f84a52016-09-09 15:39:09 -0700607 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, FPDF_ANNOT);
thestige97ea032016-05-19 10:59:15 -0700608 int stride = FPDFBitmap_GetStride(bitmap);
609 const char* buffer =
610 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
Jun Fangdf7f3662015-11-10 18:29:18 +0800611
thestige97ea032016-05-19 10:59:15 -0700612 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800613#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -0700614 case OUTPUT_BMP:
615 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
616 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800617
thestige97ea032016-05-19 10:59:15 -0700618 case OUTPUT_EMF:
619 WriteEmf(page, name.c_str(), page_index);
620 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800621#endif
dsinclairb63068f2016-06-16 07:58:09 -0700622 case OUTPUT_TEXT:
623 WriteText(page, name.c_str(), page_index);
624 break;
625
thestige97ea032016-05-19 10:59:15 -0700626 case OUTPUT_PNG:
627 WritePng(name.c_str(), page_index, buffer, stride, width, height);
628 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800629
thestige97ea032016-05-19 10:59:15 -0700630 case OUTPUT_PPM:
631 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
632 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800633
Cary Clark399be5b2016-03-14 16:51:29 -0400634#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -0700635 case OUTPUT_SKP: {
636 std::unique_ptr<SkPictureRecorder> recorder(
thestigb97e07e2016-09-28 22:16:40 -0700637 reinterpret_cast<SkPictureRecorder*>(
638 FPDF_RenderPageSkp(page, width, height)));
thestige97ea032016-05-19 10:59:15 -0700639 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
640 WriteSkp(name.c_str(), page_index, recorder.get());
641 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400642#endif
thestige97ea032016-05-19 10:59:15 -0700643 default:
644 break;
645 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800646
thestige97ea032016-05-19 10:59:15 -0700647 FPDFBitmap_Destroy(bitmap);
648 } else {
649 fprintf(stderr, "Page was too large to be rendered.\n");
650 }
tonikitoo3e981582016-08-26 08:37:10 -0700651
tonikitoo81d92f82016-09-21 12:44:56 -0700652 formFillInfo.loadedPages.erase(page_index);
653
Jun Fangdf7f3662015-11-10 18:29:18 +0800654 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
655 FORM_OnBeforeClosePage(page, form);
656 FPDFText_ClosePage(text_page);
657 FPDF_ClosePage(page);
thestige97ea032016-05-19 10:59:15 -0700658 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800659}
660
tsepezf09bdfa2016-04-18 16:08:26 -0700661void RenderPdf(const std::string& name,
662 const char* pBuf,
663 size_t len,
664 const Options& options,
665 const std::string& events) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700666 IPDF_JSPLATFORM platform_callbacks;
667 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700668 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800669 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800670 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800671 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800672 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700673
tonikitoo81d92f82016-09-21 12:44:56 -0700674 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
Tom Sepezc46d0002015-11-30 15:46:36 -0800675#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800676 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800677#else // PDF_ENABLE_XFA
678 form_callbacks.version = 1;
679#endif // PDF_ENABLE_XFA
tonikitoo3e981582016-08-26 08:37:10 -0700680 form_callbacks.FFI_GetPage = GetPageForIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700681 form_callbacks.m_pJsPlatform = &platform_callbacks;
682
683 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684 FPDF_FILEACCESS file_access;
685 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700686 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700687 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700688 file_access.m_Param = &loader;
689
690 FX_FILEAVAIL file_avail;
691 memset(&file_avail, '\0', sizeof(file_avail));
692 file_avail.version = 1;
693 file_avail.IsDataAvail = Is_Data_Avail;
694
695 FX_DOWNLOADHINTS hints;
696 memset(&hints, '\0', sizeof(hints));
697 hints.version = 1;
698 hints.AddSegment = Add_Segment;
699
700 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800701 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800702 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700703 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
thestigb97e07e2016-09-28 22:16:40 -0700704 std::unique_ptr<void, AvailDeleter> scoped_pdf_avail_deleter(pdf_avail);
Tom Sepezc98895c2015-11-24 15:30:36 -0800705
Jun Fangdf7f3662015-11-10 18:29:18 +0800706 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700707 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800708 if (doc) {
thestig514e8c92016-07-15 17:57:54 -0700709 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800710 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
thestig514e8c92016-07-15 17:57:54 -0700711
Jun Fangdf7f3662015-11-10 18:29:18 +0800712 if (nRet == PDF_DATA_ERROR) {
713 fprintf(stderr, "Unknown error in checking if doc was available.\n");
thestigb97e07e2016-09-28 22:16:40 -0700714 FPDF_CloseDocument(doc);
Jun Fangdf7f3662015-11-10 18:29:18 +0800715 return;
716 }
717 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
718 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
719 fprintf(stderr,
720 "Error %d was returned in checking if form was available.\n",
721 nRet);
thestigb97e07e2016-09-28 22:16:40 -0700722 FPDF_CloseDocument(doc);
Jun Fangdf7f3662015-11-10 18:29:18 +0800723 return;
724 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800725 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800726 }
727 } else {
Lei Zhang600d4072015-09-23 15:35:25 -0700728 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800729 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700730
Lei Zhang5377ebf2015-09-23 14:52:53 -0700731 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400732 unsigned long err = FPDF_GetLastError();
733 fprintf(stderr, "Load pdf docs unsuccessful: ");
734 switch (err) {
735 case FPDF_ERR_SUCCESS:
736 fprintf(stderr, "Success");
737 break;
738 case FPDF_ERR_UNKNOWN:
739 fprintf(stderr, "Unknown error");
740 break;
741 case FPDF_ERR_FILE:
742 fprintf(stderr, "File not found or could not be opened");
743 break;
744 case FPDF_ERR_FORMAT:
745 fprintf(stderr, "File not in PDF format or corrupted");
746 break;
747 case FPDF_ERR_PASSWORD:
748 fprintf(stderr, "Password required or incorrect password");
749 break;
750 case FPDF_ERR_SECURITY:
751 fprintf(stderr, "Unsupported security scheme");
752 break;
753 case FPDF_ERR_PAGE:
754 fprintf(stderr, "Page not found or content error");
755 break;
756 default:
757 fprintf(stderr, "Unknown error %ld", err);
758 }
759 fprintf(stderr, ".\n");
760
JUN FANG827a1722015-03-05 13:39:21 -0800761 return;
762 }
763
Lei Zhang5377ebf2015-09-23 14:52:53 -0700764 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700765
tonikitoo81d92f82016-09-21 12:44:56 -0700766 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
767 form_callbacks.formHandle = form;
768
Tom Sepezc46d0002015-11-30 15:46:36 -0800769#ifdef PDF_ENABLE_XFA
thestigb97e07e2016-09-28 22:16:40 -0700770 int doc_type = DOCTYPE_PDF;
771 if (FPDF_HasXFAField(doc, &doc_type) && doc_type != DOCTYPE_PDF &&
Lei Zhang5377ebf2015-09-23 14:52:53 -0700772 !FPDF_LoadXFA(doc)) {
773 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800774 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800775#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700776 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
777 FPDF_SetFormFieldHighlightAlpha(form, 100);
778
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700779 FORM_DoDocumentJSAction(form);
780 FORM_DoDocumentOpenAction(form);
781
Jun Fangdf7f3662015-11-10 18:29:18 +0800782 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700783 int rendered_pages = 0;
784 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700785 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800786 if (bIsLinearized) {
787 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -0700788 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800789 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
thestig514e8c92016-07-15 17:57:54 -0700790
Jun Fangdf7f3662015-11-10 18:29:18 +0800791 if (nRet == PDF_DATA_ERROR) {
792 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
793 i);
thestigb97e07e2016-09-28 22:16:40 -0700794 CloseDocAndForm(doc, form);
Jun Fangdf7f3662015-11-10 18:29:18 +0800795 return;
796 }
797 }
tonikitoo81d92f82016-09-21 12:44:56 -0700798 if (RenderPage(name, doc, form, form_callbacks, i, options, events))
Jun Fangdf7f3662015-11-10 18:29:18 +0800799 ++rendered_pages;
thestig514e8c92016-07-15 17:57:54 -0700800 else
Lei Zhang5377ebf2015-09-23 14:52:53 -0700801 ++bad_pages;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700802 }
803
804 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700805
thestigb97e07e2016-09-28 22:16:40 -0700806 CloseDocAndForm(doc, form);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700807
Tom Sepez1ed8a212015-05-11 15:25:39 -0700808 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -0700809 if (bad_pages)
810 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700811}
812
Tom Sepez2991d8d2016-01-15 16:02:48 -0800813static void ShowConfig() {
814 std::string config;
815 std::string maybe_comma;
816#if PDF_ENABLE_V8
817 config.append(maybe_comma);
818 config.append("V8");
819 maybe_comma = ",";
820#endif // PDF_ENABLE_V8
821#ifdef V8_USE_EXTERNAL_STARTUP_DATA
822 config.append(maybe_comma);
823 config.append("V8_EXTERNAL");
824 maybe_comma = ",";
825#endif // V8_USE_EXTERNAL_STARTUP_DATA
826#ifdef PDF_ENABLE_XFA
827 config.append(maybe_comma);
828 config.append("XFA");
829 maybe_comma = ",";
830#endif // PDF_ENABLE_XFA
831 printf("%s\n", config.c_str());
832}
833
thestig514e8c92016-07-15 17:57:54 -0700834static const char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800835 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800836 " --show-config - print build options and exit\n"
tsepez10b01bf2016-05-04 12:52:42 -0700837 " --send-events - send input described by .evt file\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700838 " --bin-dir=<path> - override path to v8 external data\n"
839 " --font-dir=<path> - override path to external fonts\n"
840 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800841#ifdef _WIN32
842 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
843 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800844#endif // _WIN32
thestig514e8c92016-07-15 17:57:54 -0700845 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800846 " --png - write page images <pdf-name>.<page-number>.png\n"
thestig514e8c92016-07-15 17:57:54 -0700847 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400848#ifdef PDF_ENABLE_SKIA
849 " --skp - write page images <pdf-name>.<page-number>.skp\n"
850#endif
851 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800852
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700853int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800854 std::vector<std::string> args(argv, argv + argc);
855 Options options;
thestig514e8c92016-07-15 17:57:54 -0700856 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800857 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -0700858 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700859 return 1;
860 }
861
Tom Sepez2991d8d2016-01-15 16:02:48 -0800862 if (options.show_config) {
863 ShowConfig();
864 return 0;
865 }
866
867 if (files.empty()) {
868 fprintf(stderr, "No input files.\n");
869 return 1;
870 }
871
Tom Sepez452b4f32015-10-13 09:27:27 -0700872#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700873 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800874#ifdef V8_USE_EXTERNAL_STARTUP_DATA
875 v8::StartupData natives;
876 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700877 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
878 &snapshot, &platform);
879#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700880 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800881#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700882#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800883
Tom Sepeza72e8e22015-10-07 10:17:53 -0700884 FPDF_LIBRARY_CONFIG config;
885 config.version = 2;
886 config.m_pUserFontPaths = nullptr;
887 config.m_pIsolate = nullptr;
888 config.m_v8EmbedderSlot = 0;
889
890 const char* path_array[2];
891 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700892 path_array[0] = options.font_directory.c_str();
893 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700894 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700895 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700896 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897
898 UNSUPPORT_INFO unsuppored_info;
899 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
900 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800901 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700902
903 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
904
thestig514e8c92016-07-15 17:57:54 -0700905 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800906 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800907 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
908 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700909 if (!file_contents)
910 continue;
tsepez10b01bf2016-05-04 12:52:42 -0700911 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700912 std::string events;
913 if (options.send_events) {
914 std::string event_filename = filename;
915 size_t event_length = 0;
916 size_t extension_pos = event_filename.find(".pdf");
917 if (extension_pos != std::string::npos) {
918 event_filename.replace(extension_pos, 4, ".evt");
919 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
920 GetFileContents(event_filename.c_str(), &event_length);
921 if (event_contents) {
tsepez10b01bf2016-05-04 12:52:42 -0700922 fprintf(stderr, "Sending events from: %s\n", event_filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700923 events = std::string(event_contents.get(), event_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700924 }
925 }
926 }
927 RenderPdf(filename, file_contents.get(), file_length, options, events);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700928 }
929
930 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700931#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800932 v8::V8::ShutdownPlatform();
933 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -0700934
935#ifdef V8_USE_EXTERNAL_STARTUP_DATA
936 free(const_cast<char*>(natives.data));
937 free(const_cast<char*>(snapshot.data));
938#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700939#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700940
941 return 0;
942}