7#include "core/fpdfdoc/cpvt_section.h"
12#include "core/fpdfdoc/cpvt_variabletext.h"
13#include "core/fpdfdoc/cpvt_wordinfo.h"
14#include "core/fxcrt/check.h"
15#include "core/fxcrt/stl_util.h"
19constexpr std::array<
const uint8_t, 128> kSpecialChars = {{
20 0x00, 0x0C, 0x08, 0x0C, 0x08, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00,
23 0x10, 0x00, 0x00, 0x28, 0x0C, 0x08, 0x00, 0x00, 0x28, 0x28, 0x28, 0x28,
24 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x08, 0x08,
25 0x00, 0x00, 0x00, 0x08, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
26 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
27 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00,
28 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
29 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
30 0x01, 0x01, 0x01, 0x0C, 0x00, 0x08, 0x00, 0x00,
33bool IsLatin(uint16_t word) {
35 return !!(kSpecialChars[word] & 0x01);
37 return ((word >= 0x00C0 && word <= 0x00FF) ||
38 (word >= 0x0100 && word <= 0x024F) ||
39 (word >= 0x1E00 && word <= 0x1EFF) ||
40 (word >= 0x2C60 && word <= 0x2C7F) ||
41 (word >= 0xA720 && word <= 0xA7FF) ||
42 (word >= 0xFF21 && word <= 0xFF3A) ||
43 (word >= 0xFF41 && word <= 0xFF5A));
46bool IsDigit(uint32_t word) {
47 return word >= 0x0030 && word <= 0x0039;
50bool IsCJK(uint32_t word) {
51 if ((word >= 0x1100 && word <= 0x11FF) ||
52 (word >= 0x2E80 && word <= 0x2FFF) ||
53 (word >= 0x3040 && word <= 0x9FBF) ||
54 (word >= 0xAC00 && word <= 0xD7AF) ||
55 (word >= 0xF900 && word <= 0xFAFF) ||
56 (word >= 0xFE30 && word <= 0xFE4F) ||
57 (word >= 0x20000 && word <= 0x2A6DF) ||
58 (word >= 0x2F800 && word <= 0x2FA1F)) {
61 if (word >= 0x3000 && word <= 0x303F) {
63 word == 0x3005 || word == 0x3006 || word == 0x3021 || word == 0x3022 ||
64 word == 0x3023 || word == 0x3024 || word == 0x3025 || word == 0x3026 ||
65 word == 0x3027 || word == 0x3028 || word == 0x3029 || word == 0x3031 ||
66 word == 0x3032 || word == 0x3033 || word == 0x3034 || word == 0x3035);
68 return word >= 0xFF66 && word <= 0xFF9D;
71bool IsPunctuation(uint32_t word) {
73 return !!(kSpecialChars[word] & 0x08);
75 if (word >= 0x0080 && word <= 0x00FF) {
76 return (word == 0x0082 || word == 0x0084 || word == 0x0085 ||
77 word == 0x0091 || word == 0x0092 || word == 0x0093 ||
78 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);
90 if (word >= 0x3000 && word <= 0x303F) {
92 word == 0x3001 || word == 0x3002 || word == 0x3003 || word == 0x3005 ||
93 word == 0x3009 || word == 0x300A || word == 0x300B || word == 0x300C ||
94 word == 0x300D || word == 0x300F || word == 0x300E || word == 0x3010 ||
95 word == 0x3011 || word == 0x3014 || word == 0x3015 || word == 0x3016 ||
96 word == 0x3017 || word == 0x3018 || word == 0x3019 || word == 0x301A ||
97 word == 0x301B || word == 0x301D || word == 0x301E || word == 0x301F);
99 if (word >= 0xFE50 && word <= 0xFE6F) {
100 return (word >= 0xFE50 && word <= 0xFE5E) || word == 0xFE63;
102 if (word >= 0xFF00 && word <= 0xFFEF) {
104 word == 0xFF01 || word == 0xFF02 || word == 0xFF07 || word == 0xFF08 ||
105 word == 0xFF09 || word == 0xFF0C || word == 0xFF0E || word == 0xFF0F ||
106 word == 0xFF1A || word == 0xFF1B || word == 0xFF1F || word == 0xFF3B ||
107 word == 0xFF3D || word == 0xFF40 || word == 0xFF5B || word == 0xFF5C ||
108 word == 0xFF5D || word == 0xFF61 || word == 0xFF62 || word == 0xFF63 ||
109 word == 0xFF64 || word == 0xFF65 || word == 0xFF9E || word == 0xFF9F);
114bool IsConnectiveSymbol(uint32_t word) {
115 return word <= 0x007F && (kSpecialChars[word] & 0x20);
118bool IsOpenStylePunctuation(uint32_t word) {
119 if (word <= 0x007F) {
120 return !!(kSpecialChars[word] & 0x04);
122 return (word == 0x300A || word == 0x300C || word == 0x300E ||
123 word == 0x3010 || word == 0x3014 || word == 0x3016 ||
124 word == 0x3018 || word == 0x301A || word == 0xFF08 ||
125 word == 0xFF3B || word == 0xFF5B || word == 0xFF62);
128bool IsCurrencySymbol(uint16_t word) {
129 return (word == 0x0024 || word == 0x0080 || word == 0x00A2 ||
130 word == 0x00A3 || word == 0x00A4 || word == 0x00A5 ||
131 (word >= 0x20A0 && word <= 0x20CF) || word == 0xFE69 ||
132 word == 0xFF04 || word == 0xFFE0 || word == 0xFFE1 ||
133 word == 0xFFE5 || word == 0xFFE6);
136bool IsPrefixSymbol(uint16_t word) {
137 return IsCurrencySymbol(word) || word == 0x2116;
140bool IsSpace(uint16_t word) {
141 return word == 0x0020 || word == 0x3000;
144bool NeedDivision(uint16_t prevWord, uint16_t curWord) {
145 if ((IsLatin(prevWord) || IsDigit(prevWord)) &&
146 (IsLatin(curWord) || IsDigit(curWord))) {
149 if (IsSpace(curWord) || IsPunctuation(curWord)) {
152 if (IsConnectiveSymbol(prevWord) || IsConnectiveSymbol(curWord)) {
155 if (IsSpace(prevWord) || IsPunctuation(prevWord)) {
158 if (IsPrefixSymbol(prevWord)) {
161 if (IsPrefixSymbol(curWord) || IsCJK(curWord)) {
164 if (IsCJK(prevWord)) {
175CPVT_Section::
Line::~
Line() =
default;
214 for (
auto& pLine : m_LineArray) {
215 pLine->m_LinePlace = CPVT_WordPlace(m_SecPlace.nSecIndex, i, -1);
222 int32_t nWordIndex = std::clamp(place.nWordIndex, 0,
223 fxcrt::CollectionSize<int32_t>(m_WordArray));
224 m_WordArray.insert(m_WordArray.begin() + nWordIndex,
225 std::make_unique<CPVT_WordInfo>(wordinfo));
230 m_LineArray.push_back(std::make_unique<Line>(lineinfo));
231 return CPVT_WordPlace(m_SecPlace.nSecIndex,
232 fxcrt::CollectionSize<int32_t>(m_LineArray) - 1, -1);
236 if (m_pVT->GetCharArray() > 0)
237 return RearrangeCharArray();
238 return RearrangeTypeset();
242 CPVT_FloatRect result = SplitLines(
false, fFontSize);
247 if (m_LineArray.empty())
249 return m_LineArray.front()->GetBeginWordPlace();
253 if (m_LineArray.empty())
255 return m_LineArray.back()->GetEndWordPlace();
263 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
266 Line* pLine = m_LineArray[place.nLineIndex].get();
273 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex - 1))
276 return m_LineArray[place.nLineIndex - 1]->GetEndWordPlace();
284 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
287 Line* pLine = m_LineArray[place.nLineIndex].get();
291 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex + 1))
294 return m_LineArray[place.nLineIndex + 1]->GetBeginWordPlace();
299 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
300 int32_t nMid = (nLeft + nRight) / 2;
301 while (nLeft <= nRight) {
302 Line* pLine = m_LineArray[nMid].get();
305 nMid = (nLeft + nRight) / 2;
308 nMid = (nLeft + nRight) / 2;
321 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
322 int32_t nMid = fxcrt::CollectionSize<int32_t>(m_LineArray) / 2;
323 while (nLeft <= nRight) {
324 Line* pLine = m_LineArray[nMid].get();
325 float fTop = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineAscent -
326 m_pVT->GetLineLeading();
334 nMid = (nLeft + nRight) / 2;
339 nMid = (nLeft + nRight) / 2;
342 place = SearchWordPlace(
359 if (!fxcrt::IndexInBounds(m_LineArray, lineplace.nLineIndex))
362 Line* pLine = m_LineArray[lineplace.nLineIndex].get();
377 int32_t nMid = (nLeft + nRight) / 2;
378 while (nLeft < nRight) {
381 if (nMid == nRight) {
385 if (!fxcrt::IndexInBounds(m_WordArray, nMid))
388 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f) {
390 nMid = (nLeft + nRight) / 2;
394 nMid = (nLeft + nRight) / 2;
396 if (fxcrt::IndexInBounds(m_WordArray, nMid)) {
398 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f)
405 return fxcrt::CollectionSize<int32_t>(m_LineArray);
409 if (!fxcrt::IndexInBounds(m_LineArray, index))
412 return m_LineArray[index].get();
416 return fxcrt::CollectionSize<int32_t>(m_WordArray);
420 if (!fxcrt::IndexInBounds(m_WordArray, index))
423 return m_WordArray[index].get();
427 if (!fxcrt::IndexInBounds(m_WordArray, index))
430 m_WordArray.erase(m_WordArray.begin() + index, m_WordArray.end());
433CPVT_FloatRect CPVT_Section::RearrangeCharArray()
const {
434 if (m_LineArray.empty())
435 return CPVT_FloatRect
();
437 float fNodeWidth = m_pVT->GetPlateWidth() /
438 (m_pVT->GetCharArray() <= 0 ? 1 : m_pVT->GetCharArray());
440 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
442 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
444 float y = m_pVT->GetLineLeading() + fLineAscent;
446 CPVT_Section::
Line* pLine = m_LineArray.front().get();
447 switch (m_pVT->GetAlignment()) {
452 nStart = (m_pVT->GetCharArray() -
453 fxcrt::CollectionSize<int32_t>(m_WordArray)) /
459 m_pVT->GetCharArray() - fxcrt::CollectionSize<int32_t>(m_WordArray);
463 for (int32_t w = 0, sz = fxcrt::CollectionSize<int32_t>(m_WordArray); w < sz;
465 if (w >= m_pVT->GetCharArray())
468 float fNextWidth = 0;
469 if (fxcrt::IndexInBounds(m_WordArray, w + 1)) {
472 fNextWidth = m_pVT->GetWordWidth(*pNextWord);
476 float fWordWidth = m_pVT->GetWordWidth(*pWord);
477 float fWordAscent = m_pVT->GetWordAscent(*pWord);
478 float fWordDescent = m_pVT->GetWordDescent(*pWord);
479 x = (
float)(fNodeWidth * (w + nStart + 0.5) - fWordWidth * 0.5f);
485 if (w != fxcrt::CollectionSize<int32_t>(m_WordArray) - 1) {
486 pWord
->fWordTail = (fNodeWidth - (fWordWidth + fNextWidth) * 0.5f > 0
487 ? fNodeWidth - (fWordWidth + fNextWidth) * 0.5f
493 fLineAscent =
std::max(fLineAscent, fWordAscent);
494 fLineDescent =
std::min(fLineDescent, fWordDescent);
497 pLine->m_LineInfo.nEndWordIndex =
498 fxcrt::CollectionSize<int32_t>(m_WordArray) - 1;
503 return CPVT_FloatRect
(0
, 0
, x
, y - fLineDescent
);
506CPVT_FloatRect CPVT_Section::RearrangeTypeset() {
508 return OutputLines(SplitLines(
true, 0.0f));
511CPVT_FloatRect CPVT_Section::SplitLines(
bool bTypeset,
float fFontSize) {
513 if (m_WordArray.empty()) {
517 fLineAscent = m_pVT->GetLineAscent();
518 fLineDescent = m_pVT->GetLineDescent();
528 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), fFontSize);
530 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), fFontSize);
532 float fMaxY = m_pVT->GetLineLeading() + fLineAscent - fLineDescent;
533 return CPVT_FloatRect
(0
, 0
, 0
, fMaxY
);
536 int32_t nLineHead = 0;
537 int32_t nLineTail = 0;
540 float fLineWidth = 0.0f;
541 float fBackupLineWidth = 0.0f;
542 float fLineAscent = 0.0f;
543 float fBackupLineAscent = 0.0f;
544 float fLineDescent = 0.0f;
545 float fBackupLineDescent = 0.0f;
546 int32_t nWordStartPos = 0;
547 bool bFullWord =
false;
548 int32_t nLineFullWordIndex = 0;
549 int32_t nCharIndex = 0;
550 float fWordWidth = 0;
551 float fTypesetWidth =
552 std::max(m_pVT->GetPlateWidth() - m_pVT->GetLineIndent(), 0.0f);
553 int32_t nTotalWords = fxcrt::CollectionSize<int32_t>(m_WordArray);
554 bool bOpened =
false;
556 while (i < nTotalWords) {
560 pOldWord = m_WordArray[i - 1].get();
564 fLineAscent = std::max(fLineAscent, m_pVT->GetWordAscent(*pWord));
565 fLineDescent = std::min(fLineDescent, m_pVT->GetWordDescent(*pWord));
566 fWordWidth = m_pVT->GetWordWidth(*pWord);
569 std::max(fLineAscent, m_pVT->GetWordAscent(*pWord, fFontSize));
571 std::min(fLineDescent, m_pVT->GetWordDescent(*pWord, fFontSize));
572 fWordWidth = m_pVT->GetWordWidth(pWord->nFontIndex, pWord->Word,
573 m_pVT->GetSubWord(), fFontSize,
577 if (IsOpenStylePunctuation(pWord
->Word)) {
580 }
else if (pOldWord) {
586 if (!IsSpace(pWord
->Word) && !IsOpenStylePunctuation(pWord
->Word)) {
592 if (nCharIndex > 0) {
593 nLineFullWordIndex++;
596 fBackupLineWidth = fLineWidth;
597 fBackupLineAscent = fLineAscent;
598 fBackupLineDescent = fLineDescent;
602 if (m_pVT->IsAutoReturn() && fTypesetWidth > 0 &&
603 fLineWidth + fWordWidth > fTypesetWidth) {
604 if (nLineFullWordIndex > 0) {
606 fLineWidth = fBackupLineWidth;
607 fLineAscent = fBackupLineAscent;
608 fLineDescent = fBackupLineDescent;
610 if (nCharIndex == 1) {
611 fLineWidth = fWordWidth;
624 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
625 fMaxY -= fLineDescent;
626 fMaxX =
std::max(fLineWidth, fMaxX);
632 nLineFullWordIndex = 0;
635 fLineWidth += fWordWidth;
639 if (nLineHead <= nTotalWords - 1) {
640 nLineTail = nTotalWords - 1;
650 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
651 fMaxY -= fLineDescent;
652 fMaxX =
std::max(fLineWidth, fMaxX);
654 return CPVT_FloatRect
(0
, 0
, fMaxX
, fMaxY
);
657CPVT_FloatRect CPVT_Section::OutputLines(
const CPVT_FloatRect& rect)
const {
659 float fLineIndent = m_pVT->GetLineIndent();
660 float fTypesetWidth = std::max(m_pVT->GetPlateWidth() - fLineIndent, 0.0f);
661 switch (m_pVT->GetAlignment()) {
667 fMinX = (fTypesetWidth - rect
.Width()) * 0.5f;
670 fMinX = fTypesetWidth - rect
.Width();
676 int32_t nTotalLines = fxcrt::CollectionSize<int32_t>(m_LineArray);
677 if (nTotalLines > 0) {
680 for (int32_t l = 0; l < nTotalLines; l++) {
681 CPVT_Section::
Line* pLine = m_LineArray[l].get();
682 switch (m_pVT->GetAlignment()) {
694 fPosX += fLineIndent;
695 fPosY += m_pVT->GetLineLeading();
701 if (fxcrt::IndexInBounds(m_WordArray, w)) {
706 fPosX += m_pVT->GetWordWidth(*pWord);
712 return CPVT_FloatRect
(fMinX
, fMinY
, fMaxX
, fMaxY
);
715void CPVT_Section::ClearLeftWords(int32_t nWordIndex) {
716 for (int32_t i = nWordIndex; i >= 0; i--) {
717 if (fxcrt::IndexInBounds(m_WordArray, i))
718 m_WordArray.erase(m_WordArray.begin() + i);
722void CPVT_Section::ClearRightWords(int32_t nWordIndex) {
723 int32_t sz = fxcrt::CollectionSize<int32_t>(m_WordArray);
724 for (int32_t i = sz - 1; i > nWordIndex; i--) {
725 if (fxcrt::IndexInBounds(m_WordArray, i))
726 m_WordArray.erase(m_WordArray.begin() + i);
730void CPVT_Section::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) {
731 for (int32_t i = nEndIndex; i > nBeginIndex; i--) {
732 if (fxcrt::IndexInBounds(m_WordArray, i))
733 m_WordArray.erase(m_WordArray.begin() + i);
747 }
else if (PlaceRange
.EndPos <= SecEndPos) {
755 if (fxcrt::IndexInBounds(m_WordArray, place.nWordIndex))
756 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)
CFX_PTemplate< float > CFX_PointF
CFX_STemplate< float > CFX_SizeF
#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)