blob: 86c7afd529501f93946a774c4fc3b2d1945a9afc [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>
9
10#include <list>
Tom Sepezdaa2e842015-01-29 15:44:37 -080011#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012#include <string>
13#include <utility>
Tom Sepez5ee12d72014-12-17 16:24:01 -080014#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Tom Sepezaf18cb32015-02-05 15:06:01 -080016#include "image_diff_png.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#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"
Lei Zhangbde53d22015-11-12 22:21:30 -080022#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070023
Tom Sepez452b4f32015-10-13 09:27:27 -070024#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080025#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070026#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080027#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
29#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080030#define snprintf _snprintf
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031#endif
32
Vitaly Buka9e0177a2014-07-22 18:15:42 -070033enum OutputFormat {
34 OUTPUT_NONE,
35 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080036 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070037#ifdef _WIN32
38 OUTPUT_BMP,
39 OUTPUT_EMF,
40#endif
41};
42
Tom Sepez5ee12d72014-12-17 16:24:01 -080043struct Options {
44 Options() : output_format(OUTPUT_NONE) { }
45
46 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080047 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080048 std::string exe_path;
49 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070050 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080051};
52
Tom Sepezaf18cb32015-02-05 15:06:01 -080053static bool CheckDimensions(int stride, int width, int height) {
54 if (stride < 0 || width < 0 || height < 0)
55 return false;
56 if (height > 0 && width > INT_MAX / height)
57 return false;
58 return true;
59}
60
Vitaly Buka9e0177a2014-07-22 18:15:42 -070061static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
62 int stride, int width, int height) {
63 const char* buffer = reinterpret_cast<const char*>(buffer_void);
64
Tom Sepezaf18cb32015-02-05 15:06:01 -080065 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080067
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068 int out_len = width * height;
69 if (out_len > INT_MAX / 3)
70 return;
71 out_len *= 3;
72
73 char filename[256];
74 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070075 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076 if (!fp)
77 return;
78 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
79 // Source data is B, G, R, unused.
80 // Dest data is R, G, B.
81 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -070082 for (int h = 0; h < height; ++h) {
83 const char* src_line = buffer + (stride * h);
84 char* dest_line = result + (width * h * 3);
85 for (int w = 0; w < width; ++w) {
86 // R
87 dest_line[w * 3] = src_line[(w * 4) + 2];
88 // G
89 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
90 // B
91 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093 }
Lei Zhange00660b2015-08-13 15:40:18 -070094 fwrite(result, out_len, 1, fp);
95 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096 fclose(fp);
97}
98
Tom Sepezaf18cb32015-02-05 15:06:01 -080099static void WritePng(const char* pdf_name, int num, const void* buffer_void,
100 int stride, int width, int height) {
101 if (!CheckDimensions(stride, width, height))
102 return;
103
104 std::vector<unsigned char> png_encoding;
105 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
106 if (!image_diff_png::EncodeBGRAPNG(
107 buffer, width, height, stride, false, &png_encoding)) {
108 fprintf(stderr, "Failed to convert bitmap to PNG\n");
109 return;
110 }
111
112 char filename[256];
113 int chars_formatted = snprintf(
114 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
115 if (chars_formatted < 0 ||
116 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
117 fprintf(stderr, "Filname %s is too long\n", filename);
118 return;
119 }
120
121 FILE* fp = fopen(filename, "wb");
122 if (!fp) {
123 fprintf(stderr, "Failed to open %s for output\n", filename);
124 return;
125 }
126
127 size_t bytes_written = fwrite(
128 &png_encoding.front(), 1, png_encoding.size(), fp);
129 if (bytes_written != png_encoding.size())
130 fprintf(stderr, "Failed to write to %s\n", filename);
131
Lei Zhang5377ebf2015-09-23 14:52:53 -0700132 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800133}
134
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700135#ifdef _WIN32
136static void WriteBmp(const char* pdf_name, int num, const void* buffer,
137 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800138 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700139 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800140
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700141 int out_len = stride * height;
142 if (out_len > INT_MAX / 3)
143 return;
144
145 char filename[256];
146 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
147 FILE* fp = fopen(filename, "wb");
148 if (!fp)
149 return;
150
Nico Weber2827bdd2015-07-01 14:08:08 -0700151 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700152 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
153 bmi.bmiHeader.biWidth = width;
154 bmi.bmiHeader.biHeight = -height; // top-down image
155 bmi.bmiHeader.biPlanes = 1;
156 bmi.bmiHeader.biBitCount = 32;
157 bmi.bmiHeader.biCompression = BI_RGB;
158 bmi.bmiHeader.biSizeImage = 0;
159
Nico Weber2827bdd2015-07-01 14:08:08 -0700160 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700161 file_header.bfType = 0x4d42;
162 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
163 file_header.bfOffBits = file_header.bfSize - out_len;
164
165 fwrite(&file_header, sizeof(file_header), 1, fp);
166 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
167 fwrite(buffer, out_len, 1, fp);
168 fclose(fp);
169}
170
171void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
172 int width = static_cast<int>(FPDF_GetPageWidth(page));
173 int height = static_cast<int>(FPDF_GetPageHeight(page));
174
175 char filename[256];
176 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
177
Lei Zhang5377ebf2015-09-23 14:52:53 -0700178 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800179
180 HRGN rgn = CreateRectRgn(0, 0, width, height);
181 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700182 DeleteObject(rgn);
183
184 SelectObject(dc, GetStockObject(NULL_PEN));
185 SelectObject(dc, GetStockObject(WHITE_BRUSH));
186 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
187 Rectangle(dc, 0, 0, width + 1, height + 1);
188
189 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
190 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
191
192 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
193}
194#endif
195
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800196int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
197 int, int) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800198 std::wstring platform_string = GetWideString(msg);
199 printf("Alert: %ls\n", platform_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200 return 0;
201}
202
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800203void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
204 printf("Goto Page: %d\n", pageNumber);
205}
206
207void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208 std::string feature = "Unknown";
209 switch (type) {
210 case FPDF_UNSP_DOC_XFAFORM:
211 feature = "XFA";
212 break;
213 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
214 feature = "Portfolios_Packages";
215 break;
216 case FPDF_UNSP_DOC_ATTACHMENT:
217 case FPDF_UNSP_ANNOT_ATTACHMENT:
218 feature = "Attachment";
219 break;
220 case FPDF_UNSP_DOC_SECURITY:
221 feature = "Rights_Management";
222 break;
223 case FPDF_UNSP_DOC_SHAREDREVIEW:
224 feature = "Shared_Review";
225 break;
226 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
227 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
228 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
229 feature = "Shared_Form";
230 break;
231 case FPDF_UNSP_ANNOT_3DANNOT:
232 feature = "3D";
233 break;
234 case FPDF_UNSP_ANNOT_MOVIE:
235 feature = "Movie";
236 break;
237 case FPDF_UNSP_ANNOT_SOUND:
238 feature = "Sound";
239 break;
240 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
241 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
242 feature = "Screen";
243 break;
244 case FPDF_UNSP_ANNOT_SIG:
245 feature = "Digital_Signature";
246 break;
247 }
248 printf("Unsupported feature: %s.\n", feature.c_str());
249}
250
Tom Sepez5ee12d72014-12-17 16:24:01 -0800251bool ParseCommandLine(const std::vector<std::string>& args,
252 Options* options, std::list<std::string>* files) {
253 if (args.empty()) {
254 return false;
255 }
256 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800257 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800258 for (; cur_idx < args.size(); ++cur_idx) {
259 const std::string& cur_arg = args[cur_idx];
260 if (cur_arg == "--ppm") {
261 if (options->output_format != OUTPUT_NONE) {
262 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
263 return false;
264 }
265 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800266 } else if (cur_arg == "--png") {
267 if (options->output_format != OUTPUT_NONE) {
268 fprintf(stderr, "Duplicate or conflicting --png argument\n");
269 return false;
270 }
271 options->output_format = OUTPUT_PNG;
Lei Zhang6f62d532015-09-23 15:31:44 -0700272 } else if (cur_arg.size() > 11 &&
273 cur_arg.compare(0, 11, "--font-dir=") == 0) {
274 if (!options->font_directory.empty()) {
275 fprintf(stderr, "Duplicate --font-dir argument\n");
276 return false;
277 }
278 options->font_directory = cur_arg.substr(11);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800279 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700280#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800281 else if (cur_arg == "--emf") {
282 if (options->output_format != OUTPUT_NONE) {
283 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
284 return false;
285 }
286 options->output_format = OUTPUT_EMF;
287 }
288 else if (cur_arg == "--bmp") {
289 if (options->output_format != OUTPUT_NONE) {
290 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
291 return false;
292 }
293 options->output_format = OUTPUT_BMP;
294 }
295#endif // _WIN32
Tom Sepez452b4f32015-10-13 09:27:27 -0700296#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800297#ifdef V8_USE_EXTERNAL_STARTUP_DATA
298 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
299 if (!options->bin_directory.empty()) {
300 fprintf(stderr, "Duplicate --bin-dir argument\n");
301 return false;
302 }
303 options->bin_directory = cur_arg.substr(10);
304 }
305#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700306#endif // PDF_ENABLE_V8
Tom Sepezdaa2e842015-01-29 15:44:37 -0800307 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
308 if (!options->scale_factor_as_string.empty()) {
309 fprintf(stderr, "Duplicate --scale argument\n");
310 return false;
311 }
312 options->scale_factor_as_string = cur_arg.substr(8);
313 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700314 else
315 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700316 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800317 if (cur_idx >= args.size()) {
318 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700319 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800320 }
Bo Xud44e3922014-12-19 02:27:25 -0800321 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800322 files->push_back(args[i]);
323 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324 return true;
325}
326
Tom Sepezcf22eb82015-05-12 17:28:08 -0700327FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328 return true;
329}
330
331void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
332}
333
Jun Fangb553bcb2015-11-10 18:49:04 +0800334bool RenderPage(const std::string& name,
335 const FPDF_DOCUMENT& doc,
336 const FPDF_FORMHANDLE& form,
337 const int page_index,
338 const Options& options) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800339 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
340 if (!page) {
Jun Fangb553bcb2015-11-10 18:49:04 +0800341 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800342 }
343 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
344 FORM_OnAfterLoadPage(page, form);
345 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
346
347 double scale = 1.0;
348 if (!options.scale_factor_as_string.empty()) {
349 std::stringstream(options.scale_factor_as_string) >> scale;
350 }
351 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
352 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
353
354 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
355 if (!bitmap) {
356 fprintf(stderr, "Page was too large to be rendered.\n");
Jun Fangb553bcb2015-11-10 18:49:04 +0800357 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800358 }
359
360 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
361 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
362
363 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
364 int stride = FPDFBitmap_GetStride(bitmap);
365 const char* buffer =
366 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
367
368 switch (options.output_format) {
369#ifdef _WIN32
370 case OUTPUT_BMP:
371 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
372 break;
373
374 case OUTPUT_EMF:
375 WriteEmf(page, name.c_str(), page_index);
376 break;
377#endif
378 case OUTPUT_PNG:
379 WritePng(name.c_str(), page_index, buffer, stride, width, height);
380 break;
381
382 case OUTPUT_PPM:
383 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
384 break;
385
386 default:
387 break;
388 }
389
390 FPDFBitmap_Destroy(bitmap);
391 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
392 FORM_OnBeforeClosePage(page, form);
393 FPDFText_ClosePage(text_page);
394 FPDF_ClosePage(page);
Jun Fangb553bcb2015-11-10 18:49:04 +0800395 return true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800396}
397
Tom Sepez5ee12d72014-12-17 16:24:01 -0800398void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800399 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800400 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401
402 IPDF_JSPLATFORM platform_callbacks;
403 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700404 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800405 platform_callbacks.app_alert = ExampleAppAlert;
406 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407
408 FPDF_FORMFILLINFO form_callbacks;
409 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800410 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411 form_callbacks.m_pJsPlatform = &platform_callbacks;
412
413 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700414 FPDF_FILEACCESS file_access;
415 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700416 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700417 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418 file_access.m_Param = &loader;
419
420 FX_FILEAVAIL file_avail;
421 memset(&file_avail, '\0', sizeof(file_avail));
422 file_avail.version = 1;
423 file_avail.IsDataAvail = Is_Data_Avail;
424
425 FX_DOWNLOADHINTS hints;
426 memset(&hints, '\0', sizeof(hints));
427 hints.version = 1;
428 hints.AddSegment = Add_Segment;
429
430 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800431 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800432 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800434
Jun Fangdf7f3662015-11-10 18:29:18 +0800435 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
436 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700437 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800438 if (doc) {
439 while (nRet == PDF_DATA_NOTAVAIL) {
440 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
441 }
442 if (nRet == PDF_DATA_ERROR) {
443 fprintf(stderr, "Unknown error in checking if doc was available.\n");
444 return;
445 }
446 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
447 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
448 fprintf(stderr,
449 "Error %d was returned in checking if form was available.\n",
450 nRet);
451 return;
452 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800453 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800454 }
455 } else {
456 fprintf(stderr, "Non-linearized path...\n");
Lei Zhang600d4072015-09-23 15:35:25 -0700457 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800458 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459
Lei Zhang5377ebf2015-09-23 14:52:53 -0700460 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400461 unsigned long err = FPDF_GetLastError();
462 fprintf(stderr, "Load pdf docs unsuccessful: ");
463 switch (err) {
464 case FPDF_ERR_SUCCESS:
465 fprintf(stderr, "Success");
466 break;
467 case FPDF_ERR_UNKNOWN:
468 fprintf(stderr, "Unknown error");
469 break;
470 case FPDF_ERR_FILE:
471 fprintf(stderr, "File not found or could not be opened");
472 break;
473 case FPDF_ERR_FORMAT:
474 fprintf(stderr, "File not in PDF format or corrupted");
475 break;
476 case FPDF_ERR_PASSWORD:
477 fprintf(stderr, "Password required or incorrect password");
478 break;
479 case FPDF_ERR_SECURITY:
480 fprintf(stderr, "Unsupported security scheme");
481 break;
482 case FPDF_ERR_PAGE:
483 fprintf(stderr, "Page not found or content error");
484 break;
485 default:
486 fprintf(stderr, "Unknown error %ld", err);
487 }
488 fprintf(stderr, ".\n");
489
Qin Zhao05224972015-10-20 18:31:06 -0400490 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800491 return;
492 }
493
Lei Zhang5377ebf2015-09-23 14:52:53 -0700494 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700495
Bo Xu2b7a49d2014-11-14 17:40:50 -0800496 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800497 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700498 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
499 !FPDF_LoadXFA(doc)) {
500 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800501 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700502 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
503 FPDF_SetFormFieldHighlightAlpha(form, 100);
504
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700505 FORM_DoDocumentJSAction(form);
506 FORM_DoDocumentOpenAction(form);
507
Jun Fangdf7f3662015-11-10 18:29:18 +0800508 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700509 int rendered_pages = 0;
510 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800512 if (bIsLinearized) {
513 nRet = PDF_DATA_NOTAVAIL;
514 while (nRet == PDF_DATA_NOTAVAIL) {
515 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
516 }
517 if (nRet == PDF_DATA_ERROR) {
518 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
519 i);
520 return;
521 }
522 }
523 if (RenderPage(name, doc, form, i, options)) {
524 ++rendered_pages;
525 } else {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700526 ++bad_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700527 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700528 }
529
530 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700531
532 // Note: The shut down order here is the reverse of the non-XFA branch order.
533 // Need to work out if this is required, and if it is, the lifetimes of
534 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700535 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800536 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700537
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538 FPDFAvail_Destroy(pdf_avail);
539
Tom Sepez1ed8a212015-05-11 15:25:39 -0700540 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
541 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542}
543
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800544static const char usage_string[] =
545 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700546 " --bin-dir=<path> - override path to v8 external data\n"
547 " --font-dir=<path> - override path to external fonts\n"
548 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800549#ifdef _WIN32
550 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
551 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
552#endif
553 " --png - write page images <pdf-name>.<page-number>.png\n"
554 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
555
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700556int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800557 std::vector<std::string> args(argv, argv + argc);
558 Options options;
559 std::list<std::string> files;
560 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800561 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700562 return 1;
563 }
564
Tom Sepez452b4f32015-10-13 09:27:27 -0700565#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700566 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800567#ifdef V8_USE_EXTERNAL_STARTUP_DATA
568 v8::StartupData natives;
569 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700570 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
571 &snapshot, &platform);
572#else // V8_USE_EXTERNAL_STARTUP_DATA
573 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800574#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700575#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800576
Tom Sepeza72e8e22015-10-07 10:17:53 -0700577 FPDF_LIBRARY_CONFIG config;
578 config.version = 2;
579 config.m_pUserFontPaths = nullptr;
580 config.m_pIsolate = nullptr;
581 config.m_v8EmbedderSlot = 0;
582
583 const char* path_array[2];
584 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700585 path_array[0] = options.font_directory.c_str();
586 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700587 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700588 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700589 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700590
591 UNSUPPORT_INFO unsuppored_info;
592 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
593 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800594 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700595
596 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
597
598 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800599 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700600 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800601 size_t file_length = 0;
602 char* file_contents = GetFileContents(filename.c_str(), &file_length);
603 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700604 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800605 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800606 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700607 }
608
609 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700610#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800611 v8::V8::ShutdownPlatform();
612 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700613#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700614
615 return 0;
616}