| OLD | NEW |
| 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 2053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2072 if (nFieldType < 0 || nFieldType > kNumFieldTypes) | 2073 if (nFieldType < 0 || nFieldType > kNumFieldTypes) |
| 2073 return FXSYS_RGB(255, 255, 255); | 2074 return FXSYS_RGB(255, 255, 255); |
| 2074 if (nFieldType == 0) | 2075 if (nFieldType == 0) |
| 2075 return m_aHighlightColor[0]; | 2076 return m_aHighlightColor[0]; |
| 2076 return m_aHighlightColor[nFieldType - 1]; | 2077 return m_aHighlightColor[nFieldType - 1]; |
| 2077 } | 2078 } |
| 2078 | 2079 |
| 2079 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, | 2080 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, |
| 2080 const CFX_ByteString& sType, | 2081 const CFX_ByteString& sType, |
| 2081 const CFX_ByteString& sSubType) | 2082 const CFX_ByteString& sSubType) |
| 2082 : m_pPageView(pPageView), | 2083 : m_eTabOrder(STRUCTURE), |
| 2084 m_pPageView(pPageView), |
| 2083 m_sType(sType), | 2085 m_sType(sType), |
| 2084 m_sSubType(sSubType), | 2086 m_sSubType(sSubType) { |
| 2085 m_nTabs(BAI_STRUCTURE) { | |
| 2086 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); | 2087 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); |
| 2087 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs"); | 2088 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs"); |
| 2088 | 2089 if (sTabs == "R") |
| 2089 if (sTabs == "R") { | 2090 m_eTabOrder = ROW; |
| 2090 m_nTabs = BAI_ROW; | 2091 else if (sTabs == "C") |
| 2091 } else if (sTabs == "C") { | 2092 m_eTabOrder = COLUMN; |
| 2092 m_nTabs = BAI_COLUMN; | |
| 2093 } else { | |
| 2094 m_nTabs = BAI_STRUCTURE; | |
| 2095 } | |
| 2096 | 2093 |
| 2097 GenerateResults(); | 2094 GenerateResults(); |
| 2098 } | 2095 } |
| 2099 | 2096 |
| 2100 CBA_AnnotIterator::~CBA_AnnotIterator() { | 2097 CBA_AnnotIterator::~CBA_AnnotIterator() { |
| 2101 m_Annots.RemoveAll(); | |
| 2102 } | 2098 } |
| 2103 | 2099 |
| 2104 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { | 2100 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { |
| 2105 if (m_Annots.GetSize() > 0) | 2101 return m_Annots.empty() ? nullptr : m_Annots.front(); |
| 2106 return m_Annots[0]; | |
| 2107 | |
| 2108 return NULL; | |
| 2109 } | 2102 } |
| 2110 | 2103 |
| 2111 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { | 2104 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { |
| 2112 if (m_Annots.GetSize() > 0) | 2105 return m_Annots.empty() ? nullptr : m_Annots.back(); |
| 2113 return m_Annots[m_Annots.GetSize() - 1]; | |
| 2114 | |
| 2115 return NULL; | |
| 2116 } | 2106 } |
| 2117 | 2107 |
| 2118 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { | 2108 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { |
| 2119 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) { | 2109 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); |
| 2120 if (m_Annots[i] == pAnnot) | 2110 if (iter == m_Annots.end()) |
| 2121 return (i + 1 < sz) ? m_Annots[i + 1] : m_Annots[0]; | 2111 return nullptr; |
| 2122 } | 2112 ++iter; |
| 2123 return NULL; | 2113 if (iter == m_Annots.end()) |
| 2114 iter = m_Annots.begin(); |
| 2115 return *iter; |
| 2124 } | 2116 } |
| 2125 | 2117 |
| 2126 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { | 2118 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { |
| 2127 for (int i = 0, sz = m_Annots.GetSize(); i < sz; ++i) { | 2119 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); |
| 2128 if (m_Annots[i] == pAnnot) | 2120 if (iter == m_Annots.end()) |
| 2129 return (i - 1 >= 0) ? m_Annots[i - 1] : m_Annots[sz - 1]; | 2121 return nullptr; |
| 2130 } | 2122 if (iter == m_Annots.begin()) |
| 2131 return NULL; | 2123 iter = m_Annots.end(); |
| 2124 return *(--iter); |
| 2132 } | 2125 } |
| 2133 | 2126 |
| 2134 int CBA_AnnotIterator::CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { | 2127 // static |
| 2135 ASSERT(p1); | 2128 bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1, |
| 2136 ASSERT(p2); | 2129 const CPDFSDK_Annot* p2) { |
| 2137 | 2130 return GetAnnotRect(p1).left < GetAnnotRect(p2).left; |
| 2138 CPDF_Rect rcAnnot1 = GetAnnotRect(p1); | |
| 2139 CPDF_Rect rcAnnot2 = GetAnnotRect(p2); | |
| 2140 | |
| 2141 if (rcAnnot1.left < rcAnnot2.left) | |
| 2142 return -1; | |
| 2143 if (rcAnnot1.left > rcAnnot2.left) | |
| 2144 return 1; | |
| 2145 return 0; | |
| 2146 } | 2131 } |
| 2147 | 2132 |
| 2148 int CBA_AnnotIterator::CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { | 2133 // static |
| 2149 ASSERT(p1); | 2134 bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1, |
| 2150 ASSERT(p2); | 2135 const CPDFSDK_Annot* p2) { |
| 2151 | 2136 return GetAnnotRect(p1).top > GetAnnotRect(p2).top; |
| 2152 CPDF_Rect rcAnnot1 = GetAnnotRect(p1); | |
| 2153 CPDF_Rect rcAnnot2 = GetAnnotRect(p2); | |
| 2154 | |
| 2155 if (rcAnnot1.top < rcAnnot2.top) | |
| 2156 return -1; | |
| 2157 if (rcAnnot1.top > rcAnnot2.top) | |
| 2158 return 1; | |
| 2159 return 0; | |
| 2160 } | 2137 } |
| 2161 | 2138 |
| 2162 void CBA_AnnotIterator::GenerateResults() { | 2139 void CBA_AnnotIterator::GenerateResults() { |
| 2163 switch (m_nTabs) { | 2140 switch (m_eTabOrder) { |
| 2164 case BAI_STRUCTURE: { | 2141 case STRUCTURE: { |
| 2165 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { | 2142 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 2166 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); | 2143 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
| 2167 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | 2144 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) |
| 2168 m_Annots.Add(pAnnot); | 2145 m_Annots.push_back(pAnnot); |
| 2169 } | 2146 } |
| 2170 break; | 2147 } break; |
| 2171 } | 2148 case ROW: { |
| 2172 case BAI_ROW: { | 2149 std::vector<CPDFSDK_Annot*> sa; |
| 2173 CPDFSDK_SortAnnots sa; | |
| 2174 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { | 2150 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 2175 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); | 2151 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
| 2176 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | 2152 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) |
| 2177 sa.Add(pAnnot); | 2153 sa.push_back(pAnnot); |
| 2178 } | 2154 } |
| 2179 | 2155 |
| 2180 if (sa.GetSize() > 0) | 2156 std::sort(sa.begin(), sa.end(), CompareByLeftAscending); |
| 2181 sa.Sort(CBA_AnnotIterator::CompareByLeft); | 2157 while (!sa.empty()) { |
| 2182 | |
| 2183 while (sa.GetSize() > 0) { | |
| 2184 int nLeftTopIndex = -1; | 2158 int nLeftTopIndex = -1; |
| 2185 FX_FLOAT fTop = 0.0f; | 2159 FX_FLOAT fTop = 0.0f; |
| 2186 | 2160 for (int i = sa.size() - 1; i >= 0; i--) { |
| 2187 for (int i = sa.GetSize() - 1; i >= 0; i--) { | 2161 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]); |
| 2188 CPDFSDK_Annot* pAnnot = sa.GetAt(i); | |
| 2189 ASSERT(pAnnot); | |
| 2190 | |
| 2191 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot); | |
| 2192 | |
| 2193 if (rcAnnot.top > fTop) { | 2162 if (rcAnnot.top > fTop) { |
| 2194 nLeftTopIndex = i; | 2163 nLeftTopIndex = i; |
| 2195 fTop = rcAnnot.top; | 2164 fTop = rcAnnot.top; |
| 2196 } | 2165 } |
| 2197 } | 2166 } |
| 2167 if (nLeftTopIndex >= 0) { |
| 2168 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; |
| 2169 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot); |
| 2170 m_Annots.push_back(pLeftTopAnnot); |
| 2171 sa.erase(sa.begin() + nLeftTopIndex); |
| 2198 | 2172 |
| 2199 if (nLeftTopIndex >= 0) { | 2173 std::vector<int> aSelect; |
| 2200 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex); | 2174 for (int i = 0; i < sa.size(); ++i) { |
| 2201 ASSERT(pLeftTopAnnot); | 2175 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]); |
| 2202 | |
| 2203 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot); | |
| 2204 | |
| 2205 m_Annots.Add(pLeftTopAnnot); | |
| 2206 sa.RemoveAt(nLeftTopIndex); | |
| 2207 | |
| 2208 CFX_ArrayTemplate<int> aSelect; | |
| 2209 | |
| 2210 for (int i = 0, sz = sa.GetSize(); i < sz; ++i) { | |
| 2211 CPDFSDK_Annot* pAnnot = sa.GetAt(i); | |
| 2212 ASSERT(pAnnot); | |
| 2213 | |
| 2214 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot); | |
| 2215 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; | 2176 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; |
| 2216 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) | 2177 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) |
| 2217 aSelect.Add(i); | 2178 aSelect.push_back(i); |
| 2218 } | 2179 } |
| 2180 for (int i = 0; i < aSelect.size(); ++i) |
| 2181 m_Annots.push_back(sa[aSelect[i]]); |
| 2219 | 2182 |
| 2220 for (int i = 0, sz = aSelect.GetSize(); i < sz; ++i) | 2183 for (int i = aSelect.size() - 1; i >= 0; --i) |
| 2221 m_Annots.Add(sa[aSelect[i]]); | 2184 sa.erase(sa.begin() + aSelect[i]); |
| 2222 | |
| 2223 for (int i = aSelect.GetSize() - 1; i >= 0; --i) | |
| 2224 sa.RemoveAt(aSelect[i]); | |
| 2225 | |
| 2226 aSelect.RemoveAll(); | |
| 2227 } | 2185 } |
| 2228 } | 2186 } |
| 2229 sa.RemoveAll(); | 2187 } break; |
| 2230 break; | 2188 case COLUMN: { |
| 2231 } | 2189 std::vector<CPDFSDK_Annot*> sa; |
| 2232 case BAI_COLUMN: { | |
| 2233 CPDFSDK_SortAnnots sa; | |
| 2234 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { | 2190 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 2235 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); | 2191 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
| 2236 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | 2192 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) |
| 2237 sa.Add(pAnnot); | 2193 sa.push_back(pAnnot); |
| 2238 } | 2194 } |
| 2239 | 2195 |
| 2240 if (sa.GetSize() > 0) | 2196 std::sort(sa.begin(), sa.end(), CompareByTopDescending); |
| 2241 sa.Sort(CBA_AnnotIterator::CompareByTop, FALSE); | 2197 while (!sa.empty()) { |
| 2242 | |
| 2243 while (sa.GetSize() > 0) { | |
| 2244 int nLeftTopIndex = -1; | 2198 int nLeftTopIndex = -1; |
| 2245 FX_FLOAT fLeft = -1.0f; | 2199 FX_FLOAT fLeft = -1.0f; |
| 2246 | 2200 for (int i = sa.size() - 1; i >= 0; --i) { |
| 2247 for (int i = sa.GetSize() - 1; i >= 0; --i) { | 2201 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]); |
| 2248 CPDFSDK_Annot* pAnnot = sa.GetAt(i); | |
| 2249 ASSERT(pAnnot); | |
| 2250 | |
| 2251 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot); | |
| 2252 | |
| 2253 if (fLeft < 0) { | 2202 if (fLeft < 0) { |
| 2254 nLeftTopIndex = 0; | 2203 nLeftTopIndex = 0; |
| 2255 fLeft = rcAnnot.left; | 2204 fLeft = rcAnnot.left; |
| 2256 } else if (rcAnnot.left < fLeft) { | 2205 } else if (rcAnnot.left < fLeft) { |
| 2257 nLeftTopIndex = i; | 2206 nLeftTopIndex = i; |
| 2258 fLeft = rcAnnot.left; | 2207 fLeft = rcAnnot.left; |
| 2259 } | 2208 } |
| 2260 } | 2209 } |
| 2261 | 2210 |
| 2262 if (nLeftTopIndex >= 0) { | 2211 if (nLeftTopIndex >= 0) { |
| 2263 CPDFSDK_Annot* pLeftTopAnnot = sa.GetAt(nLeftTopIndex); | 2212 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; |
| 2264 ASSERT(pLeftTopAnnot); | 2213 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot); |
| 2214 m_Annots.push_back(pLeftTopAnnot); |
| 2215 sa.erase(sa.begin() + nLeftTopIndex); |
| 2265 | 2216 |
| 2266 CPDF_Rect rcLeftTop = GetAnnotRect(pLeftTopAnnot); | 2217 std::vector<int> aSelect; |
| 2267 | 2218 for (int i = 0; i < sa.size(); ++i) { |
| 2268 m_Annots.Add(pLeftTopAnnot); | 2219 CPDF_Rect rcAnnot = GetAnnotRect(sa[i]); |
| 2269 sa.RemoveAt(nLeftTopIndex); | |
| 2270 | |
| 2271 CFX_ArrayTemplate<int> aSelect; | |
| 2272 for (int i = 0, sz = sa.GetSize(); i < sz; ++i) { | |
| 2273 CPDFSDK_Annot* pAnnot = sa.GetAt(i); | |
| 2274 ASSERT(pAnnot); | |
| 2275 | |
| 2276 CPDF_Rect rcAnnot = GetAnnotRect(pAnnot); | |
| 2277 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; | 2220 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; |
| 2278 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) | 2221 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) |
| 2279 aSelect.Add(i); | 2222 aSelect.push_back(i); |
| 2280 } | 2223 } |
| 2224 for (int i = 0; i < aSelect.size(); ++i) |
| 2225 m_Annots.push_back(sa[aSelect[i]]); |
| 2281 | 2226 |
| 2282 for (int i = 0, sz = aSelect.GetSize(); i < sz; ++i) | 2227 for (int i = aSelect.size() - 1; i >= 0; --i) |
| 2283 m_Annots.Add(sa[aSelect[i]]); | 2228 sa.erase(sa.begin() + aSelect[i]); |
| 2284 | |
| 2285 for (int i = aSelect.GetSize() - 1; i >= 0; --i) | |
| 2286 sa.RemoveAt(aSelect[i]); | |
| 2287 | |
| 2288 aSelect.RemoveAll(); | |
| 2289 } | 2229 } |
| 2290 } | 2230 } |
| 2291 sa.RemoveAll(); | |
| 2292 break; | 2231 break; |
| 2293 } | 2232 } |
| 2294 } | 2233 } |
| 2295 } | 2234 } |
| 2296 | 2235 |
| 2297 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(CPDFSDK_Annot* pAnnot) { | 2236 CPDF_Rect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) { |
| 2298 CPDF_Rect rcAnnot; | 2237 CPDF_Rect rcAnnot; |
| 2299 pAnnot->GetPDFAnnot()->GetRect(rcAnnot); | 2238 pAnnot->GetPDFAnnot()->GetRect(rcAnnot); |
| 2300 return rcAnnot; | 2239 return rcAnnot; |
| 2301 } | 2240 } |
| OLD | NEW |