Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ui / base / ime / composition_text_util_pango.cc
blob98dadb6cc407cad8042833ffd472caa0c1790e45
1 // Copyright 2013 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.
5 #include "ui/base/ime/composition_text_util_pango.h"
7 #include <pango/pango-attributes.h>
9 #include "base/basictypes.h"
10 #include "base/i18n/char_iterator.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/base/ime/composition_text.h"
15 namespace ui {
17 void ExtractCompositionTextFromGtkPreedit(const gchar* utf8_text,
18 PangoAttrList* attrs,
19 int cursor_position,
20 CompositionText* composition) {
21 composition->Clear();
22 composition->text = base::UTF8ToUTF16(utf8_text);
24 if (composition->text.empty())
25 return;
27 // Gtk/Pango uses character index for cursor position and byte index for
28 // attribute range, but we use char16 offset for them. So we need to do
29 // conversion here.
30 std::vector<size_t> char16_offsets;
31 size_t length = composition->text.length();
32 base::i18n::UTF16CharIterator char_iterator(&composition->text);
33 do {
34 char16_offsets.push_back(char_iterator.array_pos());
35 } while (char_iterator.Advance());
37 // The text length in Unicode characters.
38 int char_length = static_cast<int>(char16_offsets.size());
39 // Make sure we can convert the value of |char_length| as well.
40 char16_offsets.push_back(length);
42 size_t cursor_offset =
43 char16_offsets[std::max(0, std::min(char_length, cursor_position))];
45 composition->selection = gfx::Range(cursor_offset);
47 if (attrs) {
48 int utf8_length = strlen(utf8_text);
49 PangoAttrIterator* iter = pango_attr_list_get_iterator(attrs);
51 // We only care about underline and background attributes and convert
52 // background attribute into selection if possible.
53 do {
54 gint start, end;
55 pango_attr_iterator_range(iter, &start, &end);
57 start = std::min(start, utf8_length);
58 end = std::min(end, utf8_length);
59 if (start >= end)
60 continue;
62 start = g_utf8_pointer_to_offset(utf8_text, utf8_text + start);
63 end = g_utf8_pointer_to_offset(utf8_text, utf8_text + end);
65 // Double check, in case |utf8_text| is not a valid utf-8 string.
66 start = std::min(start, char_length);
67 end = std::min(end, char_length);
68 if (start >= end)
69 continue;
71 PangoAttribute* background_attr =
72 pango_attr_iterator_get(iter, PANGO_ATTR_BACKGROUND);
73 PangoAttribute* underline_attr =
74 pango_attr_iterator_get(iter, PANGO_ATTR_UNDERLINE);
76 if (background_attr || underline_attr) {
77 // Use a black thin underline by default.
78 CompositionUnderline underline(
79 char16_offsets[start], char16_offsets[end], SK_ColorBLACK, false);
81 // Always use thick underline for a range with background color, which
82 // is usually the selection range.
83 if (background_attr) {
84 underline.thick = true;
85 // If the cursor is at start or end of this underline, then we treat
86 // it as the selection range as well, but make sure to set the cursor
87 // position to the selection end.
88 if (underline.start_offset == cursor_offset) {
89 composition->selection.set_start(underline.end_offset);
90 composition->selection.set_end(cursor_offset);
91 } else if (underline.end_offset == cursor_offset) {
92 composition->selection.set_start(underline.start_offset);
93 composition->selection.set_end(cursor_offset);
96 if (underline_attr) {
97 int type = reinterpret_cast<PangoAttrInt*>(underline_attr)->value;
98 if (type == PANGO_UNDERLINE_DOUBLE)
99 underline.thick = true;
100 else if (type == PANGO_UNDERLINE_ERROR)
101 underline.color = SK_ColorRED;
103 composition->underlines.push_back(underline);
105 } while (pango_attr_iterator_next(iter));
106 pango_attr_iterator_destroy(iter);
109 // Use a black thin underline by default.
110 if (composition->underlines.empty()) {
111 composition->underlines.push_back(
112 CompositionUnderline(0, length, SK_ColorBLACK, false));
116 } // namespace ui