xref: /aosp_15_r20/external/pdfium/core/fpdfdoc/cpvt_section.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2016 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/fpdfdoc/cpvt_section.h"
8 
9 #include <algorithm>
10 
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"
15 
16 namespace {
17 
18 constexpr 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,
30 };
31 
IsLatin(uint16_t word)32 bool IsLatin(uint16_t word) {
33   if (word <= 0x007F)
34     return !!(kSpecialChars[word] & 0x01);
35 
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));
43 }
44 
IsDigit(uint32_t word)45 bool IsDigit(uint32_t word) {
46   return word >= 0x0030 && word <= 0x0039;
47 }
48 
IsCJK(uint32_t word)49 bool 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)) {
58     return true;
59   }
60   if (word >= 0x3000 && word <= 0x303F) {
61     return (
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);
66   }
67   return word >= 0xFF66 && word <= 0xFF9D;
68 }
69 
IsPunctuation(uint32_t word)70 bool IsPunctuation(uint32_t word) {
71   if (word <= 0x007F)
72     return !!(kSpecialChars[word] & 0x08);
73 
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 ||
78             word == 0x00B8);
79   }
80 
81   if (word >= 0x2000 && word <= 0x206F) {
82     return (
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);
89   }
90 
91   if (word >= 0x3000 && word <= 0x303F) {
92     return (
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);
99   }
100 
101   if (word >= 0xFE50 && word <= 0xFE6F)
102     return (word >= 0xFE50 && word <= 0xFE5E) || word == 0xFE63;
103 
104   if (word >= 0xFF00 && word <= 0xFFEF) {
105     return (
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);
112   }
113 
114   return false;
115 }
116 
IsConnectiveSymbol(uint32_t word)117 bool IsConnectiveSymbol(uint32_t word) {
118   return word <= 0x007F && (kSpecialChars[word] & 0x20);
119 }
120 
IsOpenStylePunctuation(uint32_t word)121 bool IsOpenStylePunctuation(uint32_t word) {
122   if (word <= 0x007F)
123     return !!(kSpecialChars[word] & 0x04);
124 
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);
129 }
130 
IsCurrencySymbol(uint16_t word)131 bool 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);
137 }
138 
IsPrefixSymbol(uint16_t word)139 bool IsPrefixSymbol(uint16_t word) {
140   return IsCurrencySymbol(word) || word == 0x2116;
141 }
142 
IsSpace(uint16_t word)143 bool IsSpace(uint16_t word) {
144   return word == 0x0020 || word == 0x3000;
145 }
146 
NeedDivision(uint16_t prevWord,uint16_t curWord)147 bool NeedDivision(uint16_t prevWord, uint16_t curWord) {
148   if ((IsLatin(prevWord) || IsDigit(prevWord)) &&
149       (IsLatin(curWord) || IsDigit(curWord))) {
150     return false;
151   }
152   if (IsSpace(curWord) || IsPunctuation(curWord)) {
153     return false;
154   }
155   if (IsConnectiveSymbol(prevWord) || IsConnectiveSymbol(curWord)) {
156     return false;
157   }
158   if (IsSpace(prevWord) || IsPunctuation(prevWord)) {
159     return true;
160   }
161   if (IsPrefixSymbol(prevWord)) {
162     return false;
163   }
164   if (IsPrefixSymbol(curWord) || IsCJK(curWord)) {
165     return true;
166   }
167   if (IsCJK(prevWord)) {
168     return true;
169   }
170   return false;
171 }
172 
173 }  // namespace
174 
Line(const CPVT_LineInfo & lineinfo)175 CPVT_Section::Line::Line(const CPVT_LineInfo& lineinfo)
176     : m_LineInfo(lineinfo) {}
177 
178 CPVT_Section::Line::~Line() = default;
179 
GetBeginWordPlace() const180 CPVT_WordPlace CPVT_Section::Line::GetBeginWordPlace() const {
181   return CPVT_WordPlace(m_LinePlace.nSecIndex, m_LinePlace.nLineIndex, -1);
182 }
183 
GetEndWordPlace() const184 CPVT_WordPlace CPVT_Section::Line::GetEndWordPlace() const {
185   return CPVT_WordPlace(m_LinePlace.nSecIndex, m_LinePlace.nLineIndex,
186                         m_LineInfo.nEndWordIndex);
187 }
188 
GetPrevWordPlace(const CPVT_WordPlace & place) const189 CPVT_WordPlace CPVT_Section::Line::GetPrevWordPlace(
190     const CPVT_WordPlace& place) const {
191   if (place.nWordIndex > m_LineInfo.nEndWordIndex) {
192     return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
193                           m_LineInfo.nEndWordIndex);
194   }
195   return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
196                         place.nWordIndex - 1);
197 }
198 
GetNextWordPlace(const CPVT_WordPlace & place) const199 CPVT_WordPlace CPVT_Section::Line::GetNextWordPlace(
200     const CPVT_WordPlace& place) const {
201   if (place.nWordIndex < m_LineInfo.nBeginWordIndex) {
202     return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
203                           m_LineInfo.nBeginWordIndex);
204   }
205   return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
206                         place.nWordIndex + 1);
207 }
208 
CPVT_Section(CPVT_VariableText * pVT)209 CPVT_Section::CPVT_Section(CPVT_VariableText* pVT) : m_pVT(pVT) {
210   DCHECK(m_pVT);
211 }
212 
213 CPVT_Section::~CPVT_Section() = default;
214 
ResetLinePlace()215 void CPVT_Section::ResetLinePlace() {
216   int32_t i = 0;
217   for (auto& pLine : m_LineArray) {
218     pLine->m_LinePlace = CPVT_WordPlace(m_SecPlace.nSecIndex, i, -1);
219     ++i;
220   }
221 }
222 
AddWord(const CPVT_WordPlace & place,const CPVT_WordInfo & wordinfo)223 CPVT_WordPlace CPVT_Section::AddWord(const CPVT_WordPlace& place,
224                                      const CPVT_WordInfo& wordinfo) {
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));
229   return place;
230 }
231 
AddLine(const CPVT_LineInfo & lineinfo)232 CPVT_WordPlace CPVT_Section::AddLine(const CPVT_LineInfo& lineinfo) {
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);
236 }
237 
Rearrange()238 CPVT_FloatRect CPVT_Section::Rearrange() {
239   if (m_pVT->GetCharArray() > 0)
240     return RearrangeCharArray();
241   return RearrangeTypeset();
242 }
243 
GetSectionSize(float fFontSize)244 CFX_SizeF CPVT_Section::GetSectionSize(float fFontSize) {
245   CPVT_FloatRect result = SplitLines(/*bTypeset=*/false, fFontSize);
246   return CFX_SizeF(result.Width(), result.Height());
247 }
248 
GetBeginWordPlace() const249 CPVT_WordPlace CPVT_Section::GetBeginWordPlace() const {
250   if (m_LineArray.empty())
251     return m_SecPlace;
252   return m_LineArray.front()->GetBeginWordPlace();
253 }
254 
GetEndWordPlace() const255 CPVT_WordPlace CPVT_Section::GetEndWordPlace() const {
256   if (m_LineArray.empty())
257     return m_SecPlace;
258   return m_LineArray.back()->GetEndWordPlace();
259 }
260 
GetPrevWordPlace(const CPVT_WordPlace & place) const261 CPVT_WordPlace CPVT_Section::GetPrevWordPlace(
262     const CPVT_WordPlace& place) const {
263   if (place.nLineIndex < 0)
264     return GetBeginWordPlace();
265 
266   if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
267     return GetEndWordPlace();
268 
269   Line* pLine = m_LineArray[place.nLineIndex].get();
270   if (place.nWordIndex == pLine->m_LineInfo.nBeginWordIndex)
271     return CPVT_WordPlace(place.nSecIndex, place.nLineIndex, -1);
272 
273   if (place.nWordIndex >= pLine->m_LineInfo.nBeginWordIndex)
274     return pLine->GetPrevWordPlace(place);
275 
276   if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex - 1))
277     return place;
278 
279   return m_LineArray[place.nLineIndex - 1]->GetEndWordPlace();
280 }
281 
GetNextWordPlace(const CPVT_WordPlace & place) const282 CPVT_WordPlace CPVT_Section::GetNextWordPlace(
283     const CPVT_WordPlace& place) const {
284   if (place.nLineIndex < 0)
285     return GetBeginWordPlace();
286 
287   if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
288     return GetEndWordPlace();
289 
290   Line* pLine = m_LineArray[place.nLineIndex].get();
291   if (place.nWordIndex < pLine->m_LineInfo.nEndWordIndex)
292     return pLine->GetNextWordPlace(place);
293 
294   if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex + 1))
295     return place;
296 
297   return m_LineArray[place.nLineIndex + 1]->GetBeginWordPlace();
298 }
299 
UpdateWordPlace(CPVT_WordPlace & place) const300 void CPVT_Section::UpdateWordPlace(CPVT_WordPlace& place) const {
301   int32_t nLeft = 0;
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();
306     if (place.nWordIndex < pLine->m_LineInfo.nBeginWordIndex) {
307       nRight = nMid - 1;
308       nMid = (nLeft + nRight) / 2;
309     } else if (place.nWordIndex > pLine->m_LineInfo.nEndWordIndex) {
310       nLeft = nMid + 1;
311       nMid = (nLeft + nRight) / 2;
312     } else {
313       place.nLineIndex = nMid;
314       return;
315     }
316   }
317 }
318 
SearchWordPlace(const CFX_PointF & point) const319 CPVT_WordPlace CPVT_Section::SearchWordPlace(const CFX_PointF& point) const {
320   CPVT_WordPlace place = GetBeginWordPlace();
321   bool bUp = true;
322   bool bDown = true;
323   int32_t nLeft = 0;
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();
330     float fBottom = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineDescent;
331     if (FXSYS_IsFloatBigger(point.y, fTop))
332       bUp = false;
333     if (FXSYS_IsFloatSmaller(point.y, fBottom))
334       bDown = false;
335     if (FXSYS_IsFloatSmaller(point.y, fTop)) {
336       nRight = nMid - 1;
337       nMid = (nLeft + nRight) / 2;
338       continue;
339     }
340     if (FXSYS_IsFloatBigger(point.y, fBottom)) {
341       nLeft = nMid + 1;
342       nMid = (nLeft + nRight) / 2;
343       continue;
344     }
345     place = SearchWordPlace(
346         point.x,
347         CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()),
348                        pLine->GetEndWordPlace()));
349     place.nLineIndex = nMid;
350     return place;
351   }
352   if (bUp)
353     place = GetBeginWordPlace();
354   if (bDown)
355     place = GetEndWordPlace();
356   return place;
357 }
358 
SearchWordPlace(float fx,const CPVT_WordPlace & lineplace) const359 CPVT_WordPlace CPVT_Section::SearchWordPlace(
360     float fx,
361     const CPVT_WordPlace& lineplace) const {
362   if (!fxcrt::IndexInBounds(m_LineArray, lineplace.nLineIndex))
363     return GetBeginWordPlace();
364 
365   Line* pLine = m_LineArray[lineplace.nLineIndex].get();
366   return SearchWordPlace(
367       fx - m_Rect.left,
368       CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()),
369                      pLine->GetEndWordPlace()));
370 }
371 
SearchWordPlace(float fx,const CPVT_WordRange & range) const372 CPVT_WordPlace CPVT_Section::SearchWordPlace(
373     float fx,
374     const CPVT_WordRange& range) const {
375   CPVT_WordPlace wordplace = range.BeginPos;
376   wordplace.nWordIndex = -1;
377 
378   int32_t nLeft = range.BeginPos.nWordIndex;
379   int32_t nRight = range.EndPos.nWordIndex + 1;
380   int32_t nMid = (nLeft + nRight) / 2;
381   while (nLeft < nRight) {
382     if (nMid == nLeft)
383       break;
384     if (nMid == nRight) {
385       nMid--;
386       break;
387     }
388     if (!fxcrt::IndexInBounds(m_WordArray, nMid))
389       break;
390     CPVT_WordInfo* pWord = m_WordArray[nMid].get();
391     if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f) {
392       nLeft = nMid;
393       nMid = (nLeft + nRight) / 2;
394       continue;
395     }
396     nRight = nMid;
397     nMid = (nLeft + nRight) / 2;
398   }
399   if (fxcrt::IndexInBounds(m_WordArray, nMid)) {
400     CPVT_WordInfo* pWord = m_WordArray[nMid].get();
401     if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f)
402       wordplace.nWordIndex = nMid;
403   }
404   return wordplace;
405 }
406 
GetLineArraySize() const407 int32_t CPVT_Section::GetLineArraySize() const {
408   return fxcrt::CollectionSize<int32_t>(m_LineArray);
409 }
410 
GetLineFromArray(int32_t index) const411 const CPVT_Section::Line* CPVT_Section::GetLineFromArray(int32_t index) const {
412   if (!fxcrt::IndexInBounds(m_LineArray, index))
413     return nullptr;
414 
415   return m_LineArray[index].get();
416 }
417 
GetWordArraySize() const418 int32_t CPVT_Section::GetWordArraySize() const {
419   return fxcrt::CollectionSize<int32_t>(m_WordArray);
420 }
421 
GetWordFromArray(int32_t index) const422 const CPVT_WordInfo* CPVT_Section::GetWordFromArray(int32_t index) const {
423   if (!fxcrt::IndexInBounds(m_WordArray, index))
424     return nullptr;
425 
426   return m_WordArray[index].get();
427 }
428 
EraseWordsFrom(int32_t index)429 void CPVT_Section::EraseWordsFrom(int32_t index) {
430   if (!fxcrt::IndexInBounds(m_WordArray, index))
431     return;
432 
433   m_WordArray.erase(m_WordArray.begin() + index, m_WordArray.end());
434 }
435 
RearrangeCharArray() const436 CPVT_FloatRect CPVT_Section::RearrangeCharArray() const {
437   if (m_LineArray.empty())
438     return CPVT_FloatRect();
439 
440   float fNodeWidth = m_pVT->GetPlateWidth() /
441                      (m_pVT->GetCharArray() <= 0 ? 1 : m_pVT->GetCharArray());
442   float fLineAscent =
443       m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
444   float fLineDescent =
445       m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
446   float x = 0.0f;
447   float y = m_pVT->GetLineLeading() + fLineAscent;
448   int32_t nStart = 0;
449   CPVT_Section::Line* pLine = m_LineArray.front().get();
450   switch (m_pVT->GetAlignment()) {
451     case 0:
452       pLine->m_LineInfo.fLineX = fNodeWidth * 0.5f;
453       break;
454     case 1:
455       nStart = (m_pVT->GetCharArray() -
456                 fxcrt::CollectionSize<int32_t>(m_WordArray)) /
457                2;
458       pLine->m_LineInfo.fLineX = fNodeWidth * nStart - fNodeWidth * 0.5f;
459       break;
460     case 2:
461       nStart =
462           m_pVT->GetCharArray() - fxcrt::CollectionSize<int32_t>(m_WordArray);
463       pLine->m_LineInfo.fLineX = fNodeWidth * nStart - fNodeWidth * 0.5f;
464       break;
465   }
466   for (int32_t w = 0, sz = fxcrt::CollectionSize<int32_t>(m_WordArray); w < sz;
467        w++) {
468     if (w >= m_pVT->GetCharArray())
469       break;
470 
471     float fNextWidth = 0;
472     if (fxcrt::IndexInBounds(m_WordArray, w + 1)) {
473       CPVT_WordInfo* pNextWord = m_WordArray[w + 1].get();
474       pNextWord->fWordTail = 0;
475       fNextWidth = m_pVT->GetWordWidth(*pNextWord);
476     }
477     CPVT_WordInfo* pWord = m_WordArray[w].get();
478     pWord->fWordTail = 0;
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);
483     pWord->fWordX = x;
484     pWord->fWordY = y;
485     if (w == 0) {
486       pLine->m_LineInfo.fLineX = x;
487     }
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
491                               : 0);
492     } else {
493       pWord->fWordTail = 0;
494     }
495     x += fWordWidth;
496     fLineAscent = std::max(fLineAscent, fWordAscent);
497     fLineDescent = std::min(fLineDescent, fWordDescent);
498   }
499   pLine->m_LineInfo.nBeginWordIndex = 0;
500   pLine->m_LineInfo.nEndWordIndex =
501       fxcrt::CollectionSize<int32_t>(m_WordArray) - 1;
502   pLine->m_LineInfo.fLineY = y;
503   pLine->m_LineInfo.fLineWidth = x - pLine->m_LineInfo.fLineX;
504   pLine->m_LineInfo.fLineAscent = fLineAscent;
505   pLine->m_LineInfo.fLineDescent = fLineDescent;
506   return CPVT_FloatRect(0, 0, x, y - fLineDescent);
507 }
508 
RearrangeTypeset()509 CPVT_FloatRect CPVT_Section::RearrangeTypeset() {
510   m_LineArray.clear();
511   return OutputLines(SplitLines(/*bTypeset=*/true, /*fFontSize=*/0.0f));
512 }
513 
SplitLines(bool bTypeset,float fFontSize)514 CPVT_FloatRect CPVT_Section::SplitLines(bool bTypeset, float fFontSize) {
515   CPVT_LineInfo line;
516   if (m_WordArray.empty()) {
517     float fLineAscent;
518     float fLineDescent;
519     if (bTypeset) {
520       fLineAscent = m_pVT->GetLineAscent();
521       fLineDescent = m_pVT->GetLineDescent();
522       line.nBeginWordIndex = -1;
523       line.nEndWordIndex = -1;
524       line.nTotalWord = 0;
525       line.fLineWidth = 0;
526       line.fLineAscent = fLineAscent;
527       line.fLineDescent = fLineDescent;
528       AddLine(line);
529     } else {
530       fLineAscent =
531           m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), fFontSize);
532       fLineDescent =
533           m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), fFontSize);
534     }
535     float fMaxY = m_pVT->GetLineLeading() + fLineAscent - fLineDescent;
536     return CPVT_FloatRect(0, 0, 0, fMaxY);
537   }
538 
539   int32_t nLineHead = 0;
540   int32_t nLineTail = 0;
541   float fMaxX = 0.0f;
542   float fMaxY = 0.0f;
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;
558   int32_t i = 0;
559   while (i < nTotalWords) {
560     CPVT_WordInfo* pWord = m_WordArray[i].get();
561     CPVT_WordInfo* pOldWord = pWord;
562     if (i > 0) {
563       pOldWord = m_WordArray[i - 1].get();
564     }
565     if (pWord) {
566       if (bTypeset) {
567         fLineAscent = std::max(fLineAscent, m_pVT->GetWordAscent(*pWord));
568         fLineDescent = std::min(fLineDescent, m_pVT->GetWordDescent(*pWord));
569         fWordWidth = m_pVT->GetWordWidth(*pWord);
570       } else {
571         fLineAscent =
572             std::max(fLineAscent, m_pVT->GetWordAscent(*pWord, fFontSize));
573         fLineDescent =
574             std::min(fLineDescent, m_pVT->GetWordDescent(*pWord, fFontSize));
575         fWordWidth = m_pVT->GetWordWidth(pWord->nFontIndex, pWord->Word,
576                                          m_pVT->GetSubWord(), fFontSize,
577                                          pWord->fWordTail);
578       }
579       if (!bOpened) {
580         if (IsOpenStylePunctuation(pWord->Word)) {
581           bOpened = true;
582           bFullWord = true;
583         } else if (pOldWord) {
584           if (NeedDivision(pOldWord->Word, pWord->Word)) {
585             bFullWord = true;
586           }
587         }
588       } else {
589         if (!IsSpace(pWord->Word) && !IsOpenStylePunctuation(pWord->Word)) {
590           bOpened = false;
591         }
592       }
593       if (bFullWord) {
594         bFullWord = false;
595         if (nCharIndex > 0) {
596           nLineFullWordIndex++;
597         }
598         nWordStartPos = i;
599         fBackupLineWidth = fLineWidth;
600         fBackupLineAscent = fLineAscent;
601         fBackupLineDescent = fLineDescent;
602       }
603       nCharIndex++;
604     }
605     if (m_pVT->IsAutoReturn() && fTypesetWidth > 0 &&
606         fLineWidth + fWordWidth > fTypesetWidth) {
607       if (nLineFullWordIndex > 0) {
608         i = nWordStartPos;
609         fLineWidth = fBackupLineWidth;
610         fLineAscent = fBackupLineAscent;
611         fLineDescent = fBackupLineDescent;
612       }
613       if (nCharIndex == 1) {
614         fLineWidth = fWordWidth;
615         i++;
616       }
617       nLineTail = i - 1;
618       if (bTypeset) {
619         line.nBeginWordIndex = nLineHead;
620         line.nEndWordIndex = nLineTail;
621         line.nTotalWord = nLineTail - nLineHead + 1;
622         line.fLineWidth = fLineWidth;
623         line.fLineAscent = fLineAscent;
624         line.fLineDescent = fLineDescent;
625         AddLine(line);
626       }
627       fMaxY += (fLineAscent + m_pVT->GetLineLeading());
628       fMaxY -= fLineDescent;
629       fMaxX = std::max(fLineWidth, fMaxX);
630       nLineHead = i;
631       fLineWidth = 0.0f;
632       fLineAscent = 0.0f;
633       fLineDescent = 0.0f;
634       nCharIndex = 0;
635       nLineFullWordIndex = 0;
636       bFullWord = false;
637     } else {
638       fLineWidth += fWordWidth;
639       i++;
640     }
641   }
642   if (nLineHead <= nTotalWords - 1) {
643     nLineTail = nTotalWords - 1;
644     if (bTypeset) {
645       line.nBeginWordIndex = nLineHead;
646       line.nEndWordIndex = nLineTail;
647       line.nTotalWord = nLineTail - nLineHead + 1;
648       line.fLineWidth = fLineWidth;
649       line.fLineAscent = fLineAscent;
650       line.fLineDescent = fLineDescent;
651       AddLine(line);
652     }
653     fMaxY += (fLineAscent + m_pVT->GetLineLeading());
654     fMaxY -= fLineDescent;
655     fMaxX = std::max(fLineWidth, fMaxX);
656   }
657   return CPVT_FloatRect(0, 0, fMaxX, fMaxY);
658 }
659 
OutputLines(const CPVT_FloatRect & rect) const660 CPVT_FloatRect CPVT_Section::OutputLines(const CPVT_FloatRect& rect) const {
661   float fMinX;
662   float fLineIndent = m_pVT->GetLineIndent();
663   float fTypesetWidth = std::max(m_pVT->GetPlateWidth() - fLineIndent, 0.0f);
664   switch (m_pVT->GetAlignment()) {
665     default:
666     case 0:
667       fMinX = 0.0f;
668       break;
669     case 1:
670       fMinX = (fTypesetWidth - rect.Width()) * 0.5f;
671       break;
672     case 2:
673       fMinX = fTypesetWidth - rect.Width();
674       break;
675   }
676   float fMaxX = fMinX + rect.Width();
677   float fMinY = 0.0f;
678   float fMaxY = rect.Height();
679   int32_t nTotalLines = fxcrt::CollectionSize<int32_t>(m_LineArray);
680   if (nTotalLines > 0) {
681     float fPosX = 0.0f;
682     float fPosY = 0.0f;
683     for (int32_t l = 0; l < nTotalLines; l++) {
684       CPVT_Section::Line* pLine = m_LineArray[l].get();
685       switch (m_pVT->GetAlignment()) {
686         default:
687         case 0:
688           fPosX = 0;
689           break;
690         case 1:
691           fPosX = (fTypesetWidth - pLine->m_LineInfo.fLineWidth) * 0.5f;
692           break;
693         case 2:
694           fPosX = fTypesetWidth - pLine->m_LineInfo.fLineWidth;
695           break;
696       }
697       fPosX += fLineIndent;
698       fPosY += m_pVT->GetLineLeading();
699       fPosY += pLine->m_LineInfo.fLineAscent;
700       pLine->m_LineInfo.fLineX = fPosX - fMinX;
701       pLine->m_LineInfo.fLineY = fPosY - fMinY;
702       for (int32_t w = pLine->m_LineInfo.nBeginWordIndex;
703            w <= pLine->m_LineInfo.nEndWordIndex; w++) {
704         if (fxcrt::IndexInBounds(m_WordArray, w)) {
705           CPVT_WordInfo* pWord = m_WordArray[w].get();
706           pWord->fWordX = fPosX - fMinX;
707           pWord->fWordY = fPosY - fMinY;
708 
709           fPosX += m_pVT->GetWordWidth(*pWord);
710         }
711       }
712       fPosY -= pLine->m_LineInfo.fLineDescent;
713     }
714   }
715   return CPVT_FloatRect(fMinX, fMinY, fMaxX, fMaxY);
716 }
717 
ClearLeftWords(int32_t nWordIndex)718 void 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);
722   }
723 }
724 
ClearRightWords(int32_t nWordIndex)725 void 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);
730   }
731 }
732 
ClearMidWords(int32_t nBeginIndex,int32_t nEndIndex)733 void 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);
737   }
738 }
739 
ClearWords(const CPVT_WordRange & PlaceRange)740 void CPVT_Section::ClearWords(const CPVT_WordRange& PlaceRange) {
741   CPVT_WordPlace SecBeginPos = GetBeginWordPlace();
742   CPVT_WordPlace SecEndPos = GetEndWordPlace();
743   if (PlaceRange.BeginPos >= SecBeginPos) {
744     if (PlaceRange.EndPos <= SecEndPos) {
745       ClearMidWords(PlaceRange.BeginPos.nWordIndex,
746                     PlaceRange.EndPos.nWordIndex);
747     } else {
748       ClearRightWords(PlaceRange.BeginPos.nWordIndex);
749     }
750   } else if (PlaceRange.EndPos <= SecEndPos) {
751     ClearLeftWords(PlaceRange.EndPos.nWordIndex);
752   } else {
753     m_WordArray.clear();
754   }
755 }
756 
ClearWord(const CPVT_WordPlace & place)757 void CPVT_Section::ClearWord(const CPVT_WordPlace& place) {
758   if (fxcrt::IndexInBounds(m_WordArray, place.nWordIndex))
759     m_WordArray.erase(m_WordArray.begin() + place.nWordIndex);
760 }
761