Word wrapping of multi-line Arabic text

OS Win10:
PsychoPy version v2020.2.4post1

I need to display Arabic text that spans multiple lines. I set the language style feature to Arabic and the text displays well if it spans only one line. For text that spans multiple lines, the word wrapping is displayed bottom-up, rather than up-bottom. For example, if you have an Arabic phrase of “read each of the following dilemmas and then answer them with yes or no”, then it is displayed as:

then answer then with yes or no
read each of the following dilemma

It worked alright when I manually inserted new lines in the text. But this is not a solution that can generalize to different screens, and it makes the text take more room vertically.

Did anyone run into this before, or should this get a fix?


Thanks for your message. We do want to get this right, and might need to ask for your help to troubleshoot this further.

The issue you’ve identified with the older TextStim class is known, and not something we can improve upon, because the order of drawing of the text is out of our control with that particular stimulus (it’s handled by a third-party library called pyglet):

The current situation is that it seems to work correctly for single-line text, in that text is displayed right-to-left, and the individual glyphs are linked to each properly. But the alignment may still be wonky (i.e. text doesn’t seem to align properly to the right).

However, we now have a newer Textbox stimulus (because it allows for editable text, you’ll find it under the “Responses” section of the component panel). This has the advantage that the drawing is under the control of PsychoPy developers, so we should be able to better change the way it behaves.

I’ve just tested it however, and it is more substantially broken.

@jon: here is an example contrasting the two text stimuli, using this example Farsi text string:

در نسخه آخر سایکوپای، مشکل تبدیل متون انگلیسی به فارسی حل شد و از این پس شما قادر خواهید بود که در آزمایشات خود از متون فارسی استفاده کنید

TextStim behaves correctly:

  • text flows right to left (using the bidi algorithm to allow mixing of bi-directional text),
  • characters are linked cursively to their neighbours (using the arabic-reshaper module),

and incorrectly:

  • lines flow from bottom-to-top, rather than top-to-bottom, and


  • the text alignment is centre-aligned

TextBox2 doesn’t respond to language style settings. Even when set to 'Arabic':

  • the text flows left-to-right,
  • characters are not reshaped (i.e. linked to their neighbours cursively, which wouldn’t be possible, as they are flowing in the wrong direction), being displayed in their isolated form.

One could say that it is behaving correctly in that the lines run from top-to-bottom, but I think that is because it is actually being displayed as LTR language style, because changing the language style ('LTR', 'RTL', or 'Arabic') has no discernible effect.

@Jon - from the API it looks like language styles aren’t actually implemented for TextBox2, so I guess that setting is just being accidentally exposed via the Builder interface, and hence is ignored for that component.

I’ll try to take a look at it over the weekend. But given what you know of how TextBox2 works under the hood, what are the chances of us correctly implementing RTL and Arabic language styles for it, while correcting the inherent bottom-to-top flow issue in TextStim?

1 Like

Here’s the Arabic text that I’d like to display
إقرأ كلاً من المعضلات التالية ثم أجب عليها بنعم أو لا، للإجابة بنعم إضغط “ن” و للإجابة بلا إضغط “ل”

إضغط زر المسافة للاستمرار

In my case, the TextBox item has alternating output. It displays either this (just like the Farsi example):

or this, which does not show the letters:

The TextStim has the wrong vertical word wrapping as was shown in the Farsi example. This is tentatively solved if I place line breaks within the text such that it does not need to do word-wrapping. But I worry that this may work on some screens but not on others.

Here’s how it looks without inserting deliberate line breaks

Here’s how it looks after inserting a line break where it should word-wrap, and it looks good:

So I can use this solution, but I’m not sure how it would look on different screens of different devices.

Can you explain what causes the alternation for you? I found that I could get the Farsi test to display using TextBox2 if I specified “Arial” as the font, but the default of “Open sans” displayed only boxes (on a Mac).

I can’t see why it’s happening, since on certain runs the characters are there on others they’re not. The TextBox that I have on my version is TextBox beta. I don’t see it named as TextBox2 anywhere. Should I be getting a newer version?

No, although it’s labelled TextBox in the interface, in the code, under the hood, it is named TextBox2.

It would be useful for us to have the text pasted into a post here, annotated with what the problem is (or lack of problem) for each one. As the active PsychoPy developers aren’t able to write (or type) in Arabic or Farsi, we really need to have our hands held here a little bit. Having copy-able text is very useful to paste into the PsychoPy interface for testing. You’ve given a string of text above that doesn’t work, but having one that does would be useful too.


That is understandable. Here’s a sample text that I’d like to display:

إقرأ كلاً من المعضلات التالية ثم أجب \\\عليها بنعم أو لا، للإجابة بنعم إضغط “ن” و للإجابة بلا إضغط “ل”

Since this string is of a length that does not fit on one line horizontally, it will need to do word-wrapping (I indicated the point where it word-wraps with back-slashes), so it shows on two lines. But the bottom line is the first proportion of the string and the top line is the other proportion of it, so this is how it gets displayed:
عليها بنعم أو لا، للإجابة بنعم إضغط “ن” و للإجابة بلا إضغط “ل
إقرأ كلاً من المعضلات التالية ثم أجب

Horizontally it’s working well, but vertically it isn’t.

If I insert an line break at the place of the the backslashes, it displays correctly as

إقرأ كلاً من المعضلات التالية ثم أجب
عليها بنعم أو لا، للإجابة بنعم إضغط “ن” و للإجابة بلا إضغط “ل

Here’s another string that gets displayed correctly because it fits on one line:
إضغط زر المسافة للاستمرار

1 Like

Dear Mai_kh,

I faced the same problem as yours. What I did was writing the instructions in word, taking a screenshot of the instructions, cropping the screenshot, then adding this in a routine as an image.

It is not very appealing, but at least it worked.


That certainly isn’t ideal - we’d like to make things easier. This thread has prompted some development - hopefully we can bring the new TextBox2 stimulus up to standard here, addressing the limitations of the older TextStim in displaying Arabic and other scripts:

1 Like