From b2b8601f0cf2169a548170caaf222b392bce7a8f Mon Sep 17 00:00:00 2001 From: lieght <49651652+BAFGdeJong@users.noreply.github.com> Date: Wed, 1 Oct 2025 04:30:35 +0200 Subject: [PATCH] Added methods to FontAsset to make it usable. --- .../org/toop/framework/asset/AssetLoader.java | 2 + .../framework/asset/resources/FontAsset.java | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/toop/framework/asset/AssetLoader.java b/framework/src/main/java/org/toop/framework/asset/AssetLoader.java index 397a81a..2a622fb 100644 --- a/framework/src/main/java/org/toop/framework/asset/AssetLoader.java +++ b/framework/src/main/java/org/toop/framework/asset/AssetLoader.java @@ -10,6 +10,7 @@ import java.util.function.Function; import org.reflections.Reflections; import org.toop.framework.asset.resources.FileExtension; +import org.toop.framework.asset.resources.FontAsset; public class AssetLoader { @@ -58,6 +59,7 @@ public class AssetLoader { BaseResource resource = resourceMapper(fileEntry, BaseResource.class); if (resource != null) { assets.add(new Asset<>(fileEntry.getName(), resource)); + if (resource instanceof FontAsset fontAsset) fontAsset.load(); // Autoload if is a FONT for easy css support. } } } diff --git a/framework/src/main/java/org/toop/framework/asset/resources/FontAsset.java b/framework/src/main/java/org/toop/framework/asset/resources/FontAsset.java index db4ad84..16570de 100644 --- a/framework/src/main/java/org/toop/framework/asset/resources/FontAsset.java +++ b/framework/src/main/java/org/toop/framework/asset/resources/FontAsset.java @@ -1,8 +1,13 @@ package org.toop.framework.asset.resources; +import javafx.scene.text.Font; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +@FileExtension({"ttf", "otf"}) public class FontAsset extends BaseResource implements LoadableResource { + private String family; public FontAsset(final File fontFile) { super(fontFile); @@ -10,11 +15,25 @@ public class FontAsset extends BaseResource implements LoadableResource { @Override public void load() { - this.isLoaded = true; + if (!this.isLoaded) { + try (FileInputStream fis = new FileInputStream(this.file)) { + // Register font with JavaFX + Font font = Font.loadFont(fis, 12); // Default preview size + if (font == null) { + throw new RuntimeException("Failed to load font: " + this.file); + } + this.family = font.getFamily(); // Save family name for CSS / future use + this.isLoaded = true; + } catch (IOException e) { + throw new RuntimeException("Error reading font file: " + this.file, e); + } + } } @Override public void unload() { + // Font remains globally registered with JavaFX, but we just forget it locally + this.family = null; this.isLoaded = false; } @@ -22,4 +41,20 @@ public class FontAsset extends BaseResource implements LoadableResource { public boolean isLoaded() { return this.isLoaded; } -} + + /** Get a new font instance with the given size */ + public Font getFont(double size) { + if (!this.isLoaded) { + load(); + } + return Font.font(this.family, size); + } + + /** Get the family name (for CSS usage) */ + public String getFamily() { + if (!this.isLoaded) { + load(); + } + return this.family; + } +} \ No newline at end of file