Blame bindings/vala/pango.vala

Packit 3ff832
/* vim:set et sts=4 sw=4:
Packit 3ff832
 *
Packit 3ff832
 * ibus - The Input Bus
Packit 3ff832
 *
Packit 3ff832
 * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
 *
Packit 3ff832
 * This library is free software; you can redistribute it and/or
Packit 3ff832
 * modify it under the terms of the GNU Lesser General Public
Packit 3ff832
 * License as published by the Free Software Foundation; either
Packit 3ff832
 * version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
 *
Packit 3ff832
 * This library is distributed in the hope that it will be useful,
Packit 3ff832
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
 * Lesser General Public License for more details.
Packit 3ff832
 *
Packit 3ff832
 * You should have received a copy of the GNU Lesser General Public
Packit 3ff832
 * License along with this library; if not, write to the Free Software
Packit 3ff832
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
 * USA
Packit 3ff832
 */
Packit 3ff832
Packit 3ff832
Pango.AttrList get_pango_attr_list_from_ibus_text(IBus.Text text) {
Packit 3ff832
    Pango.AttrList pango_attrs = new Pango.AttrList();
Packit 3ff832
    unowned IBus.AttrList attrs = text.get_attributes();
Packit 3ff832
    if (attrs == null)
Packit 3ff832
        return pango_attrs;
Packit 3ff832
Packit 3ff832
    unowned string str = text.get_text();
Packit 3ff832
    long nchars = str.char_count();
Packit 3ff832
    long[] offsets = new long[nchars + 1];
Packit 3ff832
    for (int i = 0; i <= nchars; i++)
Packit 3ff832
        offsets[i] = str.index_of_nth_char(i);
Packit 3ff832
Packit 3ff832
    IBus.Attribute attr;
Packit 3ff832
    int i = 0;
Packit 3ff832
    while(true) {
Packit 3ff832
        attr = attrs.get(i++);
Packit 3ff832
        if (attr == null)
Packit 3ff832
            break;
Packit 3ff832
        long start_index =  attr.start_index;
Packit 3ff832
        if (start_index <= 0) start_index = 0;
Packit 3ff832
        start_index = start_index <= nchars ? offsets[start_index] : offsets[-1];
Packit 3ff832
Packit 3ff832
        long end_index = attr.end_index;
Packit 3ff832
        if (end_index <= 0) end_index = 0;
Packit 3ff832
        end_index = end_index <= nchars ? offsets[end_index] : offsets[-1];
Packit 3ff832
Packit 3ff832
        Pango.Attribute pango_attr = null;
Packit 3ff832
        switch(attr.type) {
Packit 3ff832
        case IBus.AttrType.FOREGROUND:
Packit 3ff832
            {
Packit 3ff832
                uint16 r = (uint16)((attr.value & 0x00ff0000) >> 8);
Packit 3ff832
                uint16 g = (uint16)(attr.value & 0x0000ff00);
Packit 3ff832
                uint16 b = (uint16)((attr.value & 0x000000ff) << 8);
Packit 3ff832
                pango_attr = Pango.attr_foreground_new(r, g, b);
Packit 3ff832
                break;
Packit 3ff832
            }
Packit 3ff832
        case IBus.AttrType.BACKGROUND:
Packit 3ff832
            {
Packit 3ff832
                uint16 r = (uint16)((attr.value & 0x00ff0000) >> 8);
Packit 3ff832
                uint16 g = (uint16)(attr.value & 0x0000ff00);
Packit 3ff832
                uint16 b = (uint16)((attr.value & 0x000000ff) << 8);
Packit 3ff832
                pango_attr = Pango.attr_background_new(r, g, b);
Packit 3ff832
                break;
Packit 3ff832
            }
Packit 3ff832
        case IBus.AttrType.UNDERLINE:
Packit 3ff832
            {
Packit 3ff832
                pango_attr = Pango.attr_underline_new((Pango.Underline)attr.value);
Packit 3ff832
                break;
Packit 3ff832
            }
Packit 3ff832
        default:
Packit 3ff832
            continue;
Packit 3ff832
        }
Packit 3ff832
        pango_attr.start_index = (uint)start_index;
Packit 3ff832
        pango_attr.end_index = (uint)end_index;
Packit 3ff832
        // Transfer the ownership to pango_attrs
Packit 3ff832
        pango_attrs.insert((owned)pango_attr);
Packit 3ff832
    }
Packit 3ff832
    return pango_attrs;
Packit 3ff832
}