RTL (Persian / Arabic) Text Support for Defold 🎮

Hi everyone :waving_hand:

I’ve released a small open-source RTL text helper library for Defold, focused on Persian (Farsi) and Arabic UI text rendering.

Defold doesn’t provide native shaping for RTL languages, so this library handles character shaping and RTL ordering in Lua, making it possible to correctly display Persian/Arabic text in GUI nodes.


:sparkles: Features

  • Correct RTL rendering for Persian & Arabic

  • Proper character shaping
    (isolated / initial / medial / final)

  • Supports Arabic diacritics (Tashkeel / Harakat)

  • Supports Persian semi-space

  • Supports multi-line text


:package: GitHub Repository

5 Likes

Thank you for sharing this! I’m not sure if you are aware, but we added RTL support in Defold 1.12.0 beta:

1 Like

Today I was able to test version 1.12.1
But it doesn’t seem to display RTL texts correctly.
In fact, I don’t see any difference
Do I need to take any special steps to enable RTL text?
I also don’t see anything about RTL support in the link you sent.

Hi @thawri !

Due to the added engine size the feature comes with, we decided to not include it “by default”.
This means you will need to use an app manifest to include it.

It’s mentioned in the release notes as "Added support for text shaping" where RTL is one use case.
To enable the feature, you need to:

  • set the font.runtime_generation = 1 in game.project to use .ttf fonts at runtime.
  • use an app manifest, selecting the “Font Layout” feature.

We haven’t finished our example project yet, but in the meantime there is a test project here:

3 Likes


I try to run extension-fontgen project but encounter with this errors.
I just cloned and ran it.

1 Like

Note that I linked to the data-only branch. The main branch still uses the older version of this feature, but is deprecated and isn’t supported in 1.12.*

Also, make sure to use the 1.12.0 Beta version for now. (I noticed that the 1.12.1 alpha is not working correctly with this example for some reason, I’m working on that)

1 Like

I’m looking at adding in Arabic to our game.

I’m now using 1.12.0 and have managed to switch over to using depth field runtime fonts. Everything’s now working fine for our roman, Chinese, Japanese, Korean etc but, adding in Arabic, I’m finding that a lot of characters aren’t displaying.

Please could you confirm - should I expect Arabic to work ‘out of the box’ with 1.12.0? Or do I need to use the library that thawri has provided here to deal with displaying it properly?

Thanks!

It should work out of the box. @Pawel is working on an example to show all of the new functionality.

2 Likes

@HHH can you share with me the font you’re using? I can check.

Yes, I’m preparing a localization example and Arabic (NotoNasksArabic) works:

2 Likes

Thanks, that’s good to hear.

Attached is my .font and .ttf file.

Thanks!

Archive.zip (92.0 KB)

In case it sheds any light on things, this is the same piece of text in three different languages… it’s only the Arabic one that doesn’t seem to display properly. It seems like the glyphs are all being generated OK, prewarming all seems fine, just the actual display seems to mess up (for every label I have using Arabic)…

This is the arabic string it’s trying to print “مجموعات الأشكال التي قمت بفتحها من شاشات الخريطة أو الفعاليات ستظهر هنا.”,

1 Like

Hmm, no problems with the font itself. Maybe you can share a repro (you can send it on DM or at pawel@defold.se).

Did you test it with All Chars enabled in the .font properties?

Yes, I used the .font file from your package, unmodified|:

(for this text, it looks like it doesn’t matter, I tested now also with it disabled)

1 Like

@HHH are you using runtime generated fonts?

3 Likes

@HHH
if you don’t want to use all char you can use below chars.
this is reduce my game size by 1.5MB. (matter for web build)
using 3 RTL font size:
۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩ءآأؤإئابةتثجچحخدذرزژسشصضطظعغفقکكگلمنوهيیىﭐﭑﭒﭓﭔﭕﭖﭗﭘﭙﭚﭛﭜﭝﭞﭟﭠﭡﭢﭣﭤﭥﭦﭧﭨﭩﭪﭫﭬﭭﭮﭯﭰﭱﭲﭳﭴﭵﭶﭷﭸﭹﺀﺁﺂﺃﺄﺅﺆﺇﺈﺉﺊﺋﺌﺍﺎﺏﺐﺑﺒﺕﺖﺗﺘﺙﺚﺛﺜﺝﺞﺟﺠﭺﭻﭼﭽﺡﺢﺣﺤﺥﺦﺧﺨﺩﺪﺫﺬﺭﺮﺯﺰﮊﮋﺱﺲﺳﺴﺵﺶﺷﺸﺹﺺﺻﺼﺽﺾﺿﻀﻁﻂﻃﻄﻅﻆﻇﻈﻉﻊﻋﻌﻍﻎﻏﻐﻑﻒﻓﻔﻕﻖﻗﻘﻙﻚﻛﻜﮎﮏﻝﻞﻟﻠﻡﻢﻣﻤﻥﻦﻧﻨﻩﻪﻫﻬﻭﻮﻱﻲﻳﻴﯼﯽﯾﯿﻻﻼﻵﻶﻷﻸ
‌َ ً ُ ٌ ِ ٍ ْ ّ ٓ ٔ ٕ ٖ ٗ ٘ ٙ ٚ

ﻹﻺ؟!.،؛:«»(){}±=*/_%"#$'0123456789?@٪‌‍‎‏ﮐﮑﮒﮔﮕﺓﺔﻯﻰ

EDIT: its also contains persian chars. But there aren’t many. maybe numbers and ک گ چ ژ پ , and their related shapes.

1 Like

It’s good to know that if “runtime fonts” is used, then the “All characters” is used as the “prewarm text” instead. We’ll parse that and do full text shaping on that text, in order to generate the correct glyphs for proper shaping.
This is good for showing a first text quickly when booting. After that, you can use the font.prewarm_text() to load new glyphs for showing.

2 Likes

Hi all, thank you for your answers.

I think the problem was down to my font size being too big (it was at 48). I was seeing warnings about the cache being too small and needing to be resized (even when I tried making it huge, like 16k*16x). Shrinking the font size down to 24 has got rid of those warnings and the text now seems to display OK.

My guess is that the resize of the cache (which obviously is best to avoid so I need to shrink the font down now I’m using real-time fonts) somehow messes up the drawing of the Arabic.

Thanks everyone for your help in finding this. Great work on the RTL support!

2 Likes

If the glyphs gets evicted from the glyph cache, then yes, it will render incorrectly, which is why it’s good to keep track of these error message about cache size.

I’m glad you got it working now!

1 Like

One more question - I don’t have any knowledge of Arabic or RTL languages generally so please excuse me if this is a stupid question…

I noticed that the text appears different if line breaks are used. I can see that the same words seem to be there but the ordering has changed. Also, when on a single line, the ordering seems different from what I see in Google translate going English→Arabic. Trying to Google translate any of the lines on my screen back to English seems to result in a correct translation. So my question is - are all these versions of the same sentence valid? Or has something gone wrong here?

If anyone here reads Arabic ( thawri ?) and can confirm, that would be really helpful.

Thanks!

1 Like