Blame ui/gtk3/pango.vala

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