java - Java 中的某些 unicode 字符不显示在 JTextField 中

标签 java unicode fonts java-10

我们最近将我们一位客户的 Oracle 数据库从 Windows-1252 转换为 Unicode(我不知道他们是如何忍受的;他们保存了许多个人姓名)。

一切正常,除了一些角色。正是字符 (下面有一个点的 s)让问题进入了我们的世界。 据我所知,当未指定时,Java 使用其逻辑字体来查找适用于每个操作系统的字体(请参阅 https://docs.oracle.com/javase/tutorial/2d/text/fonts.html)。

我建立了一个简单的例子,这是来自 https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html 的例子:

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of Oracle or the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* TextDemo.java requires no other files. */
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Chartest extends JPanel implements ActionListener {
    protected JTextField textField;
    protected JTextArea textArea;
    private final static String newline = "\n";

    public Chartest() {
        super(new GridBagLayout());
        // UIManager.getLookAndFeelDefaults().put("TextField.font", new Font("Arial",
        // Font.PLAIN, 14));
        textField = new JTextField(20);
        textField.addActionListener(this);

        textArea = new JTextArea(5, 20);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        // Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.HORIZONTAL;
        add(textField, c);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
    }

    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        textArea.append(text + newline);
        textField.selectAll();

        // Make sure the new text is visible, even if there
        // was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    /**
     * Create the GUI and show it. For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add contents to the window.
        frame.add(new Chartest());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event dispatch thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

我唯一添加的是以下行:

// UIManager.getLookAndFeelDefaults().put("TextField.font", new Font("Arial", Font.PLAIN, 14));

如果没有这一行, 在文本字段中输入时会变成一个正方形。如果你取消注释这一行,它就起作用了。查看Java lib文件夹下的fontconfig.properties.src,所有逻辑字体都指向可以显示的字体:Times, Courier, Arial。我将文件重命名为 fontconfig.properties 并将所有内容更改为 Arial 以覆盖默认值。它没有改变任何东西。

这也不起作用:

UIManager.getLookAndFeelDefaults().put("TextField.font", new Font("Dialog", Font.PLAIN, 14));

尽管 Dialog 显然在 fontconfig.properties.src 中引用了 Arial。
我忽略了什么?我正在使用 Java 10。

最佳答案

逻辑字体的渲染似乎是一个谜。在一次测试运行中,我在 JTextArea 中正确呈现了相关字符,但在 JTextField 中却没有,而 textArea.getFont()==textField.getFont () 评估为 true!

fontconfig.properties.src 指的是 Arial,但它也包含这一行:

exclusion.alphabetic=0700-1e9f,1f00-2017,2020-20ab,20ad-20b8,20bb-20bc,20be-f8ff

and and (U+1e63 and U+1e62) 正好在这个范围内。

我不知道这些明显的“字母”字符在被排除在外时应该在哪里结束。 This document只是说:

This is used if a font with a large character repertoire needs to be placed early in the search sequence (for example, for performance reasons), but some characters that it supports should be drawn with a different font instead.

但我不知道为什么不应该使用配置为“字母”的第一个字体呈现这些字符,而搜索序列中的其他字体应该呈现它们。

此外,这个范围看起来很随意,即它意味着虽然 在排除范围内,但 不是并且可以正确呈现。

当你把这一行变成评论时,即

#exclusion.alphabetic=0700-1e9f,1f00-2017,2020-20ab,20ad-20b8,20bb-20bc,20be-f8ff

问题消失了。

我有一种感觉,原来的排除条款导致渲染以嵌入的 Lucida 字体结束,无法渲染这些字符。在 JDK 11 中,这个排除行仍然存在并且是更新版本:

exclusion.alphabetic=0700-1cff,1d80-1e9f,1f00-2017,2020-20ab,20ad-20b8,20bb-20bc,20be-f8ff

仍然涵盖 (U+1e63U+1e62)。但是 Lucida 字体已被删除,因此,AWT 代码中对这些字体的硬编码引用也必须已被删除。

作为 JDK 10 和 JDK 11 之间所做更改的最终结果,字符 可以正确呈现。

所以最重要的是,与其尝试修复 JDK 10 的 AWT 配置,您还可以简单地更新到 JDK 11。

这适用于 OpenJDK 11 和 Oracle JDK 11。它们的字体配置文件仅在许可证 header 注释上有所不同……

关于java - Java 中的某些 unicode 字符不显示在 JTextField 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52851735/

相关文章:

java - 哪个 Eclipse 下载用于 Java ME 开发

java - 如何让 NetBeans 在 src 文件夹而不是 dist 文件夹中搜索我的文件?

Delphi 2009 和 Firebird 2.1 = 完整的 Unicode?

php - 正则表达式匹配任何字符串,无论是否为 Unicode?

java - Spring Boot 配置是否带有预设默认值?

java - Class如何寻找资源?

unicode - Lua支持Unicode吗?

java - 从资源文件夹加载字体文件

android - ♥ 和 ♦ 在 HTML 中在 Android 上显示为黑色,即使颜色在 CSS 中设置为红色

ios - 如何在 iOS 应用程序中为不同的语言使用不同的字体?