blob: 52e0125f09d5b0554fea12f83c9978478e22ff27 [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
John Abd-El-Malek197fd8d2014-05-23 19:27:48 -070017#include "../fpdfsdk/include/fpdf_dataavail.h"
18#include "../fpdfsdk/include/fpdf_ext.h"
19#include "../fpdfsdk/include/fpdfformfill.h"
20#include "../fpdfsdk/include/fpdftext.h"
21#include "../fpdfsdk/include/fpdfview.h"
Bruce Dawsonddc20622014-11-18 13:42:28 -080022#include "../core/include/fxcrt/fx_system.h"
Tom Sepezaf18cb32015-02-05 15:06:01 -080023#include "image_diff_png.h"
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080024#include "v8/include/v8.h"
25#include "v8/include/libplatform/libplatform.h"
26
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
28#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080029#define snprintf _snprintf
30#define PATH_SEPARATOR '\\'
31#else
32#define PATH_SEPARATOR '/'
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033#endif
34
Vitaly Buka9e0177a2014-07-22 18:15:42 -070035enum OutputFormat {
36 OUTPUT_NONE,
37 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080038 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070039#ifdef _WIN32
40 OUTPUT_BMP,
41 OUTPUT_EMF,
42#endif
43};
44
Tom Sepez5ee12d72014-12-17 16:24:01 -080045struct Options {
46 Options() : output_format(OUTPUT_NONE) { }
47
48 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080049 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080050 std::string exe_path;
51 std::string bin_directory;
52};
53
54// Reads the entire contents of a file into a newly malloc'd buffer.
55static char* GetFileContents(const char* filename, size_t* retlen) {
56 FILE* file = fopen(filename, "rb");
57 if (!file) {
58 fprintf(stderr, "Failed to open: %s\n", filename);
59 return NULL;
60 }
61 (void) fseek(file, 0, SEEK_END);
62 size_t file_length = ftell(file);
63 if (!file_length) {
64 return NULL;
65 }
66 (void) fseek(file, 0, SEEK_SET);
67 char* buffer = (char*) malloc(file_length);
68 if (!buffer) {
69 return NULL;
70 }
71 size_t bytes_read = fread(buffer, 1, file_length, file);
72 (void) fclose(file);
73 if (bytes_read != file_length) {
74 fprintf(stderr, "Failed to read: %s\n", filename);
75 free(buffer);
76 return NULL;
77 }
78 *retlen = bytes_read;
79 return buffer;
80}
81
82#ifdef V8_USE_EXTERNAL_STARTUP_DATA
83// Returns the full path for an external V8 data file based on either
84// the currect exectuable path or an explicit override.
85static std::string GetFullPathForSnapshotFile(const Options& options,
86 const std::string& filename) {
87 std::string result;
88 if (!options.bin_directory.empty()) {
89 result = options.bin_directory;
90 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
91 result += PATH_SEPARATOR;
92 }
93 } else if (!options.exe_path.empty()) {
94 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
95 if (last_separator != std::string::npos) {
96 result = options.exe_path.substr(0, last_separator + 1);
97 }
98 }
99 result += filename;
100 return result;
101}
102
103// Reads an extenal V8 data file from the |options|-indicated location,
104// returing true on success and false on error.
105static bool GetExternalData(const Options& options,
106 const std::string& bin_filename,
107 v8::StartupData* result_data) {
108 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
109 size_t data_length = 0;
110 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
111 if (!data_buffer) {
112 return false;
113 }
114 result_data->data = const_cast<const char*>(data_buffer);
115 result_data->raw_size = data_length;
116 return true;
117}
118#endif // V8_USE_EXTERNAL_STARTUP_DATA
119
Tom Sepezaf18cb32015-02-05 15:06:01 -0800120static bool CheckDimensions(int stride, int width, int height) {
121 if (stride < 0 || width < 0 || height < 0)
122 return false;
123 if (height > 0 && width > INT_MAX / height)
124 return false;
125 return true;
126}
127
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700128static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
129 int stride, int width, int height) {
130 const char* buffer = reinterpret_cast<const char*>(buffer_void);
131
Tom Sepezaf18cb32015-02-05 15:06:01 -0800132 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800134
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135 int out_len = width * height;
136 if (out_len > INT_MAX / 3)
137 return;
138 out_len *= 3;
139
140 char filename[256];
141 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700142 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143 if (!fp)
144 return;
145 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
146 // Source data is B, G, R, unused.
147 // Dest data is R, G, B.
148 char* result = new char[out_len];
149 if (result) {
150 for (int h = 0; h < height; ++h) {
151 const char* src_line = buffer + (stride * h);
152 char* dest_line = result + (width * h * 3);
153 for (int w = 0; w < width; ++w) {
154 // R
155 dest_line[w * 3] = src_line[(w * 4) + 2];
156 // G
157 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
158 // B
159 dest_line[(w * 3) + 2] = src_line[w * 4];
160 }
161 }
162 fwrite(result, out_len, 1, fp);
163 delete [] result;
164 }
165 fclose(fp);
166}
167
Tom Sepezaf18cb32015-02-05 15:06:01 -0800168static void WritePng(const char* pdf_name, int num, const void* buffer_void,
169 int stride, int width, int height) {
170 if (!CheckDimensions(stride, width, height))
171 return;
172
173 std::vector<unsigned char> png_encoding;
174 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
175 if (!image_diff_png::EncodeBGRAPNG(
176 buffer, width, height, stride, false, &png_encoding)) {
177 fprintf(stderr, "Failed to convert bitmap to PNG\n");
178 return;
179 }
180
181 char filename[256];
182 int chars_formatted = snprintf(
183 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
184 if (chars_formatted < 0 ||
185 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
186 fprintf(stderr, "Filname %s is too long\n", filename);
187 return;
188 }
189
190 FILE* fp = fopen(filename, "wb");
191 if (!fp) {
192 fprintf(stderr, "Failed to open %s for output\n", filename);
193 return;
194 }
195
196 size_t bytes_written = fwrite(
197 &png_encoding.front(), 1, png_encoding.size(), fp);
198 if (bytes_written != png_encoding.size())
199 fprintf(stderr, "Failed to write to %s\n", filename);
200
201 (void) fclose(fp);
202}
203
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700204#ifdef _WIN32
205static void WriteBmp(const char* pdf_name, int num, const void* buffer,
206 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800207 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700208 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800209
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700210 int out_len = stride * height;
211 if (out_len > INT_MAX / 3)
212 return;
213
214 char filename[256];
215 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
216 FILE* fp = fopen(filename, "wb");
217 if (!fp)
218 return;
219
220 BITMAPINFO bmi = {0};
221 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
222 bmi.bmiHeader.biWidth = width;
223 bmi.bmiHeader.biHeight = -height; // top-down image
224 bmi.bmiHeader.biPlanes = 1;
225 bmi.bmiHeader.biBitCount = 32;
226 bmi.bmiHeader.biCompression = BI_RGB;
227 bmi.bmiHeader.biSizeImage = 0;
228
229 BITMAPFILEHEADER file_header = {0};
230 file_header.bfType = 0x4d42;
231 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
232 file_header.bfOffBits = file_header.bfSize - out_len;
233
234 fwrite(&file_header, sizeof(file_header), 1, fp);
235 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
236 fwrite(buffer, out_len, 1, fp);
237 fclose(fp);
238}
239
240void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
241 int width = static_cast<int>(FPDF_GetPageWidth(page));
242 int height = static_cast<int>(FPDF_GetPageHeight(page));
243
244 char filename[256];
245 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
246
247 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800248
249 HRGN rgn = CreateRectRgn(0, 0, width, height);
250 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700251 DeleteObject(rgn);
252
253 SelectObject(dc, GetStockObject(NULL_PEN));
254 SelectObject(dc, GetStockObject(WHITE_BRUSH));
255 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
256 Rectangle(dc, 0, 0, width + 1, height + 1);
257
258 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
259 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
260
261 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
262}
263#endif
264
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800265int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
266 int, int) {
Tom Sepezfb947282014-12-08 09:55:11 -0800267 // Deal with differences between UTF16LE and wchar_t on this platform.
268 size_t characters = 0;
269 while (msg[characters]) {
270 ++characters;
271 }
272 wchar_t* platform_string =
273 (wchar_t*)malloc((characters + 1) * sizeof(wchar_t));
274 for (size_t i = 0; i < characters + 1; ++i) {
275 unsigned char* ptr = (unsigned char*)&msg[i];
276 platform_string[i] = ptr[0] + 256 * ptr[1];
277 }
278 printf("Alert: %ls\n", platform_string);
279 free(platform_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280 return 0;
281}
282
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800283void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
284 printf("Goto Page: %d\n", pageNumber);
285}
286
287void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288 std::string feature = "Unknown";
289 switch (type) {
290 case FPDF_UNSP_DOC_XFAFORM:
291 feature = "XFA";
292 break;
293 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
294 feature = "Portfolios_Packages";
295 break;
296 case FPDF_UNSP_DOC_ATTACHMENT:
297 case FPDF_UNSP_ANNOT_ATTACHMENT:
298 feature = "Attachment";
299 break;
300 case FPDF_UNSP_DOC_SECURITY:
301 feature = "Rights_Management";
302 break;
303 case FPDF_UNSP_DOC_SHAREDREVIEW:
304 feature = "Shared_Review";
305 break;
306 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
307 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
308 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
309 feature = "Shared_Form";
310 break;
311 case FPDF_UNSP_ANNOT_3DANNOT:
312 feature = "3D";
313 break;
314 case FPDF_UNSP_ANNOT_MOVIE:
315 feature = "Movie";
316 break;
317 case FPDF_UNSP_ANNOT_SOUND:
318 feature = "Sound";
319 break;
320 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
321 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
322 feature = "Screen";
323 break;
324 case FPDF_UNSP_ANNOT_SIG:
325 feature = "Digital_Signature";
326 break;
327 }
328 printf("Unsupported feature: %s.\n", feature.c_str());
329}
330
Tom Sepez5ee12d72014-12-17 16:24:01 -0800331bool ParseCommandLine(const std::vector<std::string>& args,
332 Options* options, std::list<std::string>* files) {
333 if (args.empty()) {
334 return false;
335 }
336 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800337 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800338 for (; cur_idx < args.size(); ++cur_idx) {
339 const std::string& cur_arg = args[cur_idx];
340 if (cur_arg == "--ppm") {
341 if (options->output_format != OUTPUT_NONE) {
342 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
343 return false;
344 }
345 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800346 } else if (cur_arg == "--png") {
347 if (options->output_format != OUTPUT_NONE) {
348 fprintf(stderr, "Duplicate or conflicting --png argument\n");
349 return false;
350 }
351 options->output_format = OUTPUT_PNG;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800352 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700353#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800354 else if (cur_arg == "--emf") {
355 if (options->output_format != OUTPUT_NONE) {
356 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
357 return false;
358 }
359 options->output_format = OUTPUT_EMF;
360 }
361 else if (cur_arg == "--bmp") {
362 if (options->output_format != OUTPUT_NONE) {
363 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
364 return false;
365 }
366 options->output_format = OUTPUT_BMP;
367 }
368#endif // _WIN32
369#ifdef V8_USE_EXTERNAL_STARTUP_DATA
370 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
371 if (!options->bin_directory.empty()) {
372 fprintf(stderr, "Duplicate --bin-dir argument\n");
373 return false;
374 }
375 options->bin_directory = cur_arg.substr(10);
376 }
377#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezdaa2e842015-01-29 15:44:37 -0800378 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
379 if (!options->scale_factor_as_string.empty()) {
380 fprintf(stderr, "Duplicate --scale argument\n");
381 return false;
382 }
383 options->scale_factor_as_string = cur_arg.substr(8);
384 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700385 else
386 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800388 if (cur_idx >= args.size()) {
389 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700390 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800391 }
Bo Xud44e3922014-12-19 02:27:25 -0800392 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800393 files->push_back(args[i]);
394 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395 return true;
396}
397
398class TestLoader {
399 public:
400 TestLoader(const char* pBuf, size_t len);
401
402 const char* m_pBuf;
403 size_t m_Len;
404};
405
406TestLoader::TestLoader(const char* pBuf, size_t len)
407 : m_pBuf(pBuf), m_Len(len) {
408}
409
410int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
411 unsigned long size) {
412 TestLoader* pLoader = (TestLoader*) param;
413 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
414 memcpy(pBuf, pLoader->m_pBuf + pos, size);
415 return 1;
416}
417
418bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
419 return true;
420}
421
422void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
423}
424
Tom Sepez5ee12d72014-12-17 16:24:01 -0800425void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800426 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800427 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
429 IPDF_JSPLATFORM platform_callbacks;
430 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
431 platform_callbacks.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800432 platform_callbacks.app_alert = ExampleAppAlert;
433 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434
435 FPDF_FORMFILLINFO form_callbacks;
436 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800437 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438 form_callbacks.m_pJsPlatform = &platform_callbacks;
439
440 TestLoader loader(pBuf, len);
441
442 FPDF_FILEACCESS file_access;
443 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700444 file_access.m_FileLen = static_cast<unsigned long>(len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700445 file_access.m_GetBlock = Get_Block;
446 file_access.m_Param = &loader;
447
448 FX_FILEAVAIL file_avail;
449 memset(&file_avail, '\0', sizeof(file_avail));
450 file_avail.version = 1;
451 file_avail.IsDataAvail = Is_Data_Avail;
452
453 FX_DOWNLOADHINTS hints;
454 memset(&hints, '\0', sizeof(hints));
455 hints.version = 1;
456 hints.AddSegment = Add_Segment;
457
458 FPDF_DOCUMENT doc;
459 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
460
461 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
462
463 if (!FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800464 fprintf(stderr, "Non-linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465 doc = FPDF_LoadCustomDocument(&file_access, NULL);
466 } else {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800467 fprintf(stderr, "Linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
469 }
470
JUN FANG827a1722015-03-05 13:39:21 -0800471 if (!doc)
472 {
473 fprintf(stderr, "Load pdf docs unsuccessful.\n");
474 return;
475 }
476
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700477 (void) FPDF_GetDocPermissions(doc);
478 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
479
Bo Xu2b7a49d2014-11-14 17:40:50 -0800480 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800481 int docType = DOCTYPE_PDF;
482 if (FPDF_HasXFAField(doc, docType))
483 {
484 if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc))
485 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800486 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700487 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
488 FPDF_SetFormFieldHighlightAlpha(form, 100);
489
490 int first_page = FPDFAvail_GetFirstPageNum(doc);
491 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
492
493 int page_count = FPDF_GetPageCount(doc);
494 for (int i = 0; i < page_count; ++i) {
495 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
496 }
497
498 FORM_DoDocumentJSAction(form);
499 FORM_DoDocumentOpenAction(form);
500
Jun Fangaeacba42014-08-22 17:04:29 -0700501 size_t rendered_pages = 0;
502 size_t bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503 for (int i = 0; i < page_count; ++i) {
504 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700505 if (!page) {
506 bad_pages ++;
507 continue;
508 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700509 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
510 FORM_OnAfterLoadPage(page, form);
511 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
512
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800513 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800514 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800515 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800516 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800517 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
518 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800519
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700520 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700521 if (!bitmap) {
522 fprintf(stderr, "Page was too large to be rendered.\n");
523 bad_pages++;
524 continue;
525 }
526
Lei Zhang532a6a72014-07-09 11:47:15 -0700527 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700528 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangaeacba42014-08-22 17:04:29 -0700529 rendered_pages ++;
530
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700531 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700532 int stride = FPDFBitmap_GetStride(bitmap);
533 const char* buffer =
534 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
535
Tom Sepezdaa2e842015-01-29 15:44:37 -0800536 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700537#ifdef _WIN32
538 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800539 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700540 break;
541
542 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800543 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700544 break;
545#endif
546 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800547 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700548 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800549
550 case OUTPUT_PNG:
551 WritePng(name.c_str(), i, buffer, stride, width, height);
552 break;
553
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700554 default:
555 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700556 }
557
558 FPDFBitmap_Destroy(bitmap);
559
560 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
561 FORM_OnBeforeClosePage(page, form);
562 FPDFText_ClosePage(text_page);
563 FPDF_ClosePage(page);
564 }
565
566 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700567 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800568 FPDFDOC_ExitFormFillEnvironment(form);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569 FPDFAvail_Destroy(pdf_avail);
570
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800571 fprintf(stderr, "Rendered %" PRIuS " pages.\n", rendered_pages);
572 fprintf(stderr, "Skipped %" PRIuS " bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700573}
574
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800575static const char usage_string[] =
576 "Usage: pdfium_test [OPTION] [FILE]...\n"
577 " --bin-dir=<path> - override path to v8 external data\n"
578 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
579#ifdef _WIN32
580 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
581 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
582#endif
583 " --png - write page images <pdf-name>.<page-number>.png\n"
584 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
585
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700586int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800587 std::vector<std::string> args(argv, argv + argc);
588 Options options;
589 std::list<std::string> files;
590 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800591 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700592 return 1;
593 }
594
Tom Sepez5ee12d72014-12-17 16:24:01 -0800595 v8::V8::InitializeICU();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800596 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
597 v8::V8::InitializePlatform(platform);
598 v8::V8::Initialize();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800599
600#ifdef V8_USE_EXTERNAL_STARTUP_DATA
601 v8::StartupData natives;
602 v8::StartupData snapshot;
603 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
604 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
605 return 1;
606 }
607 v8::V8::SetNativesDataBlob(&natives);
608 v8::V8::SetSnapshotDataBlob(&snapshot);
609#endif // V8_USE_EXTERNAL_STARTUP_DATA
610
John Abd-El-Malek207299b2014-12-15 12:13:45 -0800611 FPDF_InitLibrary();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700612
613 UNSUPPORT_INFO unsuppored_info;
614 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
615 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800616 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700617
618 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
619
620 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800621 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700622 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800623 size_t file_length = 0;
624 char* file_contents = GetFileContents(filename.c_str(), &file_length);
625 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800627 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800628 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700629 }
630
631 FPDF_DestroyLibrary();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800632 v8::V8::ShutdownPlatform();
633 delete platform;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700634
635 return 0;
636}