| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits.h> | 5 #include <limits.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "../public/fpdf_dataavail.h" | 16 #include "../public/fpdf_dataavail.h" |
| 17 #include "../public/fpdf_ext.h" | 17 #include "../public/fpdf_ext.h" |
| 18 #include "../public/fpdf_formfill.h" | 18 #include "../public/fpdf_formfill.h" |
| 19 #include "../public/fpdf_text.h" | 19 #include "../public/fpdf_text.h" |
| 20 #include "../public/fpdfview.h" | 20 #include "../public/fpdfview.h" |
| 21 #include "image_diff_png.h" | 21 #include "image_diff_png.h" |
| 22 #ifdef PDF_ENABLE_V8 |
| 22 #include "v8/include/libplatform/libplatform.h" | 23 #include "v8/include/libplatform/libplatform.h" |
| 23 #include "v8/include/v8.h" | 24 #include "v8/include/v8.h" |
| 25 #endif |
| 24 | 26 |
| 25 #ifdef _WIN32 | 27 #ifdef _WIN32 |
| 26 #define snprintf _snprintf | 28 #define snprintf _snprintf |
| 27 #define PATH_SEPARATOR '\\' | 29 #define PATH_SEPARATOR '\\' |
| 28 #else | 30 #else |
| 29 #define PATH_SEPARATOR '/' | 31 #define PATH_SEPARATOR '/' |
| 30 #endif | 32 #endif |
| 31 | 33 |
| 32 enum OutputFormat { | 34 enum OutputFormat { |
| 33 OUTPUT_NONE, | 35 OUTPUT_NONE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 (void)fclose(file); | 74 (void)fclose(file); |
| 73 if (bytes_read != file_length) { | 75 if (bytes_read != file_length) { |
| 74 fprintf(stderr, "Failed to read: %s\n", filename); | 76 fprintf(stderr, "Failed to read: %s\n", filename); |
| 75 free(buffer); | 77 free(buffer); |
| 76 return nullptr; | 78 return nullptr; |
| 77 } | 79 } |
| 78 *retlen = bytes_read; | 80 *retlen = bytes_read; |
| 79 return buffer; | 81 return buffer; |
| 80 } | 82 } |
| 81 | 83 |
| 84 #ifdef PDF_ENABLE_V8 |
| 82 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 85 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 83 // Returns the full path for an external V8 data file based on either | 86 // Returns the full path for an external V8 data file based on either |
| 84 // the currect exectuable path or an explicit override. | 87 // the currect exectuable path or an explicit override. |
| 85 static std::string GetFullPathForSnapshotFile(const Options& options, | 88 static std::string GetFullPathForSnapshotFile(const Options& options, |
| 86 const std::string& filename) { | 89 const std::string& filename) { |
| 87 std::string result; | 90 std::string result; |
| 88 if (!options.bin_directory.empty()) { | 91 if (!options.bin_directory.empty()) { |
| 89 result = options.bin_directory; | 92 result = options.bin_directory; |
| 90 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { | 93 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { |
| 91 result += PATH_SEPARATOR; | 94 result += PATH_SEPARATOR; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 109 size_t data_length = 0; | 112 size_t data_length = 0; |
| 110 char* data_buffer = GetFileContents(full_path.c_str(), &data_length); | 113 char* data_buffer = GetFileContents(full_path.c_str(), &data_length); |
| 111 if (!data_buffer) { | 114 if (!data_buffer) { |
| 112 return false; | 115 return false; |
| 113 } | 116 } |
| 114 result_data->data = const_cast<const char*>(data_buffer); | 117 result_data->data = const_cast<const char*>(data_buffer); |
| 115 result_data->raw_size = static_cast<int>(data_length); | 118 result_data->raw_size = static_cast<int>(data_length); |
| 116 return true; | 119 return true; |
| 117 } | 120 } |
| 118 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 121 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 122 #endif // PDF_ENABLE_V8 |
| 119 | 123 |
| 120 static bool CheckDimensions(int stride, int width, int height) { | 124 static bool CheckDimensions(int stride, int width, int height) { |
| 121 if (stride < 0 || width < 0 || height < 0) | 125 if (stride < 0 || width < 0 || height < 0) |
| 122 return false; | 126 return false; |
| 123 if (height > 0 && width > INT_MAX / height) | 127 if (height > 0 && width > INT_MAX / height) |
| 124 return false; | 128 return false; |
| 125 return true; | 129 return true; |
| 126 } | 130 } |
| 127 | 131 |
| 128 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, | 132 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 options->output_format = OUTPUT_EMF; | 370 options->output_format = OUTPUT_EMF; |
| 367 } | 371 } |
| 368 else if (cur_arg == "--bmp") { | 372 else if (cur_arg == "--bmp") { |
| 369 if (options->output_format != OUTPUT_NONE) { | 373 if (options->output_format != OUTPUT_NONE) { |
| 370 fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); | 374 fprintf(stderr, "Duplicate or conflicting --bmp argument\n"); |
| 371 return false; | 375 return false; |
| 372 } | 376 } |
| 373 options->output_format = OUTPUT_BMP; | 377 options->output_format = OUTPUT_BMP; |
| 374 } | 378 } |
| 375 #endif // _WIN32 | 379 #endif // _WIN32 |
| 380 #ifdef PDF_ENABLE_V8 |
| 376 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 381 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 377 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) { | 382 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) { |
| 378 if (!options->bin_directory.empty()) { | 383 if (!options->bin_directory.empty()) { |
| 379 fprintf(stderr, "Duplicate --bin-dir argument\n"); | 384 fprintf(stderr, "Duplicate --bin-dir argument\n"); |
| 380 return false; | 385 return false; |
| 381 } | 386 } |
| 382 options->bin_directory = cur_arg.substr(10); | 387 options->bin_directory = cur_arg.substr(10); |
| 383 } | 388 } |
| 384 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 389 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 390 #endif // PDF_ENABLE_V8 |
| 385 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { | 391 else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) { |
| 386 if (!options->scale_factor_as_string.empty()) { | 392 if (!options->scale_factor_as_string.empty()) { |
| 387 fprintf(stderr, "Duplicate --scale argument\n"); | 393 fprintf(stderr, "Duplicate --scale argument\n"); |
| 388 return false; | 394 return false; |
| 389 } | 395 } |
| 390 options->scale_factor_as_string = cur_arg.substr(8); | 396 options->scale_factor_as_string = cur_arg.substr(8); |
| 391 } | 397 } |
| 392 else | 398 else |
| 393 break; | 399 break; |
| 394 } | 400 } |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 | 594 |
| 589 int main(int argc, const char* argv[]) { | 595 int main(int argc, const char* argv[]) { |
| 590 std::vector<std::string> args(argv, argv + argc); | 596 std::vector<std::string> args(argv, argv + argc); |
| 591 Options options; | 597 Options options; |
| 592 std::list<std::string> files; | 598 std::list<std::string> files; |
| 593 if (!ParseCommandLine(args, &options, &files)) { | 599 if (!ParseCommandLine(args, &options, &files)) { |
| 594 fprintf(stderr, "%s", usage_string); | 600 fprintf(stderr, "%s", usage_string); |
| 595 return 1; | 601 return 1; |
| 596 } | 602 } |
| 597 | 603 |
| 604 #ifdef PDF_ENABLE_V8 |
| 598 v8::V8::InitializeICU(); | 605 v8::V8::InitializeICU(); |
| 599 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | 606 v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 600 v8::V8::InitializePlatform(platform); | 607 v8::V8::InitializePlatform(platform); |
| 601 v8::V8::Initialize(); | 608 v8::V8::Initialize(); |
| 602 | 609 |
| 603 // By enabling predictable mode, V8 won't post any background tasks. | 610 // By enabling predictable mode, V8 won't post any background tasks. |
| 604 static const char predictable_flag[] = "--predictable"; | 611 static const char predictable_flag[] = "--predictable"; |
| 605 v8::V8::SetFlagsFromString(predictable_flag, | 612 v8::V8::SetFlagsFromString(predictable_flag, |
| 606 static_cast<int>(strlen(predictable_flag))); | 613 static_cast<int>(strlen(predictable_flag))); |
| 607 | 614 |
| 608 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 615 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 609 v8::StartupData natives; | 616 v8::StartupData natives; |
| 610 v8::StartupData snapshot; | 617 v8::StartupData snapshot; |
| 611 if (!GetExternalData(options, "natives_blob.bin", &natives) || | 618 if (!GetExternalData(options, "natives_blob.bin", &natives) || |
| 612 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { | 619 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { |
| 613 return 1; | 620 return 1; |
| 614 } | 621 } |
| 615 v8::V8::SetNativesDataBlob(&natives); | 622 v8::V8::SetNativesDataBlob(&natives); |
| 616 v8::V8::SetSnapshotDataBlob(&snapshot); | 623 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 617 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 624 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 625 #endif // PDF_ENABLE_V8 |
| 618 | 626 |
| 619 FPDF_LIBRARY_CONFIG config; | 627 FPDF_LIBRARY_CONFIG config; |
| 620 config.version = 2; | 628 config.version = 2; |
| 621 config.m_pUserFontPaths = nullptr; | 629 config.m_pUserFontPaths = nullptr; |
| 622 config.m_pIsolate = nullptr; | 630 config.m_pIsolate = nullptr; |
| 623 config.m_v8EmbedderSlot = 0; | 631 config.m_v8EmbedderSlot = 0; |
| 624 | 632 |
| 625 const char* path_array[2]; | 633 const char* path_array[2]; |
| 626 if (!options.font_directory.empty()) { | 634 if (!options.font_directory.empty()) { |
| 627 path_array[0] = options.font_directory.c_str(); | 635 path_array[0] = options.font_directory.c_str(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 642 files.pop_front(); | 650 files.pop_front(); |
| 643 size_t file_length = 0; | 651 size_t file_length = 0; |
| 644 char* file_contents = GetFileContents(filename.c_str(), &file_length); | 652 char* file_contents = GetFileContents(filename.c_str(), &file_length); |
| 645 if (!file_contents) | 653 if (!file_contents) |
| 646 continue; | 654 continue; |
| 647 RenderPdf(filename, file_contents, file_length, options); | 655 RenderPdf(filename, file_contents, file_length, options); |
| 648 free(file_contents); | 656 free(file_contents); |
| 649 } | 657 } |
| 650 | 658 |
| 651 FPDF_DestroyLibrary(); | 659 FPDF_DestroyLibrary(); |
| 660 #ifdef PDF_ENABLE_V8 |
| 652 v8::V8::ShutdownPlatform(); | 661 v8::V8::ShutdownPlatform(); |
| 653 delete platform; | 662 delete platform; |
| 663 #endif // PDF_ENABLE_V8 |
| 654 | 664 |
| 655 return 0; | 665 return 0; |
| 656 } | 666 } |
| OLD | NEW |