This discussion is archived
13 Replies Latest reply: Mar 30, 2011 10:43 PM by DarrylBurke RSS

Java Font Size?

830031 Explorer
Currently Being Moderated
Hi, I have a question about font size.

First of all when you create a new Font is the size pt or px (I think it is pt)

Next, a web browser has a default font size of 16px how do you create a Java Font that is this size?
And, how do you scale it?

font.deriveFont(32.0) - is this 32pt or 32px (Is this 200% of 16px) How do I get 200% of 16px?

Any thoughts would be appreciated.

Richard

I have come up with a way to get 16px in pt for a particular screen with the following. I was wondering if there is an easier way?
import javax.swing.*;
import java.awt.*;
import java.awt.font.*;

public class Main
{
    public static void main(String[] args)
    {
        Frame frame = new Frame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class Frame extends JFrame
{
    public Frame()
    {
        setTitle("Get font px to pt");
        setSize(300,300);
        JPanel panel = new Panel();
        add(panel, BorderLayout.CENTER);
    }
}

class Panel extends JPanel
{
    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        super.paint(g);
        FontRenderContext context = g2.getFontRenderContext();
        Font font = new Font("Times New Roman", Font.PLAIN,1);
        String test = "A";
        boolean found = false;
        float i = 1.0f;
        while(i < 20.0f && !found)
        {
            Font f = font.deriveFont(i);
            LineMetrics lm = f.getLineMetrics(test, context);
            float h = lm.getHeight();
            if(h >= 16)
            {
                found = true;
                g2.drawString("Standard 16px is "+i+"pt", 10, (int)(h+10));
            }
            i += 0.1f;
        }
    }
}
Edited by: 827028 on Mar 25, 2011 8:27 PM
  • 1. Re: Java Font Size?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    First of all when you create a new Font is the size pt or px (I think it is pt)
    It's documented. See the API for Font.size
    The point size of this Font, rounded to integer

    db
  • 2. Re: Java Font Size?
    830031 Explorer
    Currently Being Moderated
    O.k., I guess I know that Font is in Point, My real problem is that I can't scale a font to 16px. I don't think this can be done. Lets say the my ratio of px to pt is 13.7 point = 16 pixel. If I use font.deriveFont(13.7f) I get a font of 14pt - that is not 16px. In fact I am failing to see any way to scale a font to a px size. If any one knows of a way please let me know.

    Edited by: 827028 on Mar 27, 2011 9:33 PM
  • 3. Re: Java Font Size?
    EJP Guru
    Currently Being Moderated
    Well if it rounds the font size up you are hosed. That might be a property of the physical font, as Java shouldn't really care.
  • 4. Re: Java Font Size?
    821613 Newbie
    Currently Being Moderated
    Why do you want pixels? If you use pixel font, then the text will get smaller with higher screen resolutions and bigger with lower screen resolutions. Point font is barely dependent on screen resolution, if it's dependent at all.
  • 5. Re: Java Font Size?
    EJP Guru
    Currently Being Moderated
    Why do you want pixels? If you use pixel font, then the text will get smaller with higher screen resolutions and bigger with lower screen resolutions.
    Exactly.
    Point font is barely dependent on screen resolution, if it's dependent at all.
    It isn't dependent on resolution at all. A point is 1/72 of an inch, anywhere, any time.
  • 6. Re: Java Font Size?
    830031 Explorer
    Currently Being Moderated
    ElectrifiedBrain wrote:
    Why do you want pixels? If you use pixel font, then the text will get smaller with higher screen resolutions and bigger with lower screen resolutions. Point font is barely dependent on screen resolution, if it's dependent at all.
    I am working on a project that generates CSS. It is my understanding that the default Browser Font Size is 16px. I want the text in the Java application to look the same as the Browser View. Perhaps I have the wrong understanding of Browser Fonts; but, I don't think so due to the experimenting I have been doing. I know that that in CSS you can have % and em as well as px and pt. (Actually I don't see much with pt)

    Edited by: 827028 on Mar 29, 2011 6:53 PM
  • 7. Re: Java Font Size?
    EJP Guru
    Currently Being Moderated
    It doesn't matter what mistakes other people make. Font sizes should be measured in points.
  • 8. Re: Java Font Size?
    810604 Newbie
    Currently Being Moderated
    One way to generate a font that produces a specific pixel height for a certain string is to create a TextLayout object from the String using the base font, use getBounds().getHeight() on that to find out the height, then derive a font from that base font which is scaled by an affine transform to the pixel height you need using baseFont.deriveFont(at) where at is your affine transform (which you set to a scaling by the required ratio). Generating the TextLayout will require a FontRenderContext, which contains the information needed to translate the font's point size to a pixel height for the display device.
  • 9. Re: Java Font Size?
    830031 Explorer
    Currently Being Moderated
    JohnPM wrote:
    then derive a font from that base font which is scaled by an affine transform to the pixel height you need using baseFont.deriveFont(at)
    I tried that already. Believe it or not the affine transform will do things like shearing but when you scale it simply jumps between integral point sizes. I thought that was kind of strange; but, that is the way it handles affine transforms on text in the deriveFont method and the Graphics2D object. Strange but true.
  • 10. Re: Java Font Size?
    EJP Guru
    Currently Being Moderated
    Which all goes to suggest again that it is a limitation of the physical font.
  • 11. Re: Java Font Size?
    830031 Explorer
    Currently Being Moderated
    827028 wrote:
    JohnPM wrote:
    then derive a font from that base font which is scaled by an affine transform to the pixel height you need using baseFont.deriveFont(at)
    I tried that already. Believe it or not the affine transform will do things like shearing but when you scale it simply jumps between integral point sizes. I thought that was kind of strange; but, that is the way it handles affine transforms on text in the deriveFont method and the Graphics2D object. Strange but true.
    O.k. I guess you were right with using an AffineTransform except that I need one more thing in the Grahics2D:
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                                 RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  • 12. Re: Java Font Size?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    In the course of discussion, it has emerged that this is a Java 2D and not a Swing matter, so I'm moving this thread to a more appropriate forum.

    db
  • 13. Re: Java Font Size?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator action: Moved from Swing

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points