blob: 8ab94972a9ec79185670ea94d1297330cec61188 [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];
148 if (result) {
149 for (int h = 0; h < height; ++h) {
150 const char* src_line = buffer + (stride * h);
151 char* dest_line = result + (width * h * 3);
152 for (int w = 0; w < width; ++w) {
153 // R
154 dest_line[w * 3] = src_line[(w * 4) + 2];
155 // G
156 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
157 // B
158 dest_line[(w * 3) + 2] = src_line[w * 4];
159 }
160 }
161 fwrite(result, out_len, 1, fp);
162 delete [] result;
163 }
164 fclose(fp);
165}
166
Tom Sepezaf18cb32015-02-05 15:06:01 -0800167static void WritePng(const char* pdf_name, int num, const void* buffer_void,
168 int stride, int width, int height) {
169 if (!CheckDimensions(stride, width, height))
170 return;
171
172 std::vector<unsigned char> png_encoding;
173 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
174 if (!image_diff_png::EncodeBGRAPNG(
175 buffer, width, height, stride, false, &png_encoding)) {
176 fprintf(stderr, "Failed to convert bitmap to PNG\n");
177 return;
178 }
179
180 char filename[256];
181 int chars_formatted = snprintf(
182 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
183 if (chars_formatted < 0 ||
184 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
185 fprintf(stderr, "Filname %s is too long\n", filename);
186 return;
187 }
188
189 FILE* fp = fopen(filename, "wb");
190 if (!fp) {
191 fprintf(stderr, "Failed to open %s for output\n", filename);
192 return;
193 }
194
195 size_t bytes_written = fwrite(
196 &png_encoding.front(), 1, png_encoding.size(), fp);
197 if (bytes_written != png_encoding.size())
198 fprintf(stderr, "Failed to write to %s\n", filename);
199
200 (void) fclose(fp);
201}
202
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700203#ifdef _WIN32
204static void WriteBmp(const char* pdf_name, int num, const void* buffer,
205 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800206 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700207 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800208
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700209 int out_len = stride * height;
210 if (out_len > INT_MAX / 3)
211 return;
212
213 char filename[256];
214 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
215 FILE* fp = fopen(filename, "wb");
216 if (!fp)
217 return;
218
219 BITMAPINFO bmi = {0};
220 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
221 bmi.bmiHeader.biWidth = width;
222 bmi.bmiHeader.biHeight = -height; // top-down image
223 bmi.bmiHeader.biPlanes = 1;
224 bmi.bmiHeader.biBitCount = 32;
225 bmi.bmiHeader.biCompression = BI_RGB;
226 bmi.bmiHeader.biSizeImage = 0;
227
228 BITMAPFILEHEADER file_header = {0};
229 file_header.bfType = 0x4d42;
230 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
231 file_header.bfOffBits = file_header.bfSize - out_len;
232
233 fwrite(&file_header, sizeof(file_header), 1, fp);
234 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
235 fwrite(buffer, out_len, 1, fp);
236 fclose(fp);
237}
238
239void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
240 int width = static_cast<int>(FPDF_GetPageWidth(page));
241 int height = static_cast<int>(FPDF_GetPageHeight(page));
242
243 char filename[256];
244 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
245
246 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800247
248 HRGN rgn = CreateRectRgn(0, 0, width, height);
249 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700250 DeleteObject(rgn);
251
252 SelectObject(dc, GetStockObject(NULL_PEN));
253 SelectObject(dc, GetStockObject(WHITE_BRUSH));
254 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
255 Rectangle(dc, 0, 0, width + 1, height + 1);
256
257 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
258 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
259
260 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
261}
262#endif
263
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800264int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
265 int, int) {
Tom Sepezfb947282014-12-08 09:55:11 -0800266 // Deal with differences between UTF16LE and wchar_t on this platform.
267 size_t characters = 0;
268 while (msg[characters]) {
269 ++characters;
270 }
271 wchar_t* platform_string =
272 (wchar_t*)malloc((characters + 1) * sizeof(wchar_t));
273 for (size_t i = 0; i < characters + 1; ++i) {
274 unsigned char* ptr = (unsigned char*)&msg[i];
275 platform_string[i] = ptr[0] + 256 * ptr[1];
276 }
277 printf("Alert: %ls\n", platform_string);
278 free(platform_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279 return 0;
280}
281
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800282void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
283 printf("Goto Page: %d\n", pageNumber);
284}
285
286void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287 std::string feature = "Unknown";
288 switch (type) {
289 case FPDF_UNSP_DOC_XFAFORM:
290 feature = "XFA";
291 break;
292 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
293 feature = "Portfolios_Packages";
294 break;
295 case FPDF_UNSP_DOC_ATTACHMENT:
296 case FPDF_UNSP_ANNOT_ATTACHMENT:
297 feature = "Attachment";
298 break;
299 case FPDF_UNSP_DOC_SECURITY:
300 feature = "Rights_Management";
301 break;
302 case FPDF_UNSP_DOC_SHAREDREVIEW:
303 feature = "Shared_Review";
304 break;
305 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
306 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
307 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
308 feature = "Shared_Form";
309 break;
310 case FPDF_UNSP_ANNOT_3DANNOT:
311 feature = "3D";
312 break;
313 case FPDF_UNSP_ANNOT_MOVIE:
314 feature = "Movie";
315 break;
316 case FPDF_UNSP_ANNOT_SOUND:
317 feature = "Sound";
318 break;
319 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
320 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
321 feature = "Screen";
322 break;
323 case FPDF_UNSP_ANNOT_SIG:
324 feature = "Digital_Signature";
325 break;
326 }
327 printf("Unsupported feature: %s.\n", feature.c_str());
328}
329
Tom Sepez5ee12d72014-12-17 16:24:01 -0800330bool ParseCommandLine(const std::vector<std::string>& args,
331 Options* options, std::list<std::string>* files) {
332 if (args.empty()) {
333 return false;
334 }
335 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800336 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800337 for (; cur_idx < args.size(); ++cur_idx) {
338 const std::string& cur_arg = args[cur_idx];
339 if (cur_arg == "--ppm") {
340 if (options->output_format != OUTPUT_NONE) {
341 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
342 return false;
343 }
344 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800345 } else if (cur_arg == "--png") {
346 if (options->output_format != OUTPUT_NONE) {
347 fprintf(stderr, "Duplicate or conflicting --png argument\n");
348 return false;
349 }
350 options->output_format = OUTPUT_PNG;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800351 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700352#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800353 else if (cur_arg == "--emf") {
354 if (options->output_format != OUTPUT_NONE) {
355 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
356 return false;
357 }
358 options->output_format = OUTPUT_EMF;
359 }
360 else if (cur_arg == "--bmp") {
361 if (options->output_format != OUTPUT_NONE) {
362 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
363 return false;
364 }
365 options->output_format = OUTPUT_BMP;
366 }
367#endif // _WIN32
368#ifdef V8_USE_EXTERNAL_STARTUP_DATA
369 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
370 if (!options->bin_directory.empty()) {
371 fprintf(stderr, "Duplicate --bin-dir argument\n");
372 return false;
373 }
374 options->bin_directory = cur_arg.substr(10);
375 }
376#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezdaa2e842015-01-29 15:44:37 -0800377 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
378 if (!options->scale_factor_as_string.empty()) {
379 fprintf(stderr, "Duplicate --scale argument\n");
380 return false;
381 }
382 options->scale_factor_as_string = cur_arg.substr(8);
383 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700384 else
385 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800387 if (cur_idx >= args.size()) {
388 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700389 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800390 }
Bo Xud44e3922014-12-19 02:27:25 -0800391 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800392 files->push_back(args[i]);
393 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394 return true;
395}
396
397class TestLoader {
398 public:
399 TestLoader(const char* pBuf, size_t len);
400
401 const char* m_pBuf;
402 size_t m_Len;
403};
404
405TestLoader::TestLoader(const char* pBuf, size_t len)
406 : m_pBuf(pBuf), m_Len(len) {
407}
408
409int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
410 unsigned long size) {
411 TestLoader* pLoader = (TestLoader*) param;
412 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
413 memcpy(pBuf, pLoader->m_pBuf + pos, size);
414 return 1;
415}
416
417bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
418 return true;
419}
420
421void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
422}
423
Tom Sepez5ee12d72014-12-17 16:24:01 -0800424void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800425 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800426 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427
428 IPDF_JSPLATFORM platform_callbacks;
429 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
430 platform_callbacks.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800431 platform_callbacks.app_alert = ExampleAppAlert;
432 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433
434 FPDF_FORMFILLINFO form_callbacks;
435 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800436 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700437 form_callbacks.m_pJsPlatform = &platform_callbacks;
438
439 TestLoader loader(pBuf, len);
440
441 FPDF_FILEACCESS file_access;
442 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700443 file_access.m_FileLen = static_cast<unsigned long>(len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700444 file_access.m_GetBlock = Get_Block;
445 file_access.m_Param = &loader;
446
447 FX_FILEAVAIL file_avail;
448 memset(&file_avail, '\0', sizeof(file_avail));
449 file_avail.version = 1;
450 file_avail.IsDataAvail = Is_Data_Avail;
451
452 FX_DOWNLOADHINTS hints;
453 memset(&hints, '\0', sizeof(hints));
454 hints.version = 1;
455 hints.AddSegment = Add_Segment;
456
457 FPDF_DOCUMENT doc;
458 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
459
460 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
461
462 if (!FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800463 fprintf(stderr, "Non-linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464 doc = FPDF_LoadCustomDocument(&file_access, NULL);
465 } else {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800466 fprintf(stderr, "Linearized path...\n");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
468 }
469
JUN FANG827a1722015-03-05 13:39:21 -0800470 if (!doc)
471 {
472 fprintf(stderr, "Load pdf docs unsuccessful.\n");
473 return;
474 }
475
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700476 (void) FPDF_GetDocPermissions(doc);
477 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
478
Bo Xu2b7a49d2014-11-14 17:40:50 -0800479 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800480 int docType = DOCTYPE_PDF;
481 if (FPDF_HasXFAField(doc, docType))
482 {
483 if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc))
484 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800485 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700486 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
487 FPDF_SetFormFieldHighlightAlpha(form, 100);
488
489 int first_page = FPDFAvail_GetFirstPageNum(doc);
490 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
491
492 int page_count = FPDF_GetPageCount(doc);
493 for (int i = 0; i < page_count; ++i) {
494 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
495 }
496
497 FORM_DoDocumentJSAction(form);
498 FORM_DoDocumentOpenAction(form);
499
Tom Sepez1ed8a212015-05-11 15:25:39 -0700500 int rendered_pages = 0;
501 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700502 for (int i = 0; i < page_count; ++i) {
503 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700504 if (!page) {
505 bad_pages ++;
506 continue;
507 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
509 FORM_OnAfterLoadPage(page, form);
510 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
511
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800512 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800513 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800514 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800515 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800516 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
517 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800518
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700520 if (!bitmap) {
521 fprintf(stderr, "Page was too large to be rendered.\n");
522 bad_pages++;
523 continue;
524 }
525
Lei Zhang532a6a72014-07-09 11:47:15 -0700526 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700527 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangaeacba42014-08-22 17:04:29 -0700528 rendered_pages ++;
529
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700530 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700531 int stride = FPDFBitmap_GetStride(bitmap);
532 const char* buffer =
533 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
534
Tom Sepezdaa2e842015-01-29 15:44:37 -0800535 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700536#ifdef _WIN32
537 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800538 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700539 break;
540
541 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800542 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700543 break;
544#endif
545 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800546 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700547 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800548
549 case OUTPUT_PNG:
550 WritePng(name.c_str(), i, buffer, stride, width, height);
551 break;
552
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700553 default:
554 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555 }
556
557 FPDFBitmap_Destroy(bitmap);
558
559 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
560 FORM_OnBeforeClosePage(page, form);
561 FPDFText_ClosePage(text_page);
562 FPDF_ClosePage(page);
563 }
564
565 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700566 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800567 FPDFDOC_ExitFormFillEnvironment(form);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700568 FPDFAvail_Destroy(pdf_avail);
569
Tom Sepez1ed8a212015-05-11 15:25:39 -0700570 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
571 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700572}
573
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800574static const char usage_string[] =
575 "Usage: pdfium_test [OPTION] [FILE]...\n"
576 " --bin-dir=<path> - override path to v8 external data\n"
577 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
578#ifdef _WIN32
579 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
580 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
581#endif
582 " --png - write page images <pdf-name>.<page-number>.png\n"
583 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
584
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700585int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800586 std::vector<std::string> args(argv, argv + argc);
587 Options options;
588 std::list<std::string> files;
589 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800590 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700591 return 1;
592 }
593
Tom Sepez5ee12d72014-12-17 16:24:01 -0800594 v8::V8::InitializeICU();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800595 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
596 v8::V8::InitializePlatform(platform);
597 v8::V8::Initialize();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800598
599#ifdef V8_USE_EXTERNAL_STARTUP_DATA
600 v8::StartupData natives;
601 v8::StartupData snapshot;
602 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
603 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
604 return 1;
605 }
606 v8::V8::SetNativesDataBlob(&natives);
607 v8::V8::SetSnapshotDataBlob(&snapshot);
608#endif // V8_USE_EXTERNAL_STARTUP_DATA
609
John Abd-El-Malek207299b2014-12-15 12:13:45 -0800610 FPDF_InitLibrary();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700611
612 UNSUPPORT_INFO unsuppored_info;
613 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
614 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800615 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700616
617 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
618
619 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800620 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700621 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800622 size_t file_length = 0;
623 char* file_contents = GetFileContents(filename.c_str(), &file_length);
624 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700625 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800626 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800627 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628 }
629
630 FPDF_DestroyLibrary();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800631 v8::V8::ShutdownPlatform();
632 delete platform;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700633
634 return 0;
635}