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