Font size vs. Pixel Size

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Font size vs. Pixel Size

Postby darkspork » Sat Feb 28, 2009 12:58 am UTC

This problem has plagued me for some time: Is there ANY relationship between font size in points and font size in pixels? I have had countless program graphics failures because of this.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Font size vs. Pixel Size

Postby Berengal » Sat Feb 28, 2009 1:01 am UTC

Wikipedia.

I have little experience with typesetting, but from that page I gather it depends on the monitor size?
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Font size vs. Pixel Size

Postby darkspork » Sat Feb 28, 2009 1:09 am UTC

Berengal wrote:Wikipedia.

I have little experience with typesetting, but from that page I gather it depends on the monitor size?

That's the problem. Font points are loosely related to inches, but all that blows away the second pixels show up. I've found that using the font size as the number of vertical pixels usually avoids overlap, but width seems completely unrelated.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Font size vs. Pixel Size

Postby Berengal » Sat Feb 28, 2009 1:41 am UTC

You should probably avoid using points as your font size measurement then.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Font size vs. Pixel Size

Postby 0xBADFEED » Sat Feb 28, 2009 3:16 am UTC

There's always a direct relationship between points and pixels, it just depends on a number of factors, such as screen resolution and monitor resolution. Worse, depending on your environment you can't always depend on the pixels-per-inch reported to be accurate. If I remember correctly this problem can come up when using the Java AWT it always defaults to 72 dpi and tries to deliver the correct value but is not guaranteed to do so.

User avatar
sparkyb
Posts: 1091
Joined: Thu Sep 06, 2007 7:30 pm UTC
Location: Camberville proper!
Contact:

Re: Font size vs. Pixel Size

Postby sparkyb » Sat Feb 28, 2009 5:34 am UTC

Mostly what people have already said. I had to deal with this issue recently because I wrote my first program ever that actually printed something. This meant that I needed to make something on the screen exactly match something else on paper (in real world units). So if you didn't already know, a point is 1/72nd of an inch. To convert from inches to pixels you need to know the DPI (or at least the DPI that the system thinks it is). In an ideal world this would be the resolution you were displaying divided by the physical size of the monitor so that all 12 point text would be the same size regardless of your resolution or monitor*. But currently computers don't have a way to query the actual physical size of the monitor so it's just a setting somewhere in the OS that you have to set that almost nobody ever adjusts. On Windows I believe the default is usual 96 dpi (and in case you were wondering it is under Display Properties -> Settings -> Advanced). However any GUI system should probably allow you to fetch the DPI it is using. For instance in the Windows API you can get the DPI for a particular device context (remember, the screen and printer give very different answers) by calling GetDeviceCaps(hDC, LOGPIXELSY). There also may be a way to specify a font size in pixels if that's really what you want to do. I can't remember where but somewhere I was able to do this by specifying the size as negative (it takes the absolute value, of course). In the project I mentioned, I actually encountered a bug with wxWidgets where when you create a font if you specify the size in points it internally convert them to pixels before constructing the native font object, but since the wxWidgets font object doesn't know what device context I'm going to use it for it always converts from points to pixels assuming that I'm doing it for the screen. Then if I use the font for the printer the text comes out way to small. So I had to fetch the correct DPI for the printer's device context, convert from points to pixels myself, and then create the font specifying the size in pixels.

* Well, actually how far away you sit should also be a factor because what you want is all 12 point text to appear the same size (as measured by the angle subtended by your eye) as opposed to being the same size.

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: Font size vs. Pixel Size

Postby mrkite » Sat Feb 28, 2009 5:53 am UTC

sparkyb wrote:But currently computers don't have a way to query the actual physical size of the monitor so it's just a setting somewhere in the OS that you have to set that almost nobody ever adjusts.


But they can query the monitor make and model, and then use that info to look up the DPI for a given resolution. Gnome does this when it can.

User avatar
Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

Re: Font size vs. Pixel Size

Postby Area Man » Sat Feb 28, 2009 7:46 am UTC

This is an old problem. Non-fixed width, and with kerning and so on.
See: this. Click on the "radians" link for more interesting info.
Bisquick boxes are a dead medium.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Font size vs. Pixel Size

Postby hotaru » Sat Feb 28, 2009 8:10 am UTC

there are also things like this:
Slava Pestov wrote:With some fonts, the font glyphs exceed the font metric bounds.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
zombie_monkey
Posts: 644
Joined: Mon Oct 16, 2006 1:26 pm UTC
Location: Bulgaria

Re: Font size vs. Pixel Size

Postby zombie_monkey » Sat Feb 28, 2009 12:27 pm UTC

Actually AFAIK "pixel" is (at least in some context) theoretically defined in terms of radians at the recommended viewing distance. I think I read that when I was was reading up different ways to define sizes in CSS.

User avatar
Xanthir
My HERO!!!
Posts: 5413
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Font size vs. Pixel Size

Postby Xanthir » Sat Feb 28, 2009 6:13 pm UTC

That is specifically the definition of the "CSS pixel", which isn't directly correlated with a device pixel. CSS pixels are, however, rounded to the nearest square block of device pixels. Right now that's a 1:1 correspondence, but we're very near the day that the average monitor will have 4 device pixels to a single CSS pixel.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Font size vs. Pixel Size

Postby Karrion » Sun Mar 01, 2009 5:45 am UTC

sparkyb wrote:But currently computers don't have a way to query the actual physical size of the monitor

Actually they do; the EDID of the monitor contains the physical size of the screen. On linux for example you can see it by running xdpyinfo:
xdpyinfo | grep -B1 dot wrote: dimensions: 1680x1050 pixels (474x303 millimeters)
resolution: 90x88 dots per inch

User avatar
sparkyb
Posts: 1091
Joined: Thu Sep 06, 2007 7:30 pm UTC
Location: Camberville proper!
Contact:

Re: Font size vs. Pixel Size

Postby sparkyb » Sun Mar 01, 2009 5:55 am UTC

Karrion wrote:
sparkyb wrote:But currently computers don't have a way to query the actual physical size of the monitor

Actually they do; the EDID of the monitor contains the physical size of the screen.

Ok. I actually did know that, but practically, I haven't seen that information used in many places and I imagine there are a lot of display devices on which it is unreliable. Either way, whether you're dealing with the real DPI detected from the device or just some arbitrary setting, it amounts to the same thing regarding the OP's question. You still need to find the function to query the value being used.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Font size vs. Pixel Size

Postby darkspork » Mon Mar 02, 2009 4:49 am UTC

*IDEA: Create an image that will not be drawn on the screen. Since I'm using a monospaced font, I can draw an X on that image and measure its size with for loops.

Any better suggestions?
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: Font size vs. Pixel Size

Postby mrkite » Mon Mar 02, 2009 8:13 am UTC

darkspork wrote:*IDEA: Create an image that will not be drawn on the screen. Since I'm using a monospaced font, I can draw an X on that image and measure its size with for loops.

Any better suggestions?


Depending on the language and GUI Toolkit, there are functions that measure the width/height of a specific string. For example, Swing/AWT has Font.getStringBounds()

Comic JK
Posts: 270
Joined: Wed Feb 18, 2009 6:08 pm UTC

Re: Font size vs. Pixel Size

Postby Comic JK » Thu Mar 05, 2009 5:29 am UTC

mrkite wrote:
darkspork wrote:*IDEA: Create an image that will not be drawn on the screen. Since I'm using a monospaced font, I can draw an X on that image and measure its size with for loops.

Any better suggestions?

Depending on the language and GUI Toolkit, there are functions that measure the width/height of a specific string. For example, Swing/AWT has Font.getStringBounds()

This makes perfect sense--you don't need to query the physical bounds of the monitor, as some have suggested, as long as you have A. the size of your string in pixels, accessible from your graphics context, and B. the size of your screen/window in pixels, which is easy to get.

Then you can fit everything together in the computer's native unit, the pixel, instead of this typographical holdover, the point.
Image
A webcomic funnier than life itself. Updated Monday-Friday.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Font size vs. Pixel Size

Postby darkspork » Thu Mar 05, 2009 6:21 pm UTC

For those still interested, Java code:

Code: Select all

    /**
     * Calculate and set the values of xSize, ySize, and tSize
     * @param testFont - Font we want to find the pixel size of
     * @return an int array. a[0] = width, a[1] = height, a[2] = height to top
     */
    private int[] calcSizes(Font testFont)
    {
        final int TEST_SIZE = 100
        BufferedImage testImage = new BufferedImage(TEST_SIZE, TEST_SIZE, BufferedImage.TYPE_INT_RGB);
        Graphics g = testImage.getGraphics();
        g.setColor(Color.black);
        g.fillRect(0, 0, TEST_SIZE, TEST_SIZE);
        g.setColor(Color.white);
        g.setFont(testFont);
       
        // This will print all of the characters on top of eachother
        for(char i = 31; i < 123; i++)
        {
            g.drawString("" + i, TEST_SIZE / 2, TEST_SIZE / 2);
        }
       
        // 31 -> 122 will print all the letters
       
        int xL = TEST_SIZE, xR = 0, yT = TEST_SIZE, yB = 0;
       
        for(int y = 0; y < TEST_SIZE; y++)
        {
            for(int x = 0; x < TEST_SIZE; x++)
            {
                if (testImage.getRGB(x, y) != Color.black.getRGB())
                {
                    if (x > xR)
                    {
                        xR = x;
                    }
                    if (x < xL)
                    {
                        xL = x;
                    }
                    if (y > yB)
                    {
                        yB = y;
                    }
                    if (y < yT)
                    {
                        yT = y;
                    }
                }
            }
        }
       
        int[] a = {xR - xL, yB - yT, TEST_SIZE / 2 - yT};
    }


This method will compute three sizes, based on the maximum it finds in that font:
Width is the width in pixels of the widest character (probably W)
Height is the distance from the top of the tallest character (The $ character maybe?) to the bottom of the lowest character (this REALLY depends on the font. The lowercase letter y dips down a bit.)
In java, the (0, 0) point on any given string is to the left and near the bottom. Capital letters are usually above this point, while pqyj|;, dip below it. The third "tSize" is the distance from the top to this point.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Font size vs. Pixel Size

Postby Random832 » Thu Mar 05, 2009 7:48 pm UTC

darkspork wrote:This problem has plagued me for some time: Is there ANY relationship between font size in points and font size in pixels? I have had countless program graphics failures because of this.


On Windows, size in points = 0.75*(size in pixels) unless you've messed with things.

On Mac, size in points = size in pixels

on Linux, a decade ago, I'd have said size in points is either 0.96 or 0.72 * size in pixels. Now it's much more common to provide a way to set the screen DPI accurately and actually properly scale things.

Of course, the WIDTH is something else entirely. For Courier New, the width per character is 0.6 * the size, but this varies by font and in most fonts also by what characters you're actually typing.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Font size vs. Pixel Size

Postby darkspork » Thu Mar 05, 2009 9:29 pm UTC

Random832 wrote:On Windows, size in points = 0.75*(size in pixels) unless you've messed with things.

I've messed with things then. My script seems to work, although each character needs to be placed individually rather than just drawing the string. This is especially true on non-monospaced fonts.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

Random832
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Font size vs. Pixel Size

Postby Random832 » Thu Mar 05, 2009 9:42 pm UTC

darkspork wrote:
Random832 wrote:On Windows, size in points = 0.75*(size in pixels) unless you've messed with things.

I've messed with things then. My script seems to work, although each character needs to be placed individually rather than just drawing the string. This is especially true on non-monospaced fonts.


Things the "size in pixels" of a font is not:
  • The height of a capital letter
  • The distance between two adjacent lines
  • The result of getStringBounds/MeasureString/GetTextExtentExPoint32W/whatever
  • It's not a bad approximation, sometimes, to the distance from ascender to descender (i.e. say you have the string "dp"), but I don't think it's based on that either.

Things none of these are equal to:
  • The width of any character (except maybe U+2003)
  • The width of any string
  • Anything that can be multiplied with a character count to get the width of any string


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 4 guests