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 BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) { 13 } 14 15 BiDiLineIterator::~BiDiLineIterator() { 16 if (bidi_) { 17 ubidi_close(bidi_); 18 bidi_ = NULL; 19 } 20 } 21 22 bool BiDiLineIterator::Open(const string16& text, 23 bool right_to_left, 24 bool url) { 25 DCHECK(!bidi_); 26 UErrorCode error = U_ZERO_ERROR; 27 bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error); 28 if (U_FAILURE(error)) 29 return false; 30 if (right_to_left && url) 31 ubidi_setReorderingMode(bidi_, UBIDI_REORDER_RUNS_ONLY); 32 ubidi_setPara(bidi_, text.data(), static_cast<int>(text.length()), 33 right_to_left ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, 34 NULL, &error); 35 return (U_SUCCESS(error) == TRUE); 36 } 37 38 int BiDiLineIterator::CountRuns() { 39 DCHECK(bidi_ != NULL); 40 UErrorCode error = U_ZERO_ERROR; 41 const int runs = ubidi_countRuns(bidi_, &error); 42 return U_SUCCESS(error) ? runs : 0; 43 } 44 45 UBiDiDirection BiDiLineIterator::GetVisualRun(int index, 46 int* start, 47 int* length) { 48 DCHECK(bidi_ != NULL); 49 return ubidi_getVisualRun(bidi_, index, start, length); 50 } 51 52 void BiDiLineIterator::GetLogicalRun(int start, 53 int* end, 54 UBiDiLevel* level) { 55 DCHECK(bidi_ != NULL); 56 ubidi_getLogicalRun(bidi_, start, end, level); 57 } 58 59 } // namespace i18n 60 } // namespace base 61