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 #include "core/include/fxcrt/fx_bidi.h"
8 #include "core/include/fxcrt/fx_ucd.h"
9 
CFX_BidiChar()10 CFX_BidiChar::CFX_BidiChar()
11     : m_iCurStart(0),
12       m_iCurCount(0),
13       m_CurBidi(NEUTRAL),
14       m_iLastStart(0),
15       m_iLastCount(0),
16       m_LastBidi(NEUTRAL) {
17 }
18 
~CFX_BidiChar()19 CFX_BidiChar::~CFX_BidiChar() {
20 }
21 
AppendChar(FX_WCHAR wch)22 bool CFX_BidiChar::AppendChar(FX_WCHAR wch) {
23   FX_DWORD dwProps = FX_GetUnicodeProperties(wch);
24   int32_t iBidiCls = (dwProps & FX_BIDICLASSBITSMASK) >> FX_BIDICLASSBITS;
25   Direction bidi = NEUTRAL;
26   switch (iBidiCls) {
27     case FX_BIDICLASS_L:
28     case FX_BIDICLASS_AN:
29     case FX_BIDICLASS_EN:
30       bidi = LEFT;
31       break;
32     case FX_BIDICLASS_R:
33     case FX_BIDICLASS_AL:
34       bidi = RIGHT;
35       break;
36   }
37 
38   bool bRet = (bidi != m_CurBidi);
39   if (bRet) {
40     SaveCurrentStateToLastState();
41     m_CurBidi = bidi;
42   }
43   m_iCurCount++;
44   return bRet;
45 }
46 
EndChar()47 bool CFX_BidiChar::EndChar() {
48   SaveCurrentStateToLastState();
49   return m_iLastCount > 0;
50 }
51 
GetBidiInfo(int32_t * iStart,int32_t * iCount) const52 CFX_BidiChar::Direction CFX_BidiChar::GetBidiInfo(int32_t* iStart,
53                                                   int32_t* iCount) const {
54   if (iStart)
55     *iStart = m_iLastStart;
56   if (iCount)
57     *iCount = m_iLastCount;
58   return m_LastBidi;
59 }
60 
SaveCurrentStateToLastState()61 void CFX_BidiChar::SaveCurrentStateToLastState() {
62   m_LastBidi = m_CurBidi;
63   m_iLastStart = m_iCurStart;
64   m_iCurStart = m_iCurCount;
65   m_iLastCount = m_iCurCount - m_iLastStart;
66 }
67