bidi_line_iterator.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef UI_GFX_BIDI_LINE_ITERATOR_H_
  5. #define UI_GFX_BIDI_LINE_ITERATOR_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/i18n/rtl.h"
  9. #include "third_party/icu/source/common/unicode/ubidi.h"
  10. #include "third_party/icu/source/common/unicode/uchar.h"
  11. #include "ui/gfx/gfx_export.h"
  12. namespace ui {
  13. namespace gfx {
  14. class UBiDiDeleter {
  15. public:
  16. void operator()(UBiDi* ptr) { ubidi_close(ptr); }
  17. };
  18. // A simple wrapper class for the bidirectional iterator of ICU.
  19. // This class uses the bidirectional iterator of ICU to split a line of
  20. // bidirectional texts into visual runs in its display order.
  21. class GFX_EXPORT BiDiLineIterator {
  22. public:
  23. BiDiLineIterator();
  24. BiDiLineIterator(const BiDiLineIterator&) = delete;
  25. BiDiLineIterator& operator=(const BiDiLineIterator&) = delete;
  26. ~BiDiLineIterator();
  27. // Initializes the bidirectional iterator with the specified text. Returns
  28. // whether initialization succeeded.
  29. bool Open(const std::u16string& text, base::i18n::TextDirection direction);
  30. // Returns the number of visual runs in the text, or zero on error.
  31. int CountRuns() const;
  32. // Gets the logical offset, length, and direction of the specified visual run.
  33. UBiDiDirection GetVisualRun(int index, int* start, int* length) const;
  34. // Given a start position, figure out where the run ends (and the BiDiLevel).
  35. void GetLogicalRun(int start, int* end, UBiDiLevel* level) const;
  36. private:
  37. std::unique_ptr<UBiDi, UBiDiDeleter> bidi_;
  38. };
  39. } // namespace gfx
  40. } // namespace ui
  41. #endif // UI_GFX_BIDI_LINE_ITERATOR_H_