blob: 91b4f905d0b6abc468b0168d4352275f15bed27a [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8
Lei Zhang9a25ede2017-06-22 00:05:12 -07009#include <iterator>
tonikitoo3e981582016-08-26 08:37:10 -070010#include <map>
Tom Sepezbfa2a972017-07-24 11:38:31 -070011#include <memory>
Tom Sepezdaa2e842015-01-29 15:44:37 -080012#include <sstream>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013#include <string>
Tom Sepez5ee12d72014-12-17 16:24:01 -080014#include <vector>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Lei Zhang4facbc92020-07-30 16:49:26 +000016#if defined(PDF_ENABLE_SKIA) && !defined(_SKIA_SUPPORT_)
Cary Clark399be5b2016-03-14 16:51:29 -040017#define _SKIA_SUPPORT_
18#endif
19
Lei Zhang4facbc92020-07-30 16:49:26 +000020#if defined(PDF_ENABLE_SKIA_PATHS) && !defined(_SKIA_SUPPORT_PATHS_)
Lei Zhang2cb8f852019-09-16 19:39:16 +000021#define _SKIA_SUPPORT_PATHS_
22#endif
23
Tom Sepeze08d2b12018-04-25 18:49:32 +000024#include "public/cpp/fpdf_scopers.h"
Jane Liu4fd9a472017-06-01 18:56:09 -040025#include "public/fpdf_annot.h"
Jane Liu4442d452017-07-19 10:19:42 -040026#include "public/fpdf_attachment.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080027#include "public/fpdf_dataavail.h"
Lei Zhang453d96b2015-12-31 13:13:10 -080028#include "public/fpdf_edit.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080029#include "public/fpdf_ext.h"
30#include "public/fpdf_formfill.h"
Dan Sinclairaeadad12017-07-18 16:43:41 -040031#include "public/fpdf_progressive.h"
Dan Sinclairddcb6e72017-04-05 10:30:33 -040032#include "public/fpdf_structtree.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080033#include "public/fpdf_text.h"
34#include "public/fpdfview.h"
Dan Sinclaircb876822018-03-22 16:21:04 +000035#include "samples/pdfium_test_dump_helper.h"
36#include "samples/pdfium_test_event_helper.h"
37#include "samples/pdfium_test_write_helper.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000038#include "testing/fx_string_testhelpers.h"
Lei Zhangd50bdff2019-02-05 19:42:33 +000039#include "testing/test_loader.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000040#include "testing/utils/file_util.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000041#include "testing/utils/hash.h"
Ryan Harrisondd8da5e2018-05-17 20:43:42 +000042#include "testing/utils/path_service.h"
Ryan Harrison16adb3e2018-04-04 17:44:31 +000043#include "third_party/base/optional.h"
Tom Sepezd831dc72015-10-19 16:04:22 -070044
thestigf2b940c2016-10-13 06:48:47 -070045#ifdef _WIN32
46#include <io.h>
47#else
48#include <unistd.h>
49#endif
50
Henrique Nakashima95ea7782017-07-11 16:42:43 -040051#ifdef ENABLE_CALLGRIND
52#include <valgrind/callgrind.h>
53#endif // ENABLE_CALLGRIND
54
Tom Sepez452b4f32015-10-13 09:27:27 -070055#ifdef PDF_ENABLE_V8
Lei Zhang72e8b692019-02-05 19:16:52 +000056#include "testing/v8_initializer.h"
John Abd-El-Malekb045ed22015-02-10 09:15:12 -080057#include "v8/include/libplatform/libplatform.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -070058#include "v8/include/v8.h"
Lei Zhang8241df72015-11-06 14:38:48 -080059#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
thestigf2b940c2016-10-13 06:48:47 -070061#ifdef _WIN32
62#define access _access
63#define snprintf _snprintf
64#define R_OK 4
65#endif
66
Lei Zhang8ecff962020-07-30 21:10:42 +000067// wordexp is a POSIX function that is only available on macOS and non-Android
Ryan Harrison16adb3e2018-04-04 17:44:31 +000068// Linux platforms.
69#if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
70#define WORDEXP_AVAILABLE
71#endif
72
73#ifdef WORDEXP_AVAILABLE
74#include <wordexp.h>
75#endif // WORDEXP_AVAILABLE
76
Lei Zhang38235eb2020-07-28 06:38:37 +000077enum class OutputFormat {
78 kNone,
79 kPageInfo,
80 kStructure,
81 kText,
82 kPpm,
83 kPng,
84 kAnnot,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070085#ifdef _WIN32
Lei Zhang38235eb2020-07-28 06:38:37 +000086 kBmp,
87 kEmf,
88 kPs2,
89 kPs3,
Vitaly Buka9e0177a2014-07-22 18:15:42 -070090#endif
Cary Clark399be5b2016-03-14 16:51:29 -040091#ifdef PDF_ENABLE_SKIA
Lei Zhang38235eb2020-07-28 06:38:37 +000092 kSkp,
Cary Clark399be5b2016-03-14 16:51:29 -040093#endif
Vitaly Buka9e0177a2014-07-22 18:15:42 -070094};
95
Lei Zhangd2be6462017-07-21 14:31:21 -070096namespace {
97
Tom Sepez5ee12d72014-12-17 16:24:01 -080098struct Options {
Tom Sepez0784c732018-04-23 18:02:57 +000099 Options() = default;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800100
Tom Sepez0784c732018-04-23 18:02:57 +0000101 bool show_config = false;
102 bool show_metadata = false;
103 bool send_events = false;
Lei Zhang793c1352019-07-12 17:35:57 +0000104 bool use_load_mem_document = false;
Tom Sepez0784c732018-04-23 18:02:57 +0000105 bool render_oneshot = false;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000106 bool lcd_text = false;
107 bool no_nativetext = false;
108 bool grayscale = false;
Gourab Kundud35a62d2020-04-24 10:29:27 +0000109 bool forced_color = false;
110 bool fill_to_stroke = false;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000111 bool limit_cache = false;
112 bool force_halftone = false;
113 bool printing = false;
114 bool no_smoothtext = false;
115 bool no_smoothimage = false;
116 bool no_smoothpath = false;
Lei Zhangb82d9072019-11-20 21:20:57 +0000117 bool reverse_byte_order = false;
Tom Sepez0784c732018-04-23 18:02:57 +0000118 bool save_attachments = false;
119 bool save_images = false;
Lei Zhang0ac7b4e2020-07-18 00:35:53 +0000120 bool save_rendered_images = false;
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +0000121 bool save_thumbnails = false;
122 bool save_thumbnails_decoded = false;
123 bool save_thumbnails_raw = false;
Tom Sepez0784c732018-04-23 18:02:57 +0000124#ifdef PDF_ENABLE_V8
125 bool disable_javascript = false;
Tom Sepeze0d3c502020-08-11 23:51:10 +0000126 std::string js_flags; // Extra flags to pass to v8 init.
Tom Sepez0784c732018-04-23 18:02:57 +0000127#ifdef PDF_ENABLE_XFA
128 bool disable_xfa = false;
129#endif // PDF_ENABLE_XFA
130#endif // PDF_ENABLE_V8
131 bool pages = false;
132 bool md5 = false;
Henrique Nakashima95ea7782017-07-11 16:42:43 -0400133#ifdef ENABLE_CALLGRIND
Tom Sepez0784c732018-04-23 18:02:57 +0000134 bool callgrind_delimiters = false;
Lei Zhangd0f989e2019-04-26 18:03:13 +0000135#endif
136#if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
137 bool linux_no_system_fonts = false;
138#endif
Lei Zhang38235eb2020-07-28 06:38:37 +0000139 OutputFormat output_format = OutputFormat::kNone;
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000140 std::string password;
Tom Sepezdaa2e842015-01-29 15:44:37 -0800141 std::string scale_factor_as_string;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800142 std::string exe_path;
143 std::string bin_directory;
Lei Zhang6f62d532015-09-23 15:31:44 -0700144 std::string font_directory;
Tom Sepez0784c732018-04-23 18:02:57 +0000145 int first_page = 0; // First 0-based page number to renderer.
146 int last_page = 0; // Last 0-based page number to renderer.
Ryan Harrison70cca362018-08-10 18:55:46 +0000147 time_t time = -1;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800148};
149
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000150int PageRenderFlagsFromOptions(const Options& options) {
151 int flags = FPDF_ANNOT;
152 if (options.lcd_text)
153 flags |= FPDF_LCD_TEXT;
154 if (options.no_nativetext)
155 flags |= FPDF_NO_NATIVETEXT;
156 if (options.grayscale)
157 flags |= FPDF_GRAYSCALE;
Gourab Kundud35a62d2020-04-24 10:29:27 +0000158 if (options.fill_to_stroke)
159 flags |= FPDF_CONVERT_FILL_TO_STROKE;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000160 if (options.limit_cache)
161 flags |= FPDF_RENDER_LIMITEDIMAGECACHE;
162 if (options.force_halftone)
163 flags |= FPDF_RENDER_FORCEHALFTONE;
164 if (options.printing)
165 flags |= FPDF_PRINTING;
166 if (options.no_smoothtext)
167 flags |= FPDF_RENDER_NO_SMOOTHTEXT;
168 if (options.no_smoothimage)
169 flags |= FPDF_RENDER_NO_SMOOTHIMAGE;
170 if (options.no_smoothpath)
171 flags |= FPDF_RENDER_NO_SMOOTHPATH;
Lei Zhangb82d9072019-11-20 21:20:57 +0000172 if (options.reverse_byte_order)
173 flags |= FPDF_REVERSE_BYTE_ORDER;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000174 return flags;
175}
176
Ryan Harrison16adb3e2018-04-04 17:44:31 +0000177Optional<std::string> ExpandDirectoryPath(const std::string& path) {
178#if defined(WORDEXP_AVAILABLE)
179 wordexp_t expansion;
180 if (wordexp(path.c_str(), &expansion, 0) != 0 || expansion.we_wordc < 1) {
181 wordfree(&expansion);
182 return {};
183 }
184 // Need to contruct the return value before hand, since wordfree will
185 // deallocate |expansion|.
186 Optional<std::string> ret_val = {expansion.we_wordv[0]};
187 wordfree(&expansion);
188 return ret_val;
189#else
190 return {path};
191#endif // WORDEXP_AVAILABLE
192}
193
Lei Zhangd0f989e2019-04-26 18:03:13 +0000194Optional<const char*> GetCustomFontPath(const Options& options) {
195#if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
196 // Set custom font path to an empty path. This avoids the fallback to default
197 // font paths.
198 if (options.linux_no_system_fonts)
199 return nullptr;
200#endif
201
202 // No custom font path. Use default.
203 if (options.font_directory.empty())
204 return pdfium::nullopt;
205
206 // Set custom font path to |options.font_directory|.
207 return options.font_directory.c_str();
208}
209
Tom Sepez55865452018-08-27 20:18:04 +0000210struct FPDF_FORMFILLINFO_PDFiumTest final : public FPDF_FORMFILLINFO {
tonikitoo81d92f82016-09-21 12:44:56 -0700211 // Hold a map of the currently loaded pages in order to avoid them
212 // to get loaded twice.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000213 std::map<int, ScopedFPDFPage> loaded_pages;
tonikitoo81d92f82016-09-21 12:44:56 -0700214
215 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
216 // make use of it.
npmfa20cd52016-11-14 13:33:40 -0800217 FPDF_FORMHANDLE form_handle;
tonikitoo81d92f82016-09-21 12:44:56 -0700218};
219
Lei Zhangd2be6462017-07-21 14:31:21 -0700220FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
npmfa20cd52016-11-14 13:33:40 -0800221 FPDF_FORMFILLINFO* form_fill_info) {
222 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info);
tonikitoo81d92f82016-09-21 12:44:56 -0700223}
224
Lei Zhang23c52852019-12-18 23:50:55 +0000225void OutputMD5Hash(const char* file_name, const uint8_t* buffer, int len) {
stephanafa05e972017-01-02 06:19:41 -0800226 // Get the MD5 hash and write it to stdout.
Lei Zhang23c52852019-12-18 23:50:55 +0000227 std::string hash = GenerateMD5Base16(buffer, len);
Lei Zhang143959d2017-06-22 12:20:58 -0700228 printf("MD5:%s:%s\n", file_name, hash.c_str());
stephanafa05e972017-01-02 06:19:41 -0800229}
230
Lei Zhanga41801e2018-08-23 21:59:33 +0000231#ifdef PDF_ENABLE_V8
Tom Sepezdba19d52020-06-04 23:01:17 +0000232
233struct V8IsolateDeleter {
234 inline void operator()(v8::Isolate* ptr) { ptr->Dispose(); }
235};
236
Tom Sepez58fb36a2016-02-01 10:32:14 -0800237// These example JS platform callback handlers are entirely optional,
238// and exist here to show the flow of information from a document back
239// to the embedder.
Tom Sepezbd932572016-01-29 09:10:41 -0800240int ExampleAppAlert(IPDF_JSPLATFORM*,
241 FPDF_WIDESTRING msg,
242 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800243 int type,
244 int icon) {
Tom Sepezbd932572016-01-29 09:10:41 -0800245 printf("%ls", GetPlatformWString(title).c_str());
npmfa20cd52016-11-14 13:33:40 -0800246 if (icon || type)
247 printf("[icon=%d,type=%d]", icon, type);
Tom Sepezbd932572016-01-29 09:10:41 -0800248 printf(": %ls\n", GetPlatformWString(msg).c_str());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249 return 0;
250}
251
Tom Sepez1f120c72018-10-18 22:54:02 +0000252void ExampleAppBeep(IPDF_JSPLATFORM*, int type) {
253 printf("BEEP!!! %d\n", type);
254}
255
Tom Sepez58fb36a2016-02-01 10:32:14 -0800256int ExampleAppResponse(IPDF_JSPLATFORM*,
257 FPDF_WIDESTRING question,
258 FPDF_WIDESTRING title,
npmfa20cd52016-11-14 13:33:40 -0800259 FPDF_WIDESTRING default_value,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800260 FPDF_WIDESTRING label,
npmfa20cd52016-11-14 13:33:40 -0800261 FPDF_BOOL is_password,
Tom Sepez58fb36a2016-02-01 10:32:14 -0800262 void* response,
263 int length) {
264 printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
265 GetPlatformWString(title).c_str(),
266 GetPlatformWString(question).c_str(),
npmfa20cd52016-11-14 13:33:40 -0800267 GetPlatformWString(default_value).c_str(),
268 GetPlatformWString(label).c_str(), is_password, length);
Tom Sepez58fb36a2016-02-01 10:32:14 -0800269
270 // UTF-16, always LE regardless of platform.
Dan Sinclairdddfdad2017-11-30 15:42:00 +0000271 auto* ptr = static_cast<uint8_t*>(response);
Tom Sepez58fb36a2016-02-01 10:32:14 -0800272 ptr[0] = 'N';
273 ptr[1] = 0;
274 ptr[2] = 'o';
275 ptr[3] = 0;
276 return 4;
277}
278
Tom Sepez1f120c72018-10-18 22:54:02 +0000279int ExampleDocGetFilePath(IPDF_JSPLATFORM*, void* file_path, int length) {
280 static const char kPath[] = "myfile.pdf";
281 constexpr int kRequired = static_cast<int>(sizeof(kPath));
282 if (file_path && length >= kRequired)
283 memcpy(file_path, kPath, kRequired);
284 return kRequired;
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800285}
286
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800287void ExampleDocMail(IPDF_JSPLATFORM*,
288 void* mailData,
289 int length,
npmfa20cd52016-11-14 13:33:40 -0800290 FPDF_BOOL UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800291 FPDF_WIDESTRING To,
292 FPDF_WIDESTRING Subject,
293 FPDF_WIDESTRING CC,
294 FPDF_WIDESTRING BCC,
295 FPDF_WIDESTRING Msg) {
npmfa20cd52016-11-14 13:33:40 -0800296 printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI,
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800297 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
298 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
299 GetPlatformWString(Msg).c_str());
300}
Tom Sepez0e5bab12018-10-18 21:39:40 +0000301
302void ExampleDocPrint(IPDF_JSPLATFORM*,
303 FPDF_BOOL bUI,
304 int nStart,
305 int nEnd,
306 FPDF_BOOL bSilent,
307 FPDF_BOOL bShrinkToFit,
308 FPDF_BOOL bPrintAsImage,
309 FPDF_BOOL bReverse,
310 FPDF_BOOL bAnnotations) {
311 printf("Doc Print: %d, %d, %d, %d, %d, %d, %d, %d\n", bUI, nStart, nEnd,
312 bSilent, bShrinkToFit, bPrintAsImage, bReverse, bAnnotations);
313}
Tom Sepez1f120c72018-10-18 22:54:02 +0000314
315void ExampleDocSubmitForm(IPDF_JSPLATFORM*,
316 void* formData,
317 int length,
318 FPDF_WIDESTRING url) {
Tom Sepez322c0af2019-12-09 21:44:14 +0000319 printf("Doc Submit Form: url=%ls + %d data bytes:\n",
320 GetPlatformWString(url).c_str(), length);
321 uint8_t* ptr = reinterpret_cast<uint8_t*>(formData);
322 for (int i = 0; i < length; ++i)
323 printf(" %02x", ptr[i]);
324 printf("\n");
Tom Sepez1f120c72018-10-18 22:54:02 +0000325}
326
327void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) {
328 printf("Goto Page: %d\n", page_number);
329}
330
331int ExampleFieldBrowse(IPDF_JSPLATFORM*, void* file_path, int length) {
332 static const char kPath[] = "selected.txt";
333 constexpr int kRequired = static_cast<int>(sizeof(kPath));
334 if (file_path && length >= kRequired)
335 memcpy(file_path, kPath, kRequired);
336 return kRequired;
337}
Lei Zhanga41801e2018-08-23 21:59:33 +0000338#endif // PDF_ENABLE_V8
Tom Sepeze5fbd7a2016-01-29 17:05:08 -0800339
Tom Sepez094f8e62020-04-01 21:01:44 +0000340#ifdef PDF_ENABLE_XFA
341FPDF_BOOL ExamplePopupMenu(FPDF_FORMFILLINFO* pInfo,
342 FPDF_PAGE page,
343 FPDF_WIDGET always_null,
344 int flags,
345 float x,
346 float y) {
347 printf("Popup: x=%2.1f, y=%2.1f, flags=0x%x\n", x, y, flags);
348 return true;
349}
350#endif // PDF_ENABLE_XFA
351
Tom Sepezb7cb36a2015-02-13 16:54:48 -0800352void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353 std::string feature = "Unknown";
354 switch (type) {
355 case FPDF_UNSP_DOC_XFAFORM:
356 feature = "XFA";
357 break;
358 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
359 feature = "Portfolios_Packages";
360 break;
361 case FPDF_UNSP_DOC_ATTACHMENT:
362 case FPDF_UNSP_ANNOT_ATTACHMENT:
363 feature = "Attachment";
364 break;
365 case FPDF_UNSP_DOC_SECURITY:
366 feature = "Rights_Management";
367 break;
368 case FPDF_UNSP_DOC_SHAREDREVIEW:
369 feature = "Shared_Review";
370 break;
371 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
372 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
373 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
374 feature = "Shared_Form";
375 break;
376 case FPDF_UNSP_ANNOT_3DANNOT:
377 feature = "3D";
378 break;
379 case FPDF_UNSP_ANNOT_MOVIE:
380 feature = "Movie";
381 break;
382 case FPDF_UNSP_ANNOT_SOUND:
383 feature = "Sound";
384 break;
385 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
386 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
387 feature = "Screen";
388 break;
389 case FPDF_UNSP_ANNOT_SIG:
390 feature = "Digital_Signature";
391 break;
392 }
393 printf("Unsupported feature: %s.\n", feature.c_str());
394}
395
Lei Zhang77d08742019-12-13 18:28:26 +0000396// |arg| is expected to be "--key=value", and |key| is "--key=".
397bool ParseSwitchKeyValue(const std::string& arg,
398 const std::string& key,
399 std::string* value) {
400 if (arg.size() <= key.size() || arg.compare(0, key.size(), key) != 0)
401 return false;
402
403 *value = arg.substr(key.size());
404 return true;
405}
406
Tom Sepez5ee12d72014-12-17 16:24:01 -0800407bool ParseCommandLine(const std::vector<std::string>& args,
thestig514e8c92016-07-15 17:57:54 -0700408 Options* options,
409 std::vector<std::string>* files) {
410 if (args.empty())
Tom Sepez5ee12d72014-12-17 16:24:01 -0800411 return false;
thestig514e8c92016-07-15 17:57:54 -0700412
Tom Sepez5ee12d72014-12-17 16:24:01 -0800413 options->exe_path = args[0];
Bo Xud44e3922014-12-19 02:27:25 -0800414 size_t cur_idx = 1;
Lei Zhang77d08742019-12-13 18:28:26 +0000415 std::string value;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800416 for (; cur_idx < args.size(); ++cur_idx) {
417 const std::string& cur_arg = args[cur_idx];
Tom Sepez2991d8d2016-01-15 16:02:48 -0800418 if (cur_arg == "--show-config") {
419 options->show_config = true;
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -0400420 } else if (cur_arg == "--show-metadata") {
421 options->show_metadata = true;
tsepezf09bdfa2016-04-18 16:08:26 -0700422 } else if (cur_arg == "--send-events") {
423 options->send_events = true;
Lei Zhang793c1352019-07-12 17:35:57 +0000424 } else if (cur_arg == "--mem-document") {
425 options->use_load_mem_document = true;
Dan Sinclairaeadad12017-07-18 16:43:41 -0400426 } else if (cur_arg == "--render-oneshot") {
427 options->render_oneshot = true;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000428 } else if (cur_arg == "--lcd-text") {
429 options->lcd_text = true;
430 } else if (cur_arg == "--no-nativetext") {
431 options->no_nativetext = true;
432 } else if (cur_arg == "--grayscale") {
433 options->grayscale = true;
Gourab Kundud35a62d2020-04-24 10:29:27 +0000434 } else if (cur_arg == "--forced-color") {
435 options->forced_color = true;
436 } else if (cur_arg == "--fill-to-stroke") {
437 options->fill_to_stroke = true;
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000438 } else if (cur_arg == "--limit-cache") {
439 options->limit_cache = true;
440 } else if (cur_arg == "--force-halftone") {
441 options->force_halftone = true;
442 } else if (cur_arg == "--printing") {
443 options->printing = true;
444 } else if (cur_arg == "--no-smoothtext") {
445 options->no_smoothtext = true;
446 } else if (cur_arg == "--no-smoothimage") {
447 options->no_smoothimage = true;
448 } else if (cur_arg == "--no-smoothpath") {
449 options->no_smoothpath = true;
Lei Zhangb82d9072019-11-20 21:20:57 +0000450 } else if (cur_arg == "--reverse-byte-order") {
451 options->reverse_byte_order = true;
Jane Liu4442d452017-07-19 10:19:42 -0400452 } else if (cur_arg == "--save-attachments") {
453 options->save_attachments = true;
Jane Liub35dbad2017-08-03 16:29:17 -0400454 } else if (cur_arg == "--save-images") {
Lei Zhang3b1a8b42020-08-01 01:12:48 +0000455 if (options->save_rendered_images) {
456 fprintf(stderr,
457 "--save-rendered-images conflicts with --save-images\n");
458 return false;
459 }
Jane Liub35dbad2017-08-03 16:29:17 -0400460 options->save_images = true;
Lei Zhang0ac7b4e2020-07-18 00:35:53 +0000461 } else if (cur_arg == "--save-rendered-images") {
Lei Zhang3b1a8b42020-08-01 01:12:48 +0000462 if (options->save_images) {
463 fprintf(stderr,
464 "--save-images conflicts with --save-rendered-images\n");
465 return false;
466 }
Lei Zhang0ac7b4e2020-07-18 00:35:53 +0000467 options->save_rendered_images = true;
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +0000468 } else if (cur_arg == "--save-thumbs") {
469 options->save_thumbnails = true;
470 } else if (cur_arg == "--save-thumbs-dec") {
471 options->save_thumbnails_decoded = true;
472 } else if (cur_arg == "--save-thumbs-raw") {
473 options->save_thumbnails_raw = true;
Ryan Harrison4f21d772018-06-01 15:19:04 +0000474#ifdef PDF_ENABLE_V8
Tom Sepez0784c732018-04-23 18:02:57 +0000475 } else if (cur_arg == "--disable-javascript") {
476 options->disable_javascript = true;
Tom Sepeze0d3c502020-08-11 23:51:10 +0000477 } else if (ParseSwitchKeyValue(cur_arg, "--js-flags=", &value)) {
478 if (!options->js_flags.empty()) {
479 fprintf(stderr, "Duplicate --js-flags argument\n");
480 return false;
481 }
482 options->js_flags = value;
Tom Sepez0784c732018-04-23 18:02:57 +0000483#ifdef PDF_ENABLE_XFA
484 } else if (cur_arg == "--disable-xfa") {
485 options->disable_xfa = true;
486#endif // PDF_ENABLE_XFA
487#endif // PDF_ENABLE_V8
Henrique Nakashima95ea7782017-07-11 16:42:43 -0400488#ifdef ENABLE_CALLGRIND
489 } else if (cur_arg == "--callgrind-delim") {
490 options->callgrind_delimiters = true;
Lei Zhangd0f989e2019-04-26 18:03:13 +0000491#endif
492#if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
493 } else if (cur_arg == "--no-system-fonts") {
494 options->linux_no_system_fonts = true;
495#endif
Tom Sepez2991d8d2016-01-15 16:02:48 -0800496 } else if (cur_arg == "--ppm") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000497 if (options->output_format != OutputFormat::kNone) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800498 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
499 return false;
500 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000501 options->output_format = OutputFormat::kPpm;
Tom Sepezaf18cb32015-02-05 15:06:01 -0800502 } else if (cur_arg == "--png") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000503 if (options->output_format != OutputFormat::kNone) {
Tom Sepezaf18cb32015-02-05 15:06:01 -0800504 fprintf(stderr, "Duplicate or conflicting --png argument\n");
505 return false;
506 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000507 options->output_format = OutputFormat::kPng;
dsinclairb63068f2016-06-16 07:58:09 -0700508 } else if (cur_arg == "--txt") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000509 if (options->output_format != OutputFormat::kNone) {
dsinclairb63068f2016-06-16 07:58:09 -0700510 fprintf(stderr, "Duplicate or conflicting --txt argument\n");
511 return false;
512 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000513 options->output_format = OutputFormat::kText;
Jane Liu4fd9a472017-06-01 18:56:09 -0400514 } else if (cur_arg == "--annot") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000515 if (options->output_format != OutputFormat::kNone) {
Jane Liu4fd9a472017-06-01 18:56:09 -0400516 fprintf(stderr, "Duplicate or conflicting --annot argument\n");
517 return false;
518 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000519 options->output_format = OutputFormat::kAnnot;
Cary Clark399be5b2016-03-14 16:51:29 -0400520#ifdef PDF_ENABLE_SKIA
521 } else if (cur_arg == "--skp") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000522 if (options->output_format != OutputFormat::kNone) {
Cary Clark399be5b2016-03-14 16:51:29 -0400523 fprintf(stderr, "Duplicate or conflicting --skp argument\n");
524 return false;
525 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000526 options->output_format = OutputFormat::kSkp;
Ryan Harrisondd8da5e2018-05-17 20:43:42 +0000527#endif // PDF_ENABLE_SKIA
Lei Zhang77d08742019-12-13 18:28:26 +0000528 } else if (ParseSwitchKeyValue(cur_arg, "--font-dir=", &value)) {
Lei Zhang6f62d532015-09-23 15:31:44 -0700529 if (!options->font_directory.empty()) {
530 fprintf(stderr, "Duplicate --font-dir argument\n");
531 return false;
532 }
Lei Zhang77d08742019-12-13 18:28:26 +0000533 std::string path = value;
Ryan Harrison16adb3e2018-04-04 17:44:31 +0000534 auto expanded_path = ExpandDirectoryPath(path);
535 if (!expanded_path) {
536 fprintf(stderr, "Failed to expand --font-dir, %s\n", path.c_str());
537 return false;
538 }
Ryan Harrisondd8da5e2018-05-17 20:43:42 +0000539
540 if (!PathService::DirectoryExists(expanded_path.value())) {
541 fprintf(stderr, "--font-dir, %s, appears to not be a directory\n",
542 path.c_str());
543 return false;
544 }
545
Ryan Harrison16adb3e2018-04-04 17:44:31 +0000546 options->font_directory = expanded_path.value();
547
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700548#ifdef _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500549 } else if (cur_arg == "--emf") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000550 if (options->output_format != OutputFormat::kNone) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800551 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
552 return false;
553 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000554 options->output_format = OutputFormat::kEmf;
Lei Zhangd1c9b452017-05-05 17:21:36 -0700555 } else if (cur_arg == "--ps2") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000556 if (options->output_format != OutputFormat::kNone) {
Lei Zhangd1c9b452017-05-05 17:21:36 -0700557 fprintf(stderr, "Duplicate or conflicting --ps2 argument\n");
558 return false;
559 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000560 options->output_format = OutputFormat::kPs2;
Lei Zhangd1c9b452017-05-05 17:21:36 -0700561 } else if (cur_arg == "--ps3") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000562 if (options->output_format != OutputFormat::kNone) {
Lei Zhangd1c9b452017-05-05 17:21:36 -0700563 fprintf(stderr, "Duplicate or conflicting --ps3 argument\n");
564 return false;
565 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000566 options->output_format = OutputFormat::kPs3;
Dan Sinclair50cce602016-02-24 09:51:16 -0500567 } else if (cur_arg == "--bmp") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000568 if (options->output_format != OutputFormat::kNone) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800569 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
570 return false;
571 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000572 options->output_format = OutputFormat::kBmp;
Tom Sepez5ee12d72014-12-17 16:24:01 -0800573#endif // _WIN32
Dan Sinclair738b08c2016-03-01 14:45:20 -0500574
Tom Sepez452b4f32015-10-13 09:27:27 -0700575#ifdef PDF_ENABLE_V8
Tom Sepez5ee12d72014-12-17 16:24:01 -0800576#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhang77d08742019-12-13 18:28:26 +0000577 } else if (ParseSwitchKeyValue(cur_arg, "--bin-dir=", &value)) {
Tom Sepez5ee12d72014-12-17 16:24:01 -0800578 if (!options->bin_directory.empty()) {
579 fprintf(stderr, "Duplicate --bin-dir argument\n");
580 return false;
581 }
Lei Zhang77d08742019-12-13 18:28:26 +0000582 std::string path = value;
Ryan Harrison16adb3e2018-04-04 17:44:31 +0000583 auto expanded_path = ExpandDirectoryPath(path);
584 if (!expanded_path) {
585 fprintf(stderr, "Failed to expand --bin-dir, %s\n", path.c_str());
586 return false;
587 }
588 options->bin_directory = expanded_path.value();
Tom Sepez5ee12d72014-12-17 16:24:01 -0800589#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez452b4f32015-10-13 09:27:27 -0700590#endif // PDF_ENABLE_V8
Dan Sinclair738b08c2016-03-01 14:45:20 -0500591
Lei Zhang77d08742019-12-13 18:28:26 +0000592 } else if (ParseSwitchKeyValue(cur_arg, "--password=", &value)) {
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000593 if (!options->password.empty()) {
594 fprintf(stderr, "Duplicate --password argument\n");
595 return false;
596 }
Lei Zhang77d08742019-12-13 18:28:26 +0000597 options->password = value;
598 } else if (ParseSwitchKeyValue(cur_arg, "--scale=", &value)) {
Tom Sepezdaa2e842015-01-29 15:44:37 -0800599 if (!options->scale_factor_as_string.empty()) {
600 fprintf(stderr, "Duplicate --scale argument\n");
601 return false;
602 }
Lei Zhang77d08742019-12-13 18:28:26 +0000603 options->scale_factor_as_string = value;
Lei Zhangbb3f58d2018-10-16 05:54:09 +0000604 } else if (cur_arg == "--show-pageinfo") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000605 if (options->output_format != OutputFormat::kNone) {
Lei Zhangbb3f58d2018-10-16 05:54:09 +0000606 fprintf(stderr, "Duplicate or conflicting --show-pageinfo argument\n");
607 return false;
608 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000609 options->output_format = OutputFormat::kPageInfo;
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400610 } else if (cur_arg == "--show-structure") {
Lei Zhang38235eb2020-07-28 06:38:37 +0000611 if (options->output_format != OutputFormat::kNone) {
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400612 fprintf(stderr, "Duplicate or conflicting --show-structure argument\n");
613 return false;
614 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000615 options->output_format = OutputFormat::kStructure;
Lei Zhang77d08742019-12-13 18:28:26 +0000616 } else if (ParseSwitchKeyValue(cur_arg, "--pages=", &value)) {
npme3c73152016-11-14 09:14:52 -0800617 if (options->pages) {
618 fprintf(stderr, "Duplicate --pages argument\n");
619 return false;
620 }
621 options->pages = true;
Lei Zhang77d08742019-12-13 18:28:26 +0000622 const std::string pages_string = value;
npmfa20cd52016-11-14 13:33:40 -0800623 size_t first_dash = pages_string.find("-");
624 if (first_dash == std::string::npos) {
625 std::stringstream(pages_string) >> options->first_page;
626 options->last_page = options->first_page;
npme3c73152016-11-14 09:14:52 -0800627 } else {
npmfa20cd52016-11-14 13:33:40 -0800628 std::stringstream(pages_string.substr(0, first_dash)) >>
629 options->first_page;
630 std::stringstream(pages_string.substr(first_dash + 1)) >>
631 options->last_page;
npme3c73152016-11-14 09:14:52 -0800632 }
stephanafa05e972017-01-02 06:19:41 -0800633 } else if (cur_arg == "--md5") {
634 options->md5 = true;
Lei Zhang77d08742019-12-13 18:28:26 +0000635 } else if (ParseSwitchKeyValue(cur_arg, "--time=", &value)) {
Ryan Harrison70cca362018-08-10 18:55:46 +0000636 if (options->time > -1) {
637 fprintf(stderr, "Duplicate --time argument\n");
638 return false;
639 }
Lei Zhang77d08742019-12-13 18:28:26 +0000640 const std::string time_string = value;
Ryan Harrison70cca362018-08-10 18:55:46 +0000641 std::stringstream(time_string) >> options->time;
642 if (options->time < 0) {
643 fprintf(stderr, "Invalid --time argument, must be non-negative\n");
644 return false;
645 }
Tom Sepez2991d8d2016-01-15 16:02:48 -0800646 } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
647 fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
648 return false;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500649 } else {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700650 break;
Dan Sinclair738b08c2016-03-01 14:45:20 -0500651 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700652 }
thestig514e8c92016-07-15 17:57:54 -0700653 for (size_t i = cur_idx; i < args.size(); i++)
Tom Sepez5ee12d72014-12-17 16:24:01 -0800654 files->push_back(args[i]);
thestig514e8c92016-07-15 17:57:54 -0700655
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700656 return true;
657}
658
Lei Zhangd2be6462017-07-21 14:31:21 -0700659void PrintLastError() {
660 unsigned long err = FPDF_GetLastError();
661 fprintf(stderr, "Load pdf docs unsuccessful: ");
662 switch (err) {
663 case FPDF_ERR_SUCCESS:
664 fprintf(stderr, "Success");
665 break;
666 case FPDF_ERR_UNKNOWN:
667 fprintf(stderr, "Unknown error");
668 break;
669 case FPDF_ERR_FILE:
670 fprintf(stderr, "File not found or could not be opened");
671 break;
672 case FPDF_ERR_FORMAT:
673 fprintf(stderr, "File not in PDF format or corrupted");
674 break;
675 case FPDF_ERR_PASSWORD:
676 fprintf(stderr, "Password required or incorrect password");
677 break;
678 case FPDF_ERR_SECURITY:
679 fprintf(stderr, "Unsupported security scheme");
680 break;
681 case FPDF_ERR_PAGE:
682 fprintf(stderr, "Page not found or content error");
683 break;
684 default:
685 fprintf(stderr, "Unknown error %ld", err);
686 }
687 fprintf(stderr, ".\n");
Lei Zhangd2be6462017-07-21 14:31:21 -0700688}
689
npmfa20cd52016-11-14 13:33:40 -0800690FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700691 return true;
692}
693
npmfa20cd52016-11-14 13:33:40 -0800694void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700695
tonikitoo3e981582016-08-26 08:37:10 -0700696FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
697 FPDF_DOCUMENT doc,
698 int index) {
npmfa20cd52016-11-14 13:33:40 -0800699 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info =
700 ToPDFiumTestFormFillInfo(param);
701 auto& loaded_pages = form_fill_info->loaded_pages;
npmfa20cd52016-11-14 13:33:40 -0800702 auto iter = loaded_pages.find(index);
703 if (iter != loaded_pages.end())
Tom Sepezbfa2a972017-07-24 11:38:31 -0700704 return iter->second.get();
tonikitoo3e981582016-08-26 08:37:10 -0700705
Tom Sepeze08d2b12018-04-25 18:49:32 +0000706 ScopedFPDFPage page(FPDF_LoadPage(doc, index));
tonikitoo3e981582016-08-26 08:37:10 -0700707 if (!page)
708 return nullptr;
709
Lei Zhang8e53c8c2018-02-06 00:18:12 +0000710 // Mark the page as loaded first to prevent infinite recursion.
711 FPDF_PAGE page_ptr = page.get();
712 loaded_pages[index] = std::move(page);
713
npmfa20cd52016-11-14 13:33:40 -0800714 FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle;
Lei Zhang8e53c8c2018-02-06 00:18:12 +0000715 FORM_OnAfterLoadPage(page_ptr, form_handle);
716 FORM_DoPageAAction(page_ptr, form_handle, FPDFPAGE_AACTION_OPEN);
717 return page_ptr;
tonikitoo3e981582016-08-26 08:37:10 -0700718}
719
Dan Sinclairaeadad12017-07-18 16:43:41 -0400720// Note, for a client using progressive rendering you'd want to determine if you
721// need the rendering to pause instead of always saying |true|. This is for
722// testing to force the renderer to break whenever possible.
723FPDF_BOOL NeedToPauseNow(IFSDK_PAUSE* p) {
724 return true;
725}
726
Lei Zhang148588f2020-07-28 06:31:57 +0000727bool ProcessPage(const std::string& name,
728 FPDF_DOCUMENT doc,
729 FPDF_FORMHANDLE form,
730 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info,
731 const int page_index,
732 const Options& options,
733 const std::string& events) {
Tom Sepezbfa2a972017-07-24 11:38:31 -0700734 FPDF_PAGE page = GetPageForIndex(form_fill_info, doc, page_index);
735 if (!page)
Jun Fangb553bcb2015-11-10 18:49:04 +0800736 return false;
Dan Sinclair3c67fbd2017-04-05 16:07:50 -0400737 if (options.send_events)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700738 SendPageEvents(form, page, events);
Jane Liub35dbad2017-08-03 16:29:17 -0400739 if (options.save_images)
Dan Sinclaircb876822018-03-22 16:21:04 +0000740 WriteImages(page, name.c_str(), page_index);
Lei Zhang0ac7b4e2020-07-18 00:35:53 +0000741 if (options.save_rendered_images)
742 WriteRenderedImages(doc, page, name.c_str(), page_index);
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +0000743 if (options.save_thumbnails)
744 WriteThumbnail(page, name.c_str(), page_index);
745 if (options.save_thumbnails_decoded)
746 WriteDecodedThumbnailStream(page, name.c_str(), page_index);
747 if (options.save_thumbnails_raw)
748 WriteRawThumbnailStream(page, name.c_str(), page_index);
Lei Zhang38235eb2020-07-28 06:38:37 +0000749 if (options.output_format == OutputFormat::kPageInfo) {
Lei Zhangbb3f58d2018-10-16 05:54:09 +0000750 DumpPageInfo(page, page_index);
751 return true;
752 }
Lei Zhang38235eb2020-07-28 06:38:37 +0000753 if (options.output_format == OutputFormat::kStructure) {
Tom Sepezbfa2a972017-07-24 11:38:31 -0700754 DumpPageStructure(page, page_index);
Dan Sinclairddcb6e72017-04-05 10:30:33 -0400755 return true;
756 }
757
Tom Sepeze08d2b12018-04-25 18:49:32 +0000758 ScopedFPDFTextPage text_page(FPDFText_LoadPage(page));
Jun Fangdf7f3662015-11-10 18:29:18 +0800759 double scale = 1.0;
thestig514e8c92016-07-15 17:57:54 -0700760 if (!options.scale_factor_as_string.empty())
Jun Fangdf7f3662015-11-10 18:29:18 +0800761 std::stringstream(options.scale_factor_as_string) >> scale;
thestig514e8c92016-07-15 17:57:54 -0700762
Lei Zhangdfa075b2019-12-05 21:52:43 +0000763 auto width = static_cast<int>(FPDF_GetPageWidthF(page) * scale);
764 auto height = static_cast<int>(FPDF_GetPageHeightF(page) * scale);
Tom Sepezbfa2a972017-07-24 11:38:31 -0700765 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0;
Tom Sepeze08d2b12018-04-25 18:49:32 +0000766 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha));
Tom Sepez1d17a042017-03-16 13:22:47 -0700767
thestige97ea032016-05-19 10:59:15 -0700768 if (bitmap) {
769 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
Tom Sepez1d17a042017-03-16 13:22:47 -0700770 FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400771
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000772 int flags = PageRenderFlagsFromOptions(options);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400773 if (options.render_oneshot) {
774 // Note, client programs probably want to use this method instead of the
775 // progressive calls. The progressive calls are if you need to pause the
776 // rendering to update the UI, the PDF renderer will break when possible.
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000777 FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400778 } else {
779 IFSDK_PAUSE pause;
780 pause.version = 1;
781 pause.NeedToPauseNow = &NeedToPauseNow;
782
Gourab Kundud35a62d2020-04-24 10:29:27 +0000783 // Client programs will be setting these values when rendering.
784 // This is a sample color scheme with distinct colors.
785 // Used only when |options.forced_color| is true.
786 const FPDF_COLORSCHEME color_scheme{
787 /*path_fill_color=*/0xFFFF0000, /*path_stroke_color=*/0xFF00FF00,
788 /*text_fill_color=*/0xFF0000FF, /*text_stroke_color=*/0xFF00FFFF};
789
790 int rv = FPDF_RenderPageBitmapWithColorScheme_Start(
791 bitmap.get(), page, 0, 0, width, height, 0, flags,
792 options.forced_color ? &color_scheme : nullptr, &pause);
Lei Zhang939426c2018-08-10 16:32:43 +0000793 while (rv == FPDF_RENDER_TOBECONTINUED)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700794 rv = FPDF_RenderPage_Continue(page, &pause);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400795 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800796
Tom Sepez8f5d29e2019-11-12 00:08:13 +0000797 FPDF_FFLDraw(form, bitmap.get(), page, 0, 0, width, height, 0, flags);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400798
799 if (!options.render_oneshot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700800 FPDF_RenderPage_Close(page);
Dan Sinclairaeadad12017-07-18 16:43:41 -0400801
Tom Sepez1d17a042017-03-16 13:22:47 -0700802 int stride = FPDFBitmap_GetStride(bitmap.get());
Lei Zhang23c52852019-12-18 23:50:55 +0000803 void* buffer = FPDFBitmap_GetBuffer(bitmap.get());
Jun Fangdf7f3662015-11-10 18:29:18 +0800804
Lei Zhangfe2cd4d2017-11-22 20:04:12 +0000805 std::string image_file_name;
thestige97ea032016-05-19 10:59:15 -0700806 switch (options.output_format) {
Jun Fangdf7f3662015-11-10 18:29:18 +0800807#ifdef _WIN32
Lei Zhang38235eb2020-07-28 06:38:37 +0000808 case OutputFormat::kBmp:
stephanafa05e972017-01-02 06:19:41 -0800809 image_file_name =
810 WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700811 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800812
Lei Zhang38235eb2020-07-28 06:38:37 +0000813 case OutputFormat::kEmf:
Tom Sepezbfa2a972017-07-24 11:38:31 -0700814 WriteEmf(page, name.c_str(), page_index);
thestige97ea032016-05-19 10:59:15 -0700815 break;
Lei Zhangd1c9b452017-05-05 17:21:36 -0700816
Lei Zhang38235eb2020-07-28 06:38:37 +0000817 case OutputFormat::kPs2:
818 case OutputFormat::kPs3:
Tom Sepezbfa2a972017-07-24 11:38:31 -0700819 WritePS(page, name.c_str(), page_index);
Lei Zhangd1c9b452017-05-05 17:21:36 -0700820 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800821#endif
Lei Zhang38235eb2020-07-28 06:38:37 +0000822 case OutputFormat::kText:
Lei Zhangf045cb22020-08-01 01:21:38 +0000823 WriteText(text_page.get(), name.c_str(), page_index);
dsinclairb63068f2016-06-16 07:58:09 -0700824 break;
825
Lei Zhang38235eb2020-07-28 06:38:37 +0000826 case OutputFormat::kAnnot:
Tom Sepezbfa2a972017-07-24 11:38:31 -0700827 WriteAnnot(page, name.c_str(), page_index);
Jane Liu4fd9a472017-06-01 18:56:09 -0400828 break;
829
Lei Zhang38235eb2020-07-28 06:38:37 +0000830 case OutputFormat::kPng:
stephanafa05e972017-01-02 06:19:41 -0800831 image_file_name =
832 WritePng(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700833 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800834
Lei Zhang38235eb2020-07-28 06:38:37 +0000835 case OutputFormat::kPpm:
stephanafa05e972017-01-02 06:19:41 -0800836 image_file_name =
837 WritePpm(name.c_str(), page_index, buffer, stride, width, height);
thestige97ea032016-05-19 10:59:15 -0700838 break;
Jun Fangdf7f3662015-11-10 18:29:18 +0800839
Cary Clark399be5b2016-03-14 16:51:29 -0400840#ifdef PDF_ENABLE_SKIA
Lei Zhang38235eb2020-07-28 06:38:37 +0000841 case OutputFormat::kSkp: {
thestige97ea032016-05-19 10:59:15 -0700842 std::unique_ptr<SkPictureRecorder> recorder(
thestigb97e07e2016-09-28 22:16:40 -0700843 reinterpret_cast<SkPictureRecorder*>(
Tom Sepezbfa2a972017-07-24 11:38:31 -0700844 FPDF_RenderPageSkp(page, width, height)));
845 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0);
stephanafa05e972017-01-02 06:19:41 -0800846 image_file_name = WriteSkp(name.c_str(), page_index, recorder.get());
thestige97ea032016-05-19 10:59:15 -0700847 } break;
Cary Clark399be5b2016-03-14 16:51:29 -0400848#endif
thestige97ea032016-05-19 10:59:15 -0700849 default:
850 break;
851 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800852
stephanafa05e972017-01-02 06:19:41 -0800853 // Write the filename and the MD5 of the buffer to stdout if we wrote a
854 // file.
Lei Zhang23c52852019-12-18 23:50:55 +0000855 if (options.md5 && !image_file_name.empty()) {
856 OutputMD5Hash(image_file_name.c_str(),
857 static_cast<const uint8_t*>(buffer), stride * height);
858 }
thestige97ea032016-05-19 10:59:15 -0700859 } else {
860 fprintf(stderr, "Page was too large to be rendered.\n");
861 }
tonikitoo3e981582016-08-26 08:37:10 -0700862
Tom Sepezbfa2a972017-07-24 11:38:31 -0700863 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
864 FORM_OnBeforeClosePage(page, form);
thestige97ea032016-05-19 10:59:15 -0700865 return !!bitmap;
Jun Fangdf7f3662015-11-10 18:29:18 +0800866}
867
Lei Zhang148588f2020-07-28 06:31:57 +0000868void ProcessPdf(const std::string& name,
869 const char* buf,
870 size_t len,
871 const Options& options,
872 const std::string& events) {
Lei Zhangd58ff632019-07-12 16:50:04 +0000873 TestLoader loader({buf, len});
Dan Sinclair04212022018-03-21 18:08:26 +0000874
Lei Zhangd2be6462017-07-21 14:31:21 -0700875 FPDF_FILEACCESS file_access = {};
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700876 file_access.m_FileLen = static_cast<unsigned long>(len);
Tom Sepezd831dc72015-10-19 16:04:22 -0700877 file_access.m_GetBlock = TestLoader::GetBlock;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700878 file_access.m_Param = &loader;
879
Lei Zhangd2be6462017-07-21 14:31:21 -0700880 FX_FILEAVAIL file_avail = {};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700881 file_avail.version = 1;
882 file_avail.IsDataAvail = Is_Data_Avail;
883
Lei Zhangd2be6462017-07-21 14:31:21 -0700884 FX_DOWNLOADHINTS hints = {};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885 hints.version = 1;
886 hints.AddSegment = Add_Segment;
887
Lei Zhangd58ff632019-07-12 16:50:04 +0000888 // |pdf_avail| must outlive |doc|.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000889 ScopedFPDFAvail pdf_avail(FPDFAvail_Create(&file_avail, &file_access));
Dan Sinclair04212022018-03-21 18:08:26 +0000890
Lei Zhangd58ff632019-07-12 16:50:04 +0000891 // |doc| must outlive |form_callbacks.loaded_pages|.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000892 ScopedFPDFDocument doc;
Dan Sinclair04212022018-03-21 18:08:26 +0000893
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000894 const char* password =
895 options.password.empty() ? nullptr : options.password.c_str();
Lei Zhangd58ff632019-07-12 16:50:04 +0000896 bool is_linearized = false;
Lei Zhang793c1352019-07-12 17:35:57 +0000897 if (options.use_load_mem_document) {
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000898 doc.reset(FPDF_LoadMemDocument(buf, len, password));
Jun Fangdf7f3662015-11-10 18:29:18 +0800899 } else {
Lei Zhang793c1352019-07-12 17:35:57 +0000900 if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) {
901 int avail_status = PDF_DATA_NOTAVAIL;
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000902 doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), password));
Lei Zhang793c1352019-07-12 17:35:57 +0000903 if (doc) {
904 while (avail_status == PDF_DATA_NOTAVAIL)
905 avail_status = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
906
907 if (avail_status == PDF_DATA_ERROR) {
908 fprintf(stderr, "Unknown error in checking if doc was available.\n");
909 return;
910 }
911 avail_status = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
912 if (avail_status == PDF_FORM_ERROR ||
913 avail_status == PDF_FORM_NOTAVAIL) {
914 fprintf(stderr,
915 "Error %d was returned in checking if form was available.\n",
916 avail_status);
917 return;
918 }
919 is_linearized = true;
920 }
921 } else {
Lei Zhang5c44e5c2019-12-13 17:22:38 +0000922 doc.reset(FPDF_LoadCustomDocument(&file_access, password));
Lei Zhang793c1352019-07-12 17:35:57 +0000923 }
Jun Fangdf7f3662015-11-10 18:29:18 +0800924 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700925
Lei Zhang5377ebf2015-09-23 14:52:53 -0700926 if (!doc) {
Lei Zhangd2be6462017-07-21 14:31:21 -0700927 PrintLastError();
JUN FANG827a1722015-03-05 13:39:21 -0800928 return;
929 }
930
Lei Zhang10689852019-01-08 20:14:47 +0000931 if (!FPDF_DocumentHasValidCrossReferenceTable(doc.get()))
932 fprintf(stderr, "Document has invalid cross reference table\n");
933
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000934 (void)FPDF_GetDocPermissions(doc.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700935
Lei Zhangd2be6462017-07-21 14:31:21 -0700936 if (options.show_metadata)
937 DumpMetaData(doc.get());
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -0400938
Lei Zhangd2be6462017-07-21 14:31:21 -0700939 if (options.save_attachments)
Dan Sinclaircb876822018-03-22 16:21:04 +0000940 WriteAttachments(doc.get(), name);
Jane Liu4442d452017-07-19 10:19:42 -0400941
Lei Zhanga41801e2018-08-23 21:59:33 +0000942#ifdef PDF_ENABLE_V8
Dan Sinclair04212022018-03-21 18:08:26 +0000943 IPDF_JSPLATFORM platform_callbacks = {};
944 platform_callbacks.version = 3;
945 platform_callbacks.app_alert = ExampleAppAlert;
Tom Sepezbcb81522018-05-15 17:46:15 +0000946 platform_callbacks.app_beep = ExampleAppBeep;
Tom Sepez1f120c72018-10-18 22:54:02 +0000947 platform_callbacks.app_response = ExampleAppResponse;
948 platform_callbacks.Doc_getFilePath = ExampleDocGetFilePath;
Dan Sinclair04212022018-03-21 18:08:26 +0000949 platform_callbacks.Doc_mail = ExampleDocMail;
Tom Sepez0e5bab12018-10-18 21:39:40 +0000950 platform_callbacks.Doc_print = ExampleDocPrint;
Tom Sepez1f120c72018-10-18 22:54:02 +0000951 platform_callbacks.Doc_submitForm = ExampleDocSubmitForm;
952 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
953 platform_callbacks.Field_browse = ExampleFieldBrowse;
Lei Zhanga41801e2018-08-23 21:59:33 +0000954#endif // PDF_ENABLE_V8
Dan Sinclair04212022018-03-21 18:08:26 +0000955
956 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
957#ifdef PDF_ENABLE_XFA
958 form_callbacks.version = 2;
Daniel Hosseiniana793e972020-01-24 02:51:31 +0000959 form_callbacks.xfa_disabled =
960 options.disable_xfa || options.disable_javascript;
Tom Sepez094f8e62020-04-01 21:01:44 +0000961 form_callbacks.FFI_PopupMenu = ExamplePopupMenu;
Dan Sinclair04212022018-03-21 18:08:26 +0000962#else // PDF_ENABLE_XFA
963 form_callbacks.version = 1;
964#endif // PDF_ENABLE_XFA
965 form_callbacks.FFI_GetPage = GetPageForIndex;
Tom Sepez0784c732018-04-23 18:02:57 +0000966
967#ifdef PDF_ENABLE_V8
968 if (!options.disable_javascript)
969 form_callbacks.m_pJsPlatform = &platform_callbacks;
970#endif // PDF_ENABLE_V8
Dan Sinclair04212022018-03-21 18:08:26 +0000971
Tom Sepeze08d2b12018-04-25 18:49:32 +0000972 ScopedFPDFFormHandle form(
Tom Sepez1d17a042017-03-16 13:22:47 -0700973 FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
974 form_callbacks.form_handle = form.get();
tonikitoo81d92f82016-09-21 12:44:56 -0700975
Tom Sepezc46d0002015-11-30 15:46:36 -0800976#ifdef PDF_ENABLE_XFA
Tom Sepez0784c732018-04-23 18:02:57 +0000977 if (!options.disable_xfa && !options.disable_javascript) {
978 int doc_type = FPDF_GetFormType(doc.get());
979 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND) {
980 if (!FPDF_LoadXFA(doc.get()))
981 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
982 }
Tom Sepez56451382014-12-05 13:30:51 -0800983 }
Tom Sepezc46d0002015-11-30 15:46:36 -0800984#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700985
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000986 FPDF_SetFormFieldHighlightColor(form.get(), FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD);
Tom Sepez1d17a042017-03-16 13:22:47 -0700987 FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
988 FORM_DoDocumentJSAction(form.get());
989 FORM_DoDocumentOpenAction(form.get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700990
Lei Zhangd1c9b452017-05-05 17:21:36 -0700991#if _WIN32
Lei Zhang38235eb2020-07-28 06:38:37 +0000992 if (options.output_format == OutputFormat::kPs2)
Lei Zhangd2be6462017-07-21 14:31:21 -0700993 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT2);
Lei Zhang38235eb2020-07-28 06:38:37 +0000994 else if (options.output_format == OutputFormat::kPs3)
Lei Zhangd2be6462017-07-21 14:31:21 -0700995 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3);
Lei Zhangd1c9b452017-05-05 17:21:36 -0700996#endif
997
Tom Sepez1d17a042017-03-16 13:22:47 -0700998 int page_count = FPDF_GetPageCount(doc.get());
Lei Zhang148588f2020-07-28 06:31:57 +0000999 int processed_pages = 0;
Tom Sepez1ed8a212015-05-11 15:25:39 -07001000 int bad_pages = 0;
npmfa20cd52016-11-14 13:33:40 -08001001 int first_page = options.pages ? options.first_page : 0;
1002 int last_page = options.pages ? options.last_page + 1 : page_count;
1003 for (int i = first_page; i < last_page; ++i) {
Lei Zhangd58ff632019-07-12 16:50:04 +00001004 if (is_linearized) {
1005 int avail_status = PDF_DATA_NOTAVAIL;
1006 while (avail_status == PDF_DATA_NOTAVAIL)
1007 avail_status = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
thestig514e8c92016-07-15 17:57:54 -07001008
Lei Zhangd58ff632019-07-12 16:50:04 +00001009 if (avail_status == PDF_DATA_ERROR) {
Jun Fangdf7f3662015-11-10 18:29:18 +08001010 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
1011 i);
1012 return;
1013 }
1014 }
Lei Zhang148588f2020-07-28 06:31:57 +00001015 if (ProcessPage(name, doc.get(), form.get(), &form_callbacks, i, options,
1016 events)) {
1017 ++processed_pages;
Lei Zhangd2be6462017-07-21 14:31:21 -07001018 } else {
Lei Zhang5377ebf2015-09-23 14:52:53 -07001019 ++bad_pages;
Lei Zhangd2be6462017-07-21 14:31:21 -07001020 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001021 }
1022
Tom Sepez1d17a042017-03-16 13:22:47 -07001023 FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC);
Lei Zhang148588f2020-07-28 06:31:57 +00001024 fprintf(stderr, "Processed %d pages.\n", processed_pages);
tsepez10b01bf2016-05-04 12:52:42 -07001025 if (bad_pages)
1026 fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001027}
1028
Lei Zhangd2be6462017-07-21 14:31:21 -07001029void ShowConfig() {
Tom Sepez2991d8d2016-01-15 16:02:48 -08001030 std::string config;
1031 std::string maybe_comma;
Ryan Harrisondd8da5e2018-05-17 20:43:42 +00001032#ifdef PDF_ENABLE_V8
Tom Sepez2991d8d2016-01-15 16:02:48 -08001033 config.append(maybe_comma);
1034 config.append("V8");
1035 maybe_comma = ",";
1036#endif // PDF_ENABLE_V8
1037#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1038 config.append(maybe_comma);
1039 config.append("V8_EXTERNAL");
1040 maybe_comma = ",";
1041#endif // V8_USE_EXTERNAL_STARTUP_DATA
1042#ifdef PDF_ENABLE_XFA
1043 config.append(maybe_comma);
1044 config.append("XFA");
1045 maybe_comma = ",";
1046#endif // PDF_ENABLE_XFA
dan sinclair00d40642017-01-30 19:48:54 -08001047#ifdef PDF_ENABLE_ASAN
1048 config.append(maybe_comma);
1049 config.append("ASAN");
1050 maybe_comma = ",";
1051#endif // PDF_ENABLE_ASAN
Lei Zhang2cb8f852019-09-16 19:39:16 +00001052#if defined(PDF_ENABLE_SKIA)
1053 config.append(maybe_comma);
1054 config.append("SKIA");
1055 maybe_comma = ",";
1056#elif defined(PDF_ENABLE_SKIA_PATHS)
1057 config.append(maybe_comma);
1058 config.append("SKIAPATHS");
1059 maybe_comma = ",";
1060#endif
Tom Sepez2991d8d2016-01-15 16:02:48 -08001061 printf("%s\n", config.c_str());
1062}
1063
Lei Zhangd2be6462017-07-21 14:31:21 -07001064constexpr char kUsageString[] =
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001065 "Usage: pdfium_test [OPTION] [FILE]...\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001066 " --show-config - print build options and exit\n"
1067 " --show-metadata - print the file metadata\n"
1068 " --show-pageinfo - print information about pages\n"
1069 " --show-structure - print the structure elements from the "
1070 "document\n"
1071 " --send-events - send input described by .evt file\n"
1072 " --mem-document - load document with FPDF_LoadMemDocument()\n"
1073 " --render-oneshot - render image without using progressive "
1074 "renderer\n"
1075 " --lcd-text - render text optimized for LCD displays\n"
1076 " --no-nativetext - render without using the native text output\n"
1077 " --grayscale - render grayscale output\n"
1078 " --forced-color - render in forced color mode\n"
1079 " --fill-to-stroke - render fill as stroke in forced color mode\n"
1080 " --limit-cache - render limiting image cache size\n"
1081 " --force-halftone - render forcing halftone\n"
1082 " --printing - render as if for printing\n"
1083 " --no-smoothtext - render disabling text anti-aliasing\n"
1084 " --no-smoothimage - render disabling image anti-alisasing\n"
1085 " --no-smoothpath - render disabling path anti-aliasing\n"
1086 " --reverse-byte-order - render to BGRA, if supported by the output "
Lei Zhangb82d9072019-11-20 21:20:57 +00001087 "format\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001088 " --save-attachments - write embedded attachments "
Jane Liu4442d452017-07-19 10:19:42 -04001089 "<pdf-name>.attachment.<attachment-name>\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001090 " --save-images - write raw embedded images "
Jane Liub35dbad2017-08-03 16:29:17 -04001091 "<pdf-name>.<page-number>.<object-number>.png\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001092 " --save-rendered-images - write embedded images as rendered on the page "
1093 "<pdf-name>.<page-number>.<object-number>.png\n"
1094 " --save-thumbs - write page thumbnails "
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +00001095 "<pdf-name>.thumbnail.<page-number>.png\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001096 " --save-thumbs-dec - write page thumbnails' decoded stream data"
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +00001097 "<pdf-name>.thumbnail.decoded.<page-number>.png\n"
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001098 " --save-thumbs-raw - write page thumbnails' raw stream data"
Jeremy Chinsen801e1bf2019-06-25 20:46:13 +00001099 "<pdf-name>.thumbnail.raw.<page-number>.png\n"
Tom Sepez0784c732018-04-23 18:02:57 +00001100#ifdef PDF_ENABLE_V8
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001101 " --disable-javascript - do not execute JS in PDF files\n"
Tom Sepeze0d3c502020-08-11 23:51:10 +00001102 " --js-flags=<flags> - additional flags to pas to V8"
Tom Sepez0784c732018-04-23 18:02:57 +00001103#ifdef PDF_ENABLE_XFA
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001104 " --disable-xfa - do not process XFA forms\n"
Tom Sepez0784c732018-04-23 18:02:57 +00001105#endif // PDF_ENABLE_XFA
1106#endif // PDF_ENABLE_V8
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001107#ifdef ENABLE_CALLGRIND
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001108 " --callgrind-delim - delimit interesting section when using "
Lei Zhangf066bf72019-04-26 17:53:16 +00001109 "callgrind\n"
1110#endif
Lei Zhangd0f989e2019-04-26 18:03:13 +00001111#if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__))
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001112 " --no-system-fonts - do not use system fonts, overrides --font-dir\n"
Lei Zhangd0f989e2019-04-26 18:03:13 +00001113#endif
Lei Zhang3b1a8b42020-08-01 01:12:48 +00001114 " --bin-dir=<path> - override path to v8 external data\n"
1115 " --font-dir=<path> - override path to external fonts\n"
1116 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
1117 " --password=<secret> - password to decrypt the PDF with\n"
npmfa20cd52016-11-14 13:33:40 -08001118 " --pages=<number>(-<number>) - only render the given 0-based page(s)\n"
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001119#ifdef _WIN32
Jane Liu57f228d2017-07-13 18:10:30 -04001120 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
1121 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
1122 " --ps2 - write page raw PostScript (Lvl 2) "
1123 "<pdf-name>.<page-number>.ps\n"
1124 " --ps3 - write page raw PostScript (Lvl 3) "
1125 "<pdf-name>.<page-number>.ps\n"
Lei Zhangf066bf72019-04-26 17:53:16 +00001126#endif
Jane Liu57f228d2017-07-13 18:10:30 -04001127 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
1128 " --png - write page images <pdf-name>.<page-number>.png\n"
1129 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
1130 " --annot - write annotation info <pdf-name>.<page-number>.annot.txt\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001131#ifdef PDF_ENABLE_SKIA
Jane Liu57f228d2017-07-13 18:10:30 -04001132 " --skp - write page images <pdf-name>.<page-number>.skp\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001133#endif
Jane Liu57f228d2017-07-13 18:10:30 -04001134 " --md5 - write output image paths and their md5 hashes to stdout.\n"
Ryan Harrison70cca362018-08-10 18:55:46 +00001135 " --time=<number> - Seconds since the epoch to set system time.\n"
Cary Clark399be5b2016-03-14 16:51:29 -04001136 "";
Tom Sepez23b4e3f2015-02-06 16:05:23 -08001137
Lei Zhangd2be6462017-07-21 14:31:21 -07001138} // namespace
1139
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001140int main(int argc, const char* argv[]) {
Tom Sepez5ee12d72014-12-17 16:24:01 -08001141 std::vector<std::string> args(argv, argv + argc);
1142 Options options;
thestig514e8c92016-07-15 17:57:54 -07001143 std::vector<std::string> files;
Tom Sepez5ee12d72014-12-17 16:24:01 -08001144 if (!ParseCommandLine(args, &options, &files)) {
thestig514e8c92016-07-15 17:57:54 -07001145 fprintf(stderr, "%s", kUsageString);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001146 return 1;
1147 }
1148
Tom Sepez2991d8d2016-01-15 16:02:48 -08001149 if (options.show_config) {
1150 ShowConfig();
1151 return 0;
1152 }
1153
1154 if (files.empty()) {
1155 fprintf(stderr, "No input files.\n");
1156 return 1;
1157 }
1158
Tom Sepeza72e8e2c2015-10-07 10:17:53 -07001159 FPDF_LIBRARY_CONFIG config;
Tom Sepeza7858e62020-06-10 00:38:19 +00001160 config.version = 3;
Tom Sepeza72e8e2c2015-10-07 10:17:53 -07001161 config.m_pUserFontPaths = nullptr;
1162 config.m_pIsolate = nullptr;
1163 config.m_v8EmbedderSlot = 0;
Tom Sepeza7858e62020-06-10 00:38:19 +00001164 config.m_pPlatform = nullptr;
Tom Sepeza72e8e2c2015-10-07 10:17:53 -07001165
Tom Sepezdba19d52020-06-04 23:01:17 +00001166#ifdef PDF_ENABLE_V8
1167#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1168 v8::StartupData snapshot;
1169#endif // V8_USE_EXTERNAL_STARTUP_DATA
1170 std::unique_ptr<v8::Platform> platform;
1171 std::unique_ptr<v8::Isolate, V8IsolateDeleter> isolate;
1172 if (!options.disable_javascript) {
1173#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1174 platform = InitializeV8ForPDFiumWithStartupData(
Tom Sepeze0d3c502020-08-11 23:51:10 +00001175 options.exe_path, options.js_flags, options.bin_directory, &snapshot);
Tom Sepezdba19d52020-06-04 23:01:17 +00001176#else // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepeze0d3c502020-08-11 23:51:10 +00001177 platform = InitializeV8ForPDFium(options.exe_path, options.js_flags);
Tom Sepezdba19d52020-06-04 23:01:17 +00001178#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepezbd5b8602020-11-12 19:45:55 +00001179 if (!platform) {
1180 fprintf(stderr, "V8 initialization failed.\n");
1181 return 1;
1182 }
Tom Sepeza7858e62020-06-10 00:38:19 +00001183 config.m_pPlatform = platform.get();
1184
Tom Sepezdba19d52020-06-04 23:01:17 +00001185 v8::Isolate::CreateParams params;
1186 params.array_buffer_allocator = static_cast<v8::ArrayBuffer::Allocator*>(
1187 FPDF_GetArrayBufferAllocatorSharedInstance());
1188 isolate.reset(v8::Isolate::New(params));
1189 config.m_pIsolate = isolate.get();
1190 }
1191#endif // PDF_ENABLE_V8
1192
Lei Zhangd0f989e2019-04-26 18:03:13 +00001193 const char* path_array[2] = {nullptr, nullptr};
1194 Optional<const char*> custom_font_path = GetCustomFontPath(options);
1195 if (custom_font_path.has_value()) {
1196 path_array[0] = custom_font_path.value();
Lei Zhang6f62d532015-09-23 15:31:44 -07001197 config.m_pUserFontPaths = path_array;
Lei Zhang6f62d532015-09-23 15:31:44 -07001198 }
Lei Zhangd0f989e2019-04-26 18:03:13 +00001199
Tom Sepeza72e8e2c2015-10-07 10:17:53 -07001200 FPDF_InitLibraryWithConfig(&config);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001201
Lei Zhangd2be6462017-07-21 14:31:21 -07001202 UNSUPPORT_INFO unsupported_info = {};
npmfa20cd52016-11-14 13:33:40 -08001203 unsupported_info.version = 1;
1204 unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001205
npmfa20cd52016-11-14 13:33:40 -08001206 FSDK_SetUnSpObjProcessHandler(&unsupported_info);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001207
Ryan Harrison70cca362018-08-10 18:55:46 +00001208 if (options.time > -1) {
1209 // This must be a static var to avoid explicit capture, so the lambda can be
1210 // converted to a function ptr.
1211 static time_t time_ret = options.time;
Tom Sepez706e1872018-10-22 19:34:53 +00001212 FSDK_SetTimeFunction([]() { return time_ret; });
1213 FSDK_SetLocaltimeFunction([](const time_t* tp) { return gmtime(tp); });
Ryan Harrison70cca362018-08-10 18:55:46 +00001214 }
1215
thestig514e8c92016-07-15 17:57:54 -07001216 for (const std::string& filename : files) {
Tom Sepez5ee12d72014-12-17 16:24:01 -08001217 size_t file_length = 0;
Tom Sepez0aa35312016-01-06 10:16:32 -08001218 std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
1219 GetFileContents(filename.c_str(), &file_length);
tsepezf09bdfa2016-04-18 16:08:26 -07001220 if (!file_contents)
1221 continue;
Lei Zhang148588f2020-07-28 06:31:57 +00001222 fprintf(stderr, "Processing PDF file %s.\n", filename.c_str());
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001223
1224#ifdef ENABLE_CALLGRIND
1225 if (options.callgrind_delimiters)
1226 CALLGRIND_START_INSTRUMENTATION;
1227#endif // ENABLE_CALLGRIND
1228
tsepezf09bdfa2016-04-18 16:08:26 -07001229 std::string events;
1230 if (options.send_events) {
1231 std::string event_filename = filename;
1232 size_t event_length = 0;
1233 size_t extension_pos = event_filename.find(".pdf");
1234 if (extension_pos != std::string::npos) {
1235 event_filename.replace(extension_pos, 4, ".evt");
thestigf2b940c2016-10-13 06:48:47 -07001236 if (access(event_filename.c_str(), R_OK) == 0) {
1237 fprintf(stderr, "Using event file %s.\n", event_filename.c_str());
1238 std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
1239 GetFileContents(event_filename.c_str(), &event_length);
1240 if (event_contents) {
1241 fprintf(stderr, "Sending events from: %s\n",
1242 event_filename.c_str());
1243 events = std::string(event_contents.get(), event_length);
1244 }
tsepezf09bdfa2016-04-18 16:08:26 -07001245 }
1246 }
1247 }
Lei Zhang148588f2020-07-28 06:31:57 +00001248 ProcessPdf(filename, file_contents.get(), file_length, options, events);
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001249
Tom Sepezae8ab222020-06-08 23:39:03 +00001250#ifdef PDF_ENABLE_V8
1251 if (!options.disable_javascript) {
1252 int task_count = 0;
1253 while (v8::platform::PumpMessageLoop(platform.get(), isolate.get()))
1254 ++task_count;
1255
1256 if (task_count)
1257 fprintf(stderr, "Pumped %d tasks\n", task_count);
1258 }
1259#endif // PDF_ENABLE_V8
1260
Henrique Nakashima95ea7782017-07-11 16:42:43 -04001261#ifdef ENABLE_CALLGRIND
1262 if (options.callgrind_delimiters)
1263 CALLGRIND_STOP_INSTRUMENTATION;
1264#endif // ENABLE_CALLGRIND
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001265 }
1266
1267 FPDF_DestroyLibrary();
thestigc08cd7a2016-06-27 09:47:59 -07001268
Tom Sepez27e1a742019-02-15 19:45:38 +00001269#ifdef PDF_ENABLE_V8
1270 if (!options.disable_javascript) {
Tom Sepezdba19d52020-06-04 23:01:17 +00001271 isolate.reset();
Tom Sepez27e1a742019-02-15 19:45:38 +00001272 v8::V8::ShutdownPlatform();
thestigc08cd7a2016-06-27 09:47:59 -07001273#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez27e1a742019-02-15 19:45:38 +00001274 free(const_cast<char*>(snapshot.data));
thestigc08cd7a2016-06-27 09:47:59 -07001275#endif // V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez27e1a742019-02-15 19:45:38 +00001276 }
Tom Sepez452b4f32015-10-13 09:27:27 -07001277#endif // PDF_ENABLE_V8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001278
1279 return 0;
1280}