Cleanup the vala code of gtk3 panel and tool by:
[platform/upstream/ibus.git] / ui / gtk3 / pango.vala
1 /* vim:set et sts=4 sw=4:
2  *
3  * ibus - The Input Bus
4  *
5  * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
6  *
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.
11  *
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.
16  *
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
21  */
22
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();
26     if (attrs == null)
27         return pango_attrs;
28
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);
34
35     IBus.Attribute attr;
36     int i = 0;
37     while(true) {
38         attr = attrs.get(i++);
39         if (attr == null)
40             break;
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];
44
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];
48
49         Pango.Attribute pango_attr = null;
50         switch(attr.type) {
51         case IBus.AttrType.FOREGROUND:
52             {
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);
57                 break;
58             }
59         case IBus.AttrType.BACKGROUND:
60             {
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);
65                 break;
66             }
67         case IBus.AttrType.UNDERLINE:
68             {
69                 pango_attr = Pango.attr_underline_new((Pango.Underline)attr.value);
70                 break;
71             }
72         default:
73             continue;
74         }
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);
79     }
80     return pango_attrs;
81 }