【发布时间】:2016-11-08 17:21:38
【问题描述】:
我正在使用 batik 库在 java 中解析仅包含折线元素的 svg 文件。这是一个示例 svg 文件:
<svg fill-rule="evenodd" height="0.38in" preserveAspectRatio="none"
stroke-linecap="round" viewBox="0 0 150 225" width="0.25in">
<style type="text/css">
.pen1 { stroke: rgb(0,0,0); stroke-width: 19; stroke-linejoin: round;}
</style>
<g>
<polyline class="pen1" fill="none" points="10.0,-95 132.0,2.5 10,105 "/>
</g>
<g/>
</svg>
之后我对 dom 元素进行了一些操作,特别是将 viewBox 更改为解析后的折线点的边界框,并将 width 和 height 参数更改为 500px。
现在我正在寻找一种方法来提取我的折线的操作(缩放和平移)点。
知道如何做到这一点吗?
编辑 1
我尝试了 Robert Longson 建议的方法,但显然 getTransformToElement 总是返回单位矩阵,所以点保持不变。也许我的代码有问题?
if ((baseElement instanceof SVGLocatable) && (e instanceof SVGElement)) {
SVGSVGElement docSVGElement = (SVGSVGElement) baseElement;
SVGLocatable locatable = (SVGLocatable) baseElement;
SVGElement svgPolyline = (SVGElement) e;
SVGMatrix transformationMatrix = docSVGElement.createSVGMatrix();
transformationMatrix = locatable.getTransformToElement(svgPolyline);
for (Point2D p : points) {
SVGPoint svgPoint = docSVGElement.createSVGPoint();
svgPoint.setX((float) p.getX());
svgPoint.setY((float) p.getY());
SVGPoint svgPoint1 = svgPoint.matrixTransform(transformationMatrix);
normalizedPoints.add(new Point2D.Float(svgPoint1.getX(),svgPoint1.getY()));
}
}
e 是 dom 结构中的折线元素之一。
【问题讨论】:
-
也许将点值乘以 getTransformToElement 从根到折线元素的结果。
-
感谢罗伯特,这似乎是一个好方法。 svg 中是否必须有一个 transform=... 属性?我还不能让它工作,请看看我编辑的问题。这是你建议的吗?
标签: java svg transformation batik