Open ysimonx opened 6 months ago
here is the code to remove too much close points and to convert float to int in dart
import 'package:xml/xml.dart';
...
final String stringSVG = _controller.toRawSVG()!;
final String stringSVGReduced =
optimiseSVG(stringSVG, float2int: true, minDistanceBetweenPoints: 3);
String optimiseSVG(String stringSVG,
{bool float2int = false, int minDistanceBetweenPoints = 0}) {
final document = XmlDocument.parse(stringSVG);
var svgroot = document.root;
for (XmlElement child in svgroot.childElements) {
if (child.localName == "svg") {
for (XmlElement childsvg in child.childElements) {
if (childsvg.localName == "polyline") {
List attrs = childsvg.attributes;
for (XmlAttribute attr in attrs) {
if (attr.localName == "points") {
String value = attr.value;
List<String> points = value.split(" ");
List<String> filteredPoints = [];
dynamic x_prec = -1;
dynamic y_prec = -1;
for (String point in points) {
List<String> xy = point.split(",");
double doublex = double.parse(xy[0]);
double doubley = double.parse(xy[1]);
dynamic x;
dynamic y;
if (float2int) {
x = doublex.round();
y = doubley.round();
} else {
x = doublex;
y = doubley;
}
if ((x - x_prec).abs() > minDistanceBetweenPoints ||
(y - y_prec).abs() > minDistanceBetweenPoints) {
String s = "${x},${y}";
filteredPoints.add(s);
x_prec = x;
y_prec = y;
}
}
childsvg.setAttribute("points", filteredPoints.join(" "));
}
}
}
}
}
}
return document.toString();
}
Hi, thanks, we will analyze this.
For a single signature, i have a 8Bbytes svg export
If you keep int instead of float in "points" if you remove duplicate "points"
you can reduce a 8KBytes to 2Kbytes
here is the python code used for this