kvdroid / Kvdroid

Some Pyjnius tools for Kivy-Android developments.
MIT License
94 stars 24 forks source link

Cleaned up `font.py` #38

Closed kengoon closed 1 year ago

kengoon commented 1 year ago

cleaned up font.py using python inbuilt xml parser and removed the use of ISO to get font due to most fonts don't have any ISO related to it

yunus-ceyhan commented 1 year ago

I think you got my approach wrong for this function. Kivy already has a very simple font registration system, my purpose is to get the proper font name for a given language automatically. You are making that process manual again.

For instance; let say i have an application which supports more than ten languages and i want my app to return the right font name automatically for the language i chose, then how can I know which font name supports it?

With your approach, first i have to find out which font supports that language then call that font name.

With my approach i only need to call target language's supported font name by its iso code.

kengoon commented 1 year ago

@yunus-ceyhan Not all iso is supported and you still have to look up the iso name before using the font for a specific Language (just that the iso is shorter). My point here is that inside the font.xml file, some language have more than one font attached to it like the Telugu language which has NotoSansTelug, NotoSerifTelugu, NotoSansTeluguUI. Using your method will skip the extra 2 and pick only the first.

Or maybe I can modify the code to still use iso and add extra parameter that will look for a specific font_style

On Fri, 30 Sep 2022, 21:48 Yunus Ceyhan, @.***> wrote:

I think you got my approach wrong for this function. Kivy already has a very simple font registration system, my purpose is to get the proper font name for a given language automatically. You are making that process manual again.

For instance; let say i have an application which supports more than ten languages and i want my app to return the right font name automatically for the language i chose, then how can I know which font name supports it?

With your approach, first i have to find out which font supports that language then call that font name.

With my approach i only need to call target language's supported font name by its iso code.

— Reply to this email directly, view it on GitHub https://github.com/kvdroid/Kvdroid/pull/38#issuecomment-1264011658, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKB42IQM6LCO5EF5LQVFMFDWA5G3FANCNFSM6AAAAAAQYWFEBE . You are receiving this because you authored the thread.Message ID: @.***>

kengoon commented 1 year ago

@yunus-ceyhan I sent you an email requesting for a write access to be able to attend to issues (add tags, close, reopen). I have not got a reply on that

yunus-ceyhan commented 1 year ago

"you still have to look up the iso name before using the font for a specific Language"

Actually not, you can get any language's iso code by Kvdroid so easily and automatically, that's why I'm so obsessed with it. Also i have 3 projects using the existing method so it's extremely necessary for me.

kengoon commented 1 year ago

But if I have no idea of what my iso language or french language is, I have to google that first, anyway I will include the use of the iso and add extra optional parameter to get other font_style

@yunus-ceyhan I sent you an email requesting for a write access to be able to attend to issues (add tags, close, reopen). I have not got a reply on that

what about this

yunus-ceyhan commented 1 year ago

@yunus-ceyhan I sent you an email requesting for a write access to be able to attend to issues (add tags, close, reopen). I have not got a reply on that

Ok. I will when I'm available. So please continue to make a pull request before changing any existing method which i need to check and know if it's affecting any of my existence projects.

kengoon commented 1 year ago

sure, I will not merge anything without your knowledge

yunus-ceyhan commented 1 year ago

But if I have no idea of what my iso language or french language is, I have to google that first, anyway I will include the use of the iso and add extra optional parameter to get other font_style

@yunus-ceyhan I sent you an email requesting for a write access to be able to attend to issues (add tags, close, reopen). I have not got a reply on that

what about this

You don't need to google it, as i said you can determine device language or get requested language's iso code by Kvdroid..

from kvdroid.tools.lang import device_lang

print(device_lang())    # en
print(device_lang("DisplayLanguage"))    # English
print(device_lang(option = "DisplayLanguage", display_lang = "fr"))     # Anglais

"""
Available options are ;

Language           ---> en      
ISO3Language       ---> eng 
Country            ---> US 
ISO3Country        ---> USA 
DisplayCountry     ---> United States 
DisplayName        ---> English (United States) 
String             ---> en_US
DisplayLanguage    ---> English
LanguageTag        ---> en-US
"""
kengoon commented 1 year ago

@yunus-ceyhan how did you get the values of the keys in iso.py. I know it exist in fonts.xml but I want to know how you associated the keys to values

yunus-ceyhan commented 1 year ago

@yunus-ceyhan how did you get the values of the keys in iso.py. I know it exist in fonts.xml but I want to know how you associated the keys to values

I don't remember well but i reviewed some repos then generated my own from a mix of them. Almost all must be true, anyway if there are mistakes we can edit it or add the missing ones..

kengoon commented 1 year ago

Ok, let me try to figure it out

yunus-ceyhan commented 1 year ago

@yunus-ceyhan I sent you an email requesting for a write access to be able to attend to issues (add tags, close, reopen). I have not got a reply on that

done

kengoon commented 1 year ago

@yunus-ceyhan you can now test and if it looks good, go ahead and merge

yunus-ceyhan commented 1 year ago

@yunus-ceyhan you can now test and if it looks good, go ahead and merge

Traceback (most recent call last):
   File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 7, in <module>
     font_name=system_font(font_name='NotoNaskhArabic'))
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kvdroid/tools/font.py", line 133, in system_font
     register_system_font(True)
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kvdroid/tools/font.py", line 127, in register_system_font
     LabelBase.register(**font_data)
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 319, in register
     fonts.append(fonts[0])  # add regular font to list again
 IndexError: list index out of range
yunus-ceyhan commented 1 year ago

Also, since it gives an error I couldn't test it completely but the system_font() function without any parameter should return the device's current language font. For instance, if the device's current language is English it should return Roboto but if it is Arabic it should return NotoNaskhArabic.

Label(text="Hello", font_name=system_font())

kengoon commented 1 year ago

@yunus-ceyhan please test again

kengoon commented 1 year ago

Also, since it gives an error I couldn't test it completely but the system_font() function without any parameter should return the device's current language font. For instance, if the device's current language is English it should return Roboto but if it is Arabic it should return NotoNaskhArabic.

Label(text="Hello", font_name=system_font())

fixed too

yunus-ceyhan commented 1 year ago

@yunus-ceyhan please test again

Label(text="你好世界", font_name=system_font('zh'))
 Traceback (most recent call last):
   File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 10, in <module>
     Label(text="你好世界", font_name=system_font('zh'))
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/uix/label.py", line 329, in __init__
     self._create_label()
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/uix/label.py", line 349, in _create_label
     self._label = CoreLabel(**dkw)
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 288, in __init__
     self.resolve_font_name()
   File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 356, in resolve_font_name
     raise IOError('Label: File %r not found' % fontname)
 OSError: Label: File 'Hant.ttf' not found
yunus-ceyhan commented 1 year ago

I don't know which android version you are using for testing but some android version has different font.xml format so when I wrote that function I considered all.

You can eighter check every android font.xml then edit your code or just leave it as before. The existing function is working without any problem.

However, if you are still so obsessed with changing it you can check all android versions' font.xml from the link below.

https://cs.android.com/android/platform/superproject

kengoon commented 1 year ago

I don't know which android version you are using for testing but some android version has different font.xml format so when I wrote that function I considered all.

You can eighter check every android font.xml then edit your code or just leave it as before. The existing function is working without any problem.

However, if you are still so obsessed with changing it you can check all android versions' font.xml from the link below.

https://cs.android.com/android/platform/superproject

@yunus-ceyhan if you check line 80 to 83 you will discover that I supported both xml files you previously added

if os.path.exists("/system/etc/fonts.xml"):
    SYSTEM_FONT_PATH = "/system/etc/fonts.xml"
else:
    SYSTEM_FONT_PATH = "/system/etc/system_fonts.xml"
kengoon commented 1 year ago
system_font('zh')

This is fixed

yunus-ceyhan commented 1 year ago
system_font('zh')

This is fixed

i already checked it, the difference is not only the file name or path, the font.xml file's content is also changing from version to version, even some manufacturer has its custom font.xml

yunus-ceyhan commented 1 year ago

Tested on Android 9 still same

Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 10, in <module> Label(text="你好世界", font_name=system_font('zh')) File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/uix/label.py", line 329, in __init__ self._create_label() File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/uix/label.py", line 349, in _create_label self._label = CoreLabel(**dkw) File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 288, in __init__ self.resolve_font_name() File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 356, in resolve_font_name raise IOError('Label: File %r not found' % fontname) OSError: Label: File 'Hant.ttf' not found

yunus-ceyhan commented 1 year ago

i test it with different languages as well.

Label(text="Merhaba", font_name=system_font('tr'))

File "/data/user/0/ru.iiec.pydroid3/files/x86_64-linux-android/lib/python3.9/site-packages/kivy/core/text/__init__.py", line 356, in resolve_font_name raise IOError('Label: File %r not found' % fontname) OSError: Label: File 'Latn.ttf' not found

kengoon commented 1 year ago
system_font('zh')

This is fixed

i already checked it, the difference is not only the file name or path, the font.xml file's content is also changing from version to version, even some manufacturer has its custom font.xml

@yunus-ceyhan I know that. The issue is not with the change of the content of the xml file, it is with mapping iso codes to font files that does not exist. for example mapping tr to Latn or en to Latn too. if you look close enough on the xml file you will discover that there is no font with the name Latn on it. And most iso code languages are mapped to Latn on the iso.py which is not the default language of the iso code. The issue is basically with the iso codes

kengoon commented 1 year ago

This last commit fixes everything, check it out

yunus-ceyhan commented 1 year ago
system_font('zh')

This is fixed

i already checked it, the difference is not only the file name or path, the font.xml file's content is also changing from version to version, even some manufacturer has its custom font.xml

@yunus-ceyhan I know that. The issue is not with the change of the content of the xml file, it is with mapping iso codes to font files that does not exist. for example mapping tr to Latn or en to Latn too. if you look close enough on the xml file you will discover that there is no font with the name Latn on it. And most iso code languages are mapped to Latn on the iso.py which is not the default language of the iso code. The issue is basically with the iso codes

i examined it a lot when i wrote it and made my code to handle almost all the cases. All "Latn languages" using Roboto so i returned Roboto when it requested Latn.

Btw you last fix didn't solve anything still getting error.

Label(text = "你好世界", font_name = system_font('zh')) File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/kvdroid/tools/font.py", line 143, in system_font raise KeyError(f"{iso} font file not found in the system's default fonts") KeyError: "zh font file not found in the system's default fonts"

kengoon commented 1 year ago

@yunus-ceyhan I raised that error when the iso does not have any font file associated with it instead of returning Roboto. There should be some level of freedom to the users instead forcing Roboto on them. The error is raised so that they can except it and use which ever font they like. Let say for example, the user wants to make use of zulu language and lets assume the iso code is zu. So now when they try to get that language using the iso they will discover that the font does not exist and instead of forcing them to use Roboto we can raise an error and they can except it and manually use any font file they got

code:

try:
    Label(text="kuma xu!lasd", font_name=system_font('zu'))
except KeyError:
    Labe(text="kuma xu!lasd", font_name='path/to/zulu.ttf')

Label(text = "你好世界", font_name = system_font('zh')) File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/kvdroid/tools/font.py", line 143, in system_font raise KeyError(f"{iso} font file not found in the system's default fonts") KeyError: "zh font file not found in the system's default fonts

By the way I have fixed the above problem, zh has a font file and I was looking in the dictionary values and not the keys. you can test and confirm

yunus-ceyhan commented 1 year ago

Still not working for me. :( I really don't know what to say. We are spending our time on a function that works as expected without getting any negative feedback.

I have 3 applications using that function, one of them has more than 2 million downloads and you want me to ruin my app for the probability someone may want to get some level of freedom in the future.

I understand your good intentions, but you're only making things harder for me by doing this.

I think I couldn't express my approach well to that functionality. Or maybe I didn't get your concept well enough, so I have another idea. Don't touch the existing function, instead, add yours as an alternative with a different name. if I will be convinced that your concept is more practical than mine I will remove it on my own.