问题
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