• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fxcrt/fx_bidi.h"
8 #include "core/fxcrt/fx_ucd.h"
9 
10 #include <algorithm>
11 
CFX_BidiChar()12 CFX_BidiChar::CFX_BidiChar()
13     : m_CurrentSegment({0, 0, NEUTRAL}), m_LastSegment({0, 0, NEUTRAL}) {}
14 
AppendChar(FX_WCHAR wch)15 bool CFX_BidiChar::AppendChar(FX_WCHAR wch) {
16   uint32_t dwProps = FX_GetUnicodeProperties(wch);
17   int32_t iBidiCls = (dwProps & FX_BIDICLASSBITSMASK) >> FX_BIDICLASSBITS;
18   Direction direction = NEUTRAL;
19   switch (iBidiCls) {
20     case FX_BIDICLASS_L:
21     case FX_BIDICLASS_AN:
22     case FX_BIDICLASS_EN:
23       direction = LEFT;
24       break;
25     case FX_BIDICLASS_R:
26     case FX_BIDICLASS_AL:
27       direction = RIGHT;
28       break;
29   }
30 
31   bool bChangeDirection = (direction != m_CurrentSegment.direction);
32   if (bChangeDirection)
33     StartNewSegment(direction);
34 
35   m_CurrentSegment.count++;
36   return bChangeDirection;
37 }
38 
EndChar()39 bool CFX_BidiChar::EndChar() {
40   StartNewSegment(NEUTRAL);
41   return m_LastSegment.count > 0;
42 }
43 
StartNewSegment(CFX_BidiChar::Direction direction)44 void CFX_BidiChar::StartNewSegment(CFX_BidiChar::Direction direction) {
45   m_LastSegment = m_CurrentSegment;
46   m_CurrentSegment.start += m_CurrentSegment.count;
47   m_CurrentSegment.count = 0;
48   m_CurrentSegment.direction = direction;
49 }
50 
CFX_BidiString(const CFX_WideString & str)51 CFX_BidiString::CFX_BidiString(const CFX_WideString& str)
52     : m_Str(str),
53       m_pBidiChar(new CFX_BidiChar),
54       m_eOverallDirection(CFX_BidiChar::LEFT) {
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();
74 }
75 
~CFX_BidiString()76 CFX_BidiString::~CFX_BidiString() {}
77 
SetOverallDirectionRight()78 void CFX_BidiString::SetOverallDirectionRight() {
79   if (m_eOverallDirection != CFX_BidiChar::RIGHT) {
80     std::reverse(m_Order.begin(), m_Order.end());
81     m_eOverallDirection = CFX_BidiChar::RIGHT;
82   }
83 }
84