在 kivy 中使用自定义字体

Using Custom font in kivy

我正在尝试创建一个简单的 kivy 软件,我需要为 button.text 使用自定义阿拉伯语字体。

我已将自定义字体包含在文件中并进行了尝试,但这就是它的样子:

我不确定这是编码问题还是我需要在 Kivy 中使用不同的东西

这是我的代码:

main.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window


class MyGrid(Widget):
    pass

class BackOffice(App):
    def build(self):
        return MyGrid()


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

backoffice.kv


#:import utils kivy.utils


<MyGrid>
    canvas.before:
        Color:
            rgba: utils.get_color_from_hex('#a1d0f4')
        Rectangle:
            pos: self.pos
            size: self.size

    GridLayout:
        cols: 1
        size: root.width, root.height
        Label:
            text: 'logo here'
        GridLayout:
            cols: 2
            Label:
                text: ' vision logo goes here'
            GridLayout:
                cols:1
                Button:
                    size: 700, 120
                    size_hint: None, None # <---
                    background_color: utils.get_color_from_hex('#0a74c4')
                    font_name: 'fonts/Shoroq-Font.ttf'
                    text: "إعدادت المستخدمين"
                    Image:
                        source: 'images/conference-256.png'
                        y: self.parent.y + 5
                        x: self.parent.x + 70



                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
        GridLayout:
            cols: 3
            Button:
                text: 'button'
                background_color: utils.get_color_from_hex('#ff0000')
            Button:
                text: 'button'
                background_color: utils.get_color_from_hex('#0a74c4')
            Label:
                text: 'some text here'

*更新* 我也尝试使用基于 的 Arabic reshaper,但它仍然给出相同的结果!因为问题是关于 Textinput,而不是一般的文本查看。

使用的解决方案arabic.reshaper and bidi.algorithm

例子

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import StringProperty
import arabic_reshaper
from bidi.algorithm import get_display

Builder.load_file("main.kv")


class MyGrid(Widget):
    bidi_text = StringProperty('')

    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        reshaped_text = arabic_reshaper.reshape(u"إعدادت المستخدمين")
        self.bidi_text = get_display(reshaped_text)


class BackOffice(App):
    def build(self):
        return MyGrid()


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

main.kv

#:import utils kivy.utils


<MyGrid>:
    canvas.before:
        Color:
            rgba: utils.get_color_from_hex('#a1d0f4')
        Rectangle:
            pos: self.pos
            size: self.size

    GridLayout:
        cols: 1
        size: root.width, root.height
        Label:
            text: 'logo here'
        GridLayout:
            cols: 2
            Label:
                text: ' vision logo goes here'
            GridLayout:
                cols:1
                Button:
                    size: 700, 120
                    size_hint: None, None # <---
                    background_color: utils.get_color_from_hex('#0a74c4')
                    font_name: 'fonts/Shoroq-Font.ttf'
                    text: root.bidi_text
                    font_size: sp(20)

                    Image:
                        source: 'images/conference-256.png'
                        y: self.parent.y + 5
                        x: self.parent.x + 70



                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
                Button:
                    text:'button'
                    background_color: utils.get_color_from_hex('#0a74c4')
        GridLayout:
            cols: 3
            Button:
                text: 'button'
                background_color: utils.get_color_from_hex('#ff0000')
            Button:
                text: 'button'
                background_color: utils.get_color_from_hex('#0a74c4')
            Label:
                text: 'some text here'

输出