python - 使用arabic-reshaper和python-bidi(在多行情况下)时如何修复反向行?
问题描述
当我使用 arabic-reshaper 和 python-bidi 时,我遇到了一个糟糕的结果,即线条从最后一个开始呈现。
解决方案
我做了一个函数来解决这个问题,因为没有办法以另一种方式解决它,bidi 正在反转字符以将第一个放在末尾等等,这是因为阿拉伯语从右到左开始,这样比迪会伪造结果以正确的形状出现,但是当文本必须进入多行时,在最后呈现第一个单词是错误的!所以我必须让它这样做然后我必须将结果反转为反向行,这取决于该行可以包含多少个单词,我通过传递两个参数来计算它,w_w 用于小部件的宽度(或另一个place) 文本将出现的位置和 (f_w) 表示所用字体的字符宽度。
然后在把每一行累加之后,我把行表示倒过来,就这样!这是我制作的功能:
import arabic_reshaper
import bidi.algorithm
def getAR(arWord, w_w=0, f_w=0):
arWord = arWord.strip()
if len(arWord) <= 0: return ''
startList0 = bidi.algorithm.get_display(arabic_reshaper.reshape(arWord))
if (not w_w) or (not f_w):
return startList0
else:
# return startList0
startList = startList0.split(' ')[::-1]
if len(startList) == 0: return ''
if len(startList) == 1: return str(startList[0])
n = floor( w_w / f_w )
for i in startList:
if len(i) > n: return startList0
tempS = ''
resultList = []
for i in range(0, len(startList)):
if (tempS != ''): tempS = ' ' + tempS
if (len(tempS) + (len(startList[i])) > n):
tempS = tempS + "\n"
resultList.append(tempS)
tempS = startList[i]
else:
tempS = startList[i] + tempS
if i == (len(startList)-1):
resultList.append(tempS)
return ''.join(resultList)
你会像这样使用它:
w_w = ... # calculat it yourself, the width of where you will put the text.
f_w = ... # calculat it yourself, the width of the character in the font you are using.
paragraph = "... ..."
widget.text = getAr(paragraph, w_w=w_w, f_w=f_w)
推荐阅读
- c# - MSBuild 以不同方式自动递增构建版本以进行发布/调试
- git - 在 git 存储库中定位函数
- django - Django PostgreSQL 中的索引
- mysql - 检查表中是否存在所有条目
- laravel - Laravel 中的 Nexmo 处理错误
- bash - “不是 git 命令”将 bash 脚本配置为 git 别名
- python - 删除列表列表中“”中的空格
- r - 两个“位置”不同的条形聊天中条形的高度代表什么?
- javascript - 如何从 sequelize findAll 结果修改虚拟字段?
- python - Python - Pandas:如何在呈指数增长的值之间进行插值?