| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/i18n/bidi_line_iterator.h" | 5 #include "base/i18n/bidi_line_iterator.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 namespace i18n { | 10 namespace i18n { |
| 11 | 11 |
| 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; |
| 27 } |
| 28 } |
| 29 } // namespace |
| 30 |
| 12 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) { | 31 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) { |
| 13 } | 32 } |
| 14 | 33 |
| 15 BiDiLineIterator::~BiDiLineIterator() { | 34 BiDiLineIterator::~BiDiLineIterator() { |
| 16 if (bidi_) { | 35 if (bidi_) { |
| 17 ubidi_close(bidi_); | 36 ubidi_close(bidi_); |
| 18 bidi_ = NULL; | 37 bidi_ = NULL; |
| 19 } | 38 } |
| 20 } | 39 } |
| 21 | 40 |
| 22 bool BiDiLineIterator::Open(const string16& text, bool right_to_left) { | 41 bool BiDiLineIterator::Open(const string16& text, TextDirection direction) { |
| 23 DCHECK(!bidi_); | 42 DCHECK(!bidi_); |
| 24 UErrorCode error = U_ZERO_ERROR; | 43 UErrorCode error = U_ZERO_ERROR; |
| 25 bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error); | 44 bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error); |
| 26 if (U_FAILURE(error)) | 45 if (U_FAILURE(error)) |
| 27 return false; | 46 return false; |
| 28 ubidi_setPara(bidi_, text.data(), static_cast<int>(text.length()), | 47 ubidi_setPara(bidi_, text.data(), static_cast<int>(text.length()), |
| 29 right_to_left ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, | 48 GetParagraphLevelForDirection(direction), NULL, &error); |
| 30 NULL, &error); | |
| 31 return (U_SUCCESS(error) == TRUE); | 49 return (U_SUCCESS(error) == TRUE); |
| 32 } | 50 } |
| 33 | 51 |
| 34 int BiDiLineIterator::CountRuns() { | 52 int BiDiLineIterator::CountRuns() { |
| 35 DCHECK(bidi_ != NULL); | 53 DCHECK(bidi_ != NULL); |
| 36 UErrorCode error = U_ZERO_ERROR; | 54 UErrorCode error = U_ZERO_ERROR; |
| 37 const int runs = ubidi_countRuns(bidi_, &error); | 55 const int runs = ubidi_countRuns(bidi_, &error); |
| 38 return U_SUCCESS(error) ? runs : 0; | 56 return U_SUCCESS(error) ? runs : 0; |
| 39 } | 57 } |
| 40 | 58 |
| 41 UBiDiDirection BiDiLineIterator::GetVisualRun(int index, | 59 UBiDiDirection BiDiLineIterator::GetVisualRun(int index, |
| 42 int* start, | 60 int* start, |
| 43 int* length) { | 61 int* length) { |
| 44 DCHECK(bidi_ != NULL); | 62 DCHECK(bidi_ != NULL); |
| 45 return ubidi_getVisualRun(bidi_, index, start, length); | 63 return ubidi_getVisualRun(bidi_, index, start, length); |
| 46 } | 64 } |
| 47 | 65 |
| 48 void BiDiLineIterator::GetLogicalRun(int start, | 66 void BiDiLineIterator::GetLogicalRun(int start, |
| 49 int* end, | 67 int* end, |
| 50 UBiDiLevel* level) { | 68 UBiDiLevel* level) { |
| 51 DCHECK(bidi_ != NULL); | 69 DCHECK(bidi_ != NULL); |
| 52 ubidi_getLogicalRun(bidi_, start, end, level); | 70 ubidi_getLogicalRun(bidi_, start, end, level); |
| 53 } | 71 } |
| 54 | 72 |
| 55 } // namespace i18n | 73 } // namespace i18n |
| 56 } // namespace base | 74 } // namespace base |
| OLD | NEW |