Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 1 | // Copyright 2011 The Chromium Authors |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 2 | // 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 | |
| 10 | namespace ui { |
| 11 | namespace gfx { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | UBiDiLevel 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 Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 31 | BiDiLineIterator::BiDiLineIterator() = default; |
| 32 | BiDiLineIterator::~BiDiLineIterator() = default; |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 33 | |
| 34 | bool BiDiLineIterator::Open(const std::u16string& text, |
| 35 | base::i18n::TextDirection direction) { |
| 36 | DCHECK(!bidi_); |
| 37 | UErrorCode error = U_ZERO_ERROR; |
Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 38 | bidi_.reset(ubidi_openSized(static_cast<int>(text.length()), 0, &error)); |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 39 | if (U_FAILURE(error)) |
| 40 | return false; |
| 41 | |
Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 42 | ubidi_setPara(bidi_.get(), text.data(), static_cast<int>(text.length()), |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 43 | GetParagraphLevelForDirection(direction), nullptr, &error); |
| 44 | return (U_SUCCESS(error)); |
| 45 | } |
| 46 | |
| 47 | int BiDiLineIterator::CountRuns() const { |
| 48 | DCHECK(bidi_ != nullptr); |
| 49 | UErrorCode error = U_ZERO_ERROR; |
Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 50 | const int runs = ubidi_countRuns(bidi_.get(), &error); |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 51 | return U_SUCCESS(error) ? runs : 0; |
| 52 | } |
| 53 | |
| 54 | UBiDiDirection BiDiLineIterator::GetVisualRun(int index, |
| 55 | int* start, |
| 56 | int* length) const { |
| 57 | DCHECK(bidi_ != nullptr); |
Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 58 | return ubidi_getVisualRun(bidi_.get(), index, start, length); |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void BiDiLineIterator::GetLogicalRun(int start, |
| 62 | int* end, |
| 63 | UBiDiLevel* level) const { |
| 64 | DCHECK(bidi_ != nullptr); |
Kaido Kert | 25902c6 | 2024-06-17 17:10:28 -0700 | [diff] [blame^] | 65 | ubidi_getLogicalRun(bidi_.get(), start, end, level); |
Kaido Kert | 56d7c4e | 2024-04-13 12:59:27 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | } // namespace gfx |
| 69 | } // namespace ui |