Browse Source

* Add some missing identifiers to TRectF. Fixes issue #40068

Michaël Van Canneyt 1 year ago
parent
commit
e28add8b31
1 changed files with 138 additions and 13 deletions
  1. 138 13
      rtl/objpas/types.pp

+ 138 - 13
rtl/objpas/types.pp

@@ -141,6 +141,7 @@ type
           procedure Offset(const apt :TPointF);
           procedure Offset(const apt :TPoint);
           procedure Offset(dx,dy : Single);
+          function EqualsTo(const apt: TPointF; const aEpsilon : Single): Boolean;
 
           function  Scale (afactor:Single)  : TPointF;
           function  Ceiling : TPoint;
@@ -213,6 +214,11 @@ type
           property Height: Single read cy write cy;
        end;
 
+  {$SCOPEDENUMS ON}
+  TVertRectAlign = (Center, Top, Bottom);
+  THorzRectAlign = (Center, Left, Right);
+  {$SCOPEDENUMS OFF}
+
   { TRectF }
   PRectF = ^TRectF;
   TRectF =
@@ -243,25 +249,33 @@ type
     class operator := (const arc: TRect): TRectF;
     class function Empty: TRectF; static;
 
-    procedure NormalizeRect;
-    function IsEmpty: Boolean;
-    function Contains(Pt: TPointF): Boolean;
-    function Contains(R: TRectF): Boolean;
-    function IntersectsWith(R: TRectF): Boolean;
     class function Intersect(R1: TRectF; R2: TRectF): TRectF; static;
-    procedure Intersect(R: TRectF);
-    class function Union(R1, R2: TRectF): TRectF; static;
     class function Union(const Points: array of TPointF): TRectF; static;
-    procedure SetLocation(X, Y: Single);
-    procedure SetLocation(P: TPointF);
-    procedure Inflate(DX, DY: Single);
-    procedure Inflate(DL, DT, DR, DB: Single);
+    class function Union(R1, R2: TRectF): TRectF; static;
+    Function Ceiling : TRectF;
+    function CenterAt(const Dest: TRectF): TRectF;
     function CenterPoint: TPointF;
-    function FitInto(const Dest: TRectF; out Ratio: Single): TRectF; overload;
+    function Contains(Pt: TPointF): Boolean;
+    function Contains(R: TRectF): Boolean;
+    function EqualsTo(const R: TRectF; const Epsilon: Single = 0): Boolean;
+    function Fit(const Dest: TRectF): Single; deprecated 'Use FitInto';
     function FitInto(const Dest: TRectF): TRectF; overload;
-    procedure Union  (const r: TRectF); inline;
+    function FitInto(const Dest: TRectF; out Ratio: Single): TRectF; overload;
+    function IntersectsWith(R: TRectF): Boolean;
+    function IsEmpty: Boolean;
+    function PlaceInto(const Dest: TRectF; const AHorzAlign: THorzRectAlign = THorzRectAlign.Center;  const AVertAlign: TVertRectAlign = TVertRectAlign.Center): TRectF;
+    function Round: TRect;
+    function SnapToPixel(AScale: Single; APlaceBetweenPixels: Boolean = True): TRectF;
+    function Truncate: TRect;
+    procedure Inflate(DL, DT, DR, DB: Single);
+    procedure Inflate(DX, DY: Single);
+    procedure Intersect(R: TRectF);
+    procedure NormalizeRect;
     procedure Offset (const dx,dy : Single); inline;
     procedure Offset (DP: TPointF); inline;
+    procedure SetLocation(P: TPointF);
+    procedure SetLocation(X, Y: Single);
+    procedure Union  (const r: TRectF); inline;
     property  Width  : Single read GetWidth write SetWidth;
     property  Height : Single read GetHeight write SetHeight;
     property  Size   : TSizeF read getSize   write SetSize;
@@ -711,6 +725,18 @@ begin
   y:=y+dy;
 end;
 
+function TPointF.EqualsTo(const apt: TPointF; const aEpsilon: Single): Boolean;
+
+  function Eq(a,b : single) : boolean; inline;
+
+  begin
+    result:=abs(a-b)<=aEpsilon;
+  end;
+
+begin
+  Result:=Eq(X,apt.X) and Eq(Y,apt.Y);
+end;
+
 function TPointF.Scale(afactor: Single): TPointF;
 begin
   result.x:=afactor*x;
@@ -1099,6 +1125,27 @@ begin
   Result.Y := (Bottom-Top) / 2 + Top;
 end;
 
+function TRectF.Ceiling: TRectF;
+begin
+  Result.BottomRight:=BottomRight.Ceiling;
+  Result.TopLeft:=TopLeft.Ceiling;
+end;
+
+function TRectF.CenterAt(const Dest: TRectF): TRectF;
+begin
+
+end;
+
+function TRectF.Fit(const Dest: TRectF): Single;
+
+var
+  R : TRectF;
+
+begin
+  R:=FitInto(Dest,Result);
+  Self:=R;
+end;
+
 function TRectF.FitInto(const Dest: TRectF; out Ratio: Single): TRectF;
 begin
   if (Dest.Width<=0) or (Dest.Height<=0) then
@@ -1122,6 +1169,66 @@ begin
   Result:=FitInto(Dest,Ratio);
 end;
 
+function TRectF.PlaceInto(const Dest: TRectF; const AHorzAlign: THorzRectAlign = THorzRectAlign.Center;  const AVertAlign: TVertRectAlign = TVertRectAlign.Center): TRectF;
+
+var
+  R : TRectF;
+  X,Y : Single;
+  D : TRectF absolute dest;
+
+begin
+  if (Height>Dest.Height) or (Width>Dest.Width) then
+    R:=FitInto(Dest)
+  else
+    R:=Self;
+  case AHorzAlign of
+     THorzRectAlign.Left:
+       X:=D.Left;
+     THorzRectAlign.Center:
+       X:=(D.Left+D.Right-R.Width)/2;
+     THorzRectAlign.Right:
+       X:=D.Right-R.Width;
+  end;
+  case AVertAlign of
+    TVertRectAlign.Top:
+      Y:=D.Top;
+    TVertRectAlign.Center:
+      Y:=(D.Top+D.Bottom-R.Height)/2;
+    TVertRectAlign.Bottom:
+      Y:=D.Bottom-R.Height;
+  end;
+  R.SetLocation(PointF(X,Y));
+  Result:=R;
+end;
+
+function TRectF.SnapToPixel(AScale: Single; APlaceBetweenPixels: Boolean): TRectF;
+
+  function sc (S : single) : single; inline;
+
+  begin
+    Result:=System.Trunc(S*AScale)/AScale;
+  end;
+
+var
+  R : TRectF;
+  Off: Single;
+
+begin
+  if AScale<=0 then
+    AScale := 1;
+  R.Top:=Sc(Top);
+  R.Left:=Sc(Left);
+  R.Width:=Sc(Width);
+  R.Height:=Sc(Height);
+  if APlaceBetweenPixels then
+    begin
+    Off:=1/(2*aScale);
+    R.Offset(Off,Off);
+    end;
+  Result:=R;
+end;
+
+
 function TRectF.Contains(Pt: TPointF): Boolean;
 begin
   Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);
@@ -1137,6 +1244,12 @@ begin
   Result := TRectF.Create(0,0,0,0);
 end;
 
+function TRectF.EqualsTo(const R: TRectF; const Epsilon: Single): Boolean;
+begin
+  Result:=TopLeft.EqualsTo(R.TopLeft,Epsilon);
+  Result:=Result and BottomRight.EqualsTo(R.BottomRight,Epsilon);
+end;
+
 function TRectF.GetHeight: Single;
 begin
   result:=bottom-top;
@@ -1231,6 +1344,18 @@ begin
   bottom:=bottom+DP.y; top:=top+DP.y;
 end;
 
+function TRectF.Truncate: TRect;
+begin
+  Result.BottomRight:=BottomRight.Truncate;
+  Result.TopLeft:=TopLeft.Truncate;
+end;
+
+function TRectF.Round: TRect;
+begin
+  Result.BottomRight:=BottomRight.Round;
+  Result.TopLeft:=TopLeft.Round;
+end;
+
 procedure TRectF.SetHeight(AValue: Single);
 begin
   bottom:=top+avalue;