Kivy Text Input for Arabic Text
Question:
I’m trying to use Kivy’s text input for Arabic text. I have an Arabic font set up with my text input but when I type into the input (in Arabic) I just get Arabic letters appearing from left to right (and they’re not connected as Arabic letters should be when they’re adjacent to each other).
Is there a way to get Kivy/text input to support RTL languages input that I’m missing (esp Arabic).
Here’s my code,
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '500')
logger = logging.getLogger('')
from kivy.uix.textinput import TextInput
class EditorApp(App):
def build(self):
f = FloatLayout()
textinput = TextInput(text='Hello world', font_name='DroidKufi-Regular.ttf')
# import pdb; pdb.set_trace()
f.add_widget(textinput)
return f
if __name__ == '__main__':
EditorApp().run()
The result of this code:
Answers:
Unfortunately, Kivy TextInput support for right-to-left is an open issue (checked 29/05/2015). Actually, Kivy is not supporting right-to-left not only to TextInput.
For static texts like labels , there is a hack by using arabic_reshaper and python-bidi (reference):
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(u'اللغة العربية رائعة')
bidi_text = get_display(reshaped_text)
Yet, as for TextInput
with a dynamic input, you had to override most of class methods to support RTL and you will end up like implementing the whole RTL support to kivy.
Here is an open attempt to implement Kivy bidi support. Another closed one: Right-to-left labels support.
So there was an answer that I luckily was able to get through and try out.
the links is here: https://github.com/hosseinofj/persian_textinput_kivy/blob/master/codes
Since it got deleted for not explaining, and not posting the code here, I’ll do so myself, albeit the user who posted this link should be thanked somehow too!
Anyway, here is the code:
test.kv
<Ar_text@TextInput>:
text: "whatever"
multiline: 0
size_hint: 1,1
font_name: "data/unifont-11.0.02.ttf" # the font you want to use
font_size: 26
padding_y: [15,0] # can be changed
padding_x: [self.size[0]-self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached)-10,8]
main.py
'''
App demonstrating a Text input field which accepts Arabic script in kivy
'''
import arabic_reshaper
from bidi.algorithm import get_display
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, NumericProperty, StringProperty
class Ar_text(TextInput):
max_chars = NumericProperty(20) # maximum character allowed
str = StringProperty()
def __init__(self, **kwargs):
super(Ar_text, self).__init__(**kwargs)
self.text = get_display(arabic_reshaper.reshape("اطبع شيئاً"))
def insert_text(self, substring, from_undo=False):
if not from_undo and (len(self.text) + len(substring) > self.max_chars):
return
self.str = self.str+substring
self.text = get_display(arabic_reshaper.reshape(self.str))
substring = ""
super(Ar_text, self).insert_text(substring, from_undo)
def do_backspace(self, from_undo=False, mode='bkspc'):
self.str = self.str[0:len(self.str)-1]
self.text = get_display(arabic_reshaper.reshape(self.str))
class TestApp(App):
def build(self):
return Ar_text()
if __name__ == '__main__':
TestApp().run()
The trick with this is to use arabic_shaper
So at every update of the text, you provide the text input with a formatted string using the arabic_shaper
.
There is still the fundamental issue that true RTL isn’t there(the cursor is always to the right at the end of a string)
I added a repo with the sample code. It ran on Ubuntu. It should run on windows too if Kivy is properly installed
I’m trying to use Kivy’s text input for Arabic text. I have an Arabic font set up with my text input but when I type into the input (in Arabic) I just get Arabic letters appearing from left to right (and they’re not connected as Arabic letters should be when they’re adjacent to each other).
Is there a way to get Kivy/text input to support RTL languages input that I’m missing (esp Arabic).
Here’s my code,
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Config.set('graphics', 'width', '300')
Config.set('graphics', 'height', '500')
logger = logging.getLogger('')
from kivy.uix.textinput import TextInput
class EditorApp(App):
def build(self):
f = FloatLayout()
textinput = TextInput(text='Hello world', font_name='DroidKufi-Regular.ttf')
# import pdb; pdb.set_trace()
f.add_widget(textinput)
return f
if __name__ == '__main__':
EditorApp().run()
The result of this code:
Unfortunately, Kivy TextInput support for right-to-left is an open issue (checked 29/05/2015). Actually, Kivy is not supporting right-to-left not only to TextInput.
For static texts like labels , there is a hack by using arabic_reshaper and python-bidi (reference):
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(u'اللغة العربية رائعة')
bidi_text = get_display(reshaped_text)
Yet, as for TextInput
with a dynamic input, you had to override most of class methods to support RTL and you will end up like implementing the whole RTL support to kivy.
Here is an open attempt to implement Kivy bidi support. Another closed one: Right-to-left labels support.
So there was an answer that I luckily was able to get through and try out.
the links is here: https://github.com/hosseinofj/persian_textinput_kivy/blob/master/codes
Since it got deleted for not explaining, and not posting the code here, I’ll do so myself, albeit the user who posted this link should be thanked somehow too!
Anyway, here is the code:
test.kv
<Ar_text@TextInput>:
text: "whatever"
multiline: 0
size_hint: 1,1
font_name: "data/unifont-11.0.02.ttf" # the font you want to use
font_size: 26
padding_y: [15,0] # can be changed
padding_x: [self.size[0]-self._get_text_width(max(self._lines, key=len), self.tab_width, self._label_cached)-10,8]
main.py
'''
App demonstrating a Text input field which accepts Arabic script in kivy
'''
import arabic_reshaper
from bidi.algorithm import get_display
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, NumericProperty, StringProperty
class Ar_text(TextInput):
max_chars = NumericProperty(20) # maximum character allowed
str = StringProperty()
def __init__(self, **kwargs):
super(Ar_text, self).__init__(**kwargs)
self.text = get_display(arabic_reshaper.reshape("اطبع شيئاً"))
def insert_text(self, substring, from_undo=False):
if not from_undo and (len(self.text) + len(substring) > self.max_chars):
return
self.str = self.str+substring
self.text = get_display(arabic_reshaper.reshape(self.str))
substring = ""
super(Ar_text, self).insert_text(substring, from_undo)
def do_backspace(self, from_undo=False, mode='bkspc'):
self.str = self.str[0:len(self.str)-1]
self.text = get_display(arabic_reshaper.reshape(self.str))
class TestApp(App):
def build(self):
return Ar_text()
if __name__ == '__main__':
TestApp().run()
The trick with this is to use arabic_shaper
So at every update of the text, you provide the text input with a formatted string using the arabic_shaper
.
There is still the fundamental issue that true RTL isn’t there(the cursor is always to the right at the end of a string)
I added a repo with the sample code. It ran on Ubuntu. It should run on windows too if Kivy is properly installed