types.pas 5.0 KB

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