当我使用 arabic-reshaper 和 python-bidi 时,我遇到了一个糟糕的结果,这些行从最后一行开始呈现。
我做了一个函数来解决这个问题,因为没有办法用另一种方式解决它,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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)