- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / ime / composition_text_util_pango.cc
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.
4
5 #include "ui/base/ime/composition_text_util_pango.h"
6
7 #include <pango/pango-attributes.h>
8
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"
14
15 namespace ui {
16
17 void ExtractCompositionTextFromGtkPreedit(const gchar* utf8_text,
18                                           PangoAttrList* attrs,
19                                           int cursor_position,
20                                           CompositionText* composition) {
21   composition->Clear();
22   composition->text = UTF8ToUTF16(utf8_text);
23
24   if (composition->text.empty())
25     return;
26
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());
36
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);
41
42   size_t cursor_offset =
43       char16_offsets[std::max(0, std::min(char_length, cursor_position))];
44
45   composition->selection = gfx::Range(cursor_offset);
46
47   if (attrs) {
48     int utf8_length = strlen(utf8_text);
49     PangoAttrIterator* iter = pango_attr_list_get_iterator(attrs);
50
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);
56
57       start = std::min(start, utf8_length);
58       end = std::min(end, utf8_length);
59       if (start >= end)
60         continue;
61
62       start = g_utf8_pointer_to_offset(utf8_text, utf8_text + start);
63       end = g_utf8_pointer_to_offset(utf8_text, utf8_text + end);
64
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;
70
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);
75
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);
80
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);
94           }
95         }
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;
102         }
103         composition->underlines.push_back(underline);
104       }
105     } while (pango_attr_iterator_next(iter));
106     pango_attr_iterator_destroy(iter);
107   }
108
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));
113   }
114 }
115
116 }  // namespace ui