Author Topic: Confusion over font size and DPI on Apple Mac  (Read 1282 times)


  • Sr. Member
  • ****
  • Posts: 258
Confusion over font size and DPI on Apple Mac
« on: July 10, 2017, 02:35:25 pm »
Hello, I'm currently writing a font selector control, and for the purposes of setting a default size value, I am using the following code:

Code: Pascal  [Select]
  1. procedure TcaFontSelecter.InitSelectedFontProperties;
  2. var
  3.   FontData: TFontData;
  4.   PPI: Integer;
  5. begin
  6.   FontData := GetFontData(FLabel.Font.Handle);
  7.   PPI := Forms.Screen.PixelsPerInch;
  8.   FontData.Height := -FontData.Height;  // GetFontData returns Height as a +ve value
  9.   FSelectedFont.Name := FontData.Name;
  10.   FSelectedFont.Size := -MulDiv(FontData.Height, 72, PPI) // This should give Size as a +ve value
  11. end;

The weird thing is that FLabel, the source of the GetFontData call, is made visible in this component, and visually, at least, appears to have the same size font as a standard TLabel, dropped straight on the form. I would expect, therefore, for the Height as returned by GetFontData to be -13 (as noted, GetFontData is returning a +ve value), and the resultant value of FSelectedFont.Size to be 10.

For some reason, which I suspect is to do with of DPI related factors, the Height as returned by GetFontData is actually 16, giving a FSelectedFont.Size to be 12. I'm sure I read somewhere recently, that recent changes to Lazarus on the Apple Mac, mean that adjustments have to be made in order for font sizes to work properly but I didn't make a note of it.

Can anyone shed some light on this question?


« Last Edit: July 10, 2017, 02:53:32 pm by carl_caulkett »
macOS 10.14.1
Lazarus 2.0.0RC2 Carbon / Lazarus 2.1.0 r59743 Cocoa
FPC 3.0.4