• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
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/ctypeset.h"
8 
9 #include <algorithm>
10 
11 #include "core/fpdfdoc/cline.h"
12 #include "core/fpdfdoc/cpdf_variabletext.h"
13 #include "core/fpdfdoc/cpvt_wordinfo.h"
14 #include "core/fpdfdoc/csection.h"
15 #include "third_party/base/stl_util.h"
16 
17 namespace {
18 
19 const uint8_t special_chars[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 !!(special_chars[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 !!(special_chars[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 && (special_chars[word] & 0x20);
120 }
121 
IsOpenStylePunctuation(uint32_t word)122 bool IsOpenStylePunctuation(uint32_t word) {
123   if (word <= 0x007F)
124     return !!(special_chars[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 
CTypeset(CSection * pSection)176 CTypeset::CTypeset(CSection* pSection)
177     : m_pVT(pSection->m_pVT), m_pSection(pSection) {}
178 
179 CTypeset::~CTypeset() = default;
180 
CharArray()181 CPVT_FloatRect CTypeset::CharArray() {
182   m_rcRet = CPVT_FloatRect();
183   if (m_pSection->m_LineArray.empty())
184     return m_rcRet;
185 
186   float fNodeWidth = m_pVT->GetPlateWidth() /
187                      (m_pVT->GetCharArray() <= 0 ? 1 : m_pVT->GetCharArray());
188   float fLineAscent =
189       m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
190   float fLineDescent =
191       m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), m_pVT->GetFontSize());
192   float x = 0.0f;
193   float y = m_pVT->GetLineLeading() + fLineAscent;
194   int32_t nStart = 0;
195   CLine* pLine = m_pSection->m_LineArray.front().get();
196   switch (m_pVT->GetAlignment()) {
197     case 0:
198       pLine->m_LineInfo.fLineX = fNodeWidth * VARIABLETEXT_HALF;
199       break;
200     case 1:
201       nStart = (m_pVT->GetCharArray() -
202                 pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray)) /
203                2;
204       pLine->m_LineInfo.fLineX =
205           fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF;
206       break;
207     case 2:
208       nStart = m_pVT->GetCharArray() -
209                pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray);
210       pLine->m_LineInfo.fLineX =
211           fNodeWidth * nStart - fNodeWidth * VARIABLETEXT_HALF;
212       break;
213   }
214   for (int32_t w = 0,
215                sz = pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray);
216        w < sz; w++) {
217     if (w >= m_pVT->GetCharArray())
218       break;
219 
220     float fNextWidth = 0;
221     if (pdfium::IndexInBounds(m_pSection->m_WordArray, w + 1)) {
222       CPVT_WordInfo* pNextWord = m_pSection->m_WordArray[w + 1].get();
223       pNextWord->fWordTail = 0;
224       fNextWidth = m_pVT->GetWordWidth(*pNextWord);
225     }
226     CPVT_WordInfo* pWord = m_pSection->m_WordArray[w].get();
227     pWord->fWordTail = 0;
228     float fWordWidth = m_pVT->GetWordWidth(*pWord);
229     float fWordAscent = m_pVT->GetWordAscent(*pWord);
230     float fWordDescent = m_pVT->GetWordDescent(*pWord);
231     x = (float)(fNodeWidth * (w + nStart + 0.5) -
232                 fWordWidth * VARIABLETEXT_HALF);
233     pWord->fWordX = x;
234     pWord->fWordY = y;
235     if (w == 0) {
236       pLine->m_LineInfo.fLineX = x;
237     }
238     if (w != pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray) - 1) {
239       pWord->fWordTail =
240           (fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF > 0
241                ? fNodeWidth - (fWordWidth + fNextWidth) * VARIABLETEXT_HALF
242                : 0);
243     } else {
244       pWord->fWordTail = 0;
245     }
246     x += fWordWidth;
247     fLineAscent = std::max(fLineAscent, fWordAscent);
248     fLineDescent = std::min(fLineDescent, fWordDescent);
249   }
250   pLine->m_LineInfo.nBeginWordIndex = 0;
251   pLine->m_LineInfo.nEndWordIndex =
252       pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray) - 1;
253   pLine->m_LineInfo.fLineY = y;
254   pLine->m_LineInfo.fLineWidth = x - pLine->m_LineInfo.fLineX;
255   pLine->m_LineInfo.fLineAscent = fLineAscent;
256   pLine->m_LineInfo.fLineDescent = fLineDescent;
257   m_rcRet = CPVT_FloatRect(0, 0, x, y - fLineDescent);
258   return m_rcRet;
259 }
260 
GetEditSize(float fFontSize)261 CFX_SizeF CTypeset::GetEditSize(float fFontSize) {
262   ASSERT(m_pSection);
263   ASSERT(m_pVT);
264   SplitLines(false, fFontSize);
265   return CFX_SizeF(m_rcRet.Width(), m_rcRet.Height());
266 }
267 
Typeset()268 CPVT_FloatRect CTypeset::Typeset() {
269   ASSERT(m_pVT);
270   m_pSection->m_LineArray.clear();
271   SplitLines(true, 0.0f);
272   OutputLines();
273   return m_rcRet;
274 }
275 
SplitLines(bool bTypeset,float fFontSize)276 void CTypeset::SplitLines(bool bTypeset, float fFontSize) {
277   ASSERT(m_pVT);
278   ASSERT(m_pSection);
279 
280   CPVT_LineInfo line;
281   if (m_pSection->m_WordArray.empty()) {
282     float fLineAscent;
283     float fLineDescent;
284     if (bTypeset) {
285       fLineAscent = m_pVT->GetLineAscent();
286       fLineDescent = m_pVT->GetLineDescent();
287     } else {
288       fLineAscent =
289           m_pVT->GetFontAscent(m_pVT->GetDefaultFontIndex(), fFontSize);
290       fLineDescent =
291           m_pVT->GetFontDescent(m_pVT->GetDefaultFontIndex(), fFontSize);
292     }
293     if (bTypeset) {
294       line.nBeginWordIndex = -1;
295       line.nEndWordIndex = -1;
296       line.nTotalWord = 0;
297       line.fLineWidth = 0;
298       line.fLineAscent = fLineAscent;
299       line.fLineDescent = fLineDescent;
300       m_pSection->AddLine(line);
301     }
302     float fMaxY = m_pVT->GetLineLeading() + fLineAscent - fLineDescent;
303     m_rcRet = CPVT_FloatRect(0, 0, 0, fMaxY);
304     return;
305   }
306 
307   int32_t nLineHead = 0;
308   int32_t nLineTail = 0;
309   float fMaxX = 0.0f;
310   float fMaxY = 0.0f;
311   float fLineWidth = 0.0f;
312   float fBackupLineWidth = 0.0f;
313   float fLineAscent = 0.0f;
314   float fBackupLineAscent = 0.0f;
315   float fLineDescent = 0.0f;
316   float fBackupLineDescent = 0.0f;
317   int32_t nWordStartPos = 0;
318   bool bFullWord = false;
319   int32_t nLineFullWordIndex = 0;
320   int32_t nCharIndex = 0;
321   float fWordWidth = 0;
322   float fTypesetWidth =
323       std::max(m_pVT->GetPlateWidth() - m_pVT->GetLineIndent(), 0.0f);
324   int32_t nTotalWords =
325       pdfium::CollectionSize<int32_t>(m_pSection->m_WordArray);
326   bool bOpened = false;
327   int32_t i = 0;
328   while (i < nTotalWords) {
329     CPVT_WordInfo* pWord = m_pSection->m_WordArray[i].get();
330     CPVT_WordInfo* pOldWord = pWord;
331     if (i > 0) {
332       pOldWord = m_pSection->m_WordArray[i - 1].get();
333     }
334     if (pWord) {
335       if (bTypeset) {
336         fLineAscent = std::max(fLineAscent, m_pVT->GetWordAscent(*pWord));
337         fLineDescent = std::min(fLineDescent, m_pVT->GetWordDescent(*pWord));
338         fWordWidth = m_pVT->GetWordWidth(*pWord);
339       } else {
340         fLineAscent =
341             std::max(fLineAscent, m_pVT->GetWordAscent(*pWord, fFontSize));
342         fLineDescent =
343             std::min(fLineDescent, m_pVT->GetWordDescent(*pWord, fFontSize));
344         fWordWidth = m_pVT->GetWordWidth(
345             pWord->nFontIndex, pWord->Word, m_pVT->GetSubWord(),
346             m_pVT->GetCharSpace(), fFontSize, pWord->fWordTail);
347       }
348       if (!bOpened) {
349         if (IsOpenStylePunctuation(pWord->Word)) {
350           bOpened = true;
351           bFullWord = true;
352         } else if (pOldWord) {
353           if (NeedDivision(pOldWord->Word, pWord->Word)) {
354             bFullWord = true;
355           }
356         }
357       } else {
358         if (!IsSpace(pWord->Word) && !IsOpenStylePunctuation(pWord->Word)) {
359           bOpened = false;
360         }
361       }
362       if (bFullWord) {
363         bFullWord = false;
364         if (nCharIndex > 0) {
365           nLineFullWordIndex++;
366         }
367         nWordStartPos = i;
368         fBackupLineWidth = fLineWidth;
369         fBackupLineAscent = fLineAscent;
370         fBackupLineDescent = fLineDescent;
371       }
372       nCharIndex++;
373     }
374     if (m_pVT->IsAutoReturn() && fTypesetWidth > 0 &&
375         fLineWidth + fWordWidth > fTypesetWidth) {
376       if (nLineFullWordIndex > 0) {
377         i = nWordStartPos;
378         fLineWidth = fBackupLineWidth;
379         fLineAscent = fBackupLineAscent;
380         fLineDescent = fBackupLineDescent;
381       }
382       if (nCharIndex == 1) {
383         fLineWidth = fWordWidth;
384         i++;
385       }
386       nLineTail = i - 1;
387       if (bTypeset) {
388         line.nBeginWordIndex = nLineHead;
389         line.nEndWordIndex = nLineTail;
390         line.nTotalWord = nLineTail - nLineHead + 1;
391         line.fLineWidth = fLineWidth;
392         line.fLineAscent = fLineAscent;
393         line.fLineDescent = fLineDescent;
394         m_pSection->AddLine(line);
395       }
396       fMaxY += (fLineAscent + m_pVT->GetLineLeading());
397       fMaxY -= fLineDescent;
398       fMaxX = std::max(fLineWidth, fMaxX);
399       nLineHead = i;
400       fLineWidth = 0.0f;
401       fLineAscent = 0.0f;
402       fLineDescent = 0.0f;
403       nCharIndex = 0;
404       nLineFullWordIndex = 0;
405       bFullWord = false;
406     } else {
407       fLineWidth += fWordWidth;
408       i++;
409     }
410   }
411   if (nLineHead <= nTotalWords - 1) {
412     nLineTail = nTotalWords - 1;
413     if (bTypeset) {
414       line.nBeginWordIndex = nLineHead;
415       line.nEndWordIndex = nLineTail;
416       line.nTotalWord = nLineTail - nLineHead + 1;
417       line.fLineWidth = fLineWidth;
418       line.fLineAscent = fLineAscent;
419       line.fLineDescent = fLineDescent;
420       m_pSection->AddLine(line);
421     }
422     fMaxY += (fLineAscent + m_pVT->GetLineLeading());
423     fMaxY -= fLineDescent;
424     fMaxX = std::max(fLineWidth, fMaxX);
425   }
426   m_rcRet = CPVT_FloatRect(0, 0, fMaxX, fMaxY);
427 }
428 
OutputLines()429 void CTypeset::OutputLines() {
430   ASSERT(m_pVT);
431   ASSERT(m_pSection);
432   float fMinX;
433   float fLineIndent = m_pVT->GetLineIndent();
434   float fTypesetWidth = std::max(m_pVT->GetPlateWidth() - fLineIndent, 0.0f);
435   switch (m_pVT->GetAlignment()) {
436     default:
437     case 0:
438       fMinX = 0.0f;
439       break;
440     case 1:
441       fMinX = (fTypesetWidth - m_rcRet.Width()) * VARIABLETEXT_HALF;
442       break;
443     case 2:
444       fMinX = fTypesetWidth - m_rcRet.Width();
445       break;
446   }
447   float fMaxX = fMinX + m_rcRet.Width();
448   float fMinY = 0.0f;
449   float fMaxY = m_rcRet.Height();
450   int32_t nTotalLines =
451       pdfium::CollectionSize<int32_t>(m_pSection->m_LineArray);
452   if (nTotalLines > 0) {
453     float fPosX = 0.0f;
454     float fPosY = 0.0f;
455     for (int32_t l = 0; l < nTotalLines; l++) {
456       CLine* pLine = m_pSection->m_LineArray[l].get();
457       switch (m_pVT->GetAlignment()) {
458         default:
459         case 0:
460           fPosX = 0;
461           break;
462         case 1:
463           fPosX = (fTypesetWidth - pLine->m_LineInfo.fLineWidth) *
464                   VARIABLETEXT_HALF;
465           break;
466         case 2:
467           fPosX = fTypesetWidth - pLine->m_LineInfo.fLineWidth;
468           break;
469       }
470       fPosX += fLineIndent;
471       fPosY += m_pVT->GetLineLeading();
472       fPosY += pLine->m_LineInfo.fLineAscent;
473       pLine->m_LineInfo.fLineX = fPosX - fMinX;
474       pLine->m_LineInfo.fLineY = fPosY - fMinY;
475       for (int32_t w = pLine->m_LineInfo.nBeginWordIndex;
476            w <= pLine->m_LineInfo.nEndWordIndex; w++) {
477         if (pdfium::IndexInBounds(m_pSection->m_WordArray, w)) {
478           CPVT_WordInfo* pWord = m_pSection->m_WordArray[w].get();
479           pWord->fWordX = fPosX - fMinX;
480           pWord->fWordY = fPosY - fMinY;
481 
482           fPosX += m_pVT->GetWordWidth(*pWord);
483         }
484       }
485       fPosY -= pLine->m_LineInfo.fLineDescent;
486     }
487   }
488   m_rcRet = CPVT_FloatRect(fMinX, fMinY, fMaxX, fMaxY);
489 }
490