如何在JavaFX中显示控制字符Unicode?

huangapple go评论98阅读模式
英文:

How to show control characters Unicode in JavaFX?

问题

I'm using javafx 11 and jdk 8 to make a program which you can write something in a textarea and it will do some operation on that text, then it will show to result to you.

The problem is that I want to paste some special characters of unicode like "U+0011 : DEVICE CONTROL ONE [DC1]" but when I paste in the input textarea It will not appear.

Is there any way to make these kind of characters appear and get used?

These chars appear in notepad++ like this:
如何在JavaFX中显示控制字符Unicode?

英文:

<br>
I'm using javafx 11 and jdk 8 to make a program which you can write something in a textarea and it will do some operation on that text, then it will show to result to you.<br><br>
The problem is that I want to paste some special characters of unicode like "U+0011 : <control> DEVICE CONTROL ONE [DC1]" but when I paste in the input textarea It will not appear.<br>
Is there any way to make these kind of characters appear and get used?<br>
These chars appear in notepad++ like this:

如何在JavaFX中显示控制字符Unicode?

答案1

得分: 2

JavaFX似乎在插入文本之前会剥离控制字符。

您不能直接显示这些字符,但Unicode有一块专门用于显示控制字符的字符块:控制字符

因此,显示字符的可视表示非常简单:

char charToDisplay = (c >= 32 || c == '\n' ? c : (char) (c + 0x2400));

您可以相对容易地转换任何字符串:

static String makeControlCharactersVisible(String s) {
    if (s == null) {
        return s;
    }

    int len = s.length();
    StringBuilder visible = new StringBuilder(len);

    for (int i = 0; i < len; i++) {
        char c = s.charAt(i);
        visible.append(c >= 32 || c == '\n' ? c : (char) (c + 0x2400));
    }

    return visible.toString();
}

在JavaFX文本组件中,您可以拦截粘贴操作:

TextArea textArea = new TextArea() {
    @Override
    public void paste() {
        String text = Clipboard.getSystemClipboard().getString();
        replaceSelection(makeControlCharactersVisible(text));
    }
};

这种方法有两个缺点:

  1. 大多数字体在正常字体大小(如12号字体)下都难以阅读控制字符的字符图像。
  2. 可能有用户希望实际粘贴控制字符,这样就无法确定他们是否粘贴了类似 '\u0011''\u2411' 这样的字符。
英文:

It appears JavaFX is stripping control characters before inserting text.

You can’t show the characters directly, but Unicode has a block of characters specifically for showing control characters: Control Pictures.

So, showing a visual representation of a character is as easy as:

char charToDisplay = (c &gt;= 32 || c == &#39;\n&#39; ? c : (char) (c + 0x2400));

You can transform any String fairly easily:

static String makeControlCharactersVisible(String s) {
    if (s == null) {
        return s;
    }

    int len = s.length();
    StringBuilder visible = new StringBuilder(len);

    for (int i = 0; i &lt; len; i++) {
        char c = s.charAt(i);
        visible.append(c &gt;= 32 || c == &#39;\n&#39; ? c : (char) (c + 0x2400));
    }

    return visible.toString();
}

In a JavaFX text component, you can intercept the paste:

TextArea textArea = new TextArea() {
    @Override
    public void paste() {
        String text = Clipboard.getSystemClipboard().getString();
        replaceSelection(makeControlCharactersVisible(text));
    }
};

There are two disadvantages to this:

  • Most fonts have terrible glyphs for the control pictures characters, which are barely readable at normal font sizes like 12 points.
  • It’s conceivable that a user might want to actually paste a control picture character, in which case there would be no way to know whether they pasted, for example, &#39;\u0011&#39; or &#39;\u2411&#39;.

huangapple
  • 本文由 发表于 2020年7月28日 17:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63131543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定