| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #ifndef CORE_INCLUDE_FXCRT_FX_BIDI_H_ | 7 #ifndef CORE_INCLUDE_FXCRT_FX_BIDI_H_ |
| 8 #define CORE_INCLUDE_FXCRT_FX_BIDI_H_ | 8 #define CORE_INCLUDE_FXCRT_FX_BIDI_H_ |
| 9 | 9 |
| 10 #include <memory> |
| 11 #include <vector> |
| 12 |
| 13 #include "fx_string.h" |
| 10 #include "fx_system.h" | 14 #include "fx_system.h" |
| 11 | 15 |
| 12 // Processes characters and group them into segments based on text direction. | 16 // Processes characters and group them into segments based on text direction. |
| 13 class CFX_BidiChar { | 17 class CFX_BidiChar { |
| 14 public: | 18 public: |
| 15 enum Direction { NEUTRAL, LEFT, RIGHT }; | 19 enum Direction { NEUTRAL, LEFT, RIGHT }; |
| 20 struct Segment { |
| 21 int32_t start; // Start position. |
| 22 int32_t count; // Character count. |
| 23 Direction direction; // Segment direction. |
| 24 }; |
| 16 | 25 |
| 17 CFX_BidiChar(); | 26 CFX_BidiChar(); |
| 18 ~CFX_BidiChar(); | |
| 19 | 27 |
| 20 // Append a character and classify it as left, right, or neutral. | 28 // Append a character and classify it as left, right, or neutral. |
| 21 // Returns true if the character has a different direction than the | 29 // Returns true if the character has a different direction than the |
| 22 // existing direction to indicate there is a segment to process. | 30 // existing direction to indicate there is a segment to process. |
| 23 bool AppendChar(FX_WCHAR wch); | 31 bool AppendChar(FX_WCHAR wch); |
| 24 | 32 |
| 25 // Call this after the last character has been appended. AppendChar() | 33 // Call this after the last character has been appended. AppendChar() |
| 26 // must not be called after this. | 34 // must not be called after this. |
| 27 // Returns true if there is still a segment to process. | 35 // Returns true if there is still a segment to process. |
| 28 bool EndChar(); | 36 bool EndChar(); |
| 29 | 37 |
| 30 // Get information about the segment to process. | 38 // Call after a change in direction is indicated by the above to get |
| 31 // The segment's start position and character count is returned in |iStart| | 39 // information about the segment to process. |
| 32 // and |iCount|, respectively. Pass in null pointers if the information is | 40 Segment GetSegmentInfo() const { return m_LastSegment; } |
| 33 // not needed. | |
| 34 // Returns the segment direction. | |
| 35 Direction GetBidiInfo(int32_t* iStart, int32_t* iCount) const; | |
| 36 | 41 |
| 37 private: | 42 private: |
| 38 void SaveCurrentStateToLastState(); | 43 void StartNewSegment(CFX_BidiChar::Direction direction); |
| 39 | 44 |
| 40 // Position of the current segment. | 45 Segment m_CurrentSegment; |
| 41 int32_t m_iCurStart; | 46 Segment m_LastSegment; |
| 47 }; |
| 42 | 48 |
| 43 // Number of characters in the current segment. | 49 class CFX_BidiString { |
| 44 int32_t m_iCurCount; | 50 public: |
| 51 using const_iterator = std::vector<CFX_BidiChar::Segment>::const_iterator; |
| 52 explicit CFX_BidiString(const CFX_WideString& str); |
| 45 | 53 |
| 46 // Direction of the current segment. | 54 // Overall direction is always LEFT or RIGHT, never NEUTRAL. |
| 47 Direction m_CurBidi; | 55 CFX_BidiChar::Direction OverallDirection() const { |
| 56 return m_eOverallDirection; |
| 57 } |
| 48 | 58 |
| 49 // Number of characters in the last segment. | 59 // Force the overall direction to be R2L regardless of what was detected. |
| 50 int32_t m_iLastStart; | 60 void SetOverallDirectionRight(); |
| 51 | 61 |
| 52 // Number of characters in the last segment. | 62 FX_WCHAR CharAt(size_t x) const { return m_Str[x]; } |
| 53 int32_t m_iLastCount; | 63 const_iterator begin() const { return m_Order.begin(); } |
| 64 const_iterator end() const { return m_Order.end(); } |
| 54 | 65 |
| 55 // Direction of the last segment. | 66 private: |
| 56 Direction m_LastBidi; | 67 const CFX_WideString m_Str; |
| 68 std::unique_ptr<CFX_BidiChar> m_pBidiChar; |
| 69 std::vector<CFX_BidiChar::Segment> m_Order; |
| 70 CFX_BidiChar::Direction m_eOverallDirection; |
| 57 }; | 71 }; |
| 58 | 72 |
| 59 #endif // CORE_INCLUDE_FXCRT_FX_BIDI_H_ | 73 #endif // CORE_INCLUDE_FXCRT_FX_BIDI_H_ |
| OLD | NEW |