Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_textpagefind.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "core/fpdftext/cpdf_textpagefind.h"
8
9#include <wchar.h>
10
11#include <vector>
12
13#include "core/fpdftext/cpdf_textpage.h"
14#include "core/fxcrt/fx_extension.h"
15#include "core/fxcrt/fx_string.h"
16#include "core/fxcrt/fx_system.h"
17#include "core/fxcrt/fx_unicode.h"
18#include "core/fxcrt/stl_util.h"
19#include "third_party/base/check.h"
20#include "third_party/base/memory/ptr_util.h"
21
22namespace {
23
24constexpr wchar_t kNonBreakingSpace = 160;
25
26bool IsIgnoreSpaceCharacter(wchar_t curChar) {
27 if (curChar < 255 || (curChar >= 0x0600 && curChar <= 0x06FF) ||
28 (curChar >= 0xFE70 && curChar <= 0xFEFF) ||
29 (curChar >= 0xFB50 && curChar <= 0xFDFF) ||
30 (curChar >= 0x0400 && curChar <= 0x04FF) ||
31 (curChar >= 0x0500 && curChar <= 0x052F) ||
32 (curChar >= 0xA640 && curChar <= 0xA69F) ||
33 (curChar >= 0x2DE0 && curChar <= 0x2DFF) || curChar == 8467 ||
34 (curChar >= 0x2000 && curChar <= 0x206F)) {
35 return false;
36 }
37 return true;
38}
39
40bool IsMatchWholeWord(const WideString& csPageText,
41 size_t startPos,
42 size_t endPos) {
43 if (startPos > endPos)
44 return false;
45 wchar_t char_left = 0;
46 wchar_t char_right = 0;
47 size_t char_count = endPos - startPos + 1;
48 if (char_count == 0)
49 return false;
50 if (char_count == 1 && csPageText[startPos] > 255)
51 return true;
52 if (startPos >= 1)
53 char_left = csPageText[startPos - 1];
54 if (startPos + char_count < csPageText.GetLength())
55 char_right = csPageText[startPos + char_count];
56 if ((char_left > 'A' && char_left < 'a') ||
57 (char_left > 'a' && char_left < 'z') ||
58 (char_left > 0xfb00 && char_left < 0xfb06) ||
59 FXSYS_IsDecimalDigit(char_left) ||
60 (char_right > 'A' && char_right < 'a') ||
61 (char_right > 'a' && char_right < 'z') ||
62 (char_right > 0xfb00 && char_right < 0xfb06) ||
63 FXSYS_IsDecimalDigit(char_right)) {
64 return false;
65 }
66 if (!(('A' > char_left || char_left > 'Z') &&
67 ('a' > char_left || char_left > 'z') &&
68 ('A' > char_right || char_right > 'Z') &&
69 ('a' > char_right || char_right > 'z'))) {
70 return false;
71 }
72 if (char_count > 0) {
73 if (FXSYS_IsDecimalDigit(char_left) &&
74 FXSYS_IsDecimalDigit(csPageText[startPos])) {
75 return false;
76 }
77 if (FXSYS_IsDecimalDigit(char_right) &&
78 FXSYS_IsDecimalDigit(csPageText[endPos])) {
79 return false;
80 }
81 }
82 return true;
83}
84
85WideString GetStringCase(const WideString& wsOriginal, bool bMatchCase) {
86 if (bMatchCase)
87 return wsOriginal;
88
89 WideString wsLower = wsOriginal;
90 wsLower.MakeLower();
91 return wsLower;
92}
93
94absl::optional<WideString> ExtractSubString(const wchar_t* lpszFullString,
95 int iSubString) {
96 DCHECK(lpszFullString);
97
98 while (iSubString--) {
99 lpszFullString = wcschr(lpszFullString, L' ');
100 if (!lpszFullString)
101 return absl::nullopt;
102
103 lpszFullString++;
104 while (*lpszFullString == L' ')
105 lpszFullString++;
106 }
107
108 const wchar_t* lpchEnd = wcschr(lpszFullString, L' ');
109 int nLen = lpchEnd ? static_cast<int>(lpchEnd - lpszFullString)
110 : static_cast<int>(wcslen(lpszFullString));
111 if (nLen < 0)
112 return absl::nullopt;
113
114 return WideString(lpszFullString, static_cast<size_t>(nLen));
115}
116
117std::vector<WideString> ExtractFindWhat(const WideString& findwhat) {
118 std::vector<WideString> findwhat_array;
119
120 size_t len = findwhat.GetLength();
121 size_t i = 0;
122 for (i = 0; i < len; ++i)
123 if (findwhat[i] != ' ')
124 break;
125 if (i == len) {
126 findwhat_array.push_back(findwhat);
127 return findwhat_array;
128 }
129
130 int index = 0;
131 while (true) {
132 absl::optional<WideString> word = ExtractSubString(findwhat.c_str(), index);
133 if (!word.has_value())
134 break;
135
136 if (word->IsEmpty()) {
137 findwhat_array.push_back(L"");
138 index++;
139 continue;
140 }
141
142 size_t pos = 0;
143 while (pos < word->GetLength()) {
144 WideString curStr = word->Substr(pos, 1);
145 wchar_t curChar = word.value()[pos];
146 if (IsIgnoreSpaceCharacter(curChar)) {
147 if (pos > 0 && curChar == pdfium::unicode::kRightSingleQuotationMark) {
148 pos++;
149 continue;
150 }
151 if (pos > 0)
152 findwhat_array.push_back(word->First(pos));
153 findwhat_array.push_back(curStr);
154 if (pos == word->GetLength() - 1) {
155 word->clear();
156 break;
157 }
158 word.emplace(word->Last(word->GetLength() - pos - 1));
159 pos = 0;
160 continue;
161 }
162 pos++;
163 }
164
165 if (!word->IsEmpty())
166 findwhat_array.push_back(word.value());
167 index++;
168 }
169 return findwhat_array;
170}
171
172} // namespace
173
174// static
175std::unique_ptr<CPDF_TextPageFind> CPDF_TextPageFind::Create(
176 const CPDF_TextPage* pTextPage,
177 const WideString& findwhat,
178 const Options& options,
179 absl::optional<size_t> startPos) {
180 std::vector<WideString> findwhat_array =
181 ExtractFindWhat(GetStringCase(findwhat, options.bMatchCase));
182 auto find = pdfium::WrapUnique(
183 new CPDF_TextPageFind(pTextPage, findwhat_array, options, startPos));
184 find->FindFirst();
185 return find;
186}
187
188CPDF_TextPageFind::CPDF_TextPageFind(
189 const CPDF_TextPage* pTextPage,
190 const std::vector<WideString>& findwhat_array,
191 const Options& options,
192 absl::optional<size_t> startPos)
193 : m_pTextPage(pTextPage),
194 m_strText(GetStringCase(pTextPage->GetAllPageText(), options.bMatchCase)),
195 m_csFindWhatArray(findwhat_array),
196 m_options(options) {
197 if (!m_strText.IsEmpty()) {
198 m_findNextStart = startPos;
199 m_findPreStart = startPos.value_or(m_strText.GetLength() - 1);
200 }
201}
202
204
205int CPDF_TextPageFind::GetCharIndex(int index) const {
206 return m_pTextPage->CharIndexFromTextIndex(index);
207}
208
209bool CPDF_TextPageFind::FindFirst() {
210 return m_strText.IsEmpty() || !m_csFindWhatArray.empty();
211}
212
214 if (m_strText.IsEmpty() || !m_findNextStart.has_value())
215 return false;
216
217 const size_t strLen = m_strText.GetLength();
218 size_t nStartPos = m_findNextStart.value();
219 if (nStartPos >= strLen) {
220 return false;
221 }
222
223 int nCount = fxcrt::CollectionSize<int>(m_csFindWhatArray);
224 absl::optional<size_t> nResultPos = 0;
225 bool bSpaceStart = false;
226 for (int iWord = 0; iWord < nCount; iWord++) {
227 WideString csWord = m_csFindWhatArray[iWord];
228 if (csWord.IsEmpty()) {
229 if (iWord == nCount - 1) {
230 if (nStartPos >= strLen) {
231 return false;
232 }
233 wchar_t strInsert = m_strText[nStartPos];
234 if (strInsert == L'\n' || strInsert == L' ' || strInsert == L'\r' ||
235 strInsert == kNonBreakingSpace) {
236 nResultPos = nStartPos + 1;
237 break;
238 }
239 iWord = -1;
240 } else if (iWord == 0) {
241 bSpaceStart = true;
242 }
243 continue;
244 }
245 nResultPos = m_strText.Find(csWord.AsStringView(), nStartPos);
246 if (!nResultPos.has_value())
247 return false;
248
249 size_t endIndex = nResultPos.value() + csWord.GetLength() - 1;
250 if (iWord == 0)
251 m_resStart = nResultPos.value();
252 bool bMatch = true;
253 if (iWord != 0 && !bSpaceStart) {
254 size_t PreResEndPos = nStartPos;
255 int curChar = csWord[0];
256 WideString lastWord = m_csFindWhatArray[iWord - 1];
257 int lastChar = lastWord.Back();
258 if (nStartPos == nResultPos.value() &&
259 !(IsIgnoreSpaceCharacter(lastChar) ||
260 IsIgnoreSpaceCharacter(curChar))) {
261 bMatch = false;
262 }
263 for (size_t d = PreResEndPos; d < nResultPos.value(); d++) {
264 wchar_t strInsert = m_strText[d];
265 if (strInsert != L'\n' && strInsert != L' ' && strInsert != L'\r' &&
266 strInsert != kNonBreakingSpace) {
267 bMatch = false;
268 break;
269 }
270 }
271 } else if (bSpaceStart) {
272 if (nResultPos.value() > 0) {
273 wchar_t strInsert = m_strText[nResultPos.value() - 1];
274 if (strInsert != L'\n' && strInsert != L' ' && strInsert != L'\r' &&
275 strInsert != kNonBreakingSpace) {
276 bMatch = false;
277 m_resStart = nResultPos.value();
278 } else {
279 m_resStart = nResultPos.value() - 1;
280 }
281 }
282 }
283 if (m_options.bMatchWholeWord && bMatch)
284 bMatch = IsMatchWholeWord(m_strText, nResultPos.value(), endIndex);
285
286 if (bMatch) {
287 nStartPos = endIndex + 1;
288 } else {
289 iWord = -1;
290 size_t index = bSpaceStart ? 1 : 0;
291 nStartPos = m_resStart + m_csFindWhatArray[index].GetLength();
292 }
293 }
294 m_resEnd = nResultPos.value() + m_csFindWhatArray.back().GetLength() - 1;
295 if (m_options.bConsecutive) {
296 m_findNextStart = m_resStart + 1;
297 m_findPreStart = m_resEnd - 1;
298 } else {
299 m_findNextStart = m_resEnd + 1;
300 m_findPreStart = m_resStart - 1;
301 }
302 return true;
303}
304
306 if (m_strText.IsEmpty() || !m_findPreStart.has_value())
307 return false;
308
309 CPDF_TextPageFind find_engine(m_pTextPage, m_csFindWhatArray, m_options, 0);
310 if (!find_engine.FindFirst())
311 return false;
312
313 int order = -1;
314 int matches = 0;
315 while (find_engine.FindNext()) {
316 int cur_order = find_engine.GetCurOrder();
317 int cur_match = find_engine.GetMatchedCount();
318 int temp = cur_order + cur_match;
319 if (temp < 0 || static_cast<size_t>(temp) > m_findPreStart.value() + 1)
320 break;
321
322 order = cur_order;
323 matches = cur_match;
324 }
325 if (order == -1)
326 return false;
327
328 m_resStart = m_pTextPage->TextIndexFromCharIndex(order);
329 m_resEnd = m_pTextPage->TextIndexFromCharIndex(order + matches - 1);
330 if (m_options.bConsecutive) {
331 m_findNextStart = m_resStart + 1;
332 m_findPreStart = m_resEnd - 1;
333 } else {
334 m_findNextStart = m_resEnd + 1;
335 m_findPreStart = m_resStart - 1;
336 }
337 return true;
338}
339
341 return GetCharIndex(m_resStart);
342}
343
345 int resStart = GetCharIndex(m_resStart);
346 int resEnd = GetCharIndex(m_resEnd);
347 return resEnd - resStart + 1;
348}
CharType operator[](const size_t index) const
Definition widestring.h:146
bool IsEmpty() const
Definition widestring.h:118
CharType Back() const
Definition widestring.h:152
bool FXSYS_IsDecimalDigit(wchar_t c)
constexpr wchar_t kRightSingleQuotationMark
Definition fx_unicode.h:99