1 // Copyright 2014 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 #ifndef XFA_FGAS_LAYOUT_FGAS_TEXTBREAK_H_
8 #define XFA_FGAS_LAYOUT_FGAS_TEXTBREAK_H_
9 
10 #include "core/fxcrt/include/fx_ucd.h"
11 #include "core/fxge/include/fx_ge.h"
12 #include "xfa/fgas/crt/fgas_utils.h"
13 #include "xfa/fgas/layout/fgas_unicode.h"
14 
15 class CFX_Char;
16 class CFGAS_GEFont;
17 class CFX_TxtChar;
18 class CFX_TxtPiece;
19 class IFX_TxtAccess;
20 
21 #define FX_TXTBREAKPOLICY_None 0x00
22 #define FX_TXTBREAKPOLICY_Pagination 0x01
23 #define FX_TXTBREAKPOLICY_SpaceBreak 0x02
24 #define FX_TXTBREAKPOLICY_NumberBreak 0x04
25 #define FX_TXTBREAK_None 0x00
26 #define FX_TXTBREAK_PieceBreak 0x01
27 #define FX_TXTBREAK_LineBreak 0x02
28 #define FX_TXTBREAK_ParagraphBreak 0x03
29 #define FX_TXTBREAK_PageBreak 0x04
30 #define FX_TXTBREAK_ControlChar 0x10
31 #define FX_TXTBREAK_BreakChar 0x20
32 #define FX_TXTBREAK_UnknownChar 0x40
33 #define FX_TXTBREAK_RemoveChar 0x80
34 #define FX_TXTLAYOUTSTYLE_MutipleFormat 0x0001
35 #define FX_TXTLAYOUTSTYLE_VerticalLayout 0x0002
36 #define FX_TXTLAYOUTSTYLE_VerticalChars 0x0004
37 #define FX_TXTLAYOUTSTYLE_ReverseLine 0x0008
38 #define FX_TXTLAYOUTSTYLE_ArabicContext 0x0010
39 #define FX_TXTLAYOUTSTYLE_ArabicShapes 0x0020
40 #define FX_TXTLAYOUTSTYLE_RTLReadingOrder 0x0040
41 #define FX_TXTLAYOUTSTYLE_ExpandTab 0x0100
42 #define FX_TXTLAYOUTSTYLE_SingleLine 0x0200
43 #define FX_TXTLAYOUTSTYLE_CombText 0x0400
44 #define FX_TXTCHARSTYLE_Alignment 0x000F
45 #define FX_TXTCHARSTYLE_ArabicNumber 0x0010
46 #define FX_TXTCHARSTYLE_ArabicShadda 0x0020
47 #define FX_TXTCHARSTYLE_OddBidiLevel 0x0040
48 #define FX_TXTCHARSTYLE_RTLReadingOrder 0x0080
49 #define FX_TXTCHARSTYLE_ArabicContext 0x0300
50 #define FX_TXTCHARSTYLE_ArabicIndic 0x0400
51 #define FX_TXTCHARSTYLE_ArabicComma 0x0800
52 #define FX_TXTLINEALIGNMENT_Left 0
53 #define FX_TXTLINEALIGNMENT_Center 1
54 #define FX_TXTLINEALIGNMENT_Right 2
55 #define FX_TXTLINEALIGNMENT_Justified (1 << 2)
56 #define FX_TXTLINEALIGNMENT_Distributed (2 << 2)
57 #define FX_TXTLINEALIGNMENT_JustifiedLeft \
58   (FX_TXTLINEALIGNMENT_Left | FX_TXTLINEALIGNMENT_Justified)
59 #define FX_TXTLINEALIGNMENT_JustifiedCenter \
60   (FX_TXTLINEALIGNMENT_Center | FX_TXTLINEALIGNMENT_Justified)
61 #define FX_TXTLINEALIGNMENT_JustifiedRight \
62   (FX_TXTLINEALIGNMENT_Right | FX_TXTLINEALIGNMENT_Justified)
63 #define FX_TXTLINEALIGNMENT_DistributedLeft \
64   (FX_TXTLINEALIGNMENT_Left | FX_TXTLINEALIGNMENT_Distributed)
65 #define FX_TXTLINEALIGNMENT_DistributedCenter \
66   (FX_TXTLINEALIGNMENT_Center | FX_TXTLINEALIGNMENT_Distributed)
67 #define FX_TXTLINEALIGNMENT_DistributedRight \
68   (FX_TXTLINEALIGNMENT_Right | FX_TXTLINEALIGNMENT_Distributed)
69 #define FX_TXTLINEALIGNMENT_LowerMask 0x03
70 #define FX_TXTLINEALIGNMENT_HigherMask 0x0C
71 #define FX_TXTBREAK_MinimumTabWidth 160000
72 
73 struct FDE_TEXTEDITPIECE;
74 
75 class IFX_TxtAccess {
76  public:
~IFX_TxtAccess()77   virtual ~IFX_TxtAccess() {}
78   virtual FX_WCHAR GetChar(const FDE_TEXTEDITPIECE* pIdentity,
79                            int32_t index) const = 0;
80   virtual int32_t GetWidth(const FDE_TEXTEDITPIECE* pIdentity,
81                            int32_t index) const = 0;
82 };
83 
84 struct FX_TXTRUN {
85   FX_TXTRUN();
86   FX_TXTRUN(const FX_TXTRUN& other);
87   ~FX_TXTRUN();
88 
89   IFX_TxtAccess* pAccess;
90   const FDE_TEXTEDITPIECE* pIdentity;
91   CFX_WideString wsStr;
92   int32_t* pWidths;
93   int32_t iLength;
94   CFGAS_GEFont* pFont;
95   FX_FLOAT fFontSize;
96   uint32_t dwStyles;
97   int32_t iHorizontalScale;
98   int32_t iVerticalScale;
99   int32_t iCharRotation;
100   uint32_t dwCharStyles;
101   const CFX_RectF* pRect;
102   FX_WCHAR wLineBreakChar;
103   FX_BOOL bSkipSpace;
104 };
105 
106 class CFX_TxtPiece : public CFX_Target {
107  public:
108   CFX_TxtPiece();
109 
GetEndPos()110   int32_t GetEndPos() const {
111     return m_iWidth < 0 ? m_iStartPos : m_iStartPos + m_iWidth;
112   }
GetLength()113   int32_t GetLength() const { return m_iChars; }
GetEndChar()114   int32_t GetEndChar() const { return m_iStartChar + m_iChars; }
GetCharPtr(int32_t index)115   CFX_TxtChar* GetCharPtr(int32_t index) const {
116     ASSERT(index > -1 && index < m_iChars && m_pChars);
117     return m_pChars->GetDataPtr(m_iStartChar + index);
118   }
GetString(FX_WCHAR * pText)119   void GetString(FX_WCHAR* pText) const {
120     ASSERT(pText);
121     int32_t iEndChar = m_iStartChar + m_iChars;
122     CFX_Char* pChar;
123     for (int32_t i = m_iStartChar; i < iEndChar; i++) {
124       pChar = m_pChars->GetDataPtr(i);
125       *pText++ = (FX_WCHAR)pChar->m_wCharCode;
126     }
127   }
GetString(CFX_WideString & wsText)128   void GetString(CFX_WideString& wsText) const {
129     FX_WCHAR* pText = wsText.GetBuffer(m_iChars);
130     GetString(pText);
131     wsText.ReleaseBuffer(m_iChars);
132   }
GetWidths(int32_t * pWidths)133   void GetWidths(int32_t* pWidths) const {
134     ASSERT(pWidths);
135     int32_t iEndChar = m_iStartChar + m_iChars;
136     CFX_Char* pChar;
137     for (int32_t i = m_iStartChar; i < iEndChar; i++) {
138       pChar = m_pChars->GetDataPtr(i);
139       *pWidths++ = pChar->m_iCharWidth;
140     }
141   }
142 
143   uint32_t m_dwStatus;
144   int32_t m_iStartPos;
145   int32_t m_iWidth;
146   int32_t m_iStartChar;
147   int32_t m_iChars;
148   int32_t m_iBidiLevel;
149   int32_t m_iBidiPos;
150   int32_t m_iHorizontalScale;
151   int32_t m_iVerticalScale;
152   uint32_t m_dwCharStyles;
153   CFX_TxtCharArray* m_pChars;
154   void* m_pUserData;
155 };
156 
157 typedef CFX_BaseArrayTemplate<CFX_TxtPiece> CFX_TxtPieceArray;
158 
159 class CFX_TxtLine {
160  public:
161   CFX_TxtLine(int32_t iBlockSize);
162   ~CFX_TxtLine();
163 
CountChars()164   int32_t CountChars() const { return m_pLineChars->GetSize(); }
GetCharPtr(int32_t index)165   CFX_TxtChar* GetCharPtr(int32_t index) const {
166     ASSERT(index > -1 && index < m_pLineChars->GetSize());
167     return m_pLineChars->GetDataPtr(index);
168   }
CountPieces()169   int32_t CountPieces() const { return m_pLinePieces->GetSize(); }
GetPiecePtr(int32_t index)170   CFX_TxtPiece* GetPiecePtr(int32_t index) const {
171     ASSERT(index > -1 && index < m_pLinePieces->GetSize());
172     return m_pLinePieces->GetPtrAt(index);
173   }
GetString(CFX_WideString & wsStr)174   void GetString(CFX_WideString& wsStr) const {
175     int32_t iCount = m_pLineChars->GetSize();
176     FX_WCHAR* pBuf = wsStr.GetBuffer(iCount);
177     CFX_Char* pChar;
178     for (int32_t i = 0; i < iCount; i++) {
179       pChar = m_pLineChars->GetDataPtr(i);
180       *pBuf++ = (FX_WCHAR)pChar->m_wCharCode;
181     }
182     wsStr.ReleaseBuffer(iCount);
183   }
184   void RemoveAll(FX_BOOL bLeaveMemory = FALSE) {
185     m_pLineChars->RemoveAll();
186     m_pLinePieces->RemoveAll(bLeaveMemory);
187     m_iWidth = 0;
188     m_iArabicChars = 0;
189   }
190 
191   CFX_TxtCharArray* m_pLineChars;
192   CFX_TxtPieceArray* m_pLinePieces;
193   int32_t m_iStart;
194   int32_t m_iWidth;
195   int32_t m_iArabicChars;
196 };
197 
198 class CFX_TxtBreak {
199  public:
200   CFX_TxtBreak(uint32_t dwPolicies);
201   ~CFX_TxtBreak();
202 
203   void SetLineWidth(FX_FLOAT fLineWidth);
204   void SetLinePos(FX_FLOAT fLinePos);
GetLayoutStyles()205   uint32_t GetLayoutStyles() const { return m_dwLayoutStyles; }
206   void SetLayoutStyles(uint32_t dwLayoutStyles);
207   void SetFont(CFGAS_GEFont* pFont);
208   void SetFontSize(FX_FLOAT fFontSize);
209   void SetTabWidth(FX_FLOAT fTabWidth, FX_BOOL bEquidistant);
210   void SetDefaultChar(FX_WCHAR wch);
211   void SetParagraphBreakChar(FX_WCHAR wch);
212   void SetLineBreakTolerance(FX_FLOAT fTolerance);
213   void SetHorizontalScale(int32_t iScale);
214   void SetVerticalScale(int32_t iScale);
215   void SetCharRotation(int32_t iCharRotation);
216   void SetCharSpace(FX_FLOAT fCharSpace);
217   void SetAlignment(int32_t iAlignment);
218   uint32_t GetContextCharStyles() const;
219   void SetContextCharStyles(uint32_t dwCharStyles);
220   void SetCombWidth(FX_FLOAT fCombWidth);
221   void SetUserData(void* pUserData);
222   uint32_t AppendChar(FX_WCHAR wch);
223   uint32_t EndBreak(uint32_t dwStatus = FX_TXTBREAK_PieceBreak);
224   int32_t CountBreakChars() const;
225   int32_t CountBreakPieces() const;
226   const CFX_TxtPiece* GetBreakPiece(int32_t index) const;
227   void ClearBreakPieces();
228   void Reset();
229   int32_t GetDisplayPos(const FX_TXTRUN* pTxtRun,
230                         FXTEXT_CHARPOS* pCharPos,
231                         FX_BOOL bCharCode = FALSE,
232                         CFX_WideString* pWSForms = nullptr,
233                         FX_AdjustCharDisplayPos pAdjustPos = nullptr) const;
234   int32_t GetCharRects(const FX_TXTRUN* pTxtRun,
235                        CFX_RectFArray& rtArray,
236                        FX_BOOL bCharBBox = FALSE) const;
237   void AppendChar_PageLoad(CFX_TxtChar* pCurChar, uint32_t dwProps);
238   uint32_t AppendChar_Combination(CFX_TxtChar* pCurChar, int32_t iRotation);
239   uint32_t AppendChar_Tab(CFX_TxtChar* pCurChar, int32_t iRotation);
240   uint32_t AppendChar_Control(CFX_TxtChar* pCurChar, int32_t iRotation);
241   uint32_t AppendChar_Arabic(CFX_TxtChar* pCurChar, int32_t iRotation);
242   uint32_t AppendChar_Others(CFX_TxtChar* pCurChar, int32_t iRotation);
243 
244  private:
245   void SetBreakStatus();
246   int32_t GetLineRotation(uint32_t dwStyles) const;
247   CFX_TxtChar* GetLastChar(int32_t index, FX_BOOL bOmitChar = TRUE) const;
248   CFX_TxtLine* GetTxtLine(FX_BOOL bReady) const;
249   CFX_TxtPieceArray* GetTxtPieces(FX_BOOL bReady) const;
250   FX_CHARTYPE GetUnifiedCharType(FX_CHARTYPE dwType) const;
251   void ResetArabicContext();
252   void ResetContextCharStyles();
253   void EndBreak_UpdateArabicShapes();
254   FX_BOOL EndBreak_SplitLine(CFX_TxtLine* pNextLine,
255                              FX_BOOL bAllChars,
256                              uint32_t dwStatus);
257   void EndBreak_BidiLine(CFX_TPOArray& tpos, uint32_t dwStatus);
258   void EndBreak_Alignment(CFX_TPOArray& tpos,
259                           FX_BOOL bAllChars,
260                           uint32_t dwStatus);
261   int32_t GetBreakPos(CFX_TxtCharArray& ca,
262                       int32_t& iEndPos,
263                       FX_BOOL bAllChars = FALSE,
264                       FX_BOOL bOnlyBrk = FALSE);
265   void SplitTextLine(CFX_TxtLine* pCurLine,
266                      CFX_TxtLine* pNextLine,
267                      FX_BOOL bAllChars = FALSE);
268 
269   uint32_t m_dwPolicies;
270   FX_BOOL m_bPagination;
271   int32_t m_iLineWidth;
272   uint32_t m_dwLayoutStyles;
273   FX_BOOL m_bVertical;
274   FX_BOOL m_bArabicContext;
275   FX_BOOL m_bArabicShapes;
276   FX_BOOL m_bRTL;
277   FX_BOOL m_bSingleLine;
278   FX_BOOL m_bCombText;
279   int32_t m_iArabicContext;
280   int32_t m_iCurArabicContext;
281   CFGAS_GEFont* m_pFont;
282   int32_t m_iFontSize;
283   FX_BOOL m_bEquidistant;
284   int32_t m_iTabWidth;
285   FX_WCHAR m_wDefChar;
286   FX_WCHAR m_wParagBreakChar;
287   int32_t m_iDefChar;
288   int32_t m_iLineRotation;
289   int32_t m_iCharRotation;
290   int32_t m_iRotation;
291   int32_t m_iAlignment;
292   uint32_t m_dwContextCharStyles;
293   int32_t m_iCombWidth;
294   void* m_pUserData;
295   FX_CHARTYPE m_eCharType;
296   FX_BOOL m_bCurRTL;
297   int32_t m_iCurAlignment;
298   FX_BOOL m_bArabicNumber;
299   FX_BOOL m_bArabicComma;
300   CFX_TxtLine* m_pTxtLine1;
301   CFX_TxtLine* m_pTxtLine2;
302   CFX_TxtLine* m_pCurLine;
303   int32_t m_iReady;
304   int32_t m_iTolerance;
305   int32_t m_iHorScale;
306   int32_t m_iVerScale;
307   int32_t m_iCharSpace;
308 };
309 
310 #endif  // XFA_FGAS_LAYOUT_FGAS_TEXTBREAK_H_
311