blob: da2d1f6977fa45e71fdf72ea756e96a9426a68e3 [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
Tom Sepezdaa2e842015-01-29 15:44:37 -080010#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011#include <string>
12#include <utility>
Tom Sepez5ee12d72014-12-17 16:24:01 -080013#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Cary Clark399be5b2016-03-14 16:51:29 -040015#if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
16#define _SKIA_SUPPORT_
17#endif
18
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080020#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080021#include "public/fpdf_ext.h"
22#include "public/fpdf_formfill.h"
23#include "public/fpdf_text.h"
24#include "public/fpdfview.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050025#include "samples/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080026#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070027
Tom Sepez452b4f32015-10-13 09:27:27 -070028#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080029#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070030#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080031#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
33#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080034#define snprintf _snprintf
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035#endif
36
Cary Clark399be5b2016-03-14 16:51:29 -040037#ifdef PDF_ENABLE_SKIA
38#include "third_party/skia/include/core/SkPictureRecorder.h"
39#include "third_party/skia/include/core/SkStream.h"
40#endif
41
Vitaly Buka9e0177a2014-07-22 18:15:42 -070042enum OutputFormat {
43 OUTPUT_NONE,
dsinclairb63068f2016-06-16 07:58:09 -070044 OUTPUT_TEXT,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070045 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080046 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070047#ifdef _WIN32
48 OUTPUT_BMP,
49 OUTPUT_EMF,
50#endif
Cary Clark399be5b2016-03-14 16:51:29 -040051#ifdef PDF_ENABLE_SKIA
52 OUTPUT_SKP,
53#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070054};
55
Tom Sepez5ee12d72014-12-17 16:24:01 -080056struct Options {
tsepezf09bdfa2016-04-18 16:08:26 -070057 Options()
58 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080059
Tom Sepez2991d8d2016-01-15 16:02:48 -080060 bool show_config;
tsepezf09bdfa2016-04-18 16:08:26 -070061 bool send_events;
Tom Sepez5ee12d72014-12-17 16:24:01 -080062 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080063 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080064 std::string exe_path;
65 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070066 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080067};
68
Tom Sepezaf18cb32015-02-05 15:06:01 -080069static bool CheckDimensions(int stride, int width, int height) {
70 if (stride < 0 || width < 0 || height < 0)
71 return false;
72 if (height > 0 && width > INT_MAX / height)
73 return false;
74 return true;
75}
76
Vitaly Buka9e0177a2014-07-22 18:15:42 -070077static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
78 int stride, int width, int height) {
79 const char* buffer = reinterpret_cast<const char*>(buffer_void);
80
Tom Sepezaf18cb32015-02-05 15:06:01 -080081 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080083
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084 int out_len = width * height;
85 if (out_len > INT_MAX / 3)
86 return;
87 out_len *= 3;
88
89 char filename[256];
90 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070091 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092 if (!fp)
93 return;
94 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
95 // Source data is B, G, R, unused.
96 // Dest data is R, G, B.
thestig514e8c92016-07-15 17:57:54 -070097 std::vector<char> result(out_len);
Lei Zhange00660b2015-08-13 15:40:18 -070098 for (int h = 0; h < height; ++h) {
99 const char* src_line = buffer + (stride * h);
thestig514e8c92016-07-15 17:57:54 -0700100 char* dest_line = result.data() + (width * h * 3);
Lei Zhange00660b2015-08-13 15:40:18 -0700101 for (int w = 0; w < width; ++w) {
102 // R
103 dest_line[w * 3] = src_line[(w * 4) + 2];
104 // G
105 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
106 // B
107 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109 }
thestig514e8c92016-07-15 17:57:54 -0700110 fwrite(result.data(), out_len, 1, fp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111 fclose(fp);
112}
113
dsinclairb63068f2016-06-16 07:58:09 -0700114void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
115 char filename[256];
116 int chars_formatted =
117 snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
118 if (chars_formatted < 0 ||
119 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
120 fprintf(stderr, "Filename %s is too long\n", filename);
121 return;
122 }
123
124 FILE* fp = fopen(filename, "w");
125 if (!fp) {
126 fprintf(stderr, "Failed to open %s for output\n", filename);
127 return;
128 }
129
130 // Output in UTF32-LE.
131 uint32_t bom = 0x0000FEFF;
132 fwrite(&bom, sizeof(bom), 1, fp);
133
134 FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page);
135 for (int i = 0; i < FPDFText_CountChars(textpage); i++) {
136 uint32_t c = FPDFText_GetUnicode(textpage, i);
137 fwrite(&c, sizeof(c), 1, fp);
138 }
139
140 FPDFText_ClosePage(textpage);
141
142 (void)fclose(fp);
143}
144
Tom Sepezaf18cb32015-02-05 15:06:01 -0800145static void WritePng(const char* pdf_name, int num, const void* buffer_void,
146 int stride, int width, int height) {
147 if (!CheckDimensions(stride, width, height))
148 return;
149
150 std::vector<unsigned char> png_encoding;
151 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
152 if (!image_diff_png::EncodeBGRAPNG(
153 buffer, width, height, stride, false, &png_encoding)) {
154 fprintf(stderr, "Failed to convert bitmap to PNG\n");
155 return;
156 }
157
158 char filename[256];
159 int chars_formatted = snprintf(
160 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
161 if (chars_formatted < 0 ||
162 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400163 fprintf(stderr, "Filename %s is too long\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800164 return;
165 }
166
167 FILE* fp = fopen(filename, "wb");
168 if (!fp) {
169 fprintf(stderr, "Failed to open %s for output\n", filename);
170 return;
171 }
172
173 size_t bytes_written = fwrite(
174 &png_encoding.front(), 1, png_encoding.size(), fp);
175 if (bytes_written != png_encoding.size())
176 fprintf(stderr, "Failed to write to %s\n", filename);
177
Lei Zhang5377ebf2015-09-23 14:52:53 -0700178 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800179}
180
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700181#ifdef _WIN32
182static void WriteBmp(const char* pdf_name, int num, const void* buffer,
183 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800184 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700185 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800186
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700187 int out_len = stride * height;
188 if (out_len > INT_MAX / 3)
189 return;
190
191 char filename[256];
192 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
193 FILE* fp = fopen(filename, "wb");
194 if (!fp)
195 return;
196
Nico Weber2827bdd2015-07-01 14:08:08 -0700197 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700198 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
199 bmi.bmiHeader.biWidth = width;
200 bmi.bmiHeader.biHeight = -height; // top-down image
201 bmi.bmiHeader.biPlanes = 1;
202 bmi.bmiHeader.biBitCount = 32;
203 bmi.bmiHeader.biCompression = BI_RGB;
204 bmi.bmiHeader.biSizeImage = 0;
205
Nico Weber2827bdd2015-07-01 14:08:08 -0700206 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700207 file_header.bfType = 0x4d42;
208 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
209 file_header.bfOffBits = file_header.bfSize - out_len;
210
211 fwrite(&file_header, sizeof(file_header), 1, fp);
212 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
213 fwrite(buffer, out_len, 1, fp);
214 fclose(fp);
215}
216
217void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
218 int width = static_cast<int>(FPDF_GetPageWidth(page));
219 int height = static_cast<int>(FPDF_GetPageHeight(page));
220
221 char filename[256];
222 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
223
Lei Zhang5377ebf2015-09-23 14:52:53 -0700224 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800225
226 HRGN rgn = CreateRectRgn(0, 0, width, height);
227 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700228 DeleteObject(rgn);
229
230 SelectObject(dc, GetStockObject(NULL_PEN));
231 SelectObject(dc, GetStockObject(WHITE_BRUSH));
232 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
233 Rectangle(dc, 0, 0, width + 1, height + 1);
234
235 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
236 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
237
238 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
239}
240#endif
241
Cary Clark399be5b2016-03-14 16:51:29 -0400242#ifdef PDF_ENABLE_SKIA
243void WriteSkp(const char* pdf_name, int num, const void* recorder) {
244 char filename[256];
245 int chars_formatted =
246 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
247
248 if (chars_formatted < 0 ||
249 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
250 fprintf(stderr, "Filename %s is too long\n", filename);
251 return;
252 }
253
254 SkPictureRecorder* r = (SkPictureRecorder*)recorder;
caryclark31735432016-05-11 10:52:53 -0700255 sk_sp<SkPicture> picture(r->finishRecordingAsPicture());
Cary Clark399be5b2016-03-14 16:51:29 -0400256 SkFILEWStream wStream(filename);
257 picture->serialize(&wStream);
258}
259#endif
260
Tom Sepez58fb36a2016-02-01 10:32:14 -0800261// These example JS platform callback handlers are entirely optional,
262// and exist here to show the flow of information from a document back
263// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800264int ExampleAppAlert(IPDF_JSPLATFORM*,
265 FPDF_WIDESTRING msg,
266 FPDF_WIDESTRING title,
267 int nType,
268 int nIcon) {
269 printf("%ls", GetPlatformWString(title).c_str());
270 if (nIcon || nType)
271 printf("[icon=%d,type=%d]", nIcon, nType);
272 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273 return 0;
274}
275
Tom Sepez58fb36a2016-02-01 10:32:14 -0800276int ExampleAppResponse(IPDF_JSPLATFORM*,
277 FPDF_WIDESTRING question,
278 FPDF_WIDESTRING title,
279 FPDF_WIDESTRING defaultValue,
280 FPDF_WIDESTRING label,
281 FPDF_BOOL isPassword,
282 void* response,
283 int length) {
284 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
285 GetPlatformWString(title).c_str(),
286 GetPlatformWString(question).c_str(),
287 GetPlatformWString(defaultValue).c_str(),
288 GetPlatformWString(label).c_str(), isPassword, length);
289
290 // UTF-16, always LE regardless of platform.
291 uint8_t* ptr = static_cast<uint8_t*>(response);
292 ptr[0] = 'N';
293 ptr[1] = 0;
294 ptr[2] = 'o';
295 ptr[3] = 0;
296 return 4;
297}
298
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800299void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
300 printf("Goto Page: %d\n", pageNumber);
301}
302
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800303void ExampleDocMail(IPDF_JSPLATFORM*,
304 void* mailData,
305 int length,
306 FPDF_BOOL bUI,
307 FPDF_WIDESTRING To,
308 FPDF_WIDESTRING Subject,
309 FPDF_WIDESTRING CC,
310 FPDF_WIDESTRING BCC,
311 FPDF_WIDESTRING Msg) {
312 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
313 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
314 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
315 GetPlatformWString(Msg).c_str());
316}
317
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800318void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319 std::string feature = "Unknown";
320 switch (type) {
321 case FPDF_UNSP_DOC_XFAFORM:
322 feature = "XFA";
323 break;
324 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
325 feature = "Portfolios_Packages";
326 break;
327 case FPDF_UNSP_DOC_ATTACHMENT:
328 case FPDF_UNSP_ANNOT_ATTACHMENT:
329 feature = "Attachment";
330 break;
331 case FPDF_UNSP_DOC_SECURITY:
332 feature = "Rights_Management";
333 break;
334 case FPDF_UNSP_DOC_SHAREDREVIEW:
335 feature = "Shared_Review";
336 break;
337 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
338 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
339 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
340 feature = "Shared_Form";
341 break;
342 case FPDF_UNSP_ANNOT_3DANNOT:
343 feature = "3D";
344 break;
345 case FPDF_UNSP_ANNOT_MOVIE:
346 feature = "Movie";
347 break;
348 case FPDF_UNSP_ANNOT_SOUND:
349 feature = "Sound";
350 break;
351 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
352 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
353 feature = "Screen";
354 break;
355 case FPDF_UNSP_ANNOT_SIG:
356 feature = "Digital_Signature";
357 break;
358 }
359 printf("Unsupported feature: %s.\n", feature.c_str());
360}
361
Tom Sepez5ee12d72014-12-17 16:24:01 -0800362bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700363 Options* options,
364 std::vector<std::string>* files) {
365 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800366 return false;
thestig514e8c92016-07-15 17:57:54 -0700367
Tom Sepez5ee12d72014-12-17 16:24:01 -0800368 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800369 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800370 for (; cur_idx < args.size(); ++cur_idx) {
371 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800372 if (cur_arg == "--show-config") {
373 options->show_config = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700374 } else if (cur_arg == "--send-events") {
375 options->send_events = true;
Tom Sepez2991d8d2016-01-15 16:02:48 -0800376 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800377 if (options->output_format != OUTPUT_NONE) {
378 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
379 return false;
380 }
381 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800382 } else if (cur_arg == "--png") {
383 if (options->output_format != OUTPUT_NONE) {
384 fprintf(stderr, "Duplicate or conflicting --png argument\n");
385 return false;
386 }
387 options->output_format = OUTPUT_PNG;
dsinclairb63068f2016-06-16 07:58:09 -0700388 } else if (cur_arg == "--txt") {
389 if (options->output_format != OUTPUT_NONE) {
390 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
391 return false;
392 }
393 options->output_format = OUTPUT_TEXT;
Cary Clark399be5b2016-03-14 16:51:29 -0400394#ifdef PDF_ENABLE_SKIA
395 } else if (cur_arg == "--skp") {
396 if (options->output_format != OUTPUT_NONE) {
397 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
398 return false;
399 }
400 options->output_format = OUTPUT_SKP;
401#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700402 } else if (cur_arg.size() > 11 &&
403 cur_arg.compare(0, 11, "--font-dir=") == 0) {
404 if (!options->font_directory.empty()) {
405 fprintf(stderr, "Duplicate --font-dir argument\n");
406 return false;
407 }
408 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700409#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500410 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800411 if (options->output_format != OUTPUT_NONE) {
412 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
413 return false;
414 }
415 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500416 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800417 if (options->output_format != OUTPUT_NONE) {
418 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
419 return false;
420 }
421 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800422#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500423
Tom Sepez452b4f32015-10-13 09:27:27 -0700424#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800425#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500426 } else if (cur_arg.size() > 10 &&
427 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800428 if (!options->bin_directory.empty()) {
429 fprintf(stderr, "Duplicate --bin-dir argument\n");
430 return false;
431 }
432 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800433#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700434#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500435
436 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800437 if (!options->scale_factor_as_string.empty()) {
438 fprintf(stderr, "Duplicate --scale argument\n");
439 return false;
440 }
441 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800442 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
443 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
444 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500445 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700446 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500447 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700448 }
thestig514e8c92016-07-15 17:57:54 -0700449 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800450 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700451
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700452 return true;
453}
454
Tom Sepezcf22eb82015-05-12 17:28:08 -0700455FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700456 return true;
457}
458
459void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
460}
461
tsepezf09bdfa2016-04-18 16:08:26 -0700462void SendPageEvents(const FPDF_FORMHANDLE& form,
463 const FPDF_PAGE& page,
464 const std::string& events) {
465 auto lines = StringSplit(events, '\n');
466 for (auto line : lines) {
467 auto command = StringSplit(line, '#');
468 if (command[0].empty())
469 continue;
470 auto tokens = StringSplit(command[0], ',');
471 if (tokens[0] == "keycode") {
472 if (tokens.size() == 2) {
473 int keycode = atoi(tokens[1].c_str());
474 FORM_OnKeyDown(form, page, keycode, 0);
475 FORM_OnKeyUp(form, page, keycode, 0);
476 } else {
477 fprintf(stderr, "keycode: bad args\n");
478 }
479 } else if (tokens[0] == "mousedown") {
480 if (tokens.size() == 4) {
481 int x = atoi(tokens[2].c_str());
482 int y = atoi(tokens[3].c_str());
483 if (tokens[1] == "left")
484 FORM_OnLButtonDown(form, page, 0, x, y);
485#ifdef PDF_ENABLE_XFA
486 else if (tokens[1] == "right")
487 FORM_OnRButtonDown(form, page, 0, x, y);
488#endif
489 else
490 fprintf(stderr, "mousedown: bad button name\n");
491 } else {
492 fprintf(stderr, "mousedown: bad args\n");
493 }
494 } else if (tokens[0] == "mouseup") {
495 if (tokens.size() == 4) {
496 int x = atoi(tokens[2].c_str());
497 int y = atoi(tokens[3].c_str());
498 if (tokens[1] == "left")
499 FORM_OnLButtonUp(form, page, 0, x, y);
500#ifdef PDF_ENABLE_XFA
501 else if (tokens[1] == "right")
502 FORM_OnRButtonUp(form, page, 0, x, y);
503#endif
504 else
505 fprintf(stderr, "mouseup: bad button name\n");
506 } else {
507 fprintf(stderr, "mouseup: bad args\n");
508 }
509 } else if (tokens[0] == "mousemove") {
510 if (tokens.size() == 3) {
511 int x = atoi(tokens[1].c_str());
512 int y = atoi(tokens[2].c_str());
513 FORM_OnMouseMove(form, page, 0, x, y);
514 } else {
515 fprintf(stderr, "mousemove: bad args\n");
516 }
517 } else {
518 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
519 }
520 }
521}
522
Jun Fangb553bcb2015-11-10 18:49:04 +0800523bool RenderPage(const std::string& name,
524 const FPDF_DOCUMENT& doc,
525 const FPDF_FORMHANDLE& form,
526 const int page_index,
tsepezf09bdfa2016-04-18 16:08:26 -0700527 const Options& options,
528 const std::string& events) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800529 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
thestig514e8c92016-07-15 17:57:54 -0700530 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +0800531 return false;
thestig514e8c92016-07-15 17:57:54 -0700532
Jun Fangdf7f3662015-11-10 18:29:18 +0800533 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
534 FORM_OnAfterLoadPage(page, form);
535 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
536
tsepezf09bdfa2016-04-18 16:08:26 -0700537 if (options.send_events)
tsepez10b01bf2016-05-04 12:52:42 -0700538 SendPageEvents(form, page, events);
tsepezf09bdfa2016-04-18 16:08:26 -0700539
Jun Fangdf7f3662015-11-10 18:29:18 +0800540 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700541 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800542 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700543
Jun Fangdf7f3662015-11-10 18:29:18 +0800544 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
545 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800546 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
547 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
thestige97ea032016-05-19 10:59:15 -0700548 if (bitmap) {
549 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
550 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
551 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangdf7f3662015-11-10 18:29:18 +0800552
thestige97ea032016-05-19 10:59:15 -0700553 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
554 int stride = FPDFBitmap_GetStride(bitmap);
555 const char* buffer =
556 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
Jun Fangdf7f3662015-11-10 18:29:18 +0800557
thestige97ea032016-05-19 10:59:15 -0700558 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800559#ifdef _WIN32
thestige97ea032016-05-19 10:59:15 -0700560 case OUTPUT_BMP:
561 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
562 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800563
thestige97ea032016-05-19 10:59:15 -0700564 case OUTPUT_EMF:
565 WriteEmf(page, name.c_str(), page_index);
566 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800567#endif
dsinclairb63068f2016-06-16 07:58:09 -0700568 case OUTPUT_TEXT:
569 WriteText(page, name.c_str(), page_index);
570 break;
571
thestige97ea032016-05-19 10:59:15 -0700572 case OUTPUT_PNG:
573 WritePng(name.c_str(), page_index, buffer, stride, width, height);
574 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800575
thestige97ea032016-05-19 10:59:15 -0700576 case OUTPUT_PPM:
577 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
578 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800579
Cary Clark399be5b2016-03-14 16:51:29 -0400580#ifdef PDF_ENABLE_SKIA
thestige97ea032016-05-19 10:59:15 -0700581 case OUTPUT_SKP: {
582 std::unique_ptr<SkPictureRecorder> recorder(
583 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height));
584 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
585 WriteSkp(name.c_str(), page_index, recorder.get());
586 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400587#endif
thestige97ea032016-05-19 10:59:15 -0700588 default:
589 break;
590 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800591
thestige97ea032016-05-19 10:59:15 -0700592 FPDFBitmap_Destroy(bitmap);
593 } else {
594 fprintf(stderr, "Page was too large to be rendered.\n");
595 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800596 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
597 FORM_OnBeforeClosePage(page, form);
598 FPDFText_ClosePage(text_page);
599 FPDF_ClosePage(page);
thestige97ea032016-05-19 10:59:15 -0700600 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800601}
602
tsepezf09bdfa2016-04-18 16:08:26 -0700603void RenderPdf(const std::string& name,
604 const char* pBuf,
605 size_t len,
606 const Options& options,
607 const std::string& events) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700608 IPDF_JSPLATFORM platform_callbacks;
609 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700610 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800611 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800612 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800613 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800614 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700615
616 FPDF_FORMFILLINFO form_callbacks;
617 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezc46d0002015-11-30 15:46:36 -0800618#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800619 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800620#else // PDF_ENABLE_XFA
621 form_callbacks.version = 1;
622#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700623 form_callbacks.m_pJsPlatform = &platform_callbacks;
624
625 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626 FPDF_FILEACCESS file_access;
627 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700628 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700629 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630 file_access.m_Param = &loader;
631
632 FX_FILEAVAIL file_avail;
633 memset(&file_avail, '\0', sizeof(file_avail));
634 file_avail.version = 1;
635 file_avail.IsDataAvail = Is_Data_Avail;
636
637 FX_DOWNLOADHINTS hints;
638 memset(&hints, '\0', sizeof(hints));
639 hints.version = 1;
640 hints.AddSegment = Add_Segment;
641
642 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800643 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800644 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800646
Jun Fangdf7f3662015-11-10 18:29:18 +0800647 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700648 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800649 if (doc) {
thestig514e8c92016-07-15 17:57:54 -0700650 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800651 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
thestig514e8c92016-07-15 17:57:54 -0700652
Jun Fangdf7f3662015-11-10 18:29:18 +0800653 if (nRet == PDF_DATA_ERROR) {
654 fprintf(stderr, "Unknown error in checking if doc was available.\n");
655 return;
656 }
657 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
658 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
659 fprintf(stderr,
660 "Error %d was returned in checking if form was available.\n",
661 nRet);
662 return;
663 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800664 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800665 }
666 } else {
Lei Zhang600d4072015-09-23 15:35:25 -0700667 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800668 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700669
Lei Zhang5377ebf2015-09-23 14:52:53 -0700670 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400671 unsigned long err = FPDF_GetLastError();
672 fprintf(stderr, "Load pdf docs unsuccessful: ");
673 switch (err) {
674 case FPDF_ERR_SUCCESS:
675 fprintf(stderr, "Success");
676 break;
677 case FPDF_ERR_UNKNOWN:
678 fprintf(stderr, "Unknown error");
679 break;
680 case FPDF_ERR_FILE:
681 fprintf(stderr, "File not found or could not be opened");
682 break;
683 case FPDF_ERR_FORMAT:
684 fprintf(stderr, "File not in PDF format or corrupted");
685 break;
686 case FPDF_ERR_PASSWORD:
687 fprintf(stderr, "Password required or incorrect password");
688 break;
689 case FPDF_ERR_SECURITY:
690 fprintf(stderr, "Unsupported security scheme");
691 break;
692 case FPDF_ERR_PAGE:
693 fprintf(stderr, "Page not found or content error");
694 break;
695 default:
696 fprintf(stderr, "Unknown error %ld", err);
697 }
698 fprintf(stderr, ".\n");
699
Qin Zhao05224972015-10-20 18:31:06 -0400700 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800701 return;
702 }
703
Lei Zhang5377ebf2015-09-23 14:52:53 -0700704 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700705
Bo Xu2b7a49d2014-11-14 17:40:50 -0800706 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
Tom Sepezc46d0002015-11-30 15:46:36 -0800707#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800708 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700709 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
710 !FPDF_LoadXFA(doc)) {
711 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800712 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800713#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700714 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
715 FPDF_SetFormFieldHighlightAlpha(form, 100);
716
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700717 FORM_DoDocumentJSAction(form);
718 FORM_DoDocumentOpenAction(form);
719
Jun Fangdf7f3662015-11-10 18:29:18 +0800720 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700721 int rendered_pages = 0;
722 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700723 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800724 if (bIsLinearized) {
725 nRet = PDF_DATA_NOTAVAIL;
thestig514e8c92016-07-15 17:57:54 -0700726 while (nRet == PDF_DATA_NOTAVAIL)
Jun Fangdf7f3662015-11-10 18:29:18 +0800727 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
thestig514e8c92016-07-15 17:57:54 -0700728
Jun Fangdf7f3662015-11-10 18:29:18 +0800729 if (nRet == PDF_DATA_ERROR) {
730 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
731 i);
732 return;
733 }
734 }
thestig514e8c92016-07-15 17:57:54 -0700735 if (RenderPage(name, doc, form, i, options, events))
Jun Fangdf7f3662015-11-10 18:29:18 +0800736 ++rendered_pages;
thestig514e8c92016-07-15 17:57:54 -0700737 else
Lei Zhang5377ebf2015-09-23 14:52:53 -0700738 ++bad_pages;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700739 }
740
741 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700742
Tom Sepezc46d0002015-11-30 15:46:36 -0800743#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700744 // Note: The shut down order here is the reverse of the non-XFA branch order.
745 // Need to work out if this is required, and if it is, the lifetimes of
746 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700747 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800748 FPDFDOC_ExitFormFillEnvironment(form);
Tom Sepezc46d0002015-11-30 15:46:36 -0800749#else // PDF_ENABLE_XFA
750 FPDFDOC_ExitFormFillEnvironment(form);
751 FPDF_CloseDocument(doc);
752#endif // PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700753
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700754 FPDFAvail_Destroy(pdf_avail);
755
Tom Sepez1ed8a212015-05-11 15:25:39 -0700756 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
tsepez10b01bf2016-05-04 12:52:42 -0700757 if (bad_pages)
758 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700759}
760
Tom Sepez2991d8d2016-01-15 16:02:48 -0800761static void ShowConfig() {
762 std::string config;
763 std::string maybe_comma;
764#if PDF_ENABLE_V8
765 config.append(maybe_comma);
766 config.append("V8");
767 maybe_comma = ",";
768#endif // PDF_ENABLE_V8
769#ifdef V8_USE_EXTERNAL_STARTUP_DATA
770 config.append(maybe_comma);
771 config.append("V8_EXTERNAL");
772 maybe_comma = ",";
773#endif // V8_USE_EXTERNAL_STARTUP_DATA
774#ifdef PDF_ENABLE_XFA
775 config.append(maybe_comma);
776 config.append("XFA");
777 maybe_comma = ",";
778#endif // PDF_ENABLE_XFA
779 printf("%s\n", config.c_str());
780}
781
thestig514e8c92016-07-15 17:57:54 -0700782static const char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800783 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800784 " --show-config - print build options and exit\n"
tsepez10b01bf2016-05-04 12:52:42 -0700785 " --send-events - send input described by .evt file\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700786 " --bin-dir=<path> - override path to v8 external data\n"
787 " --font-dir=<path> - override path to external fonts\n"
788 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800789#ifdef _WIN32
790 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
791 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800792#endif // _WIN32
thestig514e8c92016-07-15 17:57:54 -0700793 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800794 " --png - write page images <pdf-name>.<page-number>.png\n"
thestig514e8c92016-07-15 17:57:54 -0700795 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400796#ifdef PDF_ENABLE_SKIA
797 " --skp - write page images <pdf-name>.<page-number>.skp\n"
798#endif
799 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800800
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700801int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800802 std::vector<std::string> args(argv, argv + argc);
803 Options options;
thestig514e8c92016-07-15 17:57:54 -0700804 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800805 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -0700806 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700807 return 1;
808 }
809
Tom Sepez2991d8d2016-01-15 16:02:48 -0800810 if (options.show_config) {
811 ShowConfig();
812 return 0;
813 }
814
815 if (files.empty()) {
816 fprintf(stderr, "No input files.\n");
817 return 1;
818 }
819
Tom Sepez452b4f32015-10-13 09:27:27 -0700820#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700821 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800822#ifdef V8_USE_EXTERNAL_STARTUP_DATA
823 v8::StartupData natives;
824 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700825 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
826 &snapshot, &platform);
827#else // V8_USE_EXTERNAL_STARTUP_DATA
jochen9e077d22016-06-09 02:51:13 -0700828 InitializeV8ForPDFium(options.exe_path, &platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800829#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700830#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800831
Tom Sepeza72e8e22015-10-07 10:17:53 -0700832 FPDF_LIBRARY_CONFIG config;
833 config.version = 2;
834 config.m_pUserFontPaths = nullptr;
835 config.m_pIsolate = nullptr;
836 config.m_v8EmbedderSlot = 0;
837
838 const char* path_array[2];
839 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700840 path_array[0] = options.font_directory.c_str();
841 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700842 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700843 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700844 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700845
846 UNSUPPORT_INFO unsuppored_info;
847 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
848 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800849 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700850
851 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
852
thestig514e8c92016-07-15 17:57:54 -0700853 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800854 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800855 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
856 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700857 if (!file_contents)
858 continue;
tsepez10b01bf2016-05-04 12:52:42 -0700859 fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700860 std::string events;
861 if (options.send_events) {
862 std::string event_filename = filename;
863 size_t event_length = 0;
864 size_t extension_pos = event_filename.find(".pdf");
865 if (extension_pos != std::string::npos) {
866 event_filename.replace(extension_pos, 4, ".evt");
867 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
868 GetFileContents(event_filename.c_str(), &event_length);
869 if (event_contents) {
tsepez10b01bf2016-05-04 12:52:42 -0700870 fprintf(stderr, "Sending events from: %s\n", event_filename.c_str());
tsepezf09bdfa2016-04-18 16:08:26 -0700871 events = std::string(event_contents.get(), event_length);
tsepezf09bdfa2016-04-18 16:08:26 -0700872 }
873 }
874 }
875 RenderPdf(filename, file_contents.get(), file_length, options, events);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700876 }
877
878 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700879#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800880 v8::V8::ShutdownPlatform();
881 delete platform;
thestigc08cd7a2016-06-27 09:47:59 -0700882
883#ifdef V8_USE_EXTERNAL_STARTUP_DATA
884 free(const_cast<char*>(natives.data));
885 free(const_cast<char*>(snapshot.data));
886#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700887#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888
889 return 0;
890}