blob: f060eb018bfc1640330da2e03902ca4c78b1ff58 [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);
Lei Zhang5377ebf2015-09-23 14:52:53 -070058 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080059 }
Lei Zhang5377ebf2015-09-23 14:52:53 -070060 (void)fseek(file, 0, SEEK_END);
Tom Sepez5ee12d72014-12-17 16:24:01 -080061 size_t file_length = ftell(file);
62 if (!file_length) {
Lei Zhang5377ebf2015-09-23 14:52:53 -070063 (void)fclose(file);
64 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080065 }
Lei Zhang5377ebf2015-09-23 14:52:53 -070066 (void)fseek(file, 0, SEEK_SET);
67 char* buffer = static_cast<char*>(malloc(file_length));
Tom Sepez5ee12d72014-12-17 16:24:01 -080068 if (!buffer) {
Lei Zhang5377ebf2015-09-23 14:52:53 -070069 (void)fclose(file);
70 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080071 }
72 size_t bytes_read = fread(buffer, 1, file_length, file);
Lei Zhang5377ebf2015-09-23 14:52:53 -070073 (void)fclose(file);
Tom Sepez5ee12d72014-12-17 16:24:01 -080074 if (bytes_read != file_length) {
75 fprintf(stderr, "Failed to read: %s\n", filename);
76 free(buffer);
Lei Zhang5377ebf2015-09-23 14:52:53 -070077 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080078 }
79 *retlen = bytes_read;
80 return buffer;
81}
82
83#ifdef V8_USE_EXTERNAL_STARTUP_DATA
84// Returns the full path for an external V8 data file based on either
85// the currect exectuable path or an explicit override.
86static std::string GetFullPathForSnapshotFile(const Options& options,
87 const std::string& filename) {
88 std::string result;
89 if (!options.bin_directory.empty()) {
90 result = options.bin_directory;
91 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
92 result += PATH_SEPARATOR;
93 }
94 } else if (!options.exe_path.empty()) {
95 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
96 if (last_separator != std::string::npos) {
97 result = options.exe_path.substr(0, last_separator + 1);
98 }
99 }
100 result += filename;
101 return result;
102}
103
104// Reads an extenal V8 data file from the |options|-indicated location,
105// returing true on success and false on error.
106static bool GetExternalData(const Options& options,
107 const std::string& bin_filename,
108 v8::StartupData* result_data) {
109 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
110 size_t data_length = 0;
111 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
112 if (!data_buffer) {
113 return false;
114 }
115 result_data->data = const_cast<const char*>(data_buffer);
116 result_data->raw_size = data_length;
117 return true;
118}
119#endif // V8_USE_EXTERNAL_STARTUP_DATA
120
Tom Sepezaf18cb32015-02-05 15:06:01 -0800121static bool CheckDimensions(int stride, int width, int height) {
122 if (stride < 0 || width < 0 || height < 0)
123 return false;
124 if (height > 0 && width > INT_MAX / height)
125 return false;
126 return true;
127}
128
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700129static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
130 int stride, int width, int height) {
131 const char* buffer = reinterpret_cast<const char*>(buffer_void);
132
Tom Sepezaf18cb32015-02-05 15:06:01 -0800133 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800135
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136 int out_len = width * height;
137 if (out_len > INT_MAX / 3)
138 return;
139 out_len *= 3;
140
141 char filename[256];
142 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700143 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144 if (!fp)
145 return;
146 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
147 // Source data is B, G, R, unused.
148 // Dest data is R, G, B.
149 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -0700150 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];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161 }
Lei Zhange00660b2015-08-13 15:40:18 -0700162 fwrite(result, out_len, 1, fp);
163 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164 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
Lei Zhang5377ebf2015-09-23 14:52:53 -0700200 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800201}
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
Nico Weber2827bdd2015-07-01 14:08:08 -0700219 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700220 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
Nico Weber2827bdd2015-07-01 14:08:08 -0700228 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700229 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
Lei Zhang5377ebf2015-09-23 14:52:53 -0700246 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
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 =
Lei Zhang5377ebf2015-09-23 14:52:53 -0700272 static_cast<wchar_t*>(malloc((characters + 1) * sizeof(wchar_t)));
Tom Sepezfb947282014-12-08 09:55:11 -0800273 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
Lei Zhang5377ebf2015-09-23 14:52:53 -0700409int GetBlock(void* param,
410 unsigned long pos,
411 unsigned char* pBuf,
412 unsigned long size) {
413 TestLoader* pLoader = static_cast<TestLoader*>(param);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700414 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
415 memcpy(pBuf, pLoader->m_pBuf + pos, size);
416 return 1;
417}
418
Tom Sepezcf22eb82015-05-12 17:28:08 -0700419FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420 return true;
421}
422
423void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
424}
425
Tom Sepez5ee12d72014-12-17 16:24:01 -0800426void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800427 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800428 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429
430 IPDF_JSPLATFORM platform_callbacks;
431 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200432 platform_callbacks.version = 2;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800433 platform_callbacks.app_alert = ExampleAppAlert;
434 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
436 FPDF_FORMFILLINFO form_callbacks;
437 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800438 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439 form_callbacks.m_pJsPlatform = &platform_callbacks;
440
441 TestLoader loader(pBuf, len);
442
443 FPDF_FILEACCESS file_access;
444 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700445 file_access.m_FileLen = static_cast<unsigned long>(len);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700446 file_access.m_GetBlock = GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447 file_access.m_Param = &loader;
448
449 FX_FILEAVAIL file_avail;
450 memset(&file_avail, '\0', sizeof(file_avail));
451 file_avail.version = 1;
452 file_avail.IsDataAvail = Is_Data_Avail;
453
454 FX_DOWNLOADHINTS hints;
455 memset(&hints, '\0', sizeof(hints));
456 hints.version = 1;
457 hints.AddSegment = Add_Segment;
458
459 FPDF_DOCUMENT doc;
460 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
461
Lei Zhang5377ebf2015-09-23 14:52:53 -0700462 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463
464 if (!FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800465 fprintf(stderr, "Non-linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700466 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700467 } else {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800468 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700469 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700470 }
471
Lei Zhang5377ebf2015-09-23 14:52:53 -0700472 if (!doc) {
JUN FANG827a1722015-03-05 13:39:21 -0800473 fprintf(stderr, "Load pdf docs unsuccessful.\n");
474 return;
475 }
476
Lei Zhang5377ebf2015-09-23 14:52:53 -0700477 (void)FPDF_GetDocPermissions(doc);
478 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700479
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;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700482 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
483 !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);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700490 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700491
492 int page_count = FPDF_GetPageCount(doc);
493 for (int i = 0; i < page_count; ++i) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700494 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700495 }
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) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700505 ++bad_pages;
506 continue;
Jun Fangaeacba42014-08-22 17:04:29 -0700507 }
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);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700528 ++rendered_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700529
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);
Lei Zhangba026912015-07-16 10:06:11 -0700566
567 // Note: The shut down order here is the reverse of the non-XFA branch order.
568 // Need to work out if this is required, and if it is, the lifetimes of
569 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800571 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700572
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700573 FPDFAvail_Destroy(pdf_avail);
574
Tom Sepez1ed8a212015-05-11 15:25:39 -0700575 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
576 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700577}
578
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800579static const char usage_string[] =
580 "Usage: pdfium_test [OPTION] [FILE]...\n"
581 " --bin-dir=<path> - override path to v8 external data\n"
582 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
583#ifdef _WIN32
584 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
585 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
586#endif
587 " --png - write page images <pdf-name>.<page-number>.png\n"
588 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
589
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700590int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800591 std::vector<std::string> args(argv, argv + argc);
592 Options options;
593 std::list<std::string> files;
594 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800595 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700596 return 1;
597 }
598
Tom Sepez5ee12d72014-12-17 16:24:01 -0800599 v8::V8::InitializeICU();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800600 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
601 v8::V8::InitializePlatform(platform);
602 v8::V8::Initialize();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800603
604#ifdef V8_USE_EXTERNAL_STARTUP_DATA
605 v8::StartupData natives;
606 v8::StartupData snapshot;
607 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
608 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
609 return 1;
610 }
611 v8::V8::SetNativesDataBlob(&natives);
612 v8::V8::SetSnapshotDataBlob(&snapshot);
613#endif // V8_USE_EXTERNAL_STARTUP_DATA
614
John Abd-El-Malek207299b2014-12-15 12:13:45 -0800615 FPDF_InitLibrary();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700616
617 UNSUPPORT_INFO unsuppored_info;
618 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
619 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800620 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700621
622 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
623
624 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800625 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800627 size_t file_length = 0;
628 char* file_contents = GetFileContents(filename.c_str(), &file_length);
629 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800631 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800632 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700633 }
634
635 FPDF_DestroyLibrary();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800636 v8::V8::ShutdownPlatform();
637 delete platform;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700638
639 return 0;
640}