Repository: psychopy/psychopy

Issue:
TextBox2 unable to find font in PsychoPy >= 2022.1.1


The following code works in PsychoPy Coder version 2021.2.3 as well as Spyder with Psychopy 2021.2.3.  It fails in Psychopy Coder 2022.1.1, 2022.2.1, and 2022.2.3.

Error mesage:
`3.4919     WARNING     Font b'/Users/MattPetersonsAccount/Documents/Development/PsychoPy/ScamDetection/chirp-regular.ttf' was requested. No similar font found.
`

Code: 
```
from PIL import Image, ImageDraw, ImageFont
from psychopy import visual, core, gui ,event

import os

rootPath = 	dir_path = os.path.dirname(os.path.realpath(__file__))
fontPath = os.path.join(rootPath, "chirp-regular.ttf")

win = visual.Window( size =[ 1024, 768],
				fullscr = False,
				color = (0,0,0,1),
				winType = "pyglet",
				units = "pix",
				allowStencil=True)

with Image.open("images/topLeftTwitterStuff.png").convert("RGBA") as base:
	textBox = visual.TextBox2(win,
				"Hello",
				font=fontPath,
				pos= (0, 0),
				units="pix",
				letterHeight=40,
				size=None,
				color = (1.0, 1.0, 1.0)
				)
							
	textBox.draw()
	vertices = textBox.verticesPix
	minX = min(vertices[:,0])
	maxX = max(vertices[:,0])
	minY = min(vertices[:,1])
	maxY = max(vertices[:,1])

	rWidth = maxX - minX
	rHeight =  maxY - minY
	rPos =  ( (minX+maxX)/2, (minY+maxY)/2),
	print(rWidth)
	print(rHeight)
	print(rPos)

	myRect = visual.Rect(win, width = maxX - minX, height = maxY - minY, pos = rPos, lineColor = (1.0, 1.0, 1.0) )
	myRect.draw()
	print(textBox.verticesPix)

	win.flip()
	event.waitKeys()
	win.close()
	core.quit()
```




Discussion:
I'll look further into this on Monday, but as a quick workaround you should be able to refer to the font by name of you copy the .ttf file to the "fonts" subfolder in the preferences folder - that's where Google Fonts are stored once they're accessed from the server so it's in the search path for Textbox2's font manager. Easiest way to get there is to, from Builder, go to View -> Themes -> Open Themes Folder and then go up one folder in your file explorer.
Unfortunately moving the font to the fonts folder did not work for me.

`5.8565     WARNING     Font b'chirp-regular.ttf' was requested. No similar font found.`
Here is a clue:

(1) When I change the font to "Arial" in that code snippet, it works both in Spyder and Psychopy Coder 2022.2.1
(2) When I went back to my experiment and changed the font to Arial, it properly draws the text on the screen when using Spyder.
(3) But, no text is drawn when I run my experiment from Psychopy Coder 2022.2.1.  Instead, I get the following warning:

`Fontconfig warning: ignoring UTF-8: not a valid region tag`

Note that my experiment uses both TextBox2 and TextStim, so I'm not sure which is triggering the warning.  I do not get that error with Spyder (which happens to be using the 2021.2.3 library).
What happens if you add this code to somewhere after your textbox has been created:
```
print(myTextbox.fontMGR.getFontFamilyNames())
```

That should print out the family names of all fonts available to your system - is Chirp Regular there?
Nope, no fonts containing "Chirp"are among the list, yet if I am in an app like TextEdit, I can clearly see the font in the pull-down fonts menu.
> Nope, no fonts containing "Chirp"are among the list, yet if I am in an app like TextEdit, I can clearly see the font in the pull-down fonts menu.

This sounds similar to https://github.com/psychopy/psychopy/issues/4978
I wonder if it's that the preferences folder is in a different location on Mac than it is on Windows, and font manager is only looking in the Windows location... If I download a random font and put it in my fonts folder (without installing the font), on Windows it does appear in the list when printing known fonts. Could you try that with this font:
https://fonts.google.com/specimen/Cormorant+SC

Just to rule out it being specific to the font file you're using?
Cormorat did not show up when I did _print(myTextbox.fontMGR.getFontFamilyNames())_.  
There are fonts I can see in font manager that are not on the printed listed.  The ones that are missing are listed as "User" fonts, and are stored in /Users/yourUserName/Library/Fonts.

System fonts (the ones I can see in the printout) are stored in /System/Library/Fonts

Resolving patch:
diff --git a/psychopy/visual/textbox2/fontmanager.py b/psychopy/visual/textbox2/fontmanager.py
--- a/psychopy/visual/textbox2/fontmanager.py
+++ b/psychopy/visual/textbox2/fontmanager.py
@@ -43,6 +43,7 @@
 
 _OSXFontDirectories = [
     "/Library/Fonts/",
+    str(Path.home() / "Library" / "Fonts"),
     "/Network/Library/Fonts",
     "/System/Library/Fonts",
     # fonts installed via MacPorts


Test patch: