(Some of them refer to other people's documents on the Internet)
Apache's Batik is a library for processing SVG. This is intended to use it as a filter servlet. When the browser does not support SVG, it will automatically draw a PNG Image and send it to the client. I thought it was very simple, .... depressed, this problem took me two days. Now, in retrospect, there are different causes of errors before and after the problem, but the problems are similar, as a result, I personally think that two errors are caused by the same reason, which wastes a lot of time to check. start drawing. What is Chinese ??? Needless to say, it must be a problem of Chinese encoding. After fixing and drawing, it looks normal. However, after the official operation, it is found that some Chinese characters in the image cannot be displayed ", I thought it was still the encoding problem. I plunged into Batik and checked it for half a day. I didn't see any improper handling of the encoding. Finally, I checked graphics2d all the way. drawxxx, there is really no way, and it is native code again, so I checked again later, but it still does not work, almost gave up. This morning, I was so happy that I suddenly thought about whether it was a JDK bug. So I wrote a JSP,CodeAs follows:
Graphicsenvironment Ge = Graphicsenvironment. getlocalgraphicsenvironment ();
String fontnames [] = GE. getavailablefontfamilynames ();
String path = Application. getrealpath ( " /Client " ) + " /Test " ;
// Iterate the font family names
For ( Int I = 0 ; I < Fontnames. length; I ++ ){
String fontname = Fontnames [I];
Out . Print (fontname );
Bufferedimage = New Bufferedimage ( 400 , 400 , Bufferedimage. type_3byte_bgr );
Graphics big = Bufferedimage. creategraphics ();
Big. setfont ( New Font (fontname, Font. Plain, 30 ));
Big. drawstring ( " Hello, send to " , 100 , 100 );
Outputstream OS = New Fileoutputstream (Path + " / " + Fontname + " . Jpg " );
Jpegimageencoder Encoder = Using codec. createjpegencoder (OS );
Encoder. encode (bufferedimage );
OS. Close ();
}
Use the system to list all the fonts and draw a picture one by one. The problem was found.
1. No simsun font! Khan ~~~, I don't know what fonts are used for the part of Chinese that can be drawn.
2. In the same way as batik, some Chinese characters cannot be drawn, which is a box.
After Google surfing the internet, we found that jdk5.0 in Linux is different from the previous version. By default, it does not support Chinese fonts. You have to manually create a fontconfig configuration. This file is under $ java_home/JRE/lib/, and there are a lot of fontconfig. XX. properties, the official explanation here, too lazy to read so much, directly create your own, first copy simsun from windows. TTC to the fonts directory under lib
Then compile a simple# @ (#) Linux. fontconfig. Suse. Properties1.2 03/10/17
#
# Copyright2003Sun Microsystems, Inc.AllRights Reserved.
#
# Version
Version=1
# Component font Mappings
Serif. Plain. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Serif. Bold. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Serif. italic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Serif. bolditalic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Sansserif. Plain. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Sansserif. Bold. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Sansserif. italic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Sansserif. bolditalic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Monospaced. Plain. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Monospaced. Bold. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Monospaced. italic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Monospaced. bolditalic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialog. Plain. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialog. Bold. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialog. italic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialog. bolditalic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialoginput. Plain. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialoginput. Bold. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialoginput. italic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
Dialoginput. bolditalic. ZH - 1 = Simsun GB - Medium - R - Normal -- 0-0-0-0-c-0-gb2312.1988-0
# Search Sequences
Sequence. allfonts=ZH-1
# Exclusion ranges
# FontFileNames
The name in the middle of the name fontconfig. RedHat. properties is the operating system name. For example, the RedHat system is saved as fontconfig. RedHat. properties, and the Windows XP system is saved as fontconfig. XP. properties, and so on.
Restart Java.
Lessons learned
- Do not use unfamiliar third-party libraries easily
- If you have any problems, do not give them a tip.
- Do not debug third-party libraries when Google is not easy to use