• Post Reply Bookmark Topic Watch Topic
  • New Topic

A fall back font for Swing?  RSS feed

 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am doing some i18n work on my swing application. At the moment, the application works perfectly in English. I have embeded a few font files in the application so that all text displayed in English will use those fonts. I do it like this:


The problem is that those font files only support basic Latin character set. My application will not show the correct text if it is trying to display other languages like CJK(Chinese-Japanese-Korean). To prevent an huge increase of file size, I wish not to change the font to another font which supports larger character set. Instead, I hope the application will fall back to the default Swing/system font which can display the text correctly.

How should I change my code to handle that? Nevertheless, I hope it is not going to be a big change, because I have all over everywhere in different custom GUI-generating classes.

I thank you in advanced.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why specifying a useful font would cause a "huge increase of file size" compared to automatically falling back to that same useful font. If it were my application and I expected to be supporting CJK scripts, I would just use a font which supported them.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't understand why specifying a useful font would cause a "huge increase of file size" compared to automatically falling back to that same useful font. If it were my application and I expected to be supporting CJK scripts, I would just use a font which supported them.

May be I am not understanding your message very well but let me explain my situation.

A font that covers only the Western European character sets would probably be around than 50kb or fit comfortably under 100kb, but an usual font that covers complete CJK character sets would probably be a few megabytes or even measures in 10s of megabytes.

If I embed a few font files to cover the basic Latin character set, my application can easily fit under 1Mb, but if I embed a few CJK compatible fonts, my application would become 10s of megabytes in size.

If I don't embed the CJK font files and let the application use the client system's fonts, my application could save a lot of file space while keeping the interface usable in another language.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Do you know how to get all the installed Fonts? If not, go to the API for java.awt.Font, click "Use" at the top of the page, and look for a method with a return type of Font[]. Then go through the API for that method and other methods of the class it's in.

2. Do you know how to determine if a Font can display a specific String? If not, find the appropriate method in the Font API.

You can iterate over the available Fonts to identify those that are usable for the purpose, and provide an error message if none are found.
 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:1. Do you know how to get all the installed Fonts? If not, go to the API for java.awt.Font, click "Use" at the top of the page, and look for a method with a return type of Font[]. Then go through the API for that method and other methods of the class it's in.

2. Do you know how to determine if a Font can display a specific String? If not, find the appropriate method in the Font API.

You can iterate over the available Fonts to identify those that are usable for the purpose, and provide an error message if none are found.


I know that using canDisplayUpTo can determine if a Font can display a specific String.

However, I don't think iterating a list of system fonts is a good way of doing it. For instance, a typical Taiwanese Traditional Chinese Windows XP system has about 70 default fonts installed but among these, there are only 3 fonts that is able to display Traditional Chinese. It can be very inefficient to loop through the list to display every string for each component. That's my biggest concern for why I did not choose this method in the beginning.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Chan wrote:It can be very inefficient to loop through the list to display every string for each component. That's my biggest concern for why I did not choose this method in the beginning.


Yes, I agree. So what I would to would be to go through the list as part of initializing the application, and choose a suitable font to be used throughout the application. That also solves the potential problem where you might choose different fonts for different components, or even for the same component at different times, which would be detrimental to the GUI.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!