Roll src/third_party/WebKit 4619053:6b63e20 (svn 201059:201060)
[chromium-blink-merge.git] / base / i18n / bidi_line_iterator.cc
blob216129e8b88eb1a4514ddb7ab8fc251a972eaab1
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"
9 namespace base {
10 namespace i18n {
12 namespace {
13 UBiDiLevel GetParagraphLevelForDirection(TextDirection direction) {
14 switch (direction) {
15 case UNKNOWN_DIRECTION:
16 return UBIDI_DEFAULT_LTR;
17 break;
18 case RIGHT_TO_LEFT:
19 return 1; // Highest RTL level.
20 break;
21 case LEFT_TO_RIGHT:
22 return 0; // Highest LTR level.
23 break;
24 default:
25 NOTREACHED();
26 return 0;
29 } // namespace
31 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) {
34 BiDiLineIterator::~BiDiLineIterator() {
35 if (bidi_) {
36 ubidi_close(bidi_);
37 bidi_ = NULL;
41 bool BiDiLineIterator::Open(const string16& text, TextDirection direction) {
42 DCHECK(!bidi_);
43 UErrorCode error = U_ZERO_ERROR;
44 bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error);
45 if (U_FAILURE(error))
46 return false;
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,
60 int* start,
61 int* length) {
62 DCHECK(bidi_ != NULL);
63 return ubidi_getVisualRun(bidi_, index, start, length);
66 void BiDiLineIterator::GetLogicalRun(int start,
67 int* end,
68 UBiDiLevel* level) {
69 DCHECK(bidi_ != NULL);
70 ubidi_getLogicalRun(bidi_, start, end, level);
73 } // namespace i18n
74 } // namespace base