Kivy: arabic text input

Created on 19 Oct 2013  ·  38Comments  ·  Source: kivy/kivy

how can I convert text in typing time? http://postimg.org/image/u40ftpwm9/ http://postimg.org/image/ibefw8b89/ as you can see when I type in kivy the word is out of shape so I used "arabic reshaper" to fix this problem. now I can use convert button to fix It but how should I fix this in typing time without pressing convert button?

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Has PR Feature

Most helpful comment

I'm creating a python binding for harfbuzz library to use its text glyph info inside kivy so we can advance cursor with details it gives to us. with mix of python-bidi and pyharfbuzz we can have multi language support for kivy.

All 38 comments

Try to pre-process the input by overriding the insert_text and passing the text through arabic re-shaper.

This would still be a hack and we need proper support for right to left languages, contributions/pull requests are always welcome, would help if some familiar with RtoL languages could devote time on this.

tnx for your answer. I found this "insert_text(substring, from_undo=False)" in api refrence can you give me a simple example of using It? I'm very new in kivy .

and is this possible to use halign for rtl in text input?

TextInput doesn't support alignment yet, that's y I stated the we actually need proper support for RtoL languages.

ok . what about insert_text example?

tnx I write this in my main.py so how can I tell kivy to use It for text input?

my main.py is:

import kivy
import arabic_reshaper
from bidi.algorithm import get_display

kivy.require('1.5.1')

from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout

from components.initialize import initialize_platform
from components.ttsspeak import ttsSpeak
from kivy.uix.textinput import TextInput

class NumericInput(TextInput):

    def insert_text(self, substring, from_undo=False):
        if not from_undo:
            try:
                int(substring)
            except ValueError:
                return
        super(NumericInput, self).insert_text(substring, from_undo)

class SayThis(FloatLayout):
    saywhat_text = ObjectProperty(None)

    def say_something(self, text):
        ttsSpeak(text)

    def clear(self, text):
        print text
        reshaped_text = arabic_reshaper.reshape(text)
        text = bidi_text = get_display(reshaped_text)
        print text
        self.saywhat_text.text =  text
        self.saywhat_text.focus = True


class SayThisApp(App):
    def build(self):
        initialize_platform()
        return SayThis()


if __name__ == '__main__':
    SayThisApp().run()

I changed kivy.uix.textinput.py to this :

def _create_line_label(self, text, hint=False):
        reshaped_text = arabic_reshaper.reshape(text)
        text = get_display(reshaped_text)

and now It has no problem in showing arabic and farsi but I change It in the kivy core and It's not a good way . how can I force text input to use my input text class inside of my application? as I said I'm very new in kivy

Well, maybe it would be nice to do a PR of that change? That adds a
dependency of kivy to arabic reshaper, but we need internationalisation
anyway, so unless there is a better way to do it, i think it's the way
forward.

At least it should be discussed.

If you want to do it only for your application, you could do a class

class BidiTextInput(TextInput):
def _create_line_label(self, text, hint=False):
reshaped_text = arabic_reshaper.reshape(text)
text = get_display(reshaped_text)
super(BidiTextInput, self)._create_line_label(text, hint)

and use this class instead of TextInput in the rest of your code. (in kv
and python, call BidiTextInput instead of TextInput, everytime you need
it). (BidiTextInput may not be a great name, i lake imagination right
now :P)

But i think it would be a nice addition to kivy.

On Sun, Oct 20, 2013 at 03:16:32AM -0700, Nima wrote:

I changed kivy.uix.textinput.py to this :+1:
def _create_line_label(self, text, hint=False):
reshaped_text = arabic_reshaper.reshape(text)
text = get_display(reshaped_text)
and now It has no problem with arabic and farsi but I change It in the kivy core and It's not a good way . how can I force text input to use my input text class in my application? as I said I'm very new in kivy


Reply to this email directly or view it on GitHub:
https://github.com/kivy/kivy/issues/1570#issuecomment-26670261

tnx. there is another problem with right to left and I have to work on It

I understand it's working now, PR? :P

On Mon, Oct 21, 2013 at 12:25:13AM -0700, Nima wrote:

tnx. there is another problem with right to left and I'm working on It


Reply to this email directly or view it on GitHub:
https://github.com/kivy/kivy/issues/1570#issuecomment-26697061

I still working on right to left,my solution is getting curser position and resolution of screen and using tab to move the word to the correct place. do you have a better Idea?

Hey! Where can I find arabic reshaper?

Hey! Where can I find arabic reshaper?

https://github.com/mpcabd/python-arabic-reshaper

Do you want the haxe version?
On Aug 5, 2014 7:32 PM, "Kashif Iftikhar" [email protected] wrote:

Hey! Where can I find arabic reshaper?

https://github.com/mpcabd/python-arabic-reshaper


Reply to this email directly or view it on GitHub
https://github.com/kivy/kivy/issues/1570#issuecomment-51223419.

for textinput it wont work

def _create_line_label(self, text, hint=False):
reshaped_text = arabic_reshaper.reshape(text)
text = get_display(reshaped_text)
adding it to TextInput will get:
Traceback (most recent call last):
File "/home/sajad/PycharmProjects/kimiastone/Persian.py", line 112, in
myApp().run()
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 824, in run
runTouchApp()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
EventLoop.window.mainloop()
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 401, in mainloop
self._mainloop()
File "/usr/local/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 288, in _mainloop
EventLoop.idle()
File "/usr/local/lib/python2.7/dist-packages/kivy/base.py", line 327, in idle
Clock.tick()
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 483, in tick
self._process_events()
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 615, in _process_events
event.tick(self._last_tick, remove)
File "/usr/local/lib/python2.7/dist-packages/kivy/clock.py", line 374, in tick
ret = callback(self._dt)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/textinput.py", line 1512, in _refresh_line_options
self._refresh_text_from_property()
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/textinput.py", line 1531, in _refresh_text_from_property
self._refresh_text(self._get_text(encode=False), *largs)
File "/usr/local/lib/python2.7/dist-packages/kivy/uix/textinput.py", line 1551, in _refresh_text
_line_rects.append(Rectangle(size=lbl.size))
AttributeError: 'NoneType' object has no attribute 'size'

I am currently working on kivy's rtl support. so i plan to start by implementing a reshaper in c(cython) . any suggestion ?

there is no reshaper in c but you can use cython and python arabic reshaper and bidi

i think there should be some changes in widget like algin property
im working on widget algin support

i think i have to replace windows x with 0

The Only difficulty is the multiline-text-entry, selection and markups. the LabelBase's classes (SDL, PyGame & Pil) are easy to do that.

hey @pylover did you get some result? would be nice to go forward on this :)

Interesting, but i don't have time.
I am planning a project with kivy in next month. i think it's good chance for me to do that.

I am starting just right now. I love kivy

follow

hellow
thank for all of idea
but there is problem that not solved
and problem is : when i convert .py file to .apk file i find new problem that is encoding of textinput in android system is 'str' and type is hexdecimial but reshaper get unicode utf-8
now how i can convert it to unicode for reshaper??

@motad333 , I have no idea.the core developers of kivy may help you.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Cursor has a bug on farsi/arabic text input and it wont work until SDL fix this problem.
The problem rises from kivy.core.label.LabelBase.get_extents(). it has implemented in pygame, PIL, and SDL. since pygame and PIL using SDL2 ttf, LabelBase implementation of these providers have problem too.

kivy_input

My fork contains fix for farsi/arabic Label/Button, but user has to change font_name to a valid farsi/arabic font_name, since default font doesn't work for farsi. I can create a PR if you want.

Edit: I try to find SDL2's problem with arabic/farsi rendering , but i don't have previous experience about how font/text rendering works.

@RevengeComing ..hi i want to use this solution in my App..can you tell me how

@engmms , Yet it's not a valid solution but you can install my fork:
pip install git+https://github.com/RevengeComing/kivy.git
( you might need to install rtl, arabic_reshaper, python-bidi , install them with pip)

And you have to add a farsi/arabic font to your application and change all the font_name of your Labels/Buttons/TextInputs to your font since default font doesn't support farsi/arabic.

I'm creating a python binding for harfbuzz library to use its text glyph info inside kivy so we can advance cursor with details it gives to us. with mix of python-bidi and pyharfbuzz we can have multi language support for kivy.

Hello again,

I created kivy_i18n repository in order to solve this problem:

The result is:
peek 2018-01-22 16-23

It requires pyharfbuzz and python-fribidi which only supports linux(py3, py2) and mac(py3) right now as well as harfbuzz, fribidi, freetype2.

TextInput has some problems like not being rtl and ... but I'll add these features to it.

ok @RevengeComing...thanks for your hard word ..

@RevengeComing ...I will test this solution

An experimental support is now available in master using KIVY_TEXT=pango

Although, as far as i know this solution only works on Linux and OSX, more work is certainly needed for it to work on Windows and mobile platforms.

@RevengeComing @tshirtman Hi buddy, What's up? Is this feature ready to be integrated into mobile and windows applications?I'm really concerned about Android right now rather than windows. One silly workaround for Persian/Farsi speakers is to use Finglish. :)

Was this page helpful?
0 / 5 - 0 ratings