summaryrefslogtreecommitdiff
path: root/core/src/fxcrt/fx_bidi.cpp
blob: 0310fa0e94b9d676fc9c9d74dd93691a2a039ccf (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
// 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

#include "../../include/fxcrt/fx_bidi.h"
#include "../../include/fxcrt/fx_ucd.h"

CFX_BidiChar::CFX_BidiChar()
    : m_iCurStart(0),
      m_iCurCount(0),
      m_CurBidi(NEUTRAL),
      m_iLastStart(0),
      m_iLastCount(0),
      m_LastBidi(NEUTRAL) {
}

CFX_BidiChar::~CFX_BidiChar() {
}

bool CFX_BidiChar::AppendChar(FX_WCHAR wch) {
  FX_DWORD dwProps = FX_GetUnicodeProperties(wch);
  int32_t iBidiCls = (dwProps & FX_BIDICLASSBITSMASK) >> FX_BIDICLASSBITS;
  Direction bidi = NEUTRAL;
  switch (iBidiCls) {
    case FX_BIDICLASS_L:
    case FX_BIDICLASS_AN:
    case FX_BIDICLASS_EN:
      bidi = LEFT;
      break;
    case FX_BIDICLASS_R:
    case FX_BIDICLASS_AL:
      bidi = RIGHT;
      break;
  }

  bool bRet = (bidi != m_CurBidi);
  if (bRet) {
    SaveCurrentStateToLastState();
    m_CurBidi = bidi;
  }
  m_iCurCount++;
  return bRet;
}

bool CFX_BidiChar::EndChar() {
  SaveCurrentStateToLastState();
  return m_iLastCount > 0;
}

CFX_BidiChar::Direction CFX_BidiChar::GetBidiInfo(int32_t* iStart,
                                                  int32_t* iCount) const {
  if (iStart)
    *iStart = m_iLastStart;
  if (iCount)
    *iCount = m_iLastCount;
  return m_LastBidi;
}

void CFX_BidiChar::SaveCurrentStateToLastState() {
  m_LastBidi = m_CurBidi;
  m_iLastStart = m_iCurStart;
  m_iCurStart = m_iCurCount;
  m_iLastCount = m_iCurCount - m_iLastStart;
}