[Post] font custom settings and beautification in Java 5/6 (Linux/Windows)

Source: Internet
Author: User

Font customization and beautification in Java 5/6 (Linux/Windows)
By default, the Chinese and English fonts in the Java application are used separately, in this way, you cannot use an English font or a Chinese font to display Chinese and English characters in the same program. For example, in netbeans, you cannot use the courser 10 pitch font (courser new in Windows) or a certain Chinese font (such as or) to display a Java source code. If you select the courser font, the Chinese characters in the source code cannot be displayed. If you select or, the English font uses a default ing font.
Many methods are provided on the network, but some simple methods are provided to solve the Java Chinese garbled problem. The basic principles are not clearly stated.
This article provides a complete font setting method, and provides the font configuration files tested on ubuntu7.04 and Windows XP for your reference.
This article is a bit cool, but I think if you read Sun's official font configuration file documents, you should have a comprehensive understanding of Java font configuration.
Sun official document address for font configuration files: http://java.sun.com/j2se/1.5.0/docs/guide/intl/fontconfig.html
Other references:
Http://blog.csdn.net/oldjavaman/archive/2005/05/27/382110.aspx
Http://community.csdn.net/Expert/TopicView3.asp? Id = 5663604

I. Introduction to font configuration files
The first thing to understand is the loading scheme of the Java font configuration file.
To locate the application font configuration file in an operating system, you must understand the following information:
* The javahome-JRE directory is like the "Java. Home" provided by system properties ".
* OS-a string that identifies an operating system:
O for windows, "98", "2000", "me", "XP", "2003 ".
O For Solaris, null.
O For Linux, "Sun", "RedHat", "Turbo", "SuSE ".
* Version-a string that identifies the operating system version.
When Java runs, it will use the first of the following files (some people say they are loaded in this order, but officially refer to the first of these files ):
Javahome/lib/fontconfig. OS. version. Properties
Javahome/lib/fontconfig. OS. version. BFC
Javahome/lib/fontconfig. OS. Properties
Javahome/lib/fontconfig. OS. BFC
Javahome/lib/fontconfig. version. Properties
Javahome/lib/fontconfig. version. BFC
Javahome/lib/fontconfig. Properties
Javahome/lib/fontconfig. BFC
Files suffixed with. properties are in text format, while others are in binary format. You need to modify the text file with the. properties suffix, that is, the font configuration file. This file is divided into several sections and marked with # At the beginning of each section:
* Version attributes
# Version
Used to identify the version of the font configuration file format. Set this parameter to 1.
Version = 1

* Component font ing
# Component font Mappings
Component font ing attributes describe the logical font (logical font) of a given style to determine which physical font is used (character subset) to display characters... (This sentence is too difficult to translate: (I don't know if the translation is correct, but the meaning is similar. The original sentence: component font mapping properties Describe which physical font to use to render characters from a given character subset with a given logical font in a given style .) to put it simply, the font is mapped (logical font here) in the following form ):
All fonts. Character subset name = physical font name
Allfonts. charactersubsetname = platformfontname
Logical font name. Style name. Character subset name = physical font name
Or:
Logicalfontname. stylename. charactersubsetname = platformfontname
The physical font names in Linux/Solaris are different from those in windows, and the character subset names are also different.
For example, a ing in Linux/Solaris:
Monospaced. Bold. Latin-1 =-bitstream-Courier 10 pitch-bold-r-normal -- *-% d-*-m-*-iso8859-1
Monospaced. Bold. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
A ing in Windows:
Monospaced. Bold. alphabetic = Courier New bold
Monospaced. Bold. chinese-ms950 = pmingliu
Monospaced indicates the logical font name. The logical font name can only be one of the five logical font names: Serif, sansserif, monospaced, dialog, and dialoginput. Bold is a bold font style. The font style can only be one of the four styles: Plain (normal), bold (BOLD), italic (italic), and bolditalic (bold + italic ). Latin-1 and alphabetic are character subset names of letters. The former is used in Linux and the latter is used in windows. The right side of the equal sign is the physical font name, which has a big difference in the representation of Linux/Solaris and windows. In Linux/Solaris, it is a more professional representation, in Windows, what you see is what you get (it can be Chinese, but it must be represented in unicode format, which will be mentioned later ).
The component font ing property is a part that needs to be modified.

* Search order
# Search Sequences
When Java is running, the search order of five logical fonts is determined by the order attribute. However, a font configuration file may specify the special attributes of a combination of encoding, language, and country, the search sequence attribute of each logical font is determined by searching. This is also a part to be modified.

* Font file name
# Font file names
The font file name attribute provides the file name that contains the physical font used by the font configuration file. The names of all physical fonts in windows are required. The names of all physical fonts in Solaris and Linux are recommended. (The font file names in the two operating systems vary greatly .)

There are other attributes. For details, see Sun's official documentation. They do not need to be modified. Some of them do not even need to be specified in the font configuration file:
# Exclusion ranges
# Proportional Fonts
# Appended font path
# Motif component font Mappings
# AWT X11 font path

Sun official document address of the font configuration file: http: // blog.csdn.net/oldjavaman/archive/2005/05/27/382110.aspx

Ii. Modification and beautification
Now we can customize and modify the font configuration file.
First, the font you want to use may not be specified in the font configuration file. You must specify the physical font you want to use ), it is mapped to the representation (platformfontname) of different operating systems ).
For example, if you want to select the font (msyh. TTF) and courser fonts, we assume that you have installed these fonts on the operating system.
In Linux (ubuntu7.04), we are at/etc/Java-6-sun/fontconfig. properties (ubuntu7.04 JDK installed through apt-Get. If JDK is manually installed, the fontconfig file is in the javahome/JRE/lib directory and fontconfig. properties. the SRC file is renamed to fontconfig. properties) file # font file names section add:
Filename.-microsoft-microsoft_yahei-medium-r-normal -- *-% d-*-p-*-gb18030.2000-0 =/usr/share/fonts/yahei/msyh. TTF
Note: filename is fixed, followed by a dot (.), next is the physical name of the font (excluding quotation marks) "-Microsoft-microsoft_yahei-medium-r-normal -- *-% d-*-p-*-gb18030.2000-0 ", this name is in fonts. in the Dir file, "-Microsoft yahei-Medium-r-normal -- 0-0-0-0-p-0-gb18030.2000-0". The difference is that the space in the name must be replaced, change---0-0-0-p-0-to -- *-% d-*-p-*-. % d indicates the font size, which is automatically filled in during running. Medium-r-normal R indicates the font of the normal style (msyh. TTF only contains this style ). The right side of the equal sign is the absolute path of the msyh. TTF file.
Then, we can use this name in the font configuration file.
In the # component font mappings section, replace all Chinese Text bodies with the name, as shown below:
Allfonts. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Allfonts. chinese-gb18030.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Allfonts. chinese-big5 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Allfonts. chinese-big5.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
And map the Chinese character subset of the logical font used in the program to, as follows (monospaced is used here ):
Monospaced. Plain. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Monospaced. Plain. chinese-gb18030.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0

Monospaced. Bold. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Monospaced. Bold. chinese-gb18030.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0

Monospaced. italic. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Monospaced. italic. chinese-gb18030.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0

Monospaced. bolditalic. chinese-gb18030 =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0
Monospaced. bolditalic. chinese-gb18030.motif =-Microsoft yahei-Medium-r-normal -- *-% d-*-p-*-gb18030.2000-0

Now, adding the font to the Java application has been completed. Open netbeans and check that the Chinese version of the netbeans startup interface and the Chinese font in the IDE have all become. However, the English font is not desired. The default is the monospaced logical font, which maps the English font to a font that doesn't know what it is called. Especially in the source code, if the courser 10 pitch font is selected, the Chinese characters in the Code cannot be displayed. It is difficult to use the default monospaced font to display English characters. Next, we map the monospaced logical font in English to courser 10 pitch.
In ubuntu, the physical file of courser 10 pitch is located in c0419bt _ in the/usr/share/fonts/X11/type1/directory _. pfb (normal), c0582bt _. pfb (BOLD), c0583bt _. pfb (italic), c0611bt _. pfb (bold + italic ). In the # font file names segment, add the Mappings of these files:
Filename.-bitstream-courier_10_pitch-medium-r-normal -- *-% d-*-m-*-iso8859-1 =/usr/share/fonts/X11/type1/c0419bt _. pfb
Filename.-bitstream-courier_10_pitch-medium-i-normal -- *-% d-*-m-*-iso8859-1 =/usr/share/fonts/X11/type1/c0582bt _. pfb
Filename.-bitstream-courier_10_pitch-bold-r-normal -- *-% d-*-m-*-iso8859-1 =/usr/share/fonts/X11/type1/c0583bt _. pfb
Filename.-bitstream-courier_10_pitch-bold-i-normal -- *-% d-*-m-*-iso8859-1 =/usr/share/fonts/X11/type1/c0611bt _. pfb
Description: Medium-r is the normal style, medium-I is the italic style, bold-r is the bold style, bold-I is the bold + italic style.
In the # component font mappings section, change the Latin-1 Character Set of the monospaced4 style fonts:
Monospaced. Plain. Latin-1 =-bitstream-Courier 10 pitch-Medium-r-normal -- *-% d-*-m-*-iso8859-1
Monospaced. Plain. latin-1.motif =-bitstream-Courier 10 pitch-Medium-r-normal -- *-% d-*-m-*-iso8859-1

Monospaced. Bold. Latin-1 =-bitstream-Courier 10 pitch-bold-r-normal -- *-% d-*-m-*-iso8859-1
Monospaced. Bold. latin-1.motif =-bitstream-Courier 10 pitch-bold-r-normal -- *-% d-*-m-*-iso8859-1

Monospaced. italic. Latin-1 =-bitstream-Courier 10 pitch-Medium-I-normal -- *-% d-*-m-*-iso8859-1
Monospaced. italic. latin-1.motif =-bitstream-Courier 10 pitch-Medium-I-normal -- *-% d-*-m-*-iso8859-1

Monospaced. bolditalic. Latin-1 =-bitstream-Courier 10 pitch-bold-I-normal -- *-% d-*-m-*-iso8859-1
Monospaced. bolditalic. latin-1.motif =-bitstream-Courier 10 pitch-bold-I-normal -- *-% d-*-m-*-iso8859-1

Okay, save the file and open netbeans again. Now you can set the source code font to monospaced. The English font in the Code is Courier 10 pitch, and the Chinese character is. (Netbeans font is set to "tool"-> "option"-> "font and color"-> "Syntax". By default, Java uses other English fonts, can be set to monospaced manually)

Windows XP is simpler. You do not have to re-write a font configuration file, and change the javahome \ JRE \ Lib \ fontconfig. properties. SRC file (Windows) to fontconfig. properties. Suppose the font has been installed in the C: \ WINDOWS \ fonts \ Folder. In the # Font File Names Section of the fontconfig. properties file, add the:
Filename. \ u5fae \ u8f6f \ u96c5 \ u9ed1 = msyh. TTF
Filename. \ u5fae \ u8f6f \ u96c5 \ u9ed1_bold = msyhbd. TTF

Then, change the font of the Chinese subset of allfonts in the # component font mappings section to, as shown below:
Allfonts. chinese-ms936 = \ u5fae \ u8f6f \ u96c5 \ u9ed1
Allfonts. chinese-gb18030 = \ u5fae \ u8f6f \ u96c5 \ u9ed1
Among them, ms936 is the GBK character set, and ms950 is actually the big5 character set. In traditional Chinese, you can also use.
You can see that the ing of the default Simplified Chinese fonts for the five logical fonts under Windows adopts the allfonts settings, so you do not need to set them elsewhere.
In English, monospaced is mapped to the courier new font by default, but why cannot it be displayed in Java programs (such as netbeans?
Find sequence. monospaced. GBK = chinese-ms936, alphabetic, dingbats, symbol
We can see that the Chinese font search order is first search chinese-ms936, And the chinese-ms936 is to use allfonts to display English, so it is not easy to see, so you just need to simply switch the two chinese-ms936 and alphabetic:
Sequence. monospaced. GBK = alphabetic, chinese-ms936, dingbats, symbol
After saving the file, restart netbeans and change the English font in the editor to Courier New, including the font of the output window.

This method was both modified and optimized in Windows XP, UBUNTU 7.04, and JDK 5/6, respectively. However, I haven't tested whether Java Garbled text can be solved yet, because the Java Garbled text problem on my computer has long been solved, I am too lazy to try again. If you are interested, you can test it.
Now, you should have some knowledge about Java font configuration. Now you can change the font of the Java swing program to your favorite one :).

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/AutumnHealth/archive/2007/09/19/1791222.aspx

 

The other method is not sure whether it is valid

Setting the font in the options can solve the problem in the code bar, but the debugging column and so on are still garbled.

I have found many solutions on the Internet, which are effective in the following ways:

  1. Find the ETC folder under your netbeans installation directory
  2. Open netbeans. conf in Notepad
  3. Find netbeans_default_options (not the last one with a full stop...), add a space at the end of its properties (within the colon), plus-J-Dfile.encoding = UTF-8

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.