summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/inline/ng_bidi_paragraph.cc
blob: 08a2a61198f127b8d0235202172c32cfe984f1c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 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 "third_party/blink/renderer/core/layout/ng/inline/ng_bidi_paragraph.h"

#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/text/icu_error.h"

namespace blink {

NGBidiParagraph::~NGBidiParagraph() {
  ubidi_close(ubidi_);
}

bool NGBidiParagraph::SetParagraph(const String& text,
                                   const ComputedStyle& block_style) {
  if (UNLIKELY(block_style.GetUnicodeBidi() == UnicodeBidi::kPlaintext))
    return SetParagraph(text, base::nullopt);
  return SetParagraph(text, block_style.Direction());
}

bool NGBidiParagraph::SetParagraph(
    const String& text,
    base::Optional<TextDirection> base_direction) {
  DCHECK(!ubidi_);
  ubidi_ = ubidi_open();

  UBiDiLevel para_level;
  if (base_direction) {
    base_direction_ = *base_direction;
    para_level = IsLtr(base_direction_) ? UBIDI_LTR : UBIDI_RTL;
  } else {
    para_level = UBIDI_DEFAULT_LTR;
  }

  ICUError error;
  ubidi_setPara(ubidi_, text.Characters16(), text.length(), para_level, nullptr,
                &error);
  if (U_FAILURE(error)) {
    NOTREACHED();
    ubidi_close(ubidi_);
    ubidi_ = nullptr;
    return false;
  }

  if (!base_direction)
    base_direction_ = DirectionFromLevel(ubidi_getParaLevel(ubidi_));

  return true;
}

TextDirection NGBidiParagraph::BaseDirectionForString(const StringView& text) {
  DCHECK(!text.Is8Bit());
  if (text.Is8Bit())
    return TextDirection::kLtr;
  UBiDiDirection direction =
      ubidi_getBaseDirection(text.Characters16(), text.length());
  return direction == UBIDI_RTL ? TextDirection::kRtl : TextDirection::kLtr;
}

unsigned NGBidiParagraph::GetLogicalRun(unsigned start,
                                        UBiDiLevel* level) const {
  int32_t end;
  ubidi_getLogicalRun(ubidi_, start, &end, level);
  return end;
}

void NGBidiParagraph::GetLogicalRuns(const String& text, Runs* runs) const {
  DCHECK(runs->IsEmpty());
  for (unsigned start = 0; start < text.length();) {
    UBiDiLevel level;
    unsigned end = GetLogicalRun(start, &level);
    DCHECK_GT(end, start);
    runs->emplace_back(start, end, level);
    start = end;
  }
}

void NGBidiParagraph::IndicesInVisualOrder(
    const Vector<UBiDiLevel, 32>& levels,
    Vector<int32_t, 32>* indices_in_visual_order_out) {
  // Check the size before passing the raw pointers to ICU.
  CHECK_EQ(levels.size(), indices_in_visual_order_out->size());
  ubidi_reorderVisual(levels.data(), levels.size(),
                      indices_in_visual_order_out->data());
}

}  // namespace blink