• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ExtractCompositionTextFromGtkPreedit(const gchar * utf8_text,PangoAttrList * attrs,int cursor_position,CompositionText * composition)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);
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(char16_offsets[start],
79                                        char16_offsets[end],
80                                        SK_ColorBLACK,
81                                        false,
82                                        SK_ColorTRANSPARENT);
83 
84         // Always use thick underline for a range with background color, which
85         // is usually the selection range.
86         if (background_attr) {
87           underline.thick = true;
88           // If the cursor is at start or end of this underline, then we treat
89           // it as the selection range as well, but make sure to set the cursor
90           // position to the selection end.
91           if (underline.start_offset == cursor_offset) {
92             composition->selection.set_start(underline.end_offset);
93             composition->selection.set_end(cursor_offset);
94           } else if (underline.end_offset == cursor_offset) {
95             composition->selection.set_start(underline.start_offset);
96             composition->selection.set_end(cursor_offset);
97           }
98         }
99         if (underline_attr) {
100           int type = reinterpret_cast<PangoAttrInt*>(underline_attr)->value;
101           if (type == PANGO_UNDERLINE_DOUBLE)
102             underline.thick = true;
103           else if (type == PANGO_UNDERLINE_ERROR)
104             underline.color = SK_ColorRED;
105         }
106         composition->underlines.push_back(underline);
107       }
108     } while (pango_attr_iterator_next(iter));
109     pango_attr_iterator_destroy(iter);
110   }
111 
112   // Use a black thin underline by default.
113   if (composition->underlines.empty()) {
114     composition->underlines.push_back(CompositionUnderline(
115         0, length, SK_ColorBLACK, false, SK_ColorTRANSPARENT));
116   }
117 }
118 
119 }  // namespace ui
120