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 #ifdef PDF_ENABLE_XFA |
| 449 else if (tokens[1] == "right") |
| 450 FORM_OnRButtonDown(form, page, 0, x, y); |
| 451 #endif |
| 452 else |
| 453 fprintf(stderr, "mousedown: bad button name\n"); |
| 454 } else { |
| 455 fprintf(stderr, "mousedown: bad args\n"); |
| 456 } |
| 457 } else if (tokens[0] == "mouseup") { |
| 458 if (tokens.size() == 4) { |
| 459 int x = atoi(tokens[2].c_str()); |
| 460 int y = atoi(tokens[3].c_str()); |
| 461 if (tokens[1] == "left") |
| 462 FORM_OnLButtonUp(form, page, 0, x, y); |
| 463 #ifdef PDF_ENABLE_XFA |
| 464 else if (tokens[1] == "right") |
| 465 FORM_OnRButtonUp(form, page, 0, x, y); |
| 466 #endif |
| 467 else |
| 468 fprintf(stderr, "mouseup: bad button name\n"); |
| 469 } else { |
| 470 fprintf(stderr, "mouseup: bad args\n"); |
| 471 } |
| 472 } else if (tokens[0] == "mousemove") { |
| 473 if (tokens.size() == 3) { |
| 474 int x = atoi(tokens[1].c_str()); |
| 475 int y = atoi(tokens[2].c_str()); |
| 476 FORM_OnMouseMove(form, page, 0, x, y); |
| 477 } else { |
| 478 fprintf(stderr, "mousemove: bad args\n"); |
| 479 } |
| 480 } else { |
| 481 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); |
| 482 } |
| 483 } |
| 484 } |
| 485 |
421 bool RenderPage(const std::string& name, | 486 bool RenderPage(const std::string& name, |
422 const FPDF_DOCUMENT& doc, | 487 const FPDF_DOCUMENT& doc, |
423 const FPDF_FORMHANDLE& form, | 488 const FPDF_FORMHANDLE& form, |
424 const int page_index, | 489 const int page_index, |
425 const Options& options) { | 490 const Options& options, |
| 491 const std::string& events) { |
426 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); | 492 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); |
427 if (!page) { | 493 if (!page) { |
428 return false; | 494 return false; |
429 } | 495 } |
430 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); | 496 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); |
431 FORM_OnAfterLoadPage(page, form); | 497 FORM_OnAfterLoadPage(page, form); |
432 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); | 498 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
433 | 499 |
| 500 if (options.send_events) |
| 501 SendPageEvents(doc, form, events); |
| 502 |
434 double scale = 1.0; | 503 double scale = 1.0; |
435 if (!options.scale_factor_as_string.empty()) { | 504 if (!options.scale_factor_as_string.empty()) { |
436 std::stringstream(options.scale_factor_as_string) >> scale; | 505 std::stringstream(options.scale_factor_as_string) >> scale; |
437 } | 506 } |
438 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); | 507 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); |
439 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); | 508 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); |
440 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; | 509 int alpha = FPDFPage_HasTransparency(page) ? 1 : 0; |
441 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha); | 510 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, alpha); |
442 if (!bitmap) { | 511 if (!bitmap) { |
443 fprintf(stderr, "Page was too large to be rendered.\n"); | 512 fprintf(stderr, "Page was too large to be rendered.\n"); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 } | 552 } |
484 | 553 |
485 FPDFBitmap_Destroy(bitmap); | 554 FPDFBitmap_Destroy(bitmap); |
486 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 555 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
487 FORM_OnBeforeClosePage(page, form); | 556 FORM_OnBeforeClosePage(page, form); |
488 FPDFText_ClosePage(text_page); | 557 FPDFText_ClosePage(text_page); |
489 FPDF_ClosePage(page); | 558 FPDF_ClosePage(page); |
490 return true; | 559 return true; |
491 } | 560 } |
492 | 561 |
493 void RenderPdf(const std::string& name, const char* pBuf, size_t len, | 562 void RenderPdf(const std::string& name, |
494 const Options& options) { | 563 const char* pBuf, |
| 564 size_t len, |
| 565 const Options& options, |
| 566 const std::string& events) { |
495 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); | 567 fprintf(stderr, "Rendering PDF file %s.\n", name.c_str()); |
496 | 568 |
497 IPDF_JSPLATFORM platform_callbacks; | 569 IPDF_JSPLATFORM platform_callbacks; |
498 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); | 570 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
499 platform_callbacks.version = 3; | 571 platform_callbacks.version = 3; |
500 platform_callbacks.app_alert = ExampleAppAlert; | 572 platform_callbacks.app_alert = ExampleAppAlert; |
501 platform_callbacks.app_response = ExampleAppResponse; | 573 platform_callbacks.app_response = ExampleAppResponse; |
502 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; | 574 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; |
503 platform_callbacks.Doc_mail = ExampleDocMail; | 575 platform_callbacks.Doc_mail = ExampleDocMail; |
504 | 576 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 nRet = PDF_DATA_NOTAVAIL; | 688 nRet = PDF_DATA_NOTAVAIL; |
617 while (nRet == PDF_DATA_NOTAVAIL) { | 689 while (nRet == PDF_DATA_NOTAVAIL) { |
618 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); | 690 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); |
619 } | 691 } |
620 if (nRet == PDF_DATA_ERROR) { | 692 if (nRet == PDF_DATA_ERROR) { |
621 fprintf(stderr, "Unknown error in checking if page %d is available.\n", | 693 fprintf(stderr, "Unknown error in checking if page %d is available.\n", |
622 i); | 694 i); |
623 return; | 695 return; |
624 } | 696 } |
625 } | 697 } |
626 if (RenderPage(name, doc, form, i, options)) { | 698 if (RenderPage(name, doc, form, i, options, events)) { |
627 ++rendered_pages; | 699 ++rendered_pages; |
628 } else { | 700 } else { |
629 ++bad_pages; | 701 ++bad_pages; |
630 } | 702 } |
631 } | 703 } |
632 | 704 |
633 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 705 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
634 | 706 |
635 #ifdef PDF_ENABLE_XFA | 707 #ifdef PDF_ENABLE_XFA |
636 // Note: The shut down order here is the reverse of the non-XFA branch order. | 708 // 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; | 809 unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler; |
738 | 810 |
739 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 811 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
740 | 812 |
741 while (!files.empty()) { | 813 while (!files.empty()) { |
742 std::string filename = files.front(); | 814 std::string filename = files.front(); |
743 files.pop_front(); | 815 files.pop_front(); |
744 size_t file_length = 0; | 816 size_t file_length = 0; |
745 std::unique_ptr<char, pdfium::FreeDeleter> file_contents = | 817 std::unique_ptr<char, pdfium::FreeDeleter> file_contents = |
746 GetFileContents(filename.c_str(), &file_length); | 818 GetFileContents(filename.c_str(), &file_length); |
747 if (file_contents) | 819 if (!file_contents) |
748 RenderPdf(filename, file_contents.get(), file_length, options); | 820 continue; |
| 821 std::string events; |
| 822 if (options.send_events) { |
| 823 std::string event_filename = filename; |
| 824 size_t event_length = 0; |
| 825 size_t extension_pos = event_filename.find(".pdf"); |
| 826 if (extension_pos != std::string::npos) { |
| 827 event_filename.replace(extension_pos, 4, ".evt"); |
| 828 std::unique_ptr<char, pdfium::FreeDeleter> event_contents = |
| 829 GetFileContents(event_filename.c_str(), &event_length); |
| 830 if (event_contents) { |
| 831 events = std::string(event_contents.get(), event_length); |
| 832 } else { |
| 833 fprintf(stderr, "Warning: no event file: %s\n", |
| 834 event_filename.c_str()); |
| 835 } |
| 836 } |
| 837 } |
| 838 RenderPdf(filename, file_contents.get(), file_length, options, events); |
749 } | 839 } |
750 | 840 |
751 FPDF_DestroyLibrary(); | 841 FPDF_DestroyLibrary(); |
752 #ifdef PDF_ENABLE_V8 | 842 #ifdef PDF_ENABLE_V8 |
753 v8::V8::ShutdownPlatform(); | 843 v8::V8::ShutdownPlatform(); |
754 delete platform; | 844 delete platform; |
755 #endif // PDF_ENABLE_V8 | 845 #endif // PDF_ENABLE_V8 |
756 | 846 |
757 return 0; | 847 return 0; |
758 } | 848 } |
OLD | NEW |