Working svg renderer
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package org.saturnclient.ui2.resources;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.batik.transcoder.TranscoderInput;
|
||||
import org.apache.batik.transcoder.image.ImageTranscoder;
|
||||
import com.kitfox.svg.SVGDiagram;
|
||||
import com.kitfox.svg.SVGUniverse;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.saturnclient.saturnclient.SaturnClient;
|
||||
import org.saturnclient.saturnclient.cosmetics.cloaks.utils.IdentifierUtils;
|
||||
@@ -13,30 +16,26 @@ import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public class SvgTexture {
|
||||
private static BufferedImage renderSvg(InputStream svgStream, float width, float height) throws Exception {
|
||||
TranscoderInput input = new TranscoderInput(svgStream);
|
||||
|
||||
final BufferedImage[] imagePointer = new BufferedImage[1];
|
||||
private static BufferedImage renderSvg(InputStream svgStream, int width, int height) throws Exception {
|
||||
// Create SVGUniverse and load the SVG
|
||||
SVGUniverse universe = new SVGUniverse();
|
||||
java.net.URI uri = universe.loadSVG(svgStream, "tempSvg");
|
||||
SVGDiagram diagram = universe.getDiagram(uri);
|
||||
|
||||
ImageTranscoder transcoder = new ImageTranscoder() {
|
||||
// Create a BufferedImage to render the SVG
|
||||
BufferedImage image = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g = image.createGraphics();
|
||||
|
||||
@Override
|
||||
public BufferedImage createImage(int w, int h) {
|
||||
return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
||||
}
|
||||
// Set the diagram's viewport to match the desired size
|
||||
diagram.setIgnoringClipHeuristic(true);
|
||||
diagram.setDeviceViewport(new Rectangle(0, 0, width, height));
|
||||
|
||||
@Override
|
||||
public void writeImage(BufferedImage img, org.apache.batik.transcoder.TranscoderOutput out) {
|
||||
imagePointer[0] = img;
|
||||
}
|
||||
};
|
||||
// Render the SVG onto the BufferedImage
|
||||
diagram.render(g);
|
||||
|
||||
transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, width);
|
||||
transcoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, height);
|
||||
|
||||
transcoder.transcode(input, null);
|
||||
|
||||
return imagePointer[0];
|
||||
g.dispose();
|
||||
return image;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -58,8 +57,7 @@ public class SvgTexture {
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,12 @@ 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() {
|
||||
@@ -70,5 +72,9 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user