| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/i18n/bidi_line_iterator.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace base { | |
| 10 namespace i18n { | |
| 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 | |
| 31 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) { | |
| 32 } | |
| 33 | |
| 34 BiDiLineIterator::~BiDiLineIterator() { | |
| 35 if (bidi_) { | |
| 36 ubidi_close(bidi_); | |
| 37 bidi_ = NULL; | |
| 38 } | |
| 39 } | |
| 40 | |
| 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); | |
| 50 } | |
| 51 | |
| 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; | |
| 57 } | |
| 58 | |
| 59 UBiDiDirection BiDiLineIterator::GetVisualRun(int index, | |
| 60 int* start, | |
| 61 int* length) { | |
| 62 DCHECK(bidi_ != NULL); | |
| 63 return ubidi_getVisualRun(bidi_, index, start, length); | |
| 64 } | |
| 65 | |
| 66 void BiDiLineIterator::GetLogicalRun(int start, | |
| 67 int* end, | |
| 68 UBiDiLevel* level) { | |
| 69 DCHECK(bidi_ != NULL); | |
| 70 ubidi_getLogicalRun(bidi_, start, end, level); | |
| 71 } | |
| 72 | |
| 73 } // namespace i18n | |
| 74 } // namespace base | |
| OLD | NEW |