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

Side by Side Diff: core/fpdftext/fpdf_text_int.cpp

Issue 2286723003: Split fpdf_text_int into classes (Closed)
Patch Set: Fix bots 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 | « core/fpdftext/cpdf_textpagefind.cpp ('k') | core/fpdftext/include/cpdf_textpage.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <algorithm>
8 #include <cctype>
9 #include <cwctype>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13
14 #include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
15 #include "core/fpdfapi/fpdf_page/include/cpdf_form.h"
16 #include "core/fpdfapi/fpdf_page/include/cpdf_formobject.h"
17 #include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
18 #include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
19 #include "core/fpdfapi/fpdf_page/include/cpdf_textobject.h"
20 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
21 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
22 #include "core/fpdftext/include/cpdf_linkextract.h"
23 #include "core/fpdftext/include/cpdf_textpage.h"
24 #include "core/fpdftext/include/cpdf_textpagefind.h"
25 #include "core/fpdftext/unicodenormalizationdata.h"
26 #include "core/fxcrt/fx_bidi.h"
27 #include "core/fxcrt/include/fx_ext.h"
28 #include "core/fxcrt/include/fx_ucd.h"
29 #include "third_party/base/stl_util.h"
30
31 #define FPDFTEXT_MATCHCASE 0x00000001
32 #define FPDFTEXT_MATCHWHOLEWORD 0x00000002
33 #define FPDFTEXT_CONSECUTIVE 0x00000004
34
35 #define FPDFTEXT_CHAR_ERROR -1
36 #define FPDFTEXT_CHAR_NORMAL 0
37 #define FPDFTEXT_CHAR_GENERATED 1
38 #define FPDFTEXT_CHAR_UNUNICODE 2
39 #define FPDFTEXT_CHAR_HYPHEN 3
40 #define FPDFTEXT_CHAR_PIECE 4
41
42 #define TEXT_SPACE_CHAR L' '
43 #define TEXT_LINEFEED_CHAR L'\n'
44 #define TEXT_RETURN_CHAR L'\r'
45 #define TEXT_EMPTY L""
46 #define TEXT_SPACE L" "
47 #define TEXT_RETURN_LINEFEED L"\r\n"
48 #define TEXT_LINEFEED L"\n"
49 #define TEXT_CHARRATIO_GAPDELTA 0.070
50
51 namespace {
52
53 const FX_FLOAT kDefaultFontSize = 1.0f;
54 const uint16_t* const g_UnicodeData_Normalization_Maps[5] = {
55 nullptr, g_UnicodeData_Normalization_Map1, g_UnicodeData_Normalization_Map2,
56 g_UnicodeData_Normalization_Map3, g_UnicodeData_Normalization_Map4};
57
58 FX_BOOL IsIgnoreSpaceCharacter(FX_WCHAR curChar) {
59 if (curChar < 255)
60 return FALSE;
61 if ((curChar >= 0x0600 && curChar <= 0x06FF) ||
62 (curChar >= 0xFE70 && curChar <= 0xFEFF) ||
63 (curChar >= 0xFB50 && curChar <= 0xFDFF) ||
64 (curChar >= 0x0400 && curChar <= 0x04FF) ||
65 (curChar >= 0x0500 && curChar <= 0x052F) ||
66 (curChar >= 0xA640 && curChar <= 0xA69F) ||
67 (curChar >= 0x2DE0 && curChar <= 0x2DFF) || curChar == 8467 ||
68 (curChar >= 0x2000 && curChar <= 0x206F)) {
69 return FALSE;
70 }
71 return TRUE;
72 }
73
74 FX_FLOAT NormalizeThreshold(FX_FLOAT threshold) {
75 if (threshold < 300)
76 return threshold / 2.0f;
77 if (threshold < 500)
78 return threshold / 4.0f;
79 if (threshold < 700)
80 return threshold / 5.0f;
81 return threshold / 6.0f;
82 }
83
84 FX_FLOAT CalculateBaseSpace(const CPDF_TextObject* pTextObj,
85 const CFX_Matrix& matrix) {
86 FX_FLOAT baseSpace = 0.0;
87 const int nItems = pTextObj->CountItems();
88 if (pTextObj->m_TextState.GetObject()->m_CharSpace && nItems >= 3) {
89 bool bAllChar = true;
90 FX_FLOAT spacing = matrix.TransformDistance(
91 pTextObj->m_TextState.GetObject()->m_CharSpace);
92 baseSpace = spacing;
93 for (int i = 0; i < nItems; i++) {
94 CPDF_TextObjectItem item;
95 pTextObj->GetItemInfo(i, &item);
96 if (item.m_CharCode == static_cast<uint32_t>(-1)) {
97 FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
98 FX_FLOAT kerning = -fontsize_h * item.m_OriginX / 1000;
99 baseSpace = std::min(baseSpace, kerning + spacing);
100 bAllChar = false;
101 }
102 }
103 if (baseSpace < 0.0 || (nItems == 3 && !bAllChar))
104 baseSpace = 0.0;
105 }
106 return baseSpace;
107 }
108
109 FX_STRSIZE Unicode_GetNormalization(FX_WCHAR wch, FX_WCHAR* pDst) {
110 wch = wch & 0xFFFF;
111 FX_WCHAR wFind = g_UnicodeData_Normalization[wch];
112 if (!wFind) {
113 if (pDst)
114 *pDst = wch;
115 return 1;
116 }
117 if (wFind >= 0x8000) {
118 wch = wFind - 0x8000;
119 wFind = 1;
120 } else {
121 wch = wFind & 0x0FFF;
122 wFind >>= 12;
123 }
124 const uint16_t* pMap = g_UnicodeData_Normalization_Maps[wFind];
125 if (pMap == g_UnicodeData_Normalization_Map4) {
126 pMap = g_UnicodeData_Normalization_Map4 + wch;
127 wFind = (FX_WCHAR)(*pMap++);
128 } else {
129 pMap += wch;
130 }
131 if (pDst) {
132 FX_WCHAR n = wFind;
133 while (n--)
134 *pDst++ = *pMap++;
135 }
136 return (FX_STRSIZE)wFind;
137 }
138
139 float MaskPercentFilled(const std::vector<bool>& mask,
140 int32_t start,
141 int32_t end) {
142 if (start >= end)
143 return 0;
144 float count = std::count_if(mask.begin() + start, mask.begin() + end,
145 [](bool r) { return r; });
146 return count / (end - start);
147 }
148
149 } // namespace
150
151 CPDF_TextPage::CPDF_TextPage(const CPDF_Page* pPage, FPDFText_Direction flags)
152 : m_pPage(pPage),
153 m_parserflag(flags),
154 m_pPreTextObj(nullptr),
155 m_bIsParsed(false),
156 m_TextlineDir(TextOrientation::Unknown) {
157 m_TextBuf.EstimateSize(0, 10240);
158 pPage->GetDisplayMatrix(m_DisplayMatrix, 0, 0, (int)pPage->GetPageWidth(),
159 (int)pPage->GetPageHeight(), 0);
160 }
161
162 CPDF_TextPage::~CPDF_TextPage() {}
163
164 bool CPDF_TextPage::IsControlChar(const PAGECHAR_INFO& charInfo) {
165 switch (charInfo.m_Unicode) {
166 case 0x2:
167 case 0x3:
168 case 0x93:
169 case 0x94:
170 case 0x96:
171 case 0x97:
172 case 0x98:
173 case 0xfffe:
174 return charInfo.m_Flag != FPDFTEXT_CHAR_HYPHEN;
175 default:
176 return false;
177 }
178 }
179
180 void CPDF_TextPage::ParseTextPage() {
181 m_bIsParsed = false;
182 m_TextBuf.Clear();
183 m_CharList.clear();
184 m_pPreTextObj = nullptr;
185 ProcessObject();
186
187 m_bIsParsed = true;
188 m_CharIndex.clear();
189 int nCount = pdfium::CollectionSize<int>(m_CharList);
190 if (nCount)
191 m_CharIndex.push_back(0);
192
193 for (int i = 0; i < nCount; i++) {
194 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
195 const PAGECHAR_INFO& charinfo = m_CharList[i];
196 if (charinfo.m_Flag == FPDFTEXT_CHAR_GENERATED ||
197 (charinfo.m_Unicode != 0 && !IsControlChar(charinfo))) {
198 if (indexSize % 2) {
199 m_CharIndex.push_back(1);
200 } else {
201 if (indexSize <= 0)
202 continue;
203 m_CharIndex[indexSize - 1] += 1;
204 }
205 } else {
206 if (indexSize % 2) {
207 if (indexSize <= 0)
208 continue;
209 m_CharIndex[indexSize - 1] = i + 1;
210 } else {
211 m_CharIndex.push_back(i + 1);
212 }
213 }
214 }
215 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
216 if (indexSize % 2)
217 m_CharIndex.erase(m_CharIndex.begin() + indexSize - 1);
218 }
219
220 int CPDF_TextPage::CountChars() const {
221 return pdfium::CollectionSize<int>(m_CharList);
222 }
223
224 int CPDF_TextPage::CharIndexFromTextIndex(int TextIndex) const {
225 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
226 int count = 0;
227 for (int i = 0; i < indexSize; i += 2) {
228 count += m_CharIndex[i + 1];
229 if (count > TextIndex)
230 return TextIndex - count + m_CharIndex[i + 1] + m_CharIndex[i];
231 }
232 return -1;
233 }
234
235 int CPDF_TextPage::TextIndexFromCharIndex(int CharIndex) const {
236 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
237 int count = 0;
238 for (int i = 0; i < indexSize; i += 2) {
239 count += m_CharIndex[i + 1];
240 if (m_CharIndex[i + 1] + m_CharIndex[i] > CharIndex) {
241 if (CharIndex - m_CharIndex[i] < 0)
242 return -1;
243
244 return CharIndex - m_CharIndex[i] + count - m_CharIndex[i + 1];
245 }
246 }
247 return -1;
248 }
249
250 std::vector<CFX_FloatRect> CPDF_TextPage::GetRectArray(int start,
251 int nCount) const {
252 if (start < 0 || nCount == 0 || !m_bIsParsed)
253 return std::vector<CFX_FloatRect>();
254
255 if (nCount + start > pdfium::CollectionSize<int>(m_CharList) ||
256 nCount == -1) {
257 nCount = pdfium::CollectionSize<int>(m_CharList) - start;
258 }
259
260 std::vector<CFX_FloatRect> rectArray;
261 CPDF_TextObject* pCurObj = nullptr;
262 CFX_FloatRect rect;
263 int curPos = start;
264 bool bFlagNewRect = true;
265 while (nCount--) {
266 PAGECHAR_INFO info_curchar = m_CharList[curPos++];
267 if (info_curchar.m_Flag == FPDFTEXT_CHAR_GENERATED)
268 continue;
269 if (info_curchar.m_CharBox.Width() < 0.01 ||
270 info_curchar.m_CharBox.Height() < 0.01) {
271 continue;
272 }
273 if (!pCurObj)
274 pCurObj = info_curchar.m_pTextObj;
275 if (pCurObj != info_curchar.m_pTextObj) {
276 rectArray.push_back(rect);
277 pCurObj = info_curchar.m_pTextObj;
278 bFlagNewRect = true;
279 }
280 if (bFlagNewRect) {
281 FX_FLOAT orgX = info_curchar.m_OriginX, orgY = info_curchar.m_OriginY;
282 CFX_Matrix matrix, matrix_reverse;
283 info_curchar.m_pTextObj->GetTextMatrix(&matrix);
284 matrix.Concat(info_curchar.m_Matrix);
285 matrix_reverse.SetReverse(matrix);
286 matrix_reverse.Transform(orgX, orgY);
287 rect.left = info_curchar.m_CharBox.left;
288 rect.right = info_curchar.m_CharBox.right;
289 if (pCurObj->GetFont()->GetTypeDescent()) {
290 rect.bottom = orgY +
291 pCurObj->GetFont()->GetTypeDescent() *
292 pCurObj->GetFontSize() / 1000;
293 FX_FLOAT xPosTemp = orgX;
294 matrix.Transform(xPosTemp, rect.bottom);
295 } else {
296 rect.bottom = info_curchar.m_CharBox.bottom;
297 }
298 if (pCurObj->GetFont()->GetTypeAscent()) {
299 rect.top =
300 orgY +
301 pCurObj->GetFont()->GetTypeAscent() * pCurObj->GetFontSize() / 1000;
302 FX_FLOAT xPosTemp =
303 orgX +
304 GetCharWidth(info_curchar.m_CharCode, pCurObj->GetFont()) *
305 pCurObj->GetFontSize() / 1000;
306 matrix.Transform(xPosTemp, rect.top);
307 } else {
308 rect.top = info_curchar.m_CharBox.top;
309 }
310 bFlagNewRect = false;
311 rect = info_curchar.m_CharBox;
312 rect.Normalize();
313 } else {
314 info_curchar.m_CharBox.Normalize();
315 rect.left = std::min(rect.left, info_curchar.m_CharBox.left);
316 rect.right = std::max(rect.right, info_curchar.m_CharBox.right);
317 rect.top = std::max(rect.top, info_curchar.m_CharBox.top);
318 rect.bottom = std::min(rect.bottom, info_curchar.m_CharBox.bottom);
319 }
320 }
321 rectArray.push_back(rect);
322 return rectArray;
323 }
324
325 int CPDF_TextPage::GetIndexAtPos(CFX_FloatPoint point,
326 FX_FLOAT xTolerance,
327 FX_FLOAT yTolerance) const {
328 if (!m_bIsParsed)
329 return -3;
330
331 int pos = 0;
332 int NearPos = -1;
333 double xdif = 5000;
334 double ydif = 5000;
335 while (pos < pdfium::CollectionSize<int>(m_CharList)) {
336 PAGECHAR_INFO charinfo = m_CharList[pos];
337 CFX_FloatRect charrect = charinfo.m_CharBox;
338 if (charrect.Contains(point.x, point.y))
339 break;
340 if (xTolerance > 0 || yTolerance > 0) {
341 CFX_FloatRect charRectExt;
342 charrect.Normalize();
343 charRectExt.left = charrect.left - xTolerance / 2;
344 charRectExt.right = charrect.right + xTolerance / 2;
345 charRectExt.top = charrect.top + yTolerance / 2;
346 charRectExt.bottom = charrect.bottom - yTolerance / 2;
347 if (charRectExt.Contains(point.x, point.y)) {
348 double curXdif, curYdif;
349 curXdif = FXSYS_fabs(point.x - charrect.left) <
350 FXSYS_fabs(point.x - charrect.right)
351 ? FXSYS_fabs(point.x - charrect.left)
352 : FXSYS_fabs(point.x - charrect.right);
353 curYdif = FXSYS_fabs(point.y - charrect.bottom) <
354 FXSYS_fabs(point.y - charrect.top)
355 ? FXSYS_fabs(point.y - charrect.bottom)
356 : FXSYS_fabs(point.y - charrect.top);
357 if (curYdif + curXdif < xdif + ydif) {
358 ydif = curYdif;
359 xdif = curXdif;
360 NearPos = pos;
361 }
362 }
363 }
364 ++pos;
365 }
366 return pos < pdfium::CollectionSize<int>(m_CharList) ? pos : NearPos;
367 }
368
369 CFX_WideString CPDF_TextPage::GetTextByRect(const CFX_FloatRect& rect) const {
370 if (!m_bIsParsed)
371 return CFX_WideString();
372
373 FX_FLOAT posy = 0;
374 bool IsContainPreChar = false;
375 bool IsAddLineFeed = false;
376 CFX_WideString strText;
377 for (const auto& charinfo : m_CharList) {
378 if (IsRectIntersect(rect, charinfo.m_CharBox)) {
379 if (FXSYS_fabs(posy - charinfo.m_OriginY) > 0 && !IsContainPreChar &&
380 IsAddLineFeed) {
381 posy = charinfo.m_OriginY;
382 if (!strText.IsEmpty())
383 strText += L"\r\n";
384 }
385 IsContainPreChar = true;
386 IsAddLineFeed = false;
387 if (charinfo.m_Unicode)
388 strText += charinfo.m_Unicode;
389 } else if (charinfo.m_Unicode == 32) {
390 if (IsContainPreChar && charinfo.m_Unicode) {
391 strText += charinfo.m_Unicode;
392 IsContainPreChar = false;
393 IsAddLineFeed = false;
394 }
395 } else {
396 IsContainPreChar = false;
397 IsAddLineFeed = true;
398 }
399 }
400 return strText;
401 }
402
403 int CPDF_TextPage::GetIndexAtPos(FX_FLOAT x,
404 FX_FLOAT y,
405 FX_FLOAT xTolerance,
406 FX_FLOAT yTolerance) const {
407 CFX_FloatPoint point(x, y);
408 return GetIndexAtPos(point, xTolerance, yTolerance);
409 }
410
411 void CPDF_TextPage::GetCharInfo(int index, FPDF_CHAR_INFO* info) const {
412 if (!m_bIsParsed)
413 return;
414
415 if (index < 0 || index >= pdfium::CollectionSize<int>(m_CharList))
416 return;
417
418 const PAGECHAR_INFO& charinfo = m_CharList[index];
419 info->m_Charcode = charinfo.m_CharCode;
420 info->m_OriginX = charinfo.m_OriginX;
421 info->m_OriginY = charinfo.m_OriginY;
422 info->m_Unicode = charinfo.m_Unicode;
423 info->m_Flag = charinfo.m_Flag;
424 info->m_CharBox = charinfo.m_CharBox;
425 info->m_pTextObj = charinfo.m_pTextObj;
426 if (charinfo.m_pTextObj && charinfo.m_pTextObj->GetFont())
427 info->m_FontSize = charinfo.m_pTextObj->GetFontSize();
428 else
429 info->m_FontSize = kDefaultFontSize;
430 info->m_Matrix.Copy(charinfo.m_Matrix);
431 }
432
433 void CPDF_TextPage::CheckMarkedContentObject(int32_t& start,
434 int32_t& nCount) const {
435 PAGECHAR_INFO charinfo = m_CharList[start];
436 PAGECHAR_INFO charinfo2 = m_CharList[start + nCount - 1];
437 if (FPDFTEXT_CHAR_PIECE != charinfo.m_Flag &&
438 FPDFTEXT_CHAR_PIECE != charinfo2.m_Flag) {
439 return;
440 }
441 if (FPDFTEXT_CHAR_PIECE == charinfo.m_Flag) {
442 PAGECHAR_INFO charinfo1 = charinfo;
443 int startIndex = start;
444 while (FPDFTEXT_CHAR_PIECE == charinfo1.m_Flag &&
445 charinfo1.m_Index == charinfo.m_Index) {
446 startIndex--;
447 if (startIndex < 0)
448 break;
449 charinfo1 = m_CharList[startIndex];
450 }
451 startIndex++;
452 start = startIndex;
453 }
454 if (FPDFTEXT_CHAR_PIECE == charinfo2.m_Flag) {
455 PAGECHAR_INFO charinfo3 = charinfo2;
456 int endIndex = start + nCount - 1;
457 while (FPDFTEXT_CHAR_PIECE == charinfo3.m_Flag &&
458 charinfo3.m_Index == charinfo2.m_Index) {
459 endIndex++;
460 if (endIndex >= pdfium::CollectionSize<int>(m_CharList))
461 break;
462 charinfo3 = m_CharList[endIndex];
463 }
464 endIndex--;
465 nCount = endIndex - start + 1;
466 }
467 }
468
469 CFX_WideString CPDF_TextPage::GetPageText(int start, int nCount) const {
470 if (!m_bIsParsed || nCount == 0)
471 return L"";
472
473 if (start < 0)
474 start = 0;
475
476 if (nCount == -1) {
477 nCount = pdfium::CollectionSize<int>(m_CharList) - start;
478 return CFX_WideString(
479 m_TextBuf.AsStringC().Mid(start, m_TextBuf.AsStringC().GetLength()));
480 }
481 if (nCount <= 0 || m_CharList.empty())
482 return L"";
483 if (nCount + start > pdfium::CollectionSize<int>(m_CharList) - 1)
484 nCount = pdfium::CollectionSize<int>(m_CharList) - start;
485 if (nCount <= 0)
486 return L"";
487 CheckMarkedContentObject(start, nCount);
488 int startindex = 0;
489 PAGECHAR_INFO charinfo = m_CharList[start];
490 int startOffset = 0;
491 while (charinfo.m_Index == -1) {
492 startOffset++;
493 if (startOffset > nCount ||
494 start + startOffset >= pdfium::CollectionSize<int>(m_CharList)) {
495 return L"";
496 }
497 charinfo = m_CharList[start + startOffset];
498 }
499 startindex = charinfo.m_Index;
500 charinfo = m_CharList[start + nCount - 1];
501 int nCountOffset = 0;
502 while (charinfo.m_Index == -1) {
503 nCountOffset++;
504 if (nCountOffset >= nCount)
505 return L"";
506 charinfo = m_CharList[start + nCount - nCountOffset - 1];
507 }
508 nCount = start + nCount - nCountOffset - startindex;
509 if (nCount <= 0)
510 return L"";
511 return CFX_WideString(m_TextBuf.AsStringC().Mid(startindex, nCount));
512 }
513
514 int CPDF_TextPage::CountRects(int start, int nCount) {
515 if (!m_bIsParsed || start < 0)
516 return -1;
517
518 if (nCount == -1 ||
519 nCount + start > pdfium::CollectionSize<int>(m_CharList)) {
520 nCount = pdfium::CollectionSize<int>(m_CharList) - start;
521 }
522 m_SelRects = GetRectArray(start, nCount);
523 return pdfium::CollectionSize<int>(m_SelRects);
524 }
525
526 void CPDF_TextPage::GetRect(int rectIndex,
527 FX_FLOAT& left,
528 FX_FLOAT& top,
529 FX_FLOAT& right,
530 FX_FLOAT& bottom) const {
531 if (!m_bIsParsed)
532 return;
533
534 if (rectIndex < 0 || rectIndex >= pdfium::CollectionSize<int>(m_SelRects))
535 return;
536
537 left = m_SelRects[rectIndex].left;
538 top = m_SelRects[rectIndex].top;
539 right = m_SelRects[rectIndex].right;
540 bottom = m_SelRects[rectIndex].bottom;
541 }
542
543 CPDF_TextPage::TextOrientation CPDF_TextPage::FindTextlineFlowOrientation()
544 const {
545 if (m_pPage->GetPageObjectList()->empty())
546 return TextOrientation::Unknown;
547
548 const int32_t nPageWidth = static_cast<int32_t>(m_pPage->GetPageWidth());
549 const int32_t nPageHeight = static_cast<int32_t>(m_pPage->GetPageHeight());
550 std::vector<bool> nHorizontalMask(nPageWidth);
551 std::vector<bool> nVerticalMask(nPageHeight);
552 FX_FLOAT fLineHeight = 0.0f;
553 int32_t nStartH = nPageWidth;
554 int32_t nEndH = 0;
555 int32_t nStartV = nPageHeight;
556 int32_t nEndV = 0;
557 for (const auto& pPageObj : *m_pPage->GetPageObjectList()) {
558 if (!pPageObj->IsText())
559 continue;
560
561 int32_t minH = std::max(static_cast<int32_t>(pPageObj->m_Left), 0);
562 int32_t maxH =
563 std::min(static_cast<int32_t>(pPageObj->m_Right), nPageWidth);
564 int32_t minV = std::max(static_cast<int32_t>(pPageObj->m_Bottom), 0);
565 int32_t maxV = std::min(static_cast<int32_t>(pPageObj->m_Top), nPageHeight);
566 if (minH >= maxH || minV >= maxV)
567 continue;
568
569 for (int32_t i = minH; i < maxH; ++i)
570 nHorizontalMask[i] = true;
571 for (int32_t i = minV; i < maxV; ++i)
572 nVerticalMask[i] = true;
573
574 nStartH = std::min(nStartH, minH);
575 nEndH = std::max(nEndH, maxH);
576 nStartV = std::min(nStartV, minV);
577 nEndV = std::max(nEndV, maxV);
578
579 if (fLineHeight <= 0.0f)
580 fLineHeight = pPageObj->m_Top - pPageObj->m_Bottom;
581 }
582 const int32_t nDoubleLineHeight = 2 * fLineHeight;
583 if ((nEndV - nStartV) < nDoubleLineHeight)
584 return TextOrientation::Horizontal;
585 if ((nEndH - nStartH) < nDoubleLineHeight)
586 return TextOrientation::Vertical;
587
588 const FX_FLOAT nSumH = MaskPercentFilled(nHorizontalMask, nStartH, nEndH);
589 if (nSumH > 0.8f)
590 return TextOrientation::Horizontal;
591
592 const FX_FLOAT nSumV = MaskPercentFilled(nVerticalMask, nStartV, nEndV);
593 if (nSumH > nSumV)
594 return TextOrientation::Horizontal;
595 if (nSumH < nSumV)
596 return TextOrientation::Vertical;
597 return TextOrientation::Unknown;
598 }
599
600 void CPDF_TextPage::AppendGeneratedCharacter(FX_WCHAR unicode,
601 const CFX_Matrix& formMatrix) {
602 PAGECHAR_INFO generateChar;
603 if (!GenerateCharInfo(unicode, generateChar))
604 return;
605
606 m_TextBuf.AppendChar(unicode);
607 if (!formMatrix.IsIdentity())
608 generateChar.m_Matrix.Copy(formMatrix);
609 m_CharList.push_back(generateChar);
610 }
611
612 void CPDF_TextPage::ProcessObject() {
613 if (m_pPage->GetPageObjectList()->empty())
614 return;
615
616 m_TextlineDir = FindTextlineFlowOrientation();
617 const CPDF_PageObjectList* pObjList = m_pPage->GetPageObjectList();
618 for (auto it = pObjList->begin(); it != pObjList->end(); ++it) {
619 if (CPDF_PageObject* pObj = it->get()) {
620 if (pObj->IsText()) {
621 CFX_Matrix matrix;
622 ProcessTextObject(pObj->AsText(), matrix, pObjList, it);
623 } else if (pObj->IsForm()) {
624 CFX_Matrix formMatrix(1, 0, 0, 1, 0, 0);
625 ProcessFormObject(pObj->AsForm(), formMatrix);
626 }
627 }
628 }
629 for (int i = 0; i < m_LineObj.GetSize(); i++)
630 ProcessTextObject(m_LineObj.GetAt(i));
631
632 m_LineObj.RemoveAll();
633 CloseTempLine();
634 }
635
636 void CPDF_TextPage::ProcessFormObject(CPDF_FormObject* pFormObj,
637 const CFX_Matrix& formMatrix) {
638 CPDF_PageObjectList* pObjectList = pFormObj->m_pForm->GetPageObjectList();
639 if (pObjectList->empty())
640 return;
641
642 CFX_Matrix curFormMatrix;
643 curFormMatrix.Copy(pFormObj->m_FormMatrix);
644 curFormMatrix.Concat(formMatrix);
645
646 for (auto it = pObjectList->begin(); it != pObjectList->end(); ++it) {
647 if (CPDF_PageObject* pPageObj = it->get()) {
648 if (pPageObj->IsText())
649 ProcessTextObject(pPageObj->AsText(), curFormMatrix, pObjectList, it);
650 else if (pPageObj->IsForm())
651 ProcessFormObject(pPageObj->AsForm(), curFormMatrix);
652 }
653 }
654 }
655
656 int CPDF_TextPage::GetCharWidth(uint32_t charCode, CPDF_Font* pFont) const {
657 if (charCode == CPDF_Font::kInvalidCharCode)
658 return 0;
659
660 if (int w = pFont->GetCharWidthF(charCode))
661 return w;
662
663 CFX_ByteString str;
664 pFont->AppendChar(str, charCode);
665 if (int w = pFont->GetStringWidth(str.c_str(), 1))
666 return w;
667
668 return pFont->GetCharBBox(charCode).Width();
669 }
670
671 void CPDF_TextPage::AddCharInfoByLRDirection(FX_WCHAR wChar,
672 PAGECHAR_INFO info) {
673 if (IsControlChar(info)) {
674 info.m_Index = -1;
675 m_CharList.push_back(info);
676 return;
677 }
678
679 info.m_Index = m_TextBuf.GetLength();
680 if (wChar >= 0xFB00 && wChar <= 0xFB06) {
681 FX_WCHAR* pDst = nullptr;
682 FX_STRSIZE nCount = Unicode_GetNormalization(wChar, pDst);
683 if (nCount >= 1) {
684 pDst = FX_Alloc(FX_WCHAR, nCount);
685 Unicode_GetNormalization(wChar, pDst);
686 for (int nIndex = 0; nIndex < nCount; nIndex++) {
687 PAGECHAR_INFO info2 = info;
688 info2.m_Unicode = pDst[nIndex];
689 info2.m_Flag = FPDFTEXT_CHAR_PIECE;
690 m_TextBuf.AppendChar(info2.m_Unicode);
691 m_CharList.push_back(info2);
692 }
693 FX_Free(pDst);
694 return;
695 }
696 }
697 m_TextBuf.AppendChar(wChar);
698 m_CharList.push_back(info);
699 }
700
701 void CPDF_TextPage::AddCharInfoByRLDirection(FX_WCHAR wChar,
702 PAGECHAR_INFO info) {
703 if (IsControlChar(info)) {
704 info.m_Index = -1;
705 m_CharList.push_back(info);
706 return;
707 }
708
709 info.m_Index = m_TextBuf.GetLength();
710 wChar = FX_GetMirrorChar(wChar, TRUE, FALSE);
711 FX_WCHAR* pDst = nullptr;
712 FX_STRSIZE nCount = Unicode_GetNormalization(wChar, pDst);
713 if (nCount >= 1) {
714 pDst = FX_Alloc(FX_WCHAR, nCount);
715 Unicode_GetNormalization(wChar, pDst);
716 for (int nIndex = 0; nIndex < nCount; nIndex++) {
717 PAGECHAR_INFO info2 = info;
718 info2.m_Unicode = pDst[nIndex];
719 info2.m_Flag = FPDFTEXT_CHAR_PIECE;
720 m_TextBuf.AppendChar(info2.m_Unicode);
721 m_CharList.push_back(info2);
722 }
723 FX_Free(pDst);
724 return;
725 }
726 info.m_Unicode = wChar;
727 m_TextBuf.AppendChar(info.m_Unicode);
728 m_CharList.push_back(info);
729 }
730
731 void CPDF_TextPage::CloseTempLine() {
732 if (m_TempCharList.empty())
733 return;
734
735 CFX_WideString str = m_TempTextBuf.MakeString();
736 bool bPrevSpace = false;
737 for (int i = 0; i < str.GetLength(); i++) {
738 if (str.GetAt(i) != ' ') {
739 bPrevSpace = false;
740 continue;
741 }
742 if (bPrevSpace) {
743 m_TempTextBuf.Delete(i, 1);
744 m_TempCharList.erase(m_TempCharList.begin() + i);
745 str.Delete(i);
746 i--;
747 }
748 bPrevSpace = true;
749 }
750 CFX_BidiString bidi(str);
751 if (m_parserflag == FPDFText_Direction::Right)
752 bidi.SetOverallDirectionRight();
753 CFX_BidiChar::Direction eCurrentDirection = bidi.OverallDirection();
754 for (const auto& segment : bidi) {
755 if (segment.direction == CFX_BidiChar::RIGHT ||
756 (segment.direction == CFX_BidiChar::NEUTRAL &&
757 eCurrentDirection == CFX_BidiChar::RIGHT)) {
758 eCurrentDirection = CFX_BidiChar::RIGHT;
759 for (int m = segment.start + segment.count; m > segment.start; --m)
760 AddCharInfoByRLDirection(bidi.CharAt(m - 1), m_TempCharList[m - 1]);
761 } else {
762 eCurrentDirection = CFX_BidiChar::LEFT;
763 for (int m = segment.start; m < segment.start + segment.count; m++)
764 AddCharInfoByLRDirection(bidi.CharAt(m), m_TempCharList[m]);
765 }
766 }
767 m_TempCharList.clear();
768 m_TempTextBuf.Delete(0, m_TempTextBuf.GetLength());
769 }
770
771 void CPDF_TextPage::ProcessTextObject(
772 CPDF_TextObject* pTextObj,
773 const CFX_Matrix& formMatrix,
774 const CPDF_PageObjectList* pObjList,
775 CPDF_PageObjectList::const_iterator ObjPos) {
776 CFX_FloatRect re(pTextObj->m_Left, pTextObj->m_Bottom, pTextObj->m_Right,
777 pTextObj->m_Top);
778 if (FXSYS_fabs(pTextObj->m_Right - pTextObj->m_Left) < 0.01f)
779 return;
780 int count = m_LineObj.GetSize();
781 PDFTEXT_Obj Obj;
782 Obj.m_pTextObj = pTextObj;
783 Obj.m_formMatrix = formMatrix;
784 if (count == 0) {
785 m_LineObj.Add(Obj);
786 return;
787 }
788 if (IsSameAsPreTextObject(pTextObj, pObjList, ObjPos))
789 return;
790 PDFTEXT_Obj prev_Obj = m_LineObj.GetAt(count - 1);
791 CPDF_TextObjectItem item;
792 int nItem = prev_Obj.m_pTextObj->CountItems();
793 prev_Obj.m_pTextObj->GetItemInfo(nItem - 1, &item);
794 FX_FLOAT prev_width =
795 GetCharWidth(item.m_CharCode, prev_Obj.m_pTextObj->GetFont()) *
796 prev_Obj.m_pTextObj->GetFontSize() / 1000;
797 CFX_Matrix prev_matrix;
798 prev_Obj.m_pTextObj->GetTextMatrix(&prev_matrix);
799 prev_width = FXSYS_fabs(prev_width);
800 prev_matrix.Concat(prev_Obj.m_formMatrix);
801 prev_width = prev_matrix.TransformDistance(prev_width);
802 pTextObj->GetItemInfo(0, &item);
803 FX_FLOAT this_width = GetCharWidth(item.m_CharCode, pTextObj->GetFont()) *
804 pTextObj->GetFontSize() / 1000;
805 this_width = FXSYS_fabs(this_width);
806 CFX_Matrix this_matrix;
807 pTextObj->GetTextMatrix(&this_matrix);
808 this_width = FXSYS_fabs(this_width);
809 this_matrix.Concat(formMatrix);
810 this_width = this_matrix.TransformDistance(this_width);
811 FX_FLOAT threshold =
812 prev_width > this_width ? prev_width / 4 : this_width / 4;
813 FX_FLOAT prev_x = prev_Obj.m_pTextObj->GetPosX(),
814 prev_y = prev_Obj.m_pTextObj->GetPosY();
815 prev_Obj.m_formMatrix.Transform(prev_x, prev_y);
816 m_DisplayMatrix.Transform(prev_x, prev_y);
817 FX_FLOAT this_x = pTextObj->GetPosX(), this_y = pTextObj->GetPosY();
818 formMatrix.Transform(this_x, this_y);
819 m_DisplayMatrix.Transform(this_x, this_y);
820 if (FXSYS_fabs(this_y - prev_y) > threshold * 2) {
821 for (int i = 0; i < count; i++)
822 ProcessTextObject(m_LineObj.GetAt(i));
823 m_LineObj.RemoveAll();
824 m_LineObj.Add(Obj);
825 return;
826 }
827 int i = 0;
828 for (i = count - 1; i >= 0; i--) {
829 PDFTEXT_Obj prev_text_obj = m_LineObj.GetAt(i);
830 FX_FLOAT Prev_x = prev_text_obj.m_pTextObj->GetPosX(),
831 Prev_y = prev_text_obj.m_pTextObj->GetPosY();
832 prev_text_obj.m_formMatrix.Transform(Prev_x, Prev_y);
833 m_DisplayMatrix.Transform(Prev_x, Prev_y);
834 if (this_x >= Prev_x) {
835 if (i == count - 1)
836 m_LineObj.Add(Obj);
837 else
838 m_LineObj.InsertAt(i + 1, Obj);
839 break;
840 }
841 }
842 if (i < 0)
843 m_LineObj.InsertAt(0, Obj);
844 }
845
846 FPDFText_MarkedContent CPDF_TextPage::PreMarkedContent(PDFTEXT_Obj Obj) {
847 CPDF_TextObject* pTextObj = Obj.m_pTextObj;
848 const CPDF_ContentMarkData* pMarkData = pTextObj->m_ContentMark.GetObject();
849 if (!pMarkData)
850 return FPDFText_MarkedContent::Pass;
851
852 int nContentMark = pMarkData->CountItems();
853 if (nContentMark < 1)
854 return FPDFText_MarkedContent::Pass;
855
856 CFX_WideString actText;
857 bool bExist = false;
858 CPDF_Dictionary* pDict = nullptr;
859 int n = 0;
860 for (n = 0; n < nContentMark; n++) {
861 const CPDF_ContentMarkItem& item = pMarkData->GetItem(n);
862 if (item.GetParamType() == CPDF_ContentMarkItem::ParamType::None)
863 continue;
864 pDict = item.GetParam();
865 CPDF_String* temp =
866 ToString(pDict ? pDict->GetObjectBy("ActualText") : nullptr);
867 if (temp) {
868 bExist = true;
869 actText = temp->GetUnicodeText();
870 }
871 }
872 if (!bExist)
873 return FPDFText_MarkedContent::Pass;
874
875 if (m_pPreTextObj) {
876 const CPDF_ContentMarkData* pPreMarkData =
877 m_pPreTextObj->m_ContentMark.GetObject();
878 if (pPreMarkData && pPreMarkData->CountItems() == n &&
879 pDict == pPreMarkData->GetItem(n - 1).GetParam()) {
880 return FPDFText_MarkedContent::Done;
881 }
882 }
883 FX_STRSIZE nItems = actText.GetLength();
884 if (nItems < 1)
885 return FPDFText_MarkedContent::Pass;
886
887 CPDF_Font* pFont = pTextObj->GetFont();
888 bExist = false;
889 for (FX_STRSIZE i = 0; i < nItems; i++) {
890 if (pFont->CharCodeFromUnicode(actText.GetAt(i)) !=
891 CPDF_Font::kInvalidCharCode) {
892 bExist = true;
893 break;
894 }
895 }
896 if (!bExist)
897 return FPDFText_MarkedContent::Pass;
898
899 bExist = false;
900 for (FX_STRSIZE i = 0; i < nItems; i++) {
901 FX_WCHAR wChar = actText.GetAt(i);
902 if ((wChar > 0x80 && wChar < 0xFFFD) || (wChar <= 0x80 && isprint(wChar))) {
903 bExist = true;
904 break;
905 }
906 }
907 if (!bExist)
908 return FPDFText_MarkedContent::Done;
909
910 return FPDFText_MarkedContent::Delay;
911 }
912
913 void CPDF_TextPage::ProcessMarkedContent(PDFTEXT_Obj Obj) {
914 CPDF_TextObject* pTextObj = Obj.m_pTextObj;
915 const CPDF_ContentMarkData* pMarkData = pTextObj->m_ContentMark.GetObject();
916 if (!pMarkData)
917 return;
918
919 int nContentMark = pMarkData->CountItems();
920 if (nContentMark < 1)
921 return;
922 CFX_WideString actText;
923 CPDF_Dictionary* pDict = nullptr;
924 for (int n = 0; n < nContentMark; n++) {
925 const CPDF_ContentMarkItem& item = pMarkData->GetItem(n);
926 if (item.GetParamType() == CPDF_ContentMarkItem::ParamType::None)
927 continue;
928 pDict = item.GetParam();
929 if (pDict)
930 actText = pDict->GetUnicodeTextBy("ActualText");
931 }
932 FX_STRSIZE nItems = actText.GetLength();
933 if (nItems < 1)
934 return;
935
936 CPDF_Font* pFont = pTextObj->GetFont();
937 CFX_Matrix formMatrix = Obj.m_formMatrix;
938 CFX_Matrix matrix;
939 pTextObj->GetTextMatrix(&matrix);
940 matrix.Concat(formMatrix);
941 FX_FLOAT fPosX = pTextObj->GetPosX();
942 FX_FLOAT fPosY = pTextObj->GetPosY();
943 int nCharInfoIndex = m_TextBuf.GetLength();
944 CFX_FloatRect charBox;
945 charBox.top = pTextObj->m_Top;
946 charBox.left = pTextObj->m_Left;
947 charBox.right = pTextObj->m_Right;
948 charBox.bottom = pTextObj->m_Bottom;
949 for (FX_STRSIZE k = 0; k < nItems; k++) {
950 FX_WCHAR wChar = actText.GetAt(k);
951 if (wChar <= 0x80 && !isprint(wChar))
952 wChar = 0x20;
953 if (wChar >= 0xFFFD)
954 continue;
955 PAGECHAR_INFO charinfo;
956 charinfo.m_OriginX = fPosX;
957 charinfo.m_OriginY = fPosY;
958 charinfo.m_Index = nCharInfoIndex;
959 charinfo.m_Unicode = wChar;
960 charinfo.m_CharCode = pFont->CharCodeFromUnicode(wChar);
961 charinfo.m_Flag = FPDFTEXT_CHAR_PIECE;
962 charinfo.m_pTextObj = pTextObj;
963 charinfo.m_CharBox.top = charBox.top;
964 charinfo.m_CharBox.left = charBox.left;
965 charinfo.m_CharBox.right = charBox.right;
966 charinfo.m_CharBox.bottom = charBox.bottom;
967 charinfo.m_Matrix.Copy(matrix);
968 m_TempTextBuf.AppendChar(wChar);
969 m_TempCharList.push_back(charinfo);
970 }
971 }
972
973 void CPDF_TextPage::FindPreviousTextObject() {
974 if (m_TempCharList.empty() && m_CharList.empty())
975 return;
976
977 PAGECHAR_INFO preChar =
978 m_TempCharList.empty() ? m_CharList.back() : m_TempCharList.back();
979
980 if (preChar.m_pTextObj)
981 m_pPreTextObj = preChar.m_pTextObj;
982 }
983
984 void CPDF_TextPage::SwapTempTextBuf(int32_t iCharListStartAppend,
985 int32_t iBufStartAppend) {
986 int32_t i = iCharListStartAppend;
987 int32_t j = pdfium::CollectionSize<int32_t>(m_TempCharList) - 1;
988 for (; i < j; i++, j--) {
989 std::swap(m_TempCharList[i], m_TempCharList[j]);
990 std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index);
991 }
992 FX_WCHAR* pTempBuffer = m_TempTextBuf.GetBuffer();
993 i = iBufStartAppend;
994 j = m_TempTextBuf.GetLength() - 1;
995 for (; i < j; i++, j--)
996 std::swap(pTempBuffer[i], pTempBuffer[j]);
997 }
998
999 FX_BOOL CPDF_TextPage::IsRightToLeft(const CPDF_TextObject* pTextObj,
1000 const CPDF_Font* pFont,
1001 int nItems) const {
1002 CFX_WideString str;
1003 for (int32_t i = 0; i < nItems; i++) {
1004 CPDF_TextObjectItem item;
1005 pTextObj->GetItemInfo(i, &item);
1006 if (item.m_CharCode == static_cast<uint32_t>(-1))
1007 continue;
1008 CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode);
1009 FX_WCHAR wChar = wstrItem.GetAt(0);
1010 if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode)
1011 wChar = (FX_WCHAR)item.m_CharCode;
1012 if (wChar)
1013 str += wChar;
1014 }
1015 return CFX_BidiString(str).OverallDirection() == CFX_BidiChar::RIGHT;
1016 }
1017
1018 void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) {
1019 CPDF_TextObject* pTextObj = Obj.m_pTextObj;
1020 if (FXSYS_fabs(pTextObj->m_Right - pTextObj->m_Left) < 0.01f)
1021 return;
1022 CFX_Matrix formMatrix = Obj.m_formMatrix;
1023 CPDF_Font* pFont = pTextObj->GetFont();
1024 CFX_Matrix matrix;
1025 pTextObj->GetTextMatrix(&matrix);
1026 matrix.Concat(formMatrix);
1027 FPDFText_MarkedContent ePreMKC = PreMarkedContent(Obj);
1028 if (ePreMKC == FPDFText_MarkedContent::Done) {
1029 m_pPreTextObj = pTextObj;
1030 m_perMatrix.Copy(formMatrix);
1031 return;
1032 }
1033 GenerateCharacter result = GenerateCharacter::None;
1034 if (m_pPreTextObj) {
1035 result = ProcessInsertObject(pTextObj, formMatrix);
1036 if (result == GenerateCharacter::LineBreak) {
1037 m_CurlineRect =
1038 CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
1039 Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top);
1040 } else {
1041 m_CurlineRect.Union(
1042 CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
1043 Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top));
1044 }
1045 switch (result) {
1046 case GenerateCharacter::None:
1047 break;
1048 case GenerateCharacter::Space: {
1049 PAGECHAR_INFO generateChar;
1050 if (GenerateCharInfo(TEXT_SPACE_CHAR, generateChar)) {
1051 if (!formMatrix.IsIdentity())
1052 generateChar.m_Matrix.Copy(formMatrix);
1053 m_TempTextBuf.AppendChar(TEXT_SPACE_CHAR);
1054 m_TempCharList.push_back(generateChar);
1055 }
1056 break;
1057 }
1058 case GenerateCharacter::LineBreak:
1059 CloseTempLine();
1060 if (m_TextBuf.GetSize()) {
1061 AppendGeneratedCharacter(TEXT_RETURN_CHAR, formMatrix);
1062 AppendGeneratedCharacter(TEXT_LINEFEED_CHAR, formMatrix);
1063 }
1064 break;
1065 case GenerateCharacter::Hyphen:
1066 if (pTextObj->CountChars() == 1) {
1067 CPDF_TextObjectItem item;
1068 pTextObj->GetCharInfo(0, &item);
1069 CFX_WideString wstrItem =
1070 pTextObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
1071 if (wstrItem.IsEmpty())
1072 wstrItem += (FX_WCHAR)item.m_CharCode;
1073 FX_WCHAR curChar = wstrItem.GetAt(0);
1074 if (curChar == 0x2D || curChar == 0xAD)
1075 return;
1076 }
1077 while (m_TempTextBuf.GetSize() > 0 &&
1078 m_TempTextBuf.AsStringC().GetAt(m_TempTextBuf.GetLength() - 1) ==
1079 0x20) {
1080 m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1081 m_TempCharList.pop_back();
1082 }
1083 PAGECHAR_INFO* charinfo = &m_TempCharList.back();
1084 m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1085 charinfo->m_Unicode = 0x2;
1086 charinfo->m_Flag = FPDFTEXT_CHAR_HYPHEN;
1087 m_TempTextBuf.AppendChar(0xfffe);
1088 break;
1089 }
1090 } else {
1091 m_CurlineRect =
1092 CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
1093 Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top);
1094 }
1095 if (ePreMKC == FPDFText_MarkedContent::Delay) {
1096 ProcessMarkedContent(Obj);
1097 m_pPreTextObj = pTextObj;
1098 m_perMatrix.Copy(formMatrix);
1099 return;
1100 }
1101 m_pPreTextObj = pTextObj;
1102 m_perMatrix.Copy(formMatrix);
1103 int nItems = pTextObj->CountItems();
1104 FX_FLOAT baseSpace = CalculateBaseSpace(pTextObj, matrix);
1105
1106 const FX_BOOL bR2L = IsRightToLeft(pTextObj, pFont, nItems);
1107 const FX_BOOL bIsBidiAndMirrorInverse =
1108 bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0;
1109 int32_t iBufStartAppend = m_TempTextBuf.GetLength();
1110 int32_t iCharListStartAppend =
1111 pdfium::CollectionSize<int32_t>(m_TempCharList);
1112
1113 FX_FLOAT spacing = 0;
1114 for (int i = 0; i < nItems; i++) {
1115 CPDF_TextObjectItem item;
1116 PAGECHAR_INFO charinfo;
1117 charinfo.m_OriginX = 0;
1118 charinfo.m_OriginY = 0;
1119 pTextObj->GetItemInfo(i, &item);
1120 if (item.m_CharCode == static_cast<uint32_t>(-1)) {
1121 CFX_WideString str = m_TempTextBuf.MakeString();
1122 if (str.IsEmpty())
1123 str = m_TextBuf.AsStringC();
1124 if (str.IsEmpty() || str.GetAt(str.GetLength() - 1) == TEXT_SPACE_CHAR)
1125 continue;
1126
1127 FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
1128 spacing = -fontsize_h * item.m_OriginX / 1000;
1129 continue;
1130 }
1131 FX_FLOAT charSpace = pTextObj->m_TextState.GetObject()->m_CharSpace;
1132 if (charSpace > 0.001)
1133 spacing += matrix.TransformDistance(charSpace);
1134 else if (charSpace < -0.001)
1135 spacing -= matrix.TransformDistance(FXSYS_fabs(charSpace));
1136 spacing -= baseSpace;
1137 if (spacing && i > 0) {
1138 int last_width = 0;
1139 FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
1140 uint32_t space_charcode = pFont->CharCodeFromUnicode(' ');
1141 FX_FLOAT threshold = 0;
1142 if (space_charcode != CPDF_Font::kInvalidCharCode)
1143 threshold = fontsize_h * pFont->GetCharWidthF(space_charcode) / 1000;
1144 if (threshold > fontsize_h / 3)
1145 threshold = 0;
1146 else
1147 threshold /= 2;
1148 if (threshold == 0) {
1149 threshold = fontsize_h;
1150 int this_width = FXSYS_abs(GetCharWidth(item.m_CharCode, pFont));
1151 threshold = this_width > last_width ? (FX_FLOAT)this_width
1152 : (FX_FLOAT)last_width;
1153 threshold = NormalizeThreshold(threshold);
1154 threshold = fontsize_h * threshold / 1000;
1155 }
1156 if (threshold && (spacing && spacing >= threshold)) {
1157 charinfo.m_Unicode = TEXT_SPACE_CHAR;
1158 charinfo.m_Flag = FPDFTEXT_CHAR_GENERATED;
1159 charinfo.m_pTextObj = pTextObj;
1160 charinfo.m_Index = m_TextBuf.GetLength();
1161 m_TempTextBuf.AppendChar(TEXT_SPACE_CHAR);
1162 charinfo.m_CharCode = CPDF_Font::kInvalidCharCode;
1163 charinfo.m_Matrix.Copy(formMatrix);
1164 matrix.Transform(item.m_OriginX, item.m_OriginY, charinfo.m_OriginX,
1165 charinfo.m_OriginY);
1166 charinfo.m_CharBox =
1167 CFX_FloatRect(charinfo.m_OriginX, charinfo.m_OriginY,
1168 charinfo.m_OriginX, charinfo.m_OriginY);
1169 m_TempCharList.push_back(charinfo);
1170 }
1171 if (item.m_CharCode == CPDF_Font::kInvalidCharCode)
1172 continue;
1173 }
1174 spacing = 0;
1175 CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode);
1176 bool bNoUnicode = false;
1177 FX_WCHAR wChar = wstrItem.GetAt(0);
1178 if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode) {
1179 if (wstrItem.IsEmpty())
1180 wstrItem += (FX_WCHAR)item.m_CharCode;
1181 else
1182 wstrItem.SetAt(0, (FX_WCHAR)item.m_CharCode);
1183 bNoUnicode = true;
1184 }
1185 charinfo.m_Index = -1;
1186 charinfo.m_CharCode = item.m_CharCode;
1187 if (bNoUnicode)
1188 charinfo.m_Flag = FPDFTEXT_CHAR_UNUNICODE;
1189 else
1190 charinfo.m_Flag = FPDFTEXT_CHAR_NORMAL;
1191 charinfo.m_pTextObj = pTextObj;
1192 charinfo.m_OriginX = 0, charinfo.m_OriginY = 0;
1193 matrix.Transform(item.m_OriginX, item.m_OriginY, charinfo.m_OriginX,
1194 charinfo.m_OriginY);
1195 FX_RECT rect =
1196 charinfo.m_pTextObj->GetFont()->GetCharBBox(charinfo.m_CharCode);
1197 charinfo.m_CharBox.top =
1198 rect.top * pTextObj->GetFontSize() / 1000 + item.m_OriginY;
1199 charinfo.m_CharBox.left =
1200 rect.left * pTextObj->GetFontSize() / 1000 + item.m_OriginX;
1201 charinfo.m_CharBox.right =
1202 rect.right * pTextObj->GetFontSize() / 1000 + item.m_OriginX;
1203 charinfo.m_CharBox.bottom =
1204 rect.bottom * pTextObj->GetFontSize() / 1000 + item.m_OriginY;
1205 if (fabsf(charinfo.m_CharBox.top - charinfo.m_CharBox.bottom) < 0.01f) {
1206 charinfo.m_CharBox.top =
1207 charinfo.m_CharBox.bottom + pTextObj->GetFontSize();
1208 }
1209 if (fabsf(charinfo.m_CharBox.right - charinfo.m_CharBox.left) < 0.01f) {
1210 charinfo.m_CharBox.right =
1211 charinfo.m_CharBox.left + pTextObj->GetCharWidth(charinfo.m_CharCode);
1212 }
1213 matrix.TransformRect(charinfo.m_CharBox);
1214 charinfo.m_Matrix.Copy(matrix);
1215 if (wstrItem.IsEmpty()) {
1216 charinfo.m_Unicode = 0;
1217 m_TempCharList.push_back(charinfo);
1218 m_TempTextBuf.AppendChar(0xfffe);
1219 continue;
1220 } else {
1221 int nTotal = wstrItem.GetLength();
1222 bool bDel = false;
1223 const int count =
1224 std::min(pdfium::CollectionSize<int>(m_TempCharList), 7);
1225 FX_FLOAT threshold = charinfo.m_Matrix.TransformXDistance(
1226 (FX_FLOAT)TEXT_CHARRATIO_GAPDELTA * pTextObj->GetFontSize());
1227 for (int n = pdfium::CollectionSize<int>(m_TempCharList);
1228 n > pdfium::CollectionSize<int>(m_TempCharList) - count; n--) {
1229 const PAGECHAR_INFO& charinfo1 = m_TempCharList[n - 1];
1230 if (charinfo1.m_CharCode == charinfo.m_CharCode &&
1231 charinfo1.m_pTextObj->GetFont() == charinfo.m_pTextObj->GetFont() &&
1232 FXSYS_fabs(charinfo1.m_OriginX - charinfo.m_OriginX) < threshold &&
1233 FXSYS_fabs(charinfo1.m_OriginY - charinfo.m_OriginY) < threshold) {
1234 bDel = true;
1235 break;
1236 }
1237 }
1238 if (!bDel) {
1239 for (int nIndex = 0; nIndex < nTotal; nIndex++) {
1240 charinfo.m_Unicode = wstrItem.GetAt(nIndex);
1241 if (charinfo.m_Unicode) {
1242 charinfo.m_Index = m_TextBuf.GetLength();
1243 m_TempTextBuf.AppendChar(charinfo.m_Unicode);
1244 } else {
1245 m_TempTextBuf.AppendChar(0xfffe);
1246 }
1247 m_TempCharList.push_back(charinfo);
1248 }
1249 } else if (i == 0) {
1250 CFX_WideString str = m_TempTextBuf.MakeString();
1251 if (!str.IsEmpty() &&
1252 str.GetAt(str.GetLength() - 1) == TEXT_SPACE_CHAR) {
1253 m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1254 m_TempCharList.pop_back();
1255 }
1256 }
1257 }
1258 }
1259 if (bIsBidiAndMirrorInverse)
1260 SwapTempTextBuf(iCharListStartAppend, iBufStartAppend);
1261 }
1262
1263 CPDF_TextPage::TextOrientation CPDF_TextPage::GetTextObjectWritingMode(
1264 const CPDF_TextObject* pTextObj) const {
1265 int32_t nChars = pTextObj->CountChars();
1266 if (nChars == 1)
1267 return m_TextlineDir;
1268
1269 CPDF_TextObjectItem first, last;
1270 pTextObj->GetCharInfo(0, &first);
1271 pTextObj->GetCharInfo(nChars - 1, &last);
1272 CFX_Matrix textMatrix;
1273 pTextObj->GetTextMatrix(&textMatrix);
1274 textMatrix.TransformPoint(first.m_OriginX, first.m_OriginY);
1275 textMatrix.TransformPoint(last.m_OriginX, last.m_OriginY);
1276 FX_FLOAT dX = FXSYS_fabs(last.m_OriginX - first.m_OriginX);
1277 FX_FLOAT dY = FXSYS_fabs(last.m_OriginY - first.m_OriginY);
1278 if (dX <= 0.0001f && dY <= 0.0001f)
1279 return TextOrientation::Unknown;
1280
1281 CFX_VectorF v(dX, dY);
1282 v.Normalize();
1283 if (v.y <= 0.0872f)
1284 return v.x <= 0.0872f ? m_TextlineDir : TextOrientation::Horizontal;
1285
1286 if (v.x <= 0.0872f)
1287 return TextOrientation::Vertical;
1288
1289 return m_TextlineDir;
1290 }
1291
1292 FX_BOOL CPDF_TextPage::IsHyphen(FX_WCHAR curChar) {
1293 CFX_WideString strCurText = m_TempTextBuf.MakeString();
1294 if (strCurText.IsEmpty())
1295 strCurText = m_TextBuf.AsStringC();
1296 FX_STRSIZE nCount = strCurText.GetLength();
1297 int nIndex = nCount - 1;
1298 FX_WCHAR wcTmp = strCurText.GetAt(nIndex);
1299 while (wcTmp == 0x20 && nIndex <= nCount - 1 && nIndex >= 0)
1300 wcTmp = strCurText.GetAt(--nIndex);
1301 if (0x2D == wcTmp || 0xAD == wcTmp) {
1302 if (--nIndex > 0) {
1303 FX_WCHAR preChar = strCurText.GetAt((nIndex));
1304 if (((preChar >= L'A' && preChar <= L'Z') ||
1305 (preChar >= L'a' && preChar <= L'z')) &&
1306 ((curChar >= L'A' && curChar <= L'Z') ||
1307 (curChar >= L'a' && curChar <= L'z'))) {
1308 return TRUE;
1309 }
1310 }
1311 const PAGECHAR_INFO* preInfo;
1312 if (!m_TempCharList.empty())
1313 preInfo = &m_TempCharList.back();
1314 else if (!m_CharList.empty())
1315 preInfo = &m_CharList.back();
1316 else
1317 return FALSE;
1318 if (FPDFTEXT_CHAR_PIECE == preInfo->m_Flag &&
1319 (0xAD == preInfo->m_Unicode || 0x2D == preInfo->m_Unicode)) {
1320 return TRUE;
1321 }
1322 }
1323 return FALSE;
1324 }
1325
1326 CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject(
1327 const CPDF_TextObject* pObj,
1328 const CFX_Matrix& formMatrix) {
1329 FindPreviousTextObject();
1330 TextOrientation WritingMode = GetTextObjectWritingMode(pObj);
1331 if (WritingMode == TextOrientation::Unknown)
1332 WritingMode = GetTextObjectWritingMode(m_pPreTextObj);
1333
1334 CFX_FloatRect this_rect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right,
1335 pObj->m_Top);
1336 CFX_FloatRect prev_rect(m_pPreTextObj->m_Left, m_pPreTextObj->m_Bottom,
1337 m_pPreTextObj->m_Right, m_pPreTextObj->m_Top);
1338 CPDF_TextObjectItem PrevItem, item;
1339 int nItem = m_pPreTextObj->CountItems();
1340 m_pPreTextObj->GetItemInfo(nItem - 1, &PrevItem);
1341 pObj->GetItemInfo(0, &item);
1342 CFX_WideString wstrItem =
1343 pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
1344 if (wstrItem.IsEmpty())
1345 wstrItem += static_cast<FX_WCHAR>(item.m_CharCode);
1346 FX_WCHAR curChar = wstrItem.GetAt(0);
1347 if (WritingMode == TextOrientation::Horizontal) {
1348 if (this_rect.Height() > 4.5 && prev_rect.Height() > 4.5) {
1349 FX_FLOAT top =
1350 this_rect.top < prev_rect.top ? this_rect.top : prev_rect.top;
1351 FX_FLOAT bottom = this_rect.bottom > prev_rect.bottom ? this_rect.bottom
1352 : prev_rect.bottom;
1353 if (bottom >= top) {
1354 return IsHyphen(curChar) ? GenerateCharacter::Hyphen
1355 : GenerateCharacter::LineBreak;
1356 }
1357 }
1358 } else if (WritingMode == TextOrientation::Vertical) {
1359 if (this_rect.Width() > pObj->GetFontSize() * 0.1f &&
1360 prev_rect.Width() > m_pPreTextObj->GetFontSize() * 0.1f) {
1361 FX_FLOAT left = this_rect.left > m_CurlineRect.left ? this_rect.left
1362 : m_CurlineRect.left;
1363 FX_FLOAT right = this_rect.right < m_CurlineRect.right
1364 ? this_rect.right
1365 : m_CurlineRect.right;
1366 if (right <= left) {
1367 return IsHyphen(curChar) ? GenerateCharacter::Hyphen
1368 : GenerateCharacter::LineBreak;
1369 }
1370 }
1371 }
1372 FX_FLOAT last_pos = PrevItem.m_OriginX;
1373 int nLastWidth = GetCharWidth(PrevItem.m_CharCode, m_pPreTextObj->GetFont());
1374 FX_FLOAT last_width = nLastWidth * m_pPreTextObj->GetFontSize() / 1000;
1375 last_width = FXSYS_fabs(last_width);
1376 int nThisWidth = GetCharWidth(item.m_CharCode, pObj->GetFont());
1377 FX_FLOAT this_width = nThisWidth * pObj->GetFontSize() / 1000;
1378 this_width = FXSYS_fabs(this_width);
1379 FX_FLOAT threshold =
1380 last_width > this_width ? last_width / 4 : this_width / 4;
1381 CFX_Matrix prev_matrix, prev_reverse;
1382 m_pPreTextObj->GetTextMatrix(&prev_matrix);
1383 prev_matrix.Concat(m_perMatrix);
1384 prev_reverse.SetReverse(prev_matrix);
1385 FX_FLOAT x = pObj->GetPosX();
1386 FX_FLOAT y = pObj->GetPosY();
1387 formMatrix.Transform(x, y);
1388 prev_reverse.Transform(x, y);
1389 if (last_width < this_width)
1390 threshold = prev_reverse.TransformDistance(threshold);
1391 bool bNewline = false;
1392 if (WritingMode == TextOrientation::Horizontal) {
1393 CFX_FloatRect rect1(m_pPreTextObj->m_Left, pObj->m_Bottom,
1394 m_pPreTextObj->m_Right, pObj->m_Top);
1395 CFX_FloatRect rect2(m_pPreTextObj->m_Left, m_pPreTextObj->m_Bottom,
1396 m_pPreTextObj->m_Right, m_pPreTextObj->m_Top);
1397 CFX_FloatRect rect3 = rect1;
1398 rect1.Intersect(rect2);
1399 if ((rect1.IsEmpty() && rect2.Height() > 5 && rect3.Height() > 5) ||
1400 ((y > threshold * 2 || y < threshold * -3) &&
1401 (FXSYS_fabs(y) < 1 ? FXSYS_fabs(x) < FXSYS_fabs(y) : TRUE))) {
1402 bNewline = true;
1403 if (nItem > 1) {
1404 CPDF_TextObjectItem tempItem;
1405 m_pPreTextObj->GetItemInfo(0, &tempItem);
1406 CFX_Matrix m;
1407 m_pPreTextObj->GetTextMatrix(&m);
1408 if (PrevItem.m_OriginX > tempItem.m_OriginX &&
1409 m_DisplayMatrix.a > 0.9 && m_DisplayMatrix.b < 0.1 &&
1410 m_DisplayMatrix.c < 0.1 && m_DisplayMatrix.d < -0.9 && m.b < 0.1 &&
1411 m.c < 0.1) {
1412 CFX_FloatRect re(0, m_pPreTextObj->m_Bottom, 1000,
1413 m_pPreTextObj->m_Top);
1414 if (re.Contains(pObj->GetPosX(), pObj->GetPosY())) {
1415 bNewline = false;
1416 } else {
1417 CFX_FloatRect rect(0, pObj->m_Bottom, 1000, pObj->m_Top);
1418 if (rect.Contains(m_pPreTextObj->GetPosX(),
1419 m_pPreTextObj->GetPosY())) {
1420 bNewline = false;
1421 }
1422 }
1423 }
1424 }
1425 }
1426 }
1427 if (bNewline) {
1428 return IsHyphen(curChar) ? GenerateCharacter::Hyphen
1429 : GenerateCharacter::LineBreak;
1430 }
1431
1432 int32_t nChars = pObj->CountChars();
1433 if (nChars == 1 && (0x2D == curChar || 0xAD == curChar) &&
1434 IsHyphen(curChar)) {
1435 return GenerateCharacter::Hyphen;
1436 }
1437 CFX_WideString PrevStr =
1438 m_pPreTextObj->GetFont()->UnicodeFromCharCode(PrevItem.m_CharCode);
1439 FX_WCHAR preChar = PrevStr.GetAt(PrevStr.GetLength() - 1);
1440 CFX_Matrix matrix;
1441 pObj->GetTextMatrix(&matrix);
1442 matrix.Concat(formMatrix);
1443 threshold = (FX_FLOAT)(nLastWidth > nThisWidth ? nLastWidth : nThisWidth);
1444 threshold = threshold > 400
1445 ? (threshold < 700
1446 ? threshold / 4
1447 : (threshold > 800 ? threshold / 6 : threshold / 5))
1448 : (threshold / 2);
1449 if (nLastWidth >= nThisWidth) {
1450 threshold *= FXSYS_fabs(m_pPreTextObj->GetFontSize());
1451 } else {
1452 threshold *= FXSYS_fabs(pObj->GetFontSize());
1453 threshold = matrix.TransformDistance(threshold);
1454 threshold = prev_reverse.TransformDistance(threshold);
1455 }
1456 threshold /= 1000;
1457 if ((threshold < 1.4881 && threshold > 1.4879) ||
1458 (threshold < 1.39001 && threshold > 1.38999)) {
1459 threshold *= 1.5;
1460 }
1461 if (FXSYS_fabs(last_pos + last_width - x) > threshold && curChar != L' ' &&
1462 preChar != L' ') {
1463 if (curChar != L' ' && preChar != L' ') {
1464 if ((x - last_pos - last_width) > threshold ||
1465 (last_pos - x - last_width) > threshold) {
1466 return GenerateCharacter::Space;
1467 }
1468 if (x < 0 && (last_pos - x - last_width) > threshold)
1469 return GenerateCharacter::Space;
1470 if ((x - last_pos - last_width) > this_width ||
1471 (x - last_pos - this_width) > last_width) {
1472 return GenerateCharacter::Space;
1473 }
1474 }
1475 }
1476 return GenerateCharacter::None;
1477 }
1478
1479 FX_BOOL CPDF_TextPage::IsSameTextObject(CPDF_TextObject* pTextObj1,
1480 CPDF_TextObject* pTextObj2) {
1481 if (!pTextObj1 || !pTextObj2)
1482 return FALSE;
1483 CFX_FloatRect rcPreObj(pTextObj2->m_Left, pTextObj2->m_Bottom,
1484 pTextObj2->m_Right, pTextObj2->m_Top);
1485 CFX_FloatRect rcCurObj(pTextObj1->m_Left, pTextObj1->m_Bottom,
1486 pTextObj1->m_Right, pTextObj1->m_Top);
1487 if (rcPreObj.IsEmpty() && rcCurObj.IsEmpty()) {
1488 FX_FLOAT dbXdif = FXSYS_fabs(rcPreObj.left - rcCurObj.left);
1489 size_t nCount = m_CharList.size();
1490 if (nCount >= 2) {
1491 PAGECHAR_INFO perCharTemp = m_CharList[nCount - 2];
1492 FX_FLOAT dbSpace = perCharTemp.m_CharBox.Width();
1493 if (dbXdif > dbSpace)
1494 return FALSE;
1495 }
1496 }
1497 if (!rcPreObj.IsEmpty() || !rcCurObj.IsEmpty()) {
1498 rcPreObj.Intersect(rcCurObj);
1499 if (rcPreObj.IsEmpty())
1500 return FALSE;
1501 if (FXSYS_fabs(rcPreObj.Width() - rcCurObj.Width()) >
1502 rcCurObj.Width() / 2) {
1503 return FALSE;
1504 }
1505 if (pTextObj2->GetFontSize() != pTextObj1->GetFontSize())
1506 return FALSE;
1507 }
1508 int nPreCount = pTextObj2->CountItems();
1509 int nCurCount = pTextObj1->CountItems();
1510 if (nPreCount != nCurCount)
1511 return FALSE;
1512 // If both objects have no items, consider them same.
1513 if (!nPreCount)
1514 return TRUE;
1515
1516 CPDF_TextObjectItem itemPer = {0, 0.0f, 0.0f};
1517 CPDF_TextObjectItem itemCur = {0, 0.0f, 0.0f};
1518 for (int i = 0; i < nPreCount; i++) {
1519 pTextObj2->GetItemInfo(i, &itemPer);
1520 pTextObj1->GetItemInfo(i, &itemCur);
1521 if (itemCur.m_CharCode != itemPer.m_CharCode)
1522 return FALSE;
1523 }
1524 if (FXSYS_fabs(pTextObj1->GetPosX() - pTextObj2->GetPosX()) >
1525 GetCharWidth(itemPer.m_CharCode, pTextObj2->GetFont()) *
1526 pTextObj2->GetFontSize() / 1000 * 0.9 ||
1527 FXSYS_fabs(pTextObj1->GetPosY() - pTextObj2->GetPosY()) >
1528 std::max(std::max(rcPreObj.Height(), rcPreObj.Width()),
1529 pTextObj2->GetFontSize()) /
1530 8) {
1531 return FALSE;
1532 }
1533 return TRUE;
1534 }
1535
1536 FX_BOOL CPDF_TextPage::IsSameAsPreTextObject(
1537 CPDF_TextObject* pTextObj,
1538 const CPDF_PageObjectList* pObjList,
1539 CPDF_PageObjectList::const_iterator iter) {
1540 int i = 0;
1541 while (i < 5 && iter != pObjList->begin()) {
1542 --iter;
1543 CPDF_PageObject* pOtherObj = iter->get();
1544 if (pOtherObj == pTextObj || !pOtherObj->IsText())
1545 continue;
1546 if (IsSameTextObject(pOtherObj->AsText(), pTextObj))
1547 return TRUE;
1548 ++i;
1549 }
1550 return FALSE;
1551 }
1552
1553 FX_BOOL CPDF_TextPage::GenerateCharInfo(FX_WCHAR unicode, PAGECHAR_INFO& info) {
1554 const PAGECHAR_INFO* preChar;
1555 if (!m_TempCharList.empty())
1556 preChar = &m_TempCharList.back();
1557 else if (!m_CharList.empty())
1558 preChar = &m_CharList.back();
1559 else
1560 return FALSE;
1561 info.m_Index = m_TextBuf.GetLength();
1562 info.m_Unicode = unicode;
1563 info.m_pTextObj = nullptr;
1564 info.m_CharCode = CPDF_Font::kInvalidCharCode;
1565 info.m_Flag = FPDFTEXT_CHAR_GENERATED;
1566 int preWidth = 0;
1567 if (preChar->m_pTextObj && preChar->m_CharCode != -1) {
1568 preWidth =
1569 GetCharWidth(preChar->m_CharCode, preChar->m_pTextObj->GetFont());
1570 }
1571
1572 FX_FLOAT fFontSize = preChar->m_pTextObj ? preChar->m_pTextObj->GetFontSize()
1573 : preChar->m_CharBox.Height();
1574 if (!fFontSize)
1575 fFontSize = kDefaultFontSize;
1576
1577 info.m_OriginX = preChar->m_OriginX + preWidth * (fFontSize) / 1000;
1578 info.m_OriginY = preChar->m_OriginY;
1579 info.m_CharBox = CFX_FloatRect(info.m_OriginX, info.m_OriginY, info.m_OriginX,
1580 info.m_OriginY);
1581 return TRUE;
1582 }
1583
1584 FX_BOOL CPDF_TextPage::IsRectIntersect(const CFX_FloatRect& rect1,
1585 const CFX_FloatRect& rect2) {
1586 CFX_FloatRect rect = rect1;
1587 rect.Intersect(rect2);
1588 return !rect.IsEmpty();
1589 }
1590
1591 CPDF_TextPageFind::CPDF_TextPageFind(const CPDF_TextPage* pTextPage)
1592 : m_pTextPage(pTextPage),
1593 m_flags(0),
1594 m_findNextStart(-1),
1595 m_findPreStart(-1),
1596 m_bMatchCase(FALSE),
1597 m_bMatchWholeWord(FALSE),
1598 m_resStart(0),
1599 m_resEnd(-1),
1600 m_IsFind(FALSE) {
1601 m_strText = m_pTextPage->GetPageText();
1602 int nCount = pTextPage->CountChars();
1603 if (nCount) {
1604 m_CharIndex.push_back(0);
1605 }
1606 for (int i = 0; i < nCount; i++) {
1607 FPDF_CHAR_INFO info;
1608 pTextPage->GetCharInfo(i, &info);
1609 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
1610 if (info.m_Flag == FPDFTEXT_CHAR_NORMAL ||
1611 info.m_Flag == FPDFTEXT_CHAR_GENERATED) {
1612 if (indexSize % 2) {
1613 m_CharIndex.push_back(1);
1614 } else {
1615 if (indexSize <= 0) {
1616 continue;
1617 }
1618 m_CharIndex[indexSize - 1] += 1;
1619 }
1620 } else {
1621 if (indexSize % 2) {
1622 if (indexSize <= 0) {
1623 continue;
1624 }
1625 m_CharIndex[indexSize - 1] = i + 1;
1626 } else {
1627 m_CharIndex.push_back(i + 1);
1628 }
1629 }
1630 }
1631 int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
1632 if (indexSize % 2) {
1633 m_CharIndex.erase(m_CharIndex.begin() + indexSize - 1);
1634 }
1635 }
1636
1637 CPDF_TextPageFind::~CPDF_TextPageFind() {}
1638
1639 int CPDF_TextPageFind::GetCharIndex(int index) const {
1640 return m_pTextPage->CharIndexFromTextIndex(index);
1641 }
1642
1643 FX_BOOL CPDF_TextPageFind::FindFirst(const CFX_WideString& findwhat,
1644 int flags,
1645 int startPos) {
1646 if (!m_pTextPage) {
1647 return FALSE;
1648 }
1649 if (m_strText.IsEmpty() || m_bMatchCase != (flags & FPDFTEXT_MATCHCASE)) {
1650 m_strText = m_pTextPage->GetPageText();
1651 }
1652 CFX_WideString findwhatStr = findwhat;
1653 m_findWhat = findwhatStr;
1654 m_flags = flags;
1655 m_bMatchCase = flags & FPDFTEXT_MATCHCASE;
1656 if (m_strText.IsEmpty()) {
1657 m_IsFind = FALSE;
1658 return TRUE;
1659 }
1660 FX_STRSIZE len = findwhatStr.GetLength();
1661 if (!m_bMatchCase) {
1662 findwhatStr.MakeLower();
1663 m_strText.MakeLower();
1664 }
1665 m_bMatchWholeWord = flags & FPDFTEXT_MATCHWHOLEWORD;
1666 m_findNextStart = startPos;
1667 if (startPos == -1) {
1668 m_findPreStart = m_strText.GetLength() - 1;
1669 } else {
1670 m_findPreStart = startPos;
1671 }
1672 m_csFindWhatArray.clear();
1673 int i = 0;
1674 while (i < len) {
1675 if (findwhatStr.GetAt(i) != ' ') {
1676 break;
1677 }
1678 i++;
1679 }
1680 if (i < len) {
1681 ExtractFindWhat(findwhatStr);
1682 } else {
1683 m_csFindWhatArray.push_back(findwhatStr);
1684 }
1685 if (m_csFindWhatArray.empty()) {
1686 return FALSE;
1687 }
1688 m_IsFind = TRUE;
1689 m_resStart = 0;
1690 m_resEnd = -1;
1691 return TRUE;
1692 }
1693
1694 FX_BOOL CPDF_TextPageFind::FindNext() {
1695 if (!m_pTextPage) {
1696 return FALSE;
1697 }
1698 m_resArray.clear();
1699 if (m_findNextStart == -1) {
1700 return FALSE;
1701 }
1702 if (m_strText.IsEmpty()) {
1703 m_IsFind = FALSE;
1704 return m_IsFind;
1705 }
1706 int strLen = m_strText.GetLength();
1707 if (m_findNextStart > strLen - 1) {
1708 m_IsFind = FALSE;
1709 return m_IsFind;
1710 }
1711 int nCount = pdfium::CollectionSize<int>(m_csFindWhatArray);
1712 int nResultPos = 0;
1713 int nStartPos = 0;
1714 nStartPos = m_findNextStart;
1715 FX_BOOL bSpaceStart = FALSE;
1716 for (int iWord = 0; iWord < nCount; iWord++) {
1717 CFX_WideString csWord = m_csFindWhatArray[iWord];
1718 if (csWord.IsEmpty()) {
1719 if (iWord == nCount - 1) {
1720 FX_WCHAR strInsert = m_strText.GetAt(nStartPos);
1721 if (strInsert == TEXT_LINEFEED_CHAR || strInsert == TEXT_SPACE_CHAR ||
1722 strInsert == TEXT_RETURN_CHAR || strInsert == 160) {
1723 nResultPos = nStartPos + 1;
1724 break;
1725 }
1726 iWord = -1;
1727 } else if (iWord == 0) {
1728 bSpaceStart = TRUE;
1729 }
1730 continue;
1731 }
1732 int endIndex;
1733 nResultPos = m_strText.Find(csWord.c_str(), nStartPos);
1734 if (nResultPos == -1) {
1735 m_IsFind = FALSE;
1736 return m_IsFind;
1737 }
1738 endIndex = nResultPos + csWord.GetLength() - 1;
1739 if (iWord == 0) {
1740 m_resStart = nResultPos;
1741 }
1742 FX_BOOL bMatch = TRUE;
1743 if (iWord != 0 && !bSpaceStart) {
1744 int PreResEndPos = nStartPos;
1745 int curChar = csWord.GetAt(0);
1746 CFX_WideString lastWord = m_csFindWhatArray[iWord - 1];
1747 int lastChar = lastWord.GetAt(lastWord.GetLength() - 1);
1748 if (nStartPos == nResultPos &&
1749 !(IsIgnoreSpaceCharacter(lastChar) ||
1750 IsIgnoreSpaceCharacter(curChar))) {
1751 bMatch = FALSE;
1752 }
1753 for (int d = PreResEndPos; d < nResultPos; d++) {
1754 FX_WCHAR strInsert = m_strText.GetAt(d);
1755 if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR &&
1756 strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
1757 bMatch = FALSE;
1758 break;
1759 }
1760 }
1761 } else if (bSpaceStart) {
1762 if (nResultPos > 0) {
1763 FX_WCHAR strInsert = m_strText.GetAt(nResultPos - 1);
1764 if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR &&
1765 strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
1766 bMatch = FALSE;
1767 m_resStart = nResultPos;
1768 } else {
1769 m_resStart = nResultPos - 1;
1770 }
1771 }
1772 }
1773 if (m_bMatchWholeWord && bMatch) {
1774 bMatch = IsMatchWholeWord(m_strText, nResultPos, endIndex);
1775 }
1776 nStartPos = endIndex + 1;
1777 if (!bMatch) {
1778 iWord = -1;
1779 if (bSpaceStart) {
1780 nStartPos = m_resStart + m_csFindWhatArray[1].GetLength();
1781 } else {
1782 nStartPos = m_resStart + m_csFindWhatArray[0].GetLength();
1783 }
1784 }
1785 }
1786 m_resEnd = nResultPos + m_csFindWhatArray.back().GetLength() - 1;
1787 m_IsFind = TRUE;
1788 int resStart = GetCharIndex(m_resStart);
1789 int resEnd = GetCharIndex(m_resEnd);
1790 m_resArray = m_pTextPage->GetRectArray(resStart, resEnd - resStart + 1);
1791 if (m_flags & FPDFTEXT_CONSECUTIVE) {
1792 m_findNextStart = m_resStart + 1;
1793 m_findPreStart = m_resEnd - 1;
1794 } else {
1795 m_findNextStart = m_resEnd + 1;
1796 m_findPreStart = m_resStart - 1;
1797 }
1798 return m_IsFind;
1799 }
1800
1801 FX_BOOL CPDF_TextPageFind::FindPrev() {
1802 if (!m_pTextPage) {
1803 return FALSE;
1804 }
1805 m_resArray.clear();
1806 if (m_strText.IsEmpty() || m_findPreStart < 0) {
1807 m_IsFind = FALSE;
1808 return m_IsFind;
1809 }
1810 CPDF_TextPageFind findEngine(m_pTextPage);
1811 FX_BOOL ret = findEngine.FindFirst(m_findWhat, m_flags);
1812 if (!ret) {
1813 m_IsFind = FALSE;
1814 return m_IsFind;
1815 }
1816 int order = -1, MatchedCount = 0;
1817 while (ret) {
1818 ret = findEngine.FindNext();
1819 if (ret) {
1820 int order1 = findEngine.GetCurOrder();
1821 int MatchedCount1 = findEngine.GetMatchedCount();
1822 if (((order1 + MatchedCount1) - 1) > m_findPreStart) {
1823 break;
1824 }
1825 order = order1;
1826 MatchedCount = MatchedCount1;
1827 }
1828 }
1829 if (order == -1) {
1830 m_IsFind = FALSE;
1831 return m_IsFind;
1832 }
1833 m_resStart = m_pTextPage->TextIndexFromCharIndex(order);
1834 m_resEnd = m_pTextPage->TextIndexFromCharIndex(order + MatchedCount - 1);
1835 m_IsFind = TRUE;
1836 m_resArray = m_pTextPage->GetRectArray(order, MatchedCount);
1837 if (m_flags & FPDFTEXT_CONSECUTIVE) {
1838 m_findNextStart = m_resStart + 1;
1839 m_findPreStart = m_resEnd - 1;
1840 } else {
1841 m_findNextStart = m_resEnd + 1;
1842 m_findPreStart = m_resStart - 1;
1843 }
1844 return m_IsFind;
1845 }
1846
1847 void CPDF_TextPageFind::ExtractFindWhat(const CFX_WideString& findwhat) {
1848 if (findwhat.IsEmpty()) {
1849 return;
1850 }
1851 int index = 0;
1852 while (1) {
1853 CFX_WideString csWord = TEXT_EMPTY;
1854 int ret =
1855 ExtractSubString(csWord, findwhat.c_str(), index, TEXT_SPACE_CHAR);
1856 if (csWord.IsEmpty()) {
1857 if (ret) {
1858 m_csFindWhatArray.push_back(L"");
1859 index++;
1860 continue;
1861 } else {
1862 break;
1863 }
1864 }
1865 int pos = 0;
1866 while (pos < csWord.GetLength()) {
1867 CFX_WideString curStr = csWord.Mid(pos, 1);
1868 FX_WCHAR curChar = csWord.GetAt(pos);
1869 if (IsIgnoreSpaceCharacter(curChar)) {
1870 if (pos > 0 && curChar == 0x2019) {
1871 pos++;
1872 continue;
1873 }
1874 if (pos > 0) {
1875 m_csFindWhatArray.push_back(csWord.Mid(0, pos));
1876 }
1877 m_csFindWhatArray.push_back(curStr);
1878 if (pos == csWord.GetLength() - 1) {
1879 csWord.clear();
1880 break;
1881 }
1882 csWord = csWord.Right(csWord.GetLength() - pos - 1);
1883 pos = 0;
1884 continue;
1885 }
1886 pos++;
1887 }
1888 if (!csWord.IsEmpty()) {
1889 m_csFindWhatArray.push_back(csWord);
1890 }
1891 index++;
1892 }
1893 }
1894
1895 FX_BOOL CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText,
1896 int startPos,
1897 int endPos) {
1898 FX_WCHAR char_left = 0;
1899 FX_WCHAR char_right = 0;
1900 int char_count = endPos - startPos + 1;
1901 if (char_count < 1) {
1902 return FALSE;
1903 }
1904 if (char_count == 1 && csPageText.GetAt(startPos) > 255) {
1905 return TRUE;
1906 }
1907 if (startPos - 1 >= 0) {
1908 char_left = csPageText.GetAt(startPos - 1);
1909 }
1910 if (startPos + char_count < csPageText.GetLength()) {
1911 char_right = csPageText.GetAt(startPos + char_count);
1912 }
1913 if ((char_left > 'A' && char_left < 'a') ||
1914 (char_left > 'a' && char_left < 'z') ||
1915 (char_left > 0xfb00 && char_left < 0xfb06) || std::iswdigit(char_left) ||
1916 (char_right > 'A' && char_right < 'a') ||
1917 (char_right > 'a' && char_right < 'z') ||
1918 (char_right > 0xfb00 && char_right < 0xfb06) ||
1919 std::iswdigit(char_right)) {
1920 return FALSE;
1921 }
1922 if (!(('A' > char_left || char_left > 'Z') &&
1923 ('a' > char_left || char_left > 'z') &&
1924 ('A' > char_right || char_right > 'Z') &&
1925 ('a' > char_right || char_right > 'z'))) {
1926 return FALSE;
1927 }
1928 if (char_count > 0) {
1929 if (csPageText.GetAt(startPos) >= L'0' &&
1930 csPageText.GetAt(startPos) <= L'9' && char_left >= L'0' &&
1931 char_left <= L'9') {
1932 return FALSE;
1933 }
1934 if (csPageText.GetAt(endPos) >= L'0' && csPageText.GetAt(endPos) <= L'9' &&
1935 char_right >= L'0' && char_right <= L'9') {
1936 return FALSE;
1937 }
1938 }
1939 return TRUE;
1940 }
1941
1942 FX_BOOL CPDF_TextPageFind::ExtractSubString(CFX_WideString& rString,
1943 const FX_WCHAR* lpszFullString,
1944 int iSubString,
1945 FX_WCHAR chSep) {
1946 if (!lpszFullString) {
1947 return FALSE;
1948 }
1949 while (iSubString--) {
1950 lpszFullString = wcschr(lpszFullString, chSep);
1951 if (!lpszFullString) {
1952 rString.clear();
1953 return FALSE;
1954 }
1955 lpszFullString++;
1956 while (*lpszFullString == chSep) {
1957 lpszFullString++;
1958 }
1959 }
1960 const FX_WCHAR* lpchEnd = wcschr(lpszFullString, chSep);
1961 int nLen = lpchEnd ? (int)(lpchEnd - lpszFullString)
1962 : (int)FXSYS_wcslen(lpszFullString);
1963 ASSERT(nLen >= 0);
1964 FXSYS_memcpy(rString.GetBuffer(nLen), lpszFullString,
1965 nLen * sizeof(FX_WCHAR));
1966 rString.ReleaseBuffer();
1967 return TRUE;
1968 }
1969
1970 CFX_WideString CPDF_TextPageFind::MakeReverse(const CFX_WideString& str) {
1971 CFX_WideString str2;
1972 str2.clear();
1973 int nlen = str.GetLength();
1974 for (int i = nlen - 1; i >= 0; i--) {
1975 str2 += str.GetAt(i);
1976 }
1977 return str2;
1978 }
1979
1980 int CPDF_TextPageFind::GetCurOrder() const {
1981 return GetCharIndex(m_resStart);
1982 }
1983
1984 int CPDF_TextPageFind::GetMatchedCount() const {
1985 int resStart = GetCharIndex(m_resStart);
1986 int resEnd = GetCharIndex(m_resEnd);
1987 return resEnd - resStart + 1;
1988 }
1989
1990 CPDF_LinkExtract::CPDF_LinkExtract(const CPDF_TextPage* pTextPage)
1991 : m_pTextPage(pTextPage) {}
1992
1993 CPDF_LinkExtract::~CPDF_LinkExtract() {
1994 }
1995
1996 void CPDF_LinkExtract::ExtractLinks() {
1997 m_LinkArray.clear();
1998 if (!m_pTextPage->IsParsed())
1999 return;
2000
2001 m_strPageText = m_pTextPage->GetPageText(0, -1);
2002 if (m_strPageText.IsEmpty())
2003 return;
2004
2005 ParseLink();
2006 }
2007
2008 void CPDF_LinkExtract::ParseLink() {
2009 int start = 0, pos = 0;
2010 int TotalChar = m_pTextPage->CountChars();
2011 while (pos < TotalChar) {
2012 FPDF_CHAR_INFO pageChar;
2013 m_pTextPage->GetCharInfo(pos, &pageChar);
2014 if (pageChar.m_Flag == FPDFTEXT_CHAR_GENERATED ||
2015 pageChar.m_Unicode == 0x20 || pos == TotalChar - 1) {
2016 int nCount = pos - start;
2017 if (pos == TotalChar - 1) {
2018 nCount++;
2019 }
2020 CFX_WideString strBeCheck;
2021 strBeCheck = m_pTextPage->GetPageText(start, nCount);
2022 if (strBeCheck.GetLength() > 5) {
2023 while (strBeCheck.GetLength() > 0) {
2024 FX_WCHAR ch = strBeCheck.GetAt(strBeCheck.GetLength() - 1);
2025 if (ch == L')' || ch == L',' || ch == L'>' || ch == L'.') {
2026 strBeCheck = strBeCheck.Mid(0, strBeCheck.GetLength() - 1);
2027 nCount--;
2028 } else {
2029 break;
2030 }
2031 }
2032 if (nCount > 5 &&
2033 (CheckWebLink(strBeCheck) || CheckMailLink(strBeCheck))) {
2034 m_LinkArray.push_back({start, nCount, strBeCheck});
2035 }
2036 }
2037 start = ++pos;
2038 } else {
2039 pos++;
2040 }
2041 }
2042 }
2043
2044 bool CPDF_LinkExtract::CheckWebLink(CFX_WideString& strBeCheck) {
2045 CFX_WideString str = strBeCheck;
2046 str.MakeLower();
2047 if (str.Find(L"http://www.") != -1) {
2048 strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"http://www."));
2049 return true;
2050 }
2051 if (str.Find(L"http://") != -1) {
2052 strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"http://"));
2053 return true;
2054 }
2055 if (str.Find(L"https://www.") != -1) {
2056 strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"https://www."));
2057 return true;
2058 }
2059 if (str.Find(L"https://") != -1) {
2060 strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"https://"));
2061 return true;
2062 }
2063 if (str.Find(L"www.") != -1) {
2064 strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"www."));
2065 strBeCheck = L"http://" + strBeCheck;
2066 return true;
2067 }
2068 return false;
2069 }
2070
2071 bool CPDF_LinkExtract::CheckMailLink(CFX_WideString& str) {
2072 int aPos = str.Find(L'@');
2073 // Invalid when no '@'.
2074 if (aPos < 1)
2075 return false;
2076
2077 // Check the local part.
2078 int pPos = aPos; // Used to track the position of '@' or '.'.
2079 for (int i = aPos - 1; i >= 0; i--) {
2080 FX_WCHAR ch = str.GetAt(i);
2081 if (ch == L'_' || ch == L'-' || FXSYS_iswalnum(ch))
2082 continue;
2083
2084 if (ch != L'.' || i == pPos - 1 || i == 0) {
2085 if (i == aPos - 1) {
2086 // There is '.' or invalid char before '@'.
2087 return FALSE;
2088 }
2089 // End extracting for other invalid chars, '.' at the beginning, or
2090 // consecutive '.'.
2091 int removed_len = i == pPos - 1 ? i + 2 : i + 1;
2092 str = str.Right(str.GetLength() - removed_len);
2093 break;
2094 }
2095 // Found a valid '.'.
2096 pPos = i;
2097 }
2098
2099 // Check the domain name part.
2100 aPos = str.Find(L'@');
2101 if (aPos < 1)
2102 return false;
2103
2104 str.TrimRight(L'.');
2105 // At least one '.' in domain name, but not at the beginning.
2106 // TODO(weili): RFC5322 allows domain names to be a local name without '.'.
2107 // Check whether we should remove this check.
2108 int ePos = str.Find(L'.', aPos + 1);
2109 if (ePos == -1 || ePos == aPos + 1)
2110 return false;
2111
2112 // Validate all other chars in domain name.
2113 int nLen = str.GetLength();
2114 pPos = 0; // Used to track the position of '.'.
2115 for (int i = aPos + 1; i < nLen; i++) {
2116 FX_WCHAR wch = str.GetAt(i);
2117 if (wch == L'-' || FXSYS_iswalnum(wch))
2118 continue;
2119
2120 if (wch != L'.' || i == pPos + 1) {
2121 // Domain name should end before invalid char.
2122 int host_end = i == pPos + 1 ? i - 2 : i - 1;
2123 if (pPos > 0 && host_end - aPos >= 3) {
2124 // Trim the ending invalid chars if there is at least one '.' and name.
2125 str = str.Left(host_end + 1);
2126 break;
2127 }
2128 return false;
2129 }
2130 pPos = i;
2131 }
2132
2133 if (str.Find(L"mailto:") == -1)
2134 str = L"mailto:" + str;
2135
2136 return true;
2137 }
2138
2139 CFX_WideString CPDF_LinkExtract::GetURL(size_t index) const {
2140 return index < m_LinkArray.size() ? m_LinkArray[index].m_strUrl : L"";
2141 }
2142
2143 std::vector<CFX_FloatRect> CPDF_LinkExtract::GetRects(size_t index) const {
2144 if (index >= m_LinkArray.size())
2145 return std::vector<CFX_FloatRect>();
2146
2147 return m_pTextPage->GetRectArray(m_LinkArray[index].m_Start,
2148 m_LinkArray[index].m_Count);
2149 }
OLDNEW
« no previous file with comments | « core/fpdftext/cpdf_textpagefind.cpp ('k') | core/fpdftext/include/cpdf_textpage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698