Skip to content

Commit

Permalink
anim,bridge: improve lenient parsing
Browse files Browse the repository at this point in the history
1) Make sure that shapes are updated after emptied by script.

2) Improve the performance of paths, polygons and polylines referenced by <use>.

This is a follow-up to 1d1699d.
  • Loading branch information
carlosame committed Aug 19, 2024
1 parent baaad00 commit da7a0e7
Show file tree
Hide file tree
Showing 54 changed files with 1,945 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ protected void revalidate() {
Attr attr = element.getAttributeNodeNS(namespaceURI, localName);
String s;
if (attr == null) {
missing = true;
s = getDefaultValue();
if (s == null) {
missing = true;
return;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ protected void checkItemType(Object newItem) throws SVGException {
}
}

@Override
public void copyTo(AbstractSVGList list) {
super.copyTo(list);
AbstractSVGLengthList other = (AbstractSVGLengthList) list;
other.direction = direction;
}

/**
* An {@link SVGLength} in the list.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,27 @@ public Element create(String prefix, Document doc) {
return new SVGOMPathElement(prefix, (AbstractDocument) doc);
}

@Override
public void importAttributes(Element imported, Node toImport, boolean trimId) {
ElementFactory.super.importAttributes(imported, toImport, trimId);
if (toImport instanceof SVGOMPathElement) {
SVGOMPathElement path = (SVGOMPathElement) imported;
SVGOMPathElement otherPath = (SVGOMPathElement) toImport;
SVGOMAnimatedPathData d = path.d;
// Make sure pathSegs exists
otherPath.d.getPathSegList();
boolean wasValid = otherPath.d.pathSegs.isValid();
if (otherPath.d.check() <= 0) {
// Copy only if check was successful, so the errors will be reported
d.getPathSegList();
otherPath.d.pathSegs.copyTo(d.pathSegs);
} else if (!wasValid) {
// invalidate, so the errors can be reported
otherPath.d.pathSegs.invalidate();
}
}
}

}

/**
Expand Down Expand Up @@ -1567,6 +1588,26 @@ public Element create(String prefix, Document doc) {
return new SVGOMPolygonElement(prefix, (AbstractDocument) doc);
}

@Override
public void importAttributes(Element imported, Node toImport, boolean trimId) {
ElementFactory.super.importAttributes(imported, toImport, trimId);
if (toImport instanceof SVGPointShapeElement) {
SVGPointShapeElement poly = (SVGPointShapeElement) imported;
SVGPointShapeElement otherPoly = (SVGPointShapeElement) toImport;
SVGOMAnimatedPoints points = poly.points;
otherPoly.points.getPoints();
boolean wasValid = otherPoly.points.baseVal.isValid();
if (otherPoly.points.check() <= 0) {
// Copy only if check was successful, so the errors will be reported
points.getPoints();
otherPoly.points.baseVal.copyTo(points.baseVal);
} else if (!wasValid) {
// invalidate, so the errors can be reported
otherPoly.points.baseVal.invalidate();
}
}
}

}

/**
Expand All @@ -1585,6 +1626,26 @@ public Element create(String prefix, Document doc) {
return new SVGOMPolylineElement(prefix, (AbstractDocument) doc);
}

@Override
public void importAttributes(Element imported, Node toImport, boolean trimId) {
ElementFactory.super.importAttributes(imported, toImport, trimId);
if (toImport instanceof SVGPointShapeElement) {
SVGPointShapeElement poly = (SVGPointShapeElement) imported;
SVGPointShapeElement otherPoly = (SVGPointShapeElement) toImport;
SVGOMAnimatedPoints points = poly.points;
otherPoly.points.getPoints();
boolean wasValid = otherPoly.points.baseVal.isValid();
if (otherPoly.points.check() <= 0) {
// Copy only if check was successful, so the errors will be reported
points.getPoints();
otherPoly.points.baseVal.copyTo(points.baseVal);
} else if (!wasValid) {
// invalidate, so the errors can be reported
otherPoly.points.baseVal.invalidate();
}
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import io.sf.carte.echosvg.anim.values.AnimatableLengthListValue;
import io.sf.carte.echosvg.anim.values.AnimatableValue;
import io.sf.carte.echosvg.dom.svg.AbstractSVGList;
import io.sf.carte.echosvg.dom.svg.ListBuilder;
import io.sf.carte.echosvg.dom.svg.LiveAttributeException;
import io.sf.carte.echosvg.dom.svg.SVGItem;
Expand Down Expand Up @@ -350,6 +351,14 @@ protected void revalidate() {
}
}

@Override
public void copyTo(AbstractSVGList list) {
super.copyTo(list);
BaseSVGLengthList other = (BaseSVGLengthList) list;
other.malformed = malformed;
other.missing = missing;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.w3c.dom.svg.SVGNumber;
import org.w3c.dom.svg.SVGNumberList;

import io.sf.carte.echosvg.anim.dom.SVGOMAnimatedLengthList.BaseSVGLengthList;
import io.sf.carte.echosvg.anim.values.AnimatableNumberListValue;
import io.sf.carte.echosvg.anim.values.AnimatableValue;
import io.sf.carte.echosvg.dom.svg.AbstractSVGList;
import io.sf.carte.echosvg.dom.svg.AbstractSVGNumberList;
import io.sf.carte.echosvg.dom.svg.ListBuilder;
import io.sf.carte.echosvg.dom.svg.LiveAttributeException;
Expand Down Expand Up @@ -331,6 +333,14 @@ protected void revalidate() {
}
}

@Override
public void copyTo(AbstractSVGList list) {
super.copyTo(list);
BaseSVGLengthList other = (BaseSVGLengthList) list;
other.malformed = malformed;
other.missing = missing;
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.sf.carte.echosvg.dom.svg.SVGPathSegItem;
import io.sf.carte.echosvg.parser.ParseException;
import io.sf.carte.echosvg.parser.PathArrayProducer;
import io.sf.carte.echosvg.util.CSSConstants;

/**
* This class is the implementation of the {@link SVGAnimatedPathData}
Expand Down Expand Up @@ -162,24 +163,31 @@ public SVGPathSegList getPathSegList() {
}

/**
* @return an exception if the path data is malformed, {@code null} otherwise.
* Check whether the attribute is missing, empty or malformed.
*
* @return -1 if the value is present and is valid,
* LiveAttributeException.ERR_ATTRIBUTE_MISSING if the attribute is
* missing or {@code none}, ERR_ATTRIBUTE_MALFORMED if it is invalid,
* -2 if it is invalid but that was already returned.
*/
public LiveAttributeException check() {
public short check() {
if (!hasAnimVal) {
if (pathSegs == null) {
pathSegs = new BaseSVGPathSegList();
}

boolean wasValid = pathSegs.isValid();

pathSegs.revalidate();
if (pathSegs.missing) {
return new LiveAttributeException(element, localName,
LiveAttributeException.ERR_ATTRIBUTE_MISSING, null);

if (pathSegs.none) {
return LiveAttributeException.ERR_ATTRIBUTE_MISSING;
}
if (pathSegs.malformed) {
return new LiveAttributeException(element, localName,
LiveAttributeException.ERR_ATTRIBUTE_MALFORMED, pathSegs.getValueAsString());
return wasValid ? -2 : LiveAttributeException.ERR_ATTRIBUTE_MALFORMED;
}
}
return null;
return -1;
}

/**
Expand Down Expand Up @@ -274,15 +282,19 @@ public void attrRemoved(Attr node, String oldv) {
public class BaseSVGPathSegList extends AbstractSVGPathSegList {

/**
* Whether the attribute is missing.
* Whether the attribute is missing or 'none'.
*/
protected boolean missing;
protected boolean none;

/**
* Whether the attribute is malformed.
*/
protected boolean malformed;

boolean isValid() {
return valid;
}

/**
* Create a DOMException.
*/
Expand Down Expand Up @@ -330,7 +342,7 @@ protected void setAttributeValue(String value) {
@Override
protected void resetAttribute() {
super.resetAttribute();
missing = false;
none = false;
malformed = false;
}

Expand All @@ -341,7 +353,7 @@ protected void resetAttribute() {
@Override
protected void resetAttribute(SVGItem item) {
super.resetAttribute(item);
missing = false;
none = false;
malformed = false;
}

Expand All @@ -355,12 +367,12 @@ protected void revalidate() {
}

valid = true;
missing = false;
none = false;
malformed = false;

String s = getValueAsString().trim(); // Default is '', not null
if (s.isEmpty()) {
missing = true;
String s = getValueAsString().trim(); // Default is 'none', not null
if (CSSConstants.CSS_NONE_VALUE.equalsIgnoreCase(s)) {
none = true;
clear(itemList);
itemList = new ArrayList<>(1);
return;
Expand All @@ -387,7 +399,7 @@ public void copyTo(AbstractSVGList list) {
super.copyTo(list);
BaseSVGPathSegList b = (BaseSVGPathSegList) list;
b.malformed = malformed;
b.missing = missing;
b.none = none;
}

}
Expand All @@ -399,9 +411,9 @@ public void copyTo(AbstractSVGList list) {
public class NormalizedBaseSVGPathSegList extends AbstractSVGNormPathSegList {

/**
* Whether the attribute is missing.
* Whether the attribute is missing or 'none'.
*/
protected boolean missing;
protected boolean none;

/**
* Whether the attribute is malformed.
Expand Down Expand Up @@ -459,12 +471,12 @@ protected void revalidate() {
}

valid = true;
missing = false;
none = false;
malformed = false;

String s = getValueAsString().trim(); // Default is '', not null
if (s.isEmpty()) {
missing = true;
String s = getValueAsString().trim(); // Default is 'none', not null
if (CSSConstants.CSS_NONE_VALUE.equalsIgnoreCase(s)) {
none = true;
clear(itemList);
itemList = new ArrayList<>(1);
return;
Expand All @@ -491,7 +503,7 @@ public void copyTo(AbstractSVGList list) {
super.copyTo(list);
NormalizedBaseSVGPathSegList b = (NormalizedBaseSVGPathSegList) list;
b.malformed = malformed;
b.missing = missing;
b.none = none;
}

}
Expand Down
Loading

0 comments on commit da7a0e7

Please sign in to comment.