blob: 4ccf1a77212d9425711e956aa2d82e0dad507f17 [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 Sepezd831dc72015-10-19 16:04:22 -070022#include "../testing/test_support.h"
Tom Sepezaf18cb32015-02-05 15:06:01 -080023#include "image_diff_png.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070024
Tom Sepez452b4f32015-10-13 09:27:27 -070025#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080026#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070027#include "v8/include/v8.h"
Tom Sepez452b4f32015-10-13 09:27:27 -070028#endif
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
30#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080031#define snprintf _snprintf
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
Tom Sepezaf18cb32015-02-05 15:06:01 -080054static bool CheckDimensions(int stride, int width, int height) {
55 if (stride < 0 || width < 0 || height < 0)
56 return false;
57 if (height > 0 && width > INT_MAX / height)
58 return false;
59 return true;
60}
61
Vitaly Buka9e0177a2014-07-22 18:15:42 -070062static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
63 int stride, int width, int height) {
64 const char* buffer = reinterpret_cast<const char*>(buffer_void);
65
Tom Sepezaf18cb32015-02-05 15:06:01 -080066 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080068
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069 int out_len = width * height;
70 if (out_len > INT_MAX / 3)
71 return;
72 out_len *= 3;
73
74 char filename[256];
75 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070076 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077 if (!fp)
78 return;
79 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
80 // Source data is B, G, R, unused.
81 // Dest data is R, G, B.
82 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -070083 for (int h = 0; h < height; ++h) {
84 const char* src_line = buffer + (stride * h);
85 char* dest_line = result + (width * h * 3);
86 for (int w = 0; w < width; ++w) {
87 // R
88 dest_line[w * 3] = src_line[(w * 4) + 2];
89 // G
90 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
91 // B
92 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094 }
Lei Zhange00660b2015-08-13 15:40:18 -070095 fwrite(result, out_len, 1, fp);
96 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097 fclose(fp);
98}
99
Tom Sepezaf18cb32015-02-05 15:06:01 -0800100static void WritePng(const char* pdf_name, int num, const void* buffer_void,
101 int stride, int width, int height) {
102 if (!CheckDimensions(stride, width, height))
103 return;
104
105 std::vector<unsigned char> png_encoding;
106 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
107 if (!image_diff_png::EncodeBGRAPNG(
108 buffer, width, height, stride, false, &png_encoding)) {
109 fprintf(stderr, "Failed to convert bitmap to PNG\n");
110 return;
111 }
112
113 char filename[256];
114 int chars_formatted = snprintf(
115 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
116 if (chars_formatted < 0 ||
117 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
118 fprintf(stderr, "Filname %s is too long\n", filename);
119 return;
120 }
121
122 FILE* fp = fopen(filename, "wb");
123 if (!fp) {
124 fprintf(stderr, "Failed to open %s for output\n", filename);
125 return;
126 }
127
128 size_t bytes_written = fwrite(
129 &png_encoding.front(), 1, png_encoding.size(), fp);
130 if (bytes_written != png_encoding.size())
131 fprintf(stderr, "Failed to write to %s\n", filename);
132
Lei Zhang5377ebf2015-09-23 14:52:53 -0700133 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800134}
135
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700136#ifdef _WIN32
137static void WriteBmp(const char* pdf_name, int num, const void* buffer,
138 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800139 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700140 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800141
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700142 int out_len = stride * height;
143 if (out_len > INT_MAX / 3)
144 return;
145
146 char filename[256];
147 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
148 FILE* fp = fopen(filename, "wb");
149 if (!fp)
150 return;
151
Nico Weber2827bdd2015-07-01 14:08:08 -0700152 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700153 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
154 bmi.bmiHeader.biWidth = width;
155 bmi.bmiHeader.biHeight = -height; // top-down image
156 bmi.bmiHeader.biPlanes = 1;
157 bmi.bmiHeader.biBitCount = 32;
158 bmi.bmiHeader.biCompression = BI_RGB;
159 bmi.bmiHeader.biSizeImage = 0;
160
Nico Weber2827bdd2015-07-01 14:08:08 -0700161 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700162 file_header.bfType = 0x4d42;
163 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
164 file_header.bfOffBits = file_header.bfSize - out_len;
165
166 fwrite(&file_header, sizeof(file_header), 1, fp);
167 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
168 fwrite(buffer, out_len, 1, fp);
169 fclose(fp);
170}
171
172void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
173 int width = static_cast<int>(FPDF_GetPageWidth(page));
174 int height = static_cast<int>(FPDF_GetPageHeight(page));
175
176 char filename[256];
177 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
178
Lei Zhang5377ebf2015-09-23 14:52:53 -0700179 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800180
181 HRGN rgn = CreateRectRgn(0, 0, width, height);
182 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700183 DeleteObject(rgn);
184
185 SelectObject(dc, GetStockObject(NULL_PEN));
186 SelectObject(dc, GetStockObject(WHITE_BRUSH));
187 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
188 Rectangle(dc, 0, 0, width + 1, height + 1);
189
190 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
191 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
192
193 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
194}
195#endif
196
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800197int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
198 int, int) {
Lei Zhang79e893a2015-11-04 16:02:47 -0800199 std::wstring platform_string = GetWideString(msg);
200 printf("Alert: %ls\n", platform_string.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201 return 0;
202}
203
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800204void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
205 printf("Goto Page: %d\n", pageNumber);
206}
207
208void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209 std::string feature = "Unknown";
210 switch (type) {
211 case FPDF_UNSP_DOC_XFAFORM:
212 feature = "XFA";
213 break;
214 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
215 feature = "Portfolios_Packages";
216 break;
217 case FPDF_UNSP_DOC_ATTACHMENT:
218 case FPDF_UNSP_ANNOT_ATTACHMENT:
219 feature = "Attachment";
220 break;
221 case FPDF_UNSP_DOC_SECURITY:
222 feature = "Rights_Management";
223 break;
224 case FPDF_UNSP_DOC_SHAREDREVIEW:
225 feature = "Shared_Review";
226 break;
227 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
228 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
229 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
230 feature = "Shared_Form";
231 break;
232 case FPDF_UNSP_ANNOT_3DANNOT:
233 feature = "3D";
234 break;
235 case FPDF_UNSP_ANNOT_MOVIE:
236 feature = "Movie";
237 break;
238 case FPDF_UNSP_ANNOT_SOUND:
239 feature = "Sound";
240 break;
241 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
242 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
243 feature = "Screen";
244 break;
245 case FPDF_UNSP_ANNOT_SIG:
246 feature = "Digital_Signature";
247 break;
248 }
249 printf("Unsupported feature: %s.\n", feature.c_str());
250}
251
Tom Sepez5ee12d72014-12-17 16:24:01 -0800252bool ParseCommandLine(const std::vector<std::string>& args,
253 Options* options, std::list<std::string>* files) {
254 if (args.empty()) {
255 return false;
256 }
257 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800258 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800259 for (; cur_idx < args.size(); ++cur_idx) {
260 const std::string& cur_arg = args[cur_idx];
261 if (cur_arg == "--ppm") {
262 if (options->output_format != OUTPUT_NONE) {
263 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
264 return false;
265 }
266 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800267 } else if (cur_arg == "--png") {
268 if (options->output_format != OUTPUT_NONE) {
269 fprintf(stderr, "Duplicate or conflicting --png argument\n");
270 return false;
271 }
272 options->output_format = OUTPUT_PNG;
Lei Zhang6f62d532015-09-23 15:31:44 -0700273 } else if (cur_arg.size() > 11 &&
274 cur_arg.compare(0, 11, "--font-dir=") == 0) {
275 if (!options->font_directory.empty()) {
276 fprintf(stderr, "Duplicate --font-dir argument\n");
277 return false;
278 }
279 options->font_directory = cur_arg.substr(11);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800280 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700281#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800282 else if (cur_arg == "--emf") {
283 if (options->output_format != OUTPUT_NONE) {
284 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
285 return false;
286 }
287 options->output_format = OUTPUT_EMF;
288 }
289 else if (cur_arg == "--bmp") {
290 if (options->output_format != OUTPUT_NONE) {
291 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
292 return false;
293 }
294 options->output_format = OUTPUT_BMP;
295 }
296#endif // _WIN32
Tom Sepez452b4f32015-10-13 09:27:27 -0700297#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800298#ifdef V8_USE_EXTERNAL_STARTUP_DATA
299 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
300 if (!options->bin_directory.empty()) {
301 fprintf(stderr, "Duplicate --bin-dir argument\n");
302 return false;
303 }
304 options->bin_directory = cur_arg.substr(10);
305 }
306#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700307#endif // PDF_ENABLE_V8
Tom Sepezdaa2e842015-01-29 15:44:37 -0800308 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
309 if (!options->scale_factor_as_string.empty()) {
310 fprintf(stderr, "Duplicate --scale argument\n");
311 return false;
312 }
313 options->scale_factor_as_string = cur_arg.substr(8);
314 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700315 else
316 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800318 if (cur_idx >= args.size()) {
319 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700320 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800321 }
Bo Xud44e3922014-12-19 02:27:25 -0800322 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800323 files->push_back(args[i]);
324 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325 return true;
326}
327
Tom Sepezcf22eb82015-05-12 17:28:08 -0700328FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329 return true;
330}
331
332void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
333}
334
Tom Sepez5ee12d72014-12-17 16:24:01 -0800335void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800336 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800337 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
339 IPDF_JSPLATFORM platform_callbacks;
340 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700341 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800342 platform_callbacks.app_alert = ExampleAppAlert;
343 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700344
345 FPDF_FORMFILLINFO form_callbacks;
346 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800347 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348 form_callbacks.m_pJsPlatform = &platform_callbacks;
349
350 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351 FPDF_FILEACCESS file_access;
352 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700353 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700354 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355 file_access.m_Param = &loader;
356
357 FX_FILEAVAIL file_avail;
358 memset(&file_avail, '\0', sizeof(file_avail));
359 file_avail.version = 1;
360 file_avail.IsDataAvail = Is_Data_Avail;
361
362 FX_DOWNLOADHINTS hints;
363 memset(&hints, '\0', sizeof(hints));
364 hints.version = 1;
365 hints.AddSegment = Add_Segment;
366
367 FPDF_DOCUMENT doc;
368 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
369
Lei Zhang5377ebf2015-09-23 14:52:53 -0700370 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700371
Dan Sinclaird3fca802015-10-29 09:26:51 -0400372 if (FPDFAvail_IsLinearized(pdf_avail))
Lei Zhang5377ebf2015-09-23 14:52:53 -0700373 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Dan Sinclaird3fca802015-10-29 09:26:51 -0400374 else
Lei Zhang600d4072015-09-23 15:35:25 -0700375 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376
Lei Zhang5377ebf2015-09-23 14:52:53 -0700377 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400378 unsigned long err = FPDF_GetLastError();
379 fprintf(stderr, "Load pdf docs unsuccessful: ");
380 switch (err) {
381 case FPDF_ERR_SUCCESS:
382 fprintf(stderr, "Success");
383 break;
384 case FPDF_ERR_UNKNOWN:
385 fprintf(stderr, "Unknown error");
386 break;
387 case FPDF_ERR_FILE:
388 fprintf(stderr, "File not found or could not be opened");
389 break;
390 case FPDF_ERR_FORMAT:
391 fprintf(stderr, "File not in PDF format or corrupted");
392 break;
393 case FPDF_ERR_PASSWORD:
394 fprintf(stderr, "Password required or incorrect password");
395 break;
396 case FPDF_ERR_SECURITY:
397 fprintf(stderr, "Unsupported security scheme");
398 break;
399 case FPDF_ERR_PAGE:
400 fprintf(stderr, "Page not found or content error");
401 break;
402 default:
403 fprintf(stderr, "Unknown error %ld", err);
404 }
405 fprintf(stderr, ".\n");
406
Qin Zhao05224972015-10-20 18:31:06 -0400407 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800408 return;
409 }
410
Lei Zhang5377ebf2015-09-23 14:52:53 -0700411 (void)FPDF_GetDocPermissions(doc);
412 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700413
Bo Xu2b7a49d2014-11-14 17:40:50 -0800414 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800415 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700416 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
417 !FPDF_LoadXFA(doc)) {
418 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800419 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
421 FPDF_SetFormFieldHighlightAlpha(form, 100);
422
423 int first_page = FPDFAvail_GetFirstPageNum(doc);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700424 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
426 int page_count = FPDF_GetPageCount(doc);
427 for (int i = 0; i < page_count; ++i) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700428 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429 }
430
431 FORM_DoDocumentJSAction(form);
432 FORM_DoDocumentOpenAction(form);
433
Tom Sepez1ed8a212015-05-11 15:25:39 -0700434 int rendered_pages = 0;
435 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436 for (int i = 0; i < page_count; ++i) {
437 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700438 if (!page) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700439 ++bad_pages;
440 continue;
Jun Fangaeacba42014-08-22 17:04:29 -0700441 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
443 FORM_OnAfterLoadPage(page, form);
444 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
445
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800446 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800447 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800448 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800449 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800450 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
451 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800452
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700453 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700454 if (!bitmap) {
455 fprintf(stderr, "Page was too large to be rendered.\n");
456 bad_pages++;
457 continue;
458 }
459
Lei Zhang532a6a72014-07-09 11:47:15 -0700460 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700461 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700462 ++rendered_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700463
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700465 int stride = FPDFBitmap_GetStride(bitmap);
466 const char* buffer =
467 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
468
Tom Sepezdaa2e842015-01-29 15:44:37 -0800469 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700470#ifdef _WIN32
471 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800472 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700473 break;
474
475 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800476 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700477 break;
478#endif
479 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800480 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700481 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800482
483 case OUTPUT_PNG:
484 WritePng(name.c_str(), i, buffer, stride, width, height);
485 break;
486
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700487 default:
488 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489 }
490
491 FPDFBitmap_Destroy(bitmap);
492
493 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
494 FORM_OnBeforeClosePage(page, form);
495 FPDFText_ClosePage(text_page);
496 FPDF_ClosePage(page);
497 }
498
499 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700500
501 // Note: The shut down order here is the reverse of the non-XFA branch order.
502 // Need to work out if this is required, and if it is, the lifetimes of
503 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800505 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700506
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507 FPDFAvail_Destroy(pdf_avail);
508
Tom Sepez1ed8a212015-05-11 15:25:39 -0700509 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
510 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800513static const char usage_string[] =
514 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700515 " --bin-dir=<path> - override path to v8 external data\n"
516 " --font-dir=<path> - override path to external fonts\n"
517 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800518#ifdef _WIN32
519 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
520 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
521#endif
522 " --png - write page images <pdf-name>.<page-number>.png\n"
523 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
524
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700525int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800526 std::vector<std::string> args(argv, argv + argc);
527 Options options;
528 std::list<std::string> files;
529 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800530 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700531 return 1;
532 }
533
Tom Sepez452b4f32015-10-13 09:27:27 -0700534#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700535 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800536#ifdef V8_USE_EXTERNAL_STARTUP_DATA
537 v8::StartupData natives;
538 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700539 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
540 &snapshot, &platform);
541#else // V8_USE_EXTERNAL_STARTUP_DATA
542 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800543#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700544#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800545
Tom Sepeza72e8e22015-10-07 10:17:53 -0700546 FPDF_LIBRARY_CONFIG config;
547 config.version = 2;
548 config.m_pUserFontPaths = nullptr;
549 config.m_pIsolate = nullptr;
550 config.m_v8EmbedderSlot = 0;
551
552 const char* path_array[2];
553 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700554 path_array[0] = options.font_directory.c_str();
555 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700556 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700557 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700558 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700559
560 UNSUPPORT_INFO unsuppored_info;
561 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
562 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800563 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700564
565 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
566
567 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800568 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800570 size_t file_length = 0;
571 char* file_contents = GetFileContents(filename.c_str(), &file_length);
572 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700573 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800574 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800575 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700576 }
577
578 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700579#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800580 v8::V8::ShutdownPlatform();
581 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700582#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700583
584 return 0;
585}