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 "core/fpdftext/cpdf_textpage.h"
8 
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13 
14 #include "core/fpdfapi/font/cpdf_font.h"
15 #include "core/fpdfapi/page/cpdf_form.h"
16 #include "core/fpdfapi/page/cpdf_formobject.h"
17 #include "core/fpdfapi/page/cpdf_page.h"
18 #include "core/fpdfapi/page/cpdf_pageobject.h"
19 #include "core/fpdfapi/page/cpdf_textobject.h"
20 #include "core/fpdfapi/parser/cpdf_dictionary.h"
21 #include "core/fpdfapi/parser/cpdf_string.h"
22 #include "core/fpdftext/unicodenormalizationdata.h"
23 #include "core/fxcrt/fx_bidi.h"
24 #include "core/fxcrt/fx_extension.h"
25 #include "core/fxcrt/fx_memory_wrappers.h"
26 #include "core/fxcrt/fx_unicode.h"
27 #include "third_party/base/stl_util.h"
28 
29 namespace {
30 
31 constexpr float kDefaultFontSize = 1.0f;
32 constexpr float kSizeEpsilon = 0.01f;
33 
34 const uint16_t* const g_UnicodeData_Normalization_Maps[] = {
35     g_UnicodeData_Normalization_Map2, g_UnicodeData_Normalization_Map3,
36     g_UnicodeData_Normalization_Map4};
37 
NormalizeThreshold(float threshold,int t1,int t2,int t3)38 float NormalizeThreshold(float threshold, int t1, int t2, int t3) {
39   ASSERT(t1 < t2);
40   ASSERT(t2 < t3);
41   if (threshold < t1)
42     return threshold / 2.0f;
43   if (threshold < t2)
44     return threshold / 4.0f;
45   if (threshold < t3)
46     return threshold / 5.0f;
47   return threshold / 6.0f;
48 }
49 
CalculateBaseSpace(const CPDF_TextObject * pTextObj,const CFX_Matrix & matrix)50 float CalculateBaseSpace(const CPDF_TextObject* pTextObj,
51                          const CFX_Matrix& matrix) {
52   const size_t nItems = pTextObj->CountItems();
53   if (!pTextObj->m_TextState.GetCharSpace() || nItems < 3)
54     return 0.0f;
55 
56   bool bAllChar = true;
57   float spacing =
58       matrix.TransformDistance(pTextObj->m_TextState.GetCharSpace());
59   float baseSpace = spacing;
60   for (size_t i = 0; i < nItems; ++i) {
61     CPDF_TextObjectItem item;
62     pTextObj->GetItemInfo(i, &item);
63     if (item.m_CharCode == 0xffffffff) {
64       float fontsize_h = pTextObj->m_TextState.GetFontSizeH();
65       float kerning = -fontsize_h * item.m_Origin.x / 1000;
66       baseSpace = std::min(baseSpace, kerning + spacing);
67       bAllChar = false;
68     }
69   }
70   if (baseSpace < 0.0 || (nItems == 3 && !bAllChar))
71     return 0.0f;
72 
73   return baseSpace;
74 }
75 
Unicode_GetNormalization(wchar_t wch,wchar_t * pDst)76 size_t Unicode_GetNormalization(wchar_t wch, wchar_t* pDst) {
77   wch = wch & 0xFFFF;
78   wchar_t wFind = g_UnicodeData_Normalization[wch];
79   if (!wFind) {
80     if (pDst)
81       *pDst = wch;
82     return 1;
83   }
84   if (wFind >= 0x8000) {
85     const uint16_t* pMap = g_UnicodeData_Normalization_Map1 + (wFind - 0x8000);
86     if (pDst)
87       *pDst = *pMap;
88     return 1;
89   }
90 
91   wch = wFind & 0x0FFF;
92   wFind >>= 12;
93   const uint16_t* pMap = g_UnicodeData_Normalization_Maps[wFind - 2] + wch;
94   if (wFind == 4)
95     wFind = static_cast<wchar_t>(*pMap++);
96 
97   if (pDst) {
98     wchar_t n = wFind;
99     while (n--)
100       *pDst++ = *pMap++;
101   }
102   return static_cast<size_t>(wFind);
103 }
104 
MaskPercentFilled(const std::vector<bool> & mask,int32_t start,int32_t end)105 float MaskPercentFilled(const std::vector<bool>& mask,
106                         int32_t start,
107                         int32_t end) {
108   if (start >= end)
109     return 0;
110   float count = std::count_if(mask.begin() + start, mask.begin() + end,
111                               [](bool r) { return r; });
112   return count / (end - start);
113 }
114 
IsControlChar(const CPDF_TextPage::CharInfo & char_info)115 bool IsControlChar(const CPDF_TextPage::CharInfo& char_info) {
116   switch (char_info.m_Unicode) {
117     case 0x2:
118     case 0x3:
119     case 0x93:
120     case 0x94:
121     case 0x96:
122     case 0x97:
123     case 0x98:
124     case 0xfffe:
125       return char_info.m_CharType != CPDF_TextPage::CharType::kHyphen;
126     default:
127       return false;
128   }
129 }
130 
IsHyphenCode(wchar_t c)131 bool IsHyphenCode(wchar_t c) {
132   return c == 0x2D || c == 0xAD;
133 }
134 
IsRectIntersect(const CFX_FloatRect & rect1,const CFX_FloatRect & rect2)135 bool IsRectIntersect(const CFX_FloatRect& rect1, const CFX_FloatRect& rect2) {
136   CFX_FloatRect rect = rect1;
137   rect.Intersect(rect2);
138   return !rect.IsEmpty();
139 }
140 
IsRightToLeft(const CPDF_TextObject & text_obj,const CPDF_Font & font)141 bool IsRightToLeft(const CPDF_TextObject& text_obj, const CPDF_Font& font) {
142   const size_t nItems = text_obj.CountItems();
143   WideString str;
144   str.Reserve(nItems);
145   for (size_t i = 0; i < nItems; ++i) {
146     CPDF_TextObjectItem item;
147     text_obj.GetItemInfo(i, &item);
148     if (item.m_CharCode == 0xffffffff)
149       continue;
150     WideString wstrItem = font.UnicodeFromCharCode(item.m_CharCode);
151     wchar_t wChar = !wstrItem.IsEmpty() ? wstrItem[0] : 0;
152     if (wChar == 0)
153       wChar = item.m_CharCode;
154     if (wChar)
155       str += wChar;
156   }
157   return CFX_BidiString(str).OverallDirection() == CFX_BidiChar::RIGHT;
158 }
159 
GetCharWidth(uint32_t charCode,CPDF_Font * pFont)160 uint32_t GetCharWidth(uint32_t charCode, CPDF_Font* pFont) {
161   if (charCode == CPDF_Font::kInvalidCharCode)
162     return 0;
163 
164   uint32_t w = pFont->GetCharWidthF(charCode);
165   if (w > 0)
166     return w;
167 
168   ByteString str;
169   pFont->AppendChar(&str, charCode);
170   w = pFont->GetStringWidth(str.AsStringView());
171   if (w > 0)
172     return w;
173 
174   FX_RECT rect = pFont->GetCharBBox(charCode);
175   if (!rect.Valid())
176     return 0;
177 
178   return std::max(rect.Width(), 0);
179 }
180 
GenerateSpace(const CFX_PointF & pos,float last_pos,float this_width,float last_width,float threshold)181 bool GenerateSpace(const CFX_PointF& pos,
182                    float last_pos,
183                    float this_width,
184                    float last_width,
185                    float threshold) {
186   if (fabs(last_pos + last_width - pos.x) <= threshold)
187     return false;
188 
189   float threshold_pos = threshold + last_width;
190   float pos_difference = pos.x - last_pos;
191   if (fabs(pos_difference) > threshold_pos)
192     return true;
193   if (pos.x < 0 && -threshold_pos > pos_difference)
194     return true;
195   return pos_difference > this_width + last_width;
196 }
197 
EndHorizontalLine(const CFX_FloatRect & this_rect,const CFX_FloatRect & prev_rect)198 bool EndHorizontalLine(const CFX_FloatRect& this_rect,
199                        const CFX_FloatRect& prev_rect) {
200   if (this_rect.Height() <= 4.5 || prev_rect.Height() <= 4.5)
201     return false;
202 
203   float top = std::min(this_rect.top, prev_rect.top);
204   float bottom = std::max(this_rect.bottom, prev_rect.bottom);
205   return bottom >= top;
206 }
207 
EndVerticalLine(const CFX_FloatRect & this_rect,const CFX_FloatRect & prev_rect,const CFX_FloatRect & curline_rect,float this_fontsize,float prev_fontsize)208 bool EndVerticalLine(const CFX_FloatRect& this_rect,
209                      const CFX_FloatRect& prev_rect,
210                      const CFX_FloatRect& curline_rect,
211                      float this_fontsize,
212                      float prev_fontsize) {
213   if (this_rect.Width() <= this_fontsize * 0.1f ||
214       prev_rect.Width() <= prev_fontsize * 0.1f) {
215     return false;
216   }
217 
218   float left = std::max(this_rect.left, curline_rect.left);
219   float right = std::min(this_rect.right, curline_rect.right);
220   return right <= left;
221 }
222 
GetPageMatrix(const CPDF_Page * pPage)223 CFX_Matrix GetPageMatrix(const CPDF_Page* pPage) {
224   const FX_RECT rect(0, 0, static_cast<int>(pPage->GetPageWidth()),
225                      static_cast<int>(pPage->GetPageHeight()));
226   return pPage->GetDisplayMatrix(rect, 0);
227 }
228 
229 }  // namespace
230 
PDFTEXT_Obj()231 PDFTEXT_Obj::PDFTEXT_Obj() {}
232 
233 PDFTEXT_Obj::PDFTEXT_Obj(const PDFTEXT_Obj& that) = default;
234 
~PDFTEXT_Obj()235 PDFTEXT_Obj::~PDFTEXT_Obj() {}
236 
237 CPDF_TextPage::CharInfo::CharInfo() = default;
238 
239 CPDF_TextPage::CharInfo::CharInfo(const CharInfo&) = default;
240 
241 CPDF_TextPage::CharInfo::~CharInfo() = default;
242 
CPDF_TextPage(const CPDF_Page * pPage,bool rtl)243 CPDF_TextPage::CPDF_TextPage(const CPDF_Page* pPage, bool rtl)
244     : m_pPage(pPage), m_rtl(rtl), m_DisplayMatrix(GetPageMatrix(pPage)) {
245   Init();
246 }
247 
248 CPDF_TextPage::~CPDF_TextPage() = default;
249 
Init()250 void CPDF_TextPage::Init() {
251   m_TextBuf.SetAllocStep(10240);
252   ProcessObject();
253 
254   const int nCount = CountChars();
255   if (nCount)
256     m_CharIndices.push_back(0);
257 
258   for (int i = 0; i < nCount; ++i) {
259     const CharInfo& charinfo = m_CharList[i];
260     if (charinfo.m_CharType == CPDF_TextPage::CharType::kGenerated ||
261         (charinfo.m_Unicode != 0 && !IsControlChar(charinfo)) ||
262         (charinfo.m_Unicode == 0 && charinfo.m_CharCode != 0)) {
263       if (m_CharIndices.size() % 2) {
264         m_CharIndices.push_back(1);
265       } else {
266         if (m_CharIndices.empty())
267           continue;
268         m_CharIndices.back() += 1;
269       }
270     } else {
271       if (m_CharIndices.size() % 2) {
272         if (m_CharIndices.empty())
273           continue;
274         m_CharIndices.back() = i + 1;
275       } else {
276         m_CharIndices.push_back(i + 1);
277       }
278     }
279   }
280   if (m_CharIndices.size() % 2)
281     m_CharIndices.pop_back();
282 }
283 
CountChars() const284 int CPDF_TextPage::CountChars() const {
285   return pdfium::CollectionSize<int>(m_CharList);
286 }
287 
CharIndexFromTextIndex(int text_index) const288 int CPDF_TextPage::CharIndexFromTextIndex(int text_index) const {
289   int count = 0;
290   for (size_t i = 0; i < m_CharIndices.size(); i += 2) {
291     count += m_CharIndices[i + 1];
292     if (count > text_index)
293       return text_index - count + m_CharIndices[i + 1] + m_CharIndices[i];
294   }
295   return -1;
296 }
297 
TextIndexFromCharIndex(int char_index) const298 int CPDF_TextPage::TextIndexFromCharIndex(int char_index) const {
299   int count = 0;
300   for (size_t i = 0; i < m_CharIndices.size(); i += 2) {
301     int text_index = char_index - m_CharIndices[i];
302     if (text_index < m_CharIndices[i + 1])
303       return text_index >= 0 ? text_index + count : -1;
304 
305     count += m_CharIndices[i + 1];
306   }
307   return -1;
308 }
309 
GetRectArray(int start,int nCount) const310 std::vector<CFX_FloatRect> CPDF_TextPage::GetRectArray(int start,
311                                                        int nCount) const {
312   std::vector<CFX_FloatRect> rects;
313   if (start < 0 || nCount == 0)
314     return rects;
315 
316   const int nCharListSize = CountChars();
317   if (start >= nCharListSize)
318     return rects;
319 
320   if (nCount < 0 || start + nCount > nCharListSize)
321     nCount = nCharListSize - start;
322   ASSERT(nCount > 0);
323 
324   CPDF_TextObject* pCurObj = nullptr;
325   CFX_FloatRect rect;
326   int curPos = start;
327   bool bFlagNewRect = true;
328   while (nCount--) {
329     const CharInfo& info_curchar = m_CharList[curPos++];
330     if (info_curchar.m_CharType == CPDF_TextPage::CharType::kGenerated)
331       continue;
332     if (info_curchar.m_CharBox.Width() < kSizeEpsilon ||
333         info_curchar.m_CharBox.Height() < kSizeEpsilon) {
334       continue;
335     }
336     if (!pCurObj)
337       pCurObj = info_curchar.m_pTextObj.Get();
338     if (pCurObj != info_curchar.m_pTextObj) {
339       rects.push_back(rect);
340       pCurObj = info_curchar.m_pTextObj.Get();
341       bFlagNewRect = true;
342     }
343     if (bFlagNewRect) {
344       bFlagNewRect = false;
345       rect = info_curchar.m_CharBox;
346       rect.Normalize();
347       continue;
348     }
349     rect.Union(info_curchar.m_CharBox);
350   }
351   rects.push_back(rect);
352   return rects;
353 }
354 
GetIndexAtPos(const CFX_PointF & point,const CFX_SizeF & tolerance) const355 int CPDF_TextPage::GetIndexAtPos(const CFX_PointF& point,
356                                  const CFX_SizeF& tolerance) const {
357   int pos;
358   int NearPos = -1;
359   double xdif = 5000;
360   double ydif = 5000;
361   const int nCount = CountChars();
362   for (pos = 0; pos < nCount; ++pos) {
363     const CFX_FloatRect& orig_charrect = m_CharList[pos].m_CharBox;
364     if (orig_charrect.Contains(point))
365       break;
366 
367     if (tolerance.width <= 0 && tolerance.height <= 0)
368       continue;
369 
370     CFX_FloatRect charrect = orig_charrect;
371     charrect.Normalize();
372     CFX_FloatRect char_rect_ext(charrect.left - tolerance.width / 2,
373                                 charrect.bottom - tolerance.height / 2,
374                                 charrect.right + tolerance.width / 2,
375                                 charrect.top + tolerance.height / 2);
376     if (!char_rect_ext.Contains(point))
377       continue;
378 
379     double curXdif =
380         std::min(fabs(point.x - charrect.left), fabs(point.x - charrect.right));
381     double curYdif =
382         std::min(fabs(point.y - charrect.bottom), fabs(point.y - charrect.top));
383     if (curYdif + curXdif < xdif + ydif) {
384       ydif = curYdif;
385       xdif = curXdif;
386       NearPos = pos;
387     }
388   }
389   return pos < nCount ? pos : NearPos;
390 }
391 
GetTextByPredicate(const std::function<bool (const CharInfo &)> & predicate) const392 WideString CPDF_TextPage::GetTextByPredicate(
393     const std::function<bool(const CharInfo&)>& predicate) const {
394   float posy = 0;
395   bool IsContainPreChar = false;
396   bool IsAddLineFeed = false;
397   WideString strText;
398   for (const auto& charinfo : m_CharList) {
399     if (predicate(charinfo)) {
400       if (fabs(posy - charinfo.m_Origin.y) > 0 && !IsContainPreChar &&
401           IsAddLineFeed) {
402         posy = charinfo.m_Origin.y;
403         if (!strText.IsEmpty())
404           strText += L"\r\n";
405       }
406       IsContainPreChar = true;
407       IsAddLineFeed = false;
408       if (charinfo.m_Unicode)
409         strText += charinfo.m_Unicode;
410     } else if (charinfo.m_Unicode == L' ') {
411       if (IsContainPreChar) {
412         strText += L' ';
413         IsContainPreChar = false;
414         IsAddLineFeed = false;
415       }
416     } else {
417       IsContainPreChar = false;
418       IsAddLineFeed = true;
419     }
420   }
421   return strText;
422 }
423 
GetTextByRect(const CFX_FloatRect & rect) const424 WideString CPDF_TextPage::GetTextByRect(const CFX_FloatRect& rect) const {
425   return GetTextByPredicate([&rect](const CharInfo& charinfo) {
426     return IsRectIntersect(rect, charinfo.m_CharBox);
427   });
428 }
429 
GetTextByObject(const CPDF_TextObject * pTextObj) const430 WideString CPDF_TextPage::GetTextByObject(
431     const CPDF_TextObject* pTextObj) const {
432   return GetTextByPredicate([pTextObj](const CharInfo& charinfo) {
433     return charinfo.m_pTextObj == pTextObj;
434   });
435 }
436 
GetCharInfo(size_t index) const437 const CPDF_TextPage::CharInfo& CPDF_TextPage::GetCharInfo(size_t index) const {
438   CHECK(index < m_CharList.size());
439   return m_CharList[index];
440 }
441 
GetCharFontSize(size_t index) const442 float CPDF_TextPage::GetCharFontSize(size_t index) const {
443   CHECK(index < m_CharList.size());
444   const CPDF_TextObject* text_object = m_CharList[index].m_pTextObj.Get();
445   bool has_font = text_object && text_object->GetFont();
446   return has_font ? text_object->GetFontSize() : kDefaultFontSize;
447 }
448 
GetPageText(int start,int count) const449 WideString CPDF_TextPage::GetPageText(int start, int count) const {
450   if (start < 0 || start >= CountChars() || count <= 0 || m_CharList.empty() ||
451       m_TextBuf.IsEmpty()) {
452     return WideString();
453   }
454 
455   const int count_chars = CountChars();
456   int text_start = TextIndexFromCharIndex(start);
457 
458   // If the character at |start| is a non-printing character, then
459   // TextIndexFromCharIndex will return -1, so scan ahead to the first printing
460   // character.
461   while (text_start < 0) {
462     if (start >= count_chars)
463       return WideString();
464     start++;
465     text_start = TextIndexFromCharIndex(start);
466   }
467 
468   count = std::min(count, count_chars - start);
469 
470   int last = start + count - 1;
471   int text_last = TextIndexFromCharIndex(last);
472 
473   // If the character at |last| is a non-printing character, then
474   // TextIndexFromCharIndex will return -1, so scan back to the last printing
475   // character.
476   while (text_last < 0) {
477     if (last < text_start)
478       return WideString();
479 
480     last--;
481     text_last = TextIndexFromCharIndex(last);
482   }
483 
484   if (text_last < text_start)
485     return WideString();
486 
487   int text_count = text_last - text_start + 1;
488 
489   return WideString(m_TextBuf.AsStringView().Substr(text_start, text_count));
490 }
491 
CountRects(int start,int nCount)492 int CPDF_TextPage::CountRects(int start, int nCount) {
493   if (start < 0)
494     return -1;
495 
496   m_SelRects = GetRectArray(start, nCount);
497   return pdfium::CollectionSize<int>(m_SelRects);
498 }
499 
GetRect(int rectIndex,CFX_FloatRect * pRect) const500 bool CPDF_TextPage::GetRect(int rectIndex, CFX_FloatRect* pRect) const {
501   if (!pdfium::IndexInBounds(m_SelRects, rectIndex))
502     return false;
503 
504   *pRect = m_SelRects[rectIndex];
505   return true;
506 }
507 
FindTextlineFlowOrientation() const508 CPDF_TextPage::TextOrientation CPDF_TextPage::FindTextlineFlowOrientation()
509     const {
510   DCHECK_NE(m_pPage->GetPageObjectCount(), 0);
511 
512   const int32_t nPageWidth = static_cast<int32_t>(m_pPage->GetPageWidth());
513   const int32_t nPageHeight = static_cast<int32_t>(m_pPage->GetPageHeight());
514   if (nPageWidth <= 0 || nPageHeight <= 0)
515     return TextOrientation::kUnknown;
516 
517   std::vector<bool> nHorizontalMask(nPageWidth);
518   std::vector<bool> nVerticalMask(nPageHeight);
519   float fLineHeight = 0.0f;
520   int32_t nStartH = nPageWidth;
521   int32_t nEndH = 0;
522   int32_t nStartV = nPageHeight;
523   int32_t nEndV = 0;
524   for (const auto& pPageObj : *m_pPage) {
525     if (!pPageObj->IsText())
526       continue;
527 
528     int32_t minH = std::max(static_cast<int32_t>(pPageObj->GetRect().left), 0);
529     int32_t maxH =
530         std::min(static_cast<int32_t>(pPageObj->GetRect().right), nPageWidth);
531     int32_t minV =
532         std::max(static_cast<int32_t>(pPageObj->GetRect().bottom), 0);
533     int32_t maxV =
534         std::min(static_cast<int32_t>(pPageObj->GetRect().top), nPageHeight);
535     if (minH >= maxH || minV >= maxV)
536       continue;
537 
538     for (int32_t i = minH; i < maxH; ++i)
539       nHorizontalMask[i] = true;
540     for (int32_t i = minV; i < maxV; ++i)
541       nVerticalMask[i] = true;
542 
543     nStartH = std::min(nStartH, minH);
544     nEndH = std::max(nEndH, maxH);
545     nStartV = std::min(nStartV, minV);
546     nEndV = std::max(nEndV, maxV);
547 
548     if (fLineHeight <= 0.0f)
549       fLineHeight = pPageObj->GetRect().Height();
550   }
551   const int32_t nDoubleLineHeight = 2 * fLineHeight;
552   if ((nEndV - nStartV) < nDoubleLineHeight)
553     return TextOrientation::kHorizontal;
554   if ((nEndH - nStartH) < nDoubleLineHeight)
555     return TextOrientation::kVertical;
556 
557   const float nSumH = MaskPercentFilled(nHorizontalMask, nStartH, nEndH);
558   if (nSumH > 0.8f)
559     return TextOrientation::kHorizontal;
560 
561   const float nSumV = MaskPercentFilled(nVerticalMask, nStartV, nEndV);
562   if (nSumH > nSumV)
563     return TextOrientation::kHorizontal;
564   if (nSumH < nSumV)
565     return TextOrientation::kVertical;
566   return TextOrientation::kUnknown;
567 }
568 
AppendGeneratedCharacter(wchar_t unicode,const CFX_Matrix & formMatrix)569 void CPDF_TextPage::AppendGeneratedCharacter(wchar_t unicode,
570                                              const CFX_Matrix& formMatrix) {
571   Optional<CharInfo> pGenerateChar = GenerateCharInfo(unicode);
572   if (!pGenerateChar)
573     return;
574 
575   m_TextBuf.AppendChar(unicode);
576   if (!formMatrix.IsIdentity())
577     pGenerateChar->m_Matrix = formMatrix;
578   m_CharList.push_back(*pGenerateChar);
579 }
580 
ProcessObject()581 void CPDF_TextPage::ProcessObject() {
582   if (m_pPage->GetPageObjectCount() == 0)
583     return;
584 
585   m_TextlineDir = FindTextlineFlowOrientation();
586   for (auto it = m_pPage->begin(); it != m_pPage->end(); ++it) {
587     CPDF_PageObject* pObj = it->get();
588     if (!pObj)
589       continue;
590 
591     CFX_Matrix matrix;
592     if (pObj->IsText())
593       ProcessTextObject(pObj->AsText(), matrix, m_pPage.Get(), it);
594     else if (pObj->IsForm())
595       ProcessFormObject(pObj->AsForm(), matrix);
596   }
597   for (const auto& obj : m_LineObj)
598     ProcessTextObject(obj);
599 
600   m_LineObj.clear();
601   CloseTempLine();
602 }
603 
ProcessFormObject(CPDF_FormObject * pFormObj,const CFX_Matrix & formMatrix)604 void CPDF_TextPage::ProcessFormObject(CPDF_FormObject* pFormObj,
605                                       const CFX_Matrix& formMatrix) {
606   CFX_Matrix curFormMatrix = pFormObj->form_matrix() * formMatrix;
607   const CPDF_PageObjectHolder* pHolder = pFormObj->form();
608   for (auto it = pHolder->begin(); it != pHolder->end(); ++it) {
609     CPDF_PageObject* pPageObj = it->get();
610     if (!pPageObj)
611       continue;
612 
613     if (pPageObj->IsText())
614       ProcessTextObject(pPageObj->AsText(), curFormMatrix, pHolder, it);
615     else if (pPageObj->IsForm())
616       ProcessFormObject(pPageObj->AsForm(), curFormMatrix);
617   }
618 }
619 
AddCharInfoByLRDirection(wchar_t wChar,const CharInfo & info)620 void CPDF_TextPage::AddCharInfoByLRDirection(wchar_t wChar,
621                                              const CharInfo& info) {
622   CharInfo info2 = info;
623   if (IsControlChar(info2)) {
624     info2.m_Index = -1;
625     m_CharList.push_back(info2);
626     return;
627   }
628 
629   info2.m_Index = m_TextBuf.GetLength();
630   size_t nCount = 0;
631   if (wChar >= 0xFB00 && wChar <= 0xFB06)
632     nCount = Unicode_GetNormalization(wChar, nullptr);
633   if (nCount == 0) {
634     m_TextBuf.AppendChar(wChar);
635     m_CharList.push_back(info2);
636     return;
637   }
638 
639   std::unique_ptr<wchar_t, FxFreeDeleter> pDst(FX_Alloc(wchar_t, nCount));
640   Unicode_GetNormalization(wChar, pDst.get());
641   for (size_t nIndex = 0; nIndex < nCount; ++nIndex) {
642     info2.m_Unicode = pDst.get()[nIndex];
643     info2.m_CharType = CPDF_TextPage::CharType::kPiece;
644     m_TextBuf.AppendChar(info2.m_Unicode);
645     m_CharList.push_back(info2);
646   }
647 }
648 
AddCharInfoByRLDirection(wchar_t wChar,const CharInfo & info)649 void CPDF_TextPage::AddCharInfoByRLDirection(wchar_t wChar,
650                                              const CharInfo& info) {
651   CharInfo info2 = info;
652   if (IsControlChar(info2)) {
653     info2.m_Index = -1;
654     m_CharList.push_back(info2);
655     return;
656   }
657 
658   info2.m_Index = m_TextBuf.GetLength();
659   wChar = FX_GetMirrorChar(wChar);
660   size_t nCount = Unicode_GetNormalization(wChar, nullptr);
661   if (nCount == 0) {
662     info2.m_Unicode = wChar;
663     m_TextBuf.AppendChar(info2.m_Unicode);
664     m_CharList.push_back(info2);
665     return;
666   }
667 
668   std::unique_ptr<wchar_t, FxFreeDeleter> pDst(FX_Alloc(wchar_t, nCount));
669   Unicode_GetNormalization(wChar, pDst.get());
670   for (size_t nIndex = 0; nIndex < nCount; ++nIndex) {
671     info2.m_Unicode = pDst.get()[nIndex];
672     info2.m_CharType = CPDF_TextPage::CharType::kPiece;
673     m_TextBuf.AppendChar(info2.m_Unicode);
674     m_CharList.push_back(info2);
675   }
676 }
677 
CloseTempLine()678 void CPDF_TextPage::CloseTempLine() {
679   if (m_TempCharList.empty())
680     return;
681 
682   WideString str = m_TempTextBuf.MakeString();
683   bool bPrevSpace = false;
684   for (size_t i = 0; i < str.GetLength(); ++i) {
685     if (str[i] != ' ') {
686       bPrevSpace = false;
687       continue;
688     }
689     if (bPrevSpace) {
690       m_TempTextBuf.Delete(i, 1);
691       m_TempCharList.erase(m_TempCharList.begin() + i);
692       str.Delete(i);
693       --i;
694     }
695     bPrevSpace = true;
696   }
697   CFX_BidiString bidi(str);
698   if (m_rtl)
699     bidi.SetOverallDirectionRight();
700   CFX_BidiChar::Direction eCurrentDirection = bidi.OverallDirection();
701   for (const auto& segment : bidi) {
702     if (segment.direction == CFX_BidiChar::RIGHT ||
703         (segment.direction == CFX_BidiChar::NEUTRAL &&
704          eCurrentDirection == CFX_BidiChar::RIGHT)) {
705       eCurrentDirection = CFX_BidiChar::RIGHT;
706       for (int m = segment.start + segment.count; m > segment.start; --m)
707         AddCharInfoByRLDirection(str[m - 1], m_TempCharList[m - 1]);
708     } else {
709       eCurrentDirection = CFX_BidiChar::LEFT;
710       for (int m = segment.start; m < segment.start + segment.count; ++m)
711         AddCharInfoByLRDirection(str[m], m_TempCharList[m]);
712     }
713   }
714   m_TempCharList.clear();
715   m_TempTextBuf.Delete(0, m_TempTextBuf.GetLength());
716 }
717 
ProcessTextObject(CPDF_TextObject * pTextObj,const CFX_Matrix & formMatrix,const CPDF_PageObjectHolder * pObjList,CPDF_PageObjectHolder::const_iterator ObjPos)718 void CPDF_TextPage::ProcessTextObject(
719     CPDF_TextObject* pTextObj,
720     const CFX_Matrix& formMatrix,
721     const CPDF_PageObjectHolder* pObjList,
722     CPDF_PageObjectHolder::const_iterator ObjPos) {
723   if (fabs(pTextObj->GetRect().Width()) < kSizeEpsilon)
724     return;
725 
726   size_t count = m_LineObj.size();
727   PDFTEXT_Obj Obj;
728   Obj.m_pTextObj = pTextObj;
729   Obj.m_formMatrix = formMatrix;
730   if (count == 0) {
731     m_LineObj.push_back(Obj);
732     return;
733   }
734   if (IsSameAsPreTextObject(pTextObj, pObjList, ObjPos))
735     return;
736 
737   PDFTEXT_Obj prev_Obj = m_LineObj[count - 1];
738   size_t nItem = prev_Obj.m_pTextObj->CountItems();
739   if (nItem == 0)
740     return;
741 
742   CPDF_TextObjectItem item;
743   prev_Obj.m_pTextObj->GetItemInfo(nItem - 1, &item);
744   float prev_width =
745       GetCharWidth(item.m_CharCode, prev_Obj.m_pTextObj->GetFont().Get()) *
746       prev_Obj.m_pTextObj->GetFontSize() / 1000;
747 
748   CFX_Matrix prev_matrix =
749       prev_Obj.m_pTextObj->GetTextMatrix() * prev_Obj.m_formMatrix;
750   prev_width = prev_matrix.TransformDistance(fabs(prev_width));
751   pTextObj->GetItemInfo(0, &item);
752   float this_width = GetCharWidth(item.m_CharCode, pTextObj->GetFont().Get()) *
753                      pTextObj->GetFontSize() / 1000;
754   this_width = fabs(this_width);
755 
756   CFX_Matrix this_matrix = pTextObj->GetTextMatrix() * formMatrix;
757   this_width = this_matrix.TransformDistance(fabs(this_width));
758 
759   float threshold = std::max(prev_width, this_width) / 4;
760   CFX_PointF prev_pos = m_DisplayMatrix.Transform(
761       prev_Obj.m_formMatrix.Transform(prev_Obj.m_pTextObj->GetPos()));
762   CFX_PointF this_pos =
763       m_DisplayMatrix.Transform(formMatrix.Transform(pTextObj->GetPos()));
764   if (fabs(this_pos.y - prev_pos.y) > threshold * 2) {
765     for (size_t i = 0; i < count; ++i)
766       ProcessTextObject(m_LineObj[i]);
767     m_LineObj.clear();
768     m_LineObj.push_back(Obj);
769     return;
770   }
771 
772   for (size_t i = count; i > 0; --i) {
773     PDFTEXT_Obj prev_text_obj = m_LineObj[i - 1];
774     CFX_PointF new_prev_pos =
775         m_DisplayMatrix.Transform(prev_text_obj.m_formMatrix.Transform(
776             prev_text_obj.m_pTextObj->GetPos()));
777     if (this_pos.x >= new_prev_pos.x) {
778       m_LineObj.insert(m_LineObj.begin() + i, Obj);
779       return;
780     }
781   }
782   m_LineObj.insert(m_LineObj.begin(), Obj);
783 }
784 
PreMarkedContent(PDFTEXT_Obj Obj)785 CPDF_TextPage::MarkedContentState CPDF_TextPage::PreMarkedContent(
786     PDFTEXT_Obj Obj) {
787   CPDF_TextObject* pTextObj = Obj.m_pTextObj.Get();
788   size_t nContentMarks = pTextObj->m_ContentMarks.CountItems();
789   if (nContentMarks == 0)
790     return MarkedContentState::kPass;
791 
792   WideString actText;
793   bool bExist = false;
794   const CPDF_Dictionary* pDict = nullptr;
795   for (size_t i = 0; i < nContentMarks; ++i) {
796     const CPDF_ContentMarkItem* item = pTextObj->m_ContentMarks.GetItem(i);
797     pDict = item->GetParam();
798     if (!pDict)
799       continue;
800     const CPDF_String* temp = ToString(pDict->GetObjectFor("ActualText"));
801     if (temp) {
802       bExist = true;
803       actText = temp->GetUnicodeText();
804     }
805   }
806   if (!bExist)
807     return MarkedContentState::kPass;
808 
809   if (m_pPrevTextObj) {
810     const CPDF_ContentMarks& marks = m_pPrevTextObj->m_ContentMarks;
811     if (marks.CountItems() == nContentMarks &&
812         marks.GetItem(nContentMarks - 1)->GetParam() == pDict) {
813       return MarkedContentState::kDone;
814     }
815   }
816 
817   if (actText.IsEmpty())
818     return MarkedContentState::kPass;
819 
820   RetainPtr<CPDF_Font> pFont = pTextObj->GetFont();
821   bExist = false;
822   for (size_t i = 0; i < actText.GetLength(); ++i) {
823     if (pFont->CharCodeFromUnicode(actText[i]) != CPDF_Font::kInvalidCharCode) {
824       bExist = true;
825       break;
826     }
827   }
828   if (!bExist)
829     return MarkedContentState::kPass;
830 
831   bExist = false;
832   for (size_t i = 0; i < actText.GetLength(); ++i) {
833     wchar_t wChar = actText[i];
834     if ((wChar > 0x80 && wChar < 0xFFFD) || (wChar <= 0x80 && isprint(wChar))) {
835       bExist = true;
836       break;
837     }
838   }
839   if (!bExist)
840     return MarkedContentState::kDone;
841 
842   return MarkedContentState::kDelay;
843 }
844 
ProcessMarkedContent(PDFTEXT_Obj Obj)845 void CPDF_TextPage::ProcessMarkedContent(PDFTEXT_Obj Obj) {
846   CPDF_TextObject* pTextObj = Obj.m_pTextObj.Get();
847 
848   size_t nContentMarks = pTextObj->m_ContentMarks.CountItems();
849   if (nContentMarks == 0)
850     return;
851 
852   WideString actText;
853   for (size_t n = 0; n < nContentMarks; ++n) {
854     const CPDF_ContentMarkItem* item = pTextObj->m_ContentMarks.GetItem(n);
855     const CPDF_Dictionary* pDict = item->GetParam();
856     if (pDict)
857       actText = pDict->GetUnicodeTextFor("ActualText");
858   }
859   if (actText.IsEmpty())
860     return;
861 
862   RetainPtr<CPDF_Font> pFont = pTextObj->GetFont();
863   CFX_Matrix matrix = pTextObj->GetTextMatrix() * Obj.m_formMatrix;
864 
865   for (size_t k = 0; k < actText.GetLength(); ++k) {
866     wchar_t wChar = actText[k];
867     if (wChar <= 0x80 && !isprint(wChar))
868       wChar = 0x20;
869     if (wChar >= 0xFFFD)
870       continue;
871 
872     CharInfo charinfo;
873     charinfo.m_Origin = pTextObj->GetPos();
874     charinfo.m_Index = m_TextBuf.GetLength();
875     charinfo.m_Unicode = wChar;
876     charinfo.m_CharCode = pFont->CharCodeFromUnicode(wChar);
877     charinfo.m_CharType = CPDF_TextPage::CharType::kPiece;
878     charinfo.m_pTextObj = pTextObj;
879     charinfo.m_CharBox = pTextObj->GetRect();
880     charinfo.m_Matrix = matrix;
881     m_TempTextBuf.AppendChar(wChar);
882     m_TempCharList.push_back(charinfo);
883   }
884 }
885 
FindPreviousTextObject()886 void CPDF_TextPage::FindPreviousTextObject() {
887   const CharInfo* pPrevCharInfo = GetPrevCharInfo();
888   if (!pPrevCharInfo)
889     return;
890 
891   if (pPrevCharInfo->m_pTextObj)
892     m_pPrevTextObj = pPrevCharInfo->m_pTextObj;
893 }
894 
SwapTempTextBuf(int32_t iCharListStartAppend,int32_t iBufStartAppend)895 void CPDF_TextPage::SwapTempTextBuf(int32_t iCharListStartAppend,
896                                     int32_t iBufStartAppend) {
897   int32_t i = iCharListStartAppend;
898   int32_t j = pdfium::CollectionSize<int32_t>(m_TempCharList) - 1;
899   for (; i < j; ++i, --j) {
900     std::swap(m_TempCharList[i], m_TempCharList[j]);
901     std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index);
902   }
903   wchar_t* pTempBuffer = m_TempTextBuf.GetBuffer();
904   i = iBufStartAppend;
905   j = m_TempTextBuf.GetLength() - 1;
906   for (; i < j; ++i, --j)
907     std::swap(pTempBuffer[i], pTempBuffer[j]);
908 }
909 
ProcessTextObject(PDFTEXT_Obj Obj)910 void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) {
911   CPDF_TextObject* pTextObj = Obj.m_pTextObj.Get();
912   if (fabs(pTextObj->GetRect().Width()) < kSizeEpsilon)
913     return;
914 
915   CFX_Matrix formMatrix = Obj.m_formMatrix;
916   RetainPtr<CPDF_Font> pFont = pTextObj->GetFont();
917   CFX_Matrix matrix = pTextObj->GetTextMatrix() * formMatrix;
918   MarkedContentState ePreMKC = PreMarkedContent(Obj);
919   if (ePreMKC == MarkedContentState::kDone) {
920     m_pPrevTextObj = pTextObj;
921     m_PrevMatrix = formMatrix;
922     return;
923   }
924   GenerateCharacter result = GenerateCharacter::kNone;
925   if (m_pPrevTextObj) {
926     result = ProcessInsertObject(pTextObj, formMatrix);
927     if (result == GenerateCharacter::kLineBreak)
928       m_CurlineRect = Obj.m_pTextObj->GetRect();
929     else
930       m_CurlineRect.Union(Obj.m_pTextObj->GetRect());
931 
932     switch (result) {
933       case GenerateCharacter::kNone:
934         break;
935       case GenerateCharacter::kSpace: {
936         Optional<CharInfo> pGenerateChar = GenerateCharInfo(L' ');
937         if (pGenerateChar) {
938           if (!formMatrix.IsIdentity())
939             pGenerateChar->m_Matrix = formMatrix;
940           m_TempTextBuf.AppendChar(L' ');
941           m_TempCharList.push_back(*pGenerateChar);
942         }
943         break;
944       }
945       case GenerateCharacter::kLineBreak:
946         CloseTempLine();
947         if (m_TextBuf.GetSize()) {
948           AppendGeneratedCharacter(L'\r', formMatrix);
949           AppendGeneratedCharacter(L'\n', formMatrix);
950         }
951         break;
952       case GenerateCharacter::kHyphen:
953         if (pTextObj->CountChars() == 1) {
954           CPDF_TextObjectItem item;
955           pTextObj->GetCharInfo(0, &item);
956           WideString wstrItem =
957               pTextObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
958           if (wstrItem.IsEmpty())
959             wstrItem += (wchar_t)item.m_CharCode;
960           wchar_t curChar = wstrItem[0];
961           if (IsHyphenCode(curChar))
962             return;
963         }
964         while (m_TempTextBuf.GetSize() > 0 &&
965                m_TempTextBuf.AsStringView().Back() == 0x20) {
966           m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
967           m_TempCharList.pop_back();
968         }
969         CharInfo* charinfo = &m_TempCharList.back();
970         m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
971         charinfo->m_Unicode = 0x2;
972         charinfo->m_CharType = CPDF_TextPage::CharType::kHyphen;
973         m_TempTextBuf.AppendChar(0xfffe);
974         break;
975     }
976   } else {
977     m_CurlineRect = Obj.m_pTextObj->GetRect();
978   }
979 
980   if (ePreMKC == MarkedContentState::kDelay) {
981     ProcessMarkedContent(Obj);
982     m_pPrevTextObj = pTextObj;
983     m_PrevMatrix = formMatrix;
984     return;
985   }
986   m_pPrevTextObj = pTextObj;
987   m_PrevMatrix = formMatrix;
988   float baseSpace = CalculateBaseSpace(pTextObj, matrix);
989 
990   const bool bR2L = IsRightToLeft(*pTextObj, *pFont);
991   const bool bIsBidiAndMirrorInverse =
992       bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0;
993   int32_t iBufStartAppend = m_TempTextBuf.GetLength();
994   int32_t iCharListStartAppend =
995       pdfium::CollectionSize<int32_t>(m_TempCharList);
996 
997   float spacing = 0;
998   const size_t nItems = pTextObj->CountItems();
999   for (size_t i = 0; i < nItems; ++i) {
1000     CPDF_TextObjectItem item;
1001     CharInfo charinfo;
1002     pTextObj->GetItemInfo(i, &item);
1003     if (item.m_CharCode == 0xffffffff) {
1004       WideString str = m_TempTextBuf.MakeString();
1005       if (str.IsEmpty())
1006         str = m_TextBuf.AsStringView();
1007       if (str.IsEmpty() || str.Back() == L' ')
1008         continue;
1009 
1010       float fontsize_h = pTextObj->m_TextState.GetFontSizeH();
1011       spacing = -fontsize_h * item.m_Origin.x / 1000;
1012       continue;
1013     }
1014     float charSpace = pTextObj->m_TextState.GetCharSpace();
1015     if (charSpace > 0.001)
1016       spacing += matrix.TransformDistance(charSpace);
1017     else if (charSpace < -0.001)
1018       spacing -= matrix.TransformDistance(fabs(charSpace));
1019     spacing -= baseSpace;
1020     if (spacing && i > 0) {
1021       float fontsize_h = pTextObj->m_TextState.GetFontSizeH();
1022       uint32_t space_charcode = pFont->CharCodeFromUnicode(' ');
1023       float threshold = 0;
1024       if (space_charcode != CPDF_Font::kInvalidCharCode)
1025         threshold = fontsize_h * pFont->GetCharWidthF(space_charcode) / 1000;
1026       if (threshold > fontsize_h / 3)
1027         threshold = 0;
1028       else
1029         threshold /= 2;
1030       if (threshold == 0) {
1031         threshold = GetCharWidth(item.m_CharCode, pFont.Get());
1032         threshold = NormalizeThreshold(threshold, 300, 500, 700);
1033         threshold = fontsize_h * threshold / 1000;
1034       }
1035       if (threshold && (spacing && spacing >= threshold)) {
1036         charinfo.m_Unicode = L' ';
1037         charinfo.m_CharType = CPDF_TextPage::CharType::kGenerated;
1038         charinfo.m_pTextObj = pTextObj;
1039         charinfo.m_Index = m_TextBuf.GetLength();
1040         m_TempTextBuf.AppendChar(L' ');
1041         charinfo.m_CharCode = CPDF_Font::kInvalidCharCode;
1042         charinfo.m_Matrix = formMatrix;
1043         charinfo.m_Origin = matrix.Transform(item.m_Origin);
1044         charinfo.m_CharBox =
1045             CFX_FloatRect(charinfo.m_Origin.x, charinfo.m_Origin.y,
1046                           charinfo.m_Origin.x, charinfo.m_Origin.y);
1047         m_TempCharList.push_back(charinfo);
1048       }
1049       if (item.m_CharCode == CPDF_Font::kInvalidCharCode)
1050         continue;
1051     }
1052     spacing = 0;
1053     WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode);
1054     bool bNoUnicode = false;
1055     if (wstrItem.IsEmpty() && item.m_CharCode) {
1056       wstrItem += static_cast<wchar_t>(item.m_CharCode);
1057       bNoUnicode = true;
1058     }
1059     charinfo.m_Index = -1;
1060     charinfo.m_CharCode = item.m_CharCode;
1061     charinfo.m_CharType = bNoUnicode ? CPDF_TextPage::CharType::kNotUnicode
1062                                      : CPDF_TextPage::CharType::kNormal;
1063     charinfo.m_pTextObj = pTextObj;
1064     charinfo.m_Origin = matrix.Transform(item.m_Origin);
1065 
1066     const FX_RECT rect =
1067         charinfo.m_pTextObj->GetFont()->GetCharBBox(charinfo.m_CharCode);
1068     const float fFontSize = pTextObj->GetFontSize() / 1000;
1069     charinfo.m_CharBox.top = rect.top * fFontSize + item.m_Origin.y;
1070     charinfo.m_CharBox.left = rect.left * fFontSize + item.m_Origin.x;
1071     charinfo.m_CharBox.right = rect.right * fFontSize + item.m_Origin.x;
1072     charinfo.m_CharBox.bottom = rect.bottom * fFontSize + item.m_Origin.y;
1073     if (fabsf(charinfo.m_CharBox.top - charinfo.m_CharBox.bottom) <
1074         kSizeEpsilon) {
1075       charinfo.m_CharBox.top =
1076           charinfo.m_CharBox.bottom + pTextObj->GetFontSize();
1077     }
1078     if (fabsf(charinfo.m_CharBox.right - charinfo.m_CharBox.left) <
1079         kSizeEpsilon) {
1080       charinfo.m_CharBox.right =
1081           charinfo.m_CharBox.left + pTextObj->GetCharWidth(charinfo.m_CharCode);
1082     }
1083     charinfo.m_CharBox = matrix.TransformRect(charinfo.m_CharBox);
1084     charinfo.m_Matrix = matrix;
1085     if (wstrItem.IsEmpty()) {
1086       charinfo.m_Unicode = 0;
1087       m_TempCharList.push_back(charinfo);
1088       m_TempTextBuf.AppendChar(0xfffe);
1089       continue;
1090     }
1091     int nTotal = wstrItem.GetLength();
1092     bool bDel = false;
1093     const int count = std::min(pdfium::CollectionSize<int>(m_TempCharList), 7);
1094     constexpr float kTextCharRatioGapDelta = 0.07f;
1095     float threshold = charinfo.m_Matrix.TransformXDistance(
1096         kTextCharRatioGapDelta * pTextObj->GetFontSize());
1097     for (int n = pdfium::CollectionSize<int>(m_TempCharList);
1098          n > pdfium::CollectionSize<int>(m_TempCharList) - count; --n) {
1099       const CharInfo& charinfo1 = m_TempCharList[n - 1];
1100       CFX_PointF diff = charinfo1.m_Origin - charinfo.m_Origin;
1101       if (charinfo1.m_CharCode == charinfo.m_CharCode &&
1102           charinfo1.m_pTextObj->GetFont() == charinfo.m_pTextObj->GetFont() &&
1103           fabs(diff.x) < threshold && fabs(diff.y) < threshold) {
1104         bDel = true;
1105         break;
1106       }
1107     }
1108     if (!bDel) {
1109       for (int nIndex = 0; nIndex < nTotal; ++nIndex) {
1110         charinfo.m_Unicode = wstrItem[nIndex];
1111         if (charinfo.m_Unicode) {
1112           charinfo.m_Index = m_TextBuf.GetLength();
1113           m_TempTextBuf.AppendChar(charinfo.m_Unicode);
1114         } else {
1115           m_TempTextBuf.AppendChar(0xfffe);
1116         }
1117         m_TempCharList.push_back(charinfo);
1118       }
1119     } else if (i == 0) {
1120       WideString str = m_TempTextBuf.MakeString();
1121       if (!str.IsEmpty() && str.Back() == L' ') {
1122         m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1123         m_TempCharList.pop_back();
1124       }
1125     }
1126   }
1127   if (bIsBidiAndMirrorInverse)
1128     SwapTempTextBuf(iCharListStartAppend, iBufStartAppend);
1129 }
1130 
GetTextObjectWritingMode(const CPDF_TextObject * pTextObj) const1131 CPDF_TextPage::TextOrientation CPDF_TextPage::GetTextObjectWritingMode(
1132     const CPDF_TextObject* pTextObj) const {
1133   size_t nChars = pTextObj->CountChars();
1134   if (nChars <= 1)
1135     return m_TextlineDir;
1136 
1137   CPDF_TextObjectItem first, last;
1138   pTextObj->GetCharInfo(0, &first);
1139   pTextObj->GetCharInfo(nChars - 1, &last);
1140 
1141   CFX_Matrix textMatrix = pTextObj->GetTextMatrix();
1142   first.m_Origin = textMatrix.Transform(first.m_Origin);
1143   last.m_Origin = textMatrix.Transform(last.m_Origin);
1144 
1145   static constexpr float kEpsilon = 0.0001f;
1146   float dX = fabs(last.m_Origin.x - first.m_Origin.x);
1147   float dY = fabs(last.m_Origin.y - first.m_Origin.y);
1148   if (dX <= kEpsilon && dY <= kEpsilon)
1149     return TextOrientation::kUnknown;
1150 
1151   static constexpr float kThreshold = 0.0872f;
1152   CFX_VectorF v(dX, dY);
1153   v.Normalize();
1154   bool bXUnderThreshold = v.x <= kThreshold;
1155   if (v.y <= kThreshold)
1156     return bXUnderThreshold ? m_TextlineDir : TextOrientation::kHorizontal;
1157   return bXUnderThreshold ? TextOrientation::kVertical : m_TextlineDir;
1158 }
1159 
IsHyphen(wchar_t curChar) const1160 bool CPDF_TextPage::IsHyphen(wchar_t curChar) const {
1161   WideStringView curText = m_TempTextBuf.AsStringView();
1162   if (curText.IsEmpty())
1163     curText = m_TextBuf.AsStringView();
1164 
1165   if (curText.IsEmpty())
1166     return false;
1167 
1168   auto iter = curText.rbegin();
1169   for (; (iter + 1) != curText.rend() && *iter == 0x20; ++iter) {
1170     // Do nothing
1171   }
1172 
1173   if (!IsHyphenCode(*iter))
1174     return false;
1175 
1176   if ((iter + 1) != curText.rend()) {
1177     iter++;
1178     if (FXSYS_iswalpha(*iter) && FXSYS_iswalnum(curChar))
1179       return true;
1180   }
1181 
1182   const CharInfo* pPrevCharInfo = GetPrevCharInfo();
1183   return pPrevCharInfo &&
1184          pPrevCharInfo->m_CharType == CPDF_TextPage::CharType::kPiece &&
1185          IsHyphenCode(pPrevCharInfo->m_Unicode);
1186 }
1187 
GetPrevCharInfo() const1188 const CPDF_TextPage::CharInfo* CPDF_TextPage::GetPrevCharInfo() const {
1189   if (!m_TempCharList.empty())
1190     return &m_TempCharList.back();
1191   return !m_CharList.empty() ? &m_CharList.back() : nullptr;
1192 }
1193 
ProcessInsertObject(const CPDF_TextObject * pObj,const CFX_Matrix & formMatrix)1194 CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject(
1195     const CPDF_TextObject* pObj,
1196     const CFX_Matrix& formMatrix) {
1197   FindPreviousTextObject();
1198   TextOrientation WritingMode = GetTextObjectWritingMode(pObj);
1199   if (WritingMode == TextOrientation::kUnknown)
1200     WritingMode = GetTextObjectWritingMode(m_pPrevTextObj.Get());
1201 
1202   size_t nItem = m_pPrevTextObj->CountItems();
1203   if (nItem == 0)
1204     return GenerateCharacter::kNone;
1205 
1206   CPDF_TextObjectItem PrevItem;
1207   m_pPrevTextObj->GetItemInfo(nItem - 1, &PrevItem);
1208 
1209   CPDF_TextObjectItem item;
1210   pObj->GetItemInfo(0, &item);
1211 
1212   const CFX_FloatRect& this_rect = pObj->GetRect();
1213   const CFX_FloatRect& prev_rect = m_pPrevTextObj->GetRect();
1214 
1215   WideString wstrItem = pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
1216   if (wstrItem.IsEmpty())
1217     wstrItem += static_cast<wchar_t>(item.m_CharCode);
1218   wchar_t curChar = wstrItem[0];
1219   if (WritingMode == TextOrientation::kHorizontal) {
1220     if (EndHorizontalLine(this_rect, prev_rect)) {
1221       return IsHyphen(curChar) ? GenerateCharacter::kHyphen
1222                                : GenerateCharacter::kLineBreak;
1223     }
1224   } else if (WritingMode == TextOrientation::kVertical) {
1225     if (EndVerticalLine(this_rect, prev_rect, m_CurlineRect,
1226                         pObj->GetFontSize(), m_pPrevTextObj->GetFontSize())) {
1227       return IsHyphen(curChar) ? GenerateCharacter::kHyphen
1228                                : GenerateCharacter::kLineBreak;
1229     }
1230   }
1231 
1232   float last_pos = PrevItem.m_Origin.x;
1233   uint32_t nLastWidth =
1234       GetCharWidth(PrevItem.m_CharCode, m_pPrevTextObj->GetFont().Get());
1235   float last_width = nLastWidth * m_pPrevTextObj->GetFontSize() / 1000;
1236   last_width = fabs(last_width);
1237   uint32_t nThisWidth = GetCharWidth(item.m_CharCode, pObj->GetFont().Get());
1238   float this_width = fabs(nThisWidth * pObj->GetFontSize() / 1000);
1239   float threshold = std::max(last_width, this_width) / 4;
1240 
1241   CFX_Matrix prev_matrix = m_pPrevTextObj->GetTextMatrix() * m_PrevMatrix;
1242   CFX_Matrix prev_reverse = prev_matrix.GetInverse();
1243 
1244   CFX_PointF pos = prev_reverse.Transform(formMatrix.Transform(pObj->GetPos()));
1245   if (last_width < this_width)
1246     threshold = prev_reverse.TransformDistance(threshold);
1247 
1248   bool bNewline = false;
1249   if (WritingMode == TextOrientation::kHorizontal) {
1250     CFX_FloatRect rect = m_pPrevTextObj->GetRect();
1251     float rect_height = rect.Height();
1252     rect.Normalize();
1253     if ((rect.IsEmpty() && rect_height > 5) ||
1254         ((pos.y > threshold * 2 || pos.y < threshold * -3) &&
1255          (fabs(pos.y) >= 1 || fabs(pos.y) > fabs(pos.x)))) {
1256       bNewline = true;
1257       if (nItem > 1) {
1258         CPDF_TextObjectItem tempItem;
1259         m_pPrevTextObj->GetItemInfo(0, &tempItem);
1260         CFX_Matrix m = m_pPrevTextObj->GetTextMatrix();
1261         if (PrevItem.m_Origin.x > tempItem.m_Origin.x &&
1262             m_DisplayMatrix.a > 0.9 && m_DisplayMatrix.b < 0.1 &&
1263             m_DisplayMatrix.c < 0.1 && m_DisplayMatrix.d < -0.9 && m.b < 0.1 &&
1264             m.c < 0.1) {
1265           CFX_FloatRect re(0, m_pPrevTextObj->GetRect().bottom, 1000,
1266                            m_pPrevTextObj->GetRect().top);
1267           if (re.Contains(pObj->GetPos())) {
1268             bNewline = false;
1269           } else {
1270             if (CFX_FloatRect(0, pObj->GetRect().bottom, 1000,
1271                               pObj->GetRect().top)
1272                     .Contains(m_pPrevTextObj->GetPos())) {
1273               bNewline = false;
1274             }
1275           }
1276         }
1277       }
1278     }
1279   }
1280   if (bNewline) {
1281     return IsHyphen(curChar) ? GenerateCharacter::kHyphen
1282                              : GenerateCharacter::kLineBreak;
1283   }
1284 
1285   if (pObj->CountChars() == 1 && IsHyphenCode(curChar) && IsHyphen(curChar))
1286     return GenerateCharacter::kHyphen;
1287 
1288   if (curChar == L' ')
1289     return GenerateCharacter::kNone;
1290 
1291   WideString PrevStr =
1292       m_pPrevTextObj->GetFont()->UnicodeFromCharCode(PrevItem.m_CharCode);
1293   wchar_t preChar = PrevStr.Back();
1294   if (preChar == L' ')
1295     return GenerateCharacter::kNone;
1296 
1297   CFX_Matrix matrix = pObj->GetTextMatrix() * formMatrix;
1298   float threshold2 = std::max(nLastWidth, nThisWidth);
1299   threshold2 = NormalizeThreshold(threshold2, 400, 700, 800);
1300   if (nLastWidth >= nThisWidth) {
1301     threshold2 *= fabs(m_pPrevTextObj->GetFontSize());
1302   } else {
1303     threshold2 *= fabs(pObj->GetFontSize());
1304     threshold2 = matrix.TransformDistance(threshold2);
1305     threshold2 = prev_reverse.TransformDistance(threshold2);
1306   }
1307   threshold2 /= 1000;
1308   if ((threshold2 < 1.4881 && threshold2 > 1.4879) ||
1309       (threshold2 < 1.39001 && threshold2 > 1.38999)) {
1310     threshold2 *= 1.5;
1311   }
1312   return GenerateSpace(pos, last_pos, this_width, last_width, threshold2)
1313              ? GenerateCharacter::kSpace
1314              : GenerateCharacter::kNone;
1315 }
1316 
IsSameTextObject(CPDF_TextObject * pTextObj1,CPDF_TextObject * pTextObj2) const1317 bool CPDF_TextPage::IsSameTextObject(CPDF_TextObject* pTextObj1,
1318                                      CPDF_TextObject* pTextObj2) const {
1319   if (!pTextObj1 || !pTextObj2)
1320     return false;
1321 
1322   CFX_FloatRect rcPreObj = pTextObj2->GetRect();
1323   const CFX_FloatRect& rcCurObj = pTextObj1->GetRect();
1324   if (rcPreObj.IsEmpty() && rcCurObj.IsEmpty()) {
1325     float dbXdif = fabs(rcPreObj.left - rcCurObj.left);
1326     size_t nCount = m_CharList.size();
1327     if (nCount >= 2) {
1328       float dbSpace = m_CharList[nCount - 2].m_CharBox.Width();
1329       if (dbXdif > dbSpace)
1330         return false;
1331     }
1332   }
1333   if (!rcPreObj.IsEmpty() || !rcCurObj.IsEmpty()) {
1334     rcPreObj.Intersect(rcCurObj);
1335     if (rcPreObj.IsEmpty())
1336       return false;
1337     if (fabs(rcPreObj.Width() - rcCurObj.Width()) > rcCurObj.Width() / 2) {
1338       return false;
1339     }
1340     if (pTextObj2->GetFontSize() != pTextObj1->GetFontSize())
1341       return false;
1342   }
1343 
1344   size_t nPreCount = pTextObj2->CountItems();
1345   if (nPreCount != pTextObj1->CountItems())
1346     return false;
1347 
1348   // If both objects have no items, consider them same.
1349   if (nPreCount == 0)
1350     return true;
1351 
1352   CPDF_TextObjectItem itemPer;
1353   CPDF_TextObjectItem itemCur;
1354   for (size_t i = 0; i < nPreCount; ++i) {
1355     pTextObj2->GetItemInfo(i, &itemPer);
1356     pTextObj1->GetItemInfo(i, &itemCur);
1357     if (itemCur.m_CharCode != itemPer.m_CharCode)
1358       return false;
1359   }
1360 
1361   CFX_PointF diff = pTextObj1->GetPos() - pTextObj2->GetPos();
1362   float font_size = pTextObj2->GetFontSize();
1363   float char_size =
1364       GetCharWidth(itemPer.m_CharCode, pTextObj2->GetFont().Get());
1365   float max_pre_size =
1366       std::max(std::max(rcPreObj.Height(), rcPreObj.Width()), font_size);
1367   return fabs(diff.x) <= 0.9 * char_size * font_size / 1000 &&
1368          fabs(diff.y) <= max_pre_size / 8;
1369 }
1370 
IsSameAsPreTextObject(CPDF_TextObject * pTextObj,const CPDF_PageObjectHolder * pObjList,CPDF_PageObjectHolder::const_iterator iter) const1371 bool CPDF_TextPage::IsSameAsPreTextObject(
1372     CPDF_TextObject* pTextObj,
1373     const CPDF_PageObjectHolder* pObjList,
1374     CPDF_PageObjectHolder::const_iterator iter) const {
1375   int i = 0;
1376   while (i < 5 && iter != pObjList->begin()) {
1377     --iter;
1378     CPDF_PageObject* pOtherObj = iter->get();
1379     if (pOtherObj == pTextObj || !pOtherObj->IsText())
1380       continue;
1381     if (IsSameTextObject(pOtherObj->AsText(), pTextObj))
1382       return true;
1383     ++i;
1384   }
1385   return false;
1386 }
1387 
GenerateCharInfo(wchar_t unicode)1388 Optional<CPDF_TextPage::CharInfo> CPDF_TextPage::GenerateCharInfo(
1389     wchar_t unicode) {
1390   const CharInfo* pPrevCharInfo = GetPrevCharInfo();
1391   if (!pPrevCharInfo)
1392     return {};
1393 
1394   CharInfo info;
1395   info.m_Index = m_TextBuf.GetLength();
1396   info.m_CharCode = CPDF_Font::kInvalidCharCode;
1397   info.m_Unicode = unicode;
1398   info.m_CharType = CPDF_TextPage::CharType::kGenerated;
1399 
1400   int preWidth = 0;
1401   if (pPrevCharInfo->m_pTextObj &&
1402       pPrevCharInfo->m_CharCode != CPDF_Font::kInvalidCharCode) {
1403     preWidth = GetCharWidth(pPrevCharInfo->m_CharCode,
1404                             pPrevCharInfo->m_pTextObj->GetFont().Get());
1405   }
1406 
1407   float fFontSize = pPrevCharInfo->m_pTextObj
1408                         ? pPrevCharInfo->m_pTextObj->GetFontSize()
1409                         : pPrevCharInfo->m_CharBox.Height();
1410   if (!fFontSize)
1411     fFontSize = kDefaultFontSize;
1412 
1413   info.m_Origin =
1414       CFX_PointF(pPrevCharInfo->m_Origin.x + preWidth * (fFontSize) / 1000,
1415                  pPrevCharInfo->m_Origin.y);
1416   info.m_CharBox = CFX_FloatRect(info.m_Origin.x, info.m_Origin.y,
1417                                  info.m_Origin.x, info.m_Origin.y);
1418   return info;
1419 }
1420