Replace a font
JOID ships no font for your application: every text needs a font that you load. This guide loads one family once, defines the text styles of the application from it, and uses them in every UI and in the tooltips.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- Text nodes and text styles: Text.
- Loading a font and its weights: Fonts.
- The font files (
.ttfor.otf), one per weight, in the resources of your application. This guide uses Montserrat Regular and Bold.
1. Load the font once
Section titled “1. Load the font once”Load every face of the family in one call to MsdfFontLoader.load, in a constant that the whole application shares:
public static final MsdfFont FONT = MsdfFontLoader.load( "/fonts/Montserrat/Montserrat-Regular.ttf", "/fonts/Montserrat/Montserrat-Bold.ttf").join();Full file: FontGuideStyle.java
join() waits until the faces are ready.
The first launch on a machine generates a cache for each face, which takes a few seconds; later launches read it (What happens when a font loads).
Check: run any UI that reads FontGuideStyle.FONT: the dev jar prints [JOID] Font … generated into the msdf cache for each face on the first run, then read from the msdf cache.
2. Define the styles of the application
Section titled “2. Define the styles of the application”Define each TextInfo once, next to the font, and lock it with lock():
private static final Color TEXT = new Color(231, 233, 238).lock();
public static final TextInfo BODY = TextInfo.create(FontGuideStyle.FONT, 24F).color(FontGuideStyle.TEXT).lock();public static final TextInfo TITLE = FontGuideStyle.BODY.withFontSize(40F).withWeight(FontWeight.BOLD).lock();public static final TextInfo TOOLTIP = FontGuideStyle.BODY.withFontSize(20F).withColor(FontGuideStyle.TEXT).lock();Full file: FontGuideStyle.java
A locked style refuses its setters, and a variant is a with... copy (Describe a style).
Check: with the red of Color, FontGuideStyle.BODY.color(Color.RED) throws an IllegalStateException, and FontGuideStyle.BODY.withColor(Color.RED) returns a red copy.
3. Use the styles in your UIs
Section titled “3. Use the styles in your UIs”Every text takes one of the styles, and the dark text of the button starts from a copy:
RectNode.create(660, 340, 600, 400).color(FontGuideUI.SURFACE).effect(RoundedNodeEffect.create(12F)).effect(BorderNodeEffect.create(FontGuideUI.ELEMENT, 1.5F)).body(card -> { TextNode.create(50, 50).text(Text.create("Settings", FontGuideStyle.TITLE)).attach(card); TextNode.create(50, 130).text(Text.create("Every text uses one font.", FontGuideStyle.BODY)).attach(card); RectNode .create(50, 290, 200, 60) .color(FontGuideUI.ACCENT) .label(Text.create("Save", FontGuideStyle.BODY.withColor(FontGuideUI.ON_ACCENT))) .hover(() -> "Saves the settings") .attach(card);}).attach(this);Check: the title is bold, the body text regular, and the text of the button dark.
4. Use the font for the tooltips
Section titled “4. Use the font for the tooltips”The window UI bridge draws tooltips only once it has a text style. Give it one after the backend is registered, for example when your first UI opens:
BridgeHandler.UI.getBridge(WindowUIBridge.class).hoverInfo(FontGuideStyle.TOOLTIP);Style the window UI bridge lists the other settings of the tooltips.
Check: hover the Save button: its tooltip shows in Montserrat.
If it does not work
Section titled “If it does not work”- The first access to
FontGuideStylethrows anExceptionInInitializerErrorcaused byIllegalArgumentException: The asset … is not found in the classpath nor as a file: the path is wrong. Check that the files are in the resources, under that path from their root (a leading/changes nothing). join()throws aCompletionExceptionthat says the atlas cannot be generated: the file is not a valid TrueType or OpenType font, or the cache folder cannot be written.- A text shows in another weight, and the dev jar prints
[JOID] The font weight … is not loaded in the family of …: the style asks for a weight that the family lacks; load that face, or use a loaded weight. - No tooltip appears, and the dev jar prints
[JOID] WindowUIBridge draws no tooltip, set its hoverInfo(TextInfo) to draw them: the window UI bridge has no text style yet. - A setter on a shared style throws
IllegalStateException: This TextInfo is locked: the style is locked on purpose; call itswith...copy instead.
