简体   繁体   English

使用蜡染获得svg折线的变换点

[英]get transformed points of svg polyline using batik

I am parsing svg files with only a polyline element in java using the batik libary. 我使用蜡染库在Java中仅用折线元素解析svg文件。 This is an example svg file: 这是一个示例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>

After that I perform some manipulations on the dom element, specifically changing the viewBox to the bounding box of the parsed polyline points and changing width and height parameters to 500px. 之后,我对dom元素执行了一些操作,特别是将viewBox更改为已解析折线点的边界框,并将widthheight参数更改为500px。

Now I am looking for a way to extract the manipulated (scaled and translated) points of my polyline. 现在,我正在寻找一种提取折线的操纵点(缩放和平移)的方法。

Any idea how this could be done? 任何想法如何做到这一点?

EDIT 1 编辑1

I tried the approach suggested by Robert Longson but apparently getTransformToElement always returns the identity matrix, so the points remain the same. 我尝试了罗伯特·朗森(Robert Longson)建议的方法,但显然getTransformToElement始终返回单位矩阵,因此点保持不变。 Maybe I got something wrong with my code? 也许我的代码有问题?

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 is one of the PolyLine Elements in the dom structure. e是dom结构中的PolyLine元素之一。

It is working now. 现在正在工作。 Use the approach in the first edit. 在第一次编辑中使用该方法。 Setting transformationMatrix = locatablePolyline.getCTM(); 设置transformationMatrix = locatablePolyline.getCTM(); as transformation matrix does the trick. 作为转换矩阵就可以了。 Then just apply the matrix to every point in the path. 然后只需将矩阵应用于路径中的每个点。 Thanks Robert for the hint! 谢谢罗伯特的提示!

Don't forget to initialize svg css dom interfaces, for example with the following code: 不要忘记初始化svg css dom接口,例如,使用以下代码:

Document doc = factory.createSVGDocument(f.toURI().toString());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
builder.build(ctx, doc);

Otherwise you cannot use the operations on SVGLocatables . 否则,您将无法使用SVGLocatables上的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM