Center svg bounds
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package org.example.petshopdesktop.ui;
|
||||
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.scene.paint.Paint;
|
||||
import javafx.scene.shape.Circle;
|
||||
@@ -26,12 +25,13 @@ public final class SvgNodeLoader {
|
||||
}
|
||||
|
||||
public static StackPane loadSquare(String resourcePath, double size) {
|
||||
SvgData svgData = readSvg(resourcePath);
|
||||
Group translated = new Group(svgData.content());
|
||||
translated.setTranslateX(-svgData.minX());
|
||||
translated.setTranslateY(-svgData.minY());
|
||||
Group content = readSvg(resourcePath);
|
||||
var bounds = content.getLayoutBounds();
|
||||
Group translated = new Group(content);
|
||||
translated.setTranslateX(-bounds.getMinX());
|
||||
translated.setTranslateY(-bounds.getMinY());
|
||||
|
||||
double scale = Math.min(size / svgData.width(), size / svgData.height());
|
||||
double scale = Math.min(size / bounds.getWidth(), size / bounds.getHeight());
|
||||
translated.setScaleX(scale);
|
||||
translated.setScaleY(scale);
|
||||
|
||||
@@ -42,17 +42,16 @@ public final class SvgNodeLoader {
|
||||
return pane;
|
||||
}
|
||||
|
||||
private static SvgData readSvg(String resourcePath) {
|
||||
private static Group readSvg(String resourcePath) {
|
||||
try (Reader reader = requireResource(resourcePath)) {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setNamespaceAware(false);
|
||||
Document document = factory.newDocumentBuilder().parse(new InputSource(reader));
|
||||
Element root = document.getDocumentElement();
|
||||
double[] viewBox = parseViewBox(root.getAttribute("viewBox"));
|
||||
Map<String, Map<String, String>> styles = parseStyles(root);
|
||||
Group content = new Group();
|
||||
appendChildren(root, content, styles);
|
||||
return new SvgData(content, viewBox[0], viewBox[1], viewBox[2], viewBox[3]);
|
||||
return content;
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Unable to load SVG: " + resourcePath, e);
|
||||
}
|
||||
@@ -66,19 +65,6 @@ public final class SvgNodeLoader {
|
||||
return new InputStreamReader(stream, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static double[] parseViewBox(String viewBox) {
|
||||
String[] parts = viewBox.trim().split("\\s+");
|
||||
if (parts.length != 4) {
|
||||
throw new IllegalArgumentException("Invalid viewBox: " + viewBox);
|
||||
}
|
||||
return new double[]{
|
||||
Double.parseDouble(parts[0]),
|
||||
Double.parseDouble(parts[1]),
|
||||
Double.parseDouble(parts[2]),
|
||||
Double.parseDouble(parts[3])
|
||||
};
|
||||
}
|
||||
|
||||
private static Map<String, Map<String, String>> parseStyles(Element root) {
|
||||
Map<String, Map<String, String>> styles = new HashMap<>();
|
||||
NodeList styleNodes = root.getElementsByTagName("style");
|
||||
@@ -191,6 +177,5 @@ public final class SvgNodeLoader {
|
||||
}
|
||||
}
|
||||
|
||||
private record SvgData(Group content, double minX, double minY, double width, double height) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user