如何在 pygame 中修复阿拉伯语/波斯语文本和字体? [英] How to fix Arabic/Persian text and font in pygame?

查看:50
本文介绍了如何在 pygame 中修复阿拉伯语/波斯语文本和字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这段代码:

导入pygamepygame.display.init()pygame.font.init()赢 = 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()对于范围内的 i (5):pygame.event.clear()pygame.time.delay(1000)pygame.quit()

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

解决方案

你必须使用 arabic_reshaper 库.

pip install arabic-reshaper

参考这个

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

解决方案

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

这篇关于如何在 pygame 中修复阿拉伯语/波斯语文本和字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆