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> |
Tom Sepez | fb94728 | 2014-12-08 09:55:11 -0800 | [diff] [blame] | 9 | #include <wchar.h> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 10 | |
| 11 | #include <list> |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 12 | #include <sstream> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <utility> |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 15 | #include <vector> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
John Abd-El-Malek | 197fd8d | 2014-05-23 19:27:48 -0700 | [diff] [blame] | 17 | #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 18 | #include "../fpdfsdk/include/fpdf_ext.h" |
| 19 | #include "../fpdfsdk/include/fpdfformfill.h" |
| 20 | #include "../fpdfsdk/include/fpdftext.h" |
| 21 | #include "../fpdfsdk/include/fpdfview.h" |
Bruce Dawson | ddc2062 | 2014-11-18 13:42:28 -0800 | [diff] [blame] | 22 | #include "../core/include/fxcrt/fx_system.h" |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 23 | #include "image_diff_png.h" |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 24 | #include "v8/include/v8.h" |
| 25 | #include "v8/include/libplatform/libplatform.h" |
| 26 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | |
| 28 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 29 | #define snprintf _snprintf |
| 30 | #define PATH_SEPARATOR '\\' |
| 31 | #else |
| 32 | #define PATH_SEPARATOR '/' |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 35 | enum OutputFormat { |
| 36 | OUTPUT_NONE, |
| 37 | OUTPUT_PPM, |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 38 | OUTPUT_PNG, |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 39 | #ifdef _WIN32 |
| 40 | OUTPUT_BMP, |
| 41 | OUTPUT_EMF, |
| 42 | #endif |
| 43 | }; |
| 44 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 45 | struct Options { |
| 46 | Options() : output_format(OUTPUT_NONE) { } |
| 47 | |
| 48 | OutputFormat output_format; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 49 | std::string scale_factor_as_string; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 50 | std::string exe_path; |
| 51 | std::string bin_directory; |
| 52 | }; |
| 53 | |
| 54 | // Reads the entire contents of a file into a newly malloc'd buffer. |
| 55 | static char* GetFileContents(const char* filename, size_t* retlen) { |
| 56 | FILE* file = fopen(filename, "rb"); |
| 57 | if (!file) { |
| 58 | fprintf(stderr, "Failed to open: %s\n", filename); |
| 59 | return NULL; |
| 60 | } |
| 61 | (void) fseek(file, 0, SEEK_END); |
| 62 | size_t file_length = ftell(file); |
| 63 | if (!file_length) { |
| 64 | return NULL; |
| 65 | } |
| 66 | (void) fseek(file, 0, SEEK_SET); |
| 67 | char* buffer = (char*) malloc(file_length); |
| 68 | if (!buffer) { |
| 69 | return NULL; |
| 70 | } |
| 71 | size_t bytes_read = fread(buffer, 1, file_length, file); |
| 72 | (void) fclose(file); |
| 73 | if (bytes_read != file_length) { |
| 74 | fprintf(stderr, "Failed to read: %s\n", filename); |
| 75 | free(buffer); |
| 76 | return NULL; |
| 77 | } |
| 78 | *retlen = bytes_read; |
| 79 | return buffer; |
| 80 | } |
| 81 | |
| 82 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 83 | // Returns the full path for an external V8 data file based on either |
| 84 | // the currect exectuable path or an explicit override. |
| 85 | static std::string GetFullPathForSnapshotFile(const Options& options, |
| 86 | const std::string& filename) { |
| 87 | std::string result; |
| 88 | if (!options.bin_directory.empty()) { |
| 89 | result = options.bin_directory; |
| 90 | if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { |
| 91 | result += PATH_SEPARATOR; |
| 92 | } |
| 93 | } else if (!options.exe_path.empty()) { |
| 94 | size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR); |
| 95 | if (last_separator != std::string::npos) { |
| 96 | result = options.exe_path.substr(0, last_separator + 1); |
| 97 | } |
| 98 | } |
| 99 | result += filename; |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | // Reads an extenal V8 data file from the |options|-indicated location, |
| 104 | // returing true on success and false on error. |
| 105 | static bool GetExternalData(const Options& options, |
| 106 | const std::string& bin_filename, |
| 107 | v8::StartupData* result_data) { |
| 108 | std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); |
| 109 | size_t data_length = 0; |
| 110 | char* data_buffer = GetFileContents(full_path.c_str(), &data_length); |
| 111 | if (!data_buffer) { |
| 112 | return false; |
| 113 | } |
| 114 | result_data->data = const_cast<const char*>(data_buffer); |
| 115 | result_data->raw_size = data_length; |
| 116 | return true; |
| 117 | } |
| 118 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 119 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 120 | static bool CheckDimensions(int stride, int width, int height) { |
| 121 | if (stride < 0 || width < 0 || height < 0) |
| 122 | return false; |
| 123 | if (height > 0 && width > INT_MAX / height) |
| 124 | return false; |
| 125 | return true; |
| 126 | } |
| 127 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 128 | static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| 129 | int stride, int width, int height) { |
| 130 | const char* buffer = reinterpret_cast<const char*>(buffer_void); |
| 131 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 132 | if (!CheckDimensions(stride, width, height)) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | return; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 134 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 135 | int out_len = width * height; |
| 136 | if (out_len > INT_MAX / 3) |
| 137 | return; |
| 138 | out_len *= 3; |
| 139 | |
| 140 | char filename[256]; |
| 141 | snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num); |
John Abd-El-Malek | a548d30 | 2014-06-26 10:18:11 -0700 | [diff] [blame] | 142 | FILE* fp = fopen(filename, "wb"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 143 | if (!fp) |
| 144 | return; |
| 145 | fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height); |
| 146 | // Source data is B, G, R, unused. |
| 147 | // Dest data is R, G, B. |
| 148 | char* result = new char[out_len]; |
| 149 | if (result) { |
| 150 | for (int h = 0; h < height; ++h) { |
| 151 | const char* src_line = buffer + (stride * h); |
| 152 | char* dest_line = result + (width * h * 3); |
| 153 | for (int w = 0; w < width; ++w) { |
| 154 | // R |
| 155 | dest_line[w * 3] = src_line[(w * 4) + 2]; |
| 156 | // G |
| 157 | dest_line[(w * 3) + 1] = src_line[(w * 4) + 1]; |
| 158 | // B |
| 159 | dest_line[(w * 3) + 2] = src_line[w * 4]; |
| 160 | } |
| 161 | } |
| 162 | fwrite(result, out_len, 1, fp); |
| 163 | delete [] result; |
| 164 | } |
| 165 | fclose(fp); |
| 166 | } |
| 167 | |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 168 | static void WritePng(const char* pdf_name, int num, const void* buffer_void, |
| 169 | int stride, int width, int height) { |
| 170 | if (!CheckDimensions(stride, width, height)) |
| 171 | return; |
| 172 | |
| 173 | std::vector<unsigned char> png_encoding; |
| 174 | const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void); |
| 175 | if (!image_diff_png::EncodeBGRAPNG( |
| 176 | buffer, width, height, stride, false, &png_encoding)) { |
| 177 | fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | char filename[256]; |
| 182 | int chars_formatted = snprintf( |
| 183 | filename, sizeof(filename), "%s.%d.png", pdf_name, num); |
| 184 | if (chars_formatted < 0 || |
| 185 | static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 186 | fprintf(stderr, "Filname %s is too long\n", filename); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | FILE* fp = fopen(filename, "wb"); |
| 191 | if (!fp) { |
| 192 | fprintf(stderr, "Failed to open %s for output\n", filename); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | size_t bytes_written = fwrite( |
| 197 | &png_encoding.front(), 1, png_encoding.size(), fp); |
| 198 | if (bytes_written != png_encoding.size()) |
| 199 | fprintf(stderr, "Failed to write to %s\n", filename); |
| 200 | |
| 201 | (void) fclose(fp); |
| 202 | } |
| 203 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 204 | #ifdef _WIN32 |
| 205 | static void WriteBmp(const char* pdf_name, int num, const void* buffer, |
| 206 | int stride, int width, int height) { |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 207 | if (!CheckDimensions(stride, width, height)) |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 208 | return; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 209 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 210 | int out_len = stride * height; |
| 211 | if (out_len > INT_MAX / 3) |
| 212 | return; |
| 213 | |
| 214 | char filename[256]; |
| 215 | snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num); |
| 216 | FILE* fp = fopen(filename, "wb"); |
| 217 | if (!fp) |
| 218 | return; |
| 219 | |
| 220 | BITMAPINFO bmi = {0}; |
| 221 | bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD); |
| 222 | bmi.bmiHeader.biWidth = width; |
| 223 | bmi.bmiHeader.biHeight = -height; // top-down image |
| 224 | bmi.bmiHeader.biPlanes = 1; |
| 225 | bmi.bmiHeader.biBitCount = 32; |
| 226 | bmi.bmiHeader.biCompression = BI_RGB; |
| 227 | bmi.bmiHeader.biSizeImage = 0; |
| 228 | |
| 229 | BITMAPFILEHEADER file_header = {0}; |
| 230 | file_header.bfType = 0x4d42; |
| 231 | file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len; |
| 232 | file_header.bfOffBits = file_header.bfSize - out_len; |
| 233 | |
| 234 | fwrite(&file_header, sizeof(file_header), 1, fp); |
| 235 | fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp); |
| 236 | fwrite(buffer, out_len, 1, fp); |
| 237 | fclose(fp); |
| 238 | } |
| 239 | |
| 240 | void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { |
| 241 | int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 242 | int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 243 | |
| 244 | char filename[256]; |
| 245 | snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num); |
| 246 | |
| 247 | HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL); |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 248 | |
| 249 | HRGN rgn = CreateRectRgn(0, 0, width, height); |
| 250 | SelectClipRgn(dc, rgn); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 251 | DeleteObject(rgn); |
| 252 | |
| 253 | SelectObject(dc, GetStockObject(NULL_PEN)); |
| 254 | SelectObject(dc, GetStockObject(WHITE_BRUSH)); |
| 255 | // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 256 | Rectangle(dc, 0, 0, width + 1, height + 1); |
| 257 | |
| 258 | FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 259 | FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 260 | |
| 261 | DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 262 | } |
| 263 | #endif |
| 264 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 265 | int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, |
| 266 | int, int) { |
Tom Sepez | fb94728 | 2014-12-08 09:55:11 -0800 | [diff] [blame] | 267 | // Deal with differences between UTF16LE and wchar_t on this platform. |
| 268 | size_t characters = 0; |
| 269 | while (msg[characters]) { |
| 270 | ++characters; |
| 271 | } |
| 272 | wchar_t* platform_string = |
| 273 | (wchar_t*)malloc((characters + 1) * sizeof(wchar_t)); |
| 274 | for (size_t i = 0; i < characters + 1; ++i) { |
| 275 | unsigned char* ptr = (unsigned char*)&msg[i]; |
| 276 | platform_string[i] = ptr[0] + 256 * ptr[1]; |
| 277 | } |
| 278 | printf("Alert: %ls\n", platform_string); |
| 279 | free(platform_string); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 283 | void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) { |
| 284 | printf("Goto Page: %d\n", pageNumber); |
| 285 | } |
| 286 | |
| 287 | void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 288 | std::string feature = "Unknown"; |
| 289 | switch (type) { |
| 290 | case FPDF_UNSP_DOC_XFAFORM: |
| 291 | feature = "XFA"; |
| 292 | break; |
| 293 | case FPDF_UNSP_DOC_PORTABLECOLLECTION: |
| 294 | feature = "Portfolios_Packages"; |
| 295 | break; |
| 296 | case FPDF_UNSP_DOC_ATTACHMENT: |
| 297 | case FPDF_UNSP_ANNOT_ATTACHMENT: |
| 298 | feature = "Attachment"; |
| 299 | break; |
| 300 | case FPDF_UNSP_DOC_SECURITY: |
| 301 | feature = "Rights_Management"; |
| 302 | break; |
| 303 | case FPDF_UNSP_DOC_SHAREDREVIEW: |
| 304 | feature = "Shared_Review"; |
| 305 | break; |
| 306 | case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: |
| 307 | case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: |
| 308 | case FPDF_UNSP_DOC_SHAREDFORM_EMAIL: |
| 309 | feature = "Shared_Form"; |
| 310 | break; |
| 311 | case FPDF_UNSP_ANNOT_3DANNOT: |
| 312 | feature = "3D"; |
| 313 | break; |
| 314 | case FPDF_UNSP_ANNOT_MOVIE: |
| 315 | feature = "Movie"; |
| 316 | break; |
| 317 | case FPDF_UNSP_ANNOT_SOUND: |
| 318 | feature = "Sound"; |
| 319 | break; |
| 320 | case FPDF_UNSP_ANNOT_SCREEN_MEDIA: |
| 321 | case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 322 | feature = "Screen"; |
| 323 | break; |
| 324 | case FPDF_UNSP_ANNOT_SIG: |
| 325 | feature = "Digital_Signature"; |
| 326 | break; |
| 327 | } |
| 328 | printf("Unsupported feature: %s.\n", feature.c_str()); |
| 329 | } |
| 330 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 331 | bool ParseCommandLine(const std::vector<std::string>& args, |
| 332 | Options* options, std::list<std::string>* files) { |
| 333 | if (args.empty()) { |
| 334 | return false; |
| 335 | } |
| 336 | options->exe_path = args[0]; |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 337 | size_t cur_idx = 1; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 338 | for (; cur_idx < args.size(); ++cur_idx) { |
| 339 | const std::string& cur_arg = args[cur_idx]; |
| 340 | if (cur_arg == "--ppm") { |
| 341 | if (options->output_format != OUTPUT_NONE) { |
| 342 | fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 343 | return false; |
| 344 | } |
| 345 | options->output_format = OUTPUT_PPM; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 346 | } else if (cur_arg == "--png") { |
| 347 | if (options->output_format != OUTPUT_NONE) { |
| 348 | fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 349 | return false; |
| 350 | } |
| 351 | options->output_format = OUTPUT_PNG; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 352 | } |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 353 | #ifdef _WIN32 |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 354 | else if (cur_arg == "--emf") { |
| 355 | if (options->output_format != OUTPUT_NONE) { |
| 356 | fprintf(stderr, "Duplicate or conflicting --emf argument\n"); |
| 357 | return false; |
| 358 | } |
| 359 | options->output_format = OUTPUT_EMF; |
| 360 | } |
| 361 | else if (cur_arg == "--bmp") { |
| 362 | if (options->output_format != OUTPUT_NONE) { |
| 363 | fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 364 | return false; |
| 365 | } |
| 366 | options->output_format = OUTPUT_BMP; |
| 367 | } |
| 368 | #endif // _WIN32 |
| 369 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 370 | else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
| 371 | if (!options->bin_directory.empty()) { |
| 372 | fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 373 | return false; |
| 374 | } |
| 375 | options->bin_directory = cur_arg.substr(10); |
| 376 | } |
| 377 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 378 | else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { |
| 379 | if (!options->scale_factor_as_string.empty()) { |
| 380 | fprintf(stderr, "Duplicate --scale argument\n"); |
| 381 | return false; |
| 382 | } |
| 383 | options->scale_factor_as_string = cur_arg.substr(8); |
| 384 | } |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 385 | else |
| 386 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 387 | } |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 388 | if (cur_idx >= args.size()) { |
| 389 | fprintf(stderr, "No input files.\n"); |
Vitaly Buka | 8f2c3dc | 2014-08-20 10:32:36 -0700 | [diff] [blame] | 390 | return false; |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 391 | } |
Bo Xu | d44e392 | 2014-12-19 02:27:25 -0800 | [diff] [blame] | 392 | for (size_t i = cur_idx; i < args.size(); i++) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 393 | files->push_back(args[i]); |
| 394 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
| 398 | class TestLoader { |
| 399 | public: |
| 400 | TestLoader(const char* pBuf, size_t len); |
| 401 | |
| 402 | const char* m_pBuf; |
| 403 | size_t m_Len; |
| 404 | }; |
| 405 | |
| 406 | TestLoader::TestLoader(const char* pBuf, size_t len) |
| 407 | : m_pBuf(pBuf), m_Len(len) { |
| 408 | } |
| 409 | |
| 410 | int Get_Block(void* param, unsigned long pos, unsigned char* pBuf, |
| 411 | unsigned long size) { |
| 412 | TestLoader* pLoader = (TestLoader*) param; |
| 413 | if (pos + size < pos || pos + size > pLoader->m_Len) return 0; |
| 414 | memcpy(pBuf, pLoader->m_pBuf + pos, size); |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 423 | } |
| 424 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 425 | void RenderPdf(const std::string& name, const char* pBuf, size_t len, |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 426 | const Options& options) { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 427 | fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 428 | |
| 429 | IPDF_JSPLATFORM platform_callbacks; |
| 430 | memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 431 | platform_callbacks.version = 1; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 432 | platform_callbacks.app_alert = ExampleAppAlert; |
| 433 | platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | |
| 435 | FPDF_FORMFILLINFO form_callbacks; |
| 436 | memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
Tom Sepez | ed63138 | 2014-11-18 14:10:25 -0800 | [diff] [blame] | 437 | form_callbacks.version = 2; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 438 | form_callbacks.m_pJsPlatform = &platform_callbacks; |
| 439 | |
| 440 | TestLoader loader(pBuf, len); |
| 441 | |
| 442 | FPDF_FILEACCESS file_access; |
| 443 | memset(&file_access, '\0', sizeof(file_access)); |
John Abd-El-Malek | 7dc5172 | 2014-05-26 12:54:31 -0700 | [diff] [blame] | 444 | file_access.m_FileLen = static_cast<unsigned long>(len); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 445 | file_access.m_GetBlock = Get_Block; |
| 446 | file_access.m_Param = &loader; |
| 447 | |
| 448 | FX_FILEAVAIL file_avail; |
| 449 | memset(&file_avail, '\0', sizeof(file_avail)); |
| 450 | file_avail.version = 1; |
| 451 | file_avail.IsDataAvail = Is_Data_Avail; |
| 452 | |
| 453 | FX_DOWNLOADHINTS hints; |
| 454 | memset(&hints, '\0', sizeof(hints)); |
| 455 | hints.version = 1; |
| 456 | hints.AddSegment = Add_Segment; |
| 457 | |
| 458 | FPDF_DOCUMENT doc; |
| 459 | FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access); |
| 460 | |
| 461 | (void) FPDFAvail_IsDocAvail(pdf_avail, &hints); |
| 462 | |
| 463 | if (!FPDFAvail_IsLinearized(pdf_avail)) { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 464 | fprintf(stderr, "Non-linearized path...\n"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 465 | doc = FPDF_LoadCustomDocument(&file_access, NULL); |
| 466 | } else { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 467 | fprintf(stderr, "Linearized path...\n"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 468 | doc = FPDFAvail_GetDocument(pdf_avail, NULL); |
| 469 | } |
| 470 | |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 471 | if (!doc) |
| 472 | { |
| 473 | fprintf(stderr, "Load pdf docs unsuccessful.\n"); |
| 474 | return; |
| 475 | } |
| 476 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 477 | (void) FPDF_GetDocPermissions(doc); |
| 478 | (void) FPDFAvail_IsFormAvail(pdf_avail, &hints); |
| 479 | |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 480 | FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); |
JUN FANG | 827a172 | 2015-03-05 13:39:21 -0800 | [diff] [blame] | 481 | int docType = DOCTYPE_PDF; |
| 482 | if (FPDF_HasXFAField(doc, docType)) |
| 483 | { |
| 484 | if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc)) |
| 485 | fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); |
Tom Sepez | 5645138 | 2014-12-05 13:30:51 -0800 | [diff] [blame] | 486 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 487 | FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); |
| 488 | FPDF_SetFormFieldHighlightAlpha(form, 100); |
| 489 | |
| 490 | int first_page = FPDFAvail_GetFirstPageNum(doc); |
| 491 | (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints); |
| 492 | |
| 493 | int page_count = FPDF_GetPageCount(doc); |
| 494 | for (int i = 0; i < page_count; ++i) { |
| 495 | (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 496 | } |
| 497 | |
| 498 | FORM_DoDocumentJSAction(form); |
| 499 | FORM_DoDocumentOpenAction(form); |
| 500 | |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 501 | size_t rendered_pages = 0; |
| 502 | size_t bad_pages = 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 503 | for (int i = 0; i < page_count; ++i) { |
| 504 | FPDF_PAGE page = FPDF_LoadPage(doc, i); |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 505 | if (!page) { |
| 506 | bad_pages ++; |
| 507 | continue; |
| 508 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 509 | FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 510 | FORM_OnAfterLoadPage(page, form); |
| 511 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 512 | |
John Abd-El-Malek | 8a7b669 | 2015-02-25 11:08:16 -0800 | [diff] [blame] | 513 | double scale = 1.0; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 514 | if (!options.scale_factor_as_string.empty()) { |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 515 | std::stringstream(options.scale_factor_as_string) >> scale; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 516 | } |
John Abd-El-Malek | 8a7b669 | 2015-02-25 11:08:16 -0800 | [diff] [blame] | 517 | int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
| 518 | int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 519 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 520 | FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
Tom Sepez | bad79b3 | 2015-05-08 12:02:05 -0700 | [diff] [blame^] | 521 | if (!bitmap) { |
| 522 | fprintf(stderr, "Page was too large to be rendered.\n"); |
| 523 | bad_pages++; |
| 524 | continue; |
| 525 | } |
| 526 | |
Lei Zhang | 532a6a7 | 2014-07-09 11:47:15 -0700 | [diff] [blame] | 527 | FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 528 | FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
Jun Fang | aeacba4 | 2014-08-22 17:04:29 -0700 | [diff] [blame] | 529 | rendered_pages ++; |
| 530 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 531 | FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 532 | int stride = FPDFBitmap_GetStride(bitmap); |
| 533 | const char* buffer = |
| 534 | reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 535 | |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 536 | switch (options.output_format) { |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 537 | #ifdef _WIN32 |
| 538 | case OUTPUT_BMP: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 539 | WriteBmp(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 540 | break; |
| 541 | |
| 542 | case OUTPUT_EMF: |
Tom Sepez | c151fbb | 2014-12-18 11:14:19 -0800 | [diff] [blame] | 543 | WriteEmf(page, name.c_str(), i); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 544 | break; |
| 545 | #endif |
| 546 | case OUTPUT_PPM: |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 547 | WritePpm(name.c_str(), i, buffer, stride, width, height); |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 548 | break; |
Tom Sepez | af18cb3 | 2015-02-05 15:06:01 -0800 | [diff] [blame] | 549 | |
| 550 | case OUTPUT_PNG: |
| 551 | WritePng(name.c_str(), i, buffer, stride, width, height); |
| 552 | break; |
| 553 | |
Vitaly Buka | 9e0177a | 2014-07-22 18:15:42 -0700 | [diff] [blame] | 554 | default: |
| 555 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | FPDFBitmap_Destroy(bitmap); |
| 559 | |
| 560 | FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 561 | FORM_OnBeforeClosePage(page, form); |
| 562 | FPDFText_ClosePage(text_page); |
| 563 | FPDF_ClosePage(page); |
| 564 | } |
| 565 | |
| 566 | FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 567 | FPDF_CloseDocument(doc); |
Bo Xu | 2b7a49d | 2014-11-14 17:40:50 -0800 | [diff] [blame] | 568 | FPDFDOC_ExitFormFillEnvironment(form); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 569 | FPDFAvail_Destroy(pdf_avail); |
| 570 | |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 571 | fprintf(stderr, "Rendered %" PRIuS " pages.\n", rendered_pages); |
| 572 | fprintf(stderr, "Skipped %" PRIuS " bad pages.\n", bad_pages); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 575 | static const char usage_string[] = |
| 576 | "Usage: pdfium_test [OPTION] [FILE]...\n" |
| 577 | " --bin-dir=<path> - override path to v8 external data\n" |
| 578 | " --scale=<number> - scale output size by number (e.g. 0.5)\n" |
| 579 | #ifdef _WIN32 |
| 580 | " --bmp - write page images <pdf-name>.<page-number>.bmp\n" |
| 581 | " --emf - write page meta files <pdf-name>.<page-number>.emf\n" |
| 582 | #endif |
| 583 | " --png - write page images <pdf-name>.<page-number>.png\n" |
| 584 | " --ppm - write page images <pdf-name>.<page-number>.ppm\n"; |
| 585 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 586 | int main(int argc, const char* argv[]) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 587 | std::vector<std::string> args(argv, argv + argc); |
| 588 | Options options; |
| 589 | std::list<std::string> files; |
| 590 | if (!ParseCommandLine(args, &options, &files)) { |
Tom Sepez | 23b4e3f | 2015-02-06 16:05:23 -0800 | [diff] [blame] | 591 | fprintf(stderr, "%s", usage_string); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 592 | return 1; |
| 593 | } |
| 594 | |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 595 | v8::V8::InitializeICU(); |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 596 | v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 597 | v8::V8::InitializePlatform(platform); |
| 598 | v8::V8::Initialize(); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 599 | |
| 600 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 601 | v8::StartupData natives; |
| 602 | v8::StartupData snapshot; |
| 603 | if (!GetExternalData(options, "natives_blob.bin", &natives) || |
| 604 | !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { |
| 605 | return 1; |
| 606 | } |
| 607 | v8::V8::SetNativesDataBlob(&natives); |
| 608 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 609 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 610 | |
John Abd-El-Malek | 207299b | 2014-12-15 12:13:45 -0800 | [diff] [blame] | 611 | FPDF_InitLibrary(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 612 | |
| 613 | UNSUPPORT_INFO unsuppored_info; |
| 614 | memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 615 | unsuppored_info.version = 1; |
Tom Sepez | b7cb36a | 2015-02-13 16:54:48 -0800 | [diff] [blame] | 616 | unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 617 | |
| 618 | FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 619 | |
| 620 | while (!files.empty()) { |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 621 | std::string filename = files.front(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 622 | files.pop_front(); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 623 | size_t file_length = 0; |
| 624 | char* file_contents = GetFileContents(filename.c_str(), &file_length); |
| 625 | if (!file_contents) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 626 | continue; |
Tom Sepez | daa2e84 | 2015-01-29 15:44:37 -0800 | [diff] [blame] | 627 | RenderPdf(filename, file_contents, file_length, options); |
Tom Sepez | 5ee12d7 | 2014-12-17 16:24:01 -0800 | [diff] [blame] | 628 | free(file_contents); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | FPDF_DestroyLibrary(); |
John Abd-El-Malek | b045ed2 | 2015-02-10 09:15:12 -0800 | [diff] [blame] | 632 | v8::V8::ShutdownPlatform(); |
| 633 | delete platform; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 634 | |
| 635 | return 0; |
| 636 | } |