Display Chinese text in applet

We have an applet that can display Chinese text. We specify the font for it (Arial), it works fine under Windows and Mac OSX.

But on Firefox on Linux, Chinese characters appear as squares. Is there any way around this? Please note that we cannot assume that a particular font file exists on the client.

+3
source share
4 answers

This is because Arial on Windows and Mac is all Unicode font, but on Linux it has only Latin encoding. On many Linux distributions, Chinese fonts are optional and may not be available in Chinese font.

, , . ,

static final Font defaultFont =new Font( "Arial Unicode MS", Font.BOLD, 48 );

static private Font[] allFonts;

static final char sampleChineseCharacter = '\u4F60';  // ni3 as in ni3 hao3

public static void loadFonts() {

    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

    allFonts = env.getAllFonts();
    int nFonts = allFonts != null ? allFonts.length : 0;
    fontNames = new String[nFonts];
    fontMap = new Hashtable();
    String currentFamily = "";
    int j = 0;

    for ( int i = 0; i < nFonts; i++ ) {

        Font font = allFonts[ i ];

        System.out.println( allFonts[ i ] );

        if ( font.canDisplay( sampleChineseCharacter )) {

                currentFamily = font.getFamily();

                Object key = fontMap.put( currentFamily, font );

                if ( key == null ) {

                    // The currentFamily hasn't been seen yet.

                    fontNames[ j ] = currentFamily;
                    j++;

                }

        }

    }

    String tmp[] = fontNames;
    fontNames = new String[j];
    System.arraycopy( tmp, 0, fontNames, 0, j );

}
+2

, ( , , ).

java.awt.Font.canDisplayUpto().

http://www.j2ee.me/javase/6/docs/api/java/awt/Font.html#canDisplayUpTo(java.lang.String)

", . Unicode , . String, . Font , -1.

+3

, : <param name="java_arguments" value="-Dfile.encoding=utf-8" />

0
source

I found the code here inadequate for my needs.

I needed to check an unknown input line to determine which font to use, therefore, I needed to check each character. (see below)

By the way, the font.canDisplayUpTo method will not work. It can approve a font that can only display some characters.

So just use this code below.

Font[] allFonts;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

allFonts = env.getAllFonts();

Font targetFont = null;
for ( int i = 0; i < allFonts.length; i++ ) 
{
    Font font = allFonts[ i ];
    boolean canDisplayAll = true;
    for (char c : text.toCharArray())
    {
        if (!font.canDisplay(c))
        {
            canDisplayAll = false;
            break;
        }
    }
    if (canDisplayAll)
    {
        logger.debug("font can display the text " + font.getName());
        targetFont = font;
        break;
    }
    else
    {
        logger.debug("cant display " + font.getName());
    }
}
0
source

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


All Articles