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