mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Added methods to FontAsset to make it usable.
This commit is contained in:
@@ -10,6 +10,7 @@ import java.util.function.Function;
|
|||||||
|
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.toop.framework.asset.resources.FileExtension;
|
import org.toop.framework.asset.resources.FileExtension;
|
||||||
|
import org.toop.framework.asset.resources.FontAsset;
|
||||||
|
|
||||||
public class AssetLoader {
|
public class AssetLoader {
|
||||||
|
|
||||||
@@ -58,6 +59,7 @@ public class AssetLoader {
|
|||||||
BaseResource resource = resourceMapper(fileEntry, BaseResource.class);
|
BaseResource resource = resourceMapper(fileEntry, BaseResource.class);
|
||||||
if (resource != null) {
|
if (resource != null) {
|
||||||
assets.add(new Asset<>(fileEntry.getName(), resource));
|
assets.add(new Asset<>(fileEntry.getName(), resource));
|
||||||
|
if (resource instanceof FontAsset fontAsset) fontAsset.load(); // Autoload if is a FONT for easy css support.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package org.toop.framework.asset.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
|
import javafx.scene.text.Font;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@FileExtension({"ttf", "otf"})
|
||||||
public class FontAsset extends BaseResource implements LoadableResource {
|
public class FontAsset extends BaseResource implements LoadableResource {
|
||||||
|
private String family;
|
||||||
|
|
||||||
public FontAsset(final File fontFile) {
|
public FontAsset(final File fontFile) {
|
||||||
super(fontFile);
|
super(fontFile);
|
||||||
@@ -10,11 +15,25 @@ public class FontAsset extends BaseResource implements LoadableResource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load() {
|
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
|
@Override
|
||||||
public void unload() {
|
public void unload() {
|
||||||
|
// Font remains globally registered with JavaFX, but we just forget it locally
|
||||||
|
this.family = null;
|
||||||
this.isLoaded = false;
|
this.isLoaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,4 +41,20 @@ public class FontAsset extends BaseResource implements LoadableResource {
|
|||||||
public boolean isLoaded() {
|
public boolean isLoaded() {
|
||||||
return this.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user