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

Side by Side Diff: samples/pdfium_test.cc

Issue 2330043002: Avoid static initializers and global variables in 'pdfium_test'. (Closed)
Patch Set: removed code left over 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 | no next file » | 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 <map>
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 OUTPUT_PNG, 47 OUTPUT_PNG,
48 #ifdef _WIN32 48 #ifdef _WIN32
49 OUTPUT_BMP, 49 OUTPUT_BMP,
50 OUTPUT_EMF, 50 OUTPUT_EMF,
51 #endif 51 #endif
52 #ifdef PDF_ENABLE_SKIA 52 #ifdef PDF_ENABLE_SKIA
53 OUTPUT_SKP, 53 OUTPUT_SKP,
54 #endif 54 #endif
55 }; 55 };
56 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;
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;
64
65 struct Options { 57 struct Options {
66 Options() 58 Options()
67 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {} 59 : show_config(false), send_events(false), output_format(OUTPUT_NONE) {}
68 60
69 bool show_config; 61 bool show_config;
70 bool send_events; 62 bool send_events;
71 OutputFormat output_format; 63 OutputFormat output_format;
72 std::string scale_factor_as_string; 64 std::string scale_factor_as_string;
73 std::string exe_path; 65 std::string exe_path;
74 std::string bin_directory; 66 std::string bin_directory;
75 std::string font_directory; 67 std::string font_directory;
76 }; 68 };
77 69
70 struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
71 // Hold a map of the currently loaded pages in order to avoid them
72 // to get loaded twice.
73 std::map<int, FPDF_PAGE> loadedPages;
74
75 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
76 // make use of it.
77 FPDF_FORMHANDLE formHandle;
78 };
79
80 static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
81 FPDF_FORMFILLINFO* formFillInfo) {
82 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(formFillInfo);
83 }
84
78 static bool CheckDimensions(int stride, int width, int height) { 85 static bool CheckDimensions(int stride, int width, int height) {
79 if (stride < 0 || width < 0 || height < 0) 86 if (stride < 0 || width < 0 || height < 0)
80 return false; 87 return false;
81 if (height > 0 && width > INT_MAX / height) 88 if (height > 0 && width > INT_MAX / height)
82 return false; 89 return false;
83 return true; 90 return true;
84 } 91 }
85 92
86 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 93 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
87 int stride, int width, int height) { 94 int stride, int width, int height) {
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 532 }
526 } else { 533 } else {
527 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); 534 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
528 } 535 }
529 } 536 }
530 } 537 }
531 538
532 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, 539 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
533 FPDF_DOCUMENT doc, 540 FPDF_DOCUMENT doc,
534 int index) { 541 int index) {
535 auto iter = g_loadedPages.find(index); 542 FPDF_FORMFILLINFO_PDFiumTest* formFillInfo = ToPDFiumTestFormFillInfo(param);
536 if (iter != g_loadedPages.end()) 543 auto& loadedPages = formFillInfo->loadedPages;
544
545 auto iter = loadedPages.find(index);
546 if (iter != loadedPages.end())
537 return iter->second; 547 return iter->second;
538 548
539 FPDF_PAGE page = FPDF_LoadPage(doc, index); 549 FPDF_PAGE page = FPDF_LoadPage(doc, index);
540 if (!page) 550 if (!page)
541 return nullptr; 551 return nullptr;
542 552
543 FORM_OnAfterLoadPage(page, g_formHandle); 553 FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
544 FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
545 554
546 g_loadedPages[index] = page; 555 FORM_OnAfterLoadPage(page, formHandle);
556 FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
557
558 loadedPages[index] = page;
547 return page; 559 return page;
548 } 560 }
549 561
550 bool RenderPage(const std::string& name, 562 bool RenderPage(const std::string& name,
551 FPDF_DOCUMENT doc, 563 FPDF_DOCUMENT doc,
552 FPDF_FORMHANDLE& form, 564 FPDF_FORMHANDLE& form,
565 FPDF_FORMFILLINFO_PDFiumTest& formFillInfo,
553 const int page_index, 566 const int page_index,
554 const Options& options, 567 const Options& options,
555 const std::string& events) { 568 const std::string& events) {
556 FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index); 569 FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
557 if (!page) 570 if (!page)
558 return false; 571 return false;
559 572
560 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 573 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
561 574
562 if (options.send_events) 575 if (options.send_events)
563 SendPageEvents(form, page, events); 576 SendPageEvents(form, page, events);
564 577
565 double scale = 1.0; 578 double scale = 1.0;
566 if (!options.scale_factor_as_string.empty()) 579 if (!options.scale_factor_as_string.empty())
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 #endif 625 #endif
613 default: 626 default:
614 break; 627 break;
615 } 628 }
616 629
617 FPDFBitmap_Destroy(bitmap); 630 FPDFBitmap_Destroy(bitmap);
618 } else { 631 } else {
619 fprintf(stderr, "Page was too large to be rendered.\n"); 632 fprintf(stderr, "Page was too large to be rendered.\n");
620 } 633 }
621 634
622 g_loadedPages.erase(page_index); 635 formFillInfo.loadedPages.erase(page_index);
636
623 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 637 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
624 FORM_OnBeforeClosePage(page, form); 638 FORM_OnBeforeClosePage(page, form);
625 FPDFText_ClosePage(text_page); 639 FPDFText_ClosePage(text_page);
626 FPDF_ClosePage(page); 640 FPDF_ClosePage(page);
627 return !!bitmap; 641 return !!bitmap;
628 } 642 }
629 643
630 void RenderPdf(const std::string& name, 644 void RenderPdf(const std::string& name,
631 const char* pBuf, 645 const char* pBuf,
632 size_t len, 646 size_t len,
633 const Options& options, 647 const Options& options,
634 const std::string& events) { 648 const std::string& events) {
635 IPDF_JSPLATFORM platform_callbacks; 649 IPDF_JSPLATFORM platform_callbacks;
636 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); 650 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
637 platform_callbacks.version = 3; 651 platform_callbacks.version = 3;
638 platform_callbacks.app_alert = ExampleAppAlert; 652 platform_callbacks.app_alert = ExampleAppAlert;
639 platform_callbacks.app_response = ExampleAppResponse; 653 platform_callbacks.app_response = ExampleAppResponse;
640 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage; 654 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
641 platform_callbacks.Doc_mail = ExampleDocMail; 655 platform_callbacks.Doc_mail = ExampleDocMail;
642 656
643 FPDF_FORMFILLINFO form_callbacks; 657 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
644 memset(&form_callbacks, '\0', sizeof(form_callbacks));
645 #ifdef PDF_ENABLE_XFA 658 #ifdef PDF_ENABLE_XFA
646 form_callbacks.version = 2; 659 form_callbacks.version = 2;
647 #else // PDF_ENABLE_XFA 660 #else // PDF_ENABLE_XFA
648 form_callbacks.version = 1; 661 form_callbacks.version = 1;
649 #endif // PDF_ENABLE_XFA 662 #endif // PDF_ENABLE_XFA
650 form_callbacks.FFI_GetPage = GetPageForIndex; 663 form_callbacks.FFI_GetPage = GetPageForIndex;
651 form_callbacks.m_pJsPlatform = &platform_callbacks; 664 form_callbacks.m_pJsPlatform = &platform_callbacks;
652 665
653 TestLoader loader(pBuf, len); 666 TestLoader loader(pBuf, len);
654 FPDF_FILEACCESS file_access; 667 FPDF_FILEACCESS file_access;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 fprintf(stderr, "Unknown error %ld", err); 737 fprintf(stderr, "Unknown error %ld", err);
725 } 738 }
726 fprintf(stderr, ".\n"); 739 fprintf(stderr, ".\n");
727 740
728 FPDFAvail_Destroy(pdf_avail); 741 FPDFAvail_Destroy(pdf_avail);
729 return; 742 return;
730 } 743 }
731 744
732 (void)FPDF_GetDocPermissions(doc); 745 (void)FPDF_GetDocPermissions(doc);
733 746
734 FPDF_FORMHANDLE form = g_formHandle = 747 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
735 FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 748 form_callbacks.formHandle = form;
749
736 #ifdef PDF_ENABLE_XFA 750 #ifdef PDF_ENABLE_XFA
737 int docType = DOCTYPE_PDF; 751 int docType = DOCTYPE_PDF;
738 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 752 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
739 !FPDF_LoadXFA(doc)) { 753 !FPDF_LoadXFA(doc)) {
740 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 754 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
741 } 755 }
742 #endif // PDF_ENABLE_XFA 756 #endif // PDF_ENABLE_XFA
743 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 757 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
744 FPDF_SetFormFieldHighlightAlpha(form, 100); 758 FPDF_SetFormFieldHighlightAlpha(form, 100);
745 759
746 FORM_DoDocumentJSAction(form); 760 FORM_DoDocumentJSAction(form);
747 FORM_DoDocumentOpenAction(form); 761 FORM_DoDocumentOpenAction(form);
748 762
749 int page_count = FPDF_GetPageCount(doc); 763 int page_count = FPDF_GetPageCount(doc);
750 int rendered_pages = 0; 764 int rendered_pages = 0;
751 int bad_pages = 0; 765 int bad_pages = 0;
752 for (int i = 0; i < page_count; ++i) { 766 for (int i = 0; i < page_count; ++i) {
753 if (bIsLinearized) { 767 if (bIsLinearized) {
754 nRet = PDF_DATA_NOTAVAIL; 768 nRet = PDF_DATA_NOTAVAIL;
755 while (nRet == PDF_DATA_NOTAVAIL) 769 while (nRet == PDF_DATA_NOTAVAIL)
756 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 770 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
757 771
758 if (nRet == PDF_DATA_ERROR) { 772 if (nRet == PDF_DATA_ERROR) {
759 fprintf(stderr, "Unknown error in checking if page %d is available.\n", 773 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
760 i); 774 i);
761 return; 775 return;
762 } 776 }
763 } 777 }
764 if (RenderPage(name, doc, form, i, options, events)) 778 if (RenderPage(name, doc, form, form_callbacks, i, options, events))
765 ++rendered_pages; 779 ++rendered_pages;
766 else 780 else
767 ++bad_pages; 781 ++bad_pages;
768 } 782 }
769 783
770 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 784 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
771 785
772 #ifdef PDF_ENABLE_XFA 786 #ifdef PDF_ENABLE_XFA
773 // Note: The shut down order here is the reverse of the non-XFA branch order. 787 // Note: The shut down order here is the reverse of the non-XFA branch order.
774 // Need to work out if this is required, and if it is, the lifetimes of 788 // Need to work out if this is required, and if it is, the lifetimes of
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 delete platform; 924 delete platform;
911 925
912 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 926 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
913 free(const_cast<char*>(natives.data)); 927 free(const_cast<char*>(natives.data));
914 free(const_cast<char*>(snapshot.data)); 928 free(const_cast<char*>(snapshot.data));
915 #endif // V8_USE_EXTERNAL_STARTUP_DATA 929 #endif // V8_USE_EXTERNAL_STARTUP_DATA
916 #endif // PDF_ENABLE_V8 930 #endif // PDF_ENABLE_V8
917 931
918 return 0; 932 return 0;
919 } 933 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698