Handling Arabic Diacritics in PsychoPy 2024.1.4 TextStim

Hi! I’m building a script for RSVP study in Arabic, but the script crashes whenever it encounters a word with diacritics (if there are no diacritics, the script works fine). I’ve tried resolving this issue by using several fonts (e.g., Arial, Sahel,…) , the problem persists. Here is the line of the script for the stimulus:
stim = visual.TextStim(win, text=words[wordIndex], languageStyle=‘Arabic’)

And the error message I get is:
TypeError: ord() expected a character, but string of length 2 found

I suspect the issue might be with the text input. The variable words[wordIndex] contains Arabic text, but I’m not sure how to verify or fix it. How can I resolve this? Thanks for any help!

The full traceback is:
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 230, in init
self.setText(text, log=False)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 395, in setText
setAttribute(self, ‘text’, text, log)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 140, in setAttribute
setattr(self, attrib, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\tools\attributetools.py”, line 29, in set
newValue = self.func(obj, value)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 386, in text
self._setTextShaders(text)
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\visual\text.py”, line 404, in setTextShaders
self.pygletTextObj = pyglet.text.Label(
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text_init.py”, line 452, in init
super(Label, self).init(document, x, y, width, height,
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text_init
.py”, line 273, in init
super(DocumentLabel, self).init(document,
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 820, in init
self.document = document
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 929, in _set_document
self._init_document()
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 1043, in _init_document
self._update()
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 966, in _update
lines = self._get_lines()
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 942, in _get_lines
glyphs = self._get_glyphs()
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\text\layout.py”, line 1085, in _get_glyphs
glyphs.extend(font.get_glyphs(text[start:end]))
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\font\base.py”, line 394, in get_glyphs
self.glyphs[c] = glyph_renderer.render(c)
File “C:\Users\vpixx\AppData\Roaming\Python\Python310\site-packages\pyglet\font\win32.py”, line 432, in render
ord(text), ord(text), byref(abc)):
TypeError: ord() expected a character, but string of length 2 found

Try a texbox2 stim.

Thank you so much! I replaced textStim with TextBox2, and the script no longer crashes. However, the font size is now very small, and I can’t fix this by adjusting letterHeight or size. One suggestion was to set unit='pix', but I am not using pixel mode.

This is the previous line: stim = visual.TextStim(win, text=words[wordIndex], languageStyle=‘Arabic’, font=stimuliFont, units=stimuliUnits, height=stimuliSize, color=stimuliColor)

And this is what I made to work, but only with a small font: stim = visual.TextBox2(win, text=words[wordIndex], languageStyle=‘Arabic’, font=stimuliFont, units=stimuliUnits, alignment=‘center’, color=stimuliColor)

Here’s the code I’m using (in height units). You need letterHeight and size.

visual.TextBox2(win, 
            name=str(Idx),
            text='',
            font='Arial',
            pos=(columnX[3] , topRow-Idx*rowGap),
            letterHeight=fontSize, 
            alignment='left',
            anchor='left',
            size = [columnX[4]-columnX[3]+fontSize,fontSize*1.2]
            )

Thank you wakecarter for your help.
The following is giving us detached letters, the font is set to Amiri. From my understanding is that the detached letter issue was resolved since PsychoPy integrated arabic_reshaper, but we seem to still have this issue.

visual.TextBox2(win,
text=words[wordIndex],
languageStyle=‘Arabic’,
font=stimuliFont, units=stimuliUnits, letterHeight=stimuliSize,
size=(boxWidth, boxHeight), anchor=‘center’, alignment=‘center’, color=stimuliColor)

Edit:
with visual.TextStim the arabic word appears correctly (when it has not any diacritic), so there seems to be something done correctly in visual.textstim that attaches the letters together correctly that is not done in TextBox2, I am trying to investigate which part is that and hopefully make TextBox2 attach the letters correctly.