Fix SVG scaling and split numbers with multiple decimal points correctly

pull/35/head
James Ball 2020-10-26 20:59:05 +00:00
rodzic 34e7229a8b
commit af70109e4f
1 zmienionych plików z 30 dodań i 5 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Vector; import java.util.Vector;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -87,6 +88,7 @@ public class SvgParser extends FileParser {
private String[] preProcessPath(String path) throws IllegalArgumentException { private String[] preProcessPath(String path) throws IllegalArgumentException {
// Replace all commas with spaces and then remove unnecessary whitespace // Replace all commas with spaces and then remove unnecessary whitespace
path = path.replace(',', ' '); path = path.replace(',', ' ');
path = path.replace("-", " -");
path = path.replaceAll("\\s+", " "); path = path.replaceAll("\\s+", " ");
path = path.replaceAll("(^\\s|\\s$)", ""); path = path.replaceAll("(^\\s|\\s$)", "");
@ -129,6 +131,28 @@ public class SvgParser extends FileParser {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static List<Float> splitCommand(String command) {
List<Float> nums = new ArrayList<>();
String[] decimalSplit = command.split("\\.");
try {
if (decimalSplit.length == 1) {
nums.add(Float.parseFloat(decimalSplit[0]));
} else {
nums.add(Float.parseFloat(decimalSplit[0] + "." + decimalSplit[1]));
for (int i = 2; i < decimalSplit.length; i++) {
nums.add(Float.parseFloat("." + decimalSplit[i]));
}
}
} catch (Exception e) {
System.out.println(Arrays.toString(decimalSplit));
System.out.println(command);
}
return nums;
}
@Override @Override
protected void parseFile(String filePath) protected void parseFile(String filePath)
throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException { throws ParserConfigurationException, IOException, SAXException, IllegalArgumentException {
@ -157,7 +181,8 @@ public class SvgParser extends FileParser {
if (commandChar != 'z' && commandChar != 'Z') { if (commandChar != 'z' && commandChar != 'Z') {
// Split the command into number strings and convert them into floats. // Split the command into number strings and convert them into floats.
nums = Arrays.stream(command.substring(1).split(" ")) nums = Arrays.stream(command.substring(1).split(" "))
.map(Float::parseFloat) .filter(Predicate.not(String::isBlank))
.flatMap((numString) -> splitCommand(numString).stream())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -287,15 +312,15 @@ public class SvgParser extends FileParser {
: prevQuadraticControlPoint.reflectRelativeToVector(currPoint); : prevQuadraticControlPoint.reflectRelativeToVector(currPoint);
} }
} else { } else {
controlPoint1 = new Vector2(args.get(i), args.get(i + 1)); controlPoint1 = new Vector2(args.get(i) / width, args.get(i + 1) / height);
} }
if (isCubic) { if (isCubic) {
controlPoint2 = new Vector2(args.get(i + 2), args.get(i + 3)); controlPoint2 = new Vector2(args.get(i + 2) / width, args.get(i + 3) / height);
} }
Vector2 newPoint = new Vector2(args.get(i + expectedArgs - 2), Vector2 newPoint = new Vector2(args.get(i + expectedArgs - 2) / width,
args.get(i + expectedArgs - 1)); args.get(i + expectedArgs - 1) / height);
if (!isAbsolute) { if (!isAbsolute) {
controlPoint1 = currPoint.translate(controlPoint1); controlPoint1 = currPoint.translate(controlPoint1);