blob: 76de7ea7a5a88fdf5fdf1e869a54b5d8fd61e71c [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) {
Tom Sepezfb947282014-12-08 09:55:11 -0800199 // Deal with differences between UTF16LE and wchar_t on this platform.
200 size_t characters = 0;
201 while (msg[characters]) {
202 ++characters;
203 }
204 wchar_t* platform_string =
Lei Zhang5377ebf2015-09-23 14:52:53 -0700205 static_cast<wchar_t*>(malloc((characters + 1) * sizeof(wchar_t)));
Tom Sepezfb947282014-12-08 09:55:11 -0800206 for (size_t i = 0; i < characters + 1; ++i) {
207 unsigned char* ptr = (unsigned char*)&msg[i];
208 platform_string[i] = ptr[0] + 256 * ptr[1];
209 }
210 printf("Alert: %ls\n", platform_string);
211 free(platform_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212 return 0;
213}
214
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800215void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
216 printf("Goto Page: %d\n", pageNumber);
217}
218
219void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220 std::string feature = "Unknown";
221 switch (type) {
222 case FPDF_UNSP_DOC_XFAFORM:
223 feature = "XFA";
224 break;
225 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
226 feature = "Portfolios_Packages";
227 break;
228 case FPDF_UNSP_DOC_ATTACHMENT:
229 case FPDF_UNSP_ANNOT_ATTACHMENT:
230 feature = "Attachment";
231 break;
232 case FPDF_UNSP_DOC_SECURITY:
233 feature = "Rights_Management";
234 break;
235 case FPDF_UNSP_DOC_SHAREDREVIEW:
236 feature = "Shared_Review";
237 break;
238 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
239 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
240 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
241 feature = "Shared_Form";
242 break;
243 case FPDF_UNSP_ANNOT_3DANNOT:
244 feature = "3D";
245 break;
246 case FPDF_UNSP_ANNOT_MOVIE:
247 feature = "Movie";
248 break;
249 case FPDF_UNSP_ANNOT_SOUND:
250 feature = "Sound";
251 break;
252 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
253 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
254 feature = "Screen";
255 break;
256 case FPDF_UNSP_ANNOT_SIG:
257 feature = "Digital_Signature";
258 break;
259 }
260 printf("Unsupported feature: %s.\n", feature.c_str());
261}
262
Tom Sepez5ee12d72014-12-17 16:24:01 -0800263bool ParseCommandLine(const std::vector<std::string>& args,
264 Options* options, std::list<std::string>* files) {
265 if (args.empty()) {
266 return false;
267 }
268 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800269 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800270 for (; cur_idx < args.size(); ++cur_idx) {
271 const std::string& cur_arg = args[cur_idx];
272 if (cur_arg == "--ppm") {
273 if (options->output_format != OUTPUT_NONE) {
274 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
275 return false;
276 }
277 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800278 } else if (cur_arg == "--png") {
279 if (options->output_format != OUTPUT_NONE) {
280 fprintf(stderr, "Duplicate or conflicting --png argument\n");
281 return false;
282 }
283 options->output_format = OUTPUT_PNG;
Lei Zhang6f62d532015-09-23 15:31:44 -0700284 } else if (cur_arg.size() > 11 &&
285 cur_arg.compare(0, 11, "--font-dir=") == 0) {
286 if (!options->font_directory.empty()) {
287 fprintf(stderr, "Duplicate --font-dir argument\n");
288 return false;
289 }
290 options->font_directory = cur_arg.substr(11);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800291 }
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700292#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -0800293 else if (cur_arg == "--emf") {
294 if (options->output_format != OUTPUT_NONE) {
295 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
296 return false;
297 }
298 options->output_format = OUTPUT_EMF;
299 }
300 else if (cur_arg == "--bmp") {
301 if (options->output_format != OUTPUT_NONE) {
302 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
303 return false;
304 }
305 options->output_format = OUTPUT_BMP;
306 }
307#endif // _WIN32
Tom Sepez452b4f32015-10-13 09:27:27 -0700308#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800309#ifdef V8_USE_EXTERNAL_STARTUP_DATA
310 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
311 if (!options->bin_directory.empty()) {
312 fprintf(stderr, "Duplicate --bin-dir argument\n");
313 return false;
314 }
315 options->bin_directory = cur_arg.substr(10);
316 }
317#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700318#endif // PDF_ENABLE_V8
Tom Sepezdaa2e842015-01-29 15:44:37 -0800319 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
320 if (!options->scale_factor_as_string.empty()) {
321 fprintf(stderr, "Duplicate --scale argument\n");
322 return false;
323 }
324 options->scale_factor_as_string = cur_arg.substr(8);
325 }
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700326 else
327 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700328 }
Tom Sepez5ee12d72014-12-17 16:24:01 -0800329 if (cur_idx >= args.size()) {
330 fprintf(stderr, "No input files.\n");
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700331 return false;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800332 }
Bo Xud44e3922014-12-19 02:27:25 -0800333 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800334 files->push_back(args[i]);
335 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336 return true;
337}
338
Tom Sepezcf22eb82015-05-12 17:28:08 -0700339FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340 return true;
341}
342
343void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
344}
345
Tom Sepez5ee12d72014-12-17 16:24:01 -0800346void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800347 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800348 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349
350 IPDF_JSPLATFORM platform_callbacks;
351 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700352 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800353 platform_callbacks.app_alert = ExampleAppAlert;
354 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355
356 FPDF_FORMFILLINFO form_callbacks;
357 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezed631382014-11-18 14:10:25 -0800358 form_callbacks.version = 2;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359 form_callbacks.m_pJsPlatform = &platform_callbacks;
360
361 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700362 FPDF_FILEACCESS file_access;
363 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700364 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700365 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366 file_access.m_Param = &loader;
367
368 FX_FILEAVAIL file_avail;
369 memset(&file_avail, '\0', sizeof(file_avail));
370 file_avail.version = 1;
371 file_avail.IsDataAvail = Is_Data_Avail;
372
373 FX_DOWNLOADHINTS hints;
374 memset(&hints, '\0', sizeof(hints));
375 hints.version = 1;
376 hints.AddSegment = Add_Segment;
377
378 FPDF_DOCUMENT doc;
379 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
380
Lei Zhang5377ebf2015-09-23 14:52:53 -0700381 (void)FPDFAvail_IsDocAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382
Dan Sinclaird3fca802015-10-29 09:26:51 -0400383 if (FPDFAvail_IsLinearized(pdf_avail))
Lei Zhang5377ebf2015-09-23 14:52:53 -0700384 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Dan Sinclaird3fca802015-10-29 09:26:51 -0400385 else
Lei Zhang600d4072015-09-23 15:35:25 -0700386 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387
Lei Zhang5377ebf2015-09-23 14:52:53 -0700388 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400389 unsigned long err = FPDF_GetLastError();
390 fprintf(stderr, "Load pdf docs unsuccessful: ");
391 switch (err) {
392 case FPDF_ERR_SUCCESS:
393 fprintf(stderr, "Success");
394 break;
395 case FPDF_ERR_UNKNOWN:
396 fprintf(stderr, "Unknown error");
397 break;
398 case FPDF_ERR_FILE:
399 fprintf(stderr, "File not found or could not be opened");
400 break;
401 case FPDF_ERR_FORMAT:
402 fprintf(stderr, "File not in PDF format or corrupted");
403 break;
404 case FPDF_ERR_PASSWORD:
405 fprintf(stderr, "Password required or incorrect password");
406 break;
407 case FPDF_ERR_SECURITY:
408 fprintf(stderr, "Unsupported security scheme");
409 break;
410 case FPDF_ERR_PAGE:
411 fprintf(stderr, "Page not found or content error");
412 break;
413 default:
414 fprintf(stderr, "Unknown error %ld", err);
415 }
416 fprintf(stderr, ".\n");
417
Qin Zhao05224972015-10-20 18:31:06 -0400418 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800419 return;
420 }
421
Lei Zhang5377ebf2015-09-23 14:52:53 -0700422 (void)FPDF_GetDocPermissions(doc);
423 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424
Bo Xu2b7a49d2014-11-14 17:40:50 -0800425 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800426 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700427 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
428 !FPDF_LoadXFA(doc)) {
429 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800430 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700431 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
432 FPDF_SetFormFieldHighlightAlpha(form, 100);
433
434 int first_page = FPDFAvail_GetFirstPageNum(doc);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700435 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
437 int page_count = FPDF_GetPageCount(doc);
438 for (int i = 0; i < page_count; ++i) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700439 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700440 }
441
442 FORM_DoDocumentJSAction(form);
443 FORM_DoDocumentOpenAction(form);
444
Tom Sepez1ed8a212015-05-11 15:25:39 -0700445 int rendered_pages = 0;
446 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447 for (int i = 0; i < page_count; ++i) {
448 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700449 if (!page) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700450 ++bad_pages;
451 continue;
Jun Fangaeacba42014-08-22 17:04:29 -0700452 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700453 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
454 FORM_OnAfterLoadPage(page, form);
455 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
456
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800457 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800458 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800459 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800460 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800461 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
462 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800463
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700464 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700465 if (!bitmap) {
466 fprintf(stderr, "Page was too large to be rendered.\n");
467 bad_pages++;
468 continue;
469 }
470
Lei Zhang532a6a72014-07-09 11:47:15 -0700471 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700473 ++rendered_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700474
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700476 int stride = FPDFBitmap_GetStride(bitmap);
477 const char* buffer =
478 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
479
Tom Sepezdaa2e842015-01-29 15:44:37 -0800480 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700481#ifdef _WIN32
482 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800483 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700484 break;
485
486 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800487 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700488 break;
489#endif
490 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800491 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700492 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800493
494 case OUTPUT_PNG:
495 WritePng(name.c_str(), i, buffer, stride, width, height);
496 break;
497
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700498 default:
499 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500 }
501
502 FPDFBitmap_Destroy(bitmap);
503
504 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
505 FORM_OnBeforeClosePage(page, form);
506 FPDFText_ClosePage(text_page);
507 FPDF_ClosePage(page);
508 }
509
510 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700511
512 // Note: The shut down order here is the reverse of the non-XFA branch order.
513 // Need to work out if this is required, and if it is, the lifetimes of
514 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800516 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700517
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700518 FPDFAvail_Destroy(pdf_avail);
519
Tom Sepez1ed8a212015-05-11 15:25:39 -0700520 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
521 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700522}
523
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800524static const char usage_string[] =
525 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700526 " --bin-dir=<path> - override path to v8 external data\n"
527 " --font-dir=<path> - override path to external fonts\n"
528 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800529#ifdef _WIN32
530 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
531 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
532#endif
533 " --png - write page images <pdf-name>.<page-number>.png\n"
534 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
535
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700536int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800537 std::vector<std::string> args(argv, argv + argc);
538 Options options;
539 std::list<std::string> files;
540 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800541 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542 return 1;
543 }
544
Tom Sepez452b4f32015-10-13 09:27:27 -0700545#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700546 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800547#ifdef V8_USE_EXTERNAL_STARTUP_DATA
548 v8::StartupData natives;
549 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700550 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
551 &snapshot, &platform);
552#else // V8_USE_EXTERNAL_STARTUP_DATA
553 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800554#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700555#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800556
Tom Sepeza72e8e22015-10-07 10:17:53 -0700557 FPDF_LIBRARY_CONFIG config;
558 config.version = 2;
559 config.m_pUserFontPaths = nullptr;
560 config.m_pIsolate = nullptr;
561 config.m_v8EmbedderSlot = 0;
562
563 const char* path_array[2];
564 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700565 path_array[0] = options.font_directory.c_str();
566 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700567 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700568 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700569 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570
571 UNSUPPORT_INFO unsuppored_info;
572 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
573 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800574 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700575
576 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
577
578 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800579 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700580 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800581 size_t file_length = 0;
582 char* file_contents = GetFileContents(filename.c_str(), &file_length);
583 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700584 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800585 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800586 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700587 }
588
589 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700590#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800591 v8::V8::ShutdownPlatform();
592 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700593#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594
595 return 0;
596}