📄 svgparser.java
字号:
/**
*
*/
package com.esri.solutions.jitk.data.svg.importer;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.List;
import org.apache.batik.parser.DefaultTransformListHandler;
import org.apache.batik.parser.ParseException;
import org.apache.batik.parser.TransformListParser;
import com.esri.adf.web.data.geometry.WebGeometry;
import com.esri.adf.web.data.geometry.WebPath;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebRing;
import com.esri.solutions.jitk.data.svg.SVGUtils;
import com.esri.solutions.jitk.web.data.geometry.WebArc;
public class SVGParser {
// This method parses SVG path data, as found in path element d attribute
public static WebGeometry parsePath(String s) throws ParseException {
final List<WebPoint> pts = new ArrayList<WebPoint>();
final List<WebArc> arcs = new ArrayList<WebArc>();
if(s != null && !s.trim().equals("")) {
String[] commands = s.split("\\s|,");
if(commands != null)
for(int i=0; i < commands.length; i++) {
if(commands[i].startsWith("M") && (i+1) < commands.length) {
double x = SVGUtils.toDouble(commands[i++].substring(1), Double.NaN);
double y = SVGUtils.toDouble(commands[i], Double.NaN);
if(x != Double.NaN && y != Double.NaN)
pts.add(new WebPoint(x, y));
} else if(commands[i].startsWith("L") && (i+1) < commands.length && pts.size() > 0) {
double x = SVGUtils.toDouble(commands[i++].substring(1), Double.NaN);
double y = SVGUtils.toDouble(commands[i], Double.NaN);
if(x != Double.NaN && y != Double.NaN)
pts.add(new WebPoint(x, y));
} else if(commands[i].startsWith("A") && (i+6) < commands.length && pts.size() > 0) {
double rx = SVGUtils.toDouble(commands[i++].substring(1), Double.NaN);
double ry = SVGUtils.toDouble(commands[i++], Double.NaN);
double phi = SVGUtils.toDouble(commands[i++], Double.NaN);
int fa = SVGUtils.toInt(commands[i++], -1);
int fs = SVGUtils.toInt(commands[i++], -1);
double x = SVGUtils.toDouble(commands[i++], Double.NaN);
double y = SVGUtils.toDouble(commands[i], Double.NaN);
// Convert from endpoint (SVG style) to center (ADF style) parameterization of the Arc
WebPoint startPoint = pts.get(pts.size() - 1);
WebPoint endPoint = new WebPoint(x, y);
WebPoint midPoint = SVGUtils.calcMidPoint(startPoint, endPoint, phi);
WebPoint midCenter = SVGUtils.calcMidCenter(midPoint, rx, ry, fa, fs);
WebPoint center = SVGUtils.calcCenter(startPoint, endPoint, midCenter, phi);
long startAngle = Math.round(Math.toDegrees(SVGUtils.calcTheta(midPoint, midCenter, rx, ry)));
long delta = Math.round(Math.toDegrees(SVGUtils.calcDelta(midPoint, midCenter, rx, ry)));
if(fa > 0) delta = 360 - delta;
long endAngle = (startAngle + delta) % 360;
// Create new WebArc object
arcs.add(new WebArc(center, rx, startAngle, endAngle));
}
}
}
if(arcs.size() == 1)
return arcs.get(0);
else if(pts.size() > 2 && (pts.get(0).equals(pts.get(pts.size() - 1))))
return new WebRing(pts);
else return new WebPath(pts);
}
// This method parses transform lists,
// as found in the transform attribute of any transformable element.
public static AffineTransform parseTransformList(String s) throws ParseException {
final AffineTransform t = new AffineTransform();
if(s != null && !s.trim().equals("")) {
TransformListParser parser = new TransformListParser();
DefaultTransformListHandler handler = new DefaultTransformListHandler() {
public void matrix(float a, float b, float c, float d, float e, float f) throws ParseException {
t.concatenate(new AffineTransform(a, b, c, d, e, f));
}
public void rotate(float theta) {
t.rotate(theta);
}
public void rotate(float theta, float cx, float cy) throws ParseException {
t.rotate(theta, cx, cy);
}
public void scale(float sx) throws ParseException {
t.scale(sx, 1);
}
public void scale(float sx, float sy) throws ParseException {
t.scale(sx, sy);
}
public void skewX(float skx) throws ParseException {
t.concatenate(new AffineTransform(1,0,Math.tan(skx),1,0,0));
}
public void skewY(float sky) throws ParseException {
t.concatenate(new AffineTransform(1,Math.tan(sky),0,1,0,0));
}
public void translate(float tx) throws ParseException {
t.translate(tx, 0);
}
public void translate(float tx, float ty) throws ParseException {
t.translate(tx, ty);
}
};
parser.setTransformListHandler(handler);
parser.parse(s);
}
return t;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -