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 | |
| 5 | #include <limits.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 10 | #include <map> |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 11 | #include <sstream> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
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 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 16 | #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_ |
| 17 | #define _SKIA_SUPPORT_ |
| 18 | #endif |
| 19 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 20 | #include "core/fdrm/crypto/fx_crypt.h" |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 21 | #include "public/cpp/fpdf_deleters.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 22 | #include "public/fpdf_dataavail.h" |
Lei Zhang | 453d96b | 2015-12-31 13:13:10 -0800 | [diff] [blame] | 23 | #include "public/fpdf_edit.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 24 | #include "public/fpdf_ext.h" |
| 25 | #include "public/fpdf_formfill.h" |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 26 | #include "public/fpdf_structtree.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 27 | #include "public/fpdf_text.h" |
| 28 | #include "public/fpdfview.h" |
Dan Sinclair | efbc191 | 2016-02-17 16:54:43 -0500 | [diff] [blame] | 29 | #include "samples/image_diff_png.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 30 | #include "testing/test_support.h" |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 31 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 32 | #ifdef _WIN32 |
| 33 | #include <io.h> |
| 34 | #else |
| 35 | #include <unistd.h> |
| 36 | #endif |
| 37 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 38 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 39 | #include "v8/include/libplatform/libplatform.h" |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 40 | #include "v8/include/v8.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 41 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 42 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 43 | #ifdef PDF_ENABLE_SKIA |
| 44 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 45 | #include "third_party/skia/include/core/SkStream.h" |
| 46 | #endif |
| 47 | |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 48 | #ifdef _WIN32 |
| 49 | #define access _access |
| 50 | #define snprintf _snprintf |
| 51 | #define R_OK 4 |
| 52 | #endif |
| 53 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 54 | enum OutputFormat { |
| 55 | OUTPUT_NONE, |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 56 | OUTPUT_STRUCTURE, |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 57 | OUTPUT_TEXT, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 58 | OUTPUT_PPM, |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 59 | OUTPUT_PNG, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 60 | #ifdef _WIN32 |
| 61 | OUTPUT_BMP, |
| 62 | OUTPUT_EMF, |
| 63 | #endif |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 64 | #ifdef PDF_ENABLE_SKIA |
| 65 | OUTPUT_SKP, |
| 66 | #endif |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 69 | struct Options { |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 70 | Options() |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 71 | : show_config(false), |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 72 | send_events(false), |
| 73 | pages(false), |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 74 | md5(false), |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 75 | output_format(OUTPUT_NONE) {} |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 76 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 77 | bool show_config; |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 78 | bool send_events; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 79 | bool pages; |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 80 | bool md5; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 81 | OutputFormat output_format; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 82 | std::string scale_factor_as_string; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 83 | std::string exe_path; |
| 84 | std::string bin_directory; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 85 | std::string font_directory; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 86 | // 0-based page numbers to be rendered. |
| 87 | int first_page; |
| 88 | int last_page; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 91 | struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO { |
| 92 | // Hold a map of the currently loaded pages in order to avoid them |
| 93 | // to get loaded twice. |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 94 | std::map<int, FPDF_PAGE> loaded_pages; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 95 | |
| 96 | // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can |
| 97 | // make use of it. |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 98 | FPDF_FORMHANDLE form_handle; |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo( |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 102 | FPDF_FORMFILLINFO* form_fill_info) { |
| 103 | return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 106 | static bool CheckDimensions(int stride, int width, int height) { |
| 107 | if (stride < 0 || width < 0 || height < 0) |
| 108 | return false; |
| 109 | if (height > 0 && width > INT_MAX / height) |
| 110 | return false; |
| 111 | return true; |
| 112 | } |
| 113 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 114 | static void OutputMD5Hash(const char* file_name, const char* buffer, int len) { |
| 115 | // Get the MD5 hash and write it to stdout. |
| 116 | uint8_t digest[16]; |
| 117 | CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(buffer), len, digest); |
| 118 | printf("MD5:%s:", file_name); |
| 119 | for (int i = 0; i < 16; i++) |
| 120 | printf("%02x", digest[i]); |
| 121 | printf("\n"); |
| 122 | } |
| 123 | |
| 124 | static std::string WritePpm(const char* pdf_name, |
| 125 | int num, |
| 126 | const void* buffer_void, |
| 127 | int stride, |
| 128 | int width, |
| 129 | int height) { |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 130 | const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 131 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 132 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 133 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 134 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 135 | int out_len = width * height; |
| 136 | if (out_len > INT_MAX / 3) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 137 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 138 | out_len *= 3; |
| 139 | |
| 140 | char filename[256]; |
| 141 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
John Abd-El-Malek | a548d30 | 2014-06-26 10:18:11 -0700 | [diff] [blame] | 142 | FILE* fp = fopen(filename, "wb"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 143 | if (!fp) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 144 | return ""; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 145 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 146 | // Source data is B, G, R, unused. |
| 147 | // Dest data is R, G, B. |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 148 | std::vector<char> result(out_len); |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 149 | for (int h = 0; h < height; ++h) { |
| 150 | const char* src_line = buffer + (stride * h); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 151 | char* dest_line = result.data() + (width * h * 3); |
Lei Zhang | e00660b | 2015-08-13 15:40:18 -0700 | [diff] [blame] | 152 | for (int w = 0; w < width; ++w) { |
| 153 | // R |
| 154 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 155 | // G |
| 156 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 157 | // B |
| 158 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 159 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | } |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 161 | fwrite(result.data(), out_len, 1, fp); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 162 | fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 163 | return std::string(filename); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 164 | } |
| 165 | |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 166 | void WriteText(FPDF_PAGE page, const char* pdf_name, int num) { |
| 167 | char filename[256]; |
| 168 | int chars_formatted = |
| 169 | snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num); |
| 170 | if (chars_formatted < 0 || |
| 171 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 172 | fprintf(stderr, "Filename %s is too long\n", filename); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | FILE* fp = fopen(filename, "w"); |
| 177 | if (!fp) { |
| 178 | fprintf(stderr, "Failed to open %s for output\n", filename); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // Output in UTF32-LE. |
| 183 | uint32_t bom = 0x0000FEFF; |
| 184 | fwrite(&bom, sizeof(bom), 1, fp); |
| 185 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 186 | std::unique_ptr<void, FPDFTextPageDeleter> textpage(FPDFText_LoadPage(page)); |
| 187 | for (int i = 0; i < FPDFText_CountChars(textpage.get()); i++) { |
| 188 | uint32_t c = FPDFText_GetUnicode(textpage.get(), i); |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 189 | fwrite(&c, sizeof(c), 1, fp); |
| 190 | } |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 191 | (void)fclose(fp); |
| 192 | } |
| 193 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 194 | static std::string WritePng(const char* pdf_name, |
| 195 | int num, |
| 196 | const void* buffer_void, |
| 197 | int stride, |
| 198 | int width, |
| 199 | int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 200 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 201 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 202 | |
| 203 | std::vector<unsigned char> png_encoding; |
| 204 | const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); |
| 205 | if (!image_diff_png::EncodeBGRAPNG( |
| 206 | buffer, width, height, stride, false, &png_encoding)) { |
| 207 | fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 208 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | char filename[256]; |
| 212 | int chars_formatted = snprintf( |
| 213 | filename, sizeof(filename), "%s.%d.png", pdf_name, num); |
| 214 | if (chars_formatted < 0 || |
| 215 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 216 | fprintf(stderr, "Filename %s is too long\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 217 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | FILE* fp = fopen(filename, "wb"); |
| 221 | if (!fp) { |
| 222 | fprintf(stderr, "Failed to open %s for output\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 223 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | size_t bytes_written = fwrite( |
| 227 | &png_encoding.front(), 1, png_encoding.size(), fp); |
| 228 | if (bytes_written != png_encoding.size()) |
| 229 | fprintf(stderr, "Failed to write to %s\n", filename); |
| 230 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 231 | (void)fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 232 | return std::string(filename); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 235 | #ifdef _WIN32 |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 236 | static std::string WriteBmp(const char* pdf_name, |
| 237 | int num, |
| 238 | const void* buffer, |
| 239 | int stride, |
| 240 | int width, |
| 241 | int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 242 | if (!CheckDimensions(stride, width, height)) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 243 | return ""; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 244 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 245 | int out_len = stride * height; |
| 246 | if (out_len > INT_MAX / 3) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 247 | return ""; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 248 | |
| 249 | char filename[256]; |
| 250 | snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); |
| 251 | FILE* fp = fopen(filename, "wb"); |
| 252 | if (!fp) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 253 | return ""; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 254 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 255 | BITMAPINFO bmi = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 256 | bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); |
| 257 | bmi.bmiHeader.biWidth = width; |
| 258 | bmi.bmiHeader.biHeight = -height; // top-down image |
| 259 | bmi.bmiHeader.biPlanes = 1; |
| 260 | bmi.bmiHeader.biBitCount = 32; |
| 261 | bmi.bmiHeader.biCompression = BI_RGB; |
| 262 | bmi.bmiHeader.biSizeImage = 0; |
| 263 | |
Nico Weber | 2827bdd | 2015-07-01 14:08:08 -0700 | [diff] [blame] | 264 | BITMAPFILEHEADER file_header = {}; |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 265 | file_header.bfType = 0x4d42; |
| 266 | file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; |
| 267 | file_header.bfOffBits = file_header.bfSize - out_len; |
| 268 | |
| 269 | fwrite(&file_header, sizeof(file_header), 1, fp); |
| 270 | fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); |
| 271 | fwrite(buffer, out_len, 1, fp); |
| 272 | fclose(fp); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 273 | return std::string(filename); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
| 277 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 278 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 279 | |
| 280 | char filename[256]; |
| 281 | snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 282 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 283 | HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 284 | |
| 285 | HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 286 | SelectClipRgn(dc, rgn); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 287 | DeleteObject(rgn); |
| 288 | |
| 289 | SelectObject(dc, GetStockObject(NULL_PEN)); |
| 290 | SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 291 | // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 292 | Rectangle(dc, 0, 0, width + 1, height + 1); |
| 293 | |
| 294 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 295 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 296 | |
| 297 | DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 298 | } |
| 299 | #endif |
| 300 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 301 | #ifdef PDF_ENABLE_SKIA |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 302 | static std::string WriteSkp(const char* pdf_name, |
| 303 | int num, |
| 304 | SkPictureRecorder* recorder) { |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 305 | char filename[256]; |
| 306 | int chars_formatted = |
| 307 | snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num); |
| 308 | |
| 309 | if (chars_formatted < 0 || |
| 310 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 311 | fprintf(stderr, "Filename %s is too long\n", filename); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 312 | return ""; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 313 | } |
| 314 | |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 315 | sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture()); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 316 | SkFILEWStream wStream(filename); |
| 317 | picture->serialize(&wStream); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 318 | return std::string(filename); |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 319 | } |
| 320 | #endif |
| 321 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 322 | // These example JS platform callback handlers are entirely optional, |
| 323 | // and exist here to show the flow of information from a document back |
| 324 | // to the embedder. |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 325 | int ExampleAppAlert(IPDF_JSPLATFORM*, |
| 326 | FPDF_WIDESTRING msg, |
| 327 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 328 | int type, |
| 329 | int icon) { |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 330 | printf("%ls", GetPlatformWString(title).c_str()); |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 331 | if (icon || type) |
| 332 | printf("[icon=%d,type=%d]", icon, type); |
Tom Sepez | bd93257 | 2016-01-29 09:10:41 -0800 | [diff] [blame] | 333 | printf(": %ls\n", GetPlatformWString(msg).c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 337 | int ExampleAppResponse(IPDF_JSPLATFORM*, |
| 338 | FPDF_WIDESTRING question, |
| 339 | FPDF_WIDESTRING title, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 340 | FPDF_WIDESTRING default_value, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 341 | FPDF_WIDESTRING label, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 342 | FPDF_BOOL is_password, |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 343 | void* response, |
| 344 | int length) { |
| 345 | printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n", |
| 346 | GetPlatformWString(title).c_str(), |
| 347 | GetPlatformWString(question).c_str(), |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 348 | GetPlatformWString(default_value).c_str(), |
| 349 | GetPlatformWString(label).c_str(), is_password, length); |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 350 | |
| 351 | // UTF-16, always LE regardless of platform. |
| 352 | uint8_t* ptr = static_cast<uint8_t*>(response); |
| 353 | ptr[0] = 'N'; |
| 354 | ptr[1] = 0; |
| 355 | ptr[2] = 'o'; |
| 356 | ptr[3] = 0; |
| 357 | return 4; |
| 358 | } |
| 359 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 360 | void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) { |
| 361 | printf("Goto Page: %d\n", page_number); |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 364 | void ExampleDocMail(IPDF_JSPLATFORM*, |
| 365 | void* mailData, |
| 366 | int length, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 367 | FPDF_BOOL UI, |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 368 | FPDF_WIDESTRING To, |
| 369 | FPDF_WIDESTRING Subject, |
| 370 | FPDF_WIDESTRING CC, |
| 371 | FPDF_WIDESTRING BCC, |
| 372 | FPDF_WIDESTRING Msg) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 373 | 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] | 374 | GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(), |
| 375 | GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(), |
| 376 | GetPlatformWString(Msg).c_str()); |
| 377 | } |
| 378 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 379 | void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 380 | std::string feature = "Unknown"; |
| 381 | switch (type) { |
| 382 | case FPDF_UNSP_DOC_XFAFORM: |
| 383 | feature = "XFA"; |
| 384 | break; |
| 385 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 386 | feature = "Portfolios_Packages"; |
| 387 | break; |
| 388 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 389 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 390 | feature = "Attachment"; |
| 391 | break; |
| 392 | case FPDF_UNSP_DOC_SECURITY: |
| 393 | feature = "Rights_Management"; |
| 394 | break; |
| 395 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 396 | feature = "Shared_Review"; |
| 397 | break; |
| 398 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 399 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 400 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 401 | feature = "Shared_Form"; |
| 402 | break; |
| 403 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 404 | feature = "3D"; |
| 405 | break; |
| 406 | case FPDF_UNSP_ANNOT_MOVIE: |
| 407 | feature = "Movie"; |
| 408 | break; |
| 409 | case FPDF_UNSP_ANNOT_SOUND: |
| 410 | feature = "Sound"; |
| 411 | break; |
| 412 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 413 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 414 | feature = "Screen"; |
| 415 | break; |
| 416 | case FPDF_UNSP_ANNOT_SIG: |
| 417 | feature = "Digital_Signature"; |
| 418 | break; |
| 419 | } |
| 420 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 421 | } |
| 422 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 423 | bool ParseCommandLine(const std::vector<std::string>& args, |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 424 | Options* options, |
| 425 | std::vector<std::string>* files) { |
| 426 | if (args.empty()) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 427 | return false; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 428 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 429 | options->exe_path = args[0]; |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 430 | size_t cur_idx = 1; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 431 | for (; cur_idx < args.size(); ++cur_idx) { |
| 432 | const std::string& cur_arg = args[cur_idx]; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 433 | if (cur_arg == "--show-config") { |
| 434 | options->show_config = true; |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 435 | } else if (cur_arg == "--send-events") { |
| 436 | options->send_events = true; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 437 | } else if (cur_arg == "--ppm") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 438 | if (options->output_format != OUTPUT_NONE) { |
| 439 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 440 | return false; |
| 441 | } |
| 442 | options->output_format = OUTPUT_PPM; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 443 | } else if (cur_arg == "--png") { |
| 444 | if (options->output_format != OUTPUT_NONE) { |
| 445 | fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 446 | return false; |
| 447 | } |
| 448 | options->output_format = OUTPUT_PNG; |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 449 | } else if (cur_arg == "--txt") { |
| 450 | if (options->output_format != OUTPUT_NONE) { |
| 451 | fprintf(stderr, "Duplicate or conflicting --txt argument\n"); |
| 452 | return false; |
| 453 | } |
| 454 | options->output_format = OUTPUT_TEXT; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 455 | #ifdef PDF_ENABLE_SKIA |
| 456 | } else if (cur_arg == "--skp") { |
| 457 | if (options->output_format != OUTPUT_NONE) { |
| 458 | fprintf(stderr, "Duplicate or conflicting --skp argument\n"); |
| 459 | return false; |
| 460 | } |
| 461 | options->output_format = OUTPUT_SKP; |
| 462 | #endif |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 463 | } else if (cur_arg.size() > 11 && |
| 464 | cur_arg.compare(0, 11, "--font-dir=") == 0) { |
| 465 | if (!options->font_directory.empty()) { |
| 466 | fprintf(stderr, "Duplicate --font-dir argument\n"); |
| 467 | return false; |
| 468 | } |
| 469 | options->font_directory = cur_arg.substr(11); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 470 | #ifdef _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 471 | } else if (cur_arg == "--emf") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 472 | if (options->output_format != OUTPUT_NONE) { |
| 473 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 474 | return false; |
| 475 | } |
| 476 | options->output_format = OUTPUT_EMF; |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 477 | } else if (cur_arg == "--bmp") { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 478 | if (options->output_format != OUTPUT_NONE) { |
| 479 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 480 | return false; |
| 481 | } |
| 482 | options->output_format = OUTPUT_BMP; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 483 | #endif // _WIN32 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 484 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 485 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 486 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 487 | } else if (cur_arg.size() > 10 && |
| 488 | cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 489 | if (!options->bin_directory.empty()) { |
| 490 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 491 | return false; |
| 492 | } |
| 493 | options->bin_directory = cur_arg.substr(10); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 494 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 495 | #endif // PDF_ENABLE_V8 |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 496 | |
| 497 | } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 498 | if (!options->scale_factor_as_string.empty()) { |
| 499 | fprintf(stderr, "Duplicate --scale argument\n"); |
| 500 | return false; |
| 501 | } |
| 502 | options->scale_factor_as_string = cur_arg.substr(8); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 503 | } else if (cur_arg == "--show-structure") { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 504 | if (options->output_format != OUTPUT_NONE) { |
| 505 | fprintf(stderr, "Duplicate or conflicting --show-structure argument\n"); |
| 506 | return false; |
| 507 | } |
| 508 | options->output_format = OUTPUT_STRUCTURE; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 509 | } else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--pages=") == 0) { |
| 510 | if (options->pages) { |
| 511 | fprintf(stderr, "Duplicate --pages argument\n"); |
| 512 | return false; |
| 513 | } |
| 514 | options->pages = true; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 515 | const std::string pages_string = cur_arg.substr(8); |
| 516 | size_t first_dash = pages_string.find("-"); |
| 517 | if (first_dash == std::string::npos) { |
| 518 | std::stringstream(pages_string) >> options->first_page; |
| 519 | options->last_page = options->first_page; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 520 | } else { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 521 | std::stringstream(pages_string.substr(0, first_dash)) >> |
| 522 | options->first_page; |
| 523 | std::stringstream(pages_string.substr(first_dash + 1)) >> |
| 524 | options->last_page; |
npm | e3c7315 | 2016-11-14 09:14:52 -0800 | [diff] [blame] | 525 | } |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 526 | } else if (cur_arg == "--md5") { |
| 527 | options->md5 = true; |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 528 | } else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') { |
| 529 | fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str()); |
| 530 | return false; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 531 | } else { |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 532 | break; |
Dan Sinclair | 738b08c | 2016-03-01 14:45:20 -0500 | [diff] [blame] | 533 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 534 | } |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 535 | for (size_t i = cur_idx; i < args.size(); i++) |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 536 | files->push_back(args[i]); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 537 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 541 | 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] | 542 | return true; |
| 543 | } |
| 544 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 545 | 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] | 546 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 547 | void SendPageEvents(const FPDF_FORMHANDLE& form, |
| 548 | const FPDF_PAGE& page, |
| 549 | const std::string& events) { |
| 550 | auto lines = StringSplit(events, '\n'); |
| 551 | for (auto line : lines) { |
| 552 | auto command = StringSplit(line, '#'); |
| 553 | if (command[0].empty()) |
| 554 | continue; |
| 555 | auto tokens = StringSplit(command[0], ','); |
| 556 | if (tokens[0] == "keycode") { |
| 557 | if (tokens.size() == 2) { |
| 558 | int keycode = atoi(tokens[1].c_str()); |
| 559 | FORM_OnKeyDown(form, page, keycode, 0); |
| 560 | FORM_OnKeyUp(form, page, keycode, 0); |
| 561 | } else { |
| 562 | fprintf(stderr, "keycode: bad args\n"); |
| 563 | } |
| 564 | } else if (tokens[0] == "mousedown") { |
| 565 | if (tokens.size() == 4) { |
| 566 | int x = atoi(tokens[2].c_str()); |
| 567 | int y = atoi(tokens[3].c_str()); |
| 568 | if (tokens[1] == "left") |
| 569 | FORM_OnLButtonDown(form, page, 0, x, y); |
| 570 | #ifdef PDF_ENABLE_XFA |
| 571 | else if (tokens[1] == "right") |
| 572 | FORM_OnRButtonDown(form, page, 0, x, y); |
| 573 | #endif |
| 574 | else |
| 575 | fprintf(stderr, "mousedown: bad button name\n"); |
| 576 | } else { |
| 577 | fprintf(stderr, "mousedown: bad args\n"); |
| 578 | } |
| 579 | } else if (tokens[0] == "mouseup") { |
| 580 | if (tokens.size() == 4) { |
| 581 | int x = atoi(tokens[2].c_str()); |
| 582 | int y = atoi(tokens[3].c_str()); |
| 583 | if (tokens[1] == "left") |
| 584 | FORM_OnLButtonUp(form, page, 0, x, y); |
| 585 | #ifdef PDF_ENABLE_XFA |
| 586 | else if (tokens[1] == "right") |
| 587 | FORM_OnRButtonUp(form, page, 0, x, y); |
| 588 | #endif |
| 589 | else |
| 590 | fprintf(stderr, "mouseup: bad button name\n"); |
| 591 | } else { |
| 592 | fprintf(stderr, "mouseup: bad args\n"); |
| 593 | } |
| 594 | } else if (tokens[0] == "mousemove") { |
| 595 | if (tokens.size() == 3) { |
| 596 | int x = atoi(tokens[1].c_str()); |
| 597 | int y = atoi(tokens[2].c_str()); |
| 598 | FORM_OnMouseMove(form, page, 0, x, y); |
| 599 | } else { |
| 600 | fprintf(stderr, "mousemove: bad args\n"); |
| 601 | } |
| 602 | } else { |
| 603 | fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 608 | FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, |
| 609 | FPDF_DOCUMENT doc, |
| 610 | int index) { |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 611 | FPDF_FORMFILLINFO_PDFiumTest* form_fill_info = |
| 612 | ToPDFiumTestFormFillInfo(param); |
| 613 | auto& loaded_pages = form_fill_info->loaded_pages; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 614 | auto iter = loaded_pages.find(index); |
| 615 | if (iter != loaded_pages.end()) |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 616 | return iter->second; |
| 617 | |
| 618 | FPDF_PAGE page = FPDF_LoadPage(doc, index); |
| 619 | if (!page) |
| 620 | return nullptr; |
| 621 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 622 | FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 623 | FORM_OnAfterLoadPage(page, form_handle); |
| 624 | FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN); |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 625 | loaded_pages[index] = page; |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 626 | return page; |
| 627 | } |
| 628 | |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 629 | std::wstring ConvertToWString(const unsigned short* buf, |
| 630 | unsigned long buf_size) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 631 | std::wstring result; |
| 632 | result.reserve(buf_size); |
| 633 | std::copy(buf, buf + buf_size, std::back_inserter(result)); |
| 634 | return result; |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 637 | void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent) { |
| 638 | static const size_t kBufSize = 1024; |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 639 | unsigned short buf[kBufSize]; |
| 640 | unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize); |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 641 | printf("%*s%ls", indent * 2, "", ConvertToWString(buf, len).c_str()); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 642 | |
| 643 | memset(buf, 0, sizeof(buf)); |
| 644 | len = FPDF_StructElement_GetAltText(child, buf, kBufSize); |
| 645 | if (len > 0) |
| 646 | printf(" (%ls)", ConvertToWString(buf, len).c_str()); |
| 647 | printf("\n"); |
| 648 | |
| 649 | for (int i = 0; i < FPDF_StructElement_CountChildren(child); ++i) { |
| 650 | FPDF_STRUCTELEMENT sub_child = FPDF_StructElement_GetChildAtIndex(child, i); |
| 651 | // If the child is not an Element then this will return null. This can |
| 652 | // happen if the element is things like an object reference or a stream. |
| 653 | if (!sub_child) |
| 654 | continue; |
| 655 | |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 656 | DumpChildStructure(sub_child, indent + 1); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | void DumpPageStructure(FPDF_PAGE page, const int page_idx) { |
| 661 | std::unique_ptr<void, FPDFStructTreeDeleter> tree( |
| 662 | FPDF_StructTree_GetForPage(page)); |
| 663 | if (!tree) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 664 | fprintf(stderr, "Failed to load struct tree for page %d\n", page_idx); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | printf("Structure Tree for Page %d\n", page_idx); |
| 669 | for (int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) { |
| 670 | FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i); |
| 671 | if (!child) { |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 672 | fprintf(stderr, "Failed to load child %d for page %d\n", i, page_idx); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 673 | continue; |
| 674 | } |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 675 | DumpChildStructure(child, 0); |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 676 | } |
| 677 | printf("\n\n"); |
| 678 | } |
| 679 | |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 680 | bool RenderPage(const std::string& name, |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 681 | FPDF_DOCUMENT doc, |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 682 | FPDF_FORMHANDLE form, |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 683 | FPDF_FORMFILLINFO_PDFiumTest& form_fill_info, |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 684 | const int page_index, |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 685 | const Options& options, |
| 686 | const std::string& events) { |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 687 | std::unique_ptr<void, FPDFPageDeleter> page( |
| 688 | GetPageForIndex(&form_fill_info, doc, page_index)); |
| 689 | if (!page.get()) |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 690 | return false; |
Dan Sinclair | 3c67fbd | 2017-04-05 16:07:50 -0400 | [diff] [blame^] | 691 | if (options.send_events) |
| 692 | SendPageEvents(form, page.get(), events); |
| 693 | if (options.output_format == OUTPUT_STRUCTURE) { |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 694 | DumpPageStructure(page.get(), page_index); |
| 695 | return true; |
| 696 | } |
| 697 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 698 | std::unique_ptr<void, FPDFTextPageDeleter> text_page( |
| 699 | FPDFText_LoadPage(page.get())); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 700 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 701 | double scale = 1.0; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 702 | if (!options.scale_factor_as_string.empty()) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 703 | std::stringstream(options.scale_factor_as_string) >> scale; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 704 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 705 | int width = static_cast<int>(FPDF_GetPageWidth(page.get()) * scale); |
| 706 | int height = static_cast<int>(FPDF_GetPageHeight(page.get()) * scale); |
| 707 | int alpha = FPDFPage_HasTransparency(page.get()) ? 1 : 0; |
| 708 | std::unique_ptr<void, FPDFBitmapDeleter> bitmap( |
| 709 | FPDFBitmap_Create(width, height, alpha)); |
| 710 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 711 | if (bitmap) { |
| 712 | FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 713 | FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color); |
| 714 | FPDF_RenderPageBitmap(bitmap.get(), page.get(), 0, 0, width, height, 0, |
| 715 | FPDF_ANNOT); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 716 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 717 | FPDF_FFLDraw(form, bitmap.get(), page.get(), 0, 0, width, height, 0, |
| 718 | FPDF_ANNOT); |
| 719 | int stride = FPDFBitmap_GetStride(bitmap.get()); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 720 | const char* buffer = |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 721 | reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get())); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 722 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 723 | std::string&& image_file_name = ""; |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 724 | switch (options.output_format) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 725 | #ifdef _WIN32 |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 726 | case OUTPUT_BMP: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 727 | image_file_name = |
| 728 | WriteBmp(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 729 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 730 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 731 | case OUTPUT_EMF: |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 732 | WriteEmf(page.get(), name.c_str(), page_index); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 733 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 734 | #endif |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 735 | case OUTPUT_TEXT: |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 736 | WriteText(page.get(), name.c_str(), page_index); |
dsinclair | b63068f | 2016-06-16 07:58:09 -0700 | [diff] [blame] | 737 | break; |
| 738 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 739 | case OUTPUT_PNG: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 740 | image_file_name = |
| 741 | WritePng(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 742 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 743 | |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 744 | case OUTPUT_PPM: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 745 | image_file_name = |
| 746 | WritePpm(name.c_str(), page_index, buffer, stride, width, height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 747 | break; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 748 | |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 749 | #ifdef PDF_ENABLE_SKIA |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 750 | case OUTPUT_SKP: { |
| 751 | std::unique_ptr<SkPictureRecorder> recorder( |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 752 | reinterpret_cast<SkPictureRecorder*>( |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 753 | FPDF_RenderPageSkp(page.get(), width, height))); |
| 754 | FPDF_FFLRecord(form, recorder.get(), page.get(), 0, 0, width, height, 0, |
| 755 | 0); |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 756 | image_file_name = WriteSkp(name.c_str(), page_index, recorder.get()); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 757 | } break; |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 758 | #endif |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 759 | default: |
| 760 | break; |
| 761 | } |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 762 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 763 | // Write the filename and the MD5 of the buffer to stdout if we wrote a |
| 764 | // file. |
| 765 | if (options.md5 && image_file_name != "") |
| 766 | OutputMD5Hash(image_file_name.c_str(), buffer, stride * height); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 767 | } else { |
| 768 | fprintf(stderr, "Page was too large to be rendered.\n"); |
| 769 | } |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 770 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 771 | form_fill_info.loaded_pages.erase(page_index); |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 772 | FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_CLOSE); |
| 773 | FORM_OnBeforeClosePage(page.get(), form); |
thestig | e97ea03 | 2016-05-19 10:59:15 -0700 | [diff] [blame] | 774 | return !!bitmap; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 775 | } |
| 776 | |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 777 | void RenderPdf(const std::string& name, |
| 778 | const char* pBuf, |
| 779 | size_t len, |
| 780 | const Options& options, |
| 781 | const std::string& events) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 782 | IPDF_JSPLATFORM platform_callbacks; |
| 783 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 784 | platform_callbacks.version = 3; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 785 | platform_callbacks.app_alert = ExampleAppAlert; |
Tom Sepez | 58fb36a | 2016-02-01 10:32:14 -0800 | [diff] [blame] | 786 | platform_callbacks.app_response = ExampleAppResponse; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 787 | platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
Tom Sepez | e5fbd7a | 2016-01-29 17:05:08 -0800 | [diff] [blame] | 788 | platform_callbacks.Doc_mail = ExampleDocMail; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 789 | |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 790 | FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {}; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 791 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | ed63138 | 2014-11-18 14:10:25 -0800 | [diff] [blame] | 792 | form_callbacks.version = 2; |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 793 | #else // PDF_ENABLE_XFA |
| 794 | form_callbacks.version = 1; |
| 795 | #endif // PDF_ENABLE_XFA |
tonikitoo | 3e98158 | 2016-08-26 08:37:10 -0700 | [diff] [blame] | 796 | form_callbacks.FFI_GetPage = GetPageForIndex; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 797 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 798 | |
| 799 | TestLoader loader(pBuf, len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 800 | FPDF_FILEACCESS file_access; |
| 801 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 802 | file_access.m_FileLen = static_cast<unsigned long>(len); |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 803 | file_access.m_GetBlock = TestLoader::GetBlock; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 804 | file_access.m_Param = &loader; |
| 805 | |
| 806 | FX_FILEAVAIL file_avail; |
| 807 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 808 | file_avail.version = 1; |
| 809 | file_avail.IsDataAvail = Is_Data_Avail; |
| 810 | |
| 811 | FX_DOWNLOADHINTS hints; |
| 812 | memset(&hints, '\0', sizeof(hints)); |
| 813 | hints.version = 1; |
| 814 | hints.AddSegment = Add_Segment; |
| 815 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 816 | int nRet = PDF_DATA_NOTAVAIL; |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 817 | bool bIsLinearized = false; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 818 | std::unique_ptr<void, FPDFDocumentDeleter> doc; |
| 819 | std::unique_ptr<void, FPDFAvailDeleter> pdf_avail( |
| 820 | FPDFAvail_Create(&file_avail, &file_access)); |
Tom Sepez | c98895c | 2015-11-24 15:30:36 -0800 | [diff] [blame] | 821 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 822 | if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) { |
| 823 | doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 824 | if (doc) { |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 825 | while (nRet == PDF_DATA_NOTAVAIL) |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 826 | nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 827 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 828 | if (nRet == PDF_DATA_ERROR) { |
| 829 | fprintf(stderr, "Unknown error in checking if doc was available.\n"); |
| 830 | return; |
| 831 | } |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 832 | nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 833 | if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) { |
| 834 | fprintf(stderr, |
| 835 | "Error %d was returned in checking if form was available.\n", |
| 836 | nRet); |
| 837 | return; |
| 838 | } |
Jun Fang | b553bcb | 2015-11-10 18:49:04 +0800 | [diff] [blame] | 839 | bIsLinearized = true; |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 840 | } |
| 841 | } else { |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 842 | doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr)); |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 843 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 844 | |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 845 | if (!doc) { |
Dan Sinclair | eb815bf | 2015-10-27 13:08:41 -0400 | [diff] [blame] | 846 | unsigned long err = FPDF_GetLastError(); |
| 847 | fprintf(stderr, "Load pdf docs unsuccessful: "); |
| 848 | switch (err) { |
| 849 | case FPDF_ERR_SUCCESS: |
| 850 | fprintf(stderr, "Success"); |
| 851 | break; |
| 852 | case FPDF_ERR_UNKNOWN: |
| 853 | fprintf(stderr, "Unknown error"); |
| 854 | break; |
| 855 | case FPDF_ERR_FILE: |
| 856 | fprintf(stderr, "File not found or could not be opened"); |
| 857 | break; |
| 858 | case FPDF_ERR_FORMAT: |
| 859 | fprintf(stderr, "File not in PDF format or corrupted"); |
| 860 | break; |
| 861 | case FPDF_ERR_PASSWORD: |
| 862 | fprintf(stderr, "Password required or incorrect password"); |
| 863 | break; |
| 864 | case FPDF_ERR_SECURITY: |
| 865 | fprintf(stderr, "Unsupported security scheme"); |
| 866 | break; |
| 867 | case FPDF_ERR_PAGE: |
| 868 | fprintf(stderr, "Page not found or content error"); |
| 869 | break; |
| 870 | default: |
| 871 | fprintf(stderr, "Unknown error %ld", err); |
| 872 | } |
| 873 | fprintf(stderr, ".\n"); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 877 | (void)FPDF_GetDocPermissions(doc.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 878 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 879 | std::unique_ptr<void, FPDFFormHandleDeleter> form( |
| 880 | FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks)); |
| 881 | form_callbacks.form_handle = form.get(); |
tonikitoo | 81d92f8 | 2016-09-21 12:44:56 -0700 | [diff] [blame] | 882 | |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 883 | #ifdef PDF_ENABLE_XFA |
thestig | b97e07e | 2016-09-28 22:16:40 -0700 | [diff] [blame] | 884 | int doc_type = DOCTYPE_PDF; |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 885 | if (FPDF_HasXFAField(doc.get(), &doc_type) && doc_type != DOCTYPE_PDF && |
| 886 | !FPDF_LoadXFA(doc.get())) { |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 887 | fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); |
Tom Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 888 | } |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 889 | #endif // PDF_ENABLE_XFA |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 890 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 891 | FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD); |
| 892 | FPDF_SetFormFieldHighlightAlpha(form.get(), 100); |
| 893 | FORM_DoDocumentJSAction(form.get()); |
| 894 | FORM_DoDocumentOpenAction(form.get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 895 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 896 | int page_count = FPDF_GetPageCount(doc.get()); |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 897 | int rendered_pages = 0; |
| 898 | int bad_pages = 0; |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 899 | int first_page = options.pages ? options.first_page : 0; |
| 900 | int last_page = options.pages ? options.last_page + 1 : page_count; |
| 901 | for (int i = first_page; i < last_page; ++i) { |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 902 | if (bIsLinearized) { |
| 903 | nRet = PDF_DATA_NOTAVAIL; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 904 | while (nRet == PDF_DATA_NOTAVAIL) |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 905 | nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints); |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 906 | |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 907 | if (nRet == PDF_DATA_ERROR) { |
| 908 | fprintf(stderr, "Unknown error in checking if page %d is available.\n", |
| 909 | i); |
| 910 | return; |
| 911 | } |
| 912 | } |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 913 | if (RenderPage(name, doc.get(), form.get(), form_callbacks, i, options, |
| 914 | events)) |
Jun Fang | df7f366 | 2015-11-10 18:29:18 +0800 | [diff] [blame] | 915 | ++rendered_pages; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 916 | else |
Lei Zhang | 5377ebf | 2015-09-23 14:52:53 -0700 | [diff] [blame] | 917 | ++bad_pages; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Tom Sepez | 1d17a04 | 2017-03-16 13:22:47 -0700 | [diff] [blame] | 920 | FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC); |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 921 | fprintf(stderr, "Rendered %d pages.\n", rendered_pages); |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 922 | if (bad_pages) |
| 923 | fprintf(stderr, "Skipped %d bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 926 | static void ShowConfig() { |
| 927 | std::string config; |
| 928 | std::string maybe_comma; |
| 929 | #if PDF_ENABLE_V8 |
| 930 | config.append(maybe_comma); |
| 931 | config.append("V8"); |
| 932 | maybe_comma = ","; |
| 933 | #endif // PDF_ENABLE_V8 |
| 934 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 935 | config.append(maybe_comma); |
| 936 | config.append("V8_EXTERNAL"); |
| 937 | maybe_comma = ","; |
| 938 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 939 | #ifdef PDF_ENABLE_XFA |
| 940 | config.append(maybe_comma); |
| 941 | config.append("XFA"); |
| 942 | maybe_comma = ","; |
| 943 | #endif // PDF_ENABLE_XFA |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 944 | #ifdef PDF_ENABLE_ASAN |
| 945 | config.append(maybe_comma); |
| 946 | config.append("ASAN"); |
| 947 | maybe_comma = ","; |
| 948 | #endif // PDF_ENABLE_ASAN |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 949 | printf("%s\n", config.c_str()); |
| 950 | } |
| 951 | |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 952 | static const char kUsageString[] = |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 953 | "Usage: pdfium_test [OPTION] [FILE]...\n" |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 954 | " --show-config - print build options and exit\n" |
Dan Sinclair | ddcb6e7 | 2017-04-05 10:30:33 -0400 | [diff] [blame] | 955 | " --show-structure - print the structure elements from the document\n" |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 956 | " --send-events - send input described by .evt file\n" |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 957 | " --bin-dir=<path> - override path to v8 external data\n" |
| 958 | " --font-dir=<path> - override path to external fonts\n" |
| 959 | " --scale=<number> - scale output size by number (e.g. 0.5)\n" |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 960 | " --pages=<number>(-<number>) - only render the given 0-based page(s)\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 961 | #ifdef _WIN32 |
| 962 | " --bmp - write page images <pdf-name>.<page-number>.bmp\n" |
| 963 | " --emf - write page meta files <pdf-name>.<page-number>.emf\n" |
Tom Sepez | c46d000 | 2015-11-30 15:46:36 -0800 | [diff] [blame] | 964 | #endif // _WIN32 |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 965 | " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n" |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 966 | " --png - write page images <pdf-name>.<page-number>.png\n" |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 967 | " --ppm - write page images <pdf-name>.<page-number>.ppm\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 968 | #ifdef PDF_ENABLE_SKIA |
| 969 | " --skp - write page images <pdf-name>.<page-number>.skp\n" |
| 970 | #endif |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 971 | " --md5 - write output image paths and their md5 hashes to stdout.\n" |
Cary Clark | 399be5b | 2016-03-14 16:51:29 -0400 | [diff] [blame] | 972 | ""; |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 973 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 974 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 975 | std::vector<std::string> args(argv, argv + argc); |
| 976 | Options options; |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 977 | std::vector<std::string> files; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 978 | if (!ParseCommandLine(args, &options, &files)) { |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 979 | fprintf(stderr, "%s", kUsageString); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 980 | return 1; |
| 981 | } |
| 982 | |
Tom Sepez | 2991d8d | 2016-01-15 16:02:48 -0800 | [diff] [blame] | 983 | if (options.show_config) { |
| 984 | ShowConfig(); |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | if (files.empty()) { |
| 989 | fprintf(stderr, "No input files.\n"); |
| 990 | return 1; |
| 991 | } |
| 992 | |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 993 | #ifdef PDF_ENABLE_V8 |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 994 | v8::Platform* platform; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 995 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 996 | v8::StartupData natives; |
| 997 | v8::StartupData snapshot; |
Tom Sepez | d831dc7 | 2015-10-19 16:04:22 -0700 | [diff] [blame] | 998 | InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives, |
| 999 | &snapshot, &platform); |
| 1000 | #else // V8_USE_EXTERNAL_STARTUP_DATA |
jochen | 9e077d2 | 2016-06-09 02:51:13 -0700 | [diff] [blame] | 1001 | InitializeV8ForPDFium(options.exe_path, &platform); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1002 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1003 | #endif // PDF_ENABLE_V8 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1004 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1005 | FPDF_LIBRARY_CONFIG config; |
| 1006 | config.version = 2; |
| 1007 | config.m_pUserFontPaths = nullptr; |
| 1008 | config.m_pIsolate = nullptr; |
| 1009 | config.m_v8EmbedderSlot = 0; |
| 1010 | |
| 1011 | const char* path_array[2]; |
| 1012 | if (!options.font_directory.empty()) { |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1013 | path_array[0] = options.font_directory.c_str(); |
| 1014 | path_array[1] = nullptr; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1015 | config.m_pUserFontPaths = path_array; |
Lei Zhang | 6f62d53 | 2015-09-23 15:31:44 -0700 | [diff] [blame] | 1016 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 1017 | FPDF_InitLibraryWithConfig(&config); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1018 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1019 | UNSUPPORT_INFO unsupported_info; |
| 1020 | memset(&unsupported_info, '\0', sizeof(unsupported_info)); |
| 1021 | unsupported_info.version = 1; |
| 1022 | unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1023 | |
npm | fa20cd5 | 2016-11-14 13:33:40 -0800 | [diff] [blame] | 1024 | FSDK_SetUnSpObjProcessHandler(&unsupported_info); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1025 | |
thestig | 514e8c9 | 2016-07-15 17:57:54 -0700 | [diff] [blame] | 1026 | for (const std::string& filename : files) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 1027 | size_t file_length = 0; |
Tom Sepez | 0aa3531 | 2016-01-06 10:16:32 -0800 | [diff] [blame] | 1028 | std::unique_ptr<char, pdfium::FreeDeleter> file_contents = |
| 1029 | GetFileContents(filename.c_str(), &file_length); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1030 | if (!file_contents) |
| 1031 | continue; |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 1032 | fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str()); |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1033 | std::string events; |
| 1034 | if (options.send_events) { |
| 1035 | std::string event_filename = filename; |
| 1036 | size_t event_length = 0; |
| 1037 | size_t extension_pos = event_filename.find(".pdf"); |
| 1038 | if (extension_pos != std::string::npos) { |
| 1039 | event_filename.replace(extension_pos, 4, ".evt"); |
thestig | f2b940c | 2016-10-13 06:48:47 -0700 | [diff] [blame] | 1040 | if (access(event_filename.c_str(), R_OK) == 0) { |
| 1041 | fprintf(stderr, "Using event file %s.\n", event_filename.c_str()); |
| 1042 | std::unique_ptr<char, pdfium::FreeDeleter> event_contents = |
| 1043 | GetFileContents(event_filename.c_str(), &event_length); |
| 1044 | if (event_contents) { |
| 1045 | fprintf(stderr, "Sending events from: %s\n", |
| 1046 | event_filename.c_str()); |
| 1047 | events = std::string(event_contents.get(), event_length); |
| 1048 | } |
tsepez | f09bdfa | 2016-04-18 16:08:26 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | RenderPdf(filename, file_contents.get(), file_length, options, events); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | FPDF_DestroyLibrary(); |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1056 | #ifdef PDF_ENABLE_V8 |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 1057 | v8::V8::ShutdownPlatform(); |
| 1058 | delete platform; |
thestig | c08cd7a | 2016-06-27 09:47:59 -0700 | [diff] [blame] | 1059 | |
| 1060 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 1061 | free(const_cast<char*>(natives.data)); |
| 1062 | free(const_cast<char*>(snapshot.data)); |
| 1063 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 1064 | #endif // PDF_ENABLE_V8 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1065 | |
| 1066 | return 0; |
| 1067 | } |