在小程序中显示中文文本 [英] Displaying Chinese text in an Applet

查看:59
本文介绍了在小程序中显示中文文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个可以显示中文文本的小程序.我们为它指定了一种字体 (Arial),它在 Windows 和 Mac OSX 下都可以正常工作.

We have an Applet that can possibly display Chinese text. We are specifying a font for it (Arial), it works fine under both Windows and Mac OSX.

但在 Linux 上的 Firefox 中,中文字符呈现为正方形.有没有办法解决这个问题?请注意,我们不能假设客户端上存在特定的字体文件.

But in Firefox on Linux the Chinese characters are rendered as squares. Is there a way to work around this? Note that we can't assume the existence of a particular font file on the client.

推荐答案

那是因为 Windows 和 Mac 上的 Arial 都是 Unicode 字体,而在 Linux 上它只有 Latin-1 字符集.在许多 Linux 发行版上,中文字体是可选的,可能没有中文字体可用.

That's because Arial on Windows and Mac are all Unicode font but it only has Latin-1 charset on Linux. On many Linux distributions, Chinese fonts are optional and there may not be Chinese font available.

一种常见的技术是搜索所有字体,看看它们中的任何一个都可以显示中文字符.例如,

A common technique is to searching through all your font to see any of them can display Chinese characters. For example,

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 );

}

这篇关于在小程序中显示中文文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆