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 _FX_TEXTBREAK_IMP
8 #define _FX_TEXTBREAK_IMP
9 
10 #include "core/include/fxcrt/fx_memory.h"
11 
12 class IFX_ArabicChar;
13 class CFX_Txtbreak;
14 
15 class CFX_TxtLine {
16  public:
CFX_TxtLine(int32_t iBlockSize)17   CFX_TxtLine(int32_t iBlockSize)
18       : m_iStart(0), m_iWidth(0), m_iArabicChars(0) {
19     m_pLineChars = new CFX_TxtCharArray;
20     m_pLinePieces = new CFX_TxtPieceArray(16);
21   }
~CFX_TxtLine()22   ~CFX_TxtLine() {
23     RemoveAll();
24     delete m_pLineChars;
25     delete m_pLinePieces;
26   }
CountChars()27   int32_t CountChars() const { return m_pLineChars->GetSize(); }
GetCharPtr(int32_t index)28   CFX_TxtChar* GetCharPtr(int32_t index) const {
29     FXSYS_assert(index > -1 && index < m_pLineChars->GetSize());
30     return m_pLineChars->GetDataPtr(index);
31   }
CountPieces()32   int32_t CountPieces() const { return m_pLinePieces->GetSize(); }
GetPiecePtr(int32_t index)33   CFX_TxtPiece* GetPiecePtr(int32_t index) const {
34     FXSYS_assert(index > -1 && index < m_pLinePieces->GetSize());
35     return m_pLinePieces->GetPtrAt(index);
36   }
GetString(CFX_WideString & wsStr)37   void GetString(CFX_WideString& wsStr) const {
38     int32_t iCount = m_pLineChars->GetSize();
39     FX_WCHAR* pBuf = wsStr.GetBuffer(iCount);
40     CFX_Char* pChar;
41     for (int32_t i = 0; i < iCount; i++) {
42       pChar = m_pLineChars->GetDataPtr(i);
43       *pBuf++ = (FX_WCHAR)pChar->m_wCharCode;
44     }
45     wsStr.ReleaseBuffer(iCount);
46   }
47   void RemoveAll(FX_BOOL bLeaveMemory = FALSE) {
48     m_pLineChars->RemoveAll();
49     m_pLinePieces->RemoveAll(bLeaveMemory);
50     m_iWidth = 0;
51     m_iArabicChars = 0;
52   }
53   CFX_TxtCharArray* m_pLineChars;
54   CFX_TxtPieceArray* m_pLinePieces;
55   int32_t m_iStart;
56   int32_t m_iWidth;
57   int32_t m_iArabicChars;
58 };
59 class CFX_TxtBreak : public IFX_TxtBreak {
60  public:
61   CFX_TxtBreak(FX_DWORD dwPolicies);
62   ~CFX_TxtBreak();
Release()63   virtual void Release() { delete this; }
64   virtual void SetLineWidth(FX_FLOAT fLineWidth);
65   virtual void SetLinePos(FX_FLOAT fLinePos);
GetLayoutStyles()66   virtual FX_DWORD GetLayoutStyles() const { return m_dwLayoutStyles; }
67   virtual void SetLayoutStyles(FX_DWORD dwLayoutStyles);
68   virtual void SetFont(IFX_Font* pFont);
69   virtual void SetFontSize(FX_FLOAT fFontSize);
70   virtual void SetTabWidth(FX_FLOAT fTabWidth, FX_BOOL bEquidistant);
71   virtual void SetDefaultChar(FX_WCHAR wch);
72   virtual void SetParagraphBreakChar(FX_WCHAR wch);
73   virtual void SetLineBreakTolerance(FX_FLOAT fTolerance);
74   virtual void SetHorizontalScale(int32_t iScale);
75   virtual void SetVerticalScale(int32_t iScale);
76   virtual void SetCharRotation(int32_t iCharRotation);
77   virtual void SetCharSpace(FX_FLOAT fCharSpace);
78   virtual void SetAlignment(int32_t iAlignment);
79   virtual FX_DWORD GetContextCharStyles() const;
80   virtual void SetContextCharStyles(FX_DWORD dwCharStyles);
81   virtual void SetCombWidth(FX_FLOAT fCombWidth);
82   virtual void SetUserData(void* pUserData);
83   virtual FX_DWORD AppendChar(FX_WCHAR wch);
84   virtual FX_DWORD EndBreak(FX_DWORD dwStatus = FX_TXTBREAK_PieceBreak);
85   virtual int32_t CountBreakChars() const;
86   virtual int32_t CountBreakPieces() const;
87   virtual const CFX_TxtPiece* GetBreakPiece(int32_t index) const;
88   virtual void ClearBreakPieces();
89   virtual void Reset();
90   virtual int32_t GetDisplayPos(
91       FX_LPCTXTRUN pTxtRun,
92       FXTEXT_CHARPOS* pCharPos,
93       FX_BOOL bCharCode = FALSE,
94       CFX_WideString* pWSForms = NULL,
95       FX_AdjustCharDisplayPos pAdjustPos = NULL) const;
96   virtual int32_t GetCharRects(FX_LPCTXTRUN pTxtRun,
97                                CFX_RectFArray& rtArray,
98                                FX_BOOL bCharBBox = FALSE) const;
99   void AppendChar_PageLoad(CFX_Char* pCurChar, FX_DWORD dwProps);
100   FX_DWORD AppendChar_Combination(CFX_Char* pCurChar, int32_t iRotation);
101   FX_DWORD AppendChar_Tab(CFX_Char* pCurChar, int32_t iRotation);
102   FX_DWORD AppendChar_Control(CFX_Char* pCurChar, int32_t iRotation);
103   FX_DWORD AppendChar_Arabic(CFX_Char* pCurChar, int32_t iRotation);
104   FX_DWORD AppendChar_Others(CFX_Char* pCurChar, int32_t iRotation);
105 
106  protected:
107   FX_DWORD m_dwPolicies;
108   FX_BOOL m_bPagination;
109   IFX_ArabicChar* m_pArabicChar;
110   int32_t m_iLineWidth;
111   FX_DWORD m_dwLayoutStyles;
112   FX_BOOL m_bVertical;
113   FX_BOOL m_bArabicContext;
114   FX_BOOL m_bArabicShapes;
115   FX_BOOL m_bRTL;
116   FX_BOOL m_bSingleLine;
117   FX_BOOL m_bCombText;
118   int32_t m_iArabicContext;
119   int32_t m_iCurArabicContext;
120   IFX_Font* m_pFont;
121   int32_t m_iFontSize;
122   FX_BOOL m_bEquidistant;
123   int32_t m_iTabWidth;
124   FX_WCHAR m_wDefChar;
125   FX_WCHAR m_wParagBreakChar;
126   int32_t m_iDefChar;
127   int32_t m_iLineRotation;
128   int32_t m_iCharRotation;
129   int32_t m_iRotation;
130   int32_t m_iAlignment;
131   FX_DWORD m_dwContextCharStyles;
132   int32_t m_iCombWidth;
133   void* m_pUserData;
134   FX_DWORD m_dwCharType;
135   FX_BOOL m_bCurRTL;
136   int32_t m_iCurAlignment;
137   FX_BOOL m_bArabicNumber;
138   FX_BOOL m_bArabicComma;
139   CFX_TxtLine* m_pTxtLine1;
140   CFX_TxtLine* m_pTxtLine2;
141   CFX_TxtLine* m_pCurLine;
142   int32_t m_iReady;
143   int32_t m_iTolerance;
144   int32_t m_iHorScale;
145   int32_t m_iVerScale;
146   int32_t m_iCharSpace;
147   void SetBreakStatus();
148   int32_t GetLineRotation(FX_DWORD dwStyles) const;
149   CFX_TxtChar* GetLastChar(int32_t index, FX_BOOL bOmitChar = TRUE) const;
150   CFX_TxtLine* GetTxtLine(FX_BOOL bReady) const;
151   CFX_TxtPieceArray* GetTxtPieces(FX_BOOL bReady) const;
152   FX_DWORD GetUnifiedCharType(FX_DWORD dwType) const;
153   void ResetArabicContext();
154   void ResetContextCharStyles();
155   void EndBreak_UpdateArabicShapes();
156   FX_BOOL EndBreak_SplitLine(CFX_TxtLine* pNextLine,
157                              FX_BOOL bAllChars,
158                              FX_DWORD dwStatus);
159   void EndBreak_BidiLine(CFX_TPOArray& tpos, FX_DWORD dwStatus);
160   void EndBreak_Alignment(CFX_TPOArray& tpos,
161                           FX_BOOL bAllChars,
162                           FX_DWORD dwStatus);
163   int32_t GetBreakPos(CFX_TxtCharArray& ca,
164                       int32_t& iEndPos,
165                       FX_BOOL bAllChars = FALSE,
166                       FX_BOOL bOnlyBrk = FALSE);
167   void SplitTextLine(CFX_TxtLine* pCurLine,
168                      CFX_TxtLine* pNextLine,
169                      FX_BOOL bAllChars = FALSE);
170 };
171 #endif
172