Chromium Code Reviews| 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> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 #ifdef _WIN32 | 47 #ifdef _WIN32 |
| 48 OUTPUT_BMP, | 48 OUTPUT_BMP, |
| 49 OUTPUT_EMF, | 49 OUTPUT_EMF, |
| 50 #endif | 50 #endif |
| 51 #ifdef PDF_ENABLE_SKIA | 51 #ifdef PDF_ENABLE_SKIA |
| 52 OUTPUT_SKP, | 52 OUTPUT_SKP, |
| 53 #endif | 53 #endif |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 struct Options { | 56 struct Options { |
| 57 Options() : show_config(false), output_format(OUTPUT_NONE) {} | 57 Options() |
| 58 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {} | |
| 58 | 59 |
| 59 bool show_config; | 60 bool show_config; |
| 61 bool send_events; | |
| 60 OutputFormat output_format; | 62 OutputFormat output_format; |
| 61 std::string scale_factor_as_string; | 63 std::string scale_factor_as_string; |
| 62 std::string exe_path; | 64 std::string exe_path; |
| 63 std::string bin_directory; | 65 std::string bin_directory; |
| 64 std::string font_directory; | 66 std::string font_directory; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 static bool CheckDimensions(int stride, int width, int height) { | 69 static bool CheckDimensions(int stride, int width, int height) { |
| 68 if (stride < 0 || width < 0 || height < 0) | 70 if (stride < 0 || width < 0 || height < 0) |
| 69 return false; | 71 return false; |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 Options* options, std::list<std::string>* files) { | 333 Options* options, std::list<std::string>* files) { |
| 332 if (args.empty()) { | 334 if (args.empty()) { |
| 333 return false; | 335 return false; |
| 334 } | 336 } |
| 335 options->exe_path = args[0]; | 337 options->exe_path = args[0]; |
| 336 size_t cur_idx = 1; | 338 size_t cur_idx = 1; |
| 337 for (; cur_idx < args.size(); ++cur_idx) { | 339 for (; cur_idx < args.size(); ++cur_idx) { |
| 338 const std::string& cur_arg = args[cur_idx]; | 340 const std::string& cur_arg = args[cur_idx]; |
| 339 if (cur_arg == "--show-config") { | 341 if (cur_arg == "--show-config") { |
| 340 options->show_config = true; | 342 options->show_config = true; |
| 343 } else if (cur_arg == "--send-events") { | |
| 344 options->send_events = true; | |
| 341 } else if (cur_arg == "--ppm") { | 345 } else if (cur_arg == "--ppm") { |
| 342 if (options->output_format != OUTPUT_NONE) { | 346 if (options->output_format != OUTPUT_NONE) { |
| 343 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); | 347 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 344 return false; | 348 return false; |
| 345 } | 349 } |
| 346 options->output_format = OUTPUT_PPM; | 350 options->output_format = OUTPUT_PPM; |
| 347 } else if (cur_arg == "--png") { | 351 } else if (cur_arg == "--png") { |
| 348 if (options->output_format != OUTPUT_NONE) { | 352 if (options->output_format != OUTPUT_NONE) { |
| 349 fprintf(stderr, "Duplicate or conflicting --png argument\n"); | 353 fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 350 return false; | 354 return false; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 return true; | 415 return true; |
| 412 } | 416 } |
| 413 | 417 |
| 414 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 418 FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 415 return true; | 419 return true; |
| 416 } | 420 } |
| 417 | 421 |
| 418 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 422 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 419 } | 423 } |
| 420 | 424 |
| 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 else if (tokens[1] == "right") | |
| 449 FORM_OnRButtonDown(form, page, 0, x, y); | |
| 450 else | |
| 451 fprintf(stderr, "mousedown: bad button name\n"); | |
| 452 } else { | |
| 453 fprintf(stderr, "mousedown: bad args\n"); | |
| 454 } | |
| 455 } else if (tokens[0] == "mouseup") { | |
| 456 if (tokens.size() == 4) { | |
| 457 int x = atoi(tokens[2].c_str()); | |
| 458 int y = atoi(tokens[3].c_str()); | |
| 459 if (tokens[1] == "left") | |
| 460 FORM_OnLButtonUp(form, page, 0, x, y); | |
| 461 else if (tokens[1] == "right") | |
| 462 FORM_OnRButtonUp(form, page, 0, x, y); | |
| 463 else | |
| 464 fprintf(stderr, "mouseup: bad button name\n"); | |
| 465 } else { | |
| 466 fprintf(stderr, "mouseup: bad args\n"); | |
| 467 } | |
| 468 } else if (tokens[0] == "mousemove") { | |
| 469 if (tokens.size() == 3) { | |
| 470 int x = atoi(tokens[1].c_str()); | |
| 471 int y = atoi(tokens[2].c_str()); | |
| 472 FORM_OnMouseMove(form, page, 0, x, y); | |
| 473 } else { | |
| 474 fprintf(stderr, "mousemove: bad args\n"); | |
| 475 } | |
| 476 } else { | |
| 477 fprintf(stderr, "Unrecognined event: %s\n", tokens[0].c_str()); | |
|
Lei Zhang
2016/04/18 22:49:10
typo
Tom Sepez
2016/04/18 23:02:11
Done.
| |
| 478 } | |
| 479 } | |
| 480 } | |
| 481 | |
| 421 bool RenderPage(const std::string& name, | 482 bool RenderPage(const std::string& name, |
| 422 const FPDF_DOCUMENT& doc, | 483 const FPDF_DOCUMENT& doc, |
| 423 const FPDF_FORMHANDLE& form, | 484 const FPDF_FORMHANDLE& form, |
| 424 const int page_index, | 485 const int page_index, |
| 425 const Options& options) { | 486 const Options& options, |
| 487 const std::string& events) { | |
| 426 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); | 488 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); |
| 427 if (!page) { | 489 if (!page) { |
| 428 return false; | 490 return false; |
| 429 } | 491 } |
| 430 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); | 492 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
| 431 FORM_OnAfterLoadPage(page, form); | 493 FORM_OnAfterLoadPage(page, form); |
| 432 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); | 494 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 433 | 495 |
| 496 if (options.send_events) | |
| 497 SendPageEvents(doc, form, events); | |
| 498 | |
| 434 double scale = 1.0; | 499 double scale = 1.0; |
| 435 if (!options.scale_factor_as_string.empty()) { | 500 if (!options.scale_factor_as_string.empty()) { |
| 436 std::stringstream(options.scale_factor_as_string) >> scale; | 501 std::stringstream(options.scale_factor_as_string) >> scale; |
| 437 } | 502 } |
| 438 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); | 503 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
| 439 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); | 504 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
| 440 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; | 505 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; |
| 441 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha); | 506 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha); |
| 442 if (!bitmap) { | 507 if (!bitmap) { |
| 443 fprintf(stderr, "Page was too large to be rendered.\n"); | 508 fprintf(stderr, "Page was too large to be rendered.\n"); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 } | 548 } |
| 484 | 549 |
| 485 FPDFBitmap_Destroy(bitmap); | 550 FPDFBitmap_Destroy(bitmap); |
| 486 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 551 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 487 FORM_OnBeforeClosePage(page, form); | 552 FORM_OnBeforeClosePage(page, form); |
| 488 FPDFText_ClosePage(text_page); | 553 FPDFText_ClosePage(text_page); |
| 489 FPDF_ClosePage(page); | 554 FPDF_ClosePage(page); |
| 490 return true; | 555 return true; |
| 491 } | 556 } |
| 492 | 557 |
| 493 void RenderPdf(const std::string& name, const char* pBuf, size_t len, | 558 void RenderPdf(const std::string& name, |
| 494 const Options& options) { | 559 const char* pBuf, |
| 560 size_t len, | |
| 561 const Options& options, | |
| 562 const std::string& events) { | |
| 495 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); | 563 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); |
| 496 | 564 |
| 497 IPDF_JSPLATFORM platform_callbacks; | 565 IPDF_JSPLATFORM platform_callbacks; |
| 498 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); | 566 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 499 platform_callbacks.version = 3; | 567 platform_callbacks.version = 3; |
| 500 platform_callbacks.app_alert = ExampleAppAlert; | 568 platform_callbacks.app_alert = ExampleAppAlert; |
| 501 platform_callbacks.app_response = ExampleAppResponse; | 569 platform_callbacks.app_response = ExampleAppResponse; |
| 502 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; | 570 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
| 503 platform_callbacks.Doc_mail = ExampleDocMail; | 571 platform_callbacks.Doc_mail = ExampleDocMail; |
| 504 | 572 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 nRet = PDF_DATA_NOTAVAIL; | 684 nRet = PDF_DATA_NOTAVAIL; |
| 617 while (nRet == PDF_DATA_NOTAVAIL) { | 685 while (nRet == PDF_DATA_NOTAVAIL) { |
| 618 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); | 686 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
| 619 } | 687 } |
| 620 if (nRet == PDF_DATA_ERROR) { | 688 if (nRet == PDF_DATA_ERROR) { |
| 621 fprintf(stderr, "Unknown error in checking if page %d is available.\n", | 689 fprintf(stderr, "Unknown error in checking if page %d is available.\n", |
| 622 i); | 690 i); |
| 623 return; | 691 return; |
| 624 } | 692 } |
| 625 } | 693 } |
| 626 if (RenderPage(name, doc, form, i, options)) { | 694 if (RenderPage(name, doc, form, i, options, events)) { |
| 627 ++rendered_pages; | 695 ++rendered_pages; |
| 628 } else { | 696 } else { |
| 629 ++bad_pages; | 697 ++bad_pages; |
| 630 } | 698 } |
| 631 } | 699 } |
| 632 | 700 |
| 633 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 701 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 634 | 702 |
| 635 #ifdef PDF_ENABLE_XFA | 703 #ifdef PDF_ENABLE_XFA |
| 636 // Note: The shut down order here is the reverse of the non-XFA branch order. | 704 // Note: The shut down order here is the reverse of the non-XFA branch order. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; | 805 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
| 738 | 806 |
| 739 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 807 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 740 | 808 |
| 741 while (!files.empty()) { | 809 while (!files.empty()) { |
| 742 std::string filename = files.front(); | 810 std::string filename = files.front(); |
| 743 files.pop_front(); | 811 files.pop_front(); |
| 744 size_t file_length = 0; | 812 size_t file_length = 0; |
| 745 std::unique_ptr<char, pdfium::FreeDeleter> file_contents = | 813 std::unique_ptr<char, pdfium::FreeDeleter> file_contents = |
| 746 GetFileContents(filename.c_str(), &file_length); | 814 GetFileContents(filename.c_str(), &file_length); |
| 747 if (file_contents) | 815 if (!file_contents) |
| 748 RenderPdf(filename, file_contents.get(), file_length, options); | 816 continue; |
| 817 std::string events; | |
| 818 if (options.send_events) { | |
| 819 std::string event_filename = filename; | |
| 820 size_t event_length = 0; | |
| 821 size_t extension_pos = event_filename.find(".pdf"); | |
| 822 if (extension_pos != std::string::npos) { | |
| 823 event_filename.replace(extension_pos, 4, ".evt"); | |
| 824 std::unique_ptr<char, pdfium::FreeDeleter> event_contents = | |
| 825 GetFileContents(event_filename.c_str(), &event_length); | |
| 826 if (event_contents) | |
| 827 events = std::string(event_contents.get(), event_length); | |
| 828 else | |
|
Lei Zhang
2016/04/18 22:49:10
braces
| |
| 829 fprintf(stderr, "Warning: no event file: %s\n", | |
| 830 event_filename.c_str()); | |
| 831 } | |
| 832 } | |
| 833 RenderPdf(filename, file_contents.get(), file_length, options, events); | |
| 749 } | 834 } |
| 750 | 835 |
| 751 FPDF_DestroyLibrary(); | 836 FPDF_DestroyLibrary(); |
| 752 #ifdef PDF_ENABLE_V8 | 837 #ifdef PDF_ENABLE_V8 |
| 753 v8::V8::ShutdownPlatform(); | 838 v8::V8::ShutdownPlatform(); |
| 754 delete platform; | 839 delete platform; |
| 755 #endif // PDF_ENABLE_V8 | 840 #endif // PDF_ENABLE_V8 |
| 756 | 841 |
| 757 return 0; | 842 return 0; |
| 758 } | 843 } |
| OLD | NEW |