SVG text for PNG does not display correctly - Arial font

I have a problem with converted PNG using Apache Batik different when there is text with a different font family like Arial. The problem occurs in an environment where Cent OS 6 works with Tomcat 7 and Java 6.

The Java code used to convert SVG to PNG is:

// 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(); 

The SVG file that I am going to convert to PNG is as follows:

 <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> 

When I open the SVG file in Firefox, it displays correctly, as shown in the image below: enter image description here

But when I convert SVG using Apache Batik, the converted image looks different. PNG Converter Apache Batik: enter image description here

On Windows 7 running tomcat 7 and Java 7, the generated image is identical to the original SVG.

As a Cent OS server, it provides a text messed image. I feel that the Arial font is not available for the tomcat / java application and needs to be downloaded manually. If so, I prefer to have advice to load them in the usual way from the main OS location (OS font location) without any changes to the SVG file.

+6
source share
1 answer

You need to set Microsoft TrueType fonts and make them available for the Java virtual machine by setting the JAVA_FONTS environment JAVA_FONTS .

 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 

Open /etc/bashrc and add the following to the end of the file

 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

+4
source

Source: https://habr.com/ru/post/950811/


All Articles