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

Side by Side Diff: fpdfsdk/src/fsdk_baseform.cpp

Issue 1657663003: Merge to XFA: Remove CGW_ArrayTemplate and its O(n^2 log n) sort. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 4 years, 10 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 | « fpdfsdk/include/fsdk_mgr.h ('k') | fpdfsdk/src/javascript/Field.cpp » ('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 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "fpdfsdk/include/fsdk_baseform.h" 7 #include "fpdfsdk/include/fsdk_baseform.h"
8 8
9 #include <algorithm>
9 #include <memory> 10 #include <memory>
10 11
11 #include "fpdfsdk/include/formfiller/FFL_FormFiller.h" 12 #include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
12 #include "fpdfsdk/include/fsdk_actionhandler.h" 13 #include "fpdfsdk/include/fsdk_actionhandler.h"
13 #include "fpdfsdk/include/fsdk_baseannot.h" 14 #include "fpdfsdk/include/fsdk_baseannot.h"
14 #include "fpdfsdk/include/fsdk_define.h" 15 #include "fpdfsdk/include/fsdk_define.h"
15 #include "fpdfsdk/include/fsdk_mgr.h" 16 #include "fpdfsdk/include/fsdk_mgr.h"
16 #include "fpdfsdk/include/javascript/IJavaScript.h" 17 #include "fpdfsdk/include/javascript/IJavaScript.h"
17 #include "fpdfsdk/include/pdfwindow/PWL_Utils.h" 18 #include "fpdfsdk/include/pdfwindow/PWL_Utils.h"
18 19
(...skipping 2715 matching lines...) Expand 10 before | Expand all | Expand 10 after
2734 if (nFieldType < 0 || nFieldType > kNumFieldTypes) 2735 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
2735 return FXSYS_RGB(255, 255, 255); 2736 return FXSYS_RGB(255, 255, 255);
2736 if (nFieldType == 0) 2737 if (nFieldType == 0)
2737 return m_aHighlightColor[0]; 2738 return m_aHighlightColor[0];
2738 return m_aHighlightColor[nFieldType - 1]; 2739 return m_aHighlightColor[nFieldType - 1];
2739 } 2740 }
2740 2741
2741 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, 2742 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2742 const CFX_ByteString& sType, 2743 const CFX_ByteString& sType,
2743 const CFX_ByteString& sSubType) 2744 const CFX_ByteString& sSubType)
2744 : m_pPageView(pPageView), 2745 : m_eTabOrder(STRUCTURE),
2746 m_pPageView(pPageView),
2745 m_sType(sType), 2747 m_sType(sType),
2746 m_sSubType(sSubType), 2748 m_sSubType(sSubType) {
2747 m_nTabs(BAI_STRUCTURE) {
2748 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); 2749 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
2749 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs"); 2750 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
2750 2751 if (sTabs == "R")
2751 if (sTabs == "R") { 2752 m_eTabOrder = ROW;
2752 m_nTabs = BAI_ROW; 2753 else if (sTabs == "C")
2753 } else if (sTabs == "C") { 2754 m_eTabOrder = COLUMN;
2754 m_nTabs = BAI_COLUMN;
2755 } else {
2756 m_nTabs = BAI_STRUCTURE;
2757 }
2758 2755
2759 GenerateResults(); 2756 GenerateResults();
2760 } 2757 }
2761 2758
2762 CBA_AnnotIterator::~CBA_AnnotIterator() { 2759 CBA_AnnotIterator::~CBA_AnnotIterator() {
2763 m_Annots.RemoveAll();
2764 } 2760 }
2765 2761
2766 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { 2762 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
2767 if (m_Annots.GetSize() > 0) 2763 return m_Annots.empty() ? nullptr : m_Annots.front();
2768 return m_Annots[0];
2769
2770 return NULL;
2771 } 2764 }
2772 2765
2773 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { 2766 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
2774 if (m_Annots.GetSize() > 0) 2767 return m_Annots.empty() ? nullptr : m_Annots.back();
2775 return m_Annots[m_Annots.GetSize() - 1];
2776
2777 return NULL;
2778 } 2768 }
2779 2769
2780 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { 2770 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
2781 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) { 2771 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2782 if (m_Annots[i] == pAnnot) 2772 if (iter == m_Annots.end())
2783 return (i + 1 < sz) ? m_Annots[i + 1] : m_Annots[0]; 2773 return nullptr;
2784 } 2774 ++iter;
2785 return NULL; 2775 if (iter == m_Annots.end())
2776 iter = m_Annots.begin();
2777 return *iter;
2786 } 2778 }
2787 2779
2788 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { 2780 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
2789 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) { 2781 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2790 if (m_Annots[i] == pAnnot) 2782 if (iter == m_Annots.end())
2791 return (i - 1 >= 0) ? m_Annots[i - 1] : m_Annots[sz - 1]; 2783 return nullptr;
2792 } 2784 if (iter == m_Annots.begin())
2793 return NULL; 2785 iter = m_Annots.end();
2786 return *(--iter);
2794 } 2787 }
2795 2788
2796 int CBA_AnnotIterator::CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { 2789 // static
2797 ASSERT(p1); 2790 bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2798 ASSERT(p2); 2791 const CPDFSDK_Annot* p2) {
2799 2792 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
2800 CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2801 CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2802
2803 if (rcAnnot1.left < rcAnnot2.left)
2804 return -1;
2805 if (rcAnnot1.left > rcAnnot2.left)
2806 return 1;
2807 return 0;
2808 } 2793 }
2809 2794
2810 int CBA_AnnotIterator::CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { 2795 // static
2811 ASSERT(p1); 2796 bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2812 ASSERT(p2); 2797 const CPDFSDK_Annot* p2) {
2813 2798 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
2814 CPDF_Rect rcAnnot1 = GetAnnotRect(p1);
2815 CPDF_Rect rcAnnot2 = GetAnnotRect(p2);
2816
2817 if (rcAnnot1.top < rcAnnot2.top)
2818 return -1;
2819 if (rcAnnot1.top > rcAnnot2.top)
2820 return 1;
2821 return 0;
2822 } 2799 }
2823 2800
2824 void CBA_AnnotIterator::GenerateResults() { 2801 void CBA_AnnotIterator::GenerateResults() {
2825 switch (m_nTabs) { 2802 switch (m_eTabOrder) {
2826 case BAI_STRUCTURE: { 2803 case STRUCTURE: {
2827 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { 2804 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2828 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); 2805 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2829 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) 2806 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
2830 m_Annots.Add(pAnnot); 2807 m_Annots.push_back(pAnnot);
2831 } 2808 }
2832 break; 2809 } break;
2833 } 2810 case ROW: {
2834 case BAI_ROW: { 2811 std::vector<CPDFSDK_Annot*> sa;
2835 CPDFSDK_SortAnnots sa;
2836 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { 2812 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2837 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); 2813 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2838 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) 2814 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
2839 sa.Add(pAnnot); 2815 sa.push_back(pAnnot);
2840 } 2816 }
2841 2817
2842 if (sa.GetSize() > 0) 2818 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2843 sa.Sort(CBA_AnnotIterator::CompareByLeft); 2819 while (!sa.empty()) {
2844
2845 while (sa.GetSize() > 0) {
2846 int nLeftTopIndex = -1; 2820 int nLeftTopIndex = -1;
2847 FX_FLOAT fTop = 0.0f; 2821 FX_FLOAT fTop = 0.0f;
2848 2822 for (int i = sa.size() - 1; i >= 0; i--) {
2849 for (int i = sa.GetSize() - 1; i >= 0; i--) { 2823 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]);
2850 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2851 ASSERT(pAnnot);
2852
2853 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2854
2855 if (rcAnnot.top > fTop) { 2824 if (rcAnnot.top > fTop) {
2856 nLeftTopIndex = i; 2825 nLeftTopIndex = i;
2857 fTop = rcAnnot.top; 2826 fTop = rcAnnot.top;
2858 } 2827 }
2859 } 2828 }
2829 if (nLeftTopIndex >= 0) {
2830 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
2831 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2832 m_Annots.push_back(pLeftTopAnnot);
2833 sa.erase(sa.begin() + nLeftTopIndex);
2860 2834
2861 if (nLeftTopIndex >= 0) { 2835 std::vector<int> aSelect;
2862 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex); 2836 for (int i = 0; i < sa.size(); ++i) {
2863 ASSERT(pLeftTopAnnot); 2837 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]);
2864
2865 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2866
2867 m_Annots.Add(pLeftTopAnnot);
2868 sa.RemoveAt(nLeftTopIndex);
2869
2870 CFX_ArrayTemplate<int> aSelect;
2871
2872 for (int i = 0, sz = sa.GetSize(); i < sz; ++i) {
2873 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2874 ASSERT(pAnnot);
2875
2876 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2877 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; 2838 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2878 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) 2839 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
2879 aSelect.Add(i); 2840 aSelect.push_back(i);
2880 } 2841 }
2842 for (int i = 0; i < aSelect.size(); ++i)
2843 m_Annots.push_back(sa[aSelect[i]]);
2881 2844
2882 for (int i = 0, sz = aSelect.GetSize(); i < sz; ++i) 2845 for (int i = aSelect.size() - 1; i >= 0; --i)
2883 m_Annots.Add(sa[aSelect[i]]); 2846 sa.erase(sa.begin() + aSelect[i]);
2884
2885 for (int i = aSelect.GetSize() - 1; i >= 0; --i)
2886 sa.RemoveAt(aSelect[i]);
2887
2888 aSelect.RemoveAll();
2889 } 2847 }
2890 } 2848 }
2891 sa.RemoveAll(); 2849 } break;
2892 break; 2850 case COLUMN: {
2893 } 2851 std::vector<CPDFSDK_Annot*> sa;
2894 case BAI_COLUMN: {
2895 CPDFSDK_SortAnnots sa;
2896 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { 2852 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2897 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); 2853 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2898 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) 2854 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
2899 sa.Add(pAnnot); 2855 sa.push_back(pAnnot);
2900 } 2856 }
2901 2857
2902 if (sa.GetSize() > 0) 2858 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2903 sa.Sort(CBA_AnnotIterator::CompareByTop, FALSE); 2859 while (!sa.empty()) {
2904
2905 while (sa.GetSize() > 0) {
2906 int nLeftTopIndex = -1; 2860 int nLeftTopIndex = -1;
2907 FX_FLOAT fLeft = -1.0f; 2861 FX_FLOAT fLeft = -1.0f;
2908 2862 for (int i = sa.size() - 1; i >= 0; --i) {
2909 for (int i = sa.GetSize() - 1; i >= 0; --i) { 2863 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]);
2910 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2911 ASSERT(pAnnot);
2912
2913 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2914
2915 if (fLeft < 0) { 2864 if (fLeft < 0) {
2916 nLeftTopIndex = 0; 2865 nLeftTopIndex = 0;
2917 fLeft = rcAnnot.left; 2866 fLeft = rcAnnot.left;
2918 } else if (rcAnnot.left < fLeft) { 2867 } else if (rcAnnot.left < fLeft) {
2919 nLeftTopIndex = i; 2868 nLeftTopIndex = i;
2920 fLeft = rcAnnot.left; 2869 fLeft = rcAnnot.left;
2921 } 2870 }
2922 } 2871 }
2923 2872
2924 if (nLeftTopIndex >= 0) { 2873 if (nLeftTopIndex >= 0) {
2925 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex); 2874 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
2926 ASSERT(pLeftTopAnnot); 2875 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
2876 m_Annots.push_back(pLeftTopAnnot);
2877 sa.erase(sa.begin() + nLeftTopIndex);
2927 2878
2928 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot); 2879 std::vector<int> aSelect;
2929 2880 for (int i = 0; i < sa.size(); ++i) {
2930 m_Annots.Add(pLeftTopAnnot); 2881 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]);
2931 sa.RemoveAt(nLeftTopIndex);
2932
2933 CFX_ArrayTemplate<int> aSelect;
2934 for (int i = 0, sz = sa.GetSize(); i < sz; ++i) {
2935 CPDFSDK_Annot* pAnnot = sa.GetAt(i);
2936 ASSERT(pAnnot);
2937
2938 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot);
2939 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; 2882 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2940 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) 2883 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
2941 aSelect.Add(i); 2884 aSelect.push_back(i);
2942 } 2885 }
2886 for (int i = 0; i < aSelect.size(); ++i)
2887 m_Annots.push_back(sa[aSelect[i]]);
2943 2888
2944 for (int i = 0, sz = aSelect.GetSize(); i < sz; ++i) 2889 for (int i = aSelect.size() - 1; i >= 0; --i)
2945 m_Annots.Add(sa[aSelect[i]]); 2890 sa.erase(sa.begin() + aSelect[i]);
2946
2947 for (int i = aSelect.GetSize() - 1; i >= 0; --i)
2948 sa.RemoveAt(aSelect[i]);
2949
2950 aSelect.RemoveAll();
2951 } 2891 }
2952 } 2892 }
2953 sa.RemoveAll();
2954 break; 2893 break;
2955 } 2894 }
2956 } 2895 }
2957 } 2896 }
2958 2897
2959 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(CPDFSDK_Annot* pAnnot) { 2898 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2960 CPDF_Rect rcAnnot; 2899 CPDF_Rect rcAnnot;
2961 pAnnot->GetPDFAnnot()->GetRect(rcAnnot); 2900 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
2962 return rcAnnot; 2901 return rcAnnot;
2963 } 2902 }
OLDNEW
« no previous file with comments | « fpdfsdk/include/fsdk_mgr.h ('k') | fpdfsdk/src/javascript/Field.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698