Search code examples
delphifontsdelphi-2007font-size

How to get font size in delphi


I am looking for a function which should take parameters as Font name, sample character, width, height of the sample character and should return Font Size.

It must look like this:

    GetFontSize(<Font Name>, <Sample Character>, <Sample Character Width>,
                <Sample Character Height>)

which must return the font size,

Is this possible in delphi?


Solution

  • I'm only aware of methods to do the opposite - that is, to get pixel sizes from point sizes.

    The only way I can think of is to set up a TCanvas, and repeatedly call GetTextExtent on it, changing the font size each time, until you get a size that's acceptably close to what you want.

    Depending on how accurate you need this, you could just convert your desired height in pixels to points - a point is 1/72 of an inch, and your screen is probably 96 pixels per inch (but check GetDeviceCaps to get the real figure). See this question.

    That would get you a font size pretty close to the pixel size you want.