summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/ime/ime_text_span.cc
blob: 9b13c0aa578a8d8b4ca6907642b3af467551b2d7 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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/editing/ime/ime_text_span.h"

#include <algorithm>
#include "ui/base/ime/ime_text_span.h"
#include "ui/base/ime/mojom/ime_types.mojom-blink.h"

namespace blink {

ImeTextSpan::ImeTextSpan(Type type,
                         unsigned start_offset,
                         unsigned end_offset,
                         const Color& underline_color,
                         ui::mojom::ImeTextSpanThickness thickness,
                         ui::mojom::ImeTextSpanUnderlineStyle underline_style,
                         const Color& text_color,
                         const Color& background_color,
                         const Color& suggestion_highlight_color,
                         bool remove_on_finish_composing,
                         bool interim_char_selection,
                         const Vector<String>& suggestions)
    : type_(type),
      underline_color_(underline_color),
      thickness_(thickness),
      underline_style_(underline_style),
      text_color_(text_color),
      background_color_(background_color),
      suggestion_highlight_color_(suggestion_highlight_color),
      remove_on_finish_composing_(remove_on_finish_composing),
      interim_char_selection_(interim_char_selection),
      suggestions_(suggestions) {
  // Sanitize offsets by ensuring a valid range corresponding to the last
  // possible position.
  // TODO(wkorman): Consider replacing with DCHECK_LT(startOffset, endOffset).
  start_offset_ =
      std::min(start_offset, std::numeric_limits<unsigned>::max() - 1u);
  end_offset_ = std::max(start_offset_ + 1u, end_offset);
}

namespace {

Vector<String> ConvertStdVectorOfStdStringsToVectorOfStrings(
    const std::vector<std::string>& input) {
  Vector<String> output;
  output.ReserveInitialCapacity(input.size());
  for (const std::string& val : input) {
    output.UncheckedAppend(String::FromUTF8(val));
  }
  return output;
}

ImeTextSpan::Type ConvertUiTypeToType(ui::ImeTextSpan::Type type) {
  switch (type) {
    case ui::ImeTextSpan::Type::kComposition:
      return ImeTextSpan::Type::kComposition;
    case ui::ImeTextSpan::Type::kSuggestion:
      return ImeTextSpan::Type::kSuggestion;
    case ui::ImeTextSpan::Type::kMisspellingSuggestion:
      return ImeTextSpan::Type::kMisspellingSuggestion;
    case ui::ImeTextSpan::Type::kAutocorrect:
      return ImeTextSpan::Type::kAutocorrect;
  }

  NOTREACHED();
  return ImeTextSpan::Type::kComposition;
}

ui::mojom::ImeTextSpanThickness ConvertUiThicknessToThickness(
    ui::ImeTextSpan::Thickness thickness) {
  switch (thickness) {
    case ui::ImeTextSpan::Thickness::kNone:
      return ui::mojom::ImeTextSpanThickness::kNone;
    case ui::ImeTextSpan::Thickness::kThin:
      return ui::mojom::ImeTextSpanThickness::kThin;
    case ui::ImeTextSpan::Thickness::kThick:
      return ui::mojom::ImeTextSpanThickness::kThick;
  }

  NOTREACHED();
  return ui::mojom::ImeTextSpanThickness::kNone;
}

ui::mojom::ImeTextSpanUnderlineStyle ConvertUiUnderlineToUnderline(
    ui::ImeTextSpan::UnderlineStyle underline) {
  switch (underline) {
    case ui::ImeTextSpan::UnderlineStyle::kNone:
      return ui::mojom::ImeTextSpanUnderlineStyle::kNone;
    case ui::ImeTextSpan::UnderlineStyle::kSolid:
      return ui::mojom::ImeTextSpanUnderlineStyle::kSolid;
    case ui::ImeTextSpan::UnderlineStyle::kDot:
      return ui::mojom::ImeTextSpanUnderlineStyle::kDot;
    case ui::ImeTextSpan::UnderlineStyle::kDash:
      return ui::mojom::ImeTextSpanUnderlineStyle::kDash;
    case ui::ImeTextSpan::UnderlineStyle::kSquiggle:
      return ui::mojom::ImeTextSpanUnderlineStyle::kSquiggle;
  }

  NOTREACHED();
  return ui::mojom::ImeTextSpanUnderlineStyle::kNone;
}

}  // namespace

ImeTextSpan::ImeTextSpan(const ui::ImeTextSpan& ime_text_span)
    : ImeTextSpan(ConvertUiTypeToType(ime_text_span.type),
                  ime_text_span.start_offset,
                  ime_text_span.end_offset,
                  Color(ime_text_span.underline_color),
                  ConvertUiThicknessToThickness(ime_text_span.thickness),
                  ConvertUiUnderlineToUnderline(ime_text_span.underline_style),
                  Color(ime_text_span.text_color),
                  Color(ime_text_span.background_color),
                  Color(ime_text_span.suggestion_highlight_color),
                  ime_text_span.remove_on_finish_composing,
                  ime_text_span.interim_char_selection,
                  ConvertStdVectorOfStdStringsToVectorOfStrings(
                      ime_text_span.suggestions)) {}
}  // namespace blink