使用 arabic-reshaper 和 python-bidi(在多行情况下)时如何修复反向行? [英] how to fix the reversed Lines when using arabic-reshaper and python-bidi (in multiple lines situation)?

查看:25
本文介绍了使用 arabic-reshaper 和 python-bidi(在多行情况下)时如何修复反向行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 arabic-reshaper 和 python-bidi 时,我遇到了一个糟糕的结果,即线条从最后一个开始出现.

When I am using arabic-reshaper and python-bidi I faced a bad result that the lines start presenting from the last one.

推荐答案

我做了一个函数来解决这个问题,因为没有其他方法可以解决它,bidi 是将字符倒置将第一个放在最后依此类推,那是因为阿拉伯语是从右到左开始的,那样比迪会伪造结果以正确的形状出现,但是当文本必须进入多行时,会出现错误的第一个单词在末尾!所以我必须让它这样做然后我这次必须将结果反转为反转行,具体取决于该行可以包含多少个单词,我正在通过传递两个参数 w_w 来计算小部件的宽度(或另一个place) 文本将出现的位置和 (f_w) 表示所用字体的字符宽度.

I made a function to round on this problem since there is no way to solve it in another way, bidi is reversing the characters to put the first at the end and so on, that's because the Arabic language starts from right to left and that way bidi will fake the result to appear in the right shape, but whene the text has to go into more than one line that will be wrong to present the first word at the end! so I have to let it do that then I have to reverse the result as reversed lines this time depending on how many words the line could contain, I am calculating that through passing two arguments, w_w for the width of the widget (or the other place) where the text will appear and (f_w) which means the character width of the used font.

然后在累积每一行之后,我将行呈现反转,就是这样!这是我制作的功能:

Then after cumulating each line, I reverse the line presentation, that's it! and here is the function I made:

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)

这篇关于使用 arabic-reshaper 和 python-bidi(在多行情况下)时如何修复反向行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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