bidi_line_iterator.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "ui/gfx/bidi_line_iterator.h"
  5. #include "base/check.h"
  6. #include "base/notreached.h"
  7. namespace ui {
  8. namespace gfx {
  9. namespace {
  10. UBiDiLevel GetParagraphLevelForDirection(base::i18n::TextDirection direction) {
  11. switch (direction) {
  12. case base::i18n::UNKNOWN_DIRECTION:
  13. return UBIDI_DEFAULT_LTR;
  14. case base::i18n::RIGHT_TO_LEFT:
  15. return 1; // Highest RTL level.
  16. case base::i18n::LEFT_TO_RIGHT:
  17. return 0; // Highest LTR level.
  18. default:
  19. NOTREACHED();
  20. return 0;
  21. }
  22. }
  23. } // namespace
  24. BiDiLineIterator::BiDiLineIterator() = default;
  25. BiDiLineIterator::~BiDiLineIterator() = default;
  26. bool BiDiLineIterator::Open(const std::u16string& text,
  27. base::i18n::TextDirection direction) {
  28. DCHECK(!bidi_);
  29. UErrorCode error = U_ZERO_ERROR;
  30. bidi_.reset(ubidi_openSized(static_cast<int>(text.length()), 0, &error));
  31. if (U_FAILURE(error))
  32. return false;
  33. ubidi_setPara(bidi_.get(), text.data(), static_cast<int>(text.length()),
  34. GetParagraphLevelForDirection(direction), nullptr, &error);
  35. return (U_SUCCESS(error));
  36. }
  37. int BiDiLineIterator::CountRuns() const {
  38. DCHECK(bidi_ != nullptr);
  39. UErrorCode error = U_ZERO_ERROR;
  40. const int runs = ubidi_countRuns(bidi_.get(), &error);
  41. return U_SUCCESS(error) ? runs : 0;
  42. }
  43. UBiDiDirection BiDiLineIterator::GetVisualRun(int index,
  44. int* start,
  45. int* length) const {
  46. DCHECK(bidi_ != nullptr);
  47. return ubidi_getVisualRun(bidi_.get(), index, start, length);
  48. }
  49. void BiDiLineIterator::GetLogicalRun(int start,
  50. int* end,
  51. UBiDiLevel* level) const {
  52. DCHECK(bidi_ != nullptr);
  53. ubidi_getLogicalRun(bidi_.get(), start, end, level);
  54. }
  55. } // namespace gfx
  56. } // namespace ui