在Applet中显示中文文本

发布于 2024-08-11 19:01:41 字数 154 浏览 5 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒夜笙歌凉 2024-08-18 19:01:41

这表明该字体不支持中文字符(您可能已经猜到了)。

您可能会发现 java.awt.Font.canDisplayUpto() 方法很有趣。

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

"指示此 Font 是否可以显示指定的字符串。对于字符串对于 Unicode 编码,了解特定字体是否可以显示字符串非常重要,如果该字体可以显示所有字符,则该方法将返回字符串 str 中的第一个字符,该偏移量是该字体无法显示的第一个字符。 ,返回-1。”

This indicated that the font does not support Chinese characters (which you probably guessed).

You might find the java.awt.Font.canDisplayUpto() method interesting.

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

"Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned."

迷爱 2024-08-18 19:01:41

这是因为 Windows 和 Mac 上的 Arial 都是 Unicode 字体,但在 Linux 上只有 Latin-1 字符集。在许多 Linux 发行版上,中文字体是可选的,并且可能没有可用的中文字体。

一种常见的技术是搜索所有字体以查看其中任何一个可以显示汉字。例如,

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

}

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

}
愚人国度 2024-08-18 19:01:41

您可能必须在对象标记中传递以下参数:

You might have to pass the following param in the object tag:
<param name="java_arguments" value="-Dfile.encoding=utf-8" />

音栖息无 2024-08-18 19:01:41

我发现这里的代码不足以满足我的需求。

我需要测试未知的输入字符串,以确定使用什么字体,因此,我需要检查每个字符。 (见下文)

顺便说一句,font.canDisplayUpTo 方法将不起作用。它可能会批准只能显示某些字符的字体。

因此,只需使用下面的代码即可。

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

I found the code here inadequate for my needs.

I needed to test an unknown input string, to determine what font to use, hence, I needed to check every single character. (see below)

By the way, the font.canDisplayUpTo method will not work. It may approve a font, that can only display some of the 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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文