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