Search code examples
javaswingfontsjlabeltruetype

Differences in JLabel when loading ttf font between Windows and Linux


I'm uploading the aller font in java with the following code:

private Font loadFont(final String path) {
    Font font = null;

    InputStream fontFile = null;
    fontFile = FontLoaderClass.class.getResourceAsStream(path);

    if (fontFile != null) {
        try {
            font = Font.createFont(Font.PLAIN, fontFile);
        } catch (FontFormatException e) {
            LOGGER.error("Error with font format {}", e);
        } catch (IOException e) {
            LOGGER.error("Error accessing font {}", e);
        }
    }
    return font;
}

The font is loaded correctly:

http://www.fontsquirrel.com/fonts/Aller

the font is set to all ".font" changing the default settings for java application, but in Linux is shown correctly but Windows isn't.

private Font buildFont(final String key, final int size) {
    Font f = loadFont(ALLER_LT_FONT_PATH);
    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f);
    if (f == null) {
        f = (Font) UIManager.get(key);
    }
    f = f.deriveFont(Font.TRUETYPE_FONT, size);
    return f;
}

Linux shows: linux image pick

Windows shows: enter image description here

As you can see in the images, there is some cut off in Windows that causes the image not to be shown correctly.

Has anyone experienced with this issue before?


Solution

  • find two small demos attached, wich enables antialiasing for Swing components respectivly for draw operations.

    for Swing components

    // to enable antialiasing (AA) for Swing components
    //
    // either:
    //    start the JVM with the option -Dawt.useSystemAAFontSettings=on
    //    see also: http://docs.oracle.com/javase/6/docs/technotes/guides/2d/flags.html#aaFonts
    // or:
    //    System.setProperty("awt.useSystemAAFontSettings", "on");
    //    - you must call it before the first Swing component is rendered
    //    - if AA it's on by default you must set it "off", otherwise you can't
    //      toggle it inside the application
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    import static java.awt.RenderingHints.KEY_ANTIALIASING;
    import static java.awt.RenderingHints.VALUE_ANTIALIAS_OFF;
    import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;
    
    public class SwingAntiAliasingDemo {
    
        public static void main(String[] args) {
            System.setProperty("awt.useSystemAAFontSettings", "off");
            initGui();
        }
    
        public static void initGui() {
            JFrame frame = new JFrame();
    
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
    
            Font font = new Font("Serif", Font.TRUETYPE_FONT, 96);
            JPanel jpanel = new JPanel(new BorderLayout());
    
            JLabel labelAA = new JLabel("Antialiasing ON") {
                @Override
                public void paintComponent(Graphics g) {
                    Graphics2D graphics2d = (Graphics2D) g;
                    graphics2d.setRenderingHint(KEY_ANTIALIASING,
                            VALUE_ANTIALIAS_ON);
                    super.paintComponent(g);
                }
            };
            labelAA.setFont(font);
            labelAA.setForeground(Color.WHITE);
    
            JLabel labelNoAA = new JLabel("Antialiasing OFF") {
                @Override
                public void paintComponent(Graphics g) {
                    Graphics2D graphics2d = (Graphics2D) g;
                    graphics2d.setRenderingHint(KEY_ANTIALIASING,
                            VALUE_ANTIALIAS_OFF);
                    super.paintComponent(g);
                }
            };
            labelNoAA.setFont(font);
            labelNoAA.setForeground(Color.WHITE);
    
            jpanel.setBackground(new Color(0, 22, 95));
            jpanel.add(labelAA, BorderLayout.NORTH);
            jpanel.add(labelNoAA, BorderLayout.SOUTH);
    
            frame.setTitle("stackoverflow question 16304254");
            frame.getContentPane().add(jpanel);
            frame.setLocation(200, 200);
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
        }
    }
    

    for draw operations

    // to enable antialiasing (AA) for draw operations
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    
    import static java.awt.RenderingHints.KEY_ANTIALIASING;
    import static java.awt.RenderingHints.VALUE_ANTIALIAS_OFF;
    import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;
    
    public class DrawAntiAliasingDemo extends JFrame {
    
        private Font font;
        private Color backGroundColor;
    
        public static void main(String[] args) {
            new DrawAntiAliasingDemo();
        }
    
        public DrawAntiAliasingDemo() {
            font = new Font("Serif", Font.TRUETYPE_FONT, 96);
            backGroundColor = new Color(0, 22, 95);
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
    
            setTitle("stackoverflow question 16304254");
            setSize(850, 260);
            setResizable(false);
            setVisible(true);
        }
    
        @Override
        public void paint(Graphics g) {
            Graphics2D d = (Graphics2D) g;
            d.setColor(backGroundColor);
            d.fillRect(0, 0, getWidth(), getHeight());
            d.setFont(font);
            d.setPaint(Color.white);
    
            d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
            d.drawString("Antialiasing ON", 10, 115);
    
            d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_OFF);
            d.drawString("Antialiasing OFF", 10, 230);
        }
    }
    

    cheers Frank