| 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 #include "core/include/fxcrt/fx_bidi.h" | 7 #include "core/include/fxcrt/fx_bidi.h" |
| 8 #include "core/include/fxcrt/fx_ucd.h" | 8 #include "core/include/fxcrt/fx_ucd.h" |
| 9 | 9 |
| 10 #include <algorithm> |
| 11 |
| 10 CFX_BidiChar::CFX_BidiChar() | 12 CFX_BidiChar::CFX_BidiChar() |
| 11 : m_iCurStart(0), | 13 : m_CurrentSegment({0, 0, NEUTRAL}), m_LastSegment({0, 0, NEUTRAL}) {} |
| 12 m_iCurCount(0), | |
| 13 m_CurBidi(NEUTRAL), | |
| 14 m_iLastStart(0), | |
| 15 m_iLastCount(0), | |
| 16 m_LastBidi(NEUTRAL) { | |
| 17 } | |
| 18 | |
| 19 CFX_BidiChar::~CFX_BidiChar() { | |
| 20 } | |
| 21 | 14 |
| 22 bool CFX_BidiChar::AppendChar(FX_WCHAR wch) { | 15 bool CFX_BidiChar::AppendChar(FX_WCHAR wch) { |
| 23 FX_DWORD dwProps = FX_GetUnicodeProperties(wch); | 16 FX_DWORD dwProps = FX_GetUnicodeProperties(wch); |
| 24 int32_t iBidiCls = (dwProps & FX_BIDICLASSBITSMASK) >> FX_BIDICLASSBITS; | 17 int32_t iBidiCls = (dwProps & FX_BIDICLASSBITSMASK) >> FX_BIDICLASSBITS; |
| 25 Direction bidi = NEUTRAL; | 18 Direction direction = NEUTRAL; |
| 26 switch (iBidiCls) { | 19 switch (iBidiCls) { |
| 27 case FX_BIDICLASS_L: | 20 case FX_BIDICLASS_L: |
| 28 case FX_BIDICLASS_AN: | 21 case FX_BIDICLASS_AN: |
| 29 case FX_BIDICLASS_EN: | 22 case FX_BIDICLASS_EN: |
| 30 bidi = LEFT; | 23 direction = LEFT; |
| 31 break; | 24 break; |
| 32 case FX_BIDICLASS_R: | 25 case FX_BIDICLASS_R: |
| 33 case FX_BIDICLASS_AL: | 26 case FX_BIDICLASS_AL: |
| 34 bidi = RIGHT; | 27 direction = RIGHT; |
| 35 break; | 28 break; |
| 36 } | 29 } |
| 37 | 30 |
| 38 bool bRet = (bidi != m_CurBidi); | 31 bool bChangeDirection = (direction != m_CurrentSegment.direction); |
| 39 if (bRet) { | 32 if (bChangeDirection) |
| 40 SaveCurrentStateToLastState(); | 33 StartNewSegment(direction); |
| 41 m_CurBidi = bidi; | 34 |
| 42 } | 35 m_CurrentSegment.count++; |
| 43 m_iCurCount++; | 36 return bChangeDirection; |
| 44 return bRet; | |
| 45 } | 37 } |
| 46 | 38 |
| 47 bool CFX_BidiChar::EndChar() { | 39 bool CFX_BidiChar::EndChar() { |
| 48 SaveCurrentStateToLastState(); | 40 StartNewSegment(NEUTRAL); |
| 49 return m_iLastCount > 0; | 41 return m_LastSegment.count > 0; |
| 50 } | 42 } |
| 51 | 43 |
| 52 CFX_BidiChar::Direction CFX_BidiChar::GetBidiInfo(int32_t* iStart, | 44 void CFX_BidiChar::StartNewSegment(CFX_BidiChar::Direction direction) { |
| 53 int32_t* iCount) const { | 45 m_LastSegment = m_CurrentSegment; |
| 54 if (iStart) | 46 m_CurrentSegment.start += m_CurrentSegment.count; |
| 55 *iStart = m_iLastStart; | 47 m_CurrentSegment.count = 0; |
| 56 if (iCount) | 48 m_CurrentSegment.direction = direction; |
| 57 *iCount = m_iLastCount; | |
| 58 return m_LastBidi; | |
| 59 } | 49 } |
| 60 | 50 |
| 61 void CFX_BidiChar::SaveCurrentStateToLastState() { | 51 CFX_BidiString::CFX_BidiString(const CFX_WideString& str) |
| 62 m_LastBidi = m_CurBidi; | 52 : m_Str(str), |
| 63 m_iLastStart = m_iCurStart; | 53 m_pBidiChar(new CFX_BidiChar), |
| 64 m_iCurStart = m_iCurCount; | 54 m_eOverallDirection(CFX_BidiChar::LEFT) { |
| 65 m_iLastCount = m_iCurCount - m_iLastStart; | 55 for (int i = 0; i < m_Str.GetLength(); ++i) { |
| 56 if (m_pBidiChar->AppendChar(m_Str.GetAt(i))) |
| 57 m_Order.push_back(m_pBidiChar->GetSegmentInfo()); |
| 58 } |
| 59 if (m_pBidiChar->EndChar()) |
| 60 m_Order.push_back(m_pBidiChar->GetSegmentInfo()); |
| 61 |
| 62 size_t nR2L = std::count_if(m_Order.begin(), m_Order.end(), |
| 63 [](const CFX_BidiChar::Segment& seg) { |
| 64 return seg.direction == CFX_BidiChar::RIGHT; |
| 65 }); |
| 66 |
| 67 size_t nL2R = std::count_if(m_Order.begin(), m_Order.end(), |
| 68 [](const CFX_BidiChar::Segment& seg) { |
| 69 return seg.direction == CFX_BidiChar::LEFT; |
| 70 }); |
| 71 |
| 72 if (nR2L > 0 && nR2L >= nL2R) |
| 73 SetOverallDirectionRight(); |
| 66 } | 74 } |
| 75 |
| 76 void CFX_BidiString::SetOverallDirectionRight() { |
| 77 if (m_eOverallDirection != CFX_BidiChar::RIGHT) { |
| 78 std::reverse(m_Order.begin(), m_Order.end()); |
| 79 m_eOverallDirection = CFX_BidiChar::RIGHT; |
| 80 } |
| 81 } |
| OLD | NEW |