Python, Direct3D and Font Sizes

Never ever try to display a font on screen so that it has a certain real size.

  • Direct3D displays a font always at 72dpi - regardless of the current DPI setting (at least on Windows Seven)
  • Every font interprets point sizes differently
  • Point size does not equal Pixel Size

So my solution now is: Display a 200pixel bar and ask the user to measure the length on his monitor. We now know the size of one pixel. For visual acuity testing the distance from the screen is also important.

The following Python Code now scales the letters for the font “Courier New Bold” to display correctly on the screen:

        # Formula found online at sciencedirect:
        # Using a high-resolution display for screening visual acuity for near vision, A. Hoffman, 1996
        gap = math.tan(math.pi/180.0 * (1.0 / 60.0)) * self.db['distance']*10
        pixel_density = self.db['font_line'] / 200.0
        num_pixels = gap / pixel_density
        height = round(num_pixels * 5.0 * 1.27 * 96.0/72.0)

To explain that a bit: The calculation is done for visual acuity of 1.0, that is normal sight. At first the gap of a letter is calculated, the “E” for instance consists of two gaps and three bars.

Then, the pixel density as calculated from the 200px line and the real length.

num_pixels is then the number of pixels necessary to represent one gap. Finally, the height of the letter (or to be exact: the size to pass to d3d.Font) is calculated: 5.0, because 2 gaps and 3 bars for “E”, 1.27 is the scale factor for Courier New Bold, 96.0/72.0 is the scaling to 72dpi. This is necessary, because I measured the scale factor for the font at 96dpi, that is the resolution of my external screen and Windows' default setting. At this setting, a font with 96pt should equal 1 inch on screen. I actually did the measurements digitally using The GIMP, I should've done that a 72dpi to simplify the formula.

Whatever, it seems to work fine, at least on my machine.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies