types.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2018 by Mattias Gaertner
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit Types;
  11. {$mode objfpc}
  12. interface
  13. type
  14. TDirection = (FromBeginning, FromEnd);
  15. TBooleanDynArray = array of Boolean;
  16. TIntegerDynArray = array of Integer;
  17. TNativeIntDynArray = array of NativeInt;
  18. TStringDynArray = array of String;
  19. TDoubleDynArray = array of Double;
  20. TJSValueDynArray = array of JSValue;
  21. TDuplicates = (dupIgnore, dupAccept, dupError);
  22. TListCallback = procedure(data, arg: JSValue) of object;
  23. TListStaticCallback = procedure(data, arg: JSValue);
  24. TSize = record
  25. cx, cy: integer;
  26. end;
  27. TPoint = record
  28. x, y: integer;
  29. end;
  30. TRect = record
  31. Left, Top, Right, Bottom: Integer;
  32. end;
  33. function EqualRect(const r1,r2 : TRect) : Boolean;
  34. function Rect(Left, Top, Right, Bottom : Integer) : TRect;
  35. function Bounds(ALeft, ATop, AWidth, AHeight : Integer) : TRect;
  36. function Point(x,y : Integer): TPoint; {$IFDEF Has_Inline}inline;{$ENDIF}
  37. function PtInRect(const aRect: TRect; const p: TPoint) : Boolean;
  38. function IntersectRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  39. function UnionRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  40. function IsRectEmpty(const aRect: TRect) : Boolean;
  41. function OffsetRect(var aRect: TRect; DX, DY: Integer) : Boolean;
  42. function CenterPoint(const aRect: TRect): TPoint;
  43. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  44. function Size(AWidth, AHeight: Integer): TSize;
  45. function Size(const aRect: TRect): TSize;
  46. implementation
  47. function EqualRect(const r1, r2: TRect): Boolean;
  48. begin
  49. Result:=(r1.left=r2.left) and (r1.right=r2.right) and (r1.top=r2.top) and (r1.bottom=r2.bottom);
  50. end;
  51. function Rect(Left, Top, Right, Bottom: Integer): TRect;
  52. begin
  53. Result.Left:=Left;
  54. Result.Top:=Top;
  55. Result.Right:=Right;
  56. Result.Bottom:=Bottom;
  57. end;
  58. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  59. begin
  60. Result.Left:=ALeft;
  61. Result.Top:=ATop;
  62. Result.Right:=ALeft+AWidth;
  63. Result.Bottom:=ATop+AHeight;
  64. end;
  65. function Point(x, y: Integer): TPoint;
  66. begin
  67. Result.x:=x;
  68. Result.y:=y;
  69. end;
  70. function PtInRect(const aRect: TRect; const p: TPoint): Boolean;
  71. begin
  72. Result:=(p.y>=aRect.Top) and
  73. (p.y<aRect.Bottom) and
  74. (p.x>=aRect.Left) and
  75. (p.x<aRect.Right);
  76. end;
  77. function IntersectRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  78. var
  79. lRect: TRect;
  80. begin
  81. lRect := R1;
  82. if R2.Left > R1.Left then
  83. lRect.Left := R2.Left;
  84. if R2.Top > R1.Top then
  85. lRect.Top := R2.Top;
  86. if R2.Right < R1.Right then
  87. lRect.Right := R2.Right;
  88. if R2.Bottom < R1.Bottom then
  89. lRect.Bottom := R2.Bottom;
  90. // The var parameter is only assigned in the end to avoid problems
  91. // when passing the same rectangle in the var and const parameters.
  92. if IsRectEmpty(lRect) then
  93. begin
  94. aRect:=Rect(0,0,0,0);
  95. Result:=false;
  96. end
  97. else
  98. begin
  99. Result:=true;
  100. aRect := lRect;
  101. end;
  102. end;
  103. function UnionRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  104. var
  105. lRect: TRect;
  106. begin
  107. lRect:=R1;
  108. if R2.Left<R1.Left then
  109. lRect.Left:=R2.Left;
  110. if R2.Top<R1.Top then
  111. lRect.Top:=R2.Top;
  112. if R2.Right>R1.Right then
  113. lRect.Right:=R2.Right;
  114. if R2.Bottom>R1.Bottom then
  115. lRect.Bottom:=R2.Bottom;
  116. if IsRectEmpty(lRect) then
  117. begin
  118. aRect:=Rect(0,0,0,0);
  119. Result:=false;
  120. end
  121. else
  122. begin
  123. aRect:=lRect;
  124. Result:=true;
  125. end;
  126. end;
  127. function IsRectEmpty(const aRect: TRect): Boolean;
  128. begin
  129. Result:=(aRect.Right<=aRect.Left) or (aRect.Bottom<=aRect.Top);
  130. end;
  131. function OffsetRect(var aRect: TRect; DX, DY: Integer): Boolean;
  132. begin
  133. with aRect do
  134. begin
  135. inc(Left,dx);
  136. inc(Top,dy);
  137. inc(Right,dx);
  138. inc(Bottom,dy);
  139. end;
  140. Result:=true;
  141. end;
  142. function CenterPoint(const aRect: TRect): TPoint;
  143. function Avg(a, b: Longint): Longint;
  144. begin
  145. if a < b then
  146. Result := a + ((b - a) shr 1)
  147. else
  148. Result := b + ((a - b) shr 1);
  149. end;
  150. begin
  151. with aRect do
  152. begin
  153. Result.X := Avg(Left, Right);
  154. Result.Y := Avg(Top, Bottom);
  155. end;
  156. end;
  157. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  158. begin
  159. with aRect do
  160. begin
  161. dec(Left, dx);
  162. dec(Top, dy);
  163. inc(Right, dx);
  164. inc(Bottom, dy);
  165. end;
  166. Result := True;
  167. end;
  168. function Size(AWidth, AHeight: Integer): TSize;
  169. begin
  170. Result.cx := AWidth;
  171. Result.cy := AHeight;
  172. end;
  173. function Size(const aRect: TRect): TSize;
  174. begin
  175. Result.cx := aRect.Right - aRect.Left;
  176. Result.cy := aRect.Bottom - aRect.Top;
  177. end;
  178. end.