Skip to content

Commit 5cbc61a

Browse files
committed
make prototype using theme files handle fonts better
1 parent bfc3748 commit 5cbc61a

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
lines changed

docs/source/theme_guide.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ of our UI theme.
413413
Prototype theme blocks must be defined higher in the theme file than where they are used, otherwise they won't exist to
414414
be imported.
415415

416-
Here's a quick example:
416+
If you are using multiple locale specifiers in font blocks, you will need to specify these in every font block in your
417+
prototype hierarchy so that the theme loading can determine which locale's font to apply the theming to.
418+
419+
Here's a quick example of using a simple prototype:
417420

418421
.. code-block:: json
419422
:caption: theme.json

docs/source/theme_reference/theme_button.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Font
5252
block has these parameters:
5353

5454
- "**name**" - Necessary to make a valid block. This is the name that this font goes by in the UI, if this is a new font then subsequent font instances with different styles or sizes should use the same name.
55-
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide.
55+
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide. You will need to keep repeating the locale specifier if using prototypes to make a hierarchy.
5656
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the button.
5757
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
5858
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.

docs/source/theme_reference/theme_label.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Font
2727
:class:`UILabel <pygame_gui.elements.UILabel>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:
2828

2929
- "**name**" - Necessary to make a valid block. This is the name that this font goes by in the UI, if this is a new font then subsequent font instances with different styles or sizes should use the same name.
30+
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide. You will need to keep repeating the locale specifier if using prototypes to make a hierarchy.
3031
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the label.
3132
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
3233
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.

docs/source/theme_reference/theme_progress_bar.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Font
4646
:class:`UIProgressBar <pygame_gui.elements.UIProgressBar>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:
4747

4848
- "**name**" - Necessary to make a valid block. This is the name that this font goes by in the UI, if this is a new font then subsequent font instances with different styles or sizes should use the same name.
49+
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide. You will need to keep repeating the locale specifier if using prototypes to make a hierarchy.
4950
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the health bar.
5051
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
5152
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.

docs/source/theme_reference/theme_screen_space_health_bar.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Font
4646
:class:`UIScreenSpaceHealthBar <pygame_gui.elements.UIScreenSpaceHealthBar>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:
4747

4848
- "**name**" - Necessary to make a valid block. This is the name that this font goes by in the UI, if this is a new font then subsequent font instances with different styles or sizes should use the same name.
49+
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide. You will need to keep repeating the locale specifier if using prototypes to make a hierarchy.
4950
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the health bar.
5051
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
5152
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.

docs/source/theme_reference/theme_text_entry_line.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Font
3535
:class:`UITextEntryLine <pygame_gui.elements.UITextEntryLine>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:
3636

3737
- "**name**" - Necessary to make a valid block. This is the name that this font goes by in the UI, if this is a new font then subsequent font instances with different styles or sizes should use the same name.
38+
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide. You will need to keep repeating the locale specifier if using prototypes to make a hierarchy.
3839
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the text entry line.
3940
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
4041
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.

pygame_gui/core/ui_appearance_theme.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,26 +897,36 @@ def _load_element_font_data_from_theme(self,
897897
self.ui_element_fonts_info[element_name][locale] = {}
898898

899899
font_info_dict = self.ui_element_fonts_info[element_name][locale]
900-
font_info_dict['name'] = file_dict['name']
901-
try:
902-
font_info_dict['size'] = int(file_dict['size'])
903-
except ValueError:
904-
default_size = self.font_dict.default_font.size
905-
font_info_dict['size'] = default_size
900+
if 'name' in file_dict:
901+
font_info_dict['name'] = file_dict['name']
902+
if 'name' not in font_info_dict:
903+
font_info_dict['name'] = self.font_dict.default_font.name
904+
905+
if 'size' in file_dict:
906+
try:
907+
font_info_dict['size'] = int(file_dict['size'])
908+
except ValueError:
909+
default_size = self.font_dict.default_font.size
910+
font_info_dict['size'] = default_size
911+
if 'size' not in font_info_dict:
912+
font_info_dict['size'] = self.font_dict.default_font.size
913+
906914
if 'bold' in file_dict:
907915
try:
908916
font_info_dict['bold'] = bool(int(file_dict['bold']))
909917
except ValueError:
910918
font_info_dict['bold'] = False
911-
else:
919+
if 'bold' not in font_info_dict:
912920
font_info_dict['bold'] = False
921+
913922
if 'italic' in file_dict:
914923
try:
915924
font_info_dict['italic'] = bool(int(file_dict['italic']))
916925
except ValueError:
917926
font_info_dict['italic'] = False
918-
else:
927+
if 'italic' not in font_info_dict:
919928
font_info_dict['italic'] = False
929+
920930
if 'regular_path' in file_dict:
921931
font_info_dict['regular_path'] = file_dict['regular_path']
922932
if 'bold_path' in file_dict:

pygame_gui/core/ui_font_dictionary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ def __init__(self, resource_loader: IResourceLoader, locale: str):
116116
'zh': self._chinese_font}
117117

118118
try:
119-
self.default_font = self.default_font_dictionary[locale]
119+
self.default_font: DefaultFontData = self.default_font_dictionary[locale]
120120
except KeyError:
121-
self.default_font = self._latin_font
121+
self.default_font: DefaultFontData = self._latin_font
122122

123123
self.debug_font_size = 8
124124

0 commit comments

Comments
 (0)