Page 1 of 1

Allow player to change font (Opendyslexic or else)

Posted: Fri Jan 24, 2020 3:32 pm
by Ayael
Hi guys, accessibility is important, so you might be interest to add an OpenDyslexic font to your game, or even just several option (a fancy one and a sans serif one for example).

→ If you want to change only the in game font please look at this great tutorial about font selection from BáiYù.

→ If you want to change all the font (so in game, interface like menu button and character name), there is two possibility :
1) All your fonts are the same
2) All your fonts are different (more likely).

1) All your font are the same.
Step 1 : put all your font, including OpenDyslexic into your game folder. (You can find it here)
Step 2 : Go into your gui.rpy file, and look for this piece of code

Code: Select all

## Fonts and Font Sizes ########################################################

## The font used for in-game text.
define gui.text_font = "DejaVuSans.ttf"

## The font used for character names.
define gui.name_text_font = "DejaVuSans.ttf"

## The font used for out-of-game text.
define gui.interface_text_font = "DejaVuSans.ttf"`
and delete it.

step 3 : Instead put :

Code: Select all

## The font used for in-game text.
define gui.text_font = gui.preference("font", "DejaVuSans.ttf") 

## The font used for character names.
define gui.name_text_font = gui.preference("font", "DejaVuSans.ttf")

## The font used for out-of-game text.
define gui.interface_text_font = gui.preference("font", "DejaVuSans.ttf")
(replace DejaVuSans by your own font. Please be careful, there is too common format .ttf or .otf don't forget to change it accordingly ! All fonts must be the same otherwise look at the second part of the tutorial).

step 4 : Go into your screen.rpy, and look for your preference screen find this line :
## Additional vboxes of type "radio_pref" or "check_pref" can be
## added here, to add additional creator-defined preferences.
Then add :

Code: Select all

vbox:
	style_prefix "check"
	label _("Font")
	textbutton _("Default") action gui.SetPreference("font", "DejaVuSans.ttf")
	textbutton _("OpenDyslexic") action  gui.SetPreference("font", "OpenDyslexic.otf")
(Once again, don't forget to change the DejaVuSans by our own font.)

Bonus : Add more font choices.
You can add as many font choice you want, at the end of the previous code inside the preference screen just add this line as much as you need :

Code: Select all

textbutton _("OpenDyslexic") action  gui.SetPreference("font", "Yourfont.otf")

2) All your font are different (so a font for in game, a font for buttons and so on.)

Step 1 : put all your font, including OpenDyslexic into your game folder. (You can find it here)
Step 2 : Go into your gui.rpy file, and look for this piece of code

Code: Select all

## Fonts and Font Sizes ########################################################

## The font used for in-game text.
define gui.text_font = "DejaVuSans.ttf"

## The font used for character names.
define gui.name_text_font = "DejaVuSans.ttf"

## The font used for out-of-game text.
define gui.interface_text_font = "DejaVuSans.ttf"`
and delete it.

step 3 : Instead put :

Code: Select all

## The font used for in-game text.
define gui.text_font = gui.preference("font_1", "DejaVuSans.ttf") 

## The font used for character names.
define gui.name_text_font = gui.preference("font_2", "DejaVuSans.ttf")

## The font used for out-of-game text.
define gui.interface_text_font = gui.preference("font_3", "DejaVuSans.ttf")
(replace DejaVuSans by your own fonts. Please be careful, there is too common format .ttf or .otf don't forget to change it accordingly !)

step 4 : go into your screen.rpy, and look for your preference screen find this line :
## Additional vboxes of type "radio_pref" or "check_pref" can be
## added here, to add additional creator-defined preferences.
Then add :

Code: Select all

vbox:
	style_prefix "check"
	label _("Font")
	textbutton _("Default") action [gui.SetPreference("font_1", "DejaVuSans.ttf"), gui.SetPreference("font_2", "DejaVuSans.ttf"), gui.SetPreference("font_3", "DejaVuSans.ttf")]
	textbutton _("OpenDyslexic") action [ gui.SetPreference("font_1", "OpenDyslexic.otf"), gui.SetPreference("font_2", "OpenDyslexic.otf"), gui.SetPreference("font_3", "OpenDyslexic.otf")]
(Once again, don't forget to change the 3 DejaVuSans by our own fonts.)

Bonus : Add more font choices.
You can add as many font choice you want, at the end of the previous code inside the preference screen just add this line as much as you need :

Code: Select all

textbutton _("Font name") action [ gui.SetPreference("font_1", "YourFont.otf"), gui.SetPreference("font_2", "YourFont.otf"), gui.SetPreference("font_3", "YourFont.otf")
It can be 3 different font if you wish.

And that's all, I hope it will be useful !

3) Adjust font size
OpenDyslexic is a font really bigger than other traditional font. It can then cause problem (text too big for the texbox, overlaping in game menu and so on). There is a simple solution to fix it, into your gui.rpy just at the begining after the :

Code: Select all

init python:
add (don't forget to indent) :

Code: Select all

config.ftfont_scale["OpenDyslexic.otf"] = .7
It will automaticaly scale.to your actual font. (You can change the number if you want it bigger but .7 is normally a good ratio).

Code: Select all

config.ftfont_vertical_extent_scale["OpenDyslexic.otf"] = .8
Will automatically reduce the line spacing.



(Turorial based on this great doc : https://www.renpy.org/doc/html/gui_advanced.html)
(To use this tutorial, your renpy version should be 6.99.4 or more)
Thanks to Rob in the Renpy discord for the scaling solution

Re: Allow player to change font (Opendyslexic or else)

Posted: Sun Jan 26, 2020 6:14 am
by Ayael
Short update to scale font size thanks to a message from Rob in the Renpy discord !

Re: Allow player to change font (Opendyslexic or else)

Posted: Fri Feb 12, 2021 11:15 am
by Ayael
Little update, it seems you can create custom new gui font. For example, I wanted a different font for my nvl_dialogue text (I believe it take this front the regular text one) so I add in gui.rpy with the other:

Code: Select all

define gui.nvl_dialogue_text_font = gui.preference("font_2", "myfont.ttf")
and then in screen.rpy.

Code: Select all

style nvl_dialogue:
    font gui.nvl_dialogue_text_font
And it works !

Re: Allow player to change font (Opendyslexic or else)

Posted: Sat Feb 13, 2021 10:51 am
by Gouvernathor
I believe code readability could be improved by having "main_font" (or just "font"), "name_font" and "interface_font" instead of "font1", "font2" and "font3".
But that's a detail if the properties aren't accessed anywhere else.

Also since the OpenDyslexic font comes in 4 variants, for bold, italics, both and neither (at least for the version I downloaded), you can add the following to ensure all variants are properly used :

Code: Select all

init python:
    config.font_replacement_map["OpenDyslexic.otf", True, False] = ["OpenDyslexic-Bold.otf", False, False]
    config.font_replacement_map["OpenDyslexic.otf", False, True] = ["OpenDyslexic-Italic.otf", False, False]
    config.font_replacement_map["OpenDyslexic.otf", True, True] = ["OpenDyslexic-BoldItalic.otf", False, False]
The documentation for this part is found here https://www.renpy.org/doc/html/text.htm ... eplacement
After doing that, the config.ftfont_scale line must be applied to each variant.

Great work !

Re: Allow player to change font (Opendyslexic or else)

Posted: Sat Feb 13, 2021 2:38 pm
by Ayael
I didn't know about font variants! Since it works without it, I always assume it was kind of magic, but the right file actually look better, thanks for your contribution!

Re: Allow player to change font (Opendyslexic or else)

Posted: Sat Sep 25, 2021 8:16 am
by Kornyart
this thread is gold, thank you so much for this!

Re: Allow player to change font (Opendyslexic or else)

Posted: Mon Oct 04, 2021 10:39 am
by Nighten
Thanks for the thread, it really helped!
Since Renpy has an accessibility menu built in now, you also can also use the action Preference("font transform", ...) used in this menu; and it's already well configured! You can find the code here on github.

Code: Select all

# copy pasted from the link above
 vbox:

                    label _("Font Override")

                    null height 10

                    textbutton _("Default"):
                        action Preference("font transform", None)
                        style_suffix "radio_button"

                    textbutton _("DejaVu Sans"):
                        action Preference("font transform", "dejavusans")
                        style_suffix "radio_button"

                    textbutton _("Opendyslexic"):
                        action Preference("font transform", "opendyslexic")
                        style_suffix "radio_button"

Re: Allow player to change font (Opendyslexic or else)

Posted: Mon Oct 04, 2021 3:50 pm
by Ayael
Thanks for the notice!