代码之家  ›  专栏  ›  技术社区  ›  Roy Tang

在小程序中显示中文文本

  •  3
  • Roy Tang  · 技术社区  · 16 年前

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

    但在Linux上的火狐中,汉字被渲染为正方形。有办法解决这个问题吗?注意,我们不能假定客户机上存在特定的字体文件。

    4 回复  |  直到 14 年前
        1
  •  2
  •   ZZ Coder    16 年前

    这是因为Windows和Mac上的Arial都是Unicode字体,但在Linux上只有拉丁-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 );
    
    }
    
        2
  •  3
  •   Thorbjørn Ravn Andersen    16 年前

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

    您可能会发现Java.AWT.Ford.CANDISPLAIPUTO()方法很有趣。

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

    “指示此字体是否可以显示指定的字符串。对于使用Unicode编码的字符串,必须知道特定字体是否可以显示该字符串。此方法将偏移量返回到字符串str中,字符串str是此字体在不使用缺少的glyph代码的情况下无法显示的第一个字符。如果字体可以显示所有字符,则返回-1。“

        3
  •  0
  •   Bozho    16 年前

    您可能需要在对象标记中传递以下参数: <param name="java_arguments" value="-Dfile.encoding=utf-8" />

        4
  •  0
  •   gregm    15 年前

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

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

    顺便说一下,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());
        }
    }