Blame ibus/_gtk.py

Packit 3ff832
# vim:set et sts=4 sw=4:
Packit 3ff832
#
Packit 3ff832
# ibus - The Input Bus
Packit 3ff832
#
Packit 3ff832
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
# Copyright (c) 2007-2010 Red Hat, Inc.
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
__all__ = (
Packit 3ff832
        "PangoAttrList",
Packit 3ff832
    )
Packit 3ff832
Packit 3ff832
import pango
Packit 3ff832
import ibus
Packit 3ff832
Packit 3ff832
class PangoAttrList(pango.AttrList):
Packit 3ff832
    def __init__(self, attrs, unistr):
Packit 3ff832
        super(PangoAttrList, self).__init__()
Packit 3ff832
        if attrs == None:
Packit 3ff832
            return
Packit 3ff832
        offsets = []
Packit 3ff832
        offset = 0
Packit 3ff832
        for c in unistr:
Packit 3ff832
            offsets.append(offset)
Packit 3ff832
            offset += len(c.encode("utf8"))
Packit 3ff832
        offsets.append(offset)
Packit 3ff832
        for attr in attrs:
Packit 3ff832
            pango_attr = None
Packit 3ff832
            start_index = attr.start_index if attr.start_index >= 0 else 0
Packit 3ff832
            end_index = attr.end_index if attr.end_index >= 0 else 0
Packit 3ff832
            start_index = offsets[start_index] if start_index < len(offsets) else offsets[-1]
Packit 3ff832
            end_index = offsets[end_index] if end_index < len(offsets) else offsets[-1]
Packit 3ff832
            if attr.type == ibus.ATTR_TYPE_FOREGROUND:
Packit 3ff832
                r = (attr.value & 0x00ff0000) >> 8
Packit 3ff832
                g = (attr.value & 0x0000ff00)
Packit 3ff832
                b = (attr.value & 0x000000ff) << 8
Packit 3ff832
                pango_attr = pango.AttrForeground(r, g, b,
Packit 3ff832
                    start_index, end_index)
Packit 3ff832
            elif attr.type == ibus.ATTR_TYPE_BACKGROUND:
Packit 3ff832
                r = (attr.value & 0x00ff0000) >> 8
Packit 3ff832
                g = (attr.value & 0x0000ff00)
Packit 3ff832
                b = (attr.value & 0x000000ff) << 8
Packit 3ff832
                pango_attr = pango.AttrBackground(r, g, b,
Packit 3ff832
                    start_index, end_index)
Packit 3ff832
            elif attr.type == ibus.ATTR_TYPE_UNDERLINE:
Packit 3ff832
                pango_attr = pango.AttrUnderline(int(attr.value),
Packit 3ff832
                    start_index, end_index)
Packit 3ff832
            if pango_attr != None:
Packit 3ff832
                self.insert(pango_attr)
Packit 3ff832