From 9caf47d81b112fc3ac22278559ef1c696d42cb09 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 22 Feb 2026 18:11:58 +0100 Subject: [PATCH] Fixed svg issues --- .../ui2/elements/ImageTexture.java | 14 +++++++-- .../ui2/resources/SvgTexture.java | 29 +++++++++++++------ .../saturnclient/ui2/screens/TitleMenu.java | 6 ---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/saturnclient/ui2/elements/ImageTexture.java b/src/main/java/org/saturnclient/ui2/elements/ImageTexture.java index b5d9426..037ef31 100644 --- a/src/main/java/org/saturnclient/ui2/elements/ImageTexture.java +++ b/src/main/java/org/saturnclient/ui2/elements/ImageTexture.java @@ -3,8 +3,9 @@ package org.saturnclient.ui2.elements; import org.saturnclient.ui2.Element; import org.saturnclient.ui2.ElementContext; import org.saturnclient.ui2.RenderScope; +import org.saturnclient.ui2.resources.SvgTexture; -import net.minecraft.client.render.RenderLayer; +import net.minecraft.client.MinecraftClient; import net.minecraft.util.Identifier; public class ImageTexture extends Element { @@ -16,7 +17,16 @@ public class ImageTexture extends Element { @Override public void render(RenderScope renderScope, ElementContext ctx) { - renderScope.setRenderLayer(RenderLayer::getGuiTextured); + if (sprite.toString().endsWith(".svg")) { + MinecraftClient client = MinecraftClient.getInstance(); + + // Get the actual window pixel dimensions for the image + int renderWidth = (int) (width * client.getWindow().getFramebufferWidth() / client.getWindow().getWidth()); + int renderHeight = (int) (height * client.getWindow().getFramebufferHeight() / client.getWindow().getHeight()); + + sprite = SvgTexture.getSvg(client, sprite, renderWidth * 2, renderHeight * 2); + } + renderScope.drawTexture(sprite, 0, 0, 0, 0, width, height); } } diff --git a/src/main/java/org/saturnclient/ui2/resources/SvgTexture.java b/src/main/java/org/saturnclient/ui2/resources/SvgTexture.java index c7a6482..438a190 100644 --- a/src/main/java/org/saturnclient/ui2/resources/SvgTexture.java +++ b/src/main/java/org/saturnclient/ui2/resources/SvgTexture.java @@ -1,7 +1,7 @@ package org.saturnclient.ui2.resources; import java.awt.Graphics2D; -import java.awt.Rectangle; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.InputStream; @@ -18,20 +18,31 @@ import net.minecraft.util.Identifier; public class SvgTexture { private static BufferedImage renderSvg(InputStream svgStream, int width, int height) throws Exception { - // Create SVGUniverse and load the SVG + // Load SVG SVGUniverse universe = new SVGUniverse(); java.net.URI uri = universe.loadSVG(svgStream, "tempSvg"); SVGDiagram diagram = universe.getDiagram(uri); - // Create a BufferedImage to render the SVG - BufferedImage image = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB); + // Get intrinsic size of SVG + float svgWidth = (float) diagram.getWidth(); + float svgHeight = (float) diagram.getHeight(); + + // Compute scale factors + float scaleX = width / svgWidth; + float scaleY = height / svgHeight; + + // Create output image + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); - // Set the diagram's viewport to match the desired size - diagram.setIgnoringClipHeuristic(true); - diagram.setDeviceViewport(new Rectangle(0, 0, width, height)); + // Enable anti-aliasing for smooth scaling + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); - // Render the SVG onto the BufferedImage + // Apply scaling transform + g.scale(scaleX, scaleY); + + // Render SVG diagram.render(g); g.dispose(); @@ -40,7 +51,7 @@ public class SvgTexture { @Nullable @SuppressWarnings("resource") - public static Identifier getSvg(MinecraftClient client, Identifier svgImage, int width, int height, float scale) { + public static Identifier getSvg(MinecraftClient client, Identifier svgImage, int width, int height) { try (InputStream svgStream = client .getResourceManager() .getResource(svgImage).get() diff --git a/src/main/java/org/saturnclient/ui2/screens/TitleMenu.java b/src/main/java/org/saturnclient/ui2/screens/TitleMenu.java index a132199..ebffa12 100644 --- a/src/main/java/org/saturnclient/ui2/screens/TitleMenu.java +++ b/src/main/java/org/saturnclient/ui2/screens/TitleMenu.java @@ -14,12 +14,10 @@ import org.saturnclient.ui2.elements.Button; import org.saturnclient.ui2.elements.ImageTexture; import org.saturnclient.ui2.elements.TextureButton; import org.saturnclient.ui2.resources.Fonts; -import org.saturnclient.ui2.resources.SvgTexture; import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; import net.minecraft.client.gui.screen.option.OptionsScreen; import net.minecraft.client.gui.screen.world.SelectWorldScreen; -import net.minecraft.util.Identifier; public class TitleMenu extends SaturnScreen { public TitleMenu() { @@ -72,9 +70,5 @@ public class TitleMenu extends SaturnScreen { }).dimensions(s, s).position((s + 2) * 2, 0).animation(new SlideFade(1000, s + 8))); draw(saturnStagger.dimensions(((s + 2) * 2) + s, s).centerHorizontal(width, height, 0, 8)); - - draw(new ImageTexture( - SvgTexture.getSvg(client, Identifier.of("saturnclient", "textures/gui/test.svg"), 98, 98, 1.0f)) - .dimensions(98, 98)); } }