1 // Copyright (c) 2011 The Chromium 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.
5 #include "base/i18n/bidi_line_iterator.h"
7 #include "base/logging.h"
13 UBiDiLevel
GetParagraphLevelForDirection(TextDirection direction
) {
15 case UNKNOWN_DIRECTION
:
16 return UBIDI_DEFAULT_LTR
;
19 return 1; // Highest RTL level.
22 return 0; // Highest LTR level.
31 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL
) {
34 BiDiLineIterator::~BiDiLineIterator() {
41 bool BiDiLineIterator::Open(const string16
& text
, TextDirection direction
) {
43 UErrorCode error
= U_ZERO_ERROR
;
44 bidi_
= ubidi_openSized(static_cast<int>(text
.length()), 0, &error
);
47 ubidi_setPara(bidi_
, text
.data(), static_cast<int>(text
.length()),
48 GetParagraphLevelForDirection(direction
), NULL
, &error
);
49 return (U_SUCCESS(error
) == TRUE
);
52 int BiDiLineIterator::CountRuns() {
53 DCHECK(bidi_
!= NULL
);
54 UErrorCode error
= U_ZERO_ERROR
;
55 const int runs
= ubidi_countRuns(bidi_
, &error
);
56 return U_SUCCESS(error
) ? runs
: 0;
59 UBiDiDirection
BiDiLineIterator::GetVisualRun(int index
,
62 DCHECK(bidi_
!= NULL
);
63 return ubidi_getVisualRun(bidi_
, index
, start
, length
);
66 void BiDiLineIterator::GetLogicalRun(int start
,
69 DCHECK(bidi_
!= NULL
);
70 ubidi_getLogicalRun(bidi_
, start
, end
, level
);