blob: c9d84af04d51e5724190d8c0c497e358a2f6a2d4 [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
Lei Zhang600d4072015-09-23 15:35:25 -0700383 if (FPDFAvail_IsLinearized(pdf_avail)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800384 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700385 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Lei Zhang600d4072015-09-23 15:35:25 -0700386 } else {
387 fprintf(stderr, "Non-linearized path...\n");
388 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389 }
390
Lei Zhang5377ebf2015-09-23 14:52:53 -0700391 if (!doc) {
JUN FANG827a1722015-03-05 13:39:21 -0800392 fprintf(stderr, "Load pdf docs unsuccessful.\n");
Qin Zhao05224972015-10-20 18:31:06 -0400393 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800394 return;
395 }
396
Lei Zhang5377ebf2015-09-23 14:52:53 -0700397 (void)FPDF_GetDocPermissions(doc);
398 (void)FPDFAvail_IsFormAvail(pdf_avail, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399
Bo Xu2b7a49d2014-11-14 17:40:50 -0800400 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
JUN FANG827a1722015-03-05 13:39:21 -0800401 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700402 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
403 !FPDF_LoadXFA(doc)) {
404 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800405 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
407 FPDF_SetFormFieldHighlightAlpha(form, 100);
408
409 int first_page = FPDFAvail_GetFirstPageNum(doc);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700410 (void)FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411
412 int page_count = FPDF_GetPageCount(doc);
413 for (int i = 0; i < page_count; ++i) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700414 (void)FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415 }
416
417 FORM_DoDocumentJSAction(form);
418 FORM_DoDocumentOpenAction(form);
419
Tom Sepez1ed8a212015-05-11 15:25:39 -0700420 int rendered_pages = 0;
421 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700422 for (int i = 0; i < page_count; ++i) {
423 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700424 if (!page) {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700425 ++bad_pages;
426 continue;
Jun Fangaeacba42014-08-22 17:04:29 -0700427 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
429 FORM_OnAfterLoadPage(page, form);
430 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
431
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800432 double scale = 1.0;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800433 if (!options.scale_factor_as_string.empty()) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800434 std::stringstream(options.scale_factor_as_string) >> scale;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800435 }
John Abd-El-Malek8a7b6692015-02-25 11:08:16 -0800436 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
437 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Tom Sepezdaa2e842015-01-29 15:44:37 -0800438
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Tom Sepezbad79b32015-05-08 12:02:05 -0700440 if (!bitmap) {
441 fprintf(stderr, "Page was too large to be rendered.\n");
442 bad_pages++;
443 continue;
444 }
445
Lei Zhang532a6a72014-07-09 11:47:15 -0700446 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Lei Zhang5377ebf2015-09-23 14:52:53 -0700448 ++rendered_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700449
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700450 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700451 int stride = FPDFBitmap_GetStride(bitmap);
452 const char* buffer =
453 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
454
Tom Sepezdaa2e842015-01-29 15:44:37 -0800455 switch (options.output_format) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700456#ifdef _WIN32
457 case OUTPUT_BMP:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800458 WriteBmp(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700459 break;
460
461 case OUTPUT_EMF:
Tom Sepezc151fbb2014-12-18 11:14:19 -0800462 WriteEmf(page, name.c_str(), i);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700463 break;
464#endif
465 case OUTPUT_PPM:
Tom Sepez5ee12d72014-12-17 16:24:01 -0800466 WritePpm(name.c_str(), i, buffer, stride, width, height);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700467 break;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800468
469 case OUTPUT_PNG:
470 WritePng(name.c_str(), i, buffer, stride, width, height);
471 break;
472
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700473 default:
474 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475 }
476
477 FPDFBitmap_Destroy(bitmap);
478
479 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
480 FORM_OnBeforeClosePage(page, form);
481 FPDFText_ClosePage(text_page);
482 FPDF_ClosePage(page);
483 }
484
485 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700486
487 // Note: The shut down order here is the reverse of the non-XFA branch order.
488 // Need to work out if this is required, and if it is, the lifetimes of
489 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700490 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800491 FPDFDOC_ExitFormFillEnvironment(form);
Lei Zhangba026912015-07-16 10:06:11 -0700492
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700493 FPDFAvail_Destroy(pdf_avail);
494
Tom Sepez1ed8a212015-05-11 15:25:39 -0700495 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
496 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497}
498
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800499static const char usage_string[] =
500 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700501 " --bin-dir=<path> - override path to v8 external data\n"
502 " --font-dir=<path> - override path to external fonts\n"
503 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800504#ifdef _WIN32
505 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
506 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
507#endif
508 " --png - write page images <pdf-name>.<page-number>.png\n"
509 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
510
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800512 std::vector<std::string> args(argv, argv + argc);
513 Options options;
514 std::list<std::string> files;
515 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800516 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700517 return 1;
518 }
519
Tom Sepez452b4f32015-10-13 09:27:27 -0700520#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700521 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800522#ifdef V8_USE_EXTERNAL_STARTUP_DATA
523 v8::StartupData natives;
524 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700525 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
526 &snapshot, &platform);
527#else // V8_USE_EXTERNAL_STARTUP_DATA
528 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800529#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700530#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800531
Tom Sepeza72e8e22015-10-07 10:17:53 -0700532 FPDF_LIBRARY_CONFIG config;
533 config.version = 2;
534 config.m_pUserFontPaths = nullptr;
535 config.m_pIsolate = nullptr;
536 config.m_v8EmbedderSlot = 0;
537
538 const char* path_array[2];
539 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700540 path_array[0] = options.font_directory.c_str();
541 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700542 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700543 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700544 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700545
546 UNSUPPORT_INFO unsuppored_info;
547 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
548 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800549 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700550
551 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
552
553 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800554 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700555 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800556 size_t file_length = 0;
557 char* file_contents = GetFileContents(filename.c_str(), &file_length);
558 if (!file_contents)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700559 continue;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800560 RenderPdf(filename, file_contents, file_length, options);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800561 free(file_contents);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700562 }
563
564 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700565#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800566 v8::V8::ShutdownPlatform();
567 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700568#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700569
570 return 0;
571}