blob: 231392a8383dac1d1682a0a01334cc4925a94724 [file] [log] [blame]
Kaido Kert25902c62024-06-17 17:10:28 -07001// Copyright 2011 The Chromium Authors
Kaido Kert56d7c4e2024-04-13 12:59:27 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gfx/bidi_line_iterator.h"
6
7#include "base/check.h"
8#include "base/notreached.h"
9
10namespace ui {
11namespace gfx {
12
13namespace {
14
15UBiDiLevel GetParagraphLevelForDirection(base::i18n::TextDirection direction) {
16 switch (direction) {
17 case base::i18n::UNKNOWN_DIRECTION:
18 return UBIDI_DEFAULT_LTR;
19 case base::i18n::RIGHT_TO_LEFT:
20 return 1; // Highest RTL level.
21 case base::i18n::LEFT_TO_RIGHT:
22 return 0; // Highest LTR level.
23 default:
24 NOTREACHED();
25 return 0;
26 }
27}
28
29} // namespace
30
Kaido Kert25902c62024-06-17 17:10:28 -070031BiDiLineIterator::BiDiLineIterator() = default;
32BiDiLineIterator::~BiDiLineIterator() = default;
Kaido Kert56d7c4e2024-04-13 12:59:27 -070033
34bool BiDiLineIterator::Open(const std::u16string& text,
35 base::i18n::TextDirection direction) {
36 DCHECK(!bidi_);
37 UErrorCode error = U_ZERO_ERROR;
Kaido Kert25902c62024-06-17 17:10:28 -070038 bidi_.reset(ubidi_openSized(static_cast<int>(text.length()), 0, &error));
Kaido Kert56d7c4e2024-04-13 12:59:27 -070039 if (U_FAILURE(error))
40 return false;
41
Kaido Kert25902c62024-06-17 17:10:28 -070042 ubidi_setPara(bidi_.get(), text.data(), static_cast<int>(text.length()),
Kaido Kert56d7c4e2024-04-13 12:59:27 -070043 GetParagraphLevelForDirection(direction), nullptr, &error);
44 return (U_SUCCESS(error));
45}
46
47int BiDiLineIterator::CountRuns() const {
48 DCHECK(bidi_ != nullptr);
49 UErrorCode error = U_ZERO_ERROR;
Kaido Kert25902c62024-06-17 17:10:28 -070050 const int runs = ubidi_countRuns(bidi_.get(), &error);
Kaido Kert56d7c4e2024-04-13 12:59:27 -070051 return U_SUCCESS(error) ? runs : 0;
52}
53
54UBiDiDirection BiDiLineIterator::GetVisualRun(int index,
55 int* start,
56 int* length) const {
57 DCHECK(bidi_ != nullptr);
Kaido Kert25902c62024-06-17 17:10:28 -070058 return ubidi_getVisualRun(bidi_.get(), index, start, length);
Kaido Kert56d7c4e2024-04-13 12:59:27 -070059}
60
61void BiDiLineIterator::GetLogicalRun(int start,
62 int* end,
63 UBiDiLevel* level) const {
64 DCHECK(bidi_ != nullptr);
Kaido Kert25902c62024-06-17 17:10:28 -070065 ubidi_getLogicalRun(bidi_.get(), start, end, level);
Kaido Kert56d7c4e2024-04-13 12:59:27 -070066}
67
68} // namespace gfx
69} // namespace ui