Allow player to change font (Opendyslexic or else)

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Ayael
Regular
Posts: 73
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

Allow player to change font (Opendyslexic or else)

#1 Post by Ayael » Fri Jan 24, 2020 3:32 pm

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).


(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

User avatar
Ayael
Regular
Posts: 73
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

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

#2 Post by Ayael » Sun Jan 26, 2020 6:14 am

Short update to scale font size thanks to a message from Rob in the Renpy discord !

User avatar
Ayael
Regular
Posts: 73
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

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

#3 Post by Ayael » Fri Feb 12, 2021 11:15 am

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 !

User avatar
Gouvernathor
Newbie
Posts: 4
Joined: Thu Aug 06, 2020 9:27 am
Github: Gouvernathor
Contact:

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

#4 Post by Gouvernathor » Sat Feb 13, 2021 10:51 am

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 !

User avatar
Ayael
Regular
Posts: 73
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Anoldor, medieval fantasy otome game.
Contact:

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

#5 Post by Ayael » Sat Feb 13, 2021 2:38 pm

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!

Post Reply

Who is online

Users browsing this forum: Syrale