Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_bidi.cpp
Go to the documentation of this file.
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
15 : m_CurrentSegment({0, 0, Direction::kNeutral}),
16 m_LastSegment({0, 0, Direction::kNeutral}) {}
17
18bool CFX_BidiChar::AppendChar(wchar_t wch) {
19 Direction direction;
20 switch (pdfium::unicode::GetBidiClass(wch)) {
22 direction = Direction::kLeft;
23 break;
31 direction = Direction::kLeftWeak;
32 break;
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
51 StartNewSegment(Direction::kNeutral);
52 return m_LastSegment.count > 0;
53}
54
55void 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
62CFX_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)
83}
84
85CFX_BidiString::~CFX_BidiString() = default;
86
88 DCHECK_NE(m_eOverallDirection, CFX_BidiChar::Direction::kNeutral);
89 DCHECK_NE(m_eOverallDirection, CFX_BidiChar::Direction::kLeftWeak);
90 return m_eOverallDirection;
91}
92
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}
bool AppendChar(wchar_t wch)
Definition fx_bidi.cpp:18
bool EndChar()
Definition fx_bidi.cpp:50
void SetOverallDirectionRight()
Definition fx_bidi.cpp:93
CFX_BidiString(const WideString &str)
Definition fx_bidi.cpp:62
CFX_BidiChar::Direction OverallDirection() const
Definition fx_bidi.cpp:87
FX_BIDICLASS
Definition fx_unicode.h:13
FX_BIDICLASS GetBidiClass(wchar_t wch)
Direction direction
Definition fx_bidi.h:23