How to get rid of gaps between arabic letters when using Justify in QTextEdit?

638 Views Asked by At

I am using QTextEdit to display Arabic text, but there are tiny gaps between the letters when I justify the text.

Here's an example of how it looks:

in-between letters gap

And I highlighted the gaps in here:

Highlighted gaps

I tried changing:

  • Fonts.

  • Font weight.

  • Font size.

  • line wrap mode.

  • Font letter spacing (this one closed the gaps a little when i gave it a negative value, but it's not the solution).

  • Font word spacing.

  • Enabling/disabling kerning

But same problem.

1

There are 1 best solutions below

0
A.R.M On

The problem is caused by applying justification on Arabic letters that's meant for Latin ones.

Arabic letters cannot have spaces between them, unlike Latin letters, and since justifying text in a QTextEdit seems to mess with letter spacing, it cannot be applied to Arabic text, because it simply makes it look ugly and unpleasant to read because of the in between letters gaps.

One solution that seems to work partially, is to use the stretch metric of the used font. In my case, a value of 50 made the gaps almost completely disappear, but it's too tight.

QFont f=text->font();

f.setStretch(50);
f.setLetterSpacing(QFont::AbsoluteSpacing,-0.3);
f.setWordSpacing(2);
text->setFont(f);

You can also combine it with a custom letter and word spacing, it depends on your situation and how it affects your text.

You can take a look at the result, the difference is quite noticeable.

Before:

Text before applying the aforementioned solution

After:

Text after applying the aforementioned solution


Note:

  • The proper way to solve this problem is to use an algorithm that inserts kashidas ('ـ') into the Arabic text, instead of manipulating font metrics, but this is not available in Qt, one has to do that on their own, which is not an easy task.