5 # Copyright (c) 2007-2008 Huang Peng <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
25 class PangoAttrList (pango
.AttrList
):
26 def __init__ (self
, attrs
, unistr
):
27 pango
.AttrList
.__init
__ (self
)
33 offsets
.append (offset
)
34 offset
+= len (c
.encode ("utf8"))
35 offsets
.append (offset
)
38 start_index
= attr
._start
_index
if attr
._start
_index
>= 0 else 0
39 end_index
= attr
._end
_index
if attr
._end
_index
>= 0 else 0
40 start_index
= offsets
[start_index
] if start_index
< len (offsets
) else offsets
[-1]
41 end_index
= offsets
[end_index
] if end_index
< len (offsets
) else offsets
[-1]
42 if attr
._type
== ibus
.ATTR_TYPE_FOREGROUND
:
43 r
= (attr
._value
& 0x00ff0000) >> 8
44 g
= (attr
._value
& 0x0000ff00)
45 b
= (attr
._value
& 0x000000ff) << 8
46 pango_attr
= pango
.AttrForeground (r
, g
, b
,
47 start_index
, end_index
)
48 elif attr
._type
== ibus
.ATTR_TYPE_BACKGROUND
:
49 r
= (attr
._value
& 0x00ff0000) >> 8
50 g
= (attr
._value
& 0x0000ff00)
51 b
= (attr
._value
& 0x000000ff) << 8
52 pango_attr
= pango
.AttrBackground (r
, g
, b
,
53 start_index
, end_index
)
54 elif attr
._type
== ibus
.ATTR_TYPE_UNDERLINE
:
55 pango_attr
= pango
.AttrUnderline (int (attr
._value
),
56 start_index
, end_index
)
57 if pango_attr
!= None:
58 self
.insert (pango_attr
)
61 if __name__
== "__main__":