将pango标记字符串转换为GtkTextTag属性

时间:2012-03-18 07:59:59

标签: python gtk pygtk pango

我有gtk.TextView我希望添加类似标记的文字。我知道这可以通过使用gtk.TextTag来实现,你可以用pango标记字符串创建类似的属性。我注意到没有简单的方法可以将set_markup设置为gtk.TextBuffer,就像使用多个其他小部件一样。相反,你必须创建一个TextTag,给它属性,然后将它插入到TextBuffer的TagTable中,指定标签适用的iters。

理想情况下,我想创建一个可以将pango标记字符串转换为TextTag以获得相同效果的函数。但是gtk似乎没有内置的功能。 我注意到您可以在标记的字符串上使用pango.parse_markup(),它将创建一个pango.AttributeList,其中包含有关字符串上设置的属性及其出现的索引的信息。但是每种类型的属性都存在细微差别,因此很难对每种情况进行概括。有没有更好的方法来解决这个问题?或者pango标记是不是要转换为gtk.TextTag'

2 个答案:

答案 0 :(得分:2)

我终于找到了解决这个问题的方法。我创建了一个解析标记字符串的函数(使用pango.parse_markup)。通过阅读文档和python内省,我能够找出如何使用pango.Attribute并将其转换为GtkTextTag可以使用的属性。

这是功能:

def parse_markup_string(string):
    '''
    Parses the string and returns a MarkupProps instance
    '''
    #The 'value' of an attribute...for some reason the same attribute is called several different things...
    attr_values = ('value', 'ink_rect', 'logical_rect', 'desc', 'color')

    #Get the AttributeList and text
    attr_list, text, accel = pango.parse_markup( string )
    attr_iter = attr_list.get_iterator()

    #Create the converter
    props = MarkupProps()
    props.text = text

    val = True
    while val:
            attrs = attr_iter.get_attrs()

            for attr in attrs:
                    name = attr.type
                    start = attr.start_index
                    end = attr.end_index
                    name = pango.AttrType(name).value_nick

                    value = None
                    #Figure out which 'value' attribute to use...there's only one per pango.Attribute
                    for attr_value in attr_values:
                            if hasattr( attr, attr_value ):
                                    value = getattr( attr, attr_value )
                                    break

                    #There are some irregularities...'font_desc' of the pango.Attribute
                    #should be mapped to the 'font' property of a GtkTextTag
                    if name == 'font_desc':
                            name = 'font'
                    props.add( name, value, start, end )

            val = attr_iter.next()

    return props

此函数创建一个MarkupProps()对象,该对象能够生成GtkTextTag s以及文本中的索引以应用它们。

这是对象:

class MarkupProps():
'''
Stores properties that contain indices and appropriate values for that property.
Includes an iterator that generates GtkTextTags with the start and end indices to 
apply them to
'''
def __init__(self): 
    '''
    properties = (  {   
                        'properties': {'foreground': 'green', 'background': 'red'}
                        'start': 0,
                        'end': 3
                    },
                    {
                        'properties': {'font': 'Lucida Sans 10'},
                        'start': 1,
                        'end':2,

                    },
                )
    '''
    self.properties = []#Sequence containing all the properties, and values, organized by like start and end indices
    self.text = ""#The raw text without any markup

def add( self, label, value, start, end ):
    '''
    Add a property to MarkupProps. If the start and end indices are already in
    a property dictionary, then add the property:value entry into
    that property, otherwise create a new one
    '''
    for prop in self.properties:
        if prop['start'] == start and prop['end'] == end:
            prop['properties'].update({label:value})
    else:
        new_prop =   {
                        'properties': {label:value},
                        'start': start,
                        'end':end,
                    }
        self.properties.append( new_prop )

def __iter__(self):
    '''
    Creates a GtkTextTag for each dict of properties
    Yields (TextTag, start, end)
    '''
    for prop in self.properties:
        tag = gtk.TextTag()
        tag.set_properties( **prop['properties'] )
        yield (tag, prop['start'], prop['end'])

所以使用这个函数和MarkupProps对象,我能够给出一个pango标记字符串,将字符串分解为它的属性和文本形式,然后将其转换为GtkTextTag s。

答案 1 :(得分:1)

没有关注GTK +开发,也许他们最近添加了一些东西,但是看到这些错误:#59390#505478。由于它们没有关闭,可能什么也没做。