英文:
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 :
Is there any way to make these kind of characters appear and get used?
These chars appear in notepad++ like this:
英文:
<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:
答案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));
}
};
这种方法有两个缺点:
- 大多数字体在正常字体大小(如12号字体)下都难以阅读控制字符的字符图像。
- 可能有用户希望实际粘贴控制字符,这样就无法确定他们是否粘贴了类似
'\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 >= 32 || c == '\n' ? 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 < len; i++) {
char c = s.charAt(i);
visible.append(c >= 32 || c == '\n' ? 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,
'\u0011'
or'\u2411'
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论