blob: 2ca31b08e9babbf6bfd69065981558611459bbe9 [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
Cary Clark399be5b2016-03-14 16:51:29 -040016#if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
17#define _SKIA_SUPPORT_
18#endif
19
Lei Zhangb4e7f302015-11-06 15:52:32 -080020#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080021#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080022#include "public/fpdf_ext.h"
23#include "public/fpdf_formfill.h"
24#include "public/fpdf_text.h"
25#include "public/fpdfview.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050026#include "samples/image_diff_png.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080027#include "testing/test_support.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070028
Tom Sepez452b4f32015-10-13 09:27:27 -070029#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080030#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070031#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080032#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
34#ifdef _WIN32
Tom Sepez5ee12d72014-12-17 16:24:01 -080035#define snprintf _snprintf
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036#endif
37
Cary Clark399be5b2016-03-14 16:51:29 -040038#ifdef PDF_ENABLE_SKIA
39#include "third_party/skia/include/core/SkPictureRecorder.h"
40#include "third_party/skia/include/core/SkStream.h"
41#endif
42
Vitaly Buka9e0177a2014-07-22 18:15:42 -070043enum OutputFormat {
44 OUTPUT_NONE,
45 OUTPUT_PPM,
Tom Sepezaf18cb32015-02-05 15:06:01 -080046 OUTPUT_PNG,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070047#ifdef _WIN32
48 OUTPUT_BMP,
49 OUTPUT_EMF,
50#endif
Cary Clark399be5b2016-03-14 16:51:29 -040051#ifdef PDF_ENABLE_SKIA
52 OUTPUT_SKP,
53#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070054};
55
Tom Sepez5ee12d72014-12-17 16:24:01 -080056struct Options {
Tom Sepez2991d8d2016-01-15 16:02:48 -080057 Options() : show_config(false), output_format(OUTPUT_NONE) {}
Tom Sepez5ee12d72014-12-17 16:24:01 -080058
Tom Sepez2991d8d2016-01-15 16:02:48 -080059 bool show_config;
Tom Sepez5ee12d72014-12-17 16:24:01 -080060 OutputFormat output_format;
Tom Sepezdaa2e842015-01-29 15:44:37 -080061 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -080062 std::string exe_path;
63 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -070064 std::string font_directory;
Tom Sepez5ee12d72014-12-17 16:24:01 -080065};
66
Tom Sepezaf18cb32015-02-05 15:06:01 -080067static bool CheckDimensions(int stride, int width, int height) {
68 if (stride < 0 || width < 0 || height < 0)
69 return false;
70 if (height > 0 && width > INT_MAX / height)
71 return false;
72 return true;
73}
74
Vitaly Buka9e0177a2014-07-22 18:15:42 -070075static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
76 int stride, int width, int height) {
77 const char* buffer = reinterpret_cast<const char*>(buffer_void);
78
Tom Sepezaf18cb32015-02-05 15:06:01 -080079 if (!CheckDimensions(stride, width, height))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -080081
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082 int out_len = width * height;
83 if (out_len > INT_MAX / 3)
84 return;
85 out_len *= 3;
86
87 char filename[256];
88 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070089 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090 if (!fp)
91 return;
92 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
93 // Source data is B, G, R, unused.
94 // Dest data is R, G, B.
95 char* result = new char[out_len];
Lei Zhange00660b2015-08-13 15:40:18 -070096 for (int h = 0; h < height; ++h) {
97 const char* src_line = buffer + (stride * h);
98 char* dest_line = result + (width * h * 3);
99 for (int w = 0; w < width; ++w) {
100 // R
101 dest_line[w * 3] = src_line[(w * 4) + 2];
102 // G
103 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
104 // B
105 dest_line[(w * 3) + 2] = src_line[w * 4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107 }
Lei Zhange00660b2015-08-13 15:40:18 -0700108 fwrite(result, out_len, 1, fp);
109 delete[] result;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110 fclose(fp);
111}
112
Tom Sepezaf18cb32015-02-05 15:06:01 -0800113static void WritePng(const char* pdf_name, int num, const void* buffer_void,
114 int stride, int width, int height) {
115 if (!CheckDimensions(stride, width, height))
116 return;
117
118 std::vector<unsigned char> png_encoding;
119 const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
120 if (!image_diff_png::EncodeBGRAPNG(
121 buffer, width, height, stride, false, &png_encoding)) {
122 fprintf(stderr, "Failed to convert bitmap to PNG\n");
123 return;
124 }
125
126 char filename[256];
127 int chars_formatted = snprintf(
128 filename, sizeof(filename), "%s.%d.png", pdf_name, num);
129 if (chars_formatted < 0 ||
130 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
Cary Clark399be5b2016-03-14 16:51:29 -0400131 fprintf(stderr, "Filename %s is too long\n", filename);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800132 return;
133 }
134
135 FILE* fp = fopen(filename, "wb");
136 if (!fp) {
137 fprintf(stderr, "Failed to open %s for output\n", filename);
138 return;
139 }
140
141 size_t bytes_written = fwrite(
142 &png_encoding.front(), 1, png_encoding.size(), fp);
143 if (bytes_written != png_encoding.size())
144 fprintf(stderr, "Failed to write to %s\n", filename);
145
Lei Zhang5377ebf2015-09-23 14:52:53 -0700146 (void)fclose(fp);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800147}
148
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700149#ifdef _WIN32
150static void WriteBmp(const char* pdf_name, int num, const void* buffer,
151 int stride, int width, int height) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800152 if (!CheckDimensions(stride, width, height))
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700153 return;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800154
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700155 int out_len = stride * height;
156 if (out_len > INT_MAX / 3)
157 return;
158
159 char filename[256];
160 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
161 FILE* fp = fopen(filename, "wb");
162 if (!fp)
163 return;
164
Nico Weber2827bdd2015-07-01 14:08:08 -0700165 BITMAPINFO bmi = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700166 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
167 bmi.bmiHeader.biWidth = width;
168 bmi.bmiHeader.biHeight = -height; // top-down image
169 bmi.bmiHeader.biPlanes = 1;
170 bmi.bmiHeader.biBitCount = 32;
171 bmi.bmiHeader.biCompression = BI_RGB;
172 bmi.bmiHeader.biSizeImage = 0;
173
Nico Weber2827bdd2015-07-01 14:08:08 -0700174 BITMAPFILEHEADER file_header = {};
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700175 file_header.bfType = 0x4d42;
176 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
177 file_header.bfOffBits = file_header.bfSize - out_len;
178
179 fwrite(&file_header, sizeof(file_header), 1, fp);
180 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
181 fwrite(buffer, out_len, 1, fp);
182 fclose(fp);
183}
184
185void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
186 int width = static_cast<int>(FPDF_GetPageWidth(page));
187 int height = static_cast<int>(FPDF_GetPageHeight(page));
188
189 char filename[256];
190 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
191
Lei Zhang5377ebf2015-09-23 14:52:53 -0700192 HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
Tom Sepezaf18cb32015-02-05 15:06:01 -0800193
194 HRGN rgn = CreateRectRgn(0, 0, width, height);
195 SelectClipRgn(dc, rgn);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700196 DeleteObject(rgn);
197
198 SelectObject(dc, GetStockObject(NULL_PEN));
199 SelectObject(dc, GetStockObject(WHITE_BRUSH));
200 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
201 Rectangle(dc, 0, 0, width + 1, height + 1);
202
203 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
204 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
205
206 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
207}
208#endif
209
Cary Clark399be5b2016-03-14 16:51:29 -0400210#ifdef PDF_ENABLE_SKIA
211void WriteSkp(const char* pdf_name, int num, const void* recorder) {
212 char filename[256];
213 int chars_formatted =
214 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
215
216 if (chars_formatted < 0 ||
217 static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
218 fprintf(stderr, "Filename %s is too long\n", filename);
219 return;
220 }
221
222 SkPictureRecorder* r = (SkPictureRecorder*)recorder;
223 SkPicture* picture = r->endRecordingAsPicture();
224 SkFILEWStream wStream(filename);
225 picture->serialize(&wStream);
226}
227#endif
228
Tom Sepez58fb36a2016-02-01 10:32:14 -0800229// These example JS platform callback handlers are entirely optional,
230// and exist here to show the flow of information from a document back
231// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800232int ExampleAppAlert(IPDF_JSPLATFORM*,
233 FPDF_WIDESTRING msg,
234 FPDF_WIDESTRING title,
235 int nType,
236 int nIcon) {
237 printf("%ls", GetPlatformWString(title).c_str());
238 if (nIcon || nType)
239 printf("[icon=%d,type=%d]", nIcon, nType);
240 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241 return 0;
242}
243
Tom Sepez58fb36a2016-02-01 10:32:14 -0800244int ExampleAppResponse(IPDF_JSPLATFORM*,
245 FPDF_WIDESTRING question,
246 FPDF_WIDESTRING title,
247 FPDF_WIDESTRING defaultValue,
248 FPDF_WIDESTRING label,
249 FPDF_BOOL isPassword,
250 void* response,
251 int length) {
252 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
253 GetPlatformWString(title).c_str(),
254 GetPlatformWString(question).c_str(),
255 GetPlatformWString(defaultValue).c_str(),
256 GetPlatformWString(label).c_str(), isPassword, length);
257
258 // UTF-16, always LE regardless of platform.
259 uint8_t* ptr = static_cast<uint8_t*>(response);
260 ptr[0] = 'N';
261 ptr[1] = 0;
262 ptr[2] = 'o';
263 ptr[3] = 0;
264 return 4;
265}
266
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800267void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
268 printf("Goto Page: %d\n", pageNumber);
269}
270
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800271void ExampleDocMail(IPDF_JSPLATFORM*,
272 void* mailData,
273 int length,
274 FPDF_BOOL bUI,
275 FPDF_WIDESTRING To,
276 FPDF_WIDESTRING Subject,
277 FPDF_WIDESTRING CC,
278 FPDF_WIDESTRING BCC,
279 FPDF_WIDESTRING Msg) {
280 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", bUI,
281 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
282 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
283 GetPlatformWString(Msg).c_str());
284}
285
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800286void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287 std::string feature = "Unknown";
288 switch (type) {
289 case FPDF_UNSP_DOC_XFAFORM:
290 feature = "XFA";
291 break;
292 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
293 feature = "Portfolios_Packages";
294 break;
295 case FPDF_UNSP_DOC_ATTACHMENT:
296 case FPDF_UNSP_ANNOT_ATTACHMENT:
297 feature = "Attachment";
298 break;
299 case FPDF_UNSP_DOC_SECURITY:
300 feature = "Rights_Management";
301 break;
302 case FPDF_UNSP_DOC_SHAREDREVIEW:
303 feature = "Shared_Review";
304 break;
305 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
306 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
307 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
308 feature = "Shared_Form";
309 break;
310 case FPDF_UNSP_ANNOT_3DANNOT:
311 feature = "3D";
312 break;
313 case FPDF_UNSP_ANNOT_MOVIE:
314 feature = "Movie";
315 break;
316 case FPDF_UNSP_ANNOT_SOUND:
317 feature = "Sound";
318 break;
319 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
320 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
321 feature = "Screen";
322 break;
323 case FPDF_UNSP_ANNOT_SIG:
324 feature = "Digital_Signature";
325 break;
326 }
327 printf("Unsupported feature: %s.\n", feature.c_str());
328}
329
Tom Sepez5ee12d72014-12-17 16:24:01 -0800330bool ParseCommandLine(const std::vector<std::string>& args,
331 Options* options, std::list<std::string>* files) {
332 if (args.empty()) {
333 return false;
334 }
335 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800336 size_t cur_idx = 1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800337 for (; cur_idx < args.size(); ++cur_idx) {
338 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800339 if (cur_arg == "--show-config") {
340 options->show_config = true;
341 } else if (cur_arg == "--ppm") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800342 if (options->output_format != OUTPUT_NONE) {
343 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
344 return false;
345 }
346 options->output_format = OUTPUT_PPM;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800347 } else if (cur_arg == "--png") {
348 if (options->output_format != OUTPUT_NONE) {
349 fprintf(stderr, "Duplicate or conflicting --png argument\n");
350 return false;
351 }
352 options->output_format = OUTPUT_PNG;
Cary Clark399be5b2016-03-14 16:51:29 -0400353#ifdef PDF_ENABLE_SKIA
354 } else if (cur_arg == "--skp") {
355 if (options->output_format != OUTPUT_NONE) {
356 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
357 return false;
358 }
359 options->output_format = OUTPUT_SKP;
360#endif
Lei Zhang6f62d532015-09-23 15:31:44 -0700361 } else if (cur_arg.size() > 11 &&
362 cur_arg.compare(0, 11, "--font-dir=") == 0) {
363 if (!options->font_directory.empty()) {
364 fprintf(stderr, "Duplicate --font-dir argument\n");
365 return false;
366 }
367 options->font_directory = cur_arg.substr(11);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700368#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500369 } else if (cur_arg == "--emf") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800370 if (options->output_format != OUTPUT_NONE) {
371 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
372 return false;
373 }
374 options->output_format = OUTPUT_EMF;
Dan Sinclair50cce602016-02-24 09:51:16 -0500375 } else if (cur_arg == "--bmp") {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800376 if (options->output_format != OUTPUT_NONE) {
377 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
378 return false;
379 }
380 options->output_format = OUTPUT_BMP;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800381#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500382
Tom Sepez452b4f32015-10-13 09:27:27 -0700383#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800384#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Dan Sinclair738b08c2016-03-01 14:45:20 -0500385 } else if (cur_arg.size() > 10 &&
386 cur_arg.compare(0, 10, "--bin-dir=") == 0) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800387 if (!options->bin_directory.empty()) {
388 fprintf(stderr, "Duplicate --bin-dir argument\n");
389 return false;
390 }
391 options->bin_directory = cur_arg.substr(10);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800392#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700393#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500394
395 } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800396 if (!options->scale_factor_as_string.empty()) {
397 fprintf(stderr, "Duplicate --scale argument\n");
398 return false;
399 }
400 options->scale_factor_as_string = cur_arg.substr(8);
Tom Sepez2991d8d2016-01-15 16:02:48 -0800401 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
402 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
403 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500404 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700405 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500406 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407 }
Bo Xud44e3922014-12-19 02:27:25 -0800408 for (size_t i = cur_idx; i < args.size(); i++) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800409 files->push_back(args[i]);
410 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411 return true;
412}
413
Tom Sepezcf22eb82015-05-12 17:28:08 -0700414FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700415 return true;
416}
417
418void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
419}
420
Jun Fangb553bcb2015-11-10 18:49:04 +0800421bool RenderPage(const std::string& name,
422 const FPDF_DOCUMENT& doc,
423 const FPDF_FORMHANDLE& form,
424 const int page_index,
425 const Options& options) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800426 FPDF_PAGE page = FPDF_LoadPage(doc, page_index);
427 if (!page) {
Jun Fangb553bcb2015-11-10 18:49:04 +0800428 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800429 }
430 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
431 FORM_OnAfterLoadPage(page, form);
432 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
433
434 double scale = 1.0;
435 if (!options.scale_factor_as_string.empty()) {
436 std::stringstream(options.scale_factor_as_string) >> scale;
437 }
438 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
439 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
Lei Zhang453d96b2015-12-31 13:13:10 -0800440 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
441 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha);
Jun Fangdf7f3662015-11-10 18:29:18 +0800442 if (!bitmap) {
443 fprintf(stderr, "Page was too large to be rendered.\n");
Jun Fangb553bcb2015-11-10 18:49:04 +0800444 return false;
Jun Fangdf7f3662015-11-10 18:29:18 +0800445 }
Lei Zhang453d96b2015-12-31 13:13:10 -0800446 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
447 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, fill_color);
Jun Fangdf7f3662015-11-10 18:29:18 +0800448 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
449
450 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
451 int stride = FPDFBitmap_GetStride(bitmap);
452 const char* buffer =
453 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
454
455 switch (options.output_format) {
456#ifdef _WIN32
457 case OUTPUT_BMP:
458 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
459 break;
460
461 case OUTPUT_EMF:
462 WriteEmf(page, name.c_str(), page_index);
463 break;
464#endif
465 case OUTPUT_PNG:
466 WritePng(name.c_str(), page_index, buffer, stride, width, height);
467 break;
468
469 case OUTPUT_PPM:
470 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
471 break;
472
Cary Clark399be5b2016-03-14 16:51:29 -0400473#ifdef PDF_ENABLE_SKIA
474 case OUTPUT_SKP: {
475 std::unique_ptr<SkPictureRecorder> recorder(
476 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height));
477 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
478 WriteSkp(name.c_str(), page_index, recorder.get());
479 } break;
480#endif
Jun Fangdf7f3662015-11-10 18:29:18 +0800481 default:
482 break;
483 }
484
485 FPDFBitmap_Destroy(bitmap);
486 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
487 FORM_OnBeforeClosePage(page, form);
488 FPDFText_ClosePage(text_page);
489 FPDF_ClosePage(page);
Jun Fangb553bcb2015-11-10 18:49:04 +0800490 return true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800491}
492
Tom Sepez5ee12d72014-12-17 16:24:01 -0800493void RenderPdf(const std::string& name, const char* pBuf, size_t len,
Tom Sepezdaa2e842015-01-29 15:44:37 -0800494 const Options& options) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800495 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700496
497 IPDF_JSPLATFORM platform_callbacks;
498 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Tom Sepeza72e8e22015-10-07 10:17:53 -0700499 platform_callbacks.version = 3;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800500 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepez58fb36a2016-02-01 10:32:14 -0800501 platform_callbacks.app_response = ExampleAppResponse;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800502 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800503 platform_callbacks.Doc_mail = ExampleDocMail;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504
505 FPDF_FORMFILLINFO form_callbacks;
506 memset(&form_callbacks, '\0', sizeof(form_callbacks));
Tom Sepezc46d0002015-11-30 15:46:36 -0800507#ifdef PDF_ENABLE_XFA
Tom Sepezed631382014-11-18 14:10:25 -0800508 form_callbacks.version = 2;
Tom Sepezc46d0002015-11-30 15:46:36 -0800509#else // PDF_ENABLE_XFA
510 form_callbacks.version = 1;
511#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700512 form_callbacks.m_pJsPlatform = &platform_callbacks;
513
514 TestLoader loader(pBuf, len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515 FPDF_FILEACCESS file_access;
516 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700517 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700518 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700519 file_access.m_Param = &loader;
520
521 FX_FILEAVAIL file_avail;
522 memset(&file_avail, '\0', sizeof(file_avail));
523 file_avail.version = 1;
524 file_avail.IsDataAvail = Is_Data_Avail;
525
526 FX_DOWNLOADHINTS hints;
527 memset(&hints, '\0', sizeof(hints));
528 hints.version = 1;
529 hints.AddSegment = Add_Segment;
530
531 FPDF_DOCUMENT doc;
Jun Fangdf7f3662015-11-10 18:29:18 +0800532 int nRet = PDF_DATA_NOTAVAIL;
Jun Fangb553bcb2015-11-10 18:49:04 +0800533 bool bIsLinearized = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700534 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
Tom Sepezc98895c2015-11-24 15:30:36 -0800535
Jun Fangdf7f3662015-11-10 18:29:18 +0800536 if (FPDFAvail_IsLinearized(pdf_avail) == PDF_LINEARIZED) {
537 fprintf(stderr, "Linearized path...\n");
Lei Zhang5377ebf2015-09-23 14:52:53 -0700538 doc = FPDFAvail_GetDocument(pdf_avail, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800539 if (doc) {
540 while (nRet == PDF_DATA_NOTAVAIL) {
541 nRet = FPDFAvail_IsDocAvail(pdf_avail, &hints);
542 }
543 if (nRet == PDF_DATA_ERROR) {
544 fprintf(stderr, "Unknown error in checking if doc was available.\n");
545 return;
546 }
547 nRet = FPDFAvail_IsFormAvail(pdf_avail, &hints);
548 if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
549 fprintf(stderr,
550 "Error %d was returned in checking if form was available.\n",
551 nRet);
552 return;
553 }
Jun Fangb553bcb2015-11-10 18:49:04 +0800554 bIsLinearized = true;
Jun Fangdf7f3662015-11-10 18:29:18 +0800555 }
556 } else {
557 fprintf(stderr, "Non-linearized path...\n");
Lei Zhang600d4072015-09-23 15:35:25 -0700558 doc = FPDF_LoadCustomDocument(&file_access, nullptr);
Jun Fangdf7f3662015-11-10 18:29:18 +0800559 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700560
Lei Zhang5377ebf2015-09-23 14:52:53 -0700561 if (!doc) {
Dan Sinclaireb815bf2015-10-27 13:08:41 -0400562 unsigned long err = FPDF_GetLastError();
563 fprintf(stderr, "Load pdf docs unsuccessful: ");
564 switch (err) {
565 case FPDF_ERR_SUCCESS:
566 fprintf(stderr, "Success");
567 break;
568 case FPDF_ERR_UNKNOWN:
569 fprintf(stderr, "Unknown error");
570 break;
571 case FPDF_ERR_FILE:
572 fprintf(stderr, "File not found or could not be opened");
573 break;
574 case FPDF_ERR_FORMAT:
575 fprintf(stderr, "File not in PDF format or corrupted");
576 break;
577 case FPDF_ERR_PASSWORD:
578 fprintf(stderr, "Password required or incorrect password");
579 break;
580 case FPDF_ERR_SECURITY:
581 fprintf(stderr, "Unsupported security scheme");
582 break;
583 case FPDF_ERR_PAGE:
584 fprintf(stderr, "Page not found or content error");
585 break;
586 default:
587 fprintf(stderr, "Unknown error %ld", err);
588 }
589 fprintf(stderr, ".\n");
590
Qin Zhao05224972015-10-20 18:31:06 -0400591 FPDFAvail_Destroy(pdf_avail);
JUN FANG827a1722015-03-05 13:39:21 -0800592 return;
593 }
594
Lei Zhang5377ebf2015-09-23 14:52:53 -0700595 (void)FPDF_GetDocPermissions(doc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700596
Bo Xu2b7a49d2014-11-14 17:40:50 -0800597 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
Tom Sepezc46d0002015-11-30 15:46:36 -0800598#ifdef PDF_ENABLE_XFA
JUN FANG827a1722015-03-05 13:39:21 -0800599 int docType = DOCTYPE_PDF;
Lei Zhang5377ebf2015-09-23 14:52:53 -0700600 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
601 !FPDF_LoadXFA(doc)) {
602 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
Tom Sepez56451382014-12-05 13:30:51 -0800603 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800604#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700605 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
606 FPDF_SetFormFieldHighlightAlpha(form, 100);
607
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700608 FORM_DoDocumentJSAction(form);
609 FORM_DoDocumentOpenAction(form);
610
Jun Fangdf7f3662015-11-10 18:29:18 +0800611 int page_count = FPDF_GetPageCount(doc);
Tom Sepez1ed8a212015-05-11 15:25:39 -0700612 int rendered_pages = 0;
613 int bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700614 for (int i = 0; i < page_count; ++i) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800615 if (bIsLinearized) {
616 nRet = PDF_DATA_NOTAVAIL;
617 while (nRet == PDF_DATA_NOTAVAIL) {
618 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
619 }
620 if (nRet == PDF_DATA_ERROR) {
621 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
622 i);
623 return;
624 }
625 }
626 if (RenderPage(name, doc, form, i, options)) {
627 ++rendered_pages;
628 } else {
Lei Zhang5377ebf2015-09-23 14:52:53 -0700629 ++bad_pages;
Jun Fangaeacba42014-08-22 17:04:29 -0700630 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700631 }
632
633 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
Lei Zhangba026912015-07-16 10:06:11 -0700634
Tom Sepezc46d0002015-11-30 15:46:36 -0800635#ifdef PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700636 // Note: The shut down order here is the reverse of the non-XFA branch order.
637 // Need to work out if this is required, and if it is, the lifetimes of
638 // objects owned by |doc| that |form| reference.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700639 FPDF_CloseDocument(doc);
Bo Xu2b7a49d2014-11-14 17:40:50 -0800640 FPDFDOC_ExitFormFillEnvironment(form);
Tom Sepezc46d0002015-11-30 15:46:36 -0800641#else // PDF_ENABLE_XFA
642 FPDFDOC_ExitFormFillEnvironment(form);
643 FPDF_CloseDocument(doc);
644#endif // PDF_ENABLE_XFA
Lei Zhangba026912015-07-16 10:06:11 -0700645
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700646 FPDFAvail_Destroy(pdf_avail);
647
Tom Sepez1ed8a212015-05-11 15:25:39 -0700648 fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
649 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700650}
651
Tom Sepez2991d8d2016-01-15 16:02:48 -0800652static void ShowConfig() {
653 std::string config;
654 std::string maybe_comma;
655#if PDF_ENABLE_V8
656 config.append(maybe_comma);
657 config.append("V8");
658 maybe_comma = ",";
659#endif // PDF_ENABLE_V8
660#ifdef V8_USE_EXTERNAL_STARTUP_DATA
661 config.append(maybe_comma);
662 config.append("V8_EXTERNAL");
663 maybe_comma = ",";
664#endif // V8_USE_EXTERNAL_STARTUP_DATA
665#ifdef PDF_ENABLE_XFA
666 config.append(maybe_comma);
667 config.append("XFA");
668 maybe_comma = ",";
669#endif // PDF_ENABLE_XFA
670 printf("%s\n", config.c_str());
671}
672
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800673static const char usage_string[] =
674 "Usage: pdfium_test [OPTION] [FILE]...\n"
Tom Sepez2991d8d2016-01-15 16:02:48 -0800675 " --show-config - print build options and exit\n"
Lei Zhang6f62d532015-09-23 15:31:44 -0700676 " --bin-dir=<path> - override path to v8 external data\n"
677 " --font-dir=<path> - override path to external fonts\n"
678 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800679#ifdef _WIN32
680 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
681 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
Tom Sepezc46d0002015-11-30 15:46:36 -0800682#endif // _WIN32
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800683 " --png - write page images <pdf-name>.<page-number>.png\n"
Cary Clark399be5b2016-03-14 16:51:29 -0400684#ifdef PDF_ENABLE_SKIA
685 " --skp - write page images <pdf-name>.<page-number>.skp\n"
686#endif
687 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800688
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700689int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800690 std::vector<std::string> args(argv, argv + argc);
691 Options options;
692 std::list<std::string> files;
693 if (!ParseCommandLine(args, &options, &files)) {
Tom Sepez23b4e3f2015-02-06 16:05:23 -0800694 fprintf(stderr, "%s", usage_string);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695 return 1;
696 }
697
Tom Sepez2991d8d2016-01-15 16:02:48 -0800698 if (options.show_config) {
699 ShowConfig();
700 return 0;
701 }
702
703 if (files.empty()) {
704 fprintf(stderr, "No input files.\n");
705 return 1;
706 }
707
Tom Sepez452b4f32015-10-13 09:27:27 -0700708#ifdef PDF_ENABLE_V8
Tom Sepezd831dc72015-10-19 16:04:22 -0700709 v8::Platform* platform;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800710#ifdef V8_USE_EXTERNAL_STARTUP_DATA
711 v8::StartupData natives;
712 v8::StartupData snapshot;
Tom Sepezd831dc72015-10-19 16:04:22 -0700713 InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
714 &snapshot, &platform);
715#else // V8_USE_EXTERNAL_STARTUP_DATA
716 InitializeV8ForPDFium(&platform);
Tom Sepez5ee12d72014-12-17 16:24:01 -0800717#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700718#endif // PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800719
Tom Sepeza72e8e22015-10-07 10:17:53 -0700720 FPDF_LIBRARY_CONFIG config;
721 config.version = 2;
722 config.m_pUserFontPaths = nullptr;
723 config.m_pIsolate = nullptr;
724 config.m_v8EmbedderSlot = 0;
725
726 const char* path_array[2];
727 if (!options.font_directory.empty()) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700728 path_array[0] = options.font_directory.c_str();
729 path_array[1] = nullptr;
Lei Zhang6f62d532015-09-23 15:31:44 -0700730 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -0700731 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700732 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700733
734 UNSUPPORT_INFO unsuppored_info;
735 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
736 unsuppored_info.version = 1;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800737 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700738
739 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
740
741 while (!files.empty()) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800742 std::string filename = files.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700743 files.pop_front();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800744 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -0800745 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
746 GetFileContents(filename.c_str(), &file_length);
747 if (file_contents)
748 RenderPdf(filename, file_contents.get(), file_length, options);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700749 }
750
751 FPDF_DestroyLibrary();
Tom Sepez452b4f32015-10-13 09:27:27 -0700752#ifdef PDF_ENABLE_V8
John Abd-El-Malekb045ed22015-02-10 09:15:12 -0800753 v8::V8::ShutdownPlatform();
754 delete platform;
Tom Sepez452b4f32015-10-13 09:27:27 -0700755#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700756
757 return 0;
758}