blob: 056ba0b6db23a9940f522ef582420d903bb2c1ef [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;
Lei Zhang6f62d532015-09-23 15:31:44 -070051 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080052};
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);
Lei Zhang5377ebf2015-09-23 14:52:53 -070059 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080060 }
Lei Zhang5377ebf2015-09-23 14:52:53 -070061 (void)fseek(file, 0, SEEK_END);
Tom Sepez5ee12d72014-12-17 16:24:01 -080062 size_t file_length = ftell(file);
63 if (!file_length) {
Lei Zhang5377ebf2015-09-23 14:52:53 -070064 (void)fclose(file);
65 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080066 }
Lei Zhang5377ebf2015-09-23 14:52:53 -070067 (void)fseek(file, 0, SEEK_SET);
68 char* buffer = static_cast<char*>(malloc(file_length));
Tom Sepez5ee12d72014-12-17 16:24:01 -080069 if (!buffer) {
Lei Zhang5377ebf2015-09-23 14:52:53 -070070 (void)fclose(file);
71 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080072 }
73 size_t bytes_read = fread(buffer, 1, file_length, file);
Lei Zhang5377ebf2015-09-23 14:52:53 -070074 (void)fclose(file);
Tom Sepez5ee12d72014-12-17 16:24:01 -080075 if (bytes_read != file_length) {
76 fprintf(stderr, "Failed to read: %s\n", filename);
77 free(buffer);
Lei Zhang5377ebf2015-09-23 14:52:53 -070078 return nullptr;
Tom Sepez5ee12d72014-12-17 16:24:01 -080079 }
80 *retlen = bytes_read;
81 return buffer;
82}
83
84#ifdef V8_USE_EXTERNAL_STARTUP_DATA
85// Returns the full path for an external V8 data file based on either
86// the currect exectuable path or an explicit override.
87static std::string GetFullPathForSnapshotFile(const Options& options,
88 const std::string& filename) {
89 std::string result;
90 if (!options.bin_directory.empty()) {
91 result = options.bin_directory;
92 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
93 result += PATH_SEPARATOR;
94 }
95 } else if (!options.exe_path.empty()) {
96 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
97 if (last_separator != std::string::npos) {
98 result = options.exe_path.substr(0, last_separator + 1);
99 }
100 }
101 result += filename;
102 return result;
103}
104
105// Reads an extenal V8 data file from the |options|-indicated location,
106// returing true on success and false on error.
107static bool GetExternalData(const Options& options,
108 const std::string& bin_filename,
109 v8::StartupData* result_data) {
110 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
111 size_t data_length = 0;
112 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
113 if (!data_buffer) {
114 return false;
115 }
116 result_data->data = const_cast<const char*>(data_buffer);
117 result_data->raw_size = data_length;
118 return true;
119}
120#endif // V8_USE_EXTERNAL_STARTUP_DATA
121
Tom Sepezaf18cb32015-02-05 15:06:01 -0800122static bool CheckDimensions(int stride, int width, int height) {
123 if (stride < 0 || width < 0 || height < 0)
124 return false;
125 if (height > 0 && width > INT_MAX / height)
126 return false;
127 return true;
128}
129
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700130static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
131 int stride, int width, int height) {
132 const char* buffer = reinterpret_cast<const char*>(buffer_void);
133
Tom Sepezaf18cb32015-02-05 15:06:01 -0800134 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800136
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137 int out_len = width * height;
138 if (out_len > INT_MAX / 3)
139 return;
140 out_len *= 3;
141
142 char filename[256];
143 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700144 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145 if (!fp)
146 return;
147 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
148 // Source data is B, G, R, unused.
149 // Dest data is R, G, B.
150 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -0700151 for (int h = 0; h < height; ++h) {
152 const char* src_line = buffer + (stride * h);
153 char* dest_line = result + (width * h * 3);
154 for (int w = 0; w < width; ++w) {
155 // R
156 dest_line[w * 3] = src_line[(w * 4) + 2];
157 // G
158 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
159 // B
160 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162 }
Lei Zhange00660b2015-08-13 15:40:18 -0700163 fwrite(result, out_len, 1, fp);
164 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165 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
Lei Zhang5377ebf2015-09-23 14:52:53 -0700201 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800202}
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
Nico Weber2827bdd2015-07-01 14:08:08 -0700220 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700221 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
Nico Weber2827bdd2015-07-01 14:08:08 -0700229 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700230 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
Lei Zhang5377ebf2015-09-23 14:52:53 -0700247 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
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 =
Lei Zhang5377ebf2015-09-23 14:52:53 -0700273 static_cast<wchar_t*>(malloc((characters + 1) * sizeof(wchar_t)));
Tom Sepezfb947282014-12-08 09:55:11 -0800274 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;
Lei Zhang6f62d532015-09-23 15:31:44 -0700352 } else if (cur_arg.size() > 11 &&
353 cur_arg.compare(0, 11, "--font-dir=") == 0) {
354 if (!options->font_directory.empty()) {
355 fprintf(stderr, "Duplicate --font-dir argument\n");
356 return false;
357 }
358 options->font_directory = cur_arg.substr(11);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800359 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700360#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800361 else if (cur_arg == "--emf") {
362 if (options->output_format != OUTPUT_NONE) {
363 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
364 return false;
365 }
366 options->output_format = OUTPUT_EMF;
367 }
368 else if (cur_arg == "--bmp") {
369 if (options->output_format != OUTPUT_NONE) {
370 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
371 return false;
372 }
373 options->output_format = OUTPUT_BMP;
374 }
375#endif // _WIN32
376#ifdef V8_USE_EXTERNAL_STARTUP_DATA
377 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
378 if (!options->bin_directory.empty()) {
379 fprintf(stderr, "Duplicate --bin-dir argument\n");
380 return false;
381 }
382 options->bin_directory = cur_arg.substr(10);
383 }
384#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezdaa2e842015-01-29 15:44:37 -0800385 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
386 if (!options->scale_factor_as_string.empty()) {
387 fprintf(stderr, "Duplicate --scale argument\n");
388 return false;
389 }
390 options->scale_factor_as_string = cur_arg.substr(8);
391 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700392 else
393 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800395 if (cur_idx >= args.size()) {
396 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700397 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800398 }
Bo Xud44e3922014-12-19 02:27:25 -0800399 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800400 files->push_back(args[i]);
401 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402 return true;
403}
404
405class TestLoader {
406 public:
407 TestLoader(const char* pBuf, size_t len);
408
409 const char* m_pBuf;
410 size_t m_Len;
411};
412
413TestLoader::TestLoader(const char* pBuf, size_t len)
414 : m_pBuf(pBuf), m_Len(len) {
415}
416
Lei Zhang5377ebf2015-09-23 14:52:53 -0700417int GetBlock(void* param,
418 unsigned long pos,
419 unsigned char* pBuf,
420 unsigned long size) {
421 TestLoader* pLoader = static_cast<TestLoader*>(param);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700422 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
423 memcpy(pBuf, pLoader->m_pBuf + pos, size);
424 return 1;
425}
426
Tom Sepezcf22eb82015-05-12 17:28:08 -0700427FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428 return true;
429}
430
431void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
432}
433
Tom Sepez5ee12d72014-12-17 16:24:01 -0800434void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800435 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800436 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700437
438 IPDF_JSPLATFORM platform_callbacks;
439 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Jochen Eisinger06b60022015-07-30 17:44:35 +0200440 platform_callbacks.version = 2;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800441 platform_callbacks.app_alert = ExampleAppAlert;
442 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700443
444 FPDF_FORMFILLINFO form_callbacks;
445 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800446 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447 form_callbacks.m_pJsPlatform = &platform_callbacks;
448
449 TestLoader loader(pBuf, len);
450
451 FPDF_FILEACCESS file_access;
452 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700453 file_access.m_FileLen = static_cast<unsigned long>(len);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700454 file_access.m_GetBlock = GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455 file_access.m_Param = &loader;
456
457 FX_FILEAVAIL file_avail;
458 memset(&file_avail, '\0', sizeof(file_avail));
459 file_avail.version = 1;
460 file_avail.IsDataAvail = Is_Data_Avail;
461
462 FX_DOWNLOADHINTS hints;
463 memset(&hints, '\0', sizeof(hints));
464 hints.version = 1;
465 hints.AddSegment = Add_Segment;
466
467 FPDF_DOCUMENT doc;
468 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
469
Lei Zhang5377ebf2015-09-23 14:52:53 -0700470 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700471
Lei Zhang600d4072015-09-23 15:35:25 -0700472 if (FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800473 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700474 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Lei Zhang600d4072015-09-23 15:35:25 -0700475 } else {
476 fprintf(stderr, "Non-linearized path...\n");
477 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700478 }
479
Lei Zhang5377ebf2015-09-23 14:52:53 -0700480 if (!doc) {
JUN FANG827a1722015-03-05 13:39:21 -0800481 fprintf(stderr, "Load pdf docs unsuccessful.\n");
482 return;
483 }
484
Lei Zhang5377ebf2015-09-23 14:52:53 -0700485 (void)FPDF_GetDocPermissions(doc);
486 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700487
Bo Xu2b7a49d2014-11-14 17:40:50 -0800488 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800489 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700490 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
491 !FPDF_LoadXFA(doc)) {
492 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800493 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700494 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
495 FPDF_SetFormFieldHighlightAlpha(form, 100);
496
497 int first_page = FPDFAvail_GetFirstPageNum(doc);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700498 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700499
500 int page_count = FPDF_GetPageCount(doc);
501 for (int i = 0; i < page_count; ++i) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700502 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503 }
504
505 FORM_DoDocumentJSAction(form);
506 FORM_DoDocumentOpenAction(form);
507
Tom Sepez1ed8a212015-05-11 15:25:39 -0700508 int rendered_pages = 0;
509 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700510 for (int i = 0; i < page_count; ++i) {
511 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700512 if (!page) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700513 ++bad_pages;
514 continue;
Jun Fangaeacba42014-08-22 17:04:29 -0700515 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700516 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
517 FORM_OnAfterLoadPage(page, form);
518 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
519
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800520 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800521 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800522 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800523 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800524 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
525 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800526
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700527 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700528 if (!bitmap) {
529 fprintf(stderr, "Page was too large to be rendered.\n");
530 bad_pages++;
531 continue;
532 }
533
Lei Zhang532a6a72014-07-09 11:47:15 -0700534 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700535 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700536 ++rendered_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700537
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700539 int stride = FPDFBitmap_GetStride(bitmap);
540 const char* buffer =
541 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
542
Tom Sepezdaa2e842015-01-29 15:44:37 -0800543 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700544#ifdef _WIN32
545 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800546 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700547 break;
548
549 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800550 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700551 break;
552#endif
553 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800554 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700555 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800556
557 case OUTPUT_PNG:
558 WritePng(name.c_str(), i, buffer, stride, width, height);
559 break;
560
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700561 default:
562 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700563 }
564
565 FPDFBitmap_Destroy(bitmap);
566
567 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
568 FORM_OnBeforeClosePage(page, form);
569 FPDFText_ClosePage(text_page);
570 FPDF_ClosePage(page);
571 }
572
573 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700574
575 // Note: The shut down order here is the reverse of the non-XFA branch order.
576 // Need to work out if this is required, and if it is, the lifetimes of
577 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700578 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800579 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700580
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700581 FPDFAvail_Destroy(pdf_avail);
582
Tom Sepez1ed8a212015-05-11 15:25:39 -0700583 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
584 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700585}
586
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800587static const char usage_string[] =
588 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700589 " --bin-dir=<path> - override path to v8 external data\n"
590 " --font-dir=<path> - override path to external fonts\n"
591 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800592#ifdef _WIN32
593 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
594 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
595#endif
596 " --png - write page images <pdf-name>.<page-number>.png\n"
597 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
598
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700599int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800600 std::vector<std::string> args(argv, argv + argc);
601 Options options;
602 std::list<std::string> files;
603 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800604 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700605 return 1;
606 }
607
Tom Sepez5ee12d72014-12-17 16:24:01 -0800608 v8::V8::InitializeICU();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800609 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
610 v8::V8::InitializePlatform(platform);
611 v8::V8::Initialize();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800612
613#ifdef V8_USE_EXTERNAL_STARTUP_DATA
614 v8::StartupData natives;
615 v8::StartupData snapshot;
616 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
617 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
618 return 1;
619 }
620 v8::V8::SetNativesDataBlob(&natives);
621 v8::V8::SetSnapshotDataBlob(&snapshot);
622#endif // V8_USE_EXTERNAL_STARTUP_DATA
623
Lei Zhang600d4072015-09-23 15:35:25 -0700624 if (options.font_directory.empty()) {
625 FPDF_InitLibrary();
626 } else {
Lei Zhang6f62d532015-09-23 15:31:44 -0700627 const char* path_array[2];
628 path_array[0] = options.font_directory.c_str();
629 path_array[1] = nullptr;
630 FPDF_LIBRARY_CONFIG config;
631 config.version = 1;
632 config.m_pUserFontPaths = path_array;
633 FPDF_InitLibraryWithConfig(&config);
Lei Zhang6f62d532015-09-23 15:31:44 -0700634 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635
636 UNSUPPORT_INFO unsuppored_info;
637 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
638 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800639 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700640
641 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
642
643 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800644 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800646 size_t file_length = 0;
647 char* file_contents = GetFileContents(filename.c_str(), &file_length);
648 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700649 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800650 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800651 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700652 }
653
654 FPDF_DestroyLibrary();
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800655 v8::V8::ShutdownPlatform();
656 delete platform;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700657
658 return 0;
659}