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 #include "third_party/base/cxx17_backports.h"
16
17 namespace {
18
19 constexpr uint8_t kSpecialChars[128] = {
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
82 if (word >= 0x2000 && word <= 0x206F) {
83 return (
84 word == 0x2010 || word == 0x2011 || word == 0x2012 || word == 0x2013 ||
85 word == 0x2018 || word == 0x2019 || word == 0x201A || word == 0x201B ||
86 word == 0x201C || word == 0x201D || word == 0x201E || word == 0x201F ||
87 word == 0x2032 || word == 0x2033 || word == 0x2034 || word == 0x2035 ||
88 word == 0x2036 || word == 0x2037 || word == 0x203C || word == 0x203D ||
89 word == 0x203E || word == 0x2044);
90 }
91
92 if (word >= 0x3000 && word <= 0x303F) {
93 return (
94 word == 0x3001 || word == 0x3002 || word == 0x3003 || word == 0x3005 ||
95 word == 0x3009 || word == 0x300A || word == 0x300B || word == 0x300C ||
96 word == 0x300D || word == 0x300F || word == 0x300E || word == 0x3010 ||
97 word == 0x3011 || word == 0x3014 || word == 0x3015 || word == 0x3016 ||
98 word == 0x3017 || word == 0x3018 || word == 0x3019 || word == 0x301A ||
99 word == 0x301B || word == 0x301D || word == 0x301E || word == 0x301F);
100 }
101
102 if (word >= 0xFE50 && word <= 0xFE6F)
103 return (word >= 0xFE50 && word <= 0xFE5E) || word == 0xFE63;
104
105 if (word >= 0xFF00 && word <= 0xFFEF) {
106 return (
107 word == 0xFF01 || word == 0xFF02 || word == 0xFF07 || word == 0xFF08 ||
108 word == 0xFF09 || word == 0xFF0C || word == 0xFF0E || word == 0xFF0F ||
109 word == 0xFF1A || word == 0xFF1B || word == 0xFF1F || word == 0xFF3B ||
110 word == 0xFF3D || word == 0xFF40 || word == 0xFF5B || word == 0xFF5C ||
111 word == 0xFF5D || word == 0xFF61 || word == 0xFF62 || word == 0xFF63 ||
112 word == 0xFF64 || word == 0xFF65 || word == 0xFF9E || word == 0xFF9F);
113 }
114
115 return false;
116 }
117
IsConnectiveSymbol(uint32_t word)118 bool IsConnectiveSymbol(uint32_t word) {
119 return word <= 0x007F && (kSpecialChars[word] & 0x20);
120 }
121
IsOpenStylePunctuation(uint32_t word)122 bool IsOpenStylePunctuation(uint32_t word) {
123 if (word <= 0x007F)
124 return !!(kSpecialChars[word] & 0x04);
125
126 return (word == 0x300A || word == 0x300C || word == 0x300E ||
127 word == 0x3010 || word == 0x3014 || word == 0x3016 ||
128 word == 0x3018 || word == 0x301A || word == 0xFF08 ||
129 word == 0xFF3B || word == 0xFF5B || word == 0xFF62);
130 }
131
IsCurrencySymbol(uint16_t word)132 bool IsCurrencySymbol(uint16_t word) {
133 return (word == 0x0024 || word == 0x0080 || word == 0x00A2 ||
134 word == 0x00A3 || word == 0x00A4 || word == 0x00A5 ||
135 (word >= 0x20A0 && word <= 0x20CF) || word == 0xFE69 ||
136 word == 0xFF04 || word == 0xFFE0 || word == 0xFFE1 ||
137 word == 0xFFE5 || word == 0xFFE6);
138 }
139
IsPrefixSymbol(uint16_t word)140 bool IsPrefixSymbol(uint16_t word) {
141 return IsCurrencySymbol(word) || word == 0x2116;
142 }
143
IsSpace(uint16_t word)144 bool IsSpace(uint16_t word) {
145 return word == 0x0020 || word == 0x3000;
146 }
147
NeedDivision(uint16_t prevWord,uint16_t curWord)148 bool NeedDivision(uint16_t prevWord, uint16_t curWord) {
149 if ((IsLatin(prevWord) || IsDigit(prevWord)) &&
150 (IsLatin(curWord) || IsDigit(curWord))) {
151 return false;
152 }
153 if (IsSpace(curWord) || IsPunctuation(curWord)) {
154 return false;
155 }
156 if (IsConnectiveSymbol(prevWord) || IsConnectiveSymbol(curWord)) {
157 return false;
158 }
159 if (IsSpace(prevWord) || IsPunctuation(prevWord)) {
160 return true;
161 }
162 if (IsPrefixSymbol(prevWord)) {
163 return false;
164 }
165 if (IsPrefixSymbol(curWord) || IsCJK(curWord)) {
166 return true;
167 }
168 if (IsCJK(prevWord)) {
169 return true;
170 }
171 return false;
172 }
173
174 } // namespace
175
Line(const CPVT_LineInfo & lineinfo)176 CPVT_Section::Line::Line(const CPVT_LineInfo& lineinfo)
177 : m_LineInfo(lineinfo) {}
178
179 CPVT_Section::Line::~Line() = default;
180
GetBeginWordPlace() const181 CPVT_WordPlace CPVT_Section::Line::GetBeginWordPlace() const {
182 return CPVT_WordPlace(m_LinePlace.nSecIndex, m_LinePlace.nLineIndex, -1);
183 }
184
GetEndWordPlace() const185 CPVT_WordPlace CPVT_Section::Line::GetEndWordPlace() const {
186 return CPVT_WordPlace(m_LinePlace.nSecIndex, m_LinePlace.nLineIndex,
187 m_LineInfo.nEndWordIndex);
188 }
189
GetPrevWordPlace(const CPVT_WordPlace & place) const190 CPVT_WordPlace CPVT_Section::Line::GetPrevWordPlace(
191 const CPVT_WordPlace& place) const {
192 if (place.nWordIndex > m_LineInfo.nEndWordIndex) {
193 return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
194 m_LineInfo.nEndWordIndex);
195 }
196 return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
197 place.nWordIndex - 1);
198 }
199
GetNextWordPlace(const CPVT_WordPlace & place) const200 CPVT_WordPlace CPVT_Section::Line::GetNextWordPlace(
201 const CPVT_WordPlace& place) const {
202 if (place.nWordIndex < m_LineInfo.nBeginWordIndex) {
203 return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
204 m_LineInfo.nBeginWordIndex);
205 }
206 return CPVT_WordPlace(place.nSecIndex, place.nLineIndex,
207 place.nWordIndex + 1);
208 }
209
CPVT_Section(CPVT_VariableText * pVT)210 CPVT_Section::CPVT_Section(CPVT_VariableText* pVT) : m_pVT(pVT) {
211 DCHECK(m_pVT);
212 }
213
214 CPVT_Section::~CPVT_Section() = default;
215
ResetLinePlace()216 void CPVT_Section::ResetLinePlace() {
217 int32_t i = 0;
218 for (auto& pLine : m_LineArray) {
219 pLine->m_LinePlace = CPVT_WordPlace(m_SecPlace.nSecIndex, i, -1);
220 ++i;
221 }
222 }
223
AddWord(const CPVT_WordPlace & place,const CPVT_WordInfo & wordinfo)224 CPVT_WordPlace CPVT_Section::AddWord(const CPVT_WordPlace& place,
225 const CPVT_WordInfo& wordinfo) {
226 int32_t nWordIndex = pdfium::clamp(
227 place.nWordIndex, 0, fxcrt::CollectionSize<int32_t>(m_WordArray));
228 m_WordArray.insert(m_WordArray.begin() + nWordIndex,
229 std::make_unique<CPVT_WordInfo>(wordinfo));
230 return place;
231 }
232
AddLine(const CPVT_LineInfo & lineinfo)233 CPVT_WordPlace CPVT_Section::AddLine(const CPVT_LineInfo& lineinfo) {
234 m_LineArray.push_back(std::make_unique<Line>(lineinfo));
235 return CPVT_WordPlace(m_SecPlace.nSecIndex,
236 fxcrt::CollectionSize<int32_t>(m_LineArray) - 1, -1);
237 }
238
Rearrange()239 CPVT_FloatRect CPVT_Section::Rearrange() {
240 if (m_pVT->GetCharArray() > 0)
241 return RearrangeCharArray();
242 return RearrangeTypeset();
243 }
244
GetSectionSize(float fFontSize)245 CFX_SizeF CPVT_Section::GetSectionSize(float fFontSize) {
246 CPVT_FloatRect result = SplitLines(/*bTypeset=*/false, fFontSize);
247 return CFX_SizeF(result.Width(), result.Height());
248 }
249
GetBeginWordPlace() const250 CPVT_WordPlace CPVT_Section::GetBeginWordPlace() const {
251 if (m_LineArray.empty())
252 return m_SecPlace;
253 return m_LineArray.front()->GetBeginWordPlace();
254 }
255
GetEndWordPlace() const256 CPVT_WordPlace CPVT_Section::GetEndWordPlace() const {
257 if (m_LineArray.empty())
258 return m_SecPlace;
259 return m_LineArray.back()->GetEndWordPlace();
260 }
261
GetPrevWordPlace(const CPVT_WordPlace & place) const262 CPVT_WordPlace CPVT_Section::GetPrevWordPlace(
263 const CPVT_WordPlace& place) const {
264 if (place.nLineIndex < 0)
265 return GetBeginWordPlace();
266
267 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
268 return GetEndWordPlace();
269
270 Line* pLine = m_LineArray[place.nLineIndex].get();
271 if (place.nWordIndex == pLine->m_LineInfo.nBeginWordIndex)
272 return CPVT_WordPlace(place.nSecIndex, place.nLineIndex, -1);
273
274 if (place.nWordIndex >= pLine->m_LineInfo.nBeginWordIndex)
275 return pLine->GetPrevWordPlace(place);
276
277 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex - 1))
278 return place;
279
280 return m_LineArray[place.nLineIndex - 1]->GetEndWordPlace();
281 }
282
GetNextWordPlace(const CPVT_WordPlace & place) const283 CPVT_WordPlace CPVT_Section::GetNextWordPlace(
284 const CPVT_WordPlace& place) const {
285 if (place.nLineIndex < 0)
286 return GetBeginWordPlace();
287
288 if (place.nLineIndex >= fxcrt::CollectionSize<int32_t>(m_LineArray))
289 return GetEndWordPlace();
290
291 Line* pLine = m_LineArray[place.nLineIndex].get();
292 if (place.nWordIndex < pLine->m_LineInfo.nEndWordIndex)
293 return pLine->GetNextWordPlace(place);
294
295 if (!fxcrt::IndexInBounds(m_LineArray, place.nLineIndex + 1))
296 return place;
297
298 return m_LineArray[place.nLineIndex + 1]->GetBeginWordPlace();
299 }
300
UpdateWordPlace(CPVT_WordPlace & place) const301 void CPVT_Section::UpdateWordPlace(CPVT_WordPlace& place) const {
302 int32_t nLeft = 0;
303 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
304 int32_t nMid = (nLeft + nRight) / 2;
305 while (nLeft <= nRight) {
306 Line* pLine = m_LineArray[nMid].get();
307 if (place.nWordIndex < pLine->m_LineInfo.nBeginWordIndex) {
308 nRight = nMid - 1;
309 nMid = (nLeft + nRight) / 2;
310 } else if (place.nWordIndex > pLine->m_LineInfo.nEndWordIndex) {
311 nLeft = nMid + 1;
312 nMid = (nLeft + nRight) / 2;
313 } else {
314 place.nLineIndex = nMid;
315 return;
316 }
317 }
318 }
319
SearchWordPlace(const CFX_PointF & point) const320 CPVT_WordPlace CPVT_Section::SearchWordPlace(const CFX_PointF& point) const {
321 CPVT_WordPlace place = GetBeginWordPlace();
322 bool bUp = true;
323 bool bDown = true;
324 int32_t nLeft = 0;
325 int32_t nRight = fxcrt::CollectionSize<int32_t>(m_LineArray) - 1;
326 int32_t nMid = fxcrt::CollectionSize<int32_t>(m_LineArray) / 2;
327 while (nLeft <= nRight) {
328 Line* pLine = m_LineArray[nMid].get();
329 float fTop = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineAscent -
330 m_pVT->GetLineLeading();
331 float fBottom = pLine->m_LineInfo.fLineY - pLine->m_LineInfo.fLineDescent;
332 if (FXSYS_IsFloatBigger(point.y, fTop))
333 bUp = false;
334 if (FXSYS_IsFloatSmaller(point.y, fBottom))
335 bDown = false;
336 if (FXSYS_IsFloatSmaller(point.y, fTop)) {
337 nRight = nMid - 1;
338 nMid = (nLeft + nRight) / 2;
339 continue;
340 }
341 if (FXSYS_IsFloatBigger(point.y, fBottom)) {
342 nLeft = nMid + 1;
343 nMid = (nLeft + nRight) / 2;
344 continue;
345 }
346 place = SearchWordPlace(
347 point.x,
348 CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()),
349 pLine->GetEndWordPlace()));
350 place.nLineIndex = nMid;
351 return place;
352 }
353 if (bUp)
354 place = GetBeginWordPlace();
355 if (bDown)
356 place = GetEndWordPlace();
357 return place;
358 }
359
SearchWordPlace(float fx,const CPVT_WordPlace & lineplace) const360 CPVT_WordPlace CPVT_Section::SearchWordPlace(
361 float fx,
362 const CPVT_WordPlace& lineplace) const {
363 if (!fxcrt::IndexInBounds(m_LineArray, lineplace.nLineIndex))
364 return GetBeginWordPlace();
365
366 Line* pLine = m_LineArray[lineplace.nLineIndex].get();
367 return SearchWordPlace(
368 fx - m_Rect.left,
369 CPVT_WordRange(pLine->GetNextWordPlace(pLine->GetBeginWordPlace()),
370 pLine->GetEndWordPlace()));
371 }
372
SearchWordPlace(float fx,const CPVT_WordRange & range) const373 CPVT_WordPlace CPVT_Section::SearchWordPlace(
374 float fx,
375 const CPVT_WordRange& range) const {
376 CPVT_WordPlace wordplace = range.BeginPos;
377 wordplace.nWordIndex = -1;
378
379 int32_t nLeft = range.BeginPos.nWordIndex;
380 int32_t nRight = range.EndPos.nWordIndex + 1;
381 int32_t nMid = (nLeft + nRight) / 2;
382 while (nLeft < nRight) {
383 if (nMid == nLeft)
384 break;
385 if (nMid == nRight) {
386 nMid--;
387 break;
388 }
389 if (!fxcrt::IndexInBounds(m_WordArray, nMid))
390 break;
391 CPVT_WordInfo* pWord = m_WordArray[nMid].get();
392 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f) {
393 nLeft = nMid;
394 nMid = (nLeft + nRight) / 2;
395 continue;
396 }
397 nRight = nMid;
398 nMid = (nLeft + nRight) / 2;
399 }
400 if (fxcrt::IndexInBounds(m_WordArray, nMid)) {
401 CPVT_WordInfo* pWord = m_WordArray[nMid].get();
402 if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * 0.5f)
403 wordplace.nWordIndex = nMid;
404 }
405 return wordplace;
406 }
407
GetLineArraySize() const408 int32_t CPVT_Section::GetLineArraySize() const {
409 return fxcrt::CollectionSize<int32_t>(m_LineArray);
410 }
411
GetLineFromArray(int32_t index) const412 const CPVT_Section::Line* CPVT_Section::GetLineFromArray(int32_t index) const {
413 if (!fxcrt::IndexInBounds(m_LineArray, index))
414 return nullptr;
415
416 return m_LineArray[index].get();
417 }
418
GetWordArraySize() const419 int32_t CPVT_Section::GetWordArraySize() const {
420 return fxcrt::CollectionSize<int32_t>(m_WordArray);
421 }
422
GetWordFromArray(int32_t index) const423 const CPVT_WordInfo* CPVT_Section::GetWordFromArray(int32_t index) const {
424 if (!fxcrt::IndexInBounds(m_WordArray, index))
425 return nullptr;
426
427 return m_WordArray[index].get();
428 }
429
EraseWordsFrom(int32_t index)430 void CPVT_Section::EraseWordsFrom(int32_t index) {
431 if (!fxcrt::IndexInBounds(m_WordArray, index))
432 return;
433
434 m_WordArray.erase(m_WordArray.begin() + index, m_WordArray.end());
435 }
436
RearrangeCharArray() const437 CPVT_FloatRect CPVT_Section::RearrangeCharArray() const {
438 if (m_LineArray.empty())
439 return CPVT_FloatRect();
440
441 float fNodeWidth = m_pVT->GetPlateWidth() /
442 (m_pVT->GetCharArray() <= 0 ? 1 : m_pVT->GetCharArray());
443 float fLineAscent =
444 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
445 float fLineDescent =
446 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
447 float x = 0.0f;
448 float y = m_pVT->GetLineLeading() + fLineAscent;
449 int32_t nStart = 0;
450 CPVT_Section::Line* pLine = m_LineArray.front().get();
451 switch (m_pVT->GetAlignment()) {
452 case 0:
453 pLine->m_LineInfo.fLineX = fNodeWidth * 0.5f;
454 break;
455 case 1:
456 nStart = (m_pVT->GetCharArray() -
457 fxcrt::CollectionSize<int32_t>(m_WordArray)) /
458 2;
459 pLine->m_LineInfo.fLineX = fNodeWidth * nStart - fNodeWidth * 0.5f;
460 break;
461 case 2:
462 nStart =
463 m_pVT->GetCharArray() - fxcrt::CollectionSize<int32_t>(m_WordArray);
464 pLine->m_LineInfo.fLineX = fNodeWidth * nStart - fNodeWidth * 0.5f;
465 break;
466 }
467 for (int32_t w = 0, sz = fxcrt::CollectionSize<int32_t>(m_WordArray); w < sz;
468 w++) {
469 if (w >= m_pVT->GetCharArray())
470 break;
471
472 float fNextWidth = 0;
473 if (fxcrt::IndexInBounds(m_WordArray, w + 1)) {
474 CPVT_WordInfo* pNextWord = m_WordArray[w + 1].get();
475 pNextWord->fWordTail = 0;
476 fNextWidth = m_pVT->GetWordWidth(*pNextWord);
477 }
478 CPVT_WordInfo* pWord = m_WordArray[w].get();
479 pWord->fWordTail = 0;
480 float fWordWidth = m_pVT->GetWordWidth(*pWord);
481 float fWordAscent = m_pVT->GetWordAscent(*pWord);
482 float fWordDescent = m_pVT->GetWordDescent(*pWord);
483 x = (float)(fNodeWidth * (w + nStart + 0.5) - fWordWidth * 0.5f);
484 pWord->fWordX = x;
485 pWord->fWordY = y;
486 if (w == 0) {
487 pLine->m_LineInfo.fLineX = x;
488 }
489 if (w != fxcrt::CollectionSize<int32_t>(m_WordArray) - 1) {
490 pWord->fWordTail = (fNodeWidth - (fWordWidth + fNextWidth) * 0.5f > 0
491 ? fNodeWidth - (fWordWidth + fNextWidth) * 0.5f
492 : 0);
493 } else {
494 pWord->fWordTail = 0;
495 }
496 x += fWordWidth;
497 fLineAscent = std::max(fLineAscent, fWordAscent);
498 fLineDescent = std::min(fLineDescent, fWordDescent);
499 }
500 pLine->m_LineInfo.nBeginWordIndex = 0;
501 pLine->m_LineInfo.nEndWordIndex =
502 fxcrt::CollectionSize<int32_t>(m_WordArray) - 1;
503 pLine->m_LineInfo.fLineY = y;
504 pLine->m_LineInfo.fLineWidth = x - pLine->m_LineInfo.fLineX;
505 pLine->m_LineInfo.fLineAscent = fLineAscent;
506 pLine->m_LineInfo.fLineDescent = fLineDescent;
507 return CPVT_FloatRect(0, 0, x, y - fLineDescent);
508 }
509
RearrangeTypeset()510 CPVT_FloatRect CPVT_Section::RearrangeTypeset() {
511 m_LineArray.clear();
512 return OutputLines(SplitLines(/*bTypeset=*/true, /*fFontSize=*/0.0f));
513 }
514
SplitLines(bool bTypeset,float fFontSize)515 CPVT_FloatRect CPVT_Section::SplitLines(bool bTypeset, float fFontSize) {
516 CPVT_LineInfo line;
517 if (m_WordArray.empty()) {
518 float fLineAscent;
519 float fLineDescent;
520 if (bTypeset) {
521 fLineAscent = m_pVT->GetLineAscent();
522 fLineDescent = m_pVT->GetLineDescent();
523 line.nBeginWordIndex = -1;
524 line.nEndWordIndex = -1;
525 line.nTotalWord = 0;
526 line.fLineWidth = 0;
527 line.fLineAscent = fLineAscent;
528 line.fLineDescent = fLineDescent;
529 AddLine(line);
530 } else {
531 fLineAscent =
532 m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), fFontSize);
533 fLineDescent =
534 m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), fFontSize);
535 }
536 float fMaxY = m_pVT->GetLineLeading() + fLineAscent - fLineDescent;
537 return CPVT_FloatRect(0, 0, 0, fMaxY);
538 }
539
540 int32_t nLineHead = 0;
541 int32_t nLineTail = 0;
542 float fMaxX = 0.0f;
543 float fMaxY = 0.0f;
544 float fLineWidth = 0.0f;
545 float fBackupLineWidth = 0.0f;
546 float fLineAscent = 0.0f;
547 float fBackupLineAscent = 0.0f;
548 float fLineDescent = 0.0f;
549 float fBackupLineDescent = 0.0f;
550 int32_t nWordStartPos = 0;
551 bool bFullWord = false;
552 int32_t nLineFullWordIndex = 0;
553 int32_t nCharIndex = 0;
554 float fWordWidth = 0;
555 float fTypesetWidth =
556 std::max(m_pVT->GetPlateWidth() - m_pVT->GetLineIndent(), 0.0f);
557 int32_t nTotalWords = fxcrt::CollectionSize<int32_t>(m_WordArray);
558 bool bOpened = false;
559 int32_t i = 0;
560 while (i < nTotalWords) {
561 CPVT_WordInfo* pWord = m_WordArray[i].get();
562 CPVT_WordInfo* pOldWord = pWord;
563 if (i > 0) {
564 pOldWord = m_WordArray[i - 1].get();
565 }
566 if (pWord) {
567 if (bTypeset) {
568 fLineAscent = std::max(fLineAscent, m_pVT->GetWordAscent(*pWord));
569 fLineDescent = std::min(fLineDescent, m_pVT->GetWordDescent(*pWord));
570 fWordWidth = m_pVT->GetWordWidth(*pWord);
571 } else {
572 fLineAscent =
573 std::max(fLineAscent, m_pVT->GetWordAscent(*pWord, fFontSize));
574 fLineDescent =
575 std::min(fLineDescent, m_pVT->GetWordDescent(*pWord, fFontSize));
576 fWordWidth = m_pVT->GetWordWidth(pWord->nFontIndex, pWord->Word,
577 m_pVT->GetSubWord(), fFontSize,
578 pWord->fWordTail);
579 }
580 if (!bOpened) {
581 if (IsOpenStylePunctuation(pWord->Word)) {
582 bOpened = true;
583 bFullWord = true;
584 } else if (pOldWord) {
585 if (NeedDivision(pOldWord->Word, pWord->Word)) {
586 bFullWord = true;
587 }
588 }
589 } else {
590 if (!IsSpace(pWord->Word) && !IsOpenStylePunctuation(pWord->Word)) {
591 bOpened = false;
592 }
593 }
594 if (bFullWord) {
595 bFullWord = false;
596 if (nCharIndex > 0) {
597 nLineFullWordIndex++;
598 }
599 nWordStartPos = i;
600 fBackupLineWidth = fLineWidth;
601 fBackupLineAscent = fLineAscent;
602 fBackupLineDescent = fLineDescent;
603 }
604 nCharIndex++;
605 }
606 if (m_pVT->IsAutoReturn() && fTypesetWidth > 0 &&
607 fLineWidth + fWordWidth > fTypesetWidth) {
608 if (nLineFullWordIndex > 0) {
609 i = nWordStartPos;
610 fLineWidth = fBackupLineWidth;
611 fLineAscent = fBackupLineAscent;
612 fLineDescent = fBackupLineDescent;
613 }
614 if (nCharIndex == 1) {
615 fLineWidth = fWordWidth;
616 i++;
617 }
618 nLineTail = i - 1;
619 if (bTypeset) {
620 line.nBeginWordIndex = nLineHead;
621 line.nEndWordIndex = nLineTail;
622 line.nTotalWord = nLineTail - nLineHead + 1;
623 line.fLineWidth = fLineWidth;
624 line.fLineAscent = fLineAscent;
625 line.fLineDescent = fLineDescent;
626 AddLine(line);
627 }
628 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
629 fMaxY -= fLineDescent;
630 fMaxX = std::max(fLineWidth, fMaxX);
631 nLineHead = i;
632 fLineWidth = 0.0f;
633 fLineAscent = 0.0f;
634 fLineDescent = 0.0f;
635 nCharIndex = 0;
636 nLineFullWordIndex = 0;
637 bFullWord = false;
638 } else {
639 fLineWidth += fWordWidth;
640 i++;
641 }
642 }
643 if (nLineHead <= nTotalWords - 1) {
644 nLineTail = nTotalWords - 1;
645 if (bTypeset) {
646 line.nBeginWordIndex = nLineHead;
647 line.nEndWordIndex = nLineTail;
648 line.nTotalWord = nLineTail - nLineHead + 1;
649 line.fLineWidth = fLineWidth;
650 line.fLineAscent = fLineAscent;
651 line.fLineDescent = fLineDescent;
652 AddLine(line);
653 }
654 fMaxY += (fLineAscent + m_pVT->GetLineLeading());
655 fMaxY -= fLineDescent;
656 fMaxX = std::max(fLineWidth, fMaxX);
657 }
658 return CPVT_FloatRect(0, 0, fMaxX, fMaxY);
659 }
660
OutputLines(const CPVT_FloatRect & rect) const661 CPVT_FloatRect CPVT_Section::OutputLines(const CPVT_FloatRect& rect) const {
662 float fMinX;
663 float fLineIndent = m_pVT->GetLineIndent();
664 float fTypesetWidth = std::max(m_pVT->GetPlateWidth() - fLineIndent, 0.0f);
665 switch (m_pVT->GetAlignment()) {
666 default:
667 case 0:
668 fMinX = 0.0f;
669 break;
670 case 1:
671 fMinX = (fTypesetWidth - rect.Width()) * 0.5f;
672 break;
673 case 2:
674 fMinX = fTypesetWidth - rect.Width();
675 break;
676 }
677 float fMaxX = fMinX + rect.Width();
678 float fMinY = 0.0f;
679 float fMaxY = rect.Height();
680 int32_t nTotalLines = fxcrt::CollectionSize<int32_t>(m_LineArray);
681 if (nTotalLines > 0) {
682 float fPosX = 0.0f;
683 float fPosY = 0.0f;
684 for (int32_t l = 0; l < nTotalLines; l++) {
685 CPVT_Section::Line* pLine = m_LineArray[l].get();
686 switch (m_pVT->GetAlignment()) {
687 default:
688 case 0:
689 fPosX = 0;
690 break;
691 case 1:
692 fPosX = (fTypesetWidth - pLine->m_LineInfo.fLineWidth) * 0.5f;
693 break;
694 case 2:
695 fPosX = fTypesetWidth - pLine->m_LineInfo.fLineWidth;
696 break;
697 }
698 fPosX += fLineIndent;
699 fPosY += m_pVT->GetLineLeading();
700 fPosY += pLine->m_LineInfo.fLineAscent;
701 pLine->m_LineInfo.fLineX = fPosX - fMinX;
702 pLine->m_LineInfo.fLineY = fPosY - fMinY;
703 for (int32_t w = pLine->m_LineInfo.nBeginWordIndex;
704 w <= pLine->m_LineInfo.nEndWordIndex; w++) {
705 if (fxcrt::IndexInBounds(m_WordArray, w)) {
706 CPVT_WordInfo* pWord = m_WordArray[w].get();
707 pWord->fWordX = fPosX - fMinX;
708 pWord->fWordY = fPosY - fMinY;
709
710 fPosX += m_pVT->GetWordWidth(*pWord);
711 }
712 }
713 fPosY -= pLine->m_LineInfo.fLineDescent;
714 }
715 }
716 return CPVT_FloatRect(fMinX, fMinY, fMaxX, fMaxY);
717 }
718
ClearLeftWords(int32_t nWordIndex)719 void CPVT_Section::ClearLeftWords(int32_t nWordIndex) {
720 for (int32_t i = nWordIndex; i >= 0; i--) {
721 if (fxcrt::IndexInBounds(m_WordArray, i))
722 m_WordArray.erase(m_WordArray.begin() + i);
723 }
724 }
725
ClearRightWords(int32_t nWordIndex)726 void CPVT_Section::ClearRightWords(int32_t nWordIndex) {
727 int32_t sz = fxcrt::CollectionSize<int32_t>(m_WordArray);
728 for (int32_t i = sz - 1; i > nWordIndex; i--) {
729 if (fxcrt::IndexInBounds(m_WordArray, i))
730 m_WordArray.erase(m_WordArray.begin() + i);
731 }
732 }
733
ClearMidWords(int32_t nBeginIndex,int32_t nEndIndex)734 void CPVT_Section::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) {
735 for (int32_t i = nEndIndex; i > nBeginIndex; i--) {
736 if (fxcrt::IndexInBounds(m_WordArray, i))
737 m_WordArray.erase(m_WordArray.begin() + i);
738 }
739 }
740
ClearWords(const CPVT_WordRange & PlaceRange)741 void CPVT_Section::ClearWords(const CPVT_WordRange& PlaceRange) {
742 CPVT_WordPlace SecBeginPos = GetBeginWordPlace();
743 CPVT_WordPlace SecEndPos = GetEndWordPlace();
744 if (PlaceRange.BeginPos >= SecBeginPos) {
745 if (PlaceRange.EndPos <= SecEndPos) {
746 ClearMidWords(PlaceRange.BeginPos.nWordIndex,
747 PlaceRange.EndPos.nWordIndex);
748 } else {
749 ClearRightWords(PlaceRange.BeginPos.nWordIndex);
750 }
751 } else if (PlaceRange.EndPos <= SecEndPos) {
752 ClearLeftWords(PlaceRange.EndPos.nWordIndex);
753 } else {
754 m_WordArray.clear();
755 }
756 }
757
ClearWord(const CPVT_WordPlace & place)758 void CPVT_Section::ClearWord(const CPVT_WordPlace& place) {
759 if (fxcrt::IndexInBounds(m_WordArray, place.nWordIndex))
760 m_WordArray.erase(m_WordArray.begin() + place.nWordIndex);
761 }
762