blob: 656e041e36d4a404c44064032e6ac48bff7f7529 [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
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080017#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080018#include "public/fpdf_ext.h"
19#include "public/fpdf_formfill.h"
20#include "public/fpdf_text.h"
21#include "public/fpdfview.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050022#include "samples/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080023#include "testing/test_support.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"
Lei Zhang8241df72015-11-06 14:38:48 -080028#endif // PDF_ENABLE_V8
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 {
Tom Sepez2991d8d2016-01-15 16:02:48 -080045 Options() : show_config(false), output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080046
Tom Sepez2991d8d2016-01-15 16:02:48 -080047 bool show_config;
Tom Sepez5ee12d72014-12-17 16:24:01 -080048 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080049 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080050 std::string exe_path;
51 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070052 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080053};
54
Tom Sepezaf18cb32015-02-05 15:06:01 -080055static bool CheckDimensions(int stride, int width, int height) {
56 if (stride < 0 || width < 0 || height < 0)
57 return false;
58 if (height > 0 && width > INT_MAX / height)
59 return false;
60 return true;
61}
62
Vitaly Buka9e0177a2014-07-22 18:15:42 -070063static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
64 int stride, int width, int height) {
65 const char* buffer = reinterpret_cast<const char*>(buffer_void);
66
Tom Sepezaf18cb32015-02-05 15:06:01 -080067 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080069
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070 int out_len = width * height;
71 if (out_len > INT_MAX / 3)
72 return;
73 out_len *= 3;
74
75 char filename[256];
76 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070077 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078 if (!fp)
79 return;
80 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
81 // Source data is B, G, R, unused.
82 // Dest data is R, G, B.
83 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -070084 for (int h = 0; h < height; ++h) {
85 const char* src_line = buffer + (stride * h);
86 char* dest_line = result + (width * h * 3);
87 for (int w = 0; w < width; ++w) {
88 // R
89 dest_line[w * 3] = src_line[(w * 4) + 2];
90 // G
91 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
92 // B
93 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095 }
Lei Zhange00660b2015-08-13 15:40:18 -070096 fwrite(result, out_len, 1, fp);
97 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098 fclose(fp);
99}
100
Tom Sepezaf18cb32015-02-05 15:06:01 -0800101static void WritePng(const char* pdf_name, int num, const void* buffer_void,
102 int stride, int width, int height) {
103 if (!CheckDimensions(stride, width, height))
104 return;
105
106 std::vector<unsigned char> png_encoding;
107 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
108 if (!image_diff_png::EncodeBGRAPNG(
109 buffer, width, height, stride, false, &png_encoding)) {
110 fprintf(stderr, "Failed to convert bitmap to PNG\n");
111 return;
112 }
113
114 char filename[256];
115 int chars_formatted = snprintf(
116 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
117 if (chars_formatted < 0 ||
118 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
119 fprintf(stderr, "Filname %s is too long\n", filename);
120 return;
121 }
122
123 FILE* fp = fopen(filename, "wb");
124 if (!fp) {
125 fprintf(stderr, "Failed to open %s for output\n", filename);
126 return;
127 }
128
129 size_t bytes_written = fwrite(
130 &png_encoding.front(), 1, png_encoding.size(), fp);
131 if (bytes_written != png_encoding.size())
132 fprintf(stderr, "Failed to write to %s\n", filename);
133
Lei Zhang5377ebf2015-09-23 14:52:53 -0700134 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800135}
136
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700137#ifdef _WIN32
138static void WriteBmp(const char* pdf_name, int num, const void* buffer,
139 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800140 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700141 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800142
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700143 int out_len = stride * height;
144 if (out_len > INT_MAX / 3)
145 return;
146
147 char filename[256];
148 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
149 FILE* fp = fopen(filename, "wb");
150 if (!fp)
151 return;
152
Nico Weber2827bdd2015-07-01 14:08:08 -0700153 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700154 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
155 bmi.bmiHeader.biWidth = width;
156 bmi.bmiHeader.biHeight = -height; // top-down image
157 bmi.bmiHeader.biPlanes = 1;
158 bmi.bmiHeader.biBitCount = 32;
159 bmi.bmiHeader.biCompression = BI_RGB;
160 bmi.bmiHeader.biSizeImage = 0;
161
Nico Weber2827bdd2015-07-01 14:08:08 -0700162 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700163 file_header.bfType = 0x4d42;
164 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
165 file_header.bfOffBits = file_header.bfSize - out_len;
166
167 fwrite(&file_header, sizeof(file_header), 1, fp);
168 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
169 fwrite(buffer, out_len, 1, fp);
170 fclose(fp);
171}
172
173void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
174 int width = static_cast<int>(FPDF_GetPageWidth(page));
175 int height = static_cast<int>(FPDF_GetPageHeight(page));
176
177 char filename[256];
178 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
179
Lei Zhang5377ebf2015-09-23 14:52:53 -0700180 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800181
182 HRGN rgn = CreateRectRgn(0, 0, width, height);
183 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700184 DeleteObject(rgn);
185
186 SelectObject(dc, GetStockObject(NULL_PEN));
187 SelectObject(dc, GetStockObject(WHITE_BRUSH));
188 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
189 Rectangle(dc, 0, 0, width + 1, height + 1);
190
191 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
192 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
193
194 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
195}
196#endif
197
Tom Sepez58fb36a2016-02-01 10:32:14 -0800198// These example JS platform callback handlers are entirely optional,
199// and exist here to show the flow of information from a document back
200// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800201int ExampleAppAlert(IPDF_JSPLATFORM*,
202 FPDF_WIDESTRING msg,
203 FPDF_WIDESTRING title,
204 int nType,
205 int nIcon) {
206 printf("%ls", GetPlatformWString(title).c_str());
207 if (nIcon || nType)
208 printf("[icon=%d,type=%d]", nIcon, nType);
209 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210 return 0;
211}
212
Tom Sepez58fb36a2016-02-01 10:32:14 -0800213int ExampleAppResponse(IPDF_JSPLATFORM*,
214 FPDF_WIDESTRING question,
215 FPDF_WIDESTRING title,
216 FPDF_WIDESTRING defaultValue,
217 FPDF_WIDESTRING label,
218 FPDF_BOOL isPassword,
219 void* response,
220 int length) {
221 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
222 GetPlatformWString(title).c_str(),
223 GetPlatformWString(question).c_str(),
224 GetPlatformWString(defaultValue).c_str(),
225 GetPlatformWString(label).c_str(), isPassword, length);
226
227 // UTF-16, always LE regardless of platform.
228 uint8_t* ptr = static_cast<uint8_t*>(response);
229 ptr[0] = 'N';
230 ptr[1] = 0;
231 ptr[2] = 'o';
232 ptr[3] = 0;
233 return 4;
234}
235
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800236void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
237 printf("Goto Page: %d\n", pageNumber);
238}
239
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800240void ExampleDocMail(IPDF_JSPLATFORM*,
241 void* mailData,
242 int length,
243 FPDF_BOOL bUI,
244 FPDF_WIDESTRING To,
245 FPDF_WIDESTRING Subject,
246 FPDF_WIDESTRING CC,
247 FPDF_WIDESTRING BCC,
248 FPDF_WIDESTRING Msg) {
249 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
250 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
251 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
252 GetPlatformWString(Msg).c_str());
253}
254
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800255void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256 std::string feature = "Unknown";
257 switch (type) {
258 case FPDF_UNSP_DOC_XFAFORM:
259 feature = "XFA";
260 break;
261 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
262 feature = "Portfolios_Packages";
263 break;
264 case FPDF_UNSP_DOC_ATTACHMENT:
265 case FPDF_UNSP_ANNOT_ATTACHMENT:
266 feature = "Attachment";
267 break;
268 case FPDF_UNSP_DOC_SECURITY:
269 feature = "Rights_Management";
270 break;
271 case FPDF_UNSP_DOC_SHAREDREVIEW:
272 feature = "Shared_Review";
273 break;
274 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
275 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
276 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
277 feature = "Shared_Form";
278 break;
279 case FPDF_UNSP_ANNOT_3DANNOT:
280 feature = "3D";
281 break;
282 case FPDF_UNSP_ANNOT_MOVIE:
283 feature = "Movie";
284 break;
285 case FPDF_UNSP_ANNOT_SOUND:
286 feature = "Sound";
287 break;
288 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
289 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
290 feature = "Screen";
291 break;
292 case FPDF_UNSP_ANNOT_SIG:
293 feature = "Digital_Signature";
294 break;
295 }
296 printf("Unsupported feature: %s.\n", feature.c_str());
297}
298
Tom Sepez5ee12d72014-12-17 16:24:01 -0800299bool ParseCommandLine(const std::vector<std::string>& args,
300 Options* options, std::list<std::string>* files) {
301 if (args.empty()) {
302 return false;
303 }
304 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800305 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800306 for (; cur_idx < args.size(); ++cur_idx) {
307 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800308 if (cur_arg == "--show-config") {
309 options->show_config = true;
310 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800311 if (options->output_format != OUTPUT_NONE) {
312 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
313 return false;
314 }
315 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800316 } else if (cur_arg == "--png") {
317 if (options->output_format != OUTPUT_NONE) {
318 fprintf(stderr, "Duplicate or conflicting --png argument\n");
319 return false;
320 }
321 options->output_format = OUTPUT_PNG;
Lei Zhang6f62d532015-09-23 15:31:44 -0700322 } else if (cur_arg.size() > 11 &&
323 cur_arg.compare(0, 11, "--font-dir=") == 0) {
324 if (!options->font_directory.empty()) {
325 fprintf(stderr, "Duplicate --font-dir argument\n");
326 return false;
327 }
328 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700329#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500330 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800331 if (options->output_format != OUTPUT_NONE) {
332 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
333 return false;
334 }
335 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500336 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800337 if (options->output_format != OUTPUT_NONE) {
338 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
339 return false;
340 }
341 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800342#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500343
Tom Sepez452b4f32015-10-13 09:27:27 -0700344#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800345#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500346 } else if (cur_arg.size() > 10 &&
347 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800348 if (!options->bin_directory.empty()) {
349 fprintf(stderr, "Duplicate --bin-dir argument\n");
350 return false;
351 }
352 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800353#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700354#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500355
356 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800357 if (!options->scale_factor_as_string.empty()) {
358 fprintf(stderr, "Duplicate --scale argument\n");
359 return false;
360 }
361 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800362 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
363 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
364 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500365 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700366 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500367 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368 }
Bo Xud44e3922014-12-19 02:27:25 -0800369 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800370 files->push_back(args[i]);
371 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372 return true;
373}
374
Tom Sepezcf22eb82015-05-12 17:28:08 -0700375FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700376 return true;
377}
378
379void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
380}
381
Jun Fangb553bcb2015-11-10 18:49:04 +0800382bool RenderPage(const std::string& name,
383 const FPDF_DOCUMENT& doc,
384 const FPDF_FORMHANDLE& form,
385 const int page_index,
386 const Options& options) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800387 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
388 if (!page) {
Jun Fangb553bcb2015-11-10 18:49:04 +0800389 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800390 }
391 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
392 FORM_OnAfterLoadPage(page, form);
393 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
394
395 double scale = 1.0;
396 if (!options.scale_factor_as_string.empty()) {
397 std::stringstream(options.scale_factor_as_string) >> scale;
398 }
399 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
400 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800401 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
402 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
Jun Fangdf7f3662015-11-10 18:29:18 +0800403 if (!bitmap) {
404 fprintf(stderr, "Page was too large to be rendered.\n");
Jun Fangb553bcb2015-11-10 18:49:04 +0800405 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800406 }
Lei Zhang453d96b2015-12-31 13:13:10 -0800407 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
408 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jun Fangdf7f3662015-11-10 18:29:18 +0800409 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
410
411 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
412 int stride = FPDFBitmap_GetStride(bitmap);
413 const char* buffer =
414 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
415
416 switch (options.output_format) {
417#ifdef _WIN32
418 case OUTPUT_BMP:
419 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
420 break;
421
422 case OUTPUT_EMF:
423 WriteEmf(page, name.c_str(), page_index);
424 break;
425#endif
426 case OUTPUT_PNG:
427 WritePng(name.c_str(), page_index, buffer, stride, width, height);
428 break;
429
430 case OUTPUT_PPM:
431 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
432 break;
433
434 default:
435 break;
436 }
437
438 FPDFBitmap_Destroy(bitmap);
439 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
440 FORM_OnBeforeClosePage(page, form);
441 FPDFText_ClosePage(text_page);
442 FPDF_ClosePage(page);
Jun Fangb553bcb2015-11-10 18:49:04 +0800443 return true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800444}
445
Tom Sepez5ee12d72014-12-17 16:24:01 -0800446void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800447 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800448 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449
450 IPDF_JSPLATFORM platform_callbacks;
451 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700452 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800453 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800454 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800455 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800456 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700457
458 FPDF_FORMFILLINFO form_callbacks;
459 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezc46d0002015-11-30 15:46:36 -0800460#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800461 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800462#else // PDF_ENABLE_XFA
463 form_callbacks.version = 1;
464#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700465 form_callbacks.m_pJsPlatform = &platform_callbacks;
466
467 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700468 FPDF_FILEACCESS file_access;
469 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700470 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700471 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472 file_access.m_Param = &loader;
473
474 FX_FILEAVAIL file_avail;
475 memset(&file_avail, '\0', sizeof(file_avail));
476 file_avail.version = 1;
477 file_avail.IsDataAvail = Is_Data_Avail;
478
479 FX_DOWNLOADHINTS hints;
480 memset(&hints, '\0', sizeof(hints));
481 hints.version = 1;
482 hints.AddSegment = Add_Segment;
483
484 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800485 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800486 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700487 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800488
Jun Fangdf7f3662015-11-10 18:29:18 +0800489 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
490 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700491 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800492 if (doc) {
493 while (nRet == PDF_DATA_NOTAVAIL) {
494 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
495 }
496 if (nRet == PDF_DATA_ERROR) {
497 fprintf(stderr, "Unknown error in checking if doc was available.\n");
498 return;
499 }
500 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
501 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
502 fprintf(stderr,
503 "Error %d was returned in checking if form was available.\n",
504 nRet);
505 return;
506 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800507 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800508 }
509 } else {
510 fprintf(stderr, "Non-linearized path...\n");
Lei Zhang600d4072015-09-23 15:35:25 -0700511 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800512 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700513
Lei Zhang5377ebf2015-09-23 14:52:53 -0700514 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400515 unsigned long err = FPDF_GetLastError();
516 fprintf(stderr, "Load pdf docs unsuccessful: ");
517 switch (err) {
518 case FPDF_ERR_SUCCESS:
519 fprintf(stderr, "Success");
520 break;
521 case FPDF_ERR_UNKNOWN:
522 fprintf(stderr, "Unknown error");
523 break;
524 case FPDF_ERR_FILE:
525 fprintf(stderr, "File not found or could not be opened");
526 break;
527 case FPDF_ERR_FORMAT:
528 fprintf(stderr, "File not in PDF format or corrupted");
529 break;
530 case FPDF_ERR_PASSWORD:
531 fprintf(stderr, "Password required or incorrect password");
532 break;
533 case FPDF_ERR_SECURITY:
534 fprintf(stderr, "Unsupported security scheme");
535 break;
536 case FPDF_ERR_PAGE:
537 fprintf(stderr, "Page not found or content error");
538 break;
539 default:
540 fprintf(stderr, "Unknown error %ld", err);
541 }
542 fprintf(stderr, ".\n");
543
Qin Zhao05224972015-10-20 18:31:06 -0400544 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800545 return;
546 }
547
Lei Zhang5377ebf2015-09-23 14:52:53 -0700548 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700549
Bo Xu2b7a49d2014-11-14 17:40:50 -0800550 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
Tom Sepezc46d0002015-11-30 15:46:36 -0800551#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800552 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700553 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
554 !FPDF_LoadXFA(doc)) {
555 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800556 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800557#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700558 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
559 FPDF_SetFormFieldHighlightAlpha(form, 100);
560
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700561 FORM_DoDocumentJSAction(form);
562 FORM_DoDocumentOpenAction(form);
563
Jun Fangdf7f3662015-11-10 18:29:18 +0800564 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700565 int rendered_pages = 0;
566 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700567 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800568 if (bIsLinearized) {
569 nRet = PDF_DATA_NOTAVAIL;
570 while (nRet == PDF_DATA_NOTAVAIL) {
571 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
572 }
573 if (nRet == PDF_DATA_ERROR) {
574 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
575 i);
576 return;
577 }
578 }
579 if (RenderPage(name, doc, form, i, options)) {
580 ++rendered_pages;
581 } else {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700582 ++bad_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700583 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700584 }
585
586 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700587
Tom Sepezc46d0002015-11-30 15:46:36 -0800588#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700589 // Note: The shut down order here is the reverse of the non-XFA branch order.
590 // Need to work out if this is required, and if it is, the lifetimes of
591 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700592 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800593 FPDFDOC_ExitFormFillEnvironment(form);
Tom Sepezc46d0002015-11-30 15:46:36 -0800594#else // PDF_ENABLE_XFA
595 FPDFDOC_ExitFormFillEnvironment(form);
596 FPDF_CloseDocument(doc);
597#endif // PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700598
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700599 FPDFAvail_Destroy(pdf_avail);
600
Tom Sepez1ed8a212015-05-11 15:25:39 -0700601 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
602 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603}
604
Tom Sepez2991d8d2016-01-15 16:02:48 -0800605static void ShowConfig() {
606 std::string config;
607 std::string maybe_comma;
608#if PDF_ENABLE_V8
609 config.append(maybe_comma);
610 config.append("V8");
611 maybe_comma = ",";
612#endif // PDF_ENABLE_V8
613#ifdef V8_USE_EXTERNAL_STARTUP_DATA
614 config.append(maybe_comma);
615 config.append("V8_EXTERNAL");
616 maybe_comma = ",";
617#endif // V8_USE_EXTERNAL_STARTUP_DATA
618#ifdef PDF_ENABLE_XFA
619 config.append(maybe_comma);
620 config.append("XFA");
621 maybe_comma = ",";
622#endif // PDF_ENABLE_XFA
623 printf("%s\n", config.c_str());
624}
625
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800626static const char usage_string[] =
627 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800628 " --show-config - print build options and exit\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700629 " --bin-dir=<path> - override path to v8 external data\n"
630 " --font-dir=<path> - override path to external fonts\n"
631 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800632#ifdef _WIN32
633 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
634 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800635#endif // _WIN32
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800636 " --png - write page images <pdf-name>.<page-number>.png\n"
637 " --ppm - write page images <pdf-name>.<page-number>.ppm\n";
638
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800640 std::vector<std::string> args(argv, argv + argc);
641 Options options;
642 std::list<std::string> files;
643 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800644 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645 return 1;
646 }
647
Tom Sepez2991d8d2016-01-15 16:02:48 -0800648 if (options.show_config) {
649 ShowConfig();
650 return 0;
651 }
652
653 if (files.empty()) {
654 fprintf(stderr, "No input files.\n");
655 return 1;
656 }
657
Tom Sepez452b4f32015-10-13 09:27:27 -0700658#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700659 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800660#ifdef V8_USE_EXTERNAL_STARTUP_DATA
661 v8::StartupData natives;
662 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700663 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
664 &snapshot, &platform);
665#else // V8_USE_EXTERNAL_STARTUP_DATA
666 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800667#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700668#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800669
Tom Sepeza72e8e22015-10-07 10:17:53 -0700670 FPDF_LIBRARY_CONFIG config;
671 config.version = 2;
672 config.m_pUserFontPaths = nullptr;
673 config.m_pIsolate = nullptr;
674 config.m_v8EmbedderSlot = 0;
675
676 const char* path_array[2];
677 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700678 path_array[0] = options.font_directory.c_str();
679 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700680 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700681 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700682 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700683
684 UNSUPPORT_INFO unsuppored_info;
685 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
686 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800687 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700688
689 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
690
691 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800692 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700693 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800694 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800695 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
696 GetFileContents(filename.c_str(), &file_length);
697 if (file_contents)
698 RenderPdf(filename, file_contents.get(), file_length, options);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700699 }
700
701 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700702#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800703 v8::V8::ShutdownPlatform();
704 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700705#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700706
707 return 0;
708}