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