summaryrefslogtreecommitdiff
path: root/xfa/fgas/layout/fgas_rtfbreak.h
blob: 613156a3e035531aa49029f0fd7a58d01eead178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef XFA_FGAS_LAYOUT_FGAS_RTFBREAK_H_
#define XFA_FGAS_LAYOUT_FGAS_RTFBREAK_H_

#include <vector>

#include "core/fxcrt/fx_basic.h"
#include "core/fxcrt/fx_ucd.h"
#include "xfa/fgas/crt/fgas_utils.h"
#include "xfa/fgas/layout/fgas_textbreak.h"
#include "xfa/fgas/layout/fgas_unicode.h"

class CFGAS_GEFont;

#define FX_RTFBREAKPOLICY_None 0x00
#define FX_RTFBREAKPOLICY_SpaceBreak 0x01
#define FX_RTFBREAKPOLICY_NumberBreak 0x02
#define FX_RTFBREAKPOLICY_InfixBreak 0x04
#define FX_RTFBREAKPOLICY_TabBreak 0x08
#define FX_RTFBREAKPOLICY_OrphanPositionedTab 0x10
#define FX_RTFBREAK_None 0x00
#define FX_RTFBREAK_PieceBreak 0x01
#define FX_RTFBREAK_LineBreak 0x02
#define FX_RTFBREAK_ParagraphBreak 0x03
#define FX_RTFBREAK_PageBreak 0x04
#define FX_RTFLAYOUTSTYLE_Pagination 0x01
#define FX_RTFLAYOUTSTYLE_VerticalLayout 0x02
#define FX_RTFLAYOUTSTYLE_VerticalChars 0x04
#define FX_RTFLAYOUTSTYLE_LineDirection 0x08
#define FX_RTFLAYOUTSTYLE_ExpandTab 0x10
#define FX_RTFLAYOUTSTYLE_ArabicNumber 0x20
#define FX_RTFLAYOUTSTYLE_SingleLine 0x40
#define FX_RTFLAYOUTSTYLE_MBCSCode 0x80
#define FX_RTFCHARSTYLE_Alignment 0x000F
#define FX_RTFCHARSTYLE_ArabicNumber 0x0010
#define FX_RTFCHARSTYLE_ArabicShadda 0x0020
#define FX_RTFCHARSTYLE_OddBidiLevel 0x0040
#define FX_RTFCHARSTYLE_RTLReadingOrder 0x0080
#define FX_RTFCHARSTYLE_ArabicContext 0x0300
#define FX_RTFCHARSTYLE_ArabicIndic 0x0400
#define FX_RTFCHARSTYLE_ArabicComma 0x0800
#define FX_RTFLINEALIGNMENT_Left 0
#define FX_RTFLINEALIGNMENT_Center 1
#define FX_RTFLINEALIGNMENT_Right 2
#define FX_RTFLINEALIGNMENT_Justified (1 << 2)
#define FX_RTFLINEALIGNMENT_Distributed (2 << 2)
#define FX_RTFLINEALIGNMENT_JustifiedLeft \
  (FX_RTFLINEALIGNMENT_Left | FX_RTFLINEALIGNMENT_Justified)
#define FX_RTFLINEALIGNMENT_JustifiedCenter \
  (FX_RTFLINEALIGNMENT_Center | FX_RTFLINEALIGNMENT_Justified)
#define FX_RTFLINEALIGNMENT_JustifiedRight \
  (FX_RTFLINEALIGNMENT_Right | FX_RTFLINEALIGNMENT_Justified)
#define FX_RTFLINEALIGNMENT_DistributedLeft \
  (FX_RTFLINEALIGNMENT_Left | FX_RTFLINEALIGNMENT_Distributed)
#define FX_RTFLINEALIGNMENT_DistributedCenter \
  (FX_RTFLINEALIGNMENT_Center | FX_RTFLINEALIGNMENT_Distributed)
#define FX_RTFLINEALIGNMENT_DistributedRight \
  (FX_RTFLINEALIGNMENT_Right | FX_RTFLINEALIGNMENT_Distributed)
#define FX_RTFLINEALIGNMENT_LowerMask 0x03
#define FX_RTFLINEALIGNMENT_HigherMask 0x0C

struct FX_RTFTEXTOBJ {
  FX_RTFTEXTOBJ();
  ~FX_RTFTEXTOBJ();

  const FX_WCHAR* pStr;
  int32_t* pWidths;
  int32_t iLength;
  CFX_RetainPtr<CFGAS_GEFont> pFont;
  FX_FLOAT fFontSize;
  uint32_t dwLayoutStyles;
  int32_t iCharRotation;
  int32_t iBidiLevel;
  const CFX_RectF* pRect;
  FX_WCHAR wLineBreakChar;
  int32_t iHorizontalScale;
  int32_t iVerticalScale;
};

class CFX_RTFPiece {
 public:
  CFX_RTFPiece();
  ~CFX_RTFPiece();

  void AppendChar(const CFX_RTFChar& tc) {
    ASSERT(m_pChars);
    m_pChars->push_back(tc);
    if (m_iWidth < 0) {
      m_iWidth = tc.m_iCharWidth;
    } else {
      m_iWidth += tc.m_iCharWidth;
    }
    m_iChars++;
  }

  int32_t GetEndPos() const {
    return m_iWidth < 0 ? m_iStartPos : m_iStartPos + m_iWidth;
  }

  int32_t GetLength() const { return m_iChars; }
  int32_t GetEndChar() const { return m_iStartChar + m_iChars; }

  CFX_RTFChar& GetChar(int32_t index) {
    ASSERT(index > -1 && index < m_iChars && m_pChars);
    return (*m_pChars)[m_iStartChar + index];
  }

  CFX_RTFChar* GetCharPtr(int32_t index) const {
    ASSERT(index > -1 && index < m_iChars && m_pChars);
    return &(*m_pChars)[m_iStartChar + index];
  }

  void GetString(FX_WCHAR* pText) const {
    ASSERT(pText);
    int32_t iEndChar = m_iStartChar + m_iChars;
    for (int32_t i = m_iStartChar; i < iEndChar; i++)
      *pText++ = static_cast<FX_WCHAR>((*m_pChars)[i].m_wCharCode);
  }

  void GetString(CFX_WideString& wsText) const {
    FX_WCHAR* pText = wsText.GetBuffer(m_iChars);
    GetString(pText);
    wsText.ReleaseBuffer(m_iChars);
  }

  void GetWidths(int32_t* pWidths) const {
    ASSERT(pWidths);
    int32_t iEndChar = m_iStartChar + m_iChars;
    for (int32_t i = m_iStartChar; i < iEndChar; i++)
      *pWidths++ = (*m_pChars)[i].m_iCharWidth;
  }

  void Reset() {
    m_dwStatus = FX_RTFBREAK_PieceBreak;
    if (m_iWidth > -1) {
      m_iStartPos += m_iWidth;
    }
    m_iWidth = -1;
    m_iStartChar += m_iChars;
    m_iChars = 0;
    m_iBidiLevel = 0;
    m_iBidiPos = 0;
    m_iHorizontalScale = 100;
    m_iVerticalScale = 100;
  }

  uint32_t m_dwStatus;
  int32_t m_iStartPos;
  int32_t m_iWidth;
  int32_t m_iStartChar;
  int32_t m_iChars;
  int32_t m_iBidiLevel;
  int32_t m_iBidiPos;
  int32_t m_iFontSize;
  int32_t m_iFontHeight;
  int32_t m_iHorizontalScale;
  int32_t m_iVerticalScale;
  uint32_t m_dwLayoutStyles;
  uint32_t m_dwIdentity;
  std::vector<CFX_RTFChar>* m_pChars;  // not owned.
  IFX_Retainable* m_pUserData;
};

typedef CFX_BaseArrayTemplate<CFX_RTFPiece> CFX_RTFPieceArray;

class CFX_RTFLine {
 public:
  CFX_RTFLine();
  ~CFX_RTFLine();

  int32_t CountChars() const {
    return pdfium::CollectionSize<int32_t>(m_LineChars);
  }
  CFX_RTFChar& GetChar(int32_t index) {
    ASSERT(index >= 0 && index < pdfium::CollectionSize<int32_t>(m_LineChars));
    return m_LineChars[index];
  }
  CFX_RTFChar* GetCharPtr(int32_t index) {
    ASSERT(index > -1 && index < pdfium::CollectionSize<int32_t>(m_LineChars));
    return &m_LineChars[index];
  }
  int32_t CountPieces() const { return m_LinePieces.GetSize(); }
  CFX_RTFPiece& GetPiece(int32_t index) const {
    ASSERT(index > -1 && index < m_LinePieces.GetSize());
    return m_LinePieces.GetAt(index);
  }
  CFX_RTFPiece* GetPiecePtr(int32_t index) const {
    ASSERT(index > -1 && index < m_LinePieces.GetSize());
    return m_LinePieces.GetPtrAt(index);
  }
  int32_t GetLineEnd() const { return m_iStart + m_iWidth; }
  void RemoveAll(bool bLeaveMemory = false) {
    int32_t iCount = pdfium::CollectionSize<int32_t>(m_LineChars);
    for (int32_t i = 0; i < iCount; i++) {
      CFX_RTFChar* pChar = &m_LineChars[i];
      if (pChar->m_pUserData)
        pChar->m_pUserData->Release();
    }
    m_LineChars.clear();
    m_LinePieces.RemoveAll(bLeaveMemory);
    m_iWidth = 0;
    m_iArabicChars = 0;
    m_iMBCSChars = 0;
  }

  std::vector<CFX_RTFChar> m_LineChars;
  CFX_RTFPieceArray m_LinePieces;
  int32_t m_iStart;
  int32_t m_iWidth;
  int32_t m_iArabicChars;
  int32_t m_iMBCSChars;
};

class CFX_RTFBreak {
 public:
  explicit CFX_RTFBreak(uint32_t dwPolicies);
  ~CFX_RTFBreak();

  void SetLineBoundary(FX_FLOAT fLineStart, FX_FLOAT fLineEnd);
  void SetLineStartPos(FX_FLOAT fLinePos);
  uint32_t GetLayoutStyles() const { return m_dwLayoutStyles; }
  void SetLayoutStyles(uint32_t dwLayoutStyles);
  void SetFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont);
  void SetFontSize(FX_FLOAT fFontSize);
  void SetTabWidth(FX_FLOAT fTabWidth);
  void AddPositionedTab(FX_FLOAT fTabPos);
  void SetPositionedTabs(const std::vector<FX_FLOAT>& tabs);
  void ClearPositionedTabs();
  void SetDefaultChar(FX_WCHAR wch);
  void SetLineBreakChar(FX_WCHAR wch);
  void SetLineBreakTolerance(FX_FLOAT fTolerance);
  void SetHorizontalScale(int32_t iScale);
  void SetVerticalScale(int32_t iScale);
  void SetCharRotation(int32_t iCharRotation);
  void SetCharSpace(FX_FLOAT fCharSpace);
  void SetWordSpace(bool bDefault, FX_FLOAT fWordSpace);
  void SetReadingOrder(bool bRTL = false);
  void SetAlignment(int32_t iAlignment = FX_RTFLINEALIGNMENT_Left);
  void SetUserData(IFX_Retainable* pUserData);
  uint32_t AppendChar(FX_WCHAR wch);
  uint32_t EndBreak(uint32_t dwStatus = FX_RTFBREAK_PieceBreak);
  int32_t CountBreakPieces() const;
  const CFX_RTFPiece* GetBreakPiece(int32_t index) const;
  void GetLineRect(CFX_RectF& rect) const;
  void ClearBreakPieces();
  void Reset();
  int32_t GetDisplayPos(const FX_RTFTEXTOBJ* pText,
                        FXTEXT_CHARPOS* pCharPos,
                        bool bCharCode = false,
                        CFX_WideString* pWSForms = nullptr,
                        FX_AdjustCharDisplayPos pAdjustPos = nullptr) const;
  int32_t GetCharRects(const FX_RTFTEXTOBJ* pText,
                       std::vector<CFX_RectF>* rtArray,
                       bool bCharBBox = false) const;
  uint32_t AppendChar_CharCode(FX_WCHAR wch);
  uint32_t AppendChar_Combination(CFX_RTFChar* pCurChar, int32_t iRotation);
  uint32_t AppendChar_Tab(CFX_RTFChar* pCurChar, int32_t iRotation);
  uint32_t AppendChar_Control(CFX_RTFChar* pCurChar, int32_t iRotation);
  uint32_t AppendChar_Arabic(CFX_RTFChar* pCurChar, int32_t iRotation);
  uint32_t AppendChar_Others(CFX_RTFChar* pCurChar, int32_t iRotation);

 protected:
  int32_t GetLineRotation(uint32_t dwStyles) const;
  void SetBreakStatus();
  CFX_RTFChar* GetLastChar(int32_t index) const;
  CFX_RTFLine* GetRTFLine(bool bReady) const;
  CFX_RTFPieceArray* GetRTFPieces(bool bReady) const;
  FX_CHARTYPE GetUnifiedCharType(FX_CHARTYPE chartype) const;
  int32_t GetLastPositionedTab() const;
  bool GetPositionedTab(int32_t& iTabPos) const;

  int32_t GetBreakPos(std::vector<CFX_RTFChar>& tca,
                      int32_t& iEndPos,
                      bool bAllChars = false,
                      bool bOnlyBrk = false);
  void SplitTextLine(CFX_RTFLine* pCurLine,
                     CFX_RTFLine* pNextLine,
                     bool bAllChars = false);
  bool EndBreak_SplitLine(CFX_RTFLine* pNextLine,
                          bool bAllChars,
                          uint32_t dwStatus);
  void EndBreak_BidiLine(CFX_TPOArray& tpos, uint32_t dwStatus);
  void EndBreak_Alignment(CFX_TPOArray& tpos,
                          bool bAllChars,
                          uint32_t dwStatus);

  uint32_t m_dwPolicies;
  int32_t m_iBoundaryStart;
  int32_t m_iBoundaryEnd;
  uint32_t m_dwLayoutStyles;
  bool m_bPagination;
  bool m_bVertical;
  bool m_bSingleLine;
  bool m_bCharCode;
  CFX_RetainPtr<CFGAS_GEFont> m_pFont;
  int32_t m_iFontHeight;
  int32_t m_iFontSize;
  int32_t m_iTabWidth;
  CFX_ArrayTemplate<int32_t> m_PositionedTabs;
  bool m_bOrphanLine;
  FX_WCHAR m_wDefChar;
  int32_t m_iDefChar;
  FX_WCHAR m_wLineBreakChar;
  int32_t m_iHorizontalScale;
  int32_t m_iVerticalScale;
  int32_t m_iLineRotation;
  int32_t m_iCharRotation;
  int32_t m_iRotation;
  int32_t m_iCharSpace;
  bool m_bWordSpace;
  int32_t m_iWordSpace;
  bool m_bRTL;
  int32_t m_iAlignment;
  IFX_Retainable* m_pUserData;
  FX_CHARTYPE m_eCharType;
  uint32_t m_dwIdentity;
  CFX_RTFLine m_RTFLine1;
  CFX_RTFLine m_RTFLine2;
  CFX_RTFLine* m_pCurLine;
  int32_t m_iReady;
  int32_t m_iTolerance;
};

#endif  // XFA_FGAS_LAYOUT_FGAS_RTFBREAK_H_