Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 源SAN Pro的TTF和OTF版本在Swing中的显示方式不同(Nimbus L&;F)_Java_Swing_Fonts_Java 7_Nimbus - Fatal编程技术网

Java 源SAN Pro的TTF和OTF版本在Swing中的显示方式不同(Nimbus L&;F)

Java 源SAN Pro的TTF和OTF版本在Swing中的显示方式不同(Nimbus L&;F),java,swing,fonts,java-7,nimbus,Java,Swing,Fonts,Java 7,Nimbus,专题信托基金: 检察官办公室: 两者的字体相同,但字体文件类型不同 我正在Windows7中运行Java7 根据Adobe的米格尔·索萨(Miguel Sousa)的说法,缺陷不在字体中 我只是将默认字体设置为新字体。TTF版本的工作没有任何问题 Font font_o = Font.createFont(Font.TRUETYPE_FONT, fonts.class.getResourceAsStream("fonts/TTF/SourceSansPro-Regular.ttf")); //

专题信托基金:

检察官办公室:

两者的字体相同,但字体文件类型不同

我正在Windows7中运行Java7

根据Adobe的米格尔·索萨(Miguel Sousa)的说法,缺陷不在字体中

我只是将默认字体设置为新字体。TTF版本的工作没有任何问题

Font font_o = Font.createFont(Font.TRUETYPE_FONT, fonts.class.getResourceAsStream("fonts/TTF/SourceSansPro-Regular.ttf"));
//Font font_o = Font.createFont(Font.TRUETYPE_FONT, fonts.class.getResourceAsStream("fonts/OTF/SourceSansPro-Regular.otf"));
Font font_n=font_o.deriveFont(Font.PLAIN, UIManager.getLookAndFeelDefaults().getFont("defaultFont").getSize());
UIManager.getLookAndFeelDefaults().put("defaultFont",font_n);
为什么线条的高度有差异?

结论(forums.oracle.com/thread/2573652与他的文章一样多,没有什么特别之处)


  • 不是真的Java,也不是Nimbus bug,这个字体中的bug,你可以看到在Metal、Win、WinClassic和Nimbus Laf中,OTF_字体缺少符号上方的2个像素
。窗户

。金属

。灵气

。经典


Java6上的WinXp

。对


  • 此代码将帮助您找到两种字体之间的差异(并向作者报告所有差异)

  • 从具体字体的(java.awt.font.)texttribute返回所有可用的属性

  • 您可以从(几乎)所有TextAttributes获取详细属性

有输出

java.awt.font.TextAttribute(family)
java.awt.font.TextAttribute(weight)
java.awt.font.TextAttribute(width)
java.awt.font.TextAttribute(posture)
java.awt.font.TextAttribute(size)
java.awt.font.TextAttribute(transform)
java.awt.font.TextAttribute(superscript)
java.awt.font.TextAttribute(char_replacement)
java.awt.font.TextAttribute(foreground)
java.awt.font.TextAttribute(background)
java.awt.font.TextAttribute(underline)
java.awt.font.TextAttribute(strikethrough)
java.awt.font.TextAttribute(run_direction)
java.awt.font.TextAttribute(bidi_embedding)
java.awt.font.TextAttribute(justification)
java.awt.font.TextAttribute(input method highlight)
java.awt.font.TextAttribute(input method underline)
java.awt.font.TextAttribute(swap_colors)
java.awt.font.TextAttribute(numeric_shaping)
java.awt.font.TextAttribute(kerning)
java.awt.font.TextAttribute(ligatures)
java.awt.font.TextAttribute(tracking)
来自代码Nimbus L&F

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class SystemFontDisplayer {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
    private JComboBox fontsBox;
    private javax.swing.Timer timer = null;
    private JButton testButton = new JButton("testButton");
    private JTextField testTextField = new JTextField("testTextField", JLabel.CENTER);
    private JLabel testLabel = new JLabel("testLabel");
    private Font font1, font2;
    private JMenuBar menuBar1 = new JMenuBar();
    private JMenu menu1= new JMenu("Menu 1");
    private JMenu menu2= new JMenu("Menu 2");
    private JMenuItem menuItem1= new JMenuItem("MenuItem 1");
    private JMenuItem menuItem2= new JMenuItem("MenuItem 2");

    public SystemFontDisplayer() {
        try {
            font1 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.ttf"));
            font2 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.otf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //ge.registerFont(font1);
        ge.registerFont(font2);
        String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
        fontsBox = new JComboBox(fontFamilyNames);
        fontsBox.setSelectedItem(0);
        fontsBox.setRenderer(new ComboRenderer());
        fontsBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final String fontName = fontsBox.getSelectedItem().toString();
                    fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
                    start();
                }
            }
        });
        fontsBox.setSelectedItem(0);
        fontsBox.getEditor().selectAll();

        menu1.add(menuItem1);
        menuBar1.add(menu1);
        menu2.add(menuItem2);
        menuBar1.add(menu2);
        frame.setJMenuBar(menuBar1);
        frame.setLayout(new GridLayout(4, 0, 5, 5));
        frame.add(fontsBox);
        frame.add(testButton);
        frame.add(testTextField);
        frame.add(testLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 105);
        frame.pack();
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                fontsBox.setPopupVisible(true);
                fontsBox.setPopupVisible(false);
            }
        });
        frame.setVisible(true);
    }

    private void start() {
        timer = new javax.swing.Timer(250, updateCol());
        timer.setRepeats(false);
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 16);
                try {
                    LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                    final FontUIResource res = new FontUIResource(fnt);
                    UIDefaults uiDefaults = lnf.getDefaults();
                    uiDefaults.put("defaultFont", res);
                    UIManager.getLookAndFeel().uninitialize();
                    UIManager.setLookAndFeel(lnf);
                } catch (InstantiationException ex) {
                    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                }
                UIDefaults defaults = UIManager.getDefaults();
                final FontUIResource res = new FontUIResource(fnt);
                Object[] obj = res.getAvailableAttributes();
                for (Object objs : obj) {
                    System.out.println(objs); //returns java.awt.font.TextAttribute
                }
                defaults.put("defaultFont", res);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            }
        };
    }

    public static void main(String arg[]) {
        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
            }
        });
    }

    private class ComboRenderer extends BasicComboBoxRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final Object fntObj = value;
            final String fontFamilyName = (String) fntObj;
            setFont(new Font(fontFamilyName, Font.PLAIN, 16));
            return this;
        }
    }
}
从旧代码L&Fs

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class SystemFontDisplayer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
    private JComboBox fontsBox;
    private javax.swing.Timer timer = null;
    private JButton testButton = new JButton("testButton");
    private JTextField testTextField = new JTextField("testTextField");
    private JLabel testLabel = new JLabel("testLabel");
    private Font font1, font2;
    private JMenuBar menuBar1 = new JMenuBar();
    private JMenu menu1 = new JMenu("Menu 1");
    private JMenu menu2 = new JMenu("Menu 2");
    private JMenuItem menuItem1 = new JMenuItem("MenuItem 1");
    private JMenuItem menuItem2 = new JMenuItem("MenuItem 2");

    public SystemFontDisplayer() {
        try {
            font1 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.ttf"));
            font2 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.otf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //ge.registerFont(font1);
        ge.registerFont(font2);
        String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
        fontsBox = new JComboBox(fontFamilyNames);
        fontsBox.setSelectedItem(0);
        fontsBox.setRenderer(new ComboRenderer());
        fontsBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final String fontName = fontsBox.getSelectedItem().toString();
                    fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
                    start();
                }
            }
        });
        fontsBox.setSelectedItem(0);
        fontsBox.getEditor().selectAll();
        menu1.add(menuItem1);
        menuBar1.add(menu1);
        menu2.add(menuItem2);
        menuBar1.add(menu2);
        frame.setJMenuBar(menuBar1);
        frame.setLayout(new GridLayout(4, 0, 20, 20));
        frame.add(fontsBox);
        frame.add(testButton);
        frame.add(testTextField);
        frame.add(testLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 105);
        frame.pack();
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                fontsBox.setPopupVisible(true);
                fontsBox.setPopupVisible(false);
            }
        });
        frame.setVisible(true);
    }

    private void start() {
        timer = new javax.swing.Timer(750, updateCol());
        timer.setRepeats(false);
        timer.start();
    }

    public Action updateCol() {
        return new AbstractAction("text load action") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12);
                final FontUIResource res = new FontUIResource(fnt);
                UIManager.getLookAndFeelDefaults().put("Button.font", res);
                UIManager.getLookAndFeelDefaults().put("TextField.font", res);
                UIManager.getLookAndFeelDefaults().put("Label.font", res);
                SwingUtilities.updateComponentTreeUI(frame);
                frame.pack();
            }
        };
    }

    public static void main(String arg[]) {
        /*try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
            }
        });
    }

    private class ComboRenderer extends BasicComboBoxRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final Object fntObj = value;
            final String fontFamilyName = (String) fntObj;
            setFont(new Font(fontFamilyName, Font.PLAIN, 16));
            return this;
        }
    }
}

  • 有关更多详细信息,请参阅

这如何帮助我比较Adobe Source Sans Pro的otf和ttf版本?此外,它不会将属性值打印到控制台,但只打印存在哪些属性,您是否查看了Github问题?
1)这如何帮助我比较Adobe Source Sans Pro的otf和ttf版本?
-->每种字体都有许多属性,其中一个是插入、空边框,显示Unicodes glyph中较高值的可用空间
2)它也不会将属性的值打印到控制台,而是只打印存在哪些属性
-->同样,您可以从所有属性中获取详细属性3)Darryl Burke的Visual Font Designer的原因,不要在我的回答中重新发明轮子
4)你看了Github的问题了吗?
-->开源的标准答案……不,你误解了什么。我正在使用Adobe提供的字体源Sans Pro。我使用的是这种字体的otf或ttf版本。只有otf字体有一些问题(行高?),但是Adobe的Miguel说它们都是一样的。你可以从列表中选择字体的一个版本(ttf或otf?),但你不能比较它们,也不能在otf和ttf版本之间选择直接比较。你已经知道我提交了一个不存在的问题-这很奇怪-它应该存在,因为我有一个bug id=(对于Oracle和Java来说,这是非常令人失望的,对于解决所有问题的这个问题/答案没有答案。所有这些都让我们一无所获,Adobe说这不是字体错误,Oracle什么也没有说,所以对我来说这是Java错误。不,你误解了一些东西。请确保没有,我在这里删除我的答案,以避免进一步讨论关于About,我到底误解了什么?我在这里尝试了ttf版本:看起来很棒,然后尝试了otf版本(评论ttf版本)lokks不一样,看到上面的两张图片,看到“O”上面的空白我看到了,但我试图避免对我这边的方法进行任何调试,没有别的:-)在FontForge中,两个版本(otf,ttf)实际上是一样的,看起来也一样,似乎是一个Java Swing(Nimbus L&F)错误如果它也被其他Java用户复制,那将是一个错误,所以让我们看看社区怎么说,也许我们必须为此提交一份错误报告。我将不断更新此stackoverflow问题,以便将来其他用户知道