Java中的某些Unicode字符不会显示在JTextField中

时间:2018-10-17 09:39:17

标签: java unicode fonts java-10

我们最近将其中一位客户的Oracle数据库从Windows-1252转换为Unicode(我不知道他们如何仍然可以继续使用;他们保存了许多个人名称)。

一切正常,除了某些字符。是字符(下面带有点的s)使问题进入我们的世界。 据我所知,未指定时,Java使用其逻辑字体来查找可在每个OS上使用的字体(请参见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。

1 个答案:

答案 0 :(得分:2)

逻辑字体的渲染似乎是一个谜。在一次测试中,当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

U+1e63U+1e62)恰好在该范围内。

我不知道这些明显的“字母”字符在此处被排除在哪里应该结束。 This document仅声明:

  

如果需要在搜索序列中早期放置具有较大字符库的字体(例如,出于性能原因),但是该字体支持的某些字符应使用另一种字体绘制,则使用此方法。

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

此外,该范围看起来很安静,即表示在排除范围内,而不在并且可以呈现正确。

当您将此行变成注释时,即

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

问题消失了。

我感到原始的排除子句导致渲染最终以嵌入的Lucida字体结束,而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 JDK11。它们的字体配置文件仅在许可证标题注释中有所不同……