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