cocoa: converting font pixel-height into point size, used by NSFont. #32175

git-svn-id: trunk@56761 -
This commit is contained in:
dmitry 2017-12-17 05:30:20 +00:00
parent ffe886c60f
commit 8474c9858e
2 changed files with 15 additions and 5 deletions

View File

@ -195,7 +195,7 @@ type
strict private
FFont: NSFont;
FName: AnsiString;
FSize: Integer;
FSize: CGFloat;
FStyle: TCocoaFontStyle;
FAntialiased: Boolean;
FIsSystemFont: Boolean;
@ -209,7 +209,7 @@ type
property Antialiased: Boolean read FAntialiased;
property Font: NSFont read FFont;
property Name: String read FName;
property Size: Integer read FSize;
property Size: CGFloat read FSize;
property Style: TCocoaFontStyle read FStyle;
end;
@ -556,6 +556,8 @@ var
Win32Weight, LoopCount: Integer;
CocoaWeight: NSInteger;
FTmpFont: NSFont;
const
DPI = 96; // todo: what about Retina displays and scaled displays?
begin
inherited Create(AGlobal);
@ -575,8 +577,14 @@ begin
if ALogFont.lfHeight = 0 then
FSize := Round(NSFont.systemFontSize)
else
FSize := Abs(ALogFont.lfHeight);
else begin
// lfHeight is height in pixels (TFont.ReferenceNeeded), FSize is in points
// (TFont has property "Size" - which is size in points, but it's not passed to LOGFONT)
// todo: check for the actual DPI
// also, negative height and positive height are different hights, thus
// abs() is not really accurate
FSize := Round(Abs(ALogFont.lfHeight * 72 / DPI)*10)/10;
end;
// create font attributes
Win32Weight := ALogFont.lfWeight;

View File

@ -1145,6 +1145,8 @@ var
AExtLogPen: PExtLogPen absolute Buf;
AFont: TCocoaFont absolute AObject;
ALogFont: PLogFont absolute Buf;
const
DPI = 96;
begin
Result := 0;
@ -1274,7 +1276,7 @@ begin
Result := SizeOf(TLogFont);
FillChar(ALogFont^, SizeOf(ALogFont^), 0);
ALogFont^.lfFaceName := AFont.Name;
ALogFont^.lfHeight := AFont.Size;
ALogFont^.lfHeight := Round( AFont.Size * DPI / 72);
Traits := NSFontManager.sharedFontManager.traitsOfFont(AFont.Font);
if (Traits and NSFontBoldTrait) <> 0 then
ALogFont^.lfWeight := FW_BOLD