John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
Lei Zhang | 9a25ede | 2017-06-22 00:05:12 -0700 | [diff] [blame] | 9 | #include <iterator> |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 10 | #include <map> |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 11 | #include <memory> |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 12 | #include <sstream> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | #include <string> |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 14 | #include <vector> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 15 | |
Lei Zhang | 4facbc9 | 2020-07-30 16:49:26 +0000 | [diff] [blame] | 16 | #if defined(PDF_ENABLE_SKIA) && !defined(_SKIA_SUPPORT_) |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 17 | #define _SKIA_SUPPORT_ |
| 18 | #endif |
| 19 | |
Lei Zhang | 4facbc9 | 2020-07-30 16:49:26 +0000 | [diff] [blame] | 20 | #if defined(PDF_ENABLE_SKIA_PATHS) && !defined(_SKIA_SUPPORT_PATHS_) |
Lei Zhang | 2cb8f85 | 2019-09-16 19:39:16 +0000 | [diff] [blame] | 21 | #define _SKIA_SUPPORT_PATHS_ |
| 22 | #endif |
| 23 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 24 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 25 | #include "public/fpdf_annot.h" |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 26 | #include "public/fpdf_attachment.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 27 | #include "public/fpdf_dataavail.h" |
Lei Zhang | 453d96b | 2015-12-31 13:13:10 -0800 | [diff] [blame] | 28 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 29 | #include "public/fpdf_ext.h" |
| 30 | #include "public/fpdf_formfill.h" |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 31 | #include "public/fpdf_progressive.h" |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 32 | #include "public/fpdf_structtree.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 33 | #include "public/fpdf_text.h" |
| 34 | #include "public/fpdfview.h" |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 35 | #include "samples/pdfium_test_dump_helper.h" |
| 36 | #include "samples/pdfium_test_event_helper.h" |
| 37 | #include "samples/pdfium_test_write_helper.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 38 | #include "testing/fx_string_testhelpers.h" |
Lei Zhang | d50bdff | 2019-02-05 19:42:33 +0000 | [diff] [blame] | 39 | #include "testing/test_loader.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame] | 40 | #include "testing/utils/file_util.h" |
Lei Zhang | 4c64e96 | 2019-02-05 19:24:12 +0000 | [diff] [blame] | 41 | #include "testing/utils/hash.h" |
Ryan Harrison | dd8da5e | 2018-05-17 20:43:42 +0000 | [diff] [blame] | 42 | #include "testing/utils/path_service.h" |
Ryan Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 43 | #include "third_party/base/optional.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 44 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 45 | #ifdef _WIN32 |
| 46 | #include <io.h> |
| 47 | #else |
| 48 | #include <unistd.h> |
| 49 | #endif |
| 50 | |
Henrique Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 51 | #ifdef ENABLE_CALLGRIND |
| 52 | #include <valgrind/callgrind.h> |
| 53 | #endif // ENABLE_CALLGRIND |
| 54 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 55 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 56 | #include "testing/v8_initializer.h" |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 57 | #include "v8/include/libplatform/libplatform.h" |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 58 | #include "v8/include/v8.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 59 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 61 | #ifdef _WIN32 |
| 62 | #define access _access |
| 63 | #define snprintf _snprintf |
| 64 | #define R_OK 4 |
| 65 | #endif |
| 66 | |
Lei Zhang | 8ecff96 | 2020-07-30 21:10:42 +0000 | [diff] [blame] | 67 | // wordexp is a POSIX function that is only available on macOS and non-Android |
Ryan Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 68 | // 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 Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 77 | enum class OutputFormat { |
| 78 | kNone, |
| 79 | kPageInfo, |
| 80 | kStructure, |
| 81 | kText, |
| 82 | kPpm, |
| 83 | kPng, |
| 84 | kAnnot, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 85 | #ifdef _WIN32 |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 86 | kBmp, |
| 87 | kEmf, |
| 88 | kPs2, |
| 89 | kPs3, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 90 | #endif |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 91 | #ifdef PDF_ENABLE_SKIA |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 92 | kSkp, |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 93 | #endif |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 96 | namespace { |
| 97 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 98 | struct Options { |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 99 | Options() = default; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 100 | |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 101 | bool show_config = false; |
| 102 | bool show_metadata = false; |
| 103 | bool send_events = false; |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 104 | bool use_load_mem_document = false; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 105 | bool render_oneshot = false; |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 106 | bool lcd_text = false; |
| 107 | bool no_nativetext = false; |
| 108 | bool grayscale = false; |
Gourab Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 109 | bool forced_color = false; |
| 110 | bool fill_to_stroke = false; |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 111 | 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 Zhang | b82d907 | 2019-11-20 21:20:57 +0000 | [diff] [blame] | 117 | bool reverse_byte_order = false; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 118 | bool save_attachments = false; |
| 119 | bool save_images = false; |
Lei Zhang | 0ac7b4e | 2020-07-18 00:35:53 +0000 | [diff] [blame] | 120 | bool save_rendered_images = false; |
Jeremy Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 121 | bool save_thumbnails = false; |
| 122 | bool save_thumbnails_decoded = false; |
| 123 | bool save_thumbnails_raw = false; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 124 | #ifdef PDF_ENABLE_V8 |
| 125 | bool disable_javascript = false; |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 126 | std::string js_flags; // Extra flags to pass to v8 init. |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 127 | #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 Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 133 | #ifdef ENABLE_CALLGRIND |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 134 | bool callgrind_delimiters = false; |
Lei Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 135 | #endif |
| 136 | #if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__)) |
| 137 | bool linux_no_system_fonts = false; |
| 138 | #endif |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 139 | OutputFormat output_format = OutputFormat::kNone; |
Lei Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 140 | std::string password; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 141 | std::string scale_factor_as_string; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 142 | std::string exe_path; |
| 143 | std::string bin_directory; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 144 | std::string font_directory; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 145 | int first_page = 0; // First 0-based page number to renderer. |
| 146 | int last_page = 0; // Last 0-based page number to renderer. |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 147 | time_t time = -1; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 150 | int 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 Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 158 | if (options.fill_to_stroke) |
| 159 | flags |= FPDF_CONVERT_FILL_TO_STROKE; |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 160 | 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 Zhang | b82d907 | 2019-11-20 21:20:57 +0000 | [diff] [blame] | 172 | if (options.reverse_byte_order) |
| 173 | flags |= FPDF_REVERSE_BYTE_ORDER; |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 174 | return flags; |
| 175 | } |
| 176 | |
Ryan Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 177 | Optional<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 Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 194 | Optional<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 Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 210 | struct FPDF_FORMFILLINFO_PDFiumTest final : public FPDF_FORMFILLINFO { |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 211 | // Hold a map of the currently loaded pages in order to avoid them |
| 212 | // to get loaded twice. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 213 | std::map<int, ScopedFPDFPage> loaded_pages; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 214 | |
| 215 | // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can |
| 216 | // make use of it. |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 217 | FPDF_FORMHANDLE form_handle; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 218 | }; |
| 219 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 220 | FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo( |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 221 | FPDF_FORMFILLINFO* form_fill_info) { |
| 222 | return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Lei Zhang | 23c5285 | 2019-12-18 23:50:55 +0000 | [diff] [blame] | 225 | void OutputMD5Hash(const char* file_name, const uint8_t* buffer, int len) { |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 226 | // Get the MD5 hash and write it to stdout. |
Lei Zhang | 23c5285 | 2019-12-18 23:50:55 +0000 | [diff] [blame] | 227 | std::string hash = GenerateMD5Base16(buffer, len); |
Lei Zhang | 143959d | 2017-06-22 12:20:58 -0700 | [diff] [blame] | 228 | printf("MD5:%s:%s\n", file_name, hash.c_str()); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Lei Zhang | a41801e | 2018-08-23 21:59:33 +0000 | [diff] [blame] | 231 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 232 | |
| 233 | struct V8IsolateDeleter { |
| 234 | inline void operator()(v8::Isolate* ptr) { ptr->Dispose(); } |
| 235 | }; |
| 236 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 237 | // 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 Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 240 | int ExampleAppAlert(IPDF_JSPLATFORM*, |
| 241 | FPDF_WIDESTRING msg, |
| 242 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 243 | int type, |
| 244 | int icon) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 245 | printf("%ls", GetPlatformWString(title).c_str()); |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 246 | if (icon || type) |
| 247 | printf("[icon=%d,type=%d]", icon, type); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 248 | printf(": %ls\n", GetPlatformWString(msg).c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Tom Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 252 | void ExampleAppBeep(IPDF_JSPLATFORM*, int type) { |
| 253 | printf("BEEP!!! %d\n", type); |
| 254 | } |
| 255 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 256 | int ExampleAppResponse(IPDF_JSPLATFORM*, |
| 257 | FPDF_WIDESTRING question, |
| 258 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 259 | FPDF_WIDESTRING default_value, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 260 | FPDF_WIDESTRING label, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 261 | FPDF_BOOL is_password, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 262 | 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(), |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 267 | GetPlatformWString(default_value).c_str(), |
| 268 | GetPlatformWString(label).c_str(), is_password, length); |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 269 | |
| 270 | // UTF-16, always LE regardless of platform. |
Dan Sinclair | dddfdad | 2017-11-30 15:42:00 +0000 | [diff] [blame] | 271 | auto* ptr = static_cast<uint8_t*>(response); |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 272 | ptr[0] = 'N'; |
| 273 | ptr[1] = 0; |
| 274 | ptr[2] = 'o'; |
| 275 | ptr[3] = 0; |
| 276 | return 4; |
| 277 | } |
| 278 | |
Tom Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 279 | int 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 Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 287 | void ExampleDocMail(IPDF_JSPLATFORM*, |
| 288 | void* mailData, |
| 289 | int length, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 290 | FPDF_BOOL UI, |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 291 | FPDF_WIDESTRING To, |
| 292 | FPDF_WIDESTRING Subject, |
| 293 | FPDF_WIDESTRING CC, |
| 294 | FPDF_WIDESTRING BCC, |
| 295 | FPDF_WIDESTRING Msg) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 296 | printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI, |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 297 | GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(), |
| 298 | GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(), |
| 299 | GetPlatformWString(Msg).c_str()); |
| 300 | } |
Tom Sepez | 0e5bab1 | 2018-10-18 21:39:40 +0000 | [diff] [blame] | 301 | |
| 302 | void 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 Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 314 | |
| 315 | void ExampleDocSubmitForm(IPDF_JSPLATFORM*, |
| 316 | void* formData, |
| 317 | int length, |
| 318 | FPDF_WIDESTRING url) { |
Tom Sepez | 322c0af | 2019-12-09 21:44:14 +0000 | [diff] [blame] | 319 | 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 Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) { |
| 328 | printf("Goto Page: %d\n", page_number); |
| 329 | } |
| 330 | |
| 331 | int 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 Zhang | a41801e | 2018-08-23 21:59:33 +0000 | [diff] [blame] | 338 | #endif // PDF_ENABLE_V8 |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 339 | |
Tom Sepez | 094f8e6 | 2020-04-01 21:01:44 +0000 | [diff] [blame] | 340 | #ifdef PDF_ENABLE_XFA |
| 341 | FPDF_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 Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 352 | void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 353 | 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 Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 396 | // |arg| is expected to be "--key=value", and |key| is "--key=". |
| 397 | bool 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 Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 407 | bool ParseCommandLine(const std::vector<std::string>& args, |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 408 | Options* options, |
| 409 | std::vector<std::string>* files) { |
| 410 | if (args.empty()) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 411 | return false; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 412 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 413 | options->exe_path = args[0]; |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 414 | size_t cur_idx = 1; |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 415 | std::string value; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 416 | for (; cur_idx < args.size(); ++cur_idx) { |
| 417 | const std::string& cur_arg = args[cur_idx]; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 418 | if (cur_arg == "--show-config") { |
| 419 | options->show_config = true; |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 420 | } else if (cur_arg == "--show-metadata") { |
| 421 | options->show_metadata = true; |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 422 | } else if (cur_arg == "--send-events") { |
| 423 | options->send_events = true; |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 424 | } else if (cur_arg == "--mem-document") { |
| 425 | options->use_load_mem_document = true; |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 426 | } else if (cur_arg == "--render-oneshot") { |
| 427 | options->render_oneshot = true; |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 428 | } 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 Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 434 | } 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 Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 438 | } 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 Zhang | b82d907 | 2019-11-20 21:20:57 +0000 | [diff] [blame] | 450 | } else if (cur_arg == "--reverse-byte-order") { |
| 451 | options->reverse_byte_order = true; |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 452 | } else if (cur_arg == "--save-attachments") { |
| 453 | options->save_attachments = true; |
Jane Liu | b35dbad | 2017-08-03 16:29:17 -0400 | [diff] [blame] | 454 | } else if (cur_arg == "--save-images") { |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 455 | if (options->save_rendered_images) { |
| 456 | fprintf(stderr, |
| 457 | "--save-rendered-images conflicts with --save-images\n"); |
| 458 | return false; |
| 459 | } |
Jane Liu | b35dbad | 2017-08-03 16:29:17 -0400 | [diff] [blame] | 460 | options->save_images = true; |
Lei Zhang | 0ac7b4e | 2020-07-18 00:35:53 +0000 | [diff] [blame] | 461 | } else if (cur_arg == "--save-rendered-images") { |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 462 | if (options->save_images) { |
| 463 | fprintf(stderr, |
| 464 | "--save-images conflicts with --save-rendered-images\n"); |
| 465 | return false; |
| 466 | } |
Lei Zhang | 0ac7b4e | 2020-07-18 00:35:53 +0000 | [diff] [blame] | 467 | options->save_rendered_images = true; |
Jeremy Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 468 | } 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 Harrison | 4f21d77 | 2018-06-01 15:19:04 +0000 | [diff] [blame] | 474 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 475 | } else if (cur_arg == "--disable-javascript") { |
| 476 | options->disable_javascript = true; |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 477 | } 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 Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 483 | #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 Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 488 | #ifdef ENABLE_CALLGRIND |
| 489 | } else if (cur_arg == "--callgrind-delim") { |
| 490 | options->callgrind_delimiters = true; |
Lei Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 491 | #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 Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 496 | } else if (cur_arg == "--ppm") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 497 | if (options->output_format != OutputFormat::kNone) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 498 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 499 | return false; |
| 500 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 501 | options->output_format = OutputFormat::kPpm; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 502 | } else if (cur_arg == "--png") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 503 | if (options->output_format != OutputFormat::kNone) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 504 | fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 505 | return false; |
| 506 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 507 | options->output_format = OutputFormat::kPng; |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 508 | } else if (cur_arg == "--txt") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 509 | if (options->output_format != OutputFormat::kNone) { |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 510 | fprintf(stderr, "Duplicate or conflicting --txt argument\n"); |
| 511 | return false; |
| 512 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 513 | options->output_format = OutputFormat::kText; |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 514 | } else if (cur_arg == "--annot") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 515 | if (options->output_format != OutputFormat::kNone) { |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 516 | fprintf(stderr, "Duplicate or conflicting --annot argument\n"); |
| 517 | return false; |
| 518 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 519 | options->output_format = OutputFormat::kAnnot; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 520 | #ifdef PDF_ENABLE_SKIA |
| 521 | } else if (cur_arg == "--skp") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 522 | if (options->output_format != OutputFormat::kNone) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 523 | fprintf(stderr, "Duplicate or conflicting --skp argument\n"); |
| 524 | return false; |
| 525 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 526 | options->output_format = OutputFormat::kSkp; |
Ryan Harrison | dd8da5e | 2018-05-17 20:43:42 +0000 | [diff] [blame] | 527 | #endif // PDF_ENABLE_SKIA |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 528 | } else if (ParseSwitchKeyValue(cur_arg, "--font-dir=", &value)) { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 529 | if (!options->font_directory.empty()) { |
| 530 | fprintf(stderr, "Duplicate --font-dir argument\n"); |
| 531 | return false; |
| 532 | } |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 533 | std::string path = value; |
Ryan Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 534 | 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 Harrison | dd8da5e | 2018-05-17 20:43:42 +0000 | [diff] [blame] | 539 | |
| 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 Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 546 | options->font_directory = expanded_path.value(); |
| 547 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 548 | #ifdef _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 549 | } else if (cur_arg == "--emf") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 550 | if (options->output_format != OutputFormat::kNone) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 551 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 552 | return false; |
| 553 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 554 | options->output_format = OutputFormat::kEmf; |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 555 | } else if (cur_arg == "--ps2") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 556 | if (options->output_format != OutputFormat::kNone) { |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 557 | fprintf(stderr, "Duplicate or conflicting --ps2 argument\n"); |
| 558 | return false; |
| 559 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 560 | options->output_format = OutputFormat::kPs2; |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 561 | } else if (cur_arg == "--ps3") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 562 | if (options->output_format != OutputFormat::kNone) { |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 563 | fprintf(stderr, "Duplicate or conflicting --ps3 argument\n"); |
| 564 | return false; |
| 565 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 566 | options->output_format = OutputFormat::kPs3; |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 567 | } else if (cur_arg == "--bmp") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 568 | if (options->output_format != OutputFormat::kNone) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 569 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 570 | return false; |
| 571 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 572 | options->output_format = OutputFormat::kBmp; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 573 | #endif // _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 574 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 575 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 576 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 577 | } else if (ParseSwitchKeyValue(cur_arg, "--bin-dir=", &value)) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 578 | if (!options->bin_directory.empty()) { |
| 579 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 580 | return false; |
| 581 | } |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 582 | std::string path = value; |
Ryan Harrison | 16adb3e | 2018-04-04 17:44:31 +0000 | [diff] [blame] | 583 | 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 Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 589 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 590 | #endif // PDF_ENABLE_V8 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 591 | |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 592 | } else if (ParseSwitchKeyValue(cur_arg, "--password=", &value)) { |
Lei Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 593 | if (!options->password.empty()) { |
| 594 | fprintf(stderr, "Duplicate --password argument\n"); |
| 595 | return false; |
| 596 | } |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 597 | options->password = value; |
| 598 | } else if (ParseSwitchKeyValue(cur_arg, "--scale=", &value)) { |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 599 | if (!options->scale_factor_as_string.empty()) { |
| 600 | fprintf(stderr, "Duplicate --scale argument\n"); |
| 601 | return false; |
| 602 | } |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 603 | options->scale_factor_as_string = value; |
Lei Zhang | bb3f58d | 2018-10-16 05:54:09 +0000 | [diff] [blame] | 604 | } else if (cur_arg == "--show-pageinfo") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 605 | if (options->output_format != OutputFormat::kNone) { |
Lei Zhang | bb3f58d | 2018-10-16 05:54:09 +0000 | [diff] [blame] | 606 | fprintf(stderr, "Duplicate or conflicting --show-pageinfo argument\n"); |
| 607 | return false; |
| 608 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 609 | options->output_format = OutputFormat::kPageInfo; |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 610 | } else if (cur_arg == "--show-structure") { |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 611 | if (options->output_format != OutputFormat::kNone) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 612 | fprintf(stderr, "Duplicate or conflicting --show-structure argument\n"); |
| 613 | return false; |
| 614 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 615 | options->output_format = OutputFormat::kStructure; |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 616 | } else if (ParseSwitchKeyValue(cur_arg, "--pages=", &value)) { |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 617 | if (options->pages) { |
| 618 | fprintf(stderr, "Duplicate --pages argument\n"); |
| 619 | return false; |
| 620 | } |
| 621 | options->pages = true; |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 622 | const std::string pages_string = value; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 623 | 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; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 627 | } else { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 628 | 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; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 632 | } |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 633 | } else if (cur_arg == "--md5") { |
| 634 | options->md5 = true; |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 635 | } else if (ParseSwitchKeyValue(cur_arg, "--time=", &value)) { |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 636 | if (options->time > -1) { |
| 637 | fprintf(stderr, "Duplicate --time argument\n"); |
| 638 | return false; |
| 639 | } |
Lei Zhang | 77d0874 | 2019-12-13 18:28:26 +0000 | [diff] [blame] | 640 | const std::string time_string = value; |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 641 | 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 Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 646 | } 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 Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 649 | } else { |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 650 | break; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 651 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 652 | } |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 653 | for (size_t i = cur_idx; i < args.size(); i++) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 654 | files->push_back(args[i]); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 655 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 659 | void 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 Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 688 | } |
| 689 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 690 | FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 691 | return true; |
| 692 | } |
| 693 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 694 | void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 695 | |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 696 | FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, |
| 697 | FPDF_DOCUMENT doc, |
| 698 | int index) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 699 | FPDF_FORMFILLINFO_PDFiumTest* form_fill_info = |
| 700 | ToPDFiumTestFormFillInfo(param); |
| 701 | auto& loaded_pages = form_fill_info->loaded_pages; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 702 | auto iter = loaded_pages.find(index); |
| 703 | if (iter != loaded_pages.end()) |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 704 | return iter->second.get(); |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 705 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 706 | ScopedFPDFPage page(FPDF_LoadPage(doc, index)); |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 707 | if (!page) |
| 708 | return nullptr; |
| 709 | |
Lei Zhang | 8e53c8c | 2018-02-06 00:18:12 +0000 | [diff] [blame] | 710 | // 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 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 714 | FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle; |
Lei Zhang | 8e53c8c | 2018-02-06 00:18:12 +0000 | [diff] [blame] | 715 | FORM_OnAfterLoadPage(page_ptr, form_handle); |
| 716 | FORM_DoPageAAction(page_ptr, form_handle, FPDFPAGE_AACTION_OPEN); |
| 717 | return page_ptr; |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 720 | // 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. |
| 723 | FPDF_BOOL NeedToPauseNow(IFSDK_PAUSE* p) { |
| 724 | return true; |
| 725 | } |
| 726 | |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 727 | bool 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 Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 734 | FPDF_PAGE page = GetPageForIndex(form_fill_info, doc, page_index); |
| 735 | if (!page) |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 736 | return false; |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame] | 737 | if (options.send_events) |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 738 | SendPageEvents(form, page, events); |
Jane Liu | b35dbad | 2017-08-03 16:29:17 -0400 | [diff] [blame] | 739 | if (options.save_images) |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 740 | WriteImages(page, name.c_str(), page_index); |
Lei Zhang | 0ac7b4e | 2020-07-18 00:35:53 +0000 | [diff] [blame] | 741 | if (options.save_rendered_images) |
| 742 | WriteRenderedImages(doc, page, name.c_str(), page_index); |
Jeremy Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 743 | 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 Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 749 | if (options.output_format == OutputFormat::kPageInfo) { |
Lei Zhang | bb3f58d | 2018-10-16 05:54:09 +0000 | [diff] [blame] | 750 | DumpPageInfo(page, page_index); |
| 751 | return true; |
| 752 | } |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 753 | if (options.output_format == OutputFormat::kStructure) { |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 754 | DumpPageStructure(page, page_index); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 755 | return true; |
| 756 | } |
| 757 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 758 | ScopedFPDFTextPage text_page(FPDFText_LoadPage(page)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 759 | double scale = 1.0; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 760 | if (!options.scale_factor_as_string.empty()) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 761 | std::stringstream(options.scale_factor_as_string) >> scale; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 762 | |
Lei Zhang | dfa075b | 2019-12-05 21:52:43 +0000 | [diff] [blame] | 763 | auto width = static_cast<int>(FPDF_GetPageWidthF(page) * scale); |
| 764 | auto height = static_cast<int>(FPDF_GetPageHeightF(page) * scale); |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 765 | int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 766 | ScopedFPDFBitmap bitmap(FPDFBitmap_Create(width, height, alpha)); |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 767 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 768 | if (bitmap) { |
| 769 | FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 770 | FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 771 | |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 772 | int flags = PageRenderFlagsFromOptions(options); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 773 | 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 Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 777 | FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, flags); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 778 | } else { |
| 779 | IFSDK_PAUSE pause; |
| 780 | pause.version = 1; |
| 781 | pause.NeedToPauseNow = &NeedToPauseNow; |
| 782 | |
Gourab Kundu | d35a62d | 2020-04-24 10:29:27 +0000 | [diff] [blame] | 783 | // 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 Zhang | 939426c | 2018-08-10 16:32:43 +0000 | [diff] [blame] | 793 | while (rv == FPDF_RENDER_TOBECONTINUED) |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 794 | rv = FPDF_RenderPage_Continue(page, &pause); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 795 | } |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 796 | |
Tom Sepez | 8f5d29e | 2019-11-12 00:08:13 +0000 | [diff] [blame] | 797 | FPDF_FFLDraw(form, bitmap.get(), page, 0, 0, width, height, 0, flags); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 798 | |
| 799 | if (!options.render_oneshot) |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 800 | FPDF_RenderPage_Close(page); |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 801 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 802 | int stride = FPDFBitmap_GetStride(bitmap.get()); |
Lei Zhang | 23c5285 | 2019-12-18 23:50:55 +0000 | [diff] [blame] | 803 | void* buffer = FPDFBitmap_GetBuffer(bitmap.get()); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 804 | |
Lei Zhang | fe2cd4d | 2017-11-22 20:04:12 +0000 | [diff] [blame] | 805 | std::string image_file_name; |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 806 | switch (options.output_format) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 807 | #ifdef _WIN32 |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 808 | case OutputFormat::kBmp: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 809 | image_file_name = |
| 810 | WriteBmp(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 811 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 812 | |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 813 | case OutputFormat::kEmf: |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 814 | WriteEmf(page, name.c_str(), page_index); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 815 | break; |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 816 | |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 817 | case OutputFormat::kPs2: |
| 818 | case OutputFormat::kPs3: |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 819 | WritePS(page, name.c_str(), page_index); |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 820 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 821 | #endif |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 822 | case OutputFormat::kText: |
Lei Zhang | f045cb2 | 2020-08-01 01:21:38 +0000 | [diff] [blame] | 823 | WriteText(text_page.get(), name.c_str(), page_index); |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 824 | break; |
| 825 | |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 826 | case OutputFormat::kAnnot: |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 827 | WriteAnnot(page, name.c_str(), page_index); |
Jane Liu | 4fd9a47 | 2017-06-01 18:56:09 -0400 | [diff] [blame] | 828 | break; |
| 829 | |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 830 | case OutputFormat::kPng: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 831 | image_file_name = |
| 832 | WritePng(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 833 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 834 | |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 835 | case OutputFormat::kPpm: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 836 | image_file_name = |
| 837 | WritePpm(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 838 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 839 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 840 | #ifdef PDF_ENABLE_SKIA |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 841 | case OutputFormat::kSkp: { |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 842 | std::unique_ptr<SkPictureRecorder> recorder( |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 843 | reinterpret_cast<SkPictureRecorder*>( |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 844 | FPDF_RenderPageSkp(page, width, height))); |
| 845 | FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 846 | image_file_name = WriteSkp(name.c_str(), page_index, recorder.get()); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 847 | } break; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 848 | #endif |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 849 | default: |
| 850 | break; |
| 851 | } |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 852 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 853 | // Write the filename and the MD5 of the buffer to stdout if we wrote a |
| 854 | // file. |
Lei Zhang | 23c5285 | 2019-12-18 23:50:55 +0000 | [diff] [blame] | 855 | if (options.md5 && !image_file_name.empty()) { |
| 856 | OutputMD5Hash(image_file_name.c_str(), |
| 857 | static_cast<const uint8_t*>(buffer), stride * height); |
| 858 | } |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 859 | } else { |
| 860 | fprintf(stderr, "Page was too large to be rendered.\n"); |
| 861 | } |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 862 | |
Tom Sepez | bfa2a97 | 2017-07-24 11:38:31 -0700 | [diff] [blame] | 863 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 864 | FORM_OnBeforeClosePage(page, form); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 865 | return !!bitmap; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 866 | } |
| 867 | |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 868 | void ProcessPdf(const std::string& name, |
| 869 | const char* buf, |
| 870 | size_t len, |
| 871 | const Options& options, |
| 872 | const std::string& events) { |
Lei Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 873 | TestLoader loader({buf, len}); |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 874 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 875 | FPDF_FILEACCESS file_access = {}; |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 876 | file_access.m_FileLen = static_cast<unsigned long>(len); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 877 | file_access.m_GetBlock = TestLoader::GetBlock; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 878 | file_access.m_Param = &loader; |
| 879 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 880 | FX_FILEAVAIL file_avail = {}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 881 | file_avail.version = 1; |
| 882 | file_avail.IsDataAvail = Is_Data_Avail; |
| 883 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 884 | FX_DOWNLOADHINTS hints = {}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 885 | hints.version = 1; |
| 886 | hints.AddSegment = Add_Segment; |
| 887 | |
Lei Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 888 | // |pdf_avail| must outlive |doc|. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 889 | ScopedFPDFAvail pdf_avail(FPDFAvail_Create(&file_avail, &file_access)); |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 890 | |
Lei Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 891 | // |doc| must outlive |form_callbacks.loaded_pages|. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 892 | ScopedFPDFDocument doc; |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 893 | |
Lei Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 894 | const char* password = |
| 895 | options.password.empty() ? nullptr : options.password.c_str(); |
Lei Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 896 | bool is_linearized = false; |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 897 | if (options.use_load_mem_document) { |
Lei Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 898 | doc.reset(FPDF_LoadMemDocument(buf, len, password)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 899 | } else { |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 900 | if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) { |
| 901 | int avail_status = PDF_DATA_NOTAVAIL; |
Lei Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 902 | doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), password)); |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 903 | 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 Zhang | 5c44e5c | 2019-12-13 17:22:38 +0000 | [diff] [blame] | 922 | doc.reset(FPDF_LoadCustomDocument(&file_access, password)); |
Lei Zhang | 793c135 | 2019-07-12 17:35:57 +0000 | [diff] [blame] | 923 | } |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 924 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 925 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 926 | if (!doc) { |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 927 | PrintLastError(); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
Lei Zhang | 1068985 | 2019-01-08 20:14:47 +0000 | [diff] [blame] | 931 | if (!FPDF_DocumentHasValidCrossReferenceTable(doc.get())) |
| 932 | fprintf(stderr, "Document has invalid cross reference table\n"); |
| 933 | |
Ryan Harrison | 6cec70a | 2018-06-05 14:06:10 +0000 | [diff] [blame] | 934 | (void)FPDF_GetDocPermissions(doc.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 935 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 936 | if (options.show_metadata) |
| 937 | DumpMetaData(doc.get()); |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 938 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 939 | if (options.save_attachments) |
Dan Sinclair | cb87682 | 2018-03-22 16:21:04 +0000 | [diff] [blame] | 940 | WriteAttachments(doc.get(), name); |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 941 | |
Lei Zhang | a41801e | 2018-08-23 21:59:33 +0000 | [diff] [blame] | 942 | #ifdef PDF_ENABLE_V8 |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 943 | IPDF_JSPLATFORM platform_callbacks = {}; |
| 944 | platform_callbacks.version = 3; |
| 945 | platform_callbacks.app_alert = ExampleAppAlert; |
Tom Sepez | bcb8152 | 2018-05-15 17:46:15 +0000 | [diff] [blame] | 946 | platform_callbacks.app_beep = ExampleAppBeep; |
Tom Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 947 | platform_callbacks.app_response = ExampleAppResponse; |
| 948 | platform_callbacks.Doc_getFilePath = ExampleDocGetFilePath; |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 949 | platform_callbacks.Doc_mail = ExampleDocMail; |
Tom Sepez | 0e5bab1 | 2018-10-18 21:39:40 +0000 | [diff] [blame] | 950 | platform_callbacks.Doc_print = ExampleDocPrint; |
Tom Sepez | 1f120c7 | 2018-10-18 22:54:02 +0000 | [diff] [blame] | 951 | platform_callbacks.Doc_submitForm = ExampleDocSubmitForm; |
| 952 | platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
| 953 | platform_callbacks.Field_browse = ExampleFieldBrowse; |
Lei Zhang | a41801e | 2018-08-23 21:59:33 +0000 | [diff] [blame] | 954 | #endif // PDF_ENABLE_V8 |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 955 | |
| 956 | FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {}; |
| 957 | #ifdef PDF_ENABLE_XFA |
| 958 | form_callbacks.version = 2; |
Daniel Hosseinian | a793e97 | 2020-01-24 02:51:31 +0000 | [diff] [blame] | 959 | form_callbacks.xfa_disabled = |
| 960 | options.disable_xfa || options.disable_javascript; |
Tom Sepez | 094f8e6 | 2020-04-01 21:01:44 +0000 | [diff] [blame] | 961 | form_callbacks.FFI_PopupMenu = ExamplePopupMenu; |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 962 | #else // PDF_ENABLE_XFA |
| 963 | form_callbacks.version = 1; |
| 964 | #endif // PDF_ENABLE_XFA |
| 965 | form_callbacks.FFI_GetPage = GetPageForIndex; |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 966 | |
| 967 | #ifdef PDF_ENABLE_V8 |
| 968 | if (!options.disable_javascript) |
| 969 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 970 | #endif // PDF_ENABLE_V8 |
Dan Sinclair | 0421202 | 2018-03-21 18:08:26 +0000 | [diff] [blame] | 971 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 972 | ScopedFPDFFormHandle form( |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 973 | FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks)); |
| 974 | form_callbacks.form_handle = form.get(); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 975 | |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 976 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 977 | 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 Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 983 | } |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 984 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 985 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 986 | FPDF_SetFormFieldHighlightColor(form.get(), FPDF_FORMFIELD_UNKNOWN, 0xFFE4DD); |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 987 | FPDF_SetFormFieldHighlightAlpha(form.get(), 100); |
| 988 | FORM_DoDocumentJSAction(form.get()); |
| 989 | FORM_DoDocumentOpenAction(form.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 990 | |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 991 | #if _WIN32 |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 992 | if (options.output_format == OutputFormat::kPs2) |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 993 | FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT2); |
Lei Zhang | 38235eb | 2020-07-28 06:38:37 +0000 | [diff] [blame] | 994 | else if (options.output_format == OutputFormat::kPs3) |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 995 | FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3); |
Lei Zhang | d1c9b45 | 2017-05-05 17:21:36 -0700 | [diff] [blame] | 996 | #endif |
| 997 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 998 | int page_count = FPDF_GetPageCount(doc.get()); |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 999 | int processed_pages = 0; |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 1000 | int bad_pages = 0; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1001 | 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 Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 1004 | 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); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1008 | |
Lei Zhang | d58ff63 | 2019-07-12 16:50:04 +0000 | [diff] [blame] | 1009 | if (avail_status == PDF_DATA_ERROR) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 1010 | fprintf(stderr, "Unknown error in checking if page %d is available.\n", |
| 1011 | i); |
| 1012 | return; |
| 1013 | } |
| 1014 | } |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 1015 | if (ProcessPage(name, doc.get(), form.get(), &form_callbacks, i, options, |
| 1016 | events)) { |
| 1017 | ++processed_pages; |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1018 | } else { |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 1019 | ++bad_pages; |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1020 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 1023 | FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC); |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 1024 | fprintf(stderr, "Processed %d pages.\n", processed_pages); |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 1025 | if (bad_pages) |
| 1026 | fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1029 | void ShowConfig() { |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1030 | std::string config; |
| 1031 | std::string maybe_comma; |
Ryan Harrison | dd8da5e | 2018-05-17 20:43:42 +0000 | [diff] [blame] | 1032 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1033 | 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 sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 1047 | #ifdef PDF_ENABLE_ASAN |
| 1048 | config.append(maybe_comma); |
| 1049 | config.append("ASAN"); |
| 1050 | maybe_comma = ","; |
| 1051 | #endif // PDF_ENABLE_ASAN |
Lei Zhang | 2cb8f85 | 2019-09-16 19:39:16 +0000 | [diff] [blame] | 1052 | #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 Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1061 | printf("%s\n", config.c_str()); |
| 1062 | } |
| 1063 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1064 | constexpr char kUsageString[] = |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1065 | "Usage: pdfium_test [OPTION] [FILE]...\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1066 | " --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 Zhang | b82d907 | 2019-11-20 21:20:57 +0000 | [diff] [blame] | 1087 | "format\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1088 | " --save-attachments - write embedded attachments " |
Jane Liu | 4442d45 | 2017-07-19 10:19:42 -0400 | [diff] [blame] | 1089 | "<pdf-name>.attachment.<attachment-name>\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1090 | " --save-images - write raw embedded images " |
Jane Liu | b35dbad | 2017-08-03 16:29:17 -0400 | [diff] [blame] | 1091 | "<pdf-name>.<page-number>.<object-number>.png\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1092 | " --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 Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 1095 | "<pdf-name>.thumbnail.<page-number>.png\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1096 | " --save-thumbs-dec - write page thumbnails' decoded stream data" |
Jeremy Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 1097 | "<pdf-name>.thumbnail.decoded.<page-number>.png\n" |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1098 | " --save-thumbs-raw - write page thumbnails' raw stream data" |
Jeremy Chinsen | 801e1bf | 2019-06-25 20:46:13 +0000 | [diff] [blame] | 1099 | "<pdf-name>.thumbnail.raw.<page-number>.png\n" |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 1100 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1101 | " --disable-javascript - do not execute JS in PDF files\n" |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 1102 | " --js-flags=<flags> - additional flags to pas to V8" |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 1103 | #ifdef PDF_ENABLE_XFA |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1104 | " --disable-xfa - do not process XFA forms\n" |
Tom Sepez | 0784c73 | 2018-04-23 18:02:57 +0000 | [diff] [blame] | 1105 | #endif // PDF_ENABLE_XFA |
| 1106 | #endif // PDF_ENABLE_V8 |
Henrique Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 1107 | #ifdef ENABLE_CALLGRIND |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1108 | " --callgrind-delim - delimit interesting section when using " |
Lei Zhang | f066bf7 | 2019-04-26 17:53:16 +0000 | [diff] [blame] | 1109 | "callgrind\n" |
| 1110 | #endif |
Lei Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 1111 | #if defined(__APPLE__) || (defined(__linux__) && !defined(__ANDROID__)) |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1112 | " --no-system-fonts - do not use system fonts, overrides --font-dir\n" |
Lei Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 1113 | #endif |
Lei Zhang | 3b1a8b4 | 2020-08-01 01:12:48 +0000 | [diff] [blame] | 1114 | " --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" |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1118 | " --pages=<number>(-<number>) - only render the given 0-based page(s)\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1119 | #ifdef _WIN32 |
Jane Liu | 57f228d | 2017-07-13 18:10:30 -0400 | [diff] [blame] | 1120 | " --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 Zhang | f066bf7 | 2019-04-26 17:53:16 +0000 | [diff] [blame] | 1126 | #endif |
Jane Liu | 57f228d | 2017-07-13 18:10:30 -0400 | [diff] [blame] | 1127 | " --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 Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1131 | #ifdef PDF_ENABLE_SKIA |
Jane Liu | 57f228d | 2017-07-13 18:10:30 -0400 | [diff] [blame] | 1132 | " --skp - write page images <pdf-name>.<page-number>.skp\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1133 | #endif |
Jane Liu | 57f228d | 2017-07-13 18:10:30 -0400 | [diff] [blame] | 1134 | " --md5 - write output image paths and their md5 hashes to stdout.\n" |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 1135 | " --time=<number> - Seconds since the epoch to set system time.\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 1136 | ""; |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 1137 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1138 | } // namespace |
| 1139 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1140 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1141 | std::vector<std::string> args(argv, argv + argc); |
| 1142 | Options options; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1143 | std::vector<std::string> files; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1144 | if (!ParseCommandLine(args, &options, &files)) { |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1145 | fprintf(stderr, "%s", kUsageString); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1146 | return 1; |
| 1147 | } |
| 1148 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 1149 | 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 Sepez | a72e8e2c | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1159 | FPDF_LIBRARY_CONFIG config; |
Tom Sepez | a7858e6 | 2020-06-10 00:38:19 +0000 | [diff] [blame] | 1160 | config.version = 3; |
Tom Sepez | a72e8e2c | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1161 | config.m_pUserFontPaths = nullptr; |
| 1162 | config.m_pIsolate = nullptr; |
| 1163 | config.m_v8EmbedderSlot = 0; |
Tom Sepez | a7858e6 | 2020-06-10 00:38:19 +0000 | [diff] [blame] | 1164 | config.m_pPlatform = nullptr; |
Tom Sepez | a72e8e2c | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1165 | |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 1166 | #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 Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 1175 | options.exe_path, options.js_flags, options.bin_directory, &snapshot); |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 1176 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | e0d3c50 | 2020-08-11 23:51:10 +0000 | [diff] [blame] | 1177 | platform = InitializeV8ForPDFium(options.exe_path, options.js_flags); |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 1178 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | bd5b860 | 2020-11-12 19:45:55 +0000 | [diff] [blame] | 1179 | if (!platform) { |
| 1180 | fprintf(stderr, "V8 initialization failed.\n"); |
| 1181 | return 1; |
| 1182 | } |
Tom Sepez | a7858e6 | 2020-06-10 00:38:19 +0000 | [diff] [blame] | 1183 | config.m_pPlatform = platform.get(); |
| 1184 | |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 1185 | 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 Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 1193 | 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 Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1197 | config.m_pUserFontPaths = path_array; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1198 | } |
Lei Zhang | d0f989e | 2019-04-26 18:03:13 +0000 | [diff] [blame] | 1199 | |
Tom Sepez | a72e8e2c | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1200 | FPDF_InitLibraryWithConfig(&config); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1201 | |
Lei Zhang | d2be646 | 2017-07-21 14:31:21 -0700 | [diff] [blame] | 1202 | UNSUPPORT_INFO unsupported_info = {}; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1203 | unsupported_info.version = 1; |
| 1204 | unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1205 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1206 | FSDK_SetUnSpObjProcessHandler(&unsupported_info); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1207 | |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 1208 | 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 Sepez | 706e187 | 2018-10-22 19:34:53 +0000 | [diff] [blame] | 1212 | FSDK_SetTimeFunction([]() { return time_ret; }); |
| 1213 | FSDK_SetLocaltimeFunction([](const time_t* tp) { return gmtime(tp); }); |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1216 | for (const std::string& filename : files) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1217 | size_t file_length = 0; |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 1218 | std::unique_ptr<char, pdfium::FreeDeleter> file_contents = |
| 1219 | GetFileContents(filename.c_str(), &file_length); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1220 | if (!file_contents) |
| 1221 | continue; |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 1222 | fprintf(stderr, "Processing PDF file %s.\n", filename.c_str()); |
Henrique Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 1223 | |
| 1224 | #ifdef ENABLE_CALLGRIND |
| 1225 | if (options.callgrind_delimiters) |
| 1226 | CALLGRIND_START_INSTRUMENTATION; |
| 1227 | #endif // ENABLE_CALLGRIND |
| 1228 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1229 | 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"); |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 1236 | 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 | } |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
Lei Zhang | 148588f | 2020-07-28 06:31:57 +0000 | [diff] [blame] | 1248 | ProcessPdf(filename, file_contents.get(), file_length, options, events); |
Henrique Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 1249 | |
Tom Sepez | ae8ab22 | 2020-06-08 23:39:03 +0000 | [diff] [blame] | 1250 | #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 Nakashima | 95ea778 | 2017-07-11 16:42:43 -0400 | [diff] [blame] | 1261 | #ifdef ENABLE_CALLGRIND |
| 1262 | if (options.callgrind_delimiters) |
| 1263 | CALLGRIND_STOP_INSTRUMENTATION; |
| 1264 | #endif // ENABLE_CALLGRIND |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | FPDF_DestroyLibrary(); |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 1268 | |
Tom Sepez | 27e1a74 | 2019-02-15 19:45:38 +0000 | [diff] [blame] | 1269 | #ifdef PDF_ENABLE_V8 |
| 1270 | if (!options.disable_javascript) { |
Tom Sepez | dba19d5 | 2020-06-04 23:01:17 +0000 | [diff] [blame] | 1271 | isolate.reset(); |
Tom Sepez | 27e1a74 | 2019-02-15 19:45:38 +0000 | [diff] [blame] | 1272 | v8::V8::ShutdownPlatform(); |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 1273 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 27e1a74 | 2019-02-15 19:45:38 +0000 | [diff] [blame] | 1274 | free(const_cast<char*>(snapshot.data)); |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 1275 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 27e1a74 | 2019-02-15 19:45:38 +0000 | [diff] [blame] | 1276 | } |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1277 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1278 | |
| 1279 | return 0; |
| 1280 | } |