Skip to content

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.

One family in two weights, for the title, the body text, the button and the tooltip.
  • Text nodes and text styles: Text.
  • Loading a font and its weights: Fonts.
  • The font files (.ttf or .otf), one per weight, in the resources of your application. This guide uses Montserrat Regular and Bold.

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.

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.

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

Full file: FontGuideUI.java

Check: the title is bold, the body text regular, and the text of the button dark.

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

Full file: FontGuideUI.java

Style the window UI bridge lists the other settings of the tooltips.

Check: hover the Save button: its tooltip shows in Montserrat.

  1. The first access to FontGuideStyle throws an ExceptionInInitializerError caused by IllegalArgumentException: 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).
  2. join() throws a CompletionException that says the atlas cannot be generated: the file is not a valid TrueType or OpenType font, or the cache folder cannot be written.
  3. 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.
  4. 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.
  5. A setter on a shared style throws IllegalStateException: This TextInfo is locked: the style is locked on purpose; call its with... copy instead.

Report a problem with this page