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