Skip to content

Commit

Permalink
Merge pull request #448 from MyreMylar/font-theme-hierarchy
Browse files Browse the repository at this point in the history
Make prototype using theme files handle fonts better
  • Loading branch information
MyreMylar authored Apr 23, 2023
2 parents 8c38dd3 + 5cbc61a commit 326dde6
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
5 changes: 4 additions & 1 deletion docs/source/theme_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ of our UI theme.
Prototype theme blocks must be defined higher in the theme file than where they are used, otherwise they won't exist to
be imported.

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

Here's a quick example of using a simple prototype:

.. code-block:: json
:caption: theme.json
Expand Down
2 changes: 1 addition & 1 deletion docs/source/theme_reference/theme_button.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Font
block has these parameters:

- "**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.
- "**locale**" - Optional parameter to set this font as belonging to a particular locale only. See the :ref:`localization` guide.
- "**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.
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the button.
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_label.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Font
:class:`UILabel <pygame_gui.elements.UILabel>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:

- "**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.
- "**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.
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the label.
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_progress_bar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Font
:class:`UIProgressBar <pygame_gui.elements.UIProgressBar>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:

- "**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.
- "**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.
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the health bar.
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Font
:class:`UIScreenSpaceHealthBar <pygame_gui.elements.UIScreenSpaceHealthBar>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:

- "**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.
- "**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.
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the health bar.
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_text_entry_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Font
:class:`UITextEntryLine <pygame_gui.elements.UITextEntryLine>` accepts a font specified in the theme via a 'font' block. A 'font' block has these parameters:

- "**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.
- "**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.
- "**size**" - Necessary to make a valid block. This is the point size of the font to use on the text entry line.
- "**bold**" - Optional parameter. Set it to "1" to make this font bold.
- "**italic**" - Optional parameter. Set it to "1" to make this font italic.
Expand Down
26 changes: 18 additions & 8 deletions pygame_gui/core/ui_appearance_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,26 +897,36 @@ def _load_element_font_data_from_theme(self,
self.ui_element_fonts_info[element_name][locale] = {}

font_info_dict = self.ui_element_fonts_info[element_name][locale]
font_info_dict['name'] = file_dict['name']
try:
font_info_dict['size'] = int(file_dict['size'])
except ValueError:
default_size = self.font_dict.default_font.size
font_info_dict['size'] = default_size
if 'name' in file_dict:
font_info_dict['name'] = file_dict['name']
if 'name' not in font_info_dict:
font_info_dict['name'] = self.font_dict.default_font.name

if 'size' in file_dict:
try:
font_info_dict['size'] = int(file_dict['size'])
except ValueError:
default_size = self.font_dict.default_font.size
font_info_dict['size'] = default_size
if 'size' not in font_info_dict:
font_info_dict['size'] = self.font_dict.default_font.size

if 'bold' in file_dict:
try:
font_info_dict['bold'] = bool(int(file_dict['bold']))
except ValueError:
font_info_dict['bold'] = False
else:
if 'bold' not in font_info_dict:
font_info_dict['bold'] = False

if 'italic' in file_dict:
try:
font_info_dict['italic'] = bool(int(file_dict['italic']))
except ValueError:
font_info_dict['italic'] = False
else:
if 'italic' not in font_info_dict:
font_info_dict['italic'] = False

if 'regular_path' in file_dict:
font_info_dict['regular_path'] = file_dict['regular_path']
if 'bold_path' in file_dict:
Expand Down
4 changes: 2 additions & 2 deletions pygame_gui/core/ui_font_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def __init__(self, resource_loader: IResourceLoader, locale: str):
'zh': self._chinese_font}

try:
self.default_font = self.default_font_dictionary[locale]
self.default_font: DefaultFontData = self.default_font_dictionary[locale]
except KeyError:
self.default_font = self._latin_font
self.default_font: DefaultFontData = self._latin_font

self.debug_font_size = 8

Expand Down

0 comments on commit 326dde6

Please sign in to comment.