Kivy文本输入框适用于阿拉伯文本。

4

我正在尝试使用Kivy的文本输入功能来输入阿拉伯文。我已经设置好了阿拉伯字体并将其用于文本输入,但当我输入阿拉伯文时,出现的字母仅从左到右排列(而且相邻的阿拉伯字母没有连接在一起,这是不正确的)。

有没有什么方法可以让Kivy/文本输入支持RTL语言输入(尤其是阿拉伯文)?

以下是我的代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '500')


logger = logging.getLogger('')

from kivy.uix.textinput import TextInput


class EditorApp(App):
    def build(self):
        f = FloatLayout()
        textinput = TextInput(text='Hello world', font_name='DroidKufi-Regular.ttf')
        # import pdb; pdb.set_trace()

        f.add_widget(textinput)

        return f


if __name__ == '__main__':
    EditorApp().run()

这段代码的结果是:

在这里输入图片描述


这是一个已知的问题,我不确定是否有人直接在处理它,但之前已经讨论过了。你可以在kivy irc频道上询问,或许会更幸运。 - inclement
2个回答

8

不幸的是,Kivy TextInput并不支持从右到左的文本显示,这是一个未解决的问题(checked 29/05/2015)。实际上,Kivy不仅在TextInput中不支持从右到左的文本。

对于像标签这样的静态文本,可以通过使用arabic_reshaperpython-bidi进行hack操作(参考资料):

import arabic_reshaper
from bidi.algorithm import get_display

reshaped_text = arabic_reshaper.reshape(u'اللغة العربية رائعة')
bidi_text = get_display(reshaped_text)

enter image description here

然而,当涉及到带有动态输入的TextInput时,您必须重写大多数类方法以支持RTL,并且最终会像实现整个RTL支持到Kivy一样。

这是一个尝试实现Kivy双向文本支持的开放性努力。另一个已关闭的尝试: 从右到左的标签支持


谢谢分享这些内容。我以前没有遇到过这些。我会深入研究一下,看看能否自己在Kivy中实现RTL..有什么想法或建议吗? - user772401
@user772401 欢迎,开始阅读PR 2411。有人尝试使用pyfribidi或python-bidi来实现此目的。更难但更有效的选择是使用Harfbuzz - Assem
这个解决方案也适用于希伯来语吗?那可能非常有帮助。 - Tal Moshel
@TalMoshel 我认为这个库不支持此功能,但是您可以通过将独立字母替换为其形状替代品来创建其希伯来语版本。 - Assem
@BigOther,我真的不明白你让我做什么?我该如何用她形状的替代字母来替换独立字母? - Tal Moshel

5

我幸运地找到了一个答案并尝试了一下。

链接在这里:https://github.com/hosseinofj/persian_textinput_kivy/blob/master/codes

因为它被删除了,所以我自己来解释一下,尽管发帖的用户也应该得到某种感谢!

无论如何,这是代码:

test.kv

<Ar_text@TextInput>:
    text: "whatever"
    multiline: 0
    size_hint: 1,1
    font_name: "data/unifont-11.0.02.ttf" # the font you want to use
    font_size: 26
    padding_y: [15,0] # can be changed
    padding_x: [self.size[0]-self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached)-10,8]

main.py

'''
App demonstrating a Text input field which accepts Arabic script in kivy

'''


import arabic_reshaper
from bidi.algorithm import get_display
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, NumericProperty, StringProperty


class Ar_text(TextInput):
    max_chars = NumericProperty(20)  # maximum character allowed
    str = StringProperty()

    def __init__(self, **kwargs):
        super(Ar_text, self).__init__(**kwargs)
        self.text = get_display(arabic_reshaper.reshape("اطبع شيئاً"))


    def insert_text(self, substring, from_undo=False):
        if not from_undo and (len(self.text) + len(substring) > self.max_chars):
            return
        self.str = self.str+substring
        self.text = get_display(arabic_reshaper.reshape(self.str))
        substring = ""
        super(Ar_text, self).insert_text(substring, from_undo)

    def do_backspace(self, from_undo=False, mode='bkspc'):
        self.str = self.str[0:len(self.str)-1]
        self.text = get_display(arabic_reshaper.reshape(self.str))


class TestApp(App):

    def build(self):
        return Ar_text()


if __name__ == '__main__':
    TestApp().run()

这个技巧是使用arabic_shaper。因此,在每次更新文本时,您需要使用arabic_shaper提供一个格式化的字符串作为文本输入。
仍然存在一个根本性问题,即真正的RTL不存在(光标始终在字符串末尾的右侧)。
我添加了一个带有示例代码的存储库。它在Ubuntu上运行。如果正确安装了Kivy,它也应该可以在Windows上运行。 https://github.com/eliasaj92/arabic_text_input_kivy

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