7#include "core/fpdfdoc/cpvt_section.h"
11#include "core/fpdfdoc/cpvt_variabletext.h"
12#include "core/fpdfdoc/cpvt_wordinfo.h"
13#include "core/fxcrt/stl_util.h"
14#include "third_party/base/check.h"
18constexpr uint8_t kSpecialChars[128] = {
19 0x00, 0x0C, 0x08, 0x0C, 0x08, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
20 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00,
22 0x10, 0x00, 0x00, 0x28, 0x0C, 0x08, 0x00, 0x00, 0x28, 0x28, 0x28, 0x28,
23 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x08, 0x08,
24 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
25 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
26 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00,
27 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
28 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
29 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00,
32bool IsLatin(uint16_t word) {
34 return !!(kSpecialChars[word] & 0x01);
36 return ((word >= 0x00C0 && word <= 0x00FF) ||
37 (word >= 0x0100 && word <= 0x024F) ||
38 (word >= 0x1E00 && word <= 0x1EFF) ||
39 (word >= 0x2C60 && word <= 0x2C7F) ||
40 (word >= 0xA720 && word <= 0xA7FF) ||
41 (word >= 0xFF21 && word <= 0xFF3A) ||
42 (word >= 0xFF41 && word <= 0xFF5A));
45bool IsDigit(uint32_t word) {
46 return word >= 0x0030 && word <= 0x0039;
49bool IsCJK(uint32_t word) {
50 if ((word >= 0x1100 && word <= 0x11FF) ||
51 (word >= 0x2E80 && word <= 0x2FFF) ||
52 (word >= 0x3040 && word <= 0x9FBF) ||
53 (word >= 0xAC00 && word <= 0xD7AF) ||
54 (word >= 0xF900 && word <= 0xFAFF) ||
55 (word >= 0xFE30 && word <= 0xFE4F) ||
56 (word >= 0x20000 && word <= 0x2A6DF) ||
57 (word >= 0x2F800 && word <= 0x2FA1F)) {
60 if (word >= 0x3000 && word <= 0x303F) {
62 word == 0x3005 || word == 0x3006 || word == 0x3021 || word == 0x3022 ||
63 word == 0x3023 || word == 0x3024 || word == 0x3025 || word == 0x3026 ||
64 word == 0x3027 || word == 0x3028 || word == 0x3029 || word == 0x3031 ||
65 word == 0x3032 || word == 0x3033 || word == 0x3034 || word == 0x3035);
67 return word >= 0xFF66 && word <= 0xFF9D;
70bool IsPunctuation(uint32_t word) {
72 return !!(kSpecialChars[word] & 0x08);
74 if (word >= 0x0080 && word <= 0x00FF) {
75 return (word == 0x0082 || word == 0x0084 || word == 0x0085 ||
76 word == 0x0091 || word == 0x0092 || word == 0x0093 ||
77 word <= 0x0094 || word == 0x0096 || word == 0x00B4 ||
81 if (word >= 0x2000 && word <= 0x206F) {
83 word == 0x2010 || word == 0x2011 || word == 0x2012 || word == 0x2013 ||
84 word == 0x2018 || word == 0x2019 || word == 0x201A || word == 0x201B ||
85 word == 0x201C || word == 0x201D || word == 0x201E || word == 0x201F ||
86 word == 0x2032 || word == 0x2033 || word == 0x2034 || word == 0x2035 ||
87 word == 0x2036 || word == 0x2037 || word == 0x203C || word == 0x203D ||
88 word == 0x203E || word == 0x2044);
91 if (word >= 0x3000 && word <= 0x303F) {
93 word == 0x3001 || word == 0x3002 || word == 0x3003 || word == 0x3005 ||
94 word == 0x3009 || word == 0x300A || word == 0x300B || word == 0x300C ||
95 word == 0x300D || word == 0x300F || word == 0x300E || word == 0x3010 ||
96 word == 0x3011 || word == 0x3014 || word == 0x3015 || word == 0x3016 ||
97 word == 0x3017 || word == 0x3018 || word == 0x3019 || word == 0x301A ||
98 word == 0x301B || word == 0x301D || word == 0x301E || word == 0x301F);
101 if (word >= 0xFE50 && word <= 0xFE6F)
102 return (word >= 0xFE50 && word <= 0xFE5E) || word == 0xFE63;
104 if (word >= 0xFF00 && word <= 0xFFEF) {
106 word == 0xFF01 || word == 0xFF02 || word == 0xFF07 || word == 0xFF08 ||
107 word == 0xFF09 || word == 0xFF0C || word == 0xFF0E || word == 0xFF0F ||
108 word == 0xFF1A || word == 0xFF1B || word == 0xFF1F || word == 0xFF3B ||
109 word == 0xFF3D || word == 0xFF40 || word == 0xFF5B || word == 0xFF5C ||
110 word == 0xFF5D || word == 0xFF61 || word == 0xFF62 || word == 0xFF63 ||
111 word == 0xFF64 || word == 0xFF65 || word == 0xFF9E || word == 0xFF9F);
117bool IsConnectiveSymbol(uint32_t word) {
118 return word <= 0x007F && (kSpecialChars[word] & 0x20);
121bool IsOpenStylePunctuation(uint32_t word) {
123 return !!(kSpecialChars[word] & 0x04);
125 return (word == 0x300A || word == 0x300C || word == 0x300E ||
126 word == 0x3010 || word == 0x3014 || word == 0x3016 ||
127 word == 0x3018 || word == 0x301A || word == 0xFF08 ||
128 word == 0xFF3B || word == 0xFF5B || word == 0xFF62);
131bool IsCurrencySymbol(uint16_t word) {
132 return (word == 0x0024 || word == 0x0080 || word == 0x00A2 ||
133 word == 0x00A3 || word == 0x00A4 || word == 0x00A5 ||
134 (word >= 0x20A0 && word <= 0x20CF) || word == 0xFE69 ||
135 word == 0xFF04 || word == 0xFFE0 || word == 0xFFE1 ||
136 word == 0xFFE5 || word == 0xFFE6);
139bool IsPrefixSymbol(uint16_t word) {
140 return IsCurrencySymbol(word) || word == 0x2116;
143bool IsSpace(uint16_t word) {
144 return word == 0x0020 || word == 0x3000;
147bool NeedDivision(uint16_t prevWord, uint16_t curWord) {
148 if ((IsLatin(prevWord) || IsDigit(prevWord)) &&
149 (IsLatin(curWord) || IsDigit(curWord))) {
152 if (IsSpace(curWord) || IsPunctuation(curWord)) {
155 if (IsConnectiveSymbol(prevWord) || IsConnectiveSymbol(curWord)) {
158 if (IsSpace(prevWord) || IsPunctuation(prevWord)) {
161 if (IsPrefixSymbol(prevWord)) {
164 if (IsPrefixSymbol(curWord) || IsCJK(curWord)) {
167 if (IsCJK(prevWord)) {
178CPVT_Section::
Line::~
Line() =
default;
217 for (
auto& pLine : m_LineArray) {
218 pLine->m_LinePlace = CPVT_WordPlace(m_SecPlace.nSecIndex, i, -1);
225 int32_t nWordIndex = std::clamp(place.nWordIndex, 0,
226 fxcrt::CollectionSize<int32_t>(m_WordArray));
227 m_WordArray.insert(m_WordArray.begin() + nWordIndex,
228 std::make_unique<CPVT_WordInfo>(wordinfo));
233 m_LineArray.push_back(std::make_unique<Line>(lineinfo));
234 return CPVT_WordPlace(m_SecPlace.nSecIndex,
235 fxcrt::CollectionSize<int32_t>(m_LineArray) - 1, -1);
239 if (m_pVT->GetCharArray() > 0)
240 return RearrangeCharArray();
241 return RearrangeTypeset();
245 CPVT_FloatRect result = SplitLines(
false, fFontSize);
250 if (m_LineArray.empty())
252 return m_LineArray.front()->GetBeginWordPlace();
256 if (m_LineArray.empty())
258 return m_LineArray.back()->GetEndWordPlace();
266 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
269 Line* pLine = m_LineArray[place.nLineIndex].get();
276 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex - 1))
279 return m_LineArray[place.nLineIndex - 1]->GetEndWordPlace();
287 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
290 Line* pLine = m_LineArray[place.nLineIndex].get();
294 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex + 1))
297 return m_LineArray[place.nLineIndex + 1]->GetBeginWordPlace();
302 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
303 int32_t nMid = (nLeft + nRight) / 2;
304 while (nLeft <= nRight) {
305 Line* pLine = m_LineArray[nMid].get();
308 nMid = (nLeft + nRight) / 2;
311 nMid = (nLeft + nRight) / 2;
324 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
325 int32_t nMid = fxcrt::CollectionSize<int32_t>(m_LineArray) / 2;
326 while (nLeft <= nRight) {
327 Line* pLine = m_LineArray[nMid].get();
328 float fTop = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineAscent -
329 m_pVT->GetLineLeading();
337 nMid = (nLeft + nRight) / 2;
342 nMid = (nLeft + nRight) / 2;
345 place = SearchWordPlace(
362 if (!fxcrt::IndexInBounds(m_LineArray, lineplace.nLineIndex))
365 Line* pLine = m_LineArray[lineplace.nLineIndex].get();
380 int32_t nMid = (nLeft + nRight) / 2;
381 while (nLeft < nRight) {
384 if (nMid == nRight) {
388 if (!fxcrt::IndexInBounds(m_WordArray, nMid))
391 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f) {
393 nMid = (nLeft + nRight) / 2;
397 nMid = (nLeft + nRight) / 2;
399 if (fxcrt::IndexInBounds(m_WordArray, nMid)) {
401 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f)
408 return fxcrt::CollectionSize<int32_t>(m_LineArray);
412 if (!fxcrt::IndexInBounds(m_LineArray, index))
415 return m_LineArray[index].get();
419 return fxcrt::CollectionSize<int32_t>(m_WordArray);
423 if (!fxcrt::IndexInBounds(m_WordArray, index))
426 return m_WordArray[index].get();
430 if (!fxcrt::IndexInBounds(m_WordArray, index))
433 m_WordArray.erase(m_WordArray.begin() + index, m_WordArray.end());
436CPVT_FloatRect CPVT_Section::RearrangeCharArray()
const {
437 if (m_LineArray.empty())
438 return CPVT_FloatRect
();
440 float fNodeWidth = m_pVT->GetPlateWidth() /
441 (m_pVT->GetCharArray() <= 0 ? 1 : m_pVT->GetCharArray());
443 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
445 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
447 float y = m_pVT->GetLineLeading() + fLineAscent;
449 CPVT_Section::
Line* pLine = m_LineArray.front().get();
450 switch (m_pVT->GetAlignment()) {
455 nStart = (m_pVT->GetCharArray() -
456 fxcrt::CollectionSize<int32_t>(m_WordArray)) /
462 m_pVT->GetCharArray() - fxcrt::CollectionSize<int32_t>(m_WordArray);
466 for (int32_t w = 0, sz = fxcrt::CollectionSize<int32_t>(m_WordArray); w < sz;
468 if (w >= m_pVT->GetCharArray())
471 float fNextWidth = 0;
472 if (fxcrt::IndexInBounds(m_WordArray, w + 1)) {
475 fNextWidth = m_pVT->GetWordWidth(*pNextWord);
479 float fWordWidth = m_pVT->GetWordWidth(*pWord);
480 float fWordAscent = m_pVT->GetWordAscent(*pWord);
481 float fWordDescent = m_pVT->GetWordDescent(*pWord);
482 x = (
float)(fNodeWidth * (w + nStart + 0.5) - fWordWidth * 0.5f);
488 if (w != fxcrt::CollectionSize<int32_t>(m_WordArray) - 1) {
489 pWord
->fWordTail = (fNodeWidth - (fWordWidth + fNextWidth) * 0.5f > 0
490 ? fNodeWidth - (fWordWidth + fNextWidth) * 0.5f
496 fLineAscent =
std::max(fLineAscent, fWordAscent);
497 fLineDescent =
std::min(fLineDescent, fWordDescent);
500 pLine->m_LineInfo.nEndWordIndex =
501 fxcrt::CollectionSize<int32_t>(m_WordArray) - 1;
506 return CPVT_FloatRect
(0
, 0
, x
, y - fLineDescent
);
509CPVT_FloatRect CPVT_Section::RearrangeTypeset() {
511 return OutputLines(SplitLines(
true, 0.0f));
514CPVT_FloatRect CPVT_Section::SplitLines(
bool bTypeset,
float fFontSize) {
516 if (m_WordArray.empty()) {
520 fLineAscent = m_pVT->GetLineAscent();
521 fLineDescent = m_pVT->GetLineDescent();
531 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), fFontSize);
533 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), fFontSize);
535 float fMaxY = m_pVT->GetLineLeading() + fLineAscent - fLineDescent;
536 return CPVT_FloatRect
(0
, 0
, 0
, fMaxY
);
539 int32_t nLineHead = 0;
540 int32_t nLineTail = 0;
543 float fLineWidth = 0.0f;
544 float fBackupLineWidth = 0.0f;
545 float fLineAscent = 0.0f;
546 float fBackupLineAscent = 0.0f;
547 float fLineDescent = 0.0f;
548 float fBackupLineDescent = 0.0f;
549 int32_t nWordStartPos = 0;
550 bool bFullWord =
false;
551 int32_t nLineFullWordIndex = 0;
552 int32_t nCharIndex = 0;
553 float fWordWidth = 0;
554 float fTypesetWidth =
555 std::max(m_pVT->GetPlateWidth() - m_pVT->GetLineIndent(), 0.0f);
556 int32_t nTotalWords = fxcrt::CollectionSize<int32_t>(m_WordArray);
557 bool bOpened =
false;
559 while (i < nTotalWords) {
563 pOldWord = m_WordArray[i - 1].get();
567 fLineAscent = std::max(fLineAscent, m_pVT->GetWordAscent(*pWord));
568 fLineDescent = std::min(fLineDescent, m_pVT->GetWordDescent(*pWord));
569 fWordWidth = m_pVT->GetWordWidth(*pWord);
572 std::max(fLineAscent, m_pVT->GetWordAscent(*pWord, fFontSize));
574 std::min(fLineDescent, m_pVT->GetWordDescent(*pWord, fFontSize));
575 fWordWidth = m_pVT->GetWordWidth(pWord->nFontIndex, pWord->Word,
576 m_pVT->GetSubWord(), fFontSize,
580 if (IsOpenStylePunctuation(pWord
->Word)) {
583 }
else if (pOldWord) {
589 if (!IsSpace(pWord
->Word) && !IsOpenStylePunctuation(pWord
->Word)) {
595 if (nCharIndex > 0) {
596 nLineFullWordIndex++;
599 fBackupLineWidth = fLineWidth;
600 fBackupLineAscent = fLineAscent;
601 fBackupLineDescent = fLineDescent;
605 if (m_pVT->IsAutoReturn() && fTypesetWidth > 0 &&
606 fLineWidth + fWordWidth > fTypesetWidth) {
607 if (nLineFullWordIndex > 0) {
609 fLineWidth = fBackupLineWidth;
610 fLineAscent = fBackupLineAscent;
611 fLineDescent = fBackupLineDescent;
613 if (nCharIndex == 1) {
614 fLineWidth = fWordWidth;
627 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
628 fMaxY -= fLineDescent;
629 fMaxX =
std::max(fLineWidth, fMaxX);
635 nLineFullWordIndex = 0;
638 fLineWidth += fWordWidth;
642 if (nLineHead <= nTotalWords - 1) {
643 nLineTail = nTotalWords - 1;
653 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
654 fMaxY -= fLineDescent;
655 fMaxX =
std::max(fLineWidth, fMaxX);
657 return CPVT_FloatRect
(0
, 0
, fMaxX
, fMaxY
);
660CPVT_FloatRect CPVT_Section::OutputLines(
const CPVT_FloatRect& rect)
const {
662 float fLineIndent = m_pVT->GetLineIndent();
663 float fTypesetWidth = std::max(m_pVT->GetPlateWidth() - fLineIndent, 0.0f);
664 switch (m_pVT->GetAlignment()) {
670 fMinX = (fTypesetWidth - rect
.Width()) * 0.5f;
673 fMinX = fTypesetWidth - rect
.Width();
679 int32_t nTotalLines = fxcrt::CollectionSize<int32_t>(m_LineArray);
680 if (nTotalLines > 0) {
683 for (int32_t l = 0; l < nTotalLines; l++) {
684 CPVT_Section::
Line* pLine = m_LineArray[l].get();
685 switch (m_pVT->GetAlignment()) {
697 fPosX += fLineIndent;
698 fPosY += m_pVT->GetLineLeading();
704 if (fxcrt::IndexInBounds(m_WordArray, w)) {
709 fPosX += m_pVT->GetWordWidth(*pWord);
715 return CPVT_FloatRect
(fMinX
, fMinY
, fMaxX
, fMaxY
);
718void CPVT_Section::ClearLeftWords(int32_t nWordIndex) {
719 for (int32_t i = nWordIndex; i >= 0; i--) {
720 if (fxcrt::IndexInBounds(m_WordArray, i))
721 m_WordArray.erase(m_WordArray.begin() + i);
725void CPVT_Section::ClearRightWords(int32_t nWordIndex) {
726 int32_t sz = fxcrt::CollectionSize<int32_t>(m_WordArray);
727 for (int32_t i = sz - 1; i > nWordIndex; i--) {
728 if (fxcrt::IndexInBounds(m_WordArray, i))
729 m_WordArray.erase(m_WordArray.begin() + i);
733void CPVT_Section::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) {
734 for (int32_t i = nEndIndex; i > nBeginIndex; i--) {
735 if (fxcrt::IndexInBounds(m_WordArray, i))
736 m_WordArray.erase(m_WordArray.begin() + i);
750 }
else if (PlaceRange
.EndPos <= SecEndPos) {
758 if (fxcrt::IndexInBounds(m_WordArray, place.nWordIndex))
759 m_WordArray.erase(m_WordArray.begin() + place.nWordIndex);
CPVT_FloatRect(float other_left, float other_top, float other_right, float other_bottom)
CPVT_WordPlace GetEndWordPlace() const
Line(const CPVT_LineInfo &lineinfo)
CPVT_WordPlace GetBeginWordPlace() const
CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace &place) const
CPVT_WordPlace m_LinePlace
CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace &place) const
int32_t GetWordArraySize() const
void ClearWord(const CPVT_WordPlace &place)
void EraseWordsFrom(int32_t index)
CPVT_Section(CPVT_VariableText *pVT)
CPVT_FloatRect Rearrange()
CPVT_WordPlace GetEndWordPlace() const
CPVT_WordPlace SearchWordPlace(float fx, const CPVT_WordRange &range) const
const Line * GetLineFromArray(int32_t index) const
CFX_SizeF GetSectionSize(float fFontSize)
void UpdateWordPlace(CPVT_WordPlace &place) const
CPVT_WordPlace SearchWordPlace(float fx, const CPVT_WordPlace &lineplace) const
void ClearWords(const CPVT_WordRange &PlaceRange)
CPVT_WordPlace GetBeginWordPlace() const
CPVT_WordPlace SearchWordPlace(const CFX_PointF &point) const
CPVT_WordPlace GetPrevWordPlace(const CPVT_WordPlace &place) const
const CPVT_WordInfo * GetWordFromArray(int32_t index) const
CPVT_WordPlace AddWord(const CPVT_WordPlace &place, const CPVT_WordInfo &wordinfo)
CPVT_WordPlace GetNextWordPlace(const CPVT_WordPlace &place) const
int32_t GetLineArraySize() const
CPVT_WordPlace AddLine(const CPVT_LineInfo &lineinfo)
#define FXSYS_IsFloatBigger(fa, fb)
#define FXSYS_IsFloatSmaller(fa, fb)
bool operator<=(const CPVT_WordPlace &wp) const
CPVT_WordPlace(int32_t other_nSecIndex, int32_t other_nLineIndex, int32_t other_nWordIndex)
bool operator>=(const CPVT_WordPlace &wp) const
CPVT_WordRange(const CPVT_WordPlace &begin, const CPVT_WordPlace &end)