简体   繁体   English

Java Unicode字符宽度

[英]Java Unicode character widths

I am attempting to program a chess game in Java that will display entirely in the console. 我试图用Java编写一个象棋游戏,该游戏将完全显示在控制台中。 I am using Unicode characters for this such as White Chess Queen U+2655. 我为此使用Unicode字符,例如White Chess Queen U + 2655。 My problem is that I cannot find any whitespace characters that match the width of the chess pieces so I can't display the board correctly. 我的问题是我找不到与国际象棋棋子的宽度匹配的空白字符,所以我无法正确显示棋盘。 Take a look at my output below, I moved the pawn from h2 to h4: 看看下面的输出,我将棋子从h2移到了h4:

8 ♜♞♝♛♚♝♞♜
7 ♟♟♟♟♟♟♟♟
6                 
5                 
4               ♙
3                 
2 ♙♙♙♙♙♙♙  
1 ♖♘♗♕♔♗♘♖
  a b c d e f g h 

Edit: the problem is easier to point out with the following example: 编辑:使用以下示例更容易指出问题:

♙♙♙♙♙♙♙♙
xxxxxxxx
iiiiiiii
||||||||

You can see that the chess pieces are slightly larger in width than the other characters below the pawns. 您会看到棋子的宽度比典当下的其他字符稍大。

Here on this website, the chess pieces have slightly more than the width of a standard character. 在此网站上,棋子的宽度略大于标准字符的宽度。 In my console in Eclipse, the chess pieces have a width that is slightly less than 2 standard characters. 在我的Eclipse控制台中,棋子的宽度略小于2个标准字符。

I suppose I am requesting one of three things: 我想我要的是以下三件事之一:

  • a whitespace character that matches the width of the Unicode chess pieces. 一个与Unicode棋子的宽度匹配的空白字符。
  • a Java method that returns the width of a character or string 返回字符或字符串宽度的Java方法
  • a Java method that can set the width of a character or string to any arbitrary value. 一种Java方法,可以将字符或字符串的宽度设置为任意值。

Thanks for any help or advice you can offer. 感谢您提供的任何帮助或建议。

Edit: turns out it was the consolas font that was causing me problems. 编辑:原来是consolas字体引起了我的问题。 Consolas, displays chess pieces with a very strange width, about 1.8 characters wide. Consolas展示的棋子宽度非常奇怪,大约为1.8个字符。 I switched the font of my console to a few different monospaced fonts until I found one that correctly sized the chess pieces. 我将控制台的字体切换为几种不同的等宽字体,直到找到一种可以正确调整棋子大小的字体。 Is there any way to ensure specific widths, regardless of the font? 有什么方法可以确保特定的宽度,而不管字体是什么?

Terminals (almost) always use monospaced fonts, so the space character is the same width as the piece characters. 终端(几乎)始终使用等宽字体,因此空格字符与分段字符的宽度相同。

Your problem is that you're rendering the board with spaces between columns, but you're not inserting them between your pieces. 您的问题是您要渲染的板在各列之间留有间隔,但没有在零件之间插入它们。

public class TextChess {
    public static void main(String[] args) {
        char[][] board = new char[][] {
            {'♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'},
            {'♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
            {'♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'},
            {'♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'}
        };

        for (int i = 0; i < board.length; i++) {
            char[] pieces = board[i];
            System.out.print("" + (8 - i) + " ");
            for (char piece : pieces) {
                System.out.print(piece + " "); // you're missing this space
            }
            System.out.println();
        }
        System.out.println("  a b c d e f g h");
    }
}

Prints: 印刷品:

8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 
6                 
5                 
4                 
3                 
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ 
  a b c d e f g h  

If you're not using a monospaced font the width of the pieces will vary with the font. 如果您不使用等宽字体,则片段的宽度将随字体而变化。 They might match one of the unicode spaces, perhaps the en-space ( \  ) or the em-space ( \  ). 它们可能与unicode空间之一匹配,可能是en空间( \  )或em空间( \  )。 Any solution would be specific to your environment. 任何解决方案都将特定于您的环境。 Ref: unicode spaces . 参考: unicode空格

Edit: I did some experiments. 编辑:我做了一些实验。 In most fonts (notably not my version of Helvetica tho') the chess glyphs are the same width as the em-quad ( \  ). 在大多数字体中(尤其不是我的Helvetica tho'版本),国际象棋字形的宽度与em-quad相同( \  )。 You'll still run in to problems with your column labels. 您仍然会遇到列标签问题。 I noticed that the enclosed alphanumeric glyphs are mostly the same width as the chess pieces too, so you could label the columns ⒜-⒣ instead of ah (ie \⒜-\⒣ ). 我注意到,封闭的字母数字标志符号也与国际象棋棋子的宽度大致相同,因此您可以将列⒜-⒣而不是ah标记为(即\⒜-\⒣ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM