当我在Linux环境中运行相同的代码并尝试使用batik将svg转换为png时,文本的位置会被移动。
OSX中的相同代码给出了正确的png。在Linux中,所有文本元素的位置似乎都在向右移动。
Linux

OS X

为什么Linux上的映像是错误的?
以下是代码:
TranscoderInput input = new TranscoderInput(svgPath);
// define OutputStream to PNG Image and attach to TranscoderOutput
OutputStream ostream = null;
File tempOutputFile = File.createTempFile(svgPath, ".png");
tempOutputFile.deleteOnExit();
try {
ostream = new FileOutputStream(tempOutputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
TranscoderOutput output = new TranscoderOutput(ostream);
// create a JPEG transcoder
PNGTranscoder t = new PNGTranscoder();
// set the transcoding hints
// convert and write output
t.transcode(input, output);
// flush and close the stream then exit
ostream.flush();
ostream.close();
return tempOutputFile;SVG文件
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - https://github.com/SVG-Edit/svgedit-->
<title>Layer 1</title>
<rect display-height="NaN" display-width="NaN" display-y="125.5" display-x="65.25" rect-id="0" id="svg_1" height="194" width="316" y="115" x="129" stroke-width="0.5" stroke="#000000" fill="none"/>
<text alignment-baseline="text-before-edge" xml:space="preserve" text-anchor="start" font-family="serif" font-size="24" id="svg_2" y="125.5" x="65.25" stroke-width="0" stroke="#000000" fill="#000000">sssssss</text>
</svg>发布于 2018-04-28 06:08:13
首先,我不知道你到底想要达到什么结果。这两个截图和问题中的片段都显示了一些不同的东西。您是否试图使文本的结尾与矩形的左手边重合?
这里有几个潜在的问题:
alignment-baseline。因此,这将影响您的文本定位。text-anchor="end"使文本正确地对齐。见下文。
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<rect id="svg_1" height="194" width="316" y="115" x="129" stroke-width="0.5" stroke="#000000" fill="none"/>
<text text-anchor="end" font-family="serif" font-size="24" id="svg_2" y="115" x="129" stroke-width="0" stroke="#000000" fill="#000000">Exact corner</text>
</svg>
https://stackoverflow.com/questions/50055191
复制相似问题