【发布时间】:2020-01-12 23:06:46
【问题描述】:
我需要了解 apache batik 如何缩进库生成的 SVG 文件。 我有以下代码:
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);
for(Drawable savedDrawable : cVwDrawLocal.getSavedDrawables()){
if(savedDrawable instanceof GeoDrawable){
savedDrawable.copyToGraphics(svgGenerator);
}
}
boolean useCSS = true; // we want to use CSS style attributes
String svgFile = svgFileName + ".svg";
try {
OutputStream outputStream = new FileOutputStream(svgFile);
Writer outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
svgGenerator.stream(outputStreamWriter, useCSS);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
生成的 SVG 文件是:
<g
><g style="fill:white; stroke-miterlimit:1; stroke-dasharray:1; stroke-dashoffset:1; stroke:white;"
><line y2="432" style="fill:none;" x1="873" x2="873" y1="432"
/></g
><g style="fill:rgb(0,252,253); stroke-miterlimit:1; stroke-dasharray:0.1,5; stroke-width:2; stroke-dashoffset:1; stroke:rgb(0,252,253);"
><line y2="479" style="fill:none;" x1="901" x2="910" y1="490"
/></g
>
但我需要这样的东西:
<g>
<g style="fill:white; stroke-miterlimit:1; stroke-dasharray:1; stroke-dashoffset:1; stroke:white;">
<line y2="432" style="fill:none;" x1="873" x2="873" y1="432"/>
</g>
<g style="fill:rgb(0,252,253); stroke-miterlimit:1; stroke-dasharray:0.1,5; stroke-width:2; stroke-dashoffset:1; stroke:rgb(0,252,253);">
<line y2="479" style="fill:none;" x1="901" x2="910" y1="490"/>
</g>
</g>
我怎样才能获得这个? 提前谢谢!
【问题讨论】:
-
你为什么要关心它如何缩进?
-
针对一个文件中不同SVG文件的合并问题。 “正确”的缩进会简化这个问题。
-
好的,我找到了适合我的解决方案:
标签: java svg indentation batik