How to fix Arabic/Persian text and font in pygame?

我是研究僧i 提交于 2021-02-10 12:44:54

问题


I have this piece of code right here:

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()

What I expect: سلام
What I get: س‌ل‎ا‌م
How to fix it? (I don't care if I need to use another library to fix it, But I must use pygame for rest of code)
I also tried it on both pygame 1.9.6 and 2.0.0


回答1:


You have to use arabic_reshaper library.

pip install arabic-reshaper

Refer to this https://github.com/mpcabd/python-arabic-reshaper

You will need python-bidi as well

pip install python-bidi

You can do it as follow


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()


OUTPUT



来源:https://stackoverflow.com/questions/65218972/how-to-fix-arabic-persian-text-and-font-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!