xref: /aosp_15_r20/external/pdfium/xfa/fgas/layout/cfgas_txtbreak.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 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 #ifndef XFA_FGAS_LAYOUT_CFGAS_TXTBREAK_H_
8 #define XFA_FGAS_LAYOUT_CFGAS_TXTBREAK_H_
9 
10 #include <deque>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_coordinates.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "core/fxcrt/unowned_ptr_exclusion.h"
17 #include "xfa/fgas/layout/cfgas_break.h"
18 #include "xfa/fgas/layout/cfgas_char.h"
19 
20 class CFGAS_GEFont;
21 class TextCharPos;
22 
23 #define FX_TXTCHARSTYLE_ArabicShadda 0x0020
24 #define FX_TXTCHARSTYLE_OddBidiLevel 0x0040
25 
26 enum CFX_TxtLineAlignment {
27   CFX_TxtLineAlignment_Left = 0,
28   CFX_TxtLineAlignment_Center = 1 << 0,
29   CFX_TxtLineAlignment_Right = 1 << 1,
30   CFX_TxtLineAlignment_Justified = 1 << 2
31 };
32 
CFX_BreakTypeNoneOrPiece(CFGAS_Char::BreakType type)33 inline bool CFX_BreakTypeNoneOrPiece(CFGAS_Char::BreakType type) {
34   return type == CFGAS_Char::BreakType::kNone ||
35          type == CFGAS_Char::BreakType::kPiece;
36 }
37 
38 class CFGAS_TxtBreak final : public CFGAS_Break {
39  public:
40   class Engine {
41    public:
42     virtual ~Engine();
43     virtual wchar_t GetChar(size_t idx) const = 0;
44     // May return negative for combining characters. Non-const so we can force
45     // a layout if needed.
46     virtual int32_t GetWidthOfChar(size_t idx) = 0;
47   };
48 
49   struct Run {
50     Run();
51     Run(const Run& other);
52     ~Run();
53 
54     UnownedPtr<CFGAS_TxtBreak::Engine> pEdtEngine;
55     WideString wsStr;
56     UNOWNED_PTR_EXCLUSION int32_t* pWidths = nullptr;
57     // TODO(thestig): These 2 members probably should be size_t.
58     int32_t iStart = 0;
59     int32_t iLength = 0;
60     RetainPtr<CFGAS_GEFont> pFont;
61     float fFontSize = 12.0f;
62     Mask<LayoutStyle> dwStyles = LayoutStyle::kNone;
63     int32_t iHorizontalScale = 100;
64     int32_t iVerticalScale = 100;
65     uint32_t dwCharStyles = 0;
66     UnownedPtr<const CFX_RectF> pRect;
67     bool bSkipSpace = true;
68   };
69 
70   CFGAS_TxtBreak();
71   ~CFGAS_TxtBreak() override;
72 
73   void SetLineWidth(float fLineWidth);
74   void SetAlignment(int32_t iAlignment);
75   void SetCombWidth(float fCombWidth);
76   CFGAS_Char::BreakType EndBreak(CFGAS_Char::BreakType dwStatus);
77 
78   size_t GetDisplayPos(const Run& run, TextCharPos* pCharPos) const;
79   std::vector<CFX_RectF> GetCharRects(const Run& run) const;
80   CFGAS_Char::BreakType AppendChar(wchar_t wch);
81 
82  private:
83   void AppendChar_Combination(CFGAS_Char* pCurChar);
84   void AppendChar_Tab(CFGAS_Char* pCurChar);
85   CFGAS_Char::BreakType AppendChar_Control(CFGAS_Char* pCurChar);
86   CFGAS_Char::BreakType AppendChar_Arabic(CFGAS_Char* pCurChar);
87   CFGAS_Char::BreakType AppendChar_Others(CFGAS_Char* pCurChar);
88 
89   void ResetContextCharStyles();
90   void EndBreakSplitLine(CFGAS_BreakLine* pNextLine, bool bAllChars);
91   std::deque<TPO> EndBreakBidiLine(CFGAS_Char::BreakType dwStatus);
92   void EndBreakAlignment(const std::deque<TPO>& tpos,
93                          bool bAllChars,
94                          CFGAS_Char::BreakType dwStatus);
95   int32_t GetBreakPos(std::vector<CFGAS_Char>* pChars,
96                       bool bAllChars,
97                       bool bOnlyBrk,
98                       int32_t* pEndPos);
99   void SplitTextLine(CFGAS_BreakLine* pCurLine,
100                      CFGAS_BreakLine* pNextLine,
101                      bool bAllChars);
102 
103   int32_t m_iAlignment = CFX_TxtLineAlignment_Left;
104   int32_t m_iCombWidth = 360000;
105 };
106 
107 #endif  // XFA_FGAS_LAYOUT_CFGAS_TXTBREAK_H_
108