Class JavaFxFontLoader

java.lang.Object
megalodonte.platform.javafx.JavaFxFontLoader

public final class JavaFxFontLoader extends Object
Auto-discovers and registers font files with JavaFX, so their family names become usable as -fx-font-family (and ThemeTypography.fontFamily()) without the application having to load each file by hand.

Convention: client applications should place their font files (.ttf/.otf) under a assets/fonts/ resource directory (e.g. src/main/resources/assets/fonts/roboto/Roboto-Regular.ttf). Subdirectories are scanned recursively, so families can be organized into their own folders. Nothing needs to be registered per file or per family — dropping a font file under that directory is enough for the application to be able to use its family name.

This runs automatically once at startup (see Bootstrap.dispatch(Class, Stage, String[])), so applications built on this framework normally never need to call it directly. It's safe to call more than once (JavaFX simply re-registers the same family) and it's a no-op — not an error — when the directory doesn't exist, so apps that don't ship custom fonts pay no cost.

  • Field Details

    • DEFAULT_FONTS_DIRECTORY

      public static final String DEFAULT_FONTS_DIRECTORY
      Conventional classpath resource directory client apps should place their fonts under.
      See Also:
  • Method Details

    • loadAll

      public static int loadAll()
      Scans DEFAULT_FONTS_DIRECTORY (on every classpath root — app jar, dependency jars, exploded classes dir when running from an IDE) and registers every font file found with Font.loadFont(InputStream, double).
      Returns:
      how many font files were successfully registered
    • loadAll

      public static int loadAll(String resourceDirectory)
      Same as loadAll(), but scanning a custom resource directory instead of the conventional DEFAULT_FONTS_DIRECTORY.