7#include "core/fpdftext/cpdf_textpage.h"
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"
36constexpr float kDefaultFontSize = 1.0f;
37constexpr float kSizeEpsilon = 0.01f;
39const uint16_t*
const kUnicodeDataNormalizationMaps[] = {
43float NormalizeThreshold(
float threshold,
int t1,
int t2,
int t3) {
47 return threshold / 2.0f;
49 return threshold / 4.0f;
51 return threshold / 5.0f;
52 return threshold / 6.0f;
55float CalculateBaseSpace(
const CPDF_TextObject* pTextObj,
57 const size_t nItems = pTextObj->CountItems();
58 if (!pTextObj
->text_state().GetCharSpace() || nItems < 3) {
65 float baseSpace = spacing;
66 for (size_t i = 0; i < nItems; ++i) {
67 CPDF_TextObject::
Item item = pTextObj->GetItemInfo(i);
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);
75 if (baseSpace < 0.0 || (nItems == 3 && !bAllChar))
81DataVector<
wchar_t> GetUnicodeNormalization(
wchar_t wch) {
85 return DataVector<
wchar_t>(1, wch);
87 if (wFind >= 0x8000) {
88 return DataVector<
wchar_t>(1,
94 const uint16_t* pMap = kUnicodeDataNormalizationMaps[wFind - 2] + wch;
96 wFind =
static_cast<
wchar_t>(*pMap++);
98 return DataVector<
wchar_t>(pMap, pMap + wFind);
101float MaskPercentFilled(
const std::vector<
bool>& mask,
106 float count =
std::count_if(mask.begin() + start, mask.begin() + end,
107 [](
bool r) {
return r; });
108 return count / (end - start);
127bool IsHyphenCode(
wchar_t c) {
128 return c == 0x2D || c == 0xAD;
137bool IsRightToLeft(
const CPDF_TextObject& text_obj,
const CPDF_Font& font) {
138 const size_t nItems = text_obj.CountItems();
141 for (size_t i = 0; i < nItems; ++i) {
142 CPDF_TextObject::
Item item = text_obj.GetItemInfo(i);
156int GetCharWidth(uint32_t charCode,
CPDF_Font* pFont) {
177bool GenerateSpace(
const CFX_PointF& pos,
182 if (fabs(last_pos + last_width - pos.x) <= threshold)
185 float threshold_pos = threshold + last_width;
186 float pos_difference = pos.x - last_pos;
187 if (fabs(pos_difference) > threshold_pos)
189 if (pos.x < 0 && -threshold_pos > pos_difference)
191 return pos_difference > this_width + last_width;
199 float top =
std::min(this_rect
.top, prev_rect
.top);
201 return bottom >= top;
208 float prev_fontsize) {
209 if (this_rect
.Width() <= this_fontsize * 0.1f ||
210 prev_rect
.Width() <= prev_fontsize * 0.1f) {
216 return right <= left;
219CFX_Matrix GetPageMatrix(
const CPDF_Page* pPage) {
225float GetFontSize(
const CPDF_TextObject* text_object) {
226 bool has_font = text_object && text_object
->GetFont();
231 float font_size = GetFontSize(charinfo.m_pTextObj);
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();
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;
242 double height = vert_width * font_size / 1000.0;
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;
251 int ascent = charinfo.m_pTextObj->GetFont()->GetTypeAscent();
252 int descent = charinfo.m_pTextObj->GetFont()->GetTypeDescent();
253 if (ascent != descent) {
255 charinfo.m_pTextObj->GetCharWidth(charinfo
.m_CharCode);
256 float font_scale = charinfo
.m_Matrix.a * font_size / (ascent - descent);
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;
272CPDF_TextPage::TransformedTextObject::TransformedTextObject() =
default;
275 const TransformedTextObject& that) =
default;
277CPDF_TextPage::TransformedTextObject::~TransformedTextObject() =
default;
286 :
m_pPage(
pPage), m_rtl(rtl), m_DisplayMatrix(GetPageMatrix(pPage)) {
293 m_TextBuf.SetAllocStep(10240);
298 m_CharIndices.push_back({0, 0});
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)) ||
306 m_CharIndices.back().count++;
310 m_CharIndices.push_back({i + 1, 0});
313 m_CharIndices.back().index = i + 1;
320 return fxcrt::CollectionSize<
int>(m_CharList);
325 for (
const auto& info : m_CharIndices) {
327 if (count > text_index)
328 return text_index - count + info.count + info.index;
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;
347 std::vector<CFX_FloatRect> rects;
348 if (start < 0 || count == 0)
352 if (start >= number_of_chars)
355 if (count < 0 || start + count > number_of_chars)
356 count = number_of_chars - start;
359 const CPDF_TextObject* text_object =
nullptr;
362 bool is_new_rect =
true;
364 const CharInfo& charinfo = m_CharList[pos++];
372 text_object = charinfo.m_pTextObj;
373 if (text_object != charinfo.m_pTextObj) {
374 rects.push_back(rect);
375 text_object = charinfo.m_pTextObj;
386 rects.push_back(rect);
391 const CFX_SizeF& tolerance)
const {
397 for (pos = 0; pos < nCount; ++pos) {
398 const CFX_FloatRect& orig_charrect = m_CharList[pos].m_CharBox;
402 if (tolerance.width <= 0 && tolerance.height <= 0)
408 charrect
.bottom - tolerance.height / 2,
409 charrect
.right + tolerance.width / 2,
410 charrect
.top + tolerance.height / 2);
415 std::min(fabs(point.x - charrect
.left), fabs(point.x - charrect
.right));
417 std::min(fabs(point.y - charrect
.bottom), fabs(point.y - charrect
.top));
418 if (curYdif + curXdif < xdif + ydif) {
424 return pos < nCount ? pos : NearPos;
428 const std::function<
bool(
const CharInfo&)>& predicate)
const {
430 bool IsContainPreChar =
false;
431 bool IsAddLineFeed =
false;
433 for (
const auto& charinfo : m_CharList) {
434 if (predicate(charinfo)) {
435 if (fabs(posy - charinfo.m_Origin.y) > 0 && !IsContainPreChar &&
437 posy = charinfo.m_Origin.y;
438 if (!strText.IsEmpty())
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) {
448 IsContainPreChar =
false;
449 IsAddLineFeed =
false;
452 IsContainPreChar =
false;
453 IsAddLineFeed =
true;
460 return GetTextByPredicate([&rect](
const CharInfo& charinfo) {
461 return IsRectIntersect(rect, charinfo
.m_CharBox);
466 const CPDF_TextObject* pTextObj)
const {
467 return GetTextByPredicate([pTextObj](
const CharInfo& charinfo) {
468 return charinfo.m_pTextObj == pTextObj;
473 CHECK(index < m_CharList.size());
474 return m_CharList[index];
478 CHECK(index < m_CharList.size());
479 return GetFontSize(m_CharList[index].m_pTextObj);
483 return GetLooseBounds(GetCharInfo(index));
487 if (start < 0 || start >= CountChars() || count <= 0 || m_CharList.empty() ||
488 m_TextBuf.IsEmpty()) {
498 while (text_start < 0) {
499 if (start >= count_chars)
505 count =
std::min(count, count_chars - start);
507 int last = start + count - 1;
513 while (text_last < 0) {
514 if (last < text_start)
521 if (text_last < text_start)
524 int text_count = text_last - text_start + 1;
526 return WideString(m_TextBuf.AsStringView().Substr(text_start, text_count));
533 m_SelRects = GetRectArray(start, nCount);
534 return fxcrt::CollectionSize<
int>(m_SelRects);
538 if (!fxcrt::IndexInBounds(m_SelRects, rectIndex))
541 *pRect = m_SelRects[rectIndex];
547 DCHECK_NE(m_pPage->GetPageObjectCount(), 0u);
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;
554 std::vector<
bool> nHorizontalMask(nPageWidth);
555 std::vector<
bool> nVerticalMask(nPageHeight);
556 float fLineHeight = 0.0f;
557 int32_t nStartH = nPageWidth;
559 int32_t nStartV = nPageHeight;
561 for (
const auto& pPageObj : *m_pPage) {
562 if (!pPageObj->IsText())
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)
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;
581 nStartH = std::min(nStartH, minH);
582 nEndH = std::max(nEndH, maxH);
583 nStartV = std::min(nStartV, minV);
584 nEndV = std::max(nEndV, maxV);
586 if (fLineHeight <= 0.0f)
587 fLineHeight = pPageObj->GetRect().Height();
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;
595 const float nSumH = MaskPercentFilled(nHorizontalMask, nStartH, nEndH);
597 return TextOrientation::kHorizontal;
599 const float nSumV = MaskPercentFilled(nVerticalMask, nStartV, nEndV);
601 return TextOrientation::kHorizontal;
603 return TextOrientation::kVertical;
604 return TextOrientation::kUnknown;
609 absl::optional<CharInfo> pGenerateChar = GenerateCharInfo(unicode);
610 if (!pGenerateChar.has_value())
613 m_TextBuf.AppendChar(unicode);
614 if (!formMatrix.IsIdentity())
615 pGenerateChar->m_Matrix = formMatrix;
616 m_CharList.push_back(pGenerateChar.value());
620 if (m_pPage->GetPageObjectCount() == 0)
623 m_TextlineDir = FindTextlineFlowOrientation();
624 for (
auto it = m_pPage->begin(); it != m_pPage->end(); ++it) {
631 ProcessTextObject(pObj->AsText(), matrix, m_pPage, it);
635 for (
const auto& obj : mTextObjects)
636 ProcessTextObject(obj);
638 mTextObjects.clear();
642void CPDF_TextPage::ProcessFormObject(CPDF_FormObject* pFormObj,
646 for (
auto it = pHolder->begin(); it != pHolder->end(); ++it) {
652 ProcessTextObject(pPageObj
->AsText(), curFormMatrix, pHolder, it);
654 ProcessFormObject(pPageObj
->AsForm(), curFormMatrix);
661 if (IsControlChar(info2)) {
663 m_CharList.push_back(info2);
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);
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);
686 if (IsControlChar(info2)) {
688 m_CharList.push_back(info2);
691 info2.m_Index = m_TextBuf.GetLength();
693 DataVector<
wchar_t> normalized = GetUnicodeNormalization(wChar);
694 if (normalized.empty()) {
696 m_TextBuf.AppendChar(info2.m_Unicode);
697 m_CharList.push_back(info2);
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);
709 if (m_TempCharList.empty())
712 WideString str = m_TempTextBuf.MakeString();
713 bool bPrevSpace =
false;
714 for (size_t i = 0; i < str.GetLength(); ++i) {
720 m_TempTextBuf.Delete(i, 1);
721 m_TempCharList.erase(m_TempCharList.begin() + i);
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]);
739 if (segment.direction != CFX_BidiChar::Direction::kLeftWeak) {
740 eCurrentDirection = CFX_BidiChar::Direction::kLeft;
742 for (
int m = segment.start; m < segment.start + segment.count; ++m)
743 AddCharInfoByLRDirection(str[m], m_TempCharList[m]);
746 m_TempCharList.clear();
747 m_TempTextBuf.Delete(0, m_TempTextBuf.GetLength());
751 CPDF_TextObject* pTextObj,
754 CPDF_PageObjectHolder::const_iterator ObjPos) {
755 if (fabs(pTextObj
->GetRect().Width()) < kSizeEpsilon)
758 size_t count = mTextObjects.size();
759 TransformedTextObject new_obj;
760 new_obj.m_pTextObj = pTextObj;
761 new_obj.m_formMatrix
= formMatrix;
763 mTextObjects.push_back(new_obj);
766 if (IsSameAsPreTextObject(pTextObj, pObjList, ObjPos))
769 TransformedTextObject prev_obj = mTextObjects[count - 1];
770 size_t nItem = prev_obj.m_pTextObj->CountItems();
774 CPDF_TextObject::
Item item = prev_obj.m_pTextObj->GetItemInfo(nItem - 1);
776 GetCharWidth(item
.m_CharCode, prev_obj.m_pTextObj->GetFont().Get()) *
777 prev_obj.m_pTextObj->GetFontSize() / 1000;
780 prev_obj.m_pTextObj->GetTextMatrix() * prev_obj.m_formMatrix;
782 item = pTextObj->GetItemInfo(0);
785 this_width = fabs(this_width);
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);
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);
813 mTextObjects.insert(mTextObjects.begin(), new_obj);
817 const CPDF_TextObject* pTextObj) {
819 const size_t nContentMarks = pMarks->CountItems();
820 if (nContentMarks == 0)
821 return MarkedContentState::kPass;
826 for (size_t i = 0; i < nContentMarks; ++i) {
827 const CPDF_ContentMarkItem* item = pMarks->GetItem(i);
831 RetainPtr<
const CPDF_String> temp = pDict->GetStringFor(
"ActualText");
834 actText = temp->GetUnicodeText();
838 return MarkedContentState::kPass;
840 if (m_pPrevTextObj) {
842 if (pPrevMarks->CountItems() == nContentMarks &&
843 pPrevMarks->GetItem(nContentMarks - 1)->GetParam() == pDict) {
844 return MarkedContentState::kDone;
849 return MarkedContentState::kPass;
853 for (size_t i = 0; i < actText.GetLength(); ++i) {
860 return MarkedContentState::kPass;
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))) {
871 return MarkedContentState::kDone;
873 return MarkedContentState::kDelay;
876void CPDF_TextPage::ProcessMarkedContent(
const TransformedTextObject& obj) {
877 const CPDF_TextObject* pTextObj = obj.m_pTextObj;
879 const size_t nContentMarks = pMarks->CountItems();
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();
885 actText = pDict->GetUnicodeTextFor(
"ActualText");
891 const bool bR2L = IsRightToLeft(*pTextObj, *pFont);
904 for (size_t k = 0; k < actText.GetLength(); ++k) {
905 wchar_t wChar = actText[k];
906 if (wChar <= 0x80 && !isprint(wChar))
912 charinfo.m_Origin = pTextObj->GetPos();
913 charinfo.m_Index = m_TextBuf.GetLength();
915 charinfo
.m_CharCode = pFont->CharCodeFromUnicode(wChar);
917 charinfo.m_pTextObj = pTextObj;
921 m_TempTextBuf.AppendChar(wChar);
922 m_TempCharList.push_back(charinfo);
927 const CharInfo* pPrevCharInfo = GetPrevCharInfo();
931 if (pPrevCharInfo->m_pTextObj)
932 m_pPrevTextObj = pPrevCharInfo->m_pTextObj;
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);
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());
953void CPDF_TextPage::ProcessTextObject(
const TransformedTextObject& obj) {
954 const CPDF_TextObject* pTextObj = obj.m_pTextObj;
955 if (fabs(pTextObj
->GetRect().Width()) < kSizeEpsilon)
961 MarkedContentState ePreMKC = PreMarkedContent(obj.m_pTextObj);
962 if (ePreMKC == MarkedContentState::kDone) {
963 m_pPrevTextObj = pTextObj;
964 m_PrevMatrix
= form_matrix;
967 GenerateCharacter result = GenerateCharacter::kNone;
968 if (m_pPrevTextObj) {
969 result = ProcessInsertObject(pTextObj, form_matrix);
970 if (result == GenerateCharacter::kLineBreak)
973 m_CurlineRect
.Union(obj.m_pTextObj->GetRect()
);
976 case GenerateCharacter::kNone:
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());
988 case GenerateCharacter::kLineBreak:
990 if (m_TextBuf.GetSize()) {
991 AppendGeneratedCharacter(L'\r', form_matrix);
992 AppendGeneratedCharacter(L'\n', form_matrix);
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);
1002 wchar_t curChar = wstrItem
[0
];
1003 if (IsHyphenCode(curChar))
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();
1011 CharInfo* charinfo = &m_TempCharList.back();
1012 m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1015 m_TempTextBuf.AppendChar(0xfffe);
1022 if (ePreMKC == MarkedContentState::kDelay) {
1023 ProcessMarkedContent(obj);
1024 m_pPrevTextObj = pTextObj;
1025 m_PrevMatrix
= form_matrix;
1028 m_pPrevTextObj = pTextObj;
1029 m_PrevMatrix
= form_matrix;
1030 float baseSpace = CalculateBaseSpace(pTextObj, matrix);
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();
1039 const size_t nItems = pTextObj->CountItems();
1040 for (size_t i = 0; i < nItems; ++i) {
1042 CPDF_TextObject::
Item item = pTextObj->GetItemInfo(i);
1044 WideString str = m_TempTextBuf.MakeString();
1046 str = m_TextBuf.AsStringView();
1050 float fontsize_h = pTextObj
->text_state().GetFontSizeH();
1051 spacing = -fontsize_h * item.m_Origin.x / 1000;
1054 float charSpace = pTextObj
->text_state().GetCharSpace();
1055 if (charSpace > 0.001)
1057 else if (charSpace < -0.001)
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;
1065 threshold = fontsize_h * pFont->GetCharWidthF(space_charcode) / 1000;
1066 if (threshold > fontsize_h / 3)
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;
1075 if (threshold && (spacing && spacing >= threshold)) {
1078 charinfo.m_pTextObj = pTextObj;
1079 charinfo.m_Index = m_TextBuf.GetLength();
1080 m_TempTextBuf.AppendChar(L' ');
1086 charinfo.m_Origin.x, charinfo.m_Origin.y);
1087 m_TempCharList.push_back(charinfo);
1093 WideString wstrItem = pFont->UnicodeFromCharCode(item
.m_CharCode);
1094 bool bNoUnicode =
false;
1103 charinfo.m_pTextObj = pTextObj;
1107 charinfo.m_pTextObj->GetFont()->GetCharBBox(charinfo
.m_CharCode);
1126 m_TempCharList.push_back(charinfo);
1127 m_TempTextBuf.AppendChar(0xfffe);
1130 size_t nTotal = wstrItem.GetLength();
1132 const int count = std::min(fxcrt::CollectionSize<
int>(m_TempCharList), 7);
1133 constexpr float kTextCharRatioGapDelta = 0.07f;
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;
1141 charinfo1.m_pTextObj->GetFont() == charinfo.m_pTextObj->GetFont() &&
1142 fabs(diff.x) < threshold && fabs(diff.y) < threshold) {
1148 for (size_t nIndex = 0; nIndex < nTotal; ++nIndex) {
1151 charinfo.m_Index = m_TextBuf.GetLength();
1152 m_TempTextBuf.AppendChar(charinfo.m_Unicode);
1154 m_TempTextBuf.AppendChar(0xfffe);
1156 m_TempCharList.push_back(charinfo);
1158 }
else if (i == 0) {
1159 WideString str = m_TempTextBuf.MakeString();
1161 m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
1162 m_TempCharList.pop_back();
1166 if (bIsBidiAndMirrorInverse)
1167 SwapTempTextBuf(iCharListStartAppend, iBufStartAppend);
1171 const CPDF_TextObject* pTextObj)
const {
1172 size_t nChars = pTextObj->CountChars();
1174 return m_TextlineDir;
1176 CPDF_TextObject::
Item first = pTextObj->GetCharInfo(0);
1177 CPDF_TextObject::
Item last = pTextObj->GetCharInfo(nChars - 1);
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;
1188 static constexpr float kThreshold = 0.0872f;
1189 CFX_VectorF v(dX, dY);
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;
1198 WideStringView curText = m_TempTextBuf.AsStringView();
1199 if (curText.IsEmpty())
1200 curText = m_TextBuf.AsStringView();
1202 if (curText.IsEmpty())
1205 auto iter = curText.rbegin();
1206 for (; (iter + 1) != curText.rend() && *iter == 0x20; ++iter) {
1210 if (!IsHyphenCode(*iter))
1213 if ((iter + 1) != curText.rend()) {
1219 const CharInfo* pPrevCharInfo = GetPrevCharInfo();
1220 return pPrevCharInfo &&
1226 if (!m_TempCharList.empty())
1227 return &m_TempCharList.back();
1228 return !m_CharList.empty() ? &m_CharList.back() :
nullptr;
1232 const CPDF_TextObject* pObj,
1234 FindPreviousTextObject();
1235 TextOrientation WritingMode = GetTextObjectWritingMode(pObj);
1236 if (WritingMode == TextOrientation::kUnknown)
1237 WritingMode = GetTextObjectWritingMode(m_pPrevTextObj);
1239 size_t nItem = m_pPrevTextObj->CountItems();
1241 return GenerateCharacter::kNone;
1243 CPDF_TextObject::
Item PrevItem = m_pPrevTextObj->GetItemInfo(nItem - 1);
1244 CPDF_TextObject::
Item item = pObj->GetItemInfo(0);
1247 WideString wstrItem = pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
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;
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;
1265 float last_pos = PrevItem.m_Origin.x;
1267 GetCharWidth(PrevItem.m_CharCode, m_pPrevTextObj->GetFont().Get());
1268 float last_width = nLastWidth * m_pPrevTextObj->GetFontSize() / 1000;
1269 last_width = fabs(last_width);
1272 float threshold =
std::max(last_width, this_width) / 4;
1274 CFX_Matrix prev_matrix = m_pPrevTextObj->GetTextMatrix() * m_PrevMatrix;
1277 CFX_PointF pos = prev_reverse.Transform(formMatrix.Transform(pObj->GetPos()));
1278 if (last_width < this_width)
1281 bool bNewline =
false;
1282 if (WritingMode == TextOrientation::kHorizontal) {
1287 ((pos.y > threshold * 2 || pos.y < threshold * -3) &&
1288 (fabs(pos.y) >= 1 || fabs(pos.y) > fabs(pos.x)))) {
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 &&
1298 m_pPrevTextObj->GetRect().top);
1299 if (re.Contains(pObj->GetPos())) {
1302 if (CFX_FloatRect(0, pObj->GetRect().bottom, 1000,
1303 pObj->GetRect().top)
1304 .Contains(m_pPrevTextObj->GetPos())) {
1313 return IsHyphen(curChar) ? GenerateCharacter::kHyphen
1314 : GenerateCharacter::kLineBreak;
1317 if (pObj->CountChars() == 1 && IsHyphenCode(curChar) && IsHyphen(curChar))
1318 return GenerateCharacter::kHyphen;
1320 if (curChar == L' ')
1321 return GenerateCharacter::kNone;
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;
1330 float threshold2 =
std::max(nLastWidth, nThisWidth);
1331 threshold2 = NormalizeThreshold(threshold2, 400, 700, 800);
1332 if (nLastWidth >= nThisWidth) {
1333 threshold2 *= fabs(m_pPrevTextObj->GetFontSize());
1340 if ((threshold2 < 1.4881 && threshold2 > 1.4879) ||
1341 (threshold2 < 1.39001 && threshold2 > 1.38999)) {
1344 return GenerateSpace(pos, last_pos, this_width, last_width, threshold2)
1345 ? GenerateCharacter::kSpace
1346 : GenerateCharacter::kNone;
1349bool CPDF_TextPage::IsSameTextObject(CPDF_TextObject* pTextObj1,
1350 CPDF_TextObject* pTextObj2)
const {
1351 if (!pTextObj1 || !pTextObj2)
1357 float dbXdif = fabs(rcPreObj
.left - rcCurObj
.left);
1358 size_t nCount = m_CharList.size();
1360 float dbSpace = m_CharList[nCount - 2].m_CharBox.Width();
1361 if (dbXdif > dbSpace)
1376 size_t nPreCount = pTextObj2->CountItems();
1377 if (nPreCount != pTextObj1->CountItems())
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);
1393 CFX_PointF diff = pTextObj1->GetPos() - pTextObj2->GetPos();
1397 float max_pre_size =
1399 return fabs(diff.x) <= 0.9 * char_size * font_size / 1000 &&
1400 fabs(diff.y) <= max_pre_size / 8;
1404 CPDF_TextObject* pTextObj,
1406 CPDF_PageObjectHolder::const_iterator iter)
const {
1408 while (i < 5 && iter != pObjList->begin()) {
1411 if (pOtherObj == pTextObj || !pOtherObj
->IsText())
1413 if (IsSameTextObject(pOtherObj
->AsText(), pTextObj))
1420absl::optional<CPDF_TextPage::CharInfo>
CPDF_TextPage::GenerateCharInfo(
1422 const CharInfo* pPrevCharInfo = GetPrevCharInfo();
1424 return absl::nullopt;
1427 info.m_Index = m_TextBuf.GetLength();
1433 if (pPrevCharInfo->m_pTextObj &&
1436 pPrevCharInfo->m_pTextObj->GetFont().Get());
1439 float fFontSize = pPrevCharInfo->m_pTextObj
1440 ? pPrevCharInfo->m_pTextObj->GetFontSize()
1443 fFontSize = kDefaultFontSize;
1446 CFX_PointF(pPrevCharInfo->m_Origin.x + preWidth * (fFontSize) / 1000,
1447 pPrevCharInfo->m_Origin.y);
1449 info.m_Origin.x, info.m_Origin.y);
void SetOverallDirectionRight()
CFX_BidiString(const WideString &str)
CFX_BidiChar::Direction OverallDirection() const
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)
void Intersect(const CFX_FloatRect &other_rect)
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
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
virtual WideString UnicodeFromCharCode(uint32_t charcode) const
void AppendChar(ByteString *str, uint32_t charcode) const
int GetStringWidth(ByteStringView pString)
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
float GetPageWidth() const override
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
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
bool FXSYS_iswalnum(wchar_t c)
#define FXSYS_IsFloatZero(f)
wchar_t GetMirrorChar(wchar_t wch)
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]