From 9ba9e81686e648e53c319190d92eda2c1891898e Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 28 Dec 2024 09:59:44 +0100 Subject: [PATCH] Implemented Dynamic sized fonts that adjust based on your need. --- .../speiger/src/coreengine/NewInputTest.java | 16 +++++++--- .../coreengine/rendering/gui/font/Font.java | 31 +++++++++---------- .../rendering/gui/font/FontManager.java | 8 ++--- .../rendering/gui/font/FontStyle.java | 18 +++++++++++ .../rendering/gui/font/GlythCache.java | 31 +++++++++++++------ .../assets/base/font/roboto/font.json | 2 +- 6 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/gui/font/FontStyle.java diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index 37240ae..a6846dc 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -80,7 +80,7 @@ public class NewInputTest { assets.addListener(fonts); assets.reload(); - System.out.println("Testing: "+GL.getCapabilities().OpenGL46); + System.out.println("Testing: "+GL.getCapabilities().OpenGL41); System.out.println("Testing: "+Integer.divideUnsigned(-1, 255)); IFontProvider provider = STBTrueTypeProvider.create(AssetLocation.of("font/roboto/font.json"), assets); @@ -150,16 +150,24 @@ public class NewInputTest { builder.pos(-0.5F, 0.5F, 0).tex(0F, 0F).rgba(-1).endVertex(); builder.pos(-0.5F, -0.5F, 0).tex(0F, 1F).rgba(-1).endVertex(); - Font font = fonts.createFont(18F, 1F); + Font font = fonts.createFont(1F); TestModel model = new TestModel(builder.getBytes()); TestModel[] guiModel = new TestModel[1]; List draws = new ObjectArrayList<>(); - font.drawText("The Quick brown fox Jumps over the Lazy dog", 50, 50, -1, new TexturedBuffer((K, V) -> { + TexturedBuffer buffer = new TexturedBuffer((K, V) -> { draws.addAll(K); guiModel[0] = new TestModel(V); System.out.println("Testing: "+V.length+" bytes, "+K.size()); - })); + }); +// String s = "The Quick brown fox Jumps over the Lazy dog"; + + float offset = font.drawText("The Quick ", 50, 50, -1, buffer, false); + offset += font.drawText(Font.DEFAULT.with(25F), "Brown ", 50+offset, 50, -1, buffer, false); + offset += font.drawText(Font.DEFAULT, "Fox ", 50+offset, 50, -1, buffer, false); + offset += font.drawText(Font.DEFAULT.with(10F), "Jumps ", 50+offset, 50, -1, buffer, false); + offset += font.drawText(Font.DEFAULT, "over the Lazy dog", 50+offset, 50, -1, buffer, true); +// font.drawText(s, 50, 50, -1, buffer, true); GLStateTracker tracker = GLStateTracker.instance(); GL11.glClearColor(0.2F, 0.55F, 0.66F, 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 index eba4942..7d1942d 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 @@ -6,28 +6,21 @@ import java.util.function.Consumer; 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.MissingGlyth; import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth.GlythBaker; import speiger.src.coreengine.rendering.tesselation.buffer.IVertexBuilder; public class Font { - private static final AssetLocation DEFAULT = AssetLocation.of("default"); + public static final FontStyle DEFAULT = new FontStyle(AssetLocation.of("default"), 18); Map fonts; GlythBaker baker; - float size; float oversample; GlythCache[] styledCache = new GlythCache[] {new GlythCache(this, 0), new GlythCache(this, 1), new GlythCache(this, 2), new GlythCache(this, 3)}; - MissingGlyth missingGlyth; - GlythData missingData; - protected Font(Map fonts, GlythBaker baker, float size, float oversample, Consumer clearing) { + protected Font(Map fonts, GlythBaker baker, float oversample, Consumer clearing) { this.fonts = fonts; this.baker = baker; - this.size = size; this.oversample = oversample; - this.missingGlyth = new MissingGlyth(size, oversample * 2F); - missingData = new GlythData(missingGlyth); clearing.accept(this::reset); } @@ -35,15 +28,13 @@ public class Font { for(int i = 0;i<4;i++) { styledCache[i].reset(); } - missingGlyth.cleanCache(); - missingData.cleanCache(); } - public GlythData data(AssetLocation font, int codepoint, int style) { + public GlythData data(FontStyle font, int codepoint, int style) { return styledCache[style & 0x3].data(font, codepoint); } - public Glyth glyth(AssetLocation font, int codepoint, int style) { + public Glyth glyth(FontStyle font, int codepoint, int style) { return styledCache[style & 0x3].glyth(font, codepoint); } @@ -51,15 +42,20 @@ public class Font { return fonts.get(font); } - public void drawText(String text, float x, float y, int color, TexturedBuffer buffer) { + public float drawText(String text, float x, float y, int color, TexturedBuffer buffer, boolean end) { + return drawText(DEFAULT, text, x, y, color, buffer, end); + } + + public float drawText(FontStyle style, String text, float x, float y, int color, TexturedBuffer buffer, boolean end) { + float xStart = x; int previousCodepoint = -1; for(int i = 0,m=text.length();i cache = new Object2ObjectOpenHashMap<>(); + Map cache = new Object2ObjectOpenHashMap<>(); public GlythCache(Font font, int style) { this.font = font; @@ -21,28 +21,39 @@ public class GlythCache { } public void reset() { + cache.values().forEach(FontCache::reset); cache.clear(); } - private FontCache cache(AssetLocation font) { + private FontCache cache(FontStyle font) { return cache.computeIfAbsent(font, FontCache::new); } - public GlythData data(AssetLocation fontId, int codepoint) { + public GlythData data(FontStyle fontId, int codepoint) { return cache(fontId).data(codepoint); } - public Glyth glyth(AssetLocation fontId, int codepoint) { + public Glyth glyth(FontStyle fontId, int codepoint) { return cache(fontId).glyth(codepoint); } public class FontCache { Int2ObjectMap data = new Int2ObjectOpenHashMap<>(); Int2ObjectMap glyth = new Int2ObjectOpenHashMap<>(); - AssetLocation fontId; + FontStyle fontStyle; + MissingGlyth missingGlyth; + GlythData missingData; - public FontCache(AssetLocation fontId) { - this.fontId = fontId; + + public FontCache(FontStyle fontStyle) { + this.fontStyle = fontStyle; + this.missingGlyth = new MissingGlyth(fontStyle.size(), font.oversample * 2F); + missingData = new GlythData(missingGlyth); + } + + private void reset() { + missingGlyth.cleanCache(); + missingData.cleanCache(); } public GlythData data(int codepoint) { @@ -54,8 +65,8 @@ public class GlythCache { } private GlythData compute(int codepoint) { - UnbakedGlyth data = font.font(fontId).data(codepoint, style, font.size, font.oversample); - return data == null ? font.missingData : new GlythData(data); + UnbakedGlyth data = font.font(fontStyle.font()).data(codepoint, style, fontStyle.size(), font.oversample); + return data == null ? missingData : new GlythData(data); } private Glyth bake(int codepoint) { diff --git a/src/main/resources/assets/base/font/roboto/font.json b/src/main/resources/assets/base/font/roboto/font.json index 0e4d95e..335c4ed 100644 --- a/src/main/resources/assets/base/font/roboto/font.json +++ b/src/main/resources/assets/base/font/roboto/font.json @@ -4,7 +4,7 @@ "file": "font/roboto/Roboto-Medium.ttf", "oversample": 3, "shadowOffset": 1, - "skip": "s", + "skip": "", "offset": { "x": 0, "y": 0 } } } \ No newline at end of file