blob: 83a8f7620b6fadc0079cbd22689ba5a22b2a00dd [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>
Tom Sepezfb947282014-12-08 09:55:11 -08009#include <wchar.h>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010
11#include <list>
Tom Sepezdaa2e842015-01-29 15:44:37 -080012#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013#include <string>
14#include <utility>
Tom Sepez5ee12d72014-12-17 16:24:01 -080015#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Tom Sepez1ed8a212015-05-11 15:25:39 -070017#include "../public/fpdf_dataavail.h"
18#include "../public/fpdf_ext.h"
19#include "../public/fpdf_formfill.h"
20#include "../public/fpdf_text.h"
21#include "../public/fpdfview.h"
Tom Sepezaf18cb32015-02-05 15:06:01 -080022#include "image_diff_png.h"
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080023#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070024#include "v8/include/v8.h"
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080025
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
27#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080028#define snprintf _snprintf
29#define PATH_SEPARATOR '\\'
30#else
31#define PATH_SEPARATOR '/'
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032#endif
33
Vitaly Buka9e0177a2014-07-22 18:15:42 -070034enum OutputFormat {
35 OUTPUT_NONE,
36 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080037 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070038#ifdef _WIN32
39 OUTPUT_BMP,
40 OUTPUT_EMF,
41#endif
42};
43
Tom Sepez5ee12d72014-12-17 16:24:01 -080044struct Options {
45 Options() : output_format(OUTPUT_NONE) { }
46
47 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080048 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080049 std::string exe_path;
50 std::string bin_directory;
51};
52
53// Reads the entire contents of a file into a newly malloc'd buffer.
54static char* GetFileContents(const char* filename, size_t* retlen) {
55 FILE* file = fopen(filename, "rb");
56 if (!file) {
57 fprintf(stderr, "Failed to open: %s\n", filename);
58 return NULL;
59 }
60 (void) fseek(file, 0, SEEK_END);
61 size_t file_length = ftell(file);
62 if (!file_length) {
63 return NULL;
64 }
65 (void) fseek(file, 0, SEEK_SET);
66 char* buffer = (char*) malloc(file_length);
67 if (!buffer) {
68 return NULL;
69 }
70 size_t bytes_read = fread(buffer, 1, file_length, file);
71 (void) fclose(file);
72 if (bytes_read != file_length) {
73 fprintf(stderr, "Failed to read: %s\n", filename);
74 free(buffer);
75 return NULL;
76 }
77 *retlen = bytes_read;
78 return buffer;
79}
80
81#ifdef V8_USE_EXTERNAL_STARTUP_DATA
82// Returns the full path for an external V8 data file based on either
83// the currect exectuable path or an explicit override.
84static std::string GetFullPathForSnapshotFile(const Options& options,
85 const std::string& filename) {
86 std::string result;
87 if (!options.bin_directory.empty()) {
88 result = options.bin_directory;
89 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
90 result += PATH_SEPARATOR;
91 }
92 } else if (!options.exe_path.empty()) {
93 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
94 if (last_separator != std::string::npos) {
95 result = options.exe_path.substr(0, last_separator + 1);
96 }
97 }
98 result += filename;
99 return result;
100}
101
102// Reads an extenal V8 data file from the |options|-indicated location,
103// returing true on success and false on error.
104static bool GetExternalData(const Options& options,
105 const std::string& bin_filename,
106 v8::StartupData* result_data) {
107 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
108 size_t data_length = 0;
109 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
110 if (!data_buffer) {
111 return false;
112 }
113 result_data->data = const_cast<const char*>(data_buffer);
114 result_data->raw_size = data_length;
115 return true;
116}
117#endif // V8_USE_EXTERNAL_STARTUP_DATA
118
Tom Sepezaf18cb32015-02-05 15:06:01 -0800119static bool CheckDimensions(int stride, int width, int height) {
120 if (stride < 0 || width < 0 || height < 0)
121 return false;
122 if (height > 0 && width > INT_MAX / height)
123 return false;
124 return true;
125}
126
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700127static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
128 int stride, int width, int height) {
129 const char* buffer = reinterpret_cast<const char*>(buffer_void);
130
Tom Sepezaf18cb32015-02-05 15:06:01 -0800131 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800133
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134 int out_len = width * height;
135 if (out_len > INT_MAX / 3)
136 return;
137 out_len *= 3;
138
139 char filename[256];
140 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700141 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142 if (!fp)
143 return;
144 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
145 // Source data is B, G, R, unused.
146 // Dest data is R, G, B.
147 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -0700148 for (int h = 0; h < height; ++h) {
149 const char* src_line = buffer + (stride * h);
150 char* dest_line = result + (width * h * 3);
151 for (int w = 0; w < width; ++w) {
152 // R
153 dest_line[w * 3] = src_line[(w * 4) + 2];
154 // G
155 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
156 // B
157 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159 }
Lei Zhange00660b2015-08-13 15:40:18 -0700160 fwrite(result, out_len, 1, fp);
161 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162 fclose(fp);
163}
164
Tom Sepezaf18cb32015-02-05 15:06:01 -0800165static void WritePng(const char* pdf_name, int num, const void* buffer_void,
166 int stride, int width, int height) {
167 if (!CheckDimensions(stride, width, height))
168 return;
169
170 std::vector<unsigned char> png_encoding;
171 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
172 if (!image_diff_png::EncodeBGRAPNG(
173 buffer, width, height, stride, false, &png_encoding)) {
174 fprintf(stderr, "Failed to convert bitmap to PNG\n");
175 return;
176 }
177
178 char filename[256];
179 int chars_formatted = snprintf(
180 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
181 if (chars_formatted < 0 ||
182 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
183 fprintf(stderr, "Filname %s is too long\n", filename);
184 return;
185 }
186
187 FILE* fp = fopen(filename, "wb");
188 if (!fp) {
189 fprintf(stderr, "Failed to open %s for output\n", filename);
190 return;
191 }
192
193 size_t bytes_written = fwrite(
194 &png_encoding.front(), 1, png_encoding.size(), fp);
195 if (bytes_written != png_encoding.size())
196 fprintf(stderr, "Failed to write to %s\n", filename);
197
198 (void) fclose(fp);
199}
200
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700201#ifdef _WIN32
202static void WriteBmp(const char* pdf_name, int num, const void* buffer,
203 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800204 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700205 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800206
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700207 int out_len = stride * height;
208 if (out_len > INT_MAX / 3)
209 return;
210
211 char filename[256];
212 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
213 FILE* fp = fopen(filename, "wb");
214 if (!fp)
215 return;
216
Nico Weber2827bdd2015-07-01 14:08:08 -0700217 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700218 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
219 bmi.bmiHeader.biWidth = width;
220 bmi.bmiHeader.biHeight = -height; // top-down image
221 bmi.bmiHeader.biPlanes = 1;
222 bmi.bmiHeader.biBitCount = 32;
223 bmi.bmiHeader.biCompression = BI_RGB;
224 bmi.bmiHeader.biSizeImage = 0;
225
Nico Weber2827bdd2015-07-01 14:08:08 -0700226 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700227 file_header.bfType = 0x4d42;
228 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
229 file_header.bfOffBits = file_header.bfSize - out_len;
230
231 fwrite(&file_header, sizeof(file_header), 1, fp);
232 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
233 fwrite(buffer, out_len, 1, fp);
234 fclose(fp);
235}
236
237void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
238 int width = static_cast<int>(FPDF_GetPageWidth(page));
239 int height = static_cast<int>(FPDF_GetPageHeight(page));
240
241 char filename[256];
242 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
243
244 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800245
246 HRGN rgn = CreateRectRgn(0, 0, width, height);
247 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700248 DeleteObject(rgn);
249
250 SelectObject(dc, GetStockObject(NULL_PEN));
251 SelectObject(dc, GetStockObject(WHITE_BRUSH));
252 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
253 Rectangle(dc, 0, 0, width + 1, height + 1);
254
255 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
256 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
257
258 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
259}
260#endif
261
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800262int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
263 int, int) {
Tom Sepezfb947282014-12-08 09:55:11 -0800264 // Deal with differences between UTF16LE and wchar_t on this platform.
265 size_t characters = 0;
266 while (msg[characters]) {
267 ++characters;
268 }
269 wchar_t* platform_string =
270 (wchar_t*)malloc((characters + 1) * sizeof(wchar_t));
271 for (size_t i = 0; i < characters + 1; ++i) {
272 unsigned char* ptr = (unsigned char*)&msg[i];
273 platform_string[i] = ptr[0] + 256 * ptr[1];
274 }
275 printf("Alert: %ls\n", platform_string);
276 free(platform_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277 return 0;
278}
279
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800280void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
281 printf("Goto Page: %d\n", pageNumber);
282}
283
284void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285 std::string feature = "Unknown";
286 switch (type) {
287 case FPDF_UNSP_DOC_XFAFORM:
288 feature = "XFA";
289 break;
290 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
291 feature = "Portfolios_Packages";
292 break;
293 case FPDF_UNSP_DOC_ATTACHMENT:
294 case FPDF_UNSP_ANNOT_ATTACHMENT:
295 feature = "Attachment";
296 break;
297 case FPDF_UNSP_DOC_SECURITY:
298 feature = "Rights_Management";
299 break;
300 case FPDF_UNSP_DOC_SHAREDREVIEW:
301 feature = "Shared_Review";
302 break;
303 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
304 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
305 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
306 feature = "Shared_Form";
307 break;
308 case FPDF_UNSP_ANNOT_3DANNOT:
309 feature = "3D";
310 break;
311 case FPDF_UNSP_ANNOT_MOVIE:
312 feature = "Movie";
313 break;
314 case FPDF_UNSP_ANNOT_SOUND:
315 feature = "Sound";
316 break;
317 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
318 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
319 feature = "Screen";
320 break;
321 case FPDF_UNSP_ANNOT_SIG:
322 feature = "Digital_Signature";
323 break;
324 }
325 printf("Unsupported feature: %s.\n", feature.c_str());
326}
327
Tom Sepez5ee12d72014-12-17 16:24:01 -0800328bool ParseCommandLine(const std::vector<std::string>& args,
329 Options* options, std::list<std::string>* files) {
330 if (args.empty()) {
331 return false;
332 }
333 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800334 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800335 for (; cur_idx < args.size(); ++cur_idx) {
336 const std::string& cur_arg = args[cur_idx];
337 if (cur_arg == "--ppm") {
338 if (options->output_format != OUTPUT_NONE) {
339 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
340 return false;
341 }
342 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800343 } else if (cur_arg == "--png") {
344 if (options->output_format != OUTPUT_NONE) {
345 fprintf(stderr, "Duplicate or conflicting --png argument\n");
346 return false;
347 }
348 options->output_format = OUTPUT_PNG;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800349 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700350#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800351 else if (cur_arg == "--emf") {
352 if (options->output_format != OUTPUT_NONE) {
353 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
354 return false;
355 }
356 options->output_format = OUTPUT_EMF;
357 }
358 else if (cur_arg == "--bmp") {
359 if (options->output_format != OUTPUT_NONE) {
360 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
361 return false;
362 }
363 options->output_format = OUTPUT_BMP;
364 }
365#endif // _WIN32
366#ifdef V8_USE_EXTERNAL_STARTUP_DATA
367 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
368 if (!options->bin_directory.empty()) {
369 fprintf(stderr, "Duplicate --bin-dir argument\n");
370 return false;
371 }
372 options->bin_directory = cur_arg.substr(10);
373 }
374#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezdaa2e842015-01-29 15:44:37 -0800375 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
376 if (!options->scale_factor_as_string.empty()) {
377 fprintf(stderr, "Duplicate --scale argument\n");
378 return false;
379 }
380 options->scale_factor_as_string = cur_arg.substr(8);
381 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700382 else
383 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800385 if (cur_idx >= args.size()) {
386 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700387 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800388 }
Bo Xud44e3922014-12-19 02:27:25 -0800389 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800390 files->push_back(args[i]);
391 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392 return true;
393}
394
395class TestLoader {
396 public:
397 TestLoader(const char* pBuf, size_t len);
398
399 const char* m_pBuf;
400 size_t m_Len;
401};
402
403TestLoader::TestLoader(const char* pBuf, size_t len)
404 : m_pBuf(pBuf), m_Len(len) {
405}
406
407int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
408 unsigned long size) {
409 TestLoader* pLoader = (TestLoader*) param;
410 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
411 memcpy(pBuf, pLoader->m_pBuf + pos, size);
412 return 1;
413}
414
Tom Sepezcf22eb82015-05-12 17:28:08 -0700415FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700416 return true;
417}
418
419void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
420}
421
Tom Sepez5ee12d72014-12-17 16:24:01 -0800422void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800423 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800424 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
426 IPDF_JSPLATFORM platform_callbacks;
427 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200428 platform_callbacks.version = 2;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800429 platform_callbacks.app_alert = ExampleAppAlert;
430 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700431
432 FPDF_FORMFILLINFO form_callbacks;
433 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800434 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435 form_callbacks.m_pJsPlatform = &platform_callbacks;
436
437 TestLoader loader(pBuf, len);
438
439 FPDF_FILEACCESS file_access;
440 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700441 file_access.m_FileLen = static_cast<unsigned long>(len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442 file_access.m_GetBlock = Get_Block;
443 file_access.m_Param = &loader;
444
445 FX_FILEAVAIL file_avail;
446 memset(&file_avail, '\0', sizeof(file_avail));
447 file_avail.version = 1;
448 file_avail.IsDataAvail = Is_Data_Avail;
449
450 FX_DOWNLOADHINTS hints;
451 memset(&hints, '\0', sizeof(hints));
452 hints.version = 1;
453 hints.AddSegment = Add_Segment;
454
455 FPDF_DOCUMENT doc;
456 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
457
458 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
459
460 if (!FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800461 fprintf(stderr, "Non-linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700462 doc = FPDF_LoadCustomDocument(&file_access, NULL);
463 } else {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800464 fprintf(stderr, "Linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
466 }
467
JUN FANG827a1722015-03-05 13:39:21 -0800468 if (!doc)
469 {
470 fprintf(stderr, "Load pdf docs unsuccessful.\n");
471 return;
472 }
473
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700474 (void) FPDF_GetDocPermissions(doc);
475 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
476
Bo Xu2b7a49d2014-11-14 17:40:50 -0800477 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800478 int docType = DOCTYPE_PDF;
Tom Sepezcf22eb82015-05-12 17:28:08 -0700479 if (FPDF_HasXFAField(doc, &docType))
JUN FANG827a1722015-03-05 13:39:21 -0800480 {
481 if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc))
482 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800483 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
485 FPDF_SetFormFieldHighlightAlpha(form, 100);
486
487 int first_page = FPDFAvail_GetFirstPageNum(doc);
488 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
489
490 int page_count = FPDF_GetPageCount(doc);
491 for (int i = 0; i < page_count; ++i) {
492 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
493 }
494
495 FORM_DoDocumentJSAction(form);
496 FORM_DoDocumentOpenAction(form);
497
Tom Sepez1ed8a212015-05-11 15:25:39 -0700498 int rendered_pages = 0;
499 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500 for (int i = 0; i < page_count; ++i) {
501 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700502 if (!page) {
503 bad_pages ++;
504 continue;
505 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700506 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
507 FORM_OnAfterLoadPage(page, form);
508 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
509
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800510 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800511 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800512 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800513 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800514 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
515 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800516
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700517 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700518 if (!bitmap) {
519 fprintf(stderr, "Page was too large to be rendered.\n");
520 bad_pages++;
521 continue;
522 }
523
Lei Zhang532a6a72014-07-09 11:47:15 -0700524 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangaeacba42014-08-22 17:04:29 -0700526 rendered_pages ++;
527
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700528 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700529 int stride = FPDFBitmap_GetStride(bitmap);
530 const char* buffer =
531 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
532
Tom Sepezdaa2e842015-01-29 15:44:37 -0800533 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700534#ifdef _WIN32
535 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800536 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700537 break;
538
539 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800540 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700541 break;
542#endif
543 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800544 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700545 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800546
547 case OUTPUT_PNG:
548 WritePng(name.c_str(), i, buffer, stride, width, height);
549 break;
550
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700551 default:
552 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700553 }
554
555 FPDFBitmap_Destroy(bitmap);
556
557 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
558 FORM_OnBeforeClosePage(page, form);
559 FPDFText_ClosePage(text_page);
560 FPDF_ClosePage(page);
561 }
562
563 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700564
565 // Note: The shut down order here is the reverse of the non-XFA branch order.
566 // Need to work out if this is required, and if it is, the lifetimes of
567 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700568 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800569 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700570
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700571 FPDFAvail_Destroy(pdf_avail);
572
Tom Sepez1ed8a212015-05-11 15:25:39 -0700573 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
574 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700575}
576
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800577static const char usage_string[] =
578 "Usage: pdfium_test [OPTION] [FILE]...\n"
579 " --bin-dir=<path> - override path to v8 external data\n"
580 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
581#ifdef _WIN32
582 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
583 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
584#endif
585 " --png - write page images <pdf-name>.<page-number>.png\n"
586 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
587
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700588int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800589 std::vector<std::string> args(argv, argv + argc);
590 Options options;
591 std::list<std::string> files;
592 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800593 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594 return 1;
595 }
596
Tom Sepez5ee12d72014-12-17 16:24:01 -0800597 v8::V8::InitializeICU();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800598 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
599 v8::V8::InitializePlatform(platform);
600 v8::V8::Initialize();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800601
602#ifdef V8_USE_EXTERNAL_STARTUP_DATA
603 v8::StartupData natives;
604 v8::StartupData snapshot;
605 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
606 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
607 return 1;
608 }
609 v8::V8::SetNativesDataBlob(&natives);
610 v8::V8::SetSnapshotDataBlob(&snapshot);
611#endif // V8_USE_EXTERNAL_STARTUP_DATA
612
John Abd-El-Malek207299b2014-12-15 12:13:45 -0800613 FPDF_InitLibrary();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700614
615 UNSUPPORT_INFO unsuppored_info;
616 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
617 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800618 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700619
620 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
621
622 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800623 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700624 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800625 size_t file_length = 0;
626 char* file_contents = GetFileContents(filename.c_str(), &file_length);
627 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800629 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800630 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700631 }
632
633 FPDF_DestroyLibrary();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800634 v8::V8::ShutdownPlatform();
635 delete platform;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700636
637 return 0;
638}