1
+ /**
2
+ * This file is part of VisiCut.
3
+ * Copyright (C) 2011 - 2024 Thomas Oster <[email protected] >
4
+ * RWTH Aachen University - 52062 Aachen, Germany
5
+ *
6
+ * VisiCut is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * VisiCut is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with VisiCut. If not, see <http://www.gnu.org/licenses/>.
18
+ **/
19
+ package de .thomas_oster .visicut .model .graphicelements ;
20
+
21
+ import org .junit .Test ;
22
+ import static org .junit .Assert .*;
23
+ import de .thomas_oster .visicut .model .graphicelements .svgsupport .SVGImporter ;
24
+ import de .thomas_oster .visicut .model .graphicelements .svgsupport .SVGShape ;
25
+ import java .awt .geom .Rectangle2D ;
26
+ import java .io .File ;
27
+ import java .io .FileWriter ;
28
+ import java .io .IOException ;
29
+ import java .util .ArrayList ;
30
+
31
+ public class SVGImportTest
32
+ {
33
+
34
+ @ Test
35
+ public void PathWithLocalTransform () throws ImportException , IOException
36
+ {
37
+ // Regression test: Bounding box calculated wrong when path has transform attribute.
38
+ // https://github.com/t-oster/VisiCut/issues/720
39
+ final String exampleSVG = "<?xml version=\" 1.0\" encoding=\" UTF-8\" standalone=\" no\" ?>\n " +
40
+ "<svg width=\" 40mm\" height=\" 30mm\" viewBox=\" 0 0 40 30\" >\n " +
41
+ " <path\n " +
42
+ " d=\" M 0,0 H 20000 L 5000,20000 h 10000\" \n " +
43
+ " style=\" fill:none;stroke:#0000ff;stroke-width:100\" \n " +
44
+ " transform=\" scale(0.001,0.001)\" \n " +
45
+ " id=\" path4\" />\n " +
46
+ "</svg>" ;
47
+ File tempFile = File .createTempFile ("example" , ".svg" );
48
+ tempFile .deleteOnExit ();
49
+ try (FileWriter s = new FileWriter (tempFile )) {
50
+ s .write (exampleSVG );
51
+ }
52
+ SVGImporter imp = new SVGImporter ();
53
+ GraphicSet result = imp .importSetFromFile (tempFile .getAbsoluteFile (), new ArrayList <>());
54
+ assertEquals (result .size (), 1 );
55
+ // stroke width = 100 * 0.001 local transform * 1 mm width per 1 unit viewbox = 0.1
56
+ assertEquals (0.1 , ((SVGShape ) result .get (0 )).getEffectiveStrokeWidthMm (), 0 );
57
+ // "visual bounding box" in SVG pixels including stroke width
58
+ // (expected values were determined in Inkscape)
59
+ // Note: here, SVG pixels are the same as millimeters
60
+ Rectangle2D bb = result .get (0 ).getBoundingBox ();
61
+ // left X = 0.0 mm according to Inkscape, but -0.05mm due to simplified approximation in VisiCut
62
+ assertEquals (-0.05 , bb .getMinX (), 1e-9 );
63
+ // other values are identical (partly because approximation errors cancel out)
64
+ assertEquals (-0.05 , bb .getMinY (), 1e-9 );
65
+ assertEquals (20.1 , bb .getHeight (), 1e-9 );
66
+ assertEquals (20.1 , bb .getWidth (), 1e-9 );
67
+ }
68
+ }
0 commit comments