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