gpt4 book ai didi

SVG 到 PNG 文本无法正确显示 - Arial 字体

转载 作者:行者123 更新时间:2023-12-02 04:03:58 27 4
gpt4 key购买 nike

我遇到一个问题,当存在具有不同字体系列的文本(例如 Arial)时,使用 Apache Batik 转换的 PNG 会有所不同。该问题发生在 Cent OS 6 运行 Tomcat 7 和 Java 6 的环境中。

用于将 SVG 转换为 PNG 的 Java 代码是:

// Convert the SVG image to png and send back
PNGTranscoder transcoder = new PNGTranscoder();
//
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgImage));
outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);

// Transcode the given SVG
transcoder.transcode(input, output);

outStream.flush();

pngImage = outStream.toByteArray();

我要转换为 PNG 的 SVG 文件是:

<svg version="1.1" x="0" y="0" id="hjtqebzv1" width="610" height="240" xmlns="http://www.w3.org/2000/svg" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="LFFFFFF0" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#FFFFFF;stop-opacity:0.8"/>
<stop offset="100%" style="stop-color:#FAFAFA;stop-opacity:1"/>
</linearGradient>
</defs>
<g id="hjtqebzv-o1" transform="translate(5,5)">
<rect x="1" y="1" width="578" height="20" fill="url(#LFFFFFF0)" stroke="#5e5ca7" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>
<text x="1" y="1" width="578" height="19" >
<tspan x="2" y="14" style="font-family:Arial;font-size:12px;fill:#000000;">This is a test text for testing text overlapping in the rectangle when convert the svg to PNG using SVG-Batik</tspan>
</text>
</g>
</svg>

当我在 Firefox 浏览器中打开 SVG 文件时,它会正确显示,如下图所示: enter image description here

但是当我使用 Apache Batik 转换 SVG 时,转换后的图像看起来有所不同。 Apache Batik 转换后的 PNG 为: enter image description here

在运行 tomcat 7 和 Java 7 的 Windows 7 中,生成的图像与原始 SVG 相同。

由于 Cent OS 服务器提供了困惑的文本图像,我觉得 Tomcat/java 应用程序无法使用 Arial 字体,需要手动加载它。如果是这样,我更愿意建议从底层操作系统位置(操作系统字体位置)以通用方式加载它们,而不需要对 SVG 文件进行任何更改。

最佳答案

您需要安装 Microsoft truetype 字体,并通过设置 JAVA_FONTS 环境变量使其可用于 java 虚拟机。

wget http://www.my-guides.net/en/images/stories/fedora12/msttcore-fonts-2.0-3.noarch.rpm

rpm -ivh msttcore-fonts-2.0-3.noarch.rpm

打开/etc/bashrc并将以下内容添加到文件末尾

JAVA_FONTS=/usr/share/fonts/msttcore

export JAVA_FONTS

http://www.itzgeek.com/how-tos/linux/centos-how-tos/how-to-install-microsoft-truetype-fonts-in-centos-6-rhel-6.html#axzz2aibHZaOI

关于SVG 到 PNG 文本无法正确显示 - Arial 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com