Il giorno mer, 19/05/2010 alle 14.34 -0700, Phil Race ha scritto:

Hi Phil,

> BTW Its not just ascent and descent, there's problems on the left and 
> right sometimes
> as logical left and advance don't necessarily encompass all the pixels.

I noticed this as well, like this screenshot proves:

http://www.limasoftware.net/neugens/downloads/stuff/font2dbug/horiz.png


> What I'd like to have is a small test case that demonstrates exactly 
> this so I can follow the chain.
> Mario sounds to  have  that, right down to specific font and size and 
> using the same mnemonic
> positioning logic as swing.

I prepared my test case on the assumption that the bounding box was
wrong, but swing doesn't use the bounding box for rendering, so this
test case is not 100% correct.

Nevertheless, this made me look at what I believe it's the right thing,
because there I noticed that the leading was wrong.

I've uploaded a small test case, where only one font is tested, my
original test case let you select the font from a combo box list with
all the font installed, but because it had more swing logic, I felt like
it was too much distracting.

It's trivial to change this test to get a font name and size from the
command line, please tell me if you want me to do this.

Depending on the fonts on your system, you may see the same result or
not, so here is the image of what I get for reference:

http://www.limasoftware.net/neugens/downloads/stuff/font2dbug/bounds.png


On the right is OpenJDK.

Thanks,
Mario
-- 
pgp key: http://subkeys.pgp.net/ PGP Key ID: 80F240CF
Fingerprint: BA39 9666 94EC 8B73 27FA  FC7C 4086 63E3 80F2 40CF

Proud GNU Classpath developer: http://www.classpath.org/
Read About us at: http://planet.classpath.org
OpenJDK: http://openjdk.java.net/projects/caciocavallo/

Please, support open standards:
http://endsoftpatents.org/
package sun.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author Mario Torre <neug...@aicas.com>
 */
public class SmallFontTest extends JPanel {

    private String fontChoice;
    private int size;
    private int style;

    private boolean showFontInfo = true;

    public SmallFontTest(String font, int style, int size) {

        this.fontChoice = font;
        this.size = size;
        this.style = style;
    }

    @Override
    public void paint(Graphics g) {

        super.paint(g);

        String character = "g";

        Graphics2D g2d = (Graphics2D) g;

        Font font = new Font(fontChoice, style, size);
        setFont(font);

        FontMetrics metrics = g2d.getFontMetrics(font);

        Rectangle2D rect = metrics.getStringBounds(character, g);

        /* draw roughly in the middle */
        int width = this.getWidth() / 2;
        int height = this.getHeight() / 2;

        if (showFontInfo) {

            System.out.println("font: " + font);
            System.out.println("font descent: " + metrics.getDescent());
            System.out.println("font leading: " + metrics.getLeading());
            System.out.println("font ascent: " + metrics.getAscent());
            System.out.println("font height: " + metrics.getHeight());
        }
        showFontInfo = false;

        g2d.setColor(Color.RED);
        g2d.drawRect(width, height - metrics.getAscent(),
                rect.getBounds().width,
                rect.getBounds().height);

        g2d.setColor(Color.BLACK);
        g2d.drawString(character, width, height);
    }

    public static void main(String [] args) {

        SmallFontTest fontTest =
            new SmallFontTest("SansSerif", Font.PLAIN, 12);

        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(150, 150);

        mainFrame.add(fontTest, BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }
}

Reply via email to