Adding a font type

Hello,
I am running an experiment on version 1.84.2 of PsychoPy.
I wish to display text in the Aharoni font. However, this works only on one computer but not on others (yet, I am able to display other fonts, such as Arial, in those computers). In all computers I am using win 10, and in those in which displaying this font does not work, the font does seem to exist in other programs such as Power Point or Word.
How can I add a font?
Thanks,
Ofir

My guess is that the font is installed on one computer but not the others. There’s two solutions:

  1. Install the font on all systems. Google how to do it for your operating system.

  2. You can add the font file (probably “aharoni.ttf”) to your experiment folder and then specify that as the font in your TextStim.

     my_text = visual.TextStim(win, text='Look, this is the Aharoni font!', font='aharoni.ttf')
1 Like

Hi,

I found a solution after doing some research. It seems that non-system font files must be registered for once before you can specify them in a text’s Font field (Builder) or .font property.

You can use a temporary TextStim object at the beginning of the experiment just for registering font files:

font_files = ['NotoSansSC-Regular.otf', 'NotoSerifSC-Regular.otf']
font_main = 'Noto Sans SC'

_ = visual.TextStim(win=win, font=font_main, fontFiles=font_files)
del _

After this, you can set the font of your texts to ‘Noto Sans SC’ or ‘Noto Serif SC’ (the names of the two Simplified Chinese fonts). PsychoPy will recognize the font name and you don’t need to pass the fontFiles argument again.

new_text = visual.TextStim(win=win, text='Hello, font', font=font_main)

Alternatively, you can directly register the font files with pyglet instead of creating a temporary TextStim. This is what the .fontFiles setter does under the hood (see the source code).

import pyglet

font_files = ['NotoSansSC-Regular.otf', 'NotoSerifSC-Regular.otf']

for font in font_files:
    pyglet.font.add_file(font)

new_text = visual.TextStim(win=win, text='Hello, font', font='Noto Sans SC')

(tested with PsychoPy 2022.1.4)