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