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

Side by Side Diff: samples/pdfium_test.cc

Issue 2330043002: Avoid static initializers and global variables in 'pdfium_test'. (Closed)
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 | 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 typedef struct {
Tom Sepez 2016/09/12 17:19:41 nit: In C++, we can avoid "typedef" adn just write
tonikitoo 2016/09/12 23:41:55 Done.
71 FPDF_FORMFILLINFO formInfo;
72
73 // Hold a map of the currently loaded pages in order to avoid them
74 // to get loaded twice.
75 std::map<int, FPDF_PAGE> loadedPages;
76
77 // Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
78 // make use of it.
79 FPDF_FORMHANDLE formHandle;
80 } PDFiumTest_FPDF_FORMFILLINFO;
81
82 static PDFiumTest_FPDF_FORMFILLINFO* ToPDFiumTestFormFillInfo(
Tom Sepez 2016/09/12 17:19:40 It might be cleaner if PDFiumTest_FPDF_FORMFILLINF
tonikitoo 2016/09/12 23:41:55 Done.
83 FPDF_FORMFILLINFO* formFillInfo) {
84 return reinterpret_cast<PDFiumTest_FPDF_FORMFILLINFO*>(formFillInfo);
85 }
86
87 static FPDF_FORMFILLINFO* ToFormFillInfo(
88 PDFiumTest_FPDF_FORMFILLINFO* pdfiumTestFormFillInfo) {
89 return reinterpret_cast<FPDF_FORMFILLINFO*>(pdfiumTestFormFillInfo);
90 }
91
78 static bool CheckDimensions(int stride, int width, int height) { 92 static bool CheckDimensions(int stride, int width, int height) {
79 if (stride < 0 || width < 0 || height < 0) 93 if (stride < 0 || width < 0 || height < 0)
80 return false; 94 return false;
81 if (height > 0 && width > INT_MAX / height) 95 if (height > 0 && width > INT_MAX / height)
82 return false; 96 return false;
83 return true; 97 return true;
84 } 98 }
85 99
86 static void WritePpm(const char* pdf_name, int num, const void* buffer_void, 100 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
87 int stride, int width, int height) { 101 int stride, int width, int height) {
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 539 }
526 } else { 540 } else {
527 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str()); 541 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
528 } 542 }
529 } 543 }
530 } 544 }
531 545
532 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param, 546 FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
533 FPDF_DOCUMENT doc, 547 FPDF_DOCUMENT doc,
534 int index) { 548 int index) {
535 auto iter = g_loadedPages.find(index); 549 PDFiumTest_FPDF_FORMFILLINFO* formFillInfo = ToPDFiumTestFormFillInfo(param);
536 if (iter != g_loadedPages.end()) 550 std::map<int, FPDF_PAGE>& loadedPages = formFillInfo->loadedPages;
551
552 auto iter = loadedPages.find(index);
553 if (iter != loadedPages.end())
537 return iter->second; 554 return iter->second;
538 555
539 FPDF_PAGE page = FPDF_LoadPage(doc, index); 556 FPDF_PAGE page = FPDF_LoadPage(doc, index);
540 if (!page) 557 if (!page)
541 return nullptr; 558 return nullptr;
542 559
543 FORM_OnAfterLoadPage(page, g_formHandle); 560 FPDF_FORMHANDLE& formHandle = formFillInfo->formHandle;
544 FORM_DoPageAAction(page, g_formHandle, FPDFPAGE_AACTION_OPEN);
545 561
546 g_loadedPages[index] = page; 562 FORM_OnAfterLoadPage(page, formHandle);
563 FORM_DoPageAAction(page, formHandle, FPDFPAGE_AACTION_OPEN);
564
565 loadedPages[index] = page;
547 return page; 566 return page;
548 } 567 }
549 568
550 bool RenderPage(const std::string& name, 569 bool RenderPage(const std::string& name,
551 FPDF_DOCUMENT doc, 570 FPDF_DOCUMENT doc,
552 FPDF_FORMHANDLE& form, 571 FPDF_FORMHANDLE& form,
572 FPDF_FORMFILLINFO& formFillInfo,
553 const int page_index, 573 const int page_index,
554 const Options& options, 574 const Options& options,
555 const std::string& events) { 575 const std::string& events) {
556 FPDF_PAGE page = GetPageForIndex(nullptr, doc, page_index); 576 FPDF_PAGE page = GetPageForIndex(&formFillInfo, doc, page_index);
557 if (!page) 577 if (!page)
558 return false; 578 return false;
559 579
560 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page); 580 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
561 581
562 if (options.send_events) 582 if (options.send_events)
563 SendPageEvents(form, page, events); 583 SendPageEvents(form, page, events);
564 584
565 double scale = 1.0; 585 double scale = 1.0;
566 if (!options.scale_factor_as_string.empty()) 586 if (!options.scale_factor_as_string.empty())
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 #endif 632 #endif
613 default: 633 default:
614 break; 634 break;
615 } 635 }
616 636
617 FPDFBitmap_Destroy(bitmap); 637 FPDFBitmap_Destroy(bitmap);
618 } else { 638 } else {
619 fprintf(stderr, "Page was too large to be rendered.\n"); 639 fprintf(stderr, "Page was too large to be rendered.\n");
620 } 640 }
621 641
622 g_loadedPages.erase(page_index); 642 PDFiumTest_FPDF_FORMFILLINFO* pdfiumTestFormFillInfo =
643 ToPDFiumTestFormFillInfo(&formFillInfo);
644 pdfiumTestFormFillInfo->loadedPages.erase(page_index);
645
623 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); 646 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
624 FORM_OnBeforeClosePage(page, form); 647 FORM_OnBeforeClosePage(page, form);
625 FPDFText_ClosePage(text_page); 648 FPDFText_ClosePage(text_page);
626 FPDF_ClosePage(page); 649 FPDF_ClosePage(page);
627 return !!bitmap; 650 return !!bitmap;
628 } 651 }
629 652
630 void RenderPdf(const std::string& name, 653 void RenderPdf(const std::string& name,
631 const char* pBuf, 654 const char* pBuf,
632 size_t len, 655 size_t len,
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 fprintf(stderr, "Unknown error %ld", err); 747 fprintf(stderr, "Unknown error %ld", err);
725 } 748 }
726 fprintf(stderr, ".\n"); 749 fprintf(stderr, ".\n");
727 750
728 FPDFAvail_Destroy(pdf_avail); 751 FPDFAvail_Destroy(pdf_avail);
729 return; 752 return;
730 } 753 }
731 754
732 (void)FPDF_GetDocPermissions(doc); 755 (void)FPDF_GetDocPermissions(doc);
733 756
734 FPDF_FORMHANDLE form = g_formHandle = 757 std::unique_ptr<PDFiumTest_FPDF_FORMFILLINFO> pdfiumTestFormFillInfo(
735 FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks); 758 new PDFiumTest_FPDF_FORMFILLINFO);
759 pdfiumTestFormFillInfo->formInfo = form_callbacks;
760
761 FPDF_FORMFILLINFO* formFillInfo =
762 ToFormFillInfo(pdfiumTestFormFillInfo.get());
763
764 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, formFillInfo);
765 pdfiumTestFormFillInfo->formHandle = form;
766
736 #ifdef PDF_ENABLE_XFA 767 #ifdef PDF_ENABLE_XFA
737 int docType = DOCTYPE_PDF; 768 int docType = DOCTYPE_PDF;
738 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF && 769 if (FPDF_HasXFAField(doc, &docType) && docType != DOCTYPE_PDF &&
739 !FPDF_LoadXFA(doc)) { 770 !FPDF_LoadXFA(doc)) {
740 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n"); 771 fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
741 } 772 }
742 #endif // PDF_ENABLE_XFA 773 #endif // PDF_ENABLE_XFA
743 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD); 774 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
744 FPDF_SetFormFieldHighlightAlpha(form, 100); 775 FPDF_SetFormFieldHighlightAlpha(form, 100);
745 776
746 FORM_DoDocumentJSAction(form); 777 FORM_DoDocumentJSAction(form);
747 FORM_DoDocumentOpenAction(form); 778 FORM_DoDocumentOpenAction(form);
748 779
749 int page_count = FPDF_GetPageCount(doc); 780 int page_count = FPDF_GetPageCount(doc);
750 int rendered_pages = 0; 781 int rendered_pages = 0;
751 int bad_pages = 0; 782 int bad_pages = 0;
752 for (int i = 0; i < page_count; ++i) { 783 for (int i = 0; i < page_count; ++i) {
753 if (bIsLinearized) { 784 if (bIsLinearized) {
754 nRet = PDF_DATA_NOTAVAIL; 785 nRet = PDF_DATA_NOTAVAIL;
755 while (nRet == PDF_DATA_NOTAVAIL) 786 while (nRet == PDF_DATA_NOTAVAIL)
756 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints); 787 nRet = FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
757 788
758 if (nRet == PDF_DATA_ERROR) { 789 if (nRet == PDF_DATA_ERROR) {
759 fprintf(stderr, "Unknown error in checking if page %d is available.\n", 790 fprintf(stderr, "Unknown error in checking if page %d is available.\n",
760 i); 791 i);
761 return; 792 return;
762 } 793 }
763 } 794 }
764 if (RenderPage(name, doc, form, i, options, events)) 795 if (RenderPage(name, doc, form, *formFillInfo, i, options, events))
765 ++rendered_pages; 796 ++rendered_pages;
766 else 797 else
767 ++bad_pages; 798 ++bad_pages;
768 } 799 }
769 800
770 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); 801 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
771 802
772 #ifdef PDF_ENABLE_XFA 803 #ifdef PDF_ENABLE_XFA
773 // Note: The shut down order here is the reverse of the non-XFA branch order. 804 // 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 805 // 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; 941 delete platform;
911 942
912 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 943 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
913 free(const_cast<char*>(natives.data)); 944 free(const_cast<char*>(natives.data));
914 free(const_cast<char*>(snapshot.data)); 945 free(const_cast<char*>(snapshot.data));
915 #endif // V8_USE_EXTERNAL_STARTUP_DATA 946 #endif // V8_USE_EXTERNAL_STARTUP_DATA
916 #endif // PDF_ENABLE_V8 947 #endif // PDF_ENABLE_V8
917 948
918 return 0; 949 return 0;
919 } 950 }
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