| Reference | Downloads | Github

Using new font files: inconsistent presentations

OS Win10:
PsychoPy version v3.0.5 (Psychopy 3):
Standard Standalone? (y)

What are you trying to achieve?:
I want to present characters from different fonts depending on the trial. The fonts are not part of the psychopy library, so I load them in a previous routine using the code (the text component is called “ImageA_Study”:

ImageA_Study.fontFiles = [‘aurebesh.ttf’] # load file(s)
ImageA_Study.fontFiles = [‘anquietas.ttf’] # load file(s)

In the next routine, I specify the font of the textcomponent using the following code (“Font” is a dummy variable created in the previous routine that allows me to use an if-then statement that specifies the font I’d like to use for this trial

if Font == 'aurebesh.ttf':
    ImageA_Study.font = "aurebesh" # set to font
elif Font == 'anquietas.ttf':
    ImageA_Study.font = "anquietas" # set to font

A screenshot of the routine is attached. The experiment runs fine on about 80% of participants. However, sometimes, a font is displayed in arial font (rather than, e.g., aurebesh) which basically ruins my experiment because I need to see if particular fonts are primed - and as far as I know, there’s no way of tracking whether this error occurs. Are there any fixes to this problem? Am I correct in leaving the ‘Font’ field of the text component blank (See screenshot)?


Do you know that your if/elif logic always works? It is generally a good idea to insert a catch all clause at the end like:

    print('OOPS!! Failed to set the font.')

Something as simple as having a stray space in your conditions file could cause both comparisons to fail: it is always worthwhile coding defensively (i.e. do you know that the variable Font will always contain the exact strings 'aurebesh.ttf' or 'anquietas.ttf'?)

Hi Michael, thank you very much for your reply.
I added that line to the code. Again, the experiment worked perfectly for about 7 demos, but when I tried an 8th demo, it showed Arial font again when it should have been showing another (e.g., aurebesh). Futhermore, there was no message at the end of the experiment (‘OOPS!! Failed to set the font.’), suggesting that the if-else statement did work: perhaps the font library does not load properly? (but this would be weird given that it loads and works sometimes for the exact same experiment).

Any other ideas? And is there any way to track exactly what stimuli was shown? For example, I tried adding:

thisExp.addData(‘ImageA_ActualFontDisplayed’ , ImageA_Study.font)

…but this does not show whether it’s defaulted to Arial or not (it just shows what I originally coded).

PS: In answer to your last comment, the font variable does always contain the exact strings because, for most demos of the (identical) experiment, the experiment works as it should (i.e., the erroroneous ‘Arial’ presentation is not consistent across demos)

To be honest, I have no experience with changing fonts in a text stimulus dynamically. From what you describe, it sounds like this works in principle, but maybe there is some sort of bug deep down that hits a limit somewhere if multiple changes occur. This clearly isn’t ideal, and if possible, such a bug should be hunted down and squashed. But it also has the flavour of something that is occurring in a lower-level library that PsychoPy uses, such as pyglet, rather than in PsychoPy’s own code, and our ability to fix that might be limited.

So if you want a work-around, what about you change tack and create two text stimuli, one defined with each font, each of which is set to be constant. Each stimulus should be defined to appear at the same location and size and text content and so on. Then on each routine, instead of changing the font, you change the visibility of the stimuli? i.e. set the opacity of the aurebesh stimulus to 1 and the other to be 0, and vice versa as required.