Skip to content

Commit fba09ef

Browse files
ToddTodd
authored andcommitted
BF: Better error handling when font isn't found - default to Open Sans Regular and deliver an alert rather than crashing with an obscure error
1 parent 0419007 commit fba09ef

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
code: 4325
3+
cat: Components
4+
msg: Font `{font} {style}` not available in weight `{weight}`, component `{name}` will default to Open Sans Regular.
5+
6+
# The following are typically used for online help pages, and support reStructured Text.
7+
label: Font not available
8+
9+
synopsis: |
10+
The font you requested could not be found in the weight and style you requested, this alert is to warn you that your component will use Open Sans Regular (fonts.google.com/specimen/Open+Sans)
11+
12+
details: |
13+
There are a few reasons why you might be receiving this alert:
14+
1. The font you requested does not exist at all, there may be a typo in the font name.
15+
2. The font is one which is not installed on your local machine or available on Google Fonts.
16+
3. The font you requested exists, but not in the style you requested (bold, italic, etc.). For example, if you requested the font Raleway Dots (fonts.google.com/specimen/Raleway+Dots) in bold, you will always receive this error as this font only exists in regular.
17+
4. The font you requested exists on Google Fonts, but could not be retrieved, for example if you are connected to the internet.
18+
19+
solutions: |
20+
The first things to check are that the name of the font is spelled correctly, that the font exists and that you are connected to the internet. You can check which fonts are installed on your machine through the Settings for your operating system, you can check whether the font exists on Google Fonts by going to fonts.google.com and searching for it. If the font exists, you can check (either on Google Fonts or your operating system's font manager) what weights and styles it exists in. If `bold` is ticked in Builder, then the requested font weight is 700 (Bold), if not then the requested weight is 400 (Regular). You can set the font weight more precisely by supplying a numeric font weight to the component rather than just True or False, for example if you wanted the font to be Extra-Light you could supply the value 200.
21+
22+
versions: |
23+
All

psychopy/alerts/alertsCatalogue/alertCategories.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
4310: Cannot calculate parameter
4949
4315: Invalid dollar sign syntax
5050
4320: Non-local font
51+
4325: Font not available
5152

5253
5000: Online studies
5354
6000:

psychopy/visual/textbox2/textbox2.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .fontmanager import FontManager, GLFont
2626
from .. import shaders
2727
from ..rect import Rect
28-
from ... import core
28+
from ... import core, alerts
2929

3030
allFonts = FontManager()
3131

@@ -160,6 +160,16 @@ def __init__(self, win, text, font,
160160
self.padding = padding
161161
self.glFont = None # will be set by the self.font attribute setter
162162
self.font = font
163+
# If font not found, default to Open Sans Regular and raise alert
164+
if not self.glFont:
165+
alerts.alert(4325, self, {
166+
'font': font,
167+
'weight': 'bold' if self.bold is True else 'regular' if self.bold is False else self.bold,
168+
'style': 'italic' if self.italic else '',
169+
'name': self.name})
170+
self.bold = False
171+
self.italic = False
172+
self.font = "Open Sans"
163173

164174
# once font is set up we can set the shader (depends on rgb/a of font)
165175
if self.glFont.atlas.format == 'rgb':

0 commit comments

Comments
 (0)