Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
RamonBoza Avatar asked Apr 30 '13 16:04

RamonBoza


People also ask

Does Linux support TTF fonts?

Or is there another standard on linux? Save this answer. Yes, True Type fonts are a standard and are very well-supported in Ubuntu. Ubuntu also supports less common vector font standards such as OpenType (OTF), etc.

Is JLabel a container?

The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text.


3 Answers

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

like image 63
SubOptimal Avatar answered Oct 03 '22 14:10

SubOptimal


It seems to me that Windows OS isn't using ClearType. ClearType is an option, when enabled it smoothes the fonts. Sometimes it is disabled for tuning or performance reasons. See http://support.microsoft.com/kb/306527. Try to enable it.

To use ClearType for screen fonts:

  1. Click Start, click Control Panel, click Appearance and Themes, and then click Display.
  2. On the Appearance tab, click Effects.
  3. Click to select the Use the following method to smooth edges of screen fonts check box, and then click ClearType in the list.
like image 39
cinhtau Avatar answered Oct 03 '22 16:10

cinhtau


I have faced a similar issue before till I found this library in SourceForge which is called Smooth Metal you can download it from Here

Library Home Smooth Metal

The Smooth Metal look and feel is an addition to some of the look and feels including with Java, enhancing them with anti-aliased text.

You would notice that ClearType option in Windows will not affect the Result...

After adding the jar file of the library in the class path I wrote this small java app that renders the JLabel and undo rendering with two JButtons the results were crystal clear:

Here is the code which you can test:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.plaf.basic.BasicLabelUI;
import smoothmetal.SmoothLabelUI; // UI Class to set for the JLabel


public class LabelRender extends JFrame{

    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    JButton button = new JButton("RENDER");
    JButton button2 = new JButton("UNDO");
    JLabel label = new JLabel("HELLO WORLD");

    public LabelRender(){
        setSize(600, 250);
        setLocationRelativeTo(null);
        setTitle("JLabel Renderer");


        setLayout(new BorderLayout());

        label.setFont(new Font("Aller", Font.PLAIN, 70));
        label.setForeground(Color.WHITE);

        panel.add(label);
        panel2.add(button);
        panel2.add(button2);

        panel.setBackground(Color.BLACK);

        this.add(panel2, BorderLayout.NORTH );
        this.add(panel,BorderLayout.CENTER);

        setVisible(true);
        validate();


       // System.out.println(label.getUI());

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                label.setUI(new SmoothLabelUI());
            }
        });

        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                label.setUI(new BasicLabelUI());
            }
        });

    }

    public static void main(String args[]){
        new LabelRender();
    }
}
like image 26
Mohammed Alaghbari Avatar answered Oct 03 '22 14:10

Mohammed Alaghbari