#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    245
    Rep Power
    11

    SetFont doesn't work for Chinese in JTextPane on Fedora


    Hello, in the following code, there is a Chinese word in the html tag. If I set content type as "text/html", then it can't display Chinese correctly; for plain text, it's fine. I am working on Fedora linux. Can't figure out what's the problem. Thank you for any comments.

    Code:
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.util.Locale;
    
    import javax.swing.*;
    
     public class MyFont extends JPanel{
    
        public MyFont() {
            
        	String text = "<html lang=\"zh\">\n" +
                    "Color and font test:\n" +
                    "<ul>\n" +
                    "<li><font color=red>中国</font>\n" +
                    "<li><font color=blue>blue</font>\n" +
                    "<li><font color=green>green</font>\n" +
                    "<li><font size=-2>small</font>\n" +
                    "<li><font size=+2>large</font>\n" +
                    "<li><i>italic</i>\n" +
                    "<li><b>bold</b>\n" +
                    "</ul>\n";
     
            JTextPane htmlTextArea = new JTextPane();
            htmlTextArea.setContentType("text/html");
            htmlTextArea.setFont(getChineseFont());
            htmlTextArea.setText(text);
            JPanel panel = new JPanel();
            panel.add(htmlTextArea);
            add(panel);
            
        }
     
        private Font getChineseFont(){
     	   
    		Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    		String chinesesample = "中文";
    		for (int j = 0; j < allfonts.length; j++) {
    		    if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { 
    		        System.out.println(allfonts[j].getName());
    		    	return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
    		    }	  	    
    		}
    		return null;
       }
        public static void main(String[] args) {
        	JFrame frame = new JFrame("HtmlDemo");
            frame.add(new MyFont());
            frame.pack();
            frame.setVisible(true);
        }
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Where you are setting the content type for the htmlTextArea, try setting the type like this instead:
    Code:
    htmlTextArea.setContentType("text/html;charset=big5");
    See if that works for you.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    245
    Rep Power
    11
    Originally Posted by Scorpions4ever
    Where you are setting the content type for the htmlTextArea, try setting the type like this instead:
    Code:
    htmlTextArea.setContentType("text/html;charset=big5");
    See if that works for you.
    This doesn't work either.
  6. #4
  7. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Unicode escape sequence


    Try convert the Chinese characters to unicode entities:

    Java Code:
     
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.util.Locale;
     
    import javax.swing.*;
     
     public class MyFont extends JPanel{
     
        public MyFont() {
     
        	String text = "<html lang=\"zh\">\n" +
                    "Color and font test:\n" +
                    "<ul>\n" +
                    "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" +
                    "<li><font color=blue>blue</font>\n" +
                    "<li><font color=green>green</font>\n" +
                    "<li><font size=-2>small</font>\n" +
                    "<li><font size=+2>large</font>\n" +
                    "<li><i>italic</i>\n" +
                    "<li><b>bold</b>\n" +
                    "</ul>\n";
     
            JTextPane htmlTextArea = new JTextPane();
            htmlTextArea.setContentType("text/html");
            htmlTextArea.setFont(getChineseFont());
            htmlTextArea.setText(text);
            JPanel panel = new JPanel();
            panel.add(htmlTextArea);
            add(panel);
     
        }
     
        private Font getChineseFont(){
     
    		Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    		String chinesesample = "&#"+"20013;"+ "&#"+ "25991;";
    		for (int j = 0; j < allfonts.length; j++) {
    		    if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { 
    		        System.out.println(allfonts[j].getName());
    		    	return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
    		    }	  	    
    		}
    		return null;
       }
        public static void main(String[] args) {
        	JFrame frame = new JFrame("HtmlDemo");
            frame.add(new MyFont());
            frame.pack();
            frame.setVisible(true);
        }
    }


    Note:
    I have to break up the unicode entities as a series of string concatenation due to the browser's conversion of unicode entities back to Chinese characters when the post was displayed.
    Last edited by tvc3mye; December 14th, 2012 at 07:35 AM. Reason: make the unicode entities display as is.
    When the programming world turns decent, the real world will turn upside down.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    245
    Rep Power
    11
    Originally Posted by tvc3mye
    Try convert the Chinese characters to unicode entities:

    Java Code:
     
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.util.Locale;
     
    import javax.swing.*;
     
     public class MyFont extends JPanel{
     
        public MyFont() {
     
        	String text = "<html lang=\"zh\">\n" +
                    "Color and font test:\n" +
                    "<ul>\n" +
                    "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" +
                    "<li><font color=blue>blue</font>\n" +
                    "<li><font color=green>green</font>\n" +
                    "<li><font size=-2>small</font>\n" +
                    "<li><font size=+2>large</font>\n" +
                    "<li><i>italic</i>\n" +
                    "<li><b>bold</b>\n" +
                    "</ul>\n";
     
            JTextPane htmlTextArea = new JTextPane();
            htmlTextArea.setContentType("text/html");
            htmlTextArea.setFont(getChineseFont());
            htmlTextArea.setText(text);
            JPanel panel = new JPanel();
            panel.add(htmlTextArea);
            add(panel);
     
        }
     
        private Font getChineseFont(){
     
    		Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    		String chinesesample = "&#"+"20013;"+ "&#"+ "25991;";
    		for (int j = 0; j < allfonts.length; j++) {
    		    if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { 
    		        System.out.println(allfonts[j].getName());
    		    	return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
    		    }	  	    
    		}
    		return null;
       }
        public static void main(String[] args) {
        	JFrame frame = new JFrame("HtmlDemo");
            frame.add(new MyFont());
            frame.pack();
            frame.setVisible(true);
        }
    }


    Note:
    I have to break up the unicode entities as a series of string concatenation due to the browser's conversion of unicode entities back to Chinese characters when the post was displayed.
    Does that work for you? I got the same problem on Fedora.
  10. #6
  11. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Font problem?


    If this does not work on your OS, could it be that your Fedora does not have a font that supports Chinese character display.
    The following post I found from StackOverflow forum may be helpful to you:

    How to make java application display Chinese under Fedora core 5
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo