1 /* vim:set et sts=4 sw=4:
5 * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or(at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
23 Pango.AttrList get_pango_attr_list_from_ibus_text(IBus.Text text) {
24 Pango.AttrList pango_attrs = new Pango.AttrList();
25 unowned IBus.AttrList attrs = text.get_attributes();
29 unowned string str = text.get_text();
30 long nchars = str.char_count();
31 long[] offsets = new long[nchars + 1];
32 for (int i = 0; i <= nchars; i++)
33 offsets[i] = str.index_of_nth_char(i);
38 attr = attrs.get(i++);
41 long start_index = attr.start_index;
42 if (start_index <= 0) start_index = 0;
43 start_index = start_index <= nchars ? offsets[start_index] : offsets[-1];
45 long end_index = attr.end_index;
46 if (end_index <= 0) end_index = 0;
47 end_index = end_index <= nchars ? offsets[end_index] : offsets[-1];
49 Pango.Attribute pango_attr = null;
51 case IBus.AttrType.FOREGROUND:
53 uint16 r = (uint16)(attr.value & 0x00ff0000) >> 8;
54 uint16 g = (uint16)(attr.value & 0x0000ff00);
55 uint16 b = (uint16)(attr.value & 0x000000ff) << 8;
56 pango_attr = Pango.attr_foreground_new(r, g, b);
59 case IBus.AttrType.BACKGROUND:
61 uint16 r = (uint16)(attr.value & 0x00ff0000) >> 8;
62 uint16 g = (uint16)(attr.value & 0x0000ff00);
63 uint16 b = (uint16)(attr.value & 0x000000ff) << 8;
64 pango_attr = Pango.attr_background_new(r, g, b);
67 case IBus.AttrType.UNDERLINE:
69 pango_attr = Pango.attr_underline_new((Pango.Underline)attr.value);
75 pango_attr.start_index = (uint)start_index;
76 pango_attr.end_index = (uint)end_index;
77 // Transfer the ownership to pango_attrs
78 pango_attrs.insert((owned)pango_attr);