xref: /aosp_15_r20/external/pdfium/core/fxcrt/fx_bidi.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
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 
9 #include <algorithm>
10 
11 #include "core/fxcrt/fx_unicode.h"
12 #include "third_party/base/check_op.h"
13 
CFX_BidiChar()14 CFX_BidiChar::CFX_BidiChar()
15     : m_CurrentSegment({0, 0, Direction::kNeutral}),
16       m_LastSegment({0, 0, Direction::kNeutral}) {}
17 
AppendChar(wchar_t wch)18 bool CFX_BidiChar::AppendChar(wchar_t wch) {
19   Direction direction;
20   switch (pdfium::unicode::GetBidiClass(wch)) {
21     case FX_BIDICLASS::kL:
22       direction = Direction::kLeft;
23       break;
24     case FX_BIDICLASS::kAN:
25     case FX_BIDICLASS::kEN:
26     case FX_BIDICLASS::kNSM:
27     case FX_BIDICLASS::kCS:
28     case FX_BIDICLASS::kES:
29     case FX_BIDICLASS::kET:
30     case FX_BIDICLASS::kBN:
31       direction = Direction::kLeftWeak;
32       break;
33     case FX_BIDICLASS::kR:
34     case FX_BIDICLASS::kAL:
35       direction = Direction::kRight;
36       break;
37     default:
38       direction = Direction::kNeutral;
39       break;
40   }
41 
42   bool bChangeDirection = (direction != m_CurrentSegment.direction);
43   if (bChangeDirection)
44     StartNewSegment(direction);
45 
46   m_CurrentSegment.count++;
47   return bChangeDirection;
48 }
49 
EndChar()50 bool CFX_BidiChar::EndChar() {
51   StartNewSegment(Direction::kNeutral);
52   return m_LastSegment.count > 0;
53 }
54 
StartNewSegment(CFX_BidiChar::Direction direction)55 void CFX_BidiChar::StartNewSegment(CFX_BidiChar::Direction direction) {
56   m_LastSegment = m_CurrentSegment;
57   m_CurrentSegment.start += m_CurrentSegment.count;
58   m_CurrentSegment.count = 0;
59   m_CurrentSegment.direction = direction;
60 }
61 
CFX_BidiString(const WideString & str)62 CFX_BidiString::CFX_BidiString(const WideString& str) : m_Str(str) {
63   CFX_BidiChar bidi;
64   for (wchar_t c : m_Str) {
65     if (bidi.AppendChar(c))
66       m_Order.push_back(bidi.GetSegmentInfo());
67   }
68   if (bidi.EndChar())
69     m_Order.push_back(bidi.GetSegmentInfo());
70 
71   size_t nR2L = std::count_if(
72       m_Order.begin(), m_Order.end(), [](const CFX_BidiChar::Segment& seg) {
73         return seg.direction == CFX_BidiChar::Direction::kRight;
74       });
75 
76   size_t nL2R = std::count_if(
77       m_Order.begin(), m_Order.end(), [](const CFX_BidiChar::Segment& seg) {
78         return seg.direction == CFX_BidiChar::Direction::kLeft;
79       });
80 
81   if (nR2L > 0 && nR2L >= nL2R)
82     SetOverallDirectionRight();
83 }
84 
85 CFX_BidiString::~CFX_BidiString() = default;
86 
OverallDirection() const87 CFX_BidiChar::Direction CFX_BidiString::OverallDirection() const {
88   DCHECK_NE(m_eOverallDirection, CFX_BidiChar::Direction::kNeutral);
89   DCHECK_NE(m_eOverallDirection, CFX_BidiChar::Direction::kLeftWeak);
90   return m_eOverallDirection;
91 }
92 
SetOverallDirectionRight()93 void CFX_BidiString::SetOverallDirectionRight() {
94   if (m_eOverallDirection != CFX_BidiChar::Direction::kRight) {
95     std::reverse(m_Order.begin(), m_Order.end());
96     m_eOverallDirection = CFX_BidiChar::Direction::kRight;
97   }
98 }
99