use of net.sf.latexdraw.parsers.svg.SVGPathElement in project latexdraw by arnobl.
the class SVGBezierCurve method pathToBezierCurve.
/**
* Creates a bezier curve and initialises its path from an SVG element.
*/
private static IBezierCurve pathToBezierCurve(final SVGElement elt) {
if (!(elt instanceof SVGPathElement)) {
return null;
}
final SVGPathSegList list = ((SVGPathElement) elt).getSegList();
if (list == null || list.size() < 2 || !(list.get(0) instanceof SVGPathSegMoveto)) {
return null;
}
final SVGPathSegMoveto m = (SVGPathSegMoveto) list.get(0);
CtrlPointsSeg c;
int i = 1;
final int size = list.size();
// Creating a point to support when the first path element is relative.
Point2D pt = new Point2D.Double();
final List<IPoint> pts = new ArrayList<>();
final List<IPoint> ctrlpts = new ArrayList<>();
final boolean closed;
pt = m.getPoint(pt);
pts.add(ShapeFactory.INST.createPoint(pt));
if (list.get(1) instanceof CtrlPointsSeg) {
// We set the control point of the first point.
c = (CtrlPointsSeg) list.get(1);
ctrlpts.add(ShapeFactory.INST.createPoint(c.getCtrl1(pt)));
}
while (i < size && list.get(i) instanceof CtrlPointsSeg) {
c = (CtrlPointsSeg) list.get(i);
final Point2D currPt = c.getPoint(pt);
pts.add(ShapeFactory.INST.createPoint(currPt));
ctrlpts.add(ShapeFactory.INST.createPoint(c.getCtrl2(pt)));
pt = currPt;
i++;
}
if (pts.size() > 2 && pts.get(pts.size() - 1).equals(pts.get(0), 0.00001)) {
// We set the shape as closed
pts.remove(pts.size() - 1);
ctrlpts.remove(ctrlpts.size() - 1);
closed = true;
} else {
// There is something else at the end of the path.
closed = i < size && list.get(i) instanceof SVGPathSegClosePath;
}
final IBezierCurve bc = ShapeFactory.INST.createBezierCurve(pts, ctrlpts);
bc.setOpened(!closed);
return bc;
}
use of net.sf.latexdraw.parsers.svg.SVGPathElement in project latexdraw by arnobl.
the class SVGFreeHand method toSVG.
@Override
public SVGElement toSVG(final SVGDocument doc) {
if (doc == null) {
return null;
}
final SVGElement root = new SVGGElement(doc);
SVGElement elt;
root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, LNamespace.XML_TYPE_FREEHAND);
root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_PATH_TYPE, String.valueOf(shape.getType()));
root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_INTERVAL, String.valueOf(shape.getInterval()));
final String path = getPath().toString();
final StringBuilder pts = new StringBuilder();
for (final IPoint pt : shape.getPoints()) {
pts.append(pt.getX()).append(' ').append(pt.getY()).append(' ');
}
root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_POINTS, pts.toString());
if (shape.hasShadow()) {
final SVGElement shad = new SVGPathElement(doc);
shad.setAttribute(SVGAttributes.SVG_D, path);
setSVGShadowAttributes(shad, false);
root.appendChild(shad);
}
// The background of the borders must be filled is there is a shadow.
if (shape.hasShadow() && shape.isFilled()) {
elt = new SVGPathElement(doc);
elt.setAttribute(SVGAttributes.SVG_D, path);
setSVGBorderBackground(elt, root);
}
elt = new SVGPathElement(doc);
elt.setAttribute(SVGAttributes.SVG_D, path);
root.appendChild(elt);
setSVGAttributes(doc, elt, false);
setSVGRotationAttribute(root);
return root;
}
use of net.sf.latexdraw.parsers.svg.SVGPathElement in project latexdraw by arnobl.
the class JFXToSVG method pathToSVGPath.
/**
* Converts a JFX path to an SVGPath.
* @param path To JFX path to convert.
* @param doc The SVG document.
* @return The created SVGPath.
* @throws NullPointerException If one of the given parameter is null.
*/
public SVGPathElement pathToSVGPath(final Path path, final SVGDocument doc) {
final SVGPathElement svgPath = new SVGPathElement(doc);
final SVGPathSegList list = new SVGPathSegList();
list.addAll(path.getElements().stream().map(elt -> createSVGPathSeg(elt)).filter(elt -> elt != null).collect(Collectors.toList()));
svgPath.setPathData(list);
copyPropertiesToSVG(svgPath, path);
return svgPath;
}
Aggregations