Skip to content

Commit 298728b

Browse files
committed
Merge branch 'upstream/master' into develop
Adapt Svg2Xml: avoid GUI code in this class, otherwise this prevents the CLI to work. # Conflicts: # src/main/java/com/mxgraph/svg2xml/Svg2Xml.java
2 parents 8920261 + 673643f commit 298728b

5 files changed

Lines changed: 136 additions & 34 deletions

File tree

src/main/java/com/mxgraph/svg2xml/Shape2Xml.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static Element parse(Element element, Document xmlDoc, XmlConfig configDo
5050
{
5151
double s = configDoc.getRelativeScalingRatio();
5252

53+
if (!configDoc.isRelativeScaling())
54+
{
55+
s = Math.min(configDoc.getAbsoluteScalingX() / configDoc.getStencilBoundsX(), configDoc.getAbsoluteScalingY() / configDoc.getStencilBoundsY());
56+
}
57+
5358
if (element.getNodeName().equals("rect"))
5459
{
5560
if (element.getAttribute("transform") != null && !element.getAttribute("transform").equals(""))

src/main/java/com/mxgraph/svg2xml/Svg2Xml.java

Lines changed: 107 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public class Svg2Xml
6464
private XmlConfig destConfigDoc = null;
6565

6666
//user identifier, mxGraph is default, but every user should come up with his own identifier, to avoid conflicts with mxGraph standard stencils and shapes
67-
private final String stencilUserMarker = "mxGraph";
67+
private final String stencilUserMarker = "mxgraph";
68+
69+
private String sourceFolder = "";
6870

6971
public Svg2Xml(Svg2XmlGui gui)
7072
{
@@ -131,15 +133,48 @@ public void convertToXml(File[] sourceFiles, File destPath) {
131133
boolean isLastInGroup = true;
132134
boolean isNewGroup = true;
133135
String groupXml = new String();
134-
ByteArrayOutputStream groupBaos = new ByteArrayOutputStream();
136+
ByteArrayOutputStream groupBaos = new ByteArrayOutputStream();String groupName = stencilUserMarker;
137+
138+
139+
// =============================================================================================================
140+
// NOTE: commented from upstream/master - in this repository, we don't have GUI code in the processing code!
141+
// =============================================================================================================
142+
// boolean areFiles = false;
143+
144+
//checks if files are selected too, or only folders
145+
// for (int i = 0; i < gui.sourceFileListComponent.getSelectedFiles().length; i++)
146+
// {
147+
// if (gui.sourceFileListComponent.getSelectedFiles()[i].isFile())
148+
// {
149+
// areFiles = true;
150+
// }
151+
// }
152+
153+
//if files are selected too, the parent folder is the root of the group naming
154+
// if (areFiles)
155+
// {
156+
// sourceFolder = gui.sourceFileListComponent.getSelectedFiles()[0].getParentFile().getParent();
157+
// }
158+
// else
159+
// {
160+
// sourceFolder = gui.sourceFileListComponent.getSelectedFiles()[0].getParent();
161+
// }
162+
// =============================================================================================================
163+
// END OF "NOTE"
164+
// =============================================================================================================
165+
166+
boolean isGroupNameInConfig = false;
167+
String lastGroupName = "";
135168

136169
// construct destConfigDoc based on default values, groupConfigDoc and stencilConfigDoc
137170
for (int i = 0; i < sourceFiles.length; i++)
138171
{
139172
System.out.println("Processing " + sourceFiles[i].getAbsolutePath());
140173
groupBaos = new ByteArrayOutputStream();
141174
isLastInGroup = false;
142-
isNewGroup = false;
175+
isNewGroup = true;
176+
177+
groupName = stencilUserMarker;
143178

144179
String shapeName = sourceFiles[i].getName();
145180
shapeName = shapeName.substring(0, shapeName.lastIndexOf("."));
@@ -208,6 +243,11 @@ public void convertToXml(File[] sourceFiles, File destPath) {
208243
destConfigDoc.setAspect(aspectType.VARIABLE);
209244
}
210245

246+
if (groupConfigRootElement.getAttribute("groupname") != null && groupConfigRootElement.getAttribute("groupname") != "")
247+
{
248+
groupName = groupConfigRootElement.getAttribute("groupname");
249+
isGroupNameInConfig = true;
250+
}
211251
//TODO implement strokewidth reading
212252
// strokeWidth = groupConfigRootElement.getAttribute("strokewidth");
213253
groupConnection = getConstraintsFromXml(groupConfigString);
@@ -338,11 +378,11 @@ else if (aspectRatio.toLowerCase().equals("variable"))
338378
Constraint currConstraint = constraints.get(j);
339379

340380
double x = currConstraint.getX() - bounds.getMinX();
341-
x = Math.round(x * 100.0 / bounds.getWidth()) / 100.0;
381+
x = Math.round(x * 1000.0 / bounds.getWidth()) / 1000.0;
342382
currConstraint.setX(x);
343383

344384
double y = currConstraint.getY() - bounds.getMinY();
345-
y = Math.round(y * 100.0 / bounds.getHeight()) / 100.0;
385+
y = Math.round(y * 1000.0 / bounds.getHeight()) / 1000.0;
346386
currConstraint.setY(y);
347387
}
348388

@@ -543,7 +583,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
543583
String currParent = sourceFiles[i].getParent();
544584
String oldParent = sourceFiles[i-1].getParent();
545585

546-
if(currParent.equals(oldParent))
586+
if(currParent.equals(oldParent) && i!=0)
547587
{
548588
isNewGroup = false;
549589
}
@@ -553,7 +593,7 @@ else if (aspectRatio.toLowerCase().equals("variable"))
553593
}
554594
}
555595

556-
//check if this is the last file in the group
596+
//if this is the last file in the group
557597
if (i + 1 == sourceFiles.length)
558598
{
559599
isLastInGroup = true;
@@ -573,36 +613,33 @@ else if (aspectRatio.toLowerCase().equals("variable"))
573613
}
574614
}
575615

576-
// here we need some group naming check
616+
// group naming check
577617
String currentPath = sourceFiles[i].getAbsolutePath();
578618
currentPath = currentPath.substring(2, currentPath.lastIndexOf("."));
579619

620+
File currFile = new File(sourceFiles[i].getAbsolutePath());
621+
580622
if (isNewGroup)
581623
{
582-
// if new group then we save the old file and open a new one
583-
String groupName = stencilUserMarker;
584-
File currFile = new File(sourceFiles[i].getAbsolutePath());
585-
ArrayList <String> folders = new ArrayList <String>();
586-
587-
while (!currFile.getParentFile().getName().equals("svgroot") && currFile.getParent().length() > 4)
624+
//if group name wasn't in config, generate it based on folder structure
625+
if (!isGroupNameInConfig)
588626
{
589-
currFile = currFile.getParentFile();
590-
folders.add(0, currFile.getName());
591-
}
627+
String fullStr = currFile.getParent().toLowerCase();
628+
String currName = fullStr.replace(sourceFolder.toLowerCase(), "");
629+
currName = currName.replace(File.separator, ".");
592630

593-
for (int j = 0; j < folders.size(); j++)
594-
{
595-
groupName += "." + folders.get(j);
631+
groupName = stencilUserMarker + currName;
596632
}
597633

634+
lastGroupName = groupName;
598635
groupXml = "<shapes name=\"" + groupName + "\">" + System.getProperty("line.separator");
599636
String tmp = Svg2Xml.printDocumentString(destDoc, groupBaos);
600637
tmp = tmp.replaceAll("\\.0\"", "\"");
601638
groupXml += tmp;
602639
}
603640
else
604641
{
605-
// if not a new group then we just add the xml to the group xml
642+
// if not a new group, just add the xml to the group xml
606643
groupXml += Svg2Xml.printDocumentString(destDoc, groupBaos);
607644
}
608645

@@ -615,8 +652,8 @@ else if (aspectRatio.toLowerCase().equals("variable"))
615652

616653
try
617654
{
618-
String currentDestPath = destPath.getAbsolutePath();
619-
currentDestPath += sourceFiles[i].getParent().substring(2, sourceFiles[i].getParent().length()) + ".xml";
655+
String currentDestPath = destPath.getAbsolutePath() + File.separator + lastGroupName.replace(".", File.separator) + ".xml";
656+
620657
currentDestPath = currentDestPath.toLowerCase();
621658
currentDestPath = currentDestPath.replaceAll("\\s", "_");
622659
File myDestFile = new File(currentDestPath);
@@ -630,7 +667,6 @@ else if (aspectRatio.toLowerCase().equals("variable"))
630667
writer.close();
631668
System.out.println("File written");
632669

633-
// JOptionPane.showMessageDialog(gui.getFrame(), "Conversion completed.\nThe library is genereted in:\n" + myDestRoot);
634670
if (!destPaths.contains(myDestRoot))
635671
{
636672
destPaths.add(myDestRoot);
@@ -855,7 +891,39 @@ else if(!styleDiff.get("fillcolor").equals("none"))
855891
if (!fontSizeStr.equals(""))
856892
{
857893
fontSizeStr = removeUnits(fontSizeStr);
858-
double fs = Double.parseDouble(fontSizeStr) * configDoc.getRelativeScalingRatio();
894+
double fs = 16;
895+
896+
if (isNumeric(fontSizeStr))
897+
{
898+
fs = Double.parseDouble(fontSizeStr) * configDoc.getRelativeScalingRatio();
899+
}
900+
else
901+
{
902+
switch (fontSizeStr)
903+
{
904+
case "xx-small":
905+
fs = 9;
906+
break;
907+
case "x-small":
908+
fs = 10;
909+
break;
910+
case "small":
911+
fs = 13;
912+
break;
913+
case "medium":
914+
fs = 16;
915+
break;
916+
case "large":
917+
fs = 18;
918+
break;
919+
case "x-large":
920+
fs = 24;
921+
break;
922+
case "xx-large":
923+
fs = 32;
924+
}
925+
926+
}
859927
el.setAttribute("size", Double.toString(fs));
860928
node.appendChild(el);
861929
}
@@ -2224,8 +2292,16 @@ private static void createBackbone(Document destDoc, XmlConfig destConfigDoc, St
22242292

22252293
if (destConfigDoc.isCalculateBorder())
22262294
{
2227-
double w = destConfigDoc.getStencilBoundsX() * s;
2228-
double h = destConfigDoc.getStencilBoundsY() * s;
2295+
double w = Math.round(destConfigDoc.getStencilBoundsX() * s * 1000.0) / 1000.0;
2296+
double h = Math.round(destConfigDoc.getStencilBoundsY() * s * 1000.0) / 1000.0;
2297+
2298+
if (!destConfigDoc.isRelativeScaling())
2299+
{
2300+
double sc = Math.min(destConfigDoc.getAbsoluteScalingX() / destConfigDoc.getStencilBoundsX(), destConfigDoc.getAbsoluteScalingY() / destConfigDoc.getStencilBoundsY());
2301+
w = Math.round(destConfigDoc.getStencilBoundsX() * sc * 1000.0) / 1000.0;
2302+
h = Math.round(destConfigDoc.getStencilBoundsY() * sc * 1000.0) / 1000.0;
2303+
}
2304+
22292305
root.setAttribute("w", String.valueOf(w));
22302306
root.setAttribute("h", String.valueOf(h));
22312307
}
@@ -2556,5 +2632,9 @@ private Document removeConnections(Document svgDoc)
25562632

25572633
return svgDoc;
25582634
}
2635+
2636+
public static boolean isNumeric(String str) {
2637+
return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
2638+
}
25592639
};
25602640

src/main/java/com/mxgraph/svg2xml/Svg2XmlGui.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class Svg2XmlGui implements ActionListener{
4949
protected boolean roundCoords=true;
5050

5151
//round to decimals (used with roundCoords=true)
52-
protected int decimalsToRound=2;
52+
protected int decimalsToRound=3;
5353

5454
//i'm not yet sure about the exact functionality of this
5555
protected boolean overrideLocalConfig=false; //TODO make use of this var
@@ -128,7 +128,7 @@ public void actionPerformed(ActionEvent e) {
128128
destinationPanel.add(destinationComponent);
129129

130130
calculateBorderComponent = new JCheckBox("Calculate border (or read from SVG)", calculateBorder);
131-
groupStencilsComponent = new JCheckBox("(TODO)Group stencils (or each in a separate file)", groupStencils);
131+
groupStencilsComponent = new JCheckBox("TODO", groupStencils); //Group stencils (or each in a separate file)
132132

133133
JPanel relScalePanel = new JPanel();
134134
useRelScaleComponent = new JRadioButton("Use relative scaling (dest/src):", relativeScaling);
@@ -137,7 +137,7 @@ public void actionPerformed(ActionEvent e) {
137137
relScalePanel.add(relScaleComponent);
138138

139139
JPanel absScalePanel = new JPanel();
140-
useAbsScaleComponent = new JRadioButton("(TODO)Use absolute scaling:", !relativeScaling);
140+
useAbsScaleComponent = new JRadioButton("Normalize size to:", !relativeScaling); //Use absolute scaling
141141
JLabel absScaleLabel1 = new JLabel("x:");
142142
absXScaleComponent = new JTextField(Double.toString(absoluteScalingX), 4);
143143
JLabel absScaleLabel2 = new JLabel("px y:");
@@ -157,14 +157,14 @@ public void actionPerformed(ActionEvent e) {
157157

158158
JPanel roundPanel = new JPanel();
159159
roundCoordinatesComponent = new JCheckBox("Round coordinates to ", roundCoords);
160-
roundDecimalNumComponent = new JTextField("2", 4);
160+
roundDecimalNumComponent = new JTextField("3", 4);
161161
JLabel decimalLabel1 = new JLabel("decimals");
162162
roundPanel.add(roundCoordinatesComponent);
163163
roundPanel.add(roundDecimalNumComponent);
164164
roundPanel.add(decimalLabel1);
165165

166-
overrideLocalConfigComponent = new JCheckBox("(TODO)Override local config", true);
167-
keepLogComponent = new JCheckBox("(TODO)Keep log", true);
166+
overrideLocalConfigComponent = new JCheckBox("TODO", true); //Override local config
167+
keepLogComponent = new JCheckBox("TODO", true); //Logging
168168

169169
filePanel.add(BorderLayout.CENTER, sourcePanel);
170170
filePanel.add(BorderLayout.CENTER, destinationPanel);

src/main/java/com/mxgraph/svg2xml/XmlConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public static enum aspectType { VARIABLE, FIXED };
4747
//round to decimals (used with roundCoords=true)
4848
private int decimalsToRound = 2;
4949

50+
//NOTE: do not set groupName here, since it is read from the groups config file. add an entry such as "groupname: yourgroupname;" without the quotations to your groups XML (config file)
51+
//if null, group name is generated from Svg2xml.stencilUserMarker and folder structure
52+
private String groupName = null;
53+
5054
private Connection connection;
5155
private XmlStyle style;
5256

@@ -272,4 +276,12 @@ public void setStencilBoundsMinY(double stencilBoundsMinY)
272276
{
273277
this.stencilBoundsMinY = stencilBoundsMinY;
274278
}
279+
280+
public String getGroupName() {
281+
return groupName;
282+
}
283+
284+
public void setGroupName(String groupName) {
285+
this.groupName = groupName;
286+
}
275287
}

src/main/java/com/mxgraph/svg2xml/mxPathParser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ else if (prevPathType == 'M')
100100
break;
101101
case 'z':
102102
case 'Z':
103-
closePath();
103+
if (Character.toLowerCase(prevPathType) != 'z')
104+
{
105+
closePath();
106+
}
104107
break;
105108
case 'm':
106109
movetoRel(currPathString);
@@ -168,6 +171,8 @@ else if (prevPathType == 'M')
168171
{
169172
svgPath = svgPath.substring(nextPartStartIndex, svgPath.length());
170173
}
174+
175+
prevPathType = currPathType;
171176
}
172177
while (svgPath.length() > 0);
173178

0 commit comments

Comments
 (0)