diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 00:24:36 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 00:24:36 +0000 |
commit | 75d78246ad3bff129539b8259e7b35f2d49117a8 (patch) | |
tree | 9a96830b5046cff95d1aa57ed7187dc39494db0a /base | |
parent | f6ab5725d4fb9b8eac2f39f54308148388d5cfa3 (diff) | |
download | chromium_src-75d78246ad3bff129539b8259e7b35f2d49117a8.zip chromium_src-75d78246ad3bff129539b8259e7b35f2d49117a8.tar.gz chromium_src-75d78246ad3bff129539b8259e7b35f2d49117a8.tar.bz2 |
Move BiDiLineIterator to base/i18n/ directory.
BUG=23581
TEST=trybots
Review URL: http://codereview.chromium.org/6249007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71884 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/i18n/bidi_line_iterator.cc | 60 | ||||
-rw-r--r-- | base/i18n/bidi_line_iterator.h | 47 |
3 files changed, 109 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index 6dc450c..fbef42b 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -31,6 +31,8 @@ 'base', ], 'sources': [ + 'i18n/bidi_line_iterator.cc', + 'i18n/bidi_line_iterator.h', 'i18n/break_iterator.cc', 'i18n/break_iterator.h', 'i18n/char_iterator.cc', diff --git a/base/i18n/bidi_line_iterator.cc b/base/i18n/bidi_line_iterator.cc new file mode 100644 index 0000000..3222a3a --- /dev/null +++ b/base/i18n/bidi_line_iterator.cc @@ -0,0 +1,60 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/i18n/bidi_line_iterator.h" + +#include "base/logging.h" + +namespace base { +namespace i18n { + +BiDiLineIterator::BiDiLineIterator() : bidi_(NULL) { +} + +BiDiLineIterator::~BiDiLineIterator() { + if (bidi_) { + ubidi_close(bidi_); + bidi_ = NULL; + } +} + +bool BiDiLineIterator::Open(const string16& text, + bool right_to_left, + bool url) { + DCHECK(bidi_ == NULL); + UErrorCode error = U_ZERO_ERROR; + bidi_ = ubidi_openSized(static_cast<int>(text.length()), 0, &error); + if (U_FAILURE(error)) + return false; + if (right_to_left && url) + ubidi_setReorderingMode(bidi_, UBIDI_REORDER_RUNS_ONLY); + ubidi_setPara(bidi_, text.data(), static_cast<int>(text.length()), + right_to_left ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, + NULL, &error); + return U_SUCCESS(error) ? true : false; +} + +int BiDiLineIterator::CountRuns() { + DCHECK(bidi_ != NULL); + UErrorCode error = U_ZERO_ERROR; + const int runs = ubidi_countRuns(bidi_, &error); + return U_SUCCESS(error) ? runs : 0; +} + +UBiDiDirection BiDiLineIterator::GetVisualRun(int index, + int* start, + int* length) { + DCHECK(bidi_ != NULL); + return ubidi_getVisualRun(bidi_, index, start, length); +} + +void BiDiLineIterator::GetLogicalRun(int start, + int* end, + UBiDiLevel* level) { + DCHECK(bidi_ != NULL); + ubidi_getLogicalRun(bidi_, start, end, level); +} + +} // namespace i18n +} // namespace base diff --git a/base/i18n/bidi_line_iterator.h b/base/i18n/bidi_line_iterator.h new file mode 100644 index 0000000..5fff6a3 --- /dev/null +++ b/base/i18n/bidi_line_iterator.h @@ -0,0 +1,47 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_I18N_BIDI_LINE_ITERATOR_H_ +#define BASE_I18N_BIDI_LINE_ITERATOR_H_ +#pragma once + +#include "unicode/ubidi.h" + +#include "base/basictypes.h" +#include "base/string16.h" + +namespace base { +namespace i18n { + +// A simple wrapper class for the bidirectional iterator of ICU. +// This class uses the bidirectional iterator of ICU to split a line of +// bidirectional texts into visual runs in its display order. +class BiDiLineIterator { + public: + BiDiLineIterator(); + ~BiDiLineIterator(); + + // Initializes the bidirectional iterator with the specified text. Returns + // whether initialization succeeded. + bool Open(const string16& text, bool right_to_left, bool url); + + // Returns the number of visual runs in the text, or zero on error. + int CountRuns(); + + // Gets the logical offset, length, and direction of the specified visual run. + UBiDiDirection GetVisualRun(int index, int* start, int* length); + + // Given a start position, figure out where the run ends (and the BiDiLevel). + void GetLogicalRun(int start, int* end, UBiDiLevel* level); + + private: + UBiDi* bidi_; + + DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); +}; + +} // namespace i18n +} // namespace base + +#endif // BASE_I18N_BIDI_LINE_ITERATOR_H_ |