Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering Hindi(Indian) Language Wrong in Kivy for Android App #7149

Closed
kriyanation opened this issue Oct 14, 2020 · 44 comments
Closed

Rendering Hindi(Indian) Language Wrong in Kivy for Android App #7149

kriyanation opened this issue Oct 14, 2020 · 44 comments
Labels
awaiting-reply Waiting for reply from issue opener, will be closed if no reply

Comments

@kriyanation
Copy link

kriyanation commented Oct 14, 2020

Software Versions

  • Python:3.7
  • OS:Android
  • Kivy:1.11
  • Kivy installation method: P4A

When I render the following text with the google NotoSansfont for Devanagari script in kivy android, I get it rendered wrongly. I have seen similar messages in SO for other languages and it says use pango text provider but it also mentions it is not available for android. Is there a possibility to render the font correctly in android in a kivy App. This will be a make or break for using kivy as the app development platform for us as we need to support Indian languages in our mobile app.

Example Text Expected output ->
image

Example Text Rendered Output ->
image

This happens in all the controls - Label and TextInputs and we have tried different ttf files - NotoSans, Akshar, atleast 10 other font files.

The only font which works correctly is GNU Unifont for us but that is because it has all the glyphs within and the problem we have is it does not look good and readable although that is now our current and only option.

Can we get some help here please?

example code:
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class MainApp(App):
def build(self):
label = Label(text="अंतरिक्ष", font_name="notosans.ttf", size_hint=(0.5,0.5),pos_hint={'center_x':.5,'center_y':.5})
layout = BoxLayout(padding =10)
layout.add_widget(label)
return layout

if name=='main':
app = MainApp()
app.run()

Font file link : https://fonts.google.com/specimen/Noto+Sans?subset=devanagari

@trivedigaurav
Copy link
Member

Let me know if I understand it correctly, the notosans font has the 'क्ष' glyph but is not displayed when you use it for the kivy label? I will try looking at this issue if that is the case.

@kriyanation
Copy link
Author

That is a minor problem or a problem we can live with as the 'क्ष' glyph is not used very frequently in words. The major problem is the रि being rendered out of sequence as र and the ि glyph coming after the letter. The sequence is the problem. This is happening for other Indian languages as well like Tamil and Bengali . Example issue -> https://stackoverflow.com/questions/63047020/kivy-isnt-showing-bengali-unicode-character-properly

@trivedigaurav
Copy link
Member

Thanks, that stack overflow question is helpful. There is long discussion on this here: #2669. This issue is still open.

A possible solution is here: #4902

@kriyanation
Copy link
Author

According to #4902 , it suggests using pango textprovider which is possible in linux but I do not think it is possible with Android or the kivy packaging provides a means to use the renderer in Android using Python for Android. So do we conclude the Kivy does not support Indian languages in Android and move ahead?

@akshayaurora
Copy link
Member

@kriyanation is your problem specific to Hindi ?

@kriyanation
Copy link
Author

From an immediate requirement point of view yes, but the problem of wrong sequencing of glyphs is happening in many of the Indian languages as mentioned above.

@akshayaurora
Copy link
Member

Can you try Arabic text reshaper ? https://github.com/mpcabd/python-arabic-reshaper I think I tried that once and it seemed to solve a lot of issues for me with Hindi too. Can not remember of it works with re specifically. Trying it out now to see if it works

@akshayaurora
Copy link
Member

If arabic text reshaper works then you can simply include it as is cause both arabic text reshaper and python-bidi are pure python and should work as is if you include them in your app dir

@kriyanation
Copy link
Author

kriyanation commented Oct 14, 2020

I tried your suggestion . Here is the code with the Arabic reshaper. It seems to render the text in the same way. Hope I used it in the right way, please check from your side as well.

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.boxlayout import  BoxLayout
from kivy.uix.button import Button
import arabic_reshaper

class MainApp(App):
    def build(self):
        text = "अंतरिक्ष"
        reshaped_text = arabic_reshaper.reshape(text)
        label = Label(text=reshaped_text, font_name="notosans.ttf", size_hint=(0.5,0.5),pos_hint={'center_x':.5,'center_y':.5})
        layout = BoxLayout(padding =10)
        layout.add_widget(label)
        return layout



if __name__=='__main__':
    app = MainApp()
    app.run()

@kriyanation
Copy link
Author

btw the issue is not only with "re" it is with all alphabets with the ि extension. Examples Words below - In effect the language becomes unreadable overall because of this problem.
कवि | तकिया | मिलन
कविता | तिनका | मिला
कितना | तिल | मिलाना

@akshayaurora
Copy link
Member

ok, I tried

import kivy.app
import kivy.uix.label
import arabic_reshaper
import bidi.algorithm
class TestApp(kivy.app.App):
    def build(self):
        reshaped_text = arabic_reshaper.reshape("अंतरिक्ष")
        bidi_text = bidi.algorithm.get_display(reshaped_text)
        return kivy.uix.label.Label(text=bidi_text, font_name="/Users/quanon/Library/Android/sdk/platforms/android-28/data/fonts/NotoSansDevanagari-Bold.ttf")

testApp = TestApp()
testApp.run()

Got the output:
Screenshot 2020-10-15 at 12 24 20 AM

From what I understand the problem is of wrong shape/positioning rather than of font fallback, correct?
If so, we should be able to adapt arabic text reshaper or python-bidi for our purpose?
Looking into it a bit ...

@kriyanation
Copy link
Author

Yes, positioning primarily is wrong. If that can be done it would mean the world to us in terms of moving forward.

@akshayaurora
Copy link
Member

akshayaurora commented Oct 14, 2020

I am looking at https://github.com/python-pillow/Pillow/pull/2576/files this seems to have added support for reshaping to pillow directly which would mean we should be able to just use pillow text provider, though just using KIVY_TEXT=pil does not seem to fix anything for me… needs further investigation.

Update: related issue for reference python-pillow/Pillow#1089

@kriyanation
Copy link
Author

Yes, I tried the pil textprovider as well on Android, the text provider changes succesfully but the rendering remains the same.

@kriyanation
Copy link
Author

While I am totally a noob with respect to font rendering aspect and just an application developer, I read in other posts that harfbuzz has capability to render complex fonts.
I see when the p4a build is running for android there is now a freetype dependency for my application after I added the arabic-shaper and in the build log I see something like compiling freetype with harfbuzz=No . Would changing it somehow to harfbuzz=yes help us? Just a random observation, you can ignore it if it does not make sense.

@akshayaurora
Copy link
Member

akshayaurora commented Oct 14, 2020

some more reference https://forums.libsdl.org/viewtopic.php?p=48243
This patch seems like a good thing to integrate in p4a sdl2_ttf as a patch….

Update, p4a already includes harfbuzz. So we should just need to update sdl2_ttf to latest and to enable harfbuzz at compile time and have font reshaping. Theoretically this should then be able to be enabled by default on all platforms as sdl2 is the default text provider.

@trivedigaurav
Copy link
Member

This is a good discussion. I am also leaving a reference to Pillow issue about this: python-pillow/Pillow#3191

@kriyanation
Copy link
Author

Any suggestions for a quick fix? Can I do something in my dev system which is ubuntu where my p4a build is running to try things out? To me p4a is a blackbox - so if you can help me with some instructions I can try it out in my system.

@kriyanation
Copy link
Author

Any update on this please? We would need to make a decision based on this by end this week. Kivy meets all our requirements and is a pleasure to develop on, but this can be a blocker from our business perspective. So any help and exploration ideas will be useful.

@trivedigaurav
Copy link
Member

I asked on the Sanskrit programmers Google group: https://groups.google.com/g/sanskrit-programmers/c/I57qkxqT-q0/m/EQ69f_TKAAAJ. Hoping to get some more response there.

@kriyanation
Copy link
Author

Looks like this is not priority for the Kivy support or development. We shall be moving to native development this week. Multi Language support is now a de-facto requirement for software frameworks these days. Would be good if you can prioritize it in the future for developers to use it more.

@matham
Copy link
Member

matham commented Oct 18, 2020

@kriyanation it's important to keep in mind that none of the kivy developers are paid to work on Kivy and that we work on Kivy in our free time and according to our own interests. So generally it's considered rude to push the developers, who are gifting you their time, to meet any business deadlines as it will probably be counter productive.

If a feature has not been implemented, it means no one has so far found the time or interest to implement it, whether devs or users. The fastest way to get a feature, especially if you need quickly it for business purposes is to either implement it yourself and find a core-dev also interested in the issue to help you get it in, or to wait if a core-dev shows interest. But if that doesn't work for you then open source software may not match your requirements.

@kriyanation
Copy link
Author

Fair Enough.Thanks.

@darpan5552
Copy link

@akshayaurora
@kriyanation

I don't know much about font rendering but interested in making kivy support indian language.
Can we write a method to re-configure unicode characters like vowel sign for इ
For example: 'किव' will give 'कवि' as per current situation.

A method that will automatically do this type of workaround for us. Also, according to hindi 'क्' + 'ष' = 'क्ष'

Rendering speed might be slow but it may do the work. If my idea is too innocent, then forgive me as a child.

@kriyanation
Copy link
Author

@darpan5552 would it then work for all combinations of इ like बल्कि or किंतु ? We should be careful to not to have case by case implementation. That said, the focus for Kivy framework does not seem to be on language support. Not sure if this issue will get traction.

@darpan5552
Copy link

darpan5552 commented Dec 25, 2020 via email

@kriyanation
Copy link
Author

Can you point me to some more details of 3. Access Android api (backup plan by kivy team)
Best way but does not guarantee success?
Your point 2 suggestion seems to be the best way, do let me know if there are some ideas or docs helping to implement the same.
That said, this is a unicode support issue, it is not only causing problem for one language but multiple Indian languages and maybe other unicodes as well. When most of other renderers on the web and mobile including native Android support these languages, not sure if a contemporary framework should be not supporting it.

@kriyanation
Copy link
Author

With all this I am still a big fan of Kivy - please do not get me wrong.....

@darpan5552
Copy link

darpan5552 commented Dec 25, 2020 via email

@misl6
Copy link
Member

misl6 commented Dec 25, 2020

About harfbuzz and SDL_ttf, this one recently got merged into SDL_ttf, so seems that We're really near to support you use case :

https://hg.libsdl.org/SDL_ttf/rev/b357aefce885

I'm currently busy, but if someone would like to update the SDL recipes on python-for-android, I'm sure it's a game-changer for you all.

@Sahil-pixel
Copy link

I am facing same problem ..with Bengali language

@darpan5552
Copy link

@misl6
Your guidance is certainly the thing we need.
The change in sdl_ttf was performed 21 months ago, still support for non-latin languages is missing.

@trivedigaurav
Copy link
Member

I tried SDL_ttf with newly merged harfbuzz support. It must be built with ./configure -DTTF_USE_HARFBUZZ=1.

I can confirm that the issue described by @kriyanation works well in my sample code: sdl-ttf-test.zip

Before:
before

After:
after

In addition to using the new SDL_ttf, I also had to call TTF_SetScript(HB_SCRIPT_DEVANAGARI); in my C++ code to render the text properly.

I will try to update the SDL recipes, but I may take longer than those who are better versed with the code. So leaving these pointers...

@kriyanation
Copy link
Author

Great. Any idea how to merge it with my current p4A build? How do I tell p4a to use this sdl_ttf?

@Sahil-pixel
Copy link

@trivedigaurav
Great
@kriyanation I have same question ...
I am facing problem with bengali language ..

@darpan5552
Copy link

darpan5552 commented Dec 26, 2020

@Sahil-pixel
your problem will also get solved once sdl_ttf in kivy will get Harfbuzz support.

@trivedigaurav
WELL DONE!
Can you download any hindi essay and test if working in all other cases?

@Sahil-pixel
Copy link

Is this problem is solved or not ?? I want to make an application with different Hindi and Bengali language ..

@tshirtman
Copy link
Member

tshirtman commented Mar 26, 2021

Well, the best way would be for a new version of sdl_ttf to be released, seems the latest release is from january 2019 and the harfbuz support was merged in april 2019, so it’s been sitting unreleased for nearly 2 years now.

Short of that, local recipes to build master for android/ios, with the flag set for harbuzz support, and for desktop building updating kivy-sdk-packager to build this version for windows as well, should be possible, but all of them is some work that hasn’t been done. On linux i think for now we can assume users to be able to build that version localy before building kivy so it’s linked against it if they need that support.

So no, it’s not solved, either way it needs some work, but it’s a bit easier if sdl_ttf do a release. (There is some activity on their size, they migrated to github, including the issues in their bugzilla, so we can certainly hope for a release in the near future, but not i’m not holding my breath either).

@matham
Copy link
Member

matham commented Mar 28, 2021

😉 #7452

image

@Sahil-pixel
Copy link

is it solved or not ?

@Pranzal360
Copy link

wink #7452

image

actually i tried using the code but nothing seems to work ! any further solutions ?

@Julian-O
Copy link
Contributor

Julian-O commented Nov 2, 2023

I assume #7227 is related.

@Julian-O
Copy link
Contributor

#7227 was solved with a new feature added in Kivy 2.2.0 for users of TTF fonts and SDL to display it: Label's font_script_name

I suspect it will solve these problems too.

@kriyanation: please try setting that value, and tell us if the problem is still there.

@Julian-O Julian-O added the awaiting-reply Waiting for reply from issue opener, will be closed if no reply label Nov 11, 2023
Copy link

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have the means to take action. Please reach out if you have or find the answers we need so that we can investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-reply Waiting for reply from issue opener, will be closed if no reply
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants