types.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. TObjectDynArray = array of TObject;
  22. TByteDynArray = array of Byte;
  23. TDuplicates = (dupIgnore, dupAccept, dupError);
  24. TListCallback = procedure(data, arg: JSValue) of object;
  25. TListStaticCallback = procedure(data, arg: JSValue);
  26. TSize = record
  27. cx, cy: integer;
  28. end;
  29. TPoint = record
  30. x, y: integer;
  31. end;
  32. TRect = record
  33. Left, Top, Right, Bottom: Integer;
  34. end;
  35. function EqualRect(const r1,r2 : TRect) : Boolean;
  36. function Rect(Left, Top, Right, Bottom : Integer) : TRect;
  37. function Bounds(ALeft, ATop, AWidth, AHeight : Integer) : TRect;
  38. function Point(x,y : Integer): TPoint; {$IFDEF Has_Inline}inline;{$ENDIF}
  39. function PtInRect(const aRect: TRect; const p: TPoint) : Boolean;
  40. function IntersectRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  41. function UnionRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  42. function IsRectEmpty(const aRect: TRect) : Boolean;
  43. function OffsetRect(var aRect: TRect; DX, DY: Integer) : Boolean;
  44. function CenterPoint(const aRect: TRect): TPoint;
  45. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  46. function Size(AWidth, AHeight: Integer): TSize;
  47. function Size(const aRect: TRect): TSize;
  48. implementation
  49. function EqualRect(const r1, r2: TRect): Boolean;
  50. begin
  51. Result:=(r1.left=r2.left) and (r1.right=r2.right) and (r1.top=r2.top) and (r1.bottom=r2.bottom);
  52. end;
  53. function Rect(Left, Top, Right, Bottom: Integer): TRect;
  54. begin
  55. Result.Left:=Left;
  56. Result.Top:=Top;
  57. Result.Right:=Right;
  58. Result.Bottom:=Bottom;
  59. end;
  60. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  61. begin
  62. Result.Left:=ALeft;
  63. Result.Top:=ATop;
  64. Result.Right:=ALeft+AWidth;
  65. Result.Bottom:=ATop+AHeight;
  66. end;
  67. function Point(x, y: Integer): TPoint;
  68. begin
  69. Result.x:=x;
  70. Result.y:=y;
  71. end;
  72. function PtInRect(const aRect: TRect; const p: TPoint): Boolean;
  73. begin
  74. Result:=(p.y>=aRect.Top) and
  75. (p.y<aRect.Bottom) and
  76. (p.x>=aRect.Left) and
  77. (p.x<aRect.Right);
  78. end;
  79. function IntersectRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  80. var
  81. lRect: TRect;
  82. begin
  83. lRect := R1;
  84. if R2.Left > R1.Left then
  85. lRect.Left := R2.Left;
  86. if R2.Top > R1.Top then
  87. lRect.Top := R2.Top;
  88. if R2.Right < R1.Right then
  89. lRect.Right := R2.Right;
  90. if R2.Bottom < R1.Bottom then
  91. lRect.Bottom := R2.Bottom;
  92. // The var parameter is only assigned in the end to avoid problems
  93. // when passing the same rectangle in the var and const parameters.
  94. if IsRectEmpty(lRect) then
  95. begin
  96. aRect:=Rect(0,0,0,0);
  97. Result:=false;
  98. end
  99. else
  100. begin
  101. Result:=true;
  102. aRect := lRect;
  103. end;
  104. end;
  105. function UnionRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  106. var
  107. lRect: TRect;
  108. begin
  109. lRect:=R1;
  110. if R2.Left<R1.Left then
  111. lRect.Left:=R2.Left;
  112. if R2.Top<R1.Top then
  113. lRect.Top:=R2.Top;
  114. if R2.Right>R1.Right then
  115. lRect.Right:=R2.Right;
  116. if R2.Bottom>R1.Bottom then
  117. lRect.Bottom:=R2.Bottom;
  118. if IsRectEmpty(lRect) then
  119. begin
  120. aRect:=Rect(0,0,0,0);
  121. Result:=false;
  122. end
  123. else
  124. begin
  125. aRect:=lRect;
  126. Result:=true;
  127. end;
  128. end;
  129. function IsRectEmpty(const aRect: TRect): Boolean;
  130. begin
  131. Result:=(aRect.Right<=aRect.Left) or (aRect.Bottom<=aRect.Top);
  132. end;
  133. function OffsetRect(var aRect: TRect; DX, DY: Integer): Boolean;
  134. begin
  135. with aRect do
  136. begin
  137. inc(Left,dx);
  138. inc(Top,dy);
  139. inc(Right,dx);
  140. inc(Bottom,dy);
  141. end;
  142. Result:=true;
  143. end;
  144. function CenterPoint(const aRect: TRect): TPoint;
  145. function Avg(a, b: Longint): Longint;
  146. begin
  147. if a < b then
  148. Result := a + ((b - a) shr 1)
  149. else
  150. Result := b + ((a - b) shr 1);
  151. end;
  152. begin
  153. with aRect do
  154. begin
  155. Result.X := Avg(Left, Right);
  156. Result.Y := Avg(Top, Bottom);
  157. end;
  158. end;
  159. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  160. begin
  161. with aRect do
  162. begin
  163. dec(Left, dx);
  164. dec(Top, dy);
  165. inc(Right, dx);
  166. inc(Bottom, dy);
  167. end;
  168. Result := True;
  169. end;
  170. function Size(AWidth, AHeight: Integer): TSize;
  171. begin
  172. Result.cx := AWidth;
  173. Result.cy := AHeight;
  174. end;
  175. function Size(const aRect: TRect): TSize;
  176. begin
  177. Result.cx := aRect.Right - aRect.Left;
  178. Result.cy := aRect.Bottom - aRect.Top;
  179. end;
  180. end.