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