Adding more implementations and fixes
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package org.saturnclient.ui.resources;
|
||||
|
||||
import org.saturnclient.common.minecraft.MinecraftProvider;
|
||||
import org.saturnclient.common.minecraft.bindings.SaturnIdentifier;
|
||||
|
||||
public class Fonts {
|
||||
public static final SaturnIdentifier INTER = SaturnIdentifier.of("saturnclient", "inter");
|
||||
public static final SaturnIdentifier INTER_BOLD = SaturnIdentifier.of("saturnclient", "inter_bold");
|
||||
public static final SaturnIdentifier DEFAULT = SaturnIdentifier.ofVanilla("default");
|
||||
|
||||
public static SaturnIdentifier getFont(int font) {
|
||||
switch (font) {
|
||||
case 0:
|
||||
return DEFAULT;
|
||||
case 1:
|
||||
return INTER;
|
||||
default:
|
||||
return INTER_BOLD;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getWidth(String text, int font) {
|
||||
int w = 0;
|
||||
for (String line : text.split("\n")) {
|
||||
w = Math.max(w, MinecraftProvider.PROVIDER.getWidth(line, font));
|
||||
}
|
||||
if (font == 0) {
|
||||
return w * 2;
|
||||
} else {
|
||||
return w;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getHeight() {
|
||||
return 18;
|
||||
}
|
||||
|
||||
public static int centerX(int w, String text, int font) {
|
||||
return (w - Fonts.getWidth(text, font)) / 2;
|
||||
}
|
||||
|
||||
public static int centerY(int h) {
|
||||
return (h - 18) / 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.saturnclient.ui.resources;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.kitfox.svg.SVGDiagram;
|
||||
import com.kitfox.svg.SVGUniverse;
|
||||
|
||||
import org.saturnclient.common.minecraft.IMinecraftClient;
|
||||
import org.saturnclient.common.minecraft.MinecraftProvider;
|
||||
import org.saturnclient.common.minecraft.bindings.SaturnIdentifier;
|
||||
|
||||
public class SvgTexture {
|
||||
|
||||
private static List<SaturnIdentifier> svgCache = new ArrayList<>();
|
||||
|
||||
private static BufferedImage renderSvg(InputStream svgStream, int width, int height) throws Exception {
|
||||
// Load SVG
|
||||
SVGUniverse universe = new SVGUniverse();
|
||||
java.net.URI uri = universe.loadSVG(svgStream, "tempSvg");
|
||||
SVGDiagram diagram = universe.getDiagram(uri);
|
||||
|
||||
// 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();
|
||||
|
||||
// Enable anti-aliasing for smooth scaling
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
|
||||
// Apply scaling transform
|
||||
g.scale(scaleX, scaleY);
|
||||
|
||||
// Render SVG
|
||||
diagram.render(g);
|
||||
|
||||
g.dispose();
|
||||
return image;
|
||||
}
|
||||
|
||||
public static SaturnIdentifier getSvg(IMinecraftClient client, SaturnIdentifier svgImage, int width, int height) {
|
||||
SaturnIdentifier id = SaturnIdentifier
|
||||
.of(svgImage.toString().replaceAll("\\.svg$", (width + "_" + height).toString() + ".png"));
|
||||
|
||||
if (svgCache.contains(id)) {
|
||||
return id;
|
||||
}
|
||||
|
||||
try (InputStream svgStream = client.getResource(svgImage)) {
|
||||
|
||||
BufferedImage image = renderSvg(svgStream, width, height);
|
||||
|
||||
MinecraftProvider.PROVIDER.registerBufferedImageTexture(id, image);
|
||||
|
||||
return id;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user