Font aliases

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
Gouvernathor
Newbie
Posts: 23
Joined: Thu Aug 06, 2020 9:27 am
Github: Gouvernathor
Discord: Armise#6515
Contact:

Font aliases

#1 Post by Gouvernathor »

Just a one-line code snippet.
One thing a bit annoying in ren'py is that when you want just some words in a sentence to display in a different font, you have to use the {font} tag, and that text tag forces you to type in the whole path to the font file.
Or does it ?

There's a feature in Ren'Py that's not exactly there for that purpose, but we can take it to our advantage. Take for example the Electroharmonix beautiful font, inspired by japanese characters.

Code: Select all

define config.font_replacement_map |= {("jap", a, b) : ("fonts/electroharmonix.ttf", a, b) for a in (True, False) for b in (True, False)}
This code, while a bit long and surprisingly complex, serves its purpose well. Instead of `"jap"`, put the alias you want to give your font. Instead of `"fonts/electroharmonix.ttf"`, put the path to the font file (relative to the game/ folder, as always). Do not modify anything else, unless you know what you're doing.

This allows the following dialogue to be much simpler than it would otherwise need to be :

Code: Select all

e "What does this say ? {font=jap}Speak, friend, and{/font}..."
g "{font=jap}and enter{/font}. It definitely says {font=jap}and enter{/font}."
e "But how do you say {font=jap}friend{/font} in normal english ?"
...
Warning : as of 7.5/8.0, only paths to actual fonts can be provided. FontGroups are not supported.

Technical explanations :
This actually remaps all four variants of the non-existing "jap" font (bold, italics, bold-italics and roman) to an actual font, the one at the provided path. The variants are determined by the a and b values, and since we're setting all four of them... See the doc.
Note though that any remapping taking the original file as a key will not work (but at the same time, would be pointless if the alias exists). In other words, there will not be two successive replacements.

Post Reply

Who is online

Users browsing this forum: No registered users