Gtk3 TextBuffer.serialize() 返回带有格式标签的文本,即使在视觉上没有任何标签。

10

我在我的项目中使用了一个 Gtk TextView/TextBuffer,用户可以通过选择正确的切换按钮来输入富文本(加粗/斜体/下划线)。

问题是,如果我将下划线或斜体Pango标志应用于TextView中的文本,然后关闭斜体/下划线并键入更多内容,然后通过TextBuffer.serialize()获取带有这些标志的文本,则返回的未格式化文本(在TextView中可见未格式化)周围带有下划线/斜体标签。

您可以在此处查看此内容:(请注意,为了易读性,我使用BeautifulSoup简化了标签,但实际位置/类型未进行任何编辑。)

以下是代码(需要安装Python3的Gtk3和BS4):

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Pango

import smtplib, mimetypes
from bs4 import BeautifulSoup

class Handler():

    def __init__(self):
        global html
        self.state = 0

    def onDeleteWindow(self, *args):
        Gtk.main_quit(*args)

    def onSendClicked(self, button):
        start, end = textBodyBuffer.get_bounds()
        self.content = textBodyBuffer.get_text(start, end, True)

        # Below is the serialization code for exporting with format tags
        format = textBodyBuffer.register_serialize_tagset() 
        exported = textBodyBuffer.serialize(textBodyBuffer, format, start, end)

        exported = exported.decode("latin-1")

        exported = exported.split('<text_view_markup>', 1)
        del exported[0]
        exported[0] = '<text_view_markup>' + str(exported[0])

        exported = exported[0].split('</tags>', 1)
        del exported[0]

        exported = exported[0].split('</text_view_markup>', 1)
        exported = str(exported[0]).replace('\n', ' ')

        soup = BeautifulSoup(exported)

        soupTags = soup.find_all('apply_tag')

        for tag in soupTags:

            if tag['name'] == 'bold':
                tag.name = 'b'
                del tag['name']
            elif tag['name'] == 'italic':
                tag.name = 'em'
                del tag['name']
            elif tag['name'] == 'underline':
                tag.name = 'u'
                del tag['name']

        print (soup)

    def bold(self, button):
        global tags_on
        name = button.get_name()
        if button.get_active():             # Button is "down"/enabled
            tags_on.append('bold')
        elif button.get_active() != True:   # Button is "up"/disabled
            del tags_on[tags_on.index('bold')]

    def italic(self, button):
        global tags_on
        name = button.get_name()
        if button.get_active():             # Button is "down"/enabled
            tags_on.append('italic')
        elif button.get_active() != True:   # Button is "up"/disabled
            del tags_on[tags_on.index('italic')]

    def underline(self, button):
        global tags_on
        name = button.get_name()
        if button.get_active():             # Button is "down"/enabled
            tags_on.append('underline')
        elif button.get_active() != True:   # Button is "up"/disabled
            del tags_on[tags_on.index('underline')]

    def alignToggled(self, radiobutton):
        pass

    def undo(self, button):
        pass

    def redo(self, button):
        pass

    def keyHandler(self, widget, event):
        global html
        if Gdk.ModifierType.CONTROL_MASK & event.state:
            if Gdk.keyval_name(event.keyval) == 'q':    # Quit the program
                w.destroy()
                Gtk.main_quit()

def get_iter_position(buffer):
    return buffer.get_iter_at_mark(buffer.get_insert())

def text_inserted(buffer, iter, char, length):

    global tags_on

    if len(tags_on) >= 0:
        iter.backward_chars(length)

        for tag in tags_on:
            w.queue_draw()
            if tag == 'bold':
                buffer.apply_tag(tag_bold, get_iter_position(buffer), iter)
            elif tag == 'italic':
                buffer.apply_tag(tag_italic, get_iter_position(buffer), iter)
            elif tag == 'underline':
                buffer.apply_tag(tag_underline, get_iter_position(buffer), iter)


if __name__ == '__main__':

    global text, html
    # Gtk tag globals
    global tag_bold, tag_italic, tag_underline, tags_on

    tags_on = []

    text = ''
    html = '<html><body><p>'

    builder = Gtk.Builder()
    builder.add_from_file('editor.glade')
    builder.connect_signals(Handler())

    buttonSend = builder.get_object('buttonSend')

    textBody = builder.get_object('textviewBody')
    textBodyBuffer = textBody.get_buffer()

    textBodyBuffer.connect_after('insert-text', text_inserted)
    tag_bold = textBodyBuffer.create_tag("bold", weight=Pango.Weight.BOLD)
    tag_italic = textBodyBuffer.create_tag("italic", style=Pango.Style.ITALIC)
    tag_underline = textBodyBuffer.create_tag("underline", underline=Pango.Underline.SINGLE)

    w = builder.get_object('window1')
    w.show_all()

    Gtk.main()

这里是 editor.glade 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkImage" id="image1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="icon_name">mail-send</property>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Keyboard Mail - Edit Message</property>
    <property name="modal">True</property>
    <property name="type_hint">dialog</property>
    <signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
    <signal name="key-press-event" handler="keyHandler" swapped="no"/>
    <child>
      <object class="GtkBox" id="box2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkBox" id="box1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkButton" id="buttonSend">
                <property name="label" translatable="yes">Send</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="image">image1</property>
                <property name="relief">none</property>
                <property name="image_position">top</property>
                <property name="always_show_image">True</property>
                <signal name="clicked" handler="onSendClicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkToolbar" id="toolbar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="toolbar_style">icons</property>
            <property name="show_arrow">False</property>
            <child>
              <object class="GtkToggleToolButton" id="buttonBold">
                <property name="name">bold</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="tooltip_text" translatable="yes">Make the selected text bold</property>
                <property name="icon_name">format-text-bold</property>
                <signal name="toggled" handler="bold" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToggleToolButton" id="buttonItalic">
                <property name="name">italic</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="icon_name">format-text-italic</property>
                <signal name="toggled" handler="italic" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
            <child>
              <object class="GtkToggleToolButton" id="buttonUnderline">
                <property name="name">underline</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="icon_name">format-text-underline</property>
                <signal name="toggled" handler="underline" swapped="no"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">3</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box5">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkTextView" id="textviewBody">
                <property name="width_request">500</property>
                <property name="height_request">100</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="hexpand">True</property>
                <property name="vexpand">True</property>
                <property name="wrap_mode">word</property>
                <property name="left_margin">5</property>
                <property name="right_margin">5</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">3</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
有人知道为什么TextBuffer.serialize()语句(第23行)会返回下划线/斜体标签内的非格式化字符吗?
我似乎找不到任何模式,它似乎随机决定是否返回带有标签的文本。
编辑:我已经使用pdb(Python调试器)逐步检查了代码,仍然没有看到会引起这种情况的任何东西。
编辑:我发现了一个有趣的模式 - 如果同时打开斜体和下划线,然后输入一些内容,然后同时关闭它们,然后再输入一些内容,则serialize()调用将返回正确的字符串。
但是,如果您逐个应用它们,每次新标签都键入一些内容,那么返回的结果将不正确。

@ace 嗯,很高兴情况有所改善。可惜你还没找出问题所在 :( - RPiAwesomeness
我怀疑这是GTK中的一个bug而不是故意的。 - Phillip
我检查了一下,看看是否可以使用register_serialize_format作为解决方法,但是似乎也有问题。回调函数必须返回一个guint8*数组,但在Python中却变成了“必须返回单个字符” - 但如果回调函数这样做,整个程序就会崩溃。我建议你向上游报告此问题 - Phillip
@Phillip 像这样吗?我尝试提交了一个错误报告 - 不确定我做得有多好。 - RPiAwesomeness
2
是的。我补充了一个最小示例,并将其作为答案发布了摘要。(如果错误报告在一段时间内仍然没有回复,请不要失望 - 根据我的经验,GTK开发人员可能会非常缓慢地回应) - Phillip
显示剩余6条评论
1个回答

4

我已经能够用一个C语言的最小示例重现这个问题。因此,我强烈怀疑这是GTK的一个bug。提问者因此向上游报告了一个bug

作为解决方法,我建议您尝试自己实现序列化。 forward_to_tag_toggle 应该对此有帮助。请注意,您不能使用明显的路径来使用 register_serialize_format 注册自己的序列化程序,然后调用序列化,因为虽然 GtkTextBufferSerializeFunc 应该返回一个字符串,在 GObject 存储库中 它显然被记录为返回单个整数的函数。(由于另一个 bug。)相反,完全从您的代码中进行序列化,即获取起始迭代器,然后遍历文本缓冲区。

啊,那很可能不是我的编程问题 - 很好。除了 Pango 标志和变量名称的明显差异之外,我无法看到我的粗体代码与我的斜体或下划线代码之间有任何不同。感谢您制作这个示例的麻烦 - 我会自己尝试迭代。 - RPiAwesomeness
此外,forward_to_tag_toggle对我来说是无用的,因为它是PyGtk而不是正确的Gtk 3。在Gtk3文档中没有关于forward_to_tag_toggle的任何内容:( - RPiAwesomeness
1
当然有! - Phillip
哦...我看的是Gtk3文档中的TextBuffer,而不是TextIter >.< - RPiAwesomeness
我想我会给你赏金——毕竟没有意义让它白白浪费,而且你确实帮助了我到目前为止。还在等GNOME开发者的回复:( - RPiAwesomeness

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接