From 2dae61adbfbbb814f743c6166853fdb8c360596c Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 29 Jul 2024 16:34:43 +0200 Subject: [PATCH] More Progress on the font system --- .../speiger/src/coreengine/NewInputTest.java | 10 +++- .../src/coreengine/assets/AssetFilter.java | 35 +++++++++++ .../src/coreengine/assets/AssetLocation.java | 2 + .../coreengine/rendering/gui/font/Font.java | 45 ++++---------- .../rendering/gui/font/FontGroup.java | 6 +- .../rendering/gui/font/FontManager.java | 23 ++++--- .../rendering/gui/font/GlythCache.java | 60 +++++++++++++++++++ .../font/providers/STBTrueTypeProvider.java | 10 +++- .../assets/base/font/roboto/font.json | 2 +- 9 files changed, 145 insertions(+), 48 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/assets/AssetFilter.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/GlythCache.java diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index 08fee3e..2d0214c 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -77,6 +77,7 @@ public class NewInputTest { int size = 512; int half = size >> 1; + int base = size >> 3; DynamicTexture texture = new DynamicTexture(size, size, DynamicTexture.DEFAULT_PARAMETERS); texture.fill(0, 0, size, size, -1); @@ -86,18 +87,20 @@ public class NewInputTest { texture.fill(0, half, half, half, 0, 0, 255, 255); texture.process(true); window.visible(true); - + var instance = bit.getCharacter("C".codePointAt(0), false); + System.out.println("Testing: "+instance.getWidth()+","+instance.getHeight()); bus.register(KeyEvent.Key.class, T -> { if(T.key() == GLFW.GLFW_KEY_T) { T.cancel(); texture.bind(); Drawable drawable = new Drawable(GLTextureFormat.RGBA, half, half); drawable.fill(0, 0, half, half, 255, 255, 0, 255); - drawable.upload(192, 192, 64, 64, 128, 128); + drawable.upload(base * 3, base * 3, base, base, base * 2, base * 2); drawable.close(); } else if(T.key() == GLFW.GLFW_KEY_Z) { T.cancel(); + System.out.println("Testing"); texture.bind(); UnbakedGlyth data = provider.glythData("C".codePointAt(0), 0); data.bake(new GlythBaker() { @@ -105,7 +108,8 @@ public class NewInputTest { public Glyth bake(IGlythSheetInfo info) { int width = info.width(); int height = info.height(); - info.upload(half - (width >> 2), half - (height >> 2)); + info.upload(half - (width >> 1), half - (height >> 1)); + System.out.println("Test2: "+width+", "+height); return null; } }, 1F, 1F); diff --git a/src/main/java/speiger/src/coreengine/assets/AssetFilter.java b/src/main/java/speiger/src/coreengine/assets/AssetFilter.java new file mode 100644 index 0000000..3995087 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/assets/AssetFilter.java @@ -0,0 +1,35 @@ +package speiger.src.coreengine.assets; + +import java.util.Map; + +import speiger.src.coreengine.assets.base.IAsset; +import speiger.src.coreengine.assets.base.IAssetProvider; +import speiger.src.coreengine.assets.base.MultiAsset; + +public record AssetFilter(String prefix, String extension) { + public static AssetFilter json(String prefix) { + return new AssetFilter(prefix, ".json"); + } + + public AssetLocation id(AssetLocation file) { + String location = file.location(); + return AssetLocation.of(file.domain(), location.substring(prefix().length()+1, location.length()-extension().length())); + } + + public AssetLocation file(AssetLocation id) { + return AssetLocation.of(id.domain(), prefix()+"/"+id.location()+extension()); + } + + public Map list(IAssetProvider provider) { + return provider.listAssets(prefix, T -> T.endsWith(extension)); + } + + public Map listRoot(IAssetProvider provider) { + return provider.listAssets(prefix, T -> T.endsWith(extension) && !T.isInFolder(prefix)); + } + + public Map multi(IAssetProvider provider) { + return provider.listAllAssets(prefix, T -> T.endsWith(extension)); + } + +} diff --git a/src/main/java/speiger/src/coreengine/assets/AssetLocation.java b/src/main/java/speiger/src/coreengine/assets/AssetLocation.java index 7a85a5e..4de8426 100644 --- a/src/main/java/speiger/src/coreengine/assets/AssetLocation.java +++ b/src/main/java/speiger/src/coreengine/assets/AssetLocation.java @@ -23,6 +23,8 @@ public final class AssetLocation implements Comparable { public String location() { return location; } public String actualLocation() { return "assets/"+domain+"/"+location; } public boolean endsWith(String suffix) { return location.endsWith(suffix); } + public boolean isInFolder() { return location.contains("/"); } + public boolean isInFolder(String prefix) { return location.indexOf("/", prefix.length()+1) >= 0; } public AssetLocation subAsset(String subPath) { return of(domain+":"+location+"/"+subPath); diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java index 636e7ca..b1d2417 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java @@ -1,56 +1,35 @@ package speiger.src.coreengine.rendering.gui.font; -import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap; -import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap; +import java.util.Map; + +import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.rendering.gui.font.glyth.Glyth; import speiger.src.coreengine.rendering.gui.font.glyth.GlythData; import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth.GlythBaker; public class Font { - FontGroup fonts; + Map fonts; GlythBaker baker; float size; float oversample; - GlythCache[] styledCache = new GlythCache[] {new GlythCache(0), new GlythCache(1), new GlythCache(2), new GlythCache(3)}; + GlythCache[] styledCache = new GlythCache[] {new GlythCache(this, 0), new GlythCache(this, 1), new GlythCache(this, 2), new GlythCache(this, 3)}; - private Font(FontGroup fonts, GlythBaker baker, float size, float oversample) { + protected Font(Map fonts, GlythBaker baker, float size, float oversample) { this.fonts = fonts; this.baker = baker; this.size = size; this.oversample = oversample; } - public GlythData data(int codepoint, int style) { - return styledCache[style & 0x3].data(codepoint); + public GlythData data(AssetLocation font, int codepoint, int style) { + return styledCache[style & 0x3].data(font, codepoint); } - public Glyth glyth(int codepoint, int style) { - return styledCache[style & 0x3].glyth(codepoint); + public Glyth glyth(AssetLocation font, int codepoint, int style) { + return styledCache[style & 0x3].glyth(font, codepoint); } - private class GlythCache { - final int style; - Int2ObjectMap data = new Int2ObjectOpenHashMap<>(); - Int2ObjectMap glyth = new Int2ObjectOpenHashMap<>(); - - public GlythCache(int style) { - this.style = style; - } - - public GlythData data(int codepoint) { - return data.computeIfAbsent(codepoint, this::compute); - } - - public Glyth glyth(int codepoint) { - return glyth.computeIfAbsent(codepoint, this::bake); - } - - private GlythData compute(int codepoint) { - return new GlythData(fonts.data(codepoint, style), size); - } - - private Glyth bake(int codepoint) { - return fonts.data(codepoint, style).bake(baker, size, oversample); - } + protected FontGroup font(AssetLocation font) { + return fonts.get(font); } } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java index c5136e1..c02dd87 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontGroup.java @@ -18,8 +18,8 @@ public class FontGroup { this.providers = providers; } - public UnbakedGlyth data(int codepoint, int style) { - return cache[style & 0x3].data(codepoint); + public UnbakedGlyth unbaked(int codepoint, int style) { + return cache[style & 0x3].unbaked(codepoint); } public void close() { @@ -35,7 +35,7 @@ public class FontGroup { this.style = style; } - public UnbakedGlyth data(int codepoint) { + public UnbakedGlyth unbaked(int codepoint) { return dataGlyths.computeIfAbsent(codepoint, this::compute); } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontManager.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontManager.java index e1deb19..6eb38e6 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/FontManager.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/FontManager.java @@ -6,16 +6,19 @@ import java.util.Map; import com.google.gson.JsonObject; import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.collections.objects.maps.interfaces.Object2ObjectMap; import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.base.IAssetProvider; import speiger.src.coreengine.assets.base.SteppedReloadableAsset; import speiger.src.coreengine.rendering.gui.font.glyth.Glyth; import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; +import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider; -public class FontManager extends SteppedReloadableAsset>> { +public class FontManager extends SteppedReloadableAsset>> { private static final int TEXTURE_SIZE = 2048; + Map fonts = Object2ObjectMap.builder().linkedMap(); List textures = new ObjectArrayList<>(); - + @Override public String getName() { return null; } @@ -25,16 +28,23 @@ public class FontManager extends SteppedReloadableAsset> prepare(IAssetProvider provider) { - + protected Map> prepare(IAssetProvider provider) { return null; } @Override - protected void apply(Map> value, IAssetProvider provider) { + protected void apply(Map> value, IAssetProvider provider) { } + public Font createFont(float size) { + return createFont(size, 1F); + } + + public Font createFont(float size, float oversample) { + return new Font(fonts, this::stitch, size, oversample); + } + private Glyth stitch(IGlythSheetInfo info) { for(int i = 0,m=textures.size();i cache = new Object2ObjectOpenHashMap<>(); + + public GlythCache(Font font, int style) { + this.font = font; + this.style = style; + } + + private FontCache cache(AssetLocation font) { + return cache.computeIfAbsent(font, FontCache::new); + } + + public GlythData data(AssetLocation fontId, int codepoint) { + return cache(fontId).data(codepoint); + } + + public Glyth glyth(AssetLocation fontId, int codepoint) { + return cache(fontId).glyth(codepoint); + } + + public class FontCache { + Int2ObjectMap data = new Int2ObjectOpenHashMap<>(); + Int2ObjectMap glyth = new Int2ObjectOpenHashMap<>(); + AssetLocation fontId; + + public FontCache(AssetLocation fontId) { + this.fontId = fontId; + } + + public GlythData data(int codepoint) { + return data.computeIfAbsent(codepoint, this::compute); + } + + public Glyth glyth(int codepoint) { + return glyth.computeIfAbsent(codepoint, this::bake); + } + + private GlythData compute(int codepoint) { + return new GlythData(font.font(fontId).unbaked(codepoint, style), font.size); + } + + private Glyth bake(int codepoint) { + return font.font(fontId).unbaked(codepoint, style).bake(font.baker, font.size, font.oversample); + } + + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/STBTrueTypeProvider.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/STBTrueTypeProvider.java index b465e9e..dba0a59 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/STBTrueTypeProvider.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/STBTrueTypeProvider.java @@ -22,6 +22,7 @@ import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth; import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth.EmptyGlythData; import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; import speiger.src.coreengine.rendering.textures.custom.Drawable; +import speiger.src.coreengine.rendering.utils.values.textures.GLTextureFormat; import speiger.src.coreengine.utils.helpers.JsonUtil; public class STBTrueTypeProvider implements IFontProvider { @@ -192,7 +193,14 @@ public class STBTrueTypeProvider implements IFontProvider { public void upload(int x, int y) { Drawable drawable = new Drawable(FontTexture.formatByColor(false), width, height); drawable.drawFont(owner.info, glyth, owner.xOff, owner.yOff, 0, 0, width, height, owner.pointScale, owner.pointScale); - drawable.upload(x, y, 0, 0, width, height); + Drawable upload = new Drawable(GLTextureFormat.RGBA, width, height); + int pattern = Integer.divideUnsigned(-1, 255); + for(int i = 0,m=width*height;i