taher
taher

Reputation: 159

python kivy textinput don't support persian

i want create textinput with kivy that support persian and arabic text and use bidi module and reshaper but doesn't work

class Fa_text(TextInput):
    max_chars = NumericProperty(20) # maximum character allowed
    str = StringProperty()
    def __init__(self, **kwargs):
        super(Fa_text, self).__init__(**kwargs)


    def insert_text(self, substring, from_undo=False):

        cc, cr = self.cursor
        sci = self.cursor_index
        ci = sci()
        text = self._lines[cr]
   
        new_text = self.encode(text[:cc] + substring + text[cc:])
        self.text = new_text
        self._set_line_text(cr, new_text)


    def encode(self,text):
        self.base_direction='rtl'
        self.markup=True
        self.font_name='arial'
        reshaped_text = arabic_reshaper.reshape(text) 
        return bidi.algorithm.get_display(reshaped_text)

Upvotes: 0

Views: 225

Answers (1)

Arapa
Arapa

Reputation: 11

that is possible just with a trick ;) You can use two TextInputs, one, for input you wish to write Persian/Arabic in, and another, for draft. You can type your text in draft, get the text with on_text event, transform it with reshaper, and finally set text of main input to that transformed word! Ofcourse you don't want to leave that draft visible to user, don't worry! we will gonna hide it =). One way is you can use RelativeLayout as parent and place main input and then draft input in it, and easily set opacity of latter to 0 and it works! Here is the sample:

RelativeLayout:
    TextInput:
        id: main_input
        font_name: "<some Persian/Arabic font>"
        font_size: "15sp"
        halign: "right"

    TextInput:
        opacity: 0
        font_name: "<some Persian/Arabic font>"
        font_size: "15sp"
        halign: "right"
        on_text: root.ids["main_input"].text = get_display(reshaper.reshape(self.text))

Upvotes: 1

Related Questions