From 7fdbe645e42a5af7fdca6057370f6b8db446c4f9 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 26 Jul 2024 17:36:08 +0200 Subject: [PATCH] Started the rework of the font system so the system works better. --- .../speiger/src/coreengine/NewInputTest.java | 9 ++- .../coreengine/rendering/gui/font/Font.java | 56 +++++++++++++++++++ .../rendering/gui/font/FontGroup.java | 37 +++--------- .../rendering/gui/font/glyth/Glyth.java | 11 +++- .../rendering/gui/font/glyth/GlythData.java | 31 ++++++---- .../gui/font/glyth/UnbakedGlyth.java | 22 ++++++++ .../gui/font/providers/BitmapProivder.java | 23 ++++---- .../gui/font/providers/IFontProvider.java | 4 +- .../font/providers/STBTrueTypeProvider.java | 18 +++--- 9 files changed, 143 insertions(+), 68 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/glyth/UnbakedGlyth.java diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index d5b843d..08fee3e 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -14,8 +14,8 @@ import speiger.src.coreengine.assets.base.IAsset; import speiger.src.coreengine.assets.base.IAssetPackage; import speiger.src.coreengine.assets.base.IAssetProvider; 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.GlythData.GlythBaker; +import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth; +import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth.GlythBaker; import speiger.src.coreengine.rendering.gui.font.glyth.IGlythSheetInfo; import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider; import speiger.src.coreengine.rendering.gui.font.providers.STBTrueTypeProvider; @@ -99,7 +99,7 @@ public class NewInputTest { else if(T.key() == GLFW.GLFW_KEY_Z) { T.cancel(); texture.bind(); - GlythData data = provider.glythData("C".codePointAt(0), 0); + UnbakedGlyth data = provider.glythData("C".codePointAt(0), 0); data.bake(new GlythBaker() { @Override public Glyth bake(IGlythSheetInfo info) { @@ -108,8 +108,7 @@ public class NewInputTest { info.upload(half - (width >> 2), half - (height >> 2)); return null; } - }); - System.out.println("Testing: "+data.advance()+", "+bit.getCharacter("C".codePointAt(0), false).getXAdvance()); + }, 1F, 1F); } }); 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 new file mode 100644 index 0000000..636e7ca --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java @@ -0,0 +1,56 @@ +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 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; + GlythBaker baker; + float size; + float oversample; + GlythCache[] styledCache = new GlythCache[] {new GlythCache(0), new GlythCache(1), new GlythCache(2), new GlythCache(3)}; + + private Font(FontGroup 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 Glyth glyth(int codepoint, int style) { + return styledCache[style & 0x3].glyth(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); + } + } +} 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 91451b5..c5136e1 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 @@ -5,31 +5,23 @@ import java.util.List; import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap; import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap; 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.GlythData.GlythBaker; +import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth; import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider; public class FontGroup { AssetLocation locations; List providers; - GlythBaker baker; StyledFont[] cache = new StyledFont[] {new StyledFont(0), new StyledFont(1), new StyledFont(2), new StyledFont(3)}; - public FontGroup(AssetLocation locations, List providers, GlythBaker baker) { + public FontGroup(AssetLocation locations, List providers) { this.locations = locations; this.providers = providers; - this.baker = baker; } - public GlythData data(int codepoint, int style) { + public UnbakedGlyth data(int codepoint, int style) { return cache[style & 0x3].data(codepoint); } - public Glyth glyth(int codepoint, int style) { - return cache[style & 0x3].glyth(codepoint); - } - public void close() { providers.forEach(IFontProvider::close); providers.clear(); @@ -37,35 +29,22 @@ public class FontGroup { private class StyledFont { final int style; - Int2ObjectMap bakedGlyths = new Int2ObjectOpenHashMap<>(); - Int2ObjectMap dataGlyths = new Int2ObjectOpenHashMap<>(); + Int2ObjectMap dataGlyths = new Int2ObjectOpenHashMap<>(); public StyledFont(int style) { this.style = style; } - public GlythData data(int codepoint) { + public UnbakedGlyth data(int codepoint) { return dataGlyths.computeIfAbsent(codepoint, this::compute); } - - public Glyth glyth(int codepoint) { - return bakedGlyths.computeIfAbsent(codepoint, this::bake); - } - - private GlythData compute(int codepoint) { + + private UnbakedGlyth compute(int codepoint) { for(int i = 0,m=providers.size();i