gpt4 book ai didi

python - 如何修复pygame中的阿拉伯语/波斯语文本和字体?

转载 作者:行者123 更新时间:2023-12-04 01:09:47 24 4
gpt4 key购买 nike

我这里有这段代码:

import pygame
pygame.display.init()
pygame.font.init()

win = pygame.display.set_mode((100, 100))
font = pygame.font.Font('./arial.ttf', 10)
text = font.render('سلام', True, (255, 255, 255))

win.fill((0, 0, 0))
win.blit(text, (0, 0))
pygame.display.update()

for i in range(5):
pygame.event.clear()
pygame.time.delay(1000)

pygame.quit()

我期望的是:سلام
我得到的:如何解决? (我不在乎是否需要使用另一个库来修复它,但我必须使用 pygame 来处理其余代码)
我还在 pygame 1.9.6 和 2.0.0 上都试过了

最佳答案

你必须使用 arabic_reshaper 库。

pip install arabic-reshaper

引用这个https://github.com/mpcabd/python-arabic-reshaper

你还需要 python-bidi

pip install python-bidi

你可以按照下面的方式做


import pygame
import arabic_reshaper
from bidi.algorithm import get_display
pygame.display.init()
pygame.font.init()

win = pygame.display.set_mode((100, 100))
font = pygame.font.Font('arial.ttf', 10)
text_to_be_reshaped = 'اللغة العربية رائعة'
reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)
bidi_text = get_display(reshaped_text)
print(reshaped_text)
text = font.render(bidi_text, True, (255, 255, 255))



win.fill((0, 0, 0))
win.blit(text, (0, 0))
pygame.display.update()

for i in range(5):
pygame.event.clear()
pygame.time.delay(1000)

pygame.quit()


输出

enter image description here

关于python - 如何修复pygame中的阿拉伯语/波斯语文本和字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65218972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com