From 0f92e50ce86f2aa96a79b46f6099acde713e1944 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Wed, 23 Jun 2021 19:32:05 +1000 Subject: [PATCH] fix(windows): osk scaling mismatch on horz axis Font scaling on individual keys would ignore the vertical axis if the horizontal axis reached the key width threshold, rather than calculating the minimum of the two axes. This lead to weird effects, such as adding characters to a key could make the font larger rather than smaller! Reported by @jmdurdin. --- .../global/delphi/comp/OnScreenKeyboard.pas | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/windows/src/global/delphi/comp/OnScreenKeyboard.pas b/windows/src/global/delphi/comp/OnScreenKeyboard.pas index f045d39e39..1a48b0d051 100644 --- a/windows/src/global/delphi/comp/OnScreenKeyboard.pas +++ b/windows/src/global/delphi/comp/OnScreenKeyboard.pas @@ -1,18 +1,18 @@ (* Name: OnScreenKeyboard Copyright: Copyright (C) SIL International. - Documentation: - Description: + Documentation: + Description: Create Date: 23 Aug 2006 Modified Date: 24 Jul 2015 Authors: mcdurdin - Related Files: - Dependencies: + Related Files: + Dependencies: - Bugs: - Todo: - Notes: + Bugs: + Todo: + Notes: History: 23 Aug 2006 - mcdurdin - Initial version 30 Aug 2006 - mcdurdin - Fix underlying layout published bug 14 Sep 2006 - mcdurdin - Add Transparent flag, retrieve aspected size, polish drawing @@ -264,6 +264,8 @@ const // I4098 // I4799 implementation uses + System.Math, + Glossary, ScanCodeMap, Types, @@ -379,7 +381,7 @@ begin ParentBackground := True; UpdateEuroLayout; // I764 - + if HasParent then // I1227, I904, I1102 - crash export OSK to BMP, PNG CalcKeyFontSizes; end; @@ -730,7 +732,7 @@ begin begin k2 := FHoverKey; if Assigned(k) and not k.Enabled then k := nil; - + FHoverKey := k; DoDrawKey(Canvas, k); //InvalidateKey(k); DoDrawKey(Canvas, k2); //InvalidateKey(k2); @@ -1383,16 +1385,27 @@ var if GetTextExtentPoint32W(Canvas.Handle, PWideChar(FKeyValue), Length(FKeyValue), FTextExtent) then // I2576 begin - if FTextExtent.cx > (RText.Right - RText.Left) * FKeyboard.FScale then + if (FTextExtent.cx > (RText.Right - RText.Left) * FKeyboard.FScale) or + (FTextExtent.cy > (RText.Bottom - RText.Top) * FKeyboard.FScale) then begin - Font.Height := Trunc(Font.Height * (RText.Right - RText.Left) * FKeyboard.FScale / FTextExtent.cx); - end - else if FTextExtent.cy > (RText.Bottom - RText.Top) * FKeyboard.FScale then - begin - Font.Height := Trunc(Font.Height * (RText.Bottom - RText.Top) * FKeyboard.FScale / FTextExtent.cy); - end + // font.height may be either positive or negative, depending on whether + // internal leading is included. It doesn't matter, so long as we keep + // the same factor. + if Font.Height < 0 then + Font.Height := System.Math.Max( + Trunc(Font.Height * (RText.Right - RText.Left) * FKeyboard.FScale / FTextExtent.cx), + Trunc(Font.Height * (RText.Bottom - RText.Top) * FKeyboard.FScale / FTextExtent.cy) + ) + else + Font.Height := System.Math.Min( + Trunc(Font.Height * (RText.Right - RText.Left) * FKeyboard.FScale / FTextExtent.cx), + Trunc(Font.Height * (RText.Bottom - RText.Top) * FKeyboard.FScale / FTextExtent.cy) + ); + end; end; + // TODO: in future re-scale the font here per designer's preference + // but this requires a file format change, so a much bigger scope if not Enabled then Font.Color := $808080 else Font.Color := clWindowText;