Center svg bounds
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package org.example.petshopdesktop.ui;
|
package org.example.petshopdesktop.ui;
|
||||||
|
|
||||||
import javafx.scene.Group;
|
import javafx.scene.Group;
|
||||||
import javafx.scene.Node;
|
|
||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
import javafx.scene.paint.Paint;
|
import javafx.scene.paint.Paint;
|
||||||
import javafx.scene.shape.Circle;
|
import javafx.scene.shape.Circle;
|
||||||
@@ -26,12 +25,13 @@ public final class SvgNodeLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static StackPane loadSquare(String resourcePath, double size) {
|
public static StackPane loadSquare(String resourcePath, double size) {
|
||||||
SvgData svgData = readSvg(resourcePath);
|
Group content = readSvg(resourcePath);
|
||||||
Group translated = new Group(svgData.content());
|
var bounds = content.getLayoutBounds();
|
||||||
translated.setTranslateX(-svgData.minX());
|
Group translated = new Group(content);
|
||||||
translated.setTranslateY(-svgData.minY());
|
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.setScaleX(scale);
|
||||||
translated.setScaleY(scale);
|
translated.setScaleY(scale);
|
||||||
|
|
||||||
@@ -42,17 +42,16 @@ public final class SvgNodeLoader {
|
|||||||
return pane;
|
return pane;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SvgData readSvg(String resourcePath) {
|
private static Group readSvg(String resourcePath) {
|
||||||
try (Reader reader = requireResource(resourcePath)) {
|
try (Reader reader = requireResource(resourcePath)) {
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
factory.setNamespaceAware(false);
|
factory.setNamespaceAware(false);
|
||||||
Document document = factory.newDocumentBuilder().parse(new InputSource(reader));
|
Document document = factory.newDocumentBuilder().parse(new InputSource(reader));
|
||||||
Element root = document.getDocumentElement();
|
Element root = document.getDocumentElement();
|
||||||
double[] viewBox = parseViewBox(root.getAttribute("viewBox"));
|
|
||||||
Map<String, Map<String, String>> styles = parseStyles(root);
|
Map<String, Map<String, String>> styles = parseStyles(root);
|
||||||
Group content = new Group();
|
Group content = new Group();
|
||||||
appendChildren(root, content, styles);
|
appendChildren(root, content, styles);
|
||||||
return new SvgData(content, viewBox[0], viewBox[1], viewBox[2], viewBox[3]);
|
return content;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("Unable to load SVG: " + resourcePath, 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);
|
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) {
|
private static Map<String, Map<String, String>> parseStyles(Element root) {
|
||||||
Map<String, Map<String, String>> styles = new HashMap<>();
|
Map<String, Map<String, String>> styles = new HashMap<>();
|
||||||
NodeList styleNodes = root.getElementsByTagName("style");
|
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