Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: samples/pdfium_test.cc

Issue 2277063003: Extend pdfium_test capability so that more Javascript can be executed (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | testing/resources/pixel/bug_492.pdf » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <map>
10 #include <sstream> 11 #include <sstream>
11 #include <string> 12 #include <string>
12 #include <utility> 13 #include <utility>
13 #include <vector> 14 #include <vector>
14 15
15 #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_ 16 #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
16 #define _SKIA_SUPPORT_ 17 #define _SKIA_SUPPORT_
17 #endif 18 #endif
18 19
19 #include "public/fpdf_dataavail.h" 20 #include "public/fpdf_dataavail.h"
(...skipping 26 matching lines...) Expand all
46 OUTPUT_PNG, 47 OUTPUT_PNG,
47 #ifdef _WIN32 48 #ifdef _WIN32
48 OUTPUT_BMP, 49 OUTPUT_BMP,
49 OUTPUT_EMF, 50 OUTPUT_EMF,
50 #endif 51 #endif
51 #ifdef PDF_ENABLE_SKIA 52 #ifdef PDF_ENABLE_SKIA
52 OUTPUT_SKP, 53 OUTPUT_SKP,
53 #endif 54 #endif
54 }; 55 };
55 56
57 // Hold a map of the currently loaded pages in order to avoid them
58 // to get loaded twice.
59 std::map<int, FPDF_PAGE> g_loadedPages;
dsinclair 2016/08/25 19:36:49 It may actually be closer to what Chromium is doin
tonikitoo 2016/08/25 19:48:58 Chromium keeps track of the pages in src/pdf/pdfiu
Lei Zhang 2016/08/27 01:03:23 I believe this actually adds a static initializer,
Lei Zhang 2016/08/27 01:03:23 Form_GetPage() does reload the page as shown. The
60
61 // Hold a global pointer of FPDF_FORMHANDLE so that PDFium
62 // app hooks can made use of it.
63 FPDF_FORMHANDLE g_formHandle;
Lei Zhang 2016/08/27 01:03:23 It would have been nice to use GetPageForIndex()'s
64
56 struct Options { 65 struct Options {
57 Options() 66 Options()
58 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {} 67 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
59 68
60 bool show_config; 69 bool show_config;
61 bool send_events; 70 bool send_events;
62 OutputFormat output_format; 71 OutputFormat output_format;
63 std::string scale_factor_as_string; 72 std::string scale_factor_as_string;
64 std::string exe_path; 73 std::string exe_path;
65 std::string bin_directory; 74 std::string bin_directory;
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 FORM_OnMouseMove(form, page, 0, x, y); 522 FORM_OnMouseMove(form, page, 0, x, y);
514 } else { 523 } else {
515 fprintf(stderr, "mousemove: bad args\n"); 524 fprintf(stderr, "mousemove: bad args\n");
516 } 525 }
517 } else { 526 } else {
518 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); 527 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
519 } 528 }
520 } 529 }
521 } 530 }
522 531
532 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
533 FPDF_DOCUMENT doc,
534 int index) {
535 auto iter = g_loadedPages.find(index);
536 if (iter != g_loadedPages.end())
537 return iter->second;
538
539 FPDF_PAGE page = FPDF_LoadPage(doc, index);
540 if (!page)
541 return nullptr;
542
543 FORM_OnAfterLoadPage(page, g_formHandle);
544 FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
545
546 g_loadedPages[index] = page;
547 return page;
548 }
549
523 bool RenderPage(const std::string& name, 550 bool RenderPage(const std::string& name,
524 const FPDF_DOCUMENT& doc, 551 FPDF_DOCUMENT doc,
525 const FPDF_FORMHANDLE& form, 552 FPDF_FORMHANDLE& form,
526 const int page_index, 553 const int page_index,
527 const Options& options, 554 const Options& options,
528 const std::string& events) { 555 const std::string& events) {
529 FPDF_PAGE page = FPDF_LoadPage(doc, page_index); 556 FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index);
530 if (!page) 557 if (!page)
531 return false; 558 return false;
532 559
533 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 560 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
534 FORM_OnAfterLoadPage(page, form);
535 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
536 561
537 if (options.send_events) 562 if (options.send_events)
538 SendPageEvents(form, page, events); 563 SendPageEvents(form, page, events);
539 564
540 double scale = 1.0; 565 double scale = 1.0;
541 if (!options.scale_factor_as_string.empty()) 566 if (!options.scale_factor_as_string.empty())
542 std::stringstream(options.scale_factor_as_string) >> scale; 567 std::stringstream(options.scale_factor_as_string) >> scale;
543 568
544 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale); 569 int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
545 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale); 570 int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 } break; 611 } break;
587 #endif 612 #endif
588 default: 613 default:
589 break; 614 break;
590 } 615 }
591 616
592 FPDFBitmap_Destroy(bitmap); 617 FPDFBitmap_Destroy(bitmap);
593 } else { 618 } else {
594 fprintf(stderr, "Page was too large to be rendered.\n"); 619 fprintf(stderr, "Page was too large to be rendered.\n");
595 } 620 }
621
622 g_loadedPages.erase(page_index);
596 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 623 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
597 FORM_OnBeforeClosePage(page, form); 624 FORM_OnBeforeClosePage(page, form);
598 FPDFText_ClosePage(text_page); 625 FPDFText_ClosePage(text_page);
599 FPDF_ClosePage(page); 626 FPDF_ClosePage(page);
600 return !!bitmap; 627 return !!bitmap;
601 } 628 }
602 629
603 void RenderPdf(const std::string& name, 630 void RenderPdf(const std::string& name,
604 const char* pBuf, 631 const char* pBuf,
605 size_t len, 632 size_t len,
606 const Options& options, 633 const Options& options,
607 const std::string& events) { 634 const std::string& events) {
608 IPDF_JSPLATFORM platform_callbacks; 635 IPDF_JSPLATFORM platform_callbacks;
609 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 636 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
610 platform_callbacks.version = 3; 637 platform_callbacks.version = 3;
611 platform_callbacks.app_alert = ExampleAppAlert; 638 platform_callbacks.app_alert = ExampleAppAlert;
612 platform_callbacks.app_response = ExampleAppResponse; 639 platform_callbacks.app_response = ExampleAppResponse;
613 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 640 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
614 platform_callbacks.Doc_mail = ExampleDocMail; 641 platform_callbacks.Doc_mail = ExampleDocMail;
615 642
616 FPDF_FORMFILLINFO form_callbacks; 643 FPDF_FORMFILLINFO form_callbacks;
617 memset(&form_callbacks, '\0', sizeof(form_callbacks)); 644 memset(&form_callbacks, '\0', sizeof(form_callbacks));
618 #ifdef PDF_ENABLE_XFA 645 #ifdef PDF_ENABLE_XFA
619 form_callbacks.version = 2; 646 form_callbacks.version = 2;
620 #else // PDF_ENABLE_XFA 647 #else // PDF_ENABLE_XFA
621 form_callbacks.version = 1; 648 form_callbacks.version = 1;
622 #endif // PDF_ENABLE_XFA 649 #endif // PDF_ENABLE_XFA
650 form_callbacks.FFI_GetPage = GetPageForIndex;
623 form_callbacks.m_pJsPlatform = &platform_callbacks; 651 form_callbacks.m_pJsPlatform = &platform_callbacks;
624 652
625 TestLoader loader(pBuf, len); 653 TestLoader loader(pBuf, len);
626 FPDF_FILEACCESS file_access; 654 FPDF_FILEACCESS file_access;
627 memset(&file_access, '\0', sizeof(file_access)); 655 memset(&file_access, '\0', sizeof(file_access));
628 file_access.m_FileLen = static_cast<unsigned long>(len); 656 file_access.m_FileLen = static_cast<unsigned long>(len);
629 file_access.m_GetBlock = TestLoader::GetBlock; 657 file_access.m_GetBlock = TestLoader::GetBlock;
630 file_access.m_Param = &loader; 658 file_access.m_Param = &loader;
631 659
632 FX_FILEAVAIL file_avail; 660 FX_FILEAVAIL file_avail;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 fprintf(stderr, "Unknown error %ld", err); 724 fprintf(stderr, "Unknown error %ld", err);
697 } 725 }
698 fprintf(stderr, ".\n"); 726 fprintf(stderr, ".\n");
699 727
700 FPDFAvail_Destroy(pdf_avail); 728 FPDFAvail_Destroy(pdf_avail);
701 return; 729 return;
702 } 730 }
703 731
704 (void)FPDF_GetDocPermissions(doc); 732 (void)FPDF_GetDocPermissions(doc);
705 733
706 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 734 FPDF_FORMHANDLE form = g_formHandle =
735 FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
707 #ifdef PDF_ENABLE_XFA 736 #ifdef PDF_ENABLE_XFA
708 int docType = DOCTYPE_PDF; 737 int docType = DOCTYPE_PDF;
709 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 738 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
710 !FPDF_LoadXFA(doc)) { 739 !FPDF_LoadXFA(doc)) {
711 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 740 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
712 } 741 }
713 #endif // PDF_ENABLE_XFA 742 #endif // PDF_ENABLE_XFA
714 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 743 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
715 FPDF_SetFormFieldHighlightAlpha(form, 100); 744 FPDF_SetFormFieldHighlightAlpha(form, 100);
716 745
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 delete platform; 910 delete platform;
882 911
883 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 912 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
884 free(const_cast<char*>(natives.data)); 913 free(const_cast<char*>(natives.data));
885 free(const_cast<char*>(snapshot.data)); 914 free(const_cast<char*>(snapshot.data));
886 #endif // V8_USE_EXTERNAL_STARTUP_DATA 915 #endif // V8_USE_EXTERNAL_STARTUP_DATA
887 #endif // PDF_ENABLE_V8 916 #endif // PDF_ENABLE_V8
888 917
889 return 0; 918 return 0;
890 } 919 }
OLDNEW
« no previous file with comments | « no previous file | testing/resources/pixel/bug_492.pdf » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698