types.pp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Florian Klaempfl,
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$MODE OBJFPC}
  12. unit Types;
  13. interface
  14. {$modeswitch advancedrecords}
  15. {$modeswitch class}
  16. {$if defined(win32) or defined(win64) or defined(wince)}
  17. uses
  18. Windows;
  19. {$elseif defined(win16)}
  20. uses
  21. WinTypes;
  22. {$endif}
  23. {$if defined(win32) or defined(win64)}
  24. const
  25. RT_RCDATA = Windows.RT_RCDATA deprecated 'Use Windows.RT_RCDATA instead';
  26. {$elseif defined(win16)}
  27. const
  28. RT_RCDATA = WinTypes.RT_RCDATA deprecated 'Use WinTypes.RT_RCDATA instead';
  29. {$endif}
  30. type
  31. TEndian = Objpas.TEndian;
  32. TDirection = (FromBeginning, FromEnd);
  33. TValueRelationship = -1..1;
  34. DWORD = LongWord;
  35. PLongint = System.PLongint;
  36. PSmallInt = System.PSmallInt;
  37. {$ifndef FPUNONE}
  38. PDouble = System.PDouble;
  39. {$endif}
  40. PByte = System.PByte;
  41. Largeint = int64;
  42. LARGE_INT = LargeInt;
  43. PLargeInt = ^LargeInt;
  44. LargeUint = qword;
  45. LARGE_UINT= LargeUInt;
  46. PLargeuInt = ^LargeuInt;
  47. TBooleanDynArray = array of Boolean;
  48. TByteDynArray = array of Byte;
  49. TCardinalDynArray = array of Cardinal;
  50. TInt64DynArray = array of Int64;
  51. TIntegerDynArray = array of Integer;
  52. TLongWordDynArray = array of LongWord;
  53. TPointerDynArray = array of Pointer;
  54. TQWordDynArray = array of QWord;
  55. TShortIntDynArray = array of ShortInt;
  56. TSmallIntDynArray = array of SmallInt;
  57. TStringDynArray = array of AnsiString;
  58. TObjectDynArray = array of TObject;
  59. TWideStringDynArray = array of WideString;
  60. TWordDynArray = array of Word;
  61. TCurrencyArray = Array of currency;
  62. {$ifndef FPUNONE}
  63. TSingleDynArray = array of Single;
  64. TDoubleDynArray = array of Double;
  65. TExtendedDynArray = array of Extended;
  66. TCompDynArray = array of Comp;
  67. {$endif}
  68. {$if defined(win32) or defined(win64) or defined(wince)}
  69. TArray4IntegerType = Windows.TArray4IntegerType;
  70. TSmallPoint = Windows.TSmallPoint;
  71. PSmallPoint = Windows.PSmallPoint;
  72. TSize = Windows.TSize;
  73. TagSize = Windows.tagSize deprecated;
  74. PSize = Windows.PSize;
  75. TPoint = Windows.TPoint;
  76. TagPoint = Windows.TagPoint deprecated;
  77. PPoint = Windows.PPoint;
  78. TRect = Windows.TRect;
  79. PRect = Windows.PRect;
  80. TSplitRectType = Windows.TSplitRectType;
  81. const
  82. srLeft = TSplitRectType.srLeft;
  83. srRight = TSplitRectType.srRight;
  84. srTop = TSplitRectType.srTop;
  85. srBottom = TSplitRectType.srBottom;
  86. type
  87. {$else}
  88. {$i typshrdh.inc}
  89. TagSize = tSize deprecated;
  90. TagPoint = TPoint deprecated;
  91. {$endif}
  92. { TPointF }
  93. PPointF = ^TPointF;
  94. TPointF =
  95. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  96. packed
  97. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  98. record
  99. x,y : Single;
  100. public
  101. function Add(const apt: TPoint): TPointF;
  102. function Add(const apt: TPointF): TPointF;
  103. function Distance(const apt : TPointF) : Single;
  104. function DotProduct(const apt : TPointF) : Single;
  105. function IsZero : Boolean;
  106. function Subtract(const apt : TPointF): TPointF;
  107. function Subtract(const apt : TPoint): TPointF;
  108. procedure SetLocation(const apt :TPointF);
  109. procedure SetLocation(const apt :TPoint);
  110. procedure SetLocation(ax,ay : Longint);
  111. procedure Offset(const apt :TPointF);
  112. procedure Offset(const apt :TPoint);
  113. procedure Offset(dx,dy : Longint);
  114. function Scale (afactor:Single) : TPointF;
  115. function Ceiling : TPoint;
  116. function Truncate: TPoint;
  117. function Floor : TPoint;
  118. function Round : TPoint;
  119. function Length : Single;
  120. class function Create(const ax, ay: Single): TPointF; overload; static; inline;
  121. class function Create(const apt: TPoint): TPointF; overload; static; inline;
  122. class operator = (const apt1, apt2 : TPointF) : Boolean;
  123. class operator <> (const apt1, apt2 : TPointF): Boolean;
  124. class operator + (const apt1, apt2 : TPointF): TPointF;
  125. class operator - (const apt1, apt2 : TPointF): TPointF;
  126. class operator - (const apt1 : TPointF): TPointF;
  127. class operator * (const apt1, apt2: TPointF): Single; // scalar product
  128. class operator * (const apt1: TPointF; afactor: single): TPointF;
  129. class operator * (afactor: single; const apt1: TPointF): TPointF;
  130. class operator := (const apt: TPoint): TPointF;
  131. end;
  132. { TSizeF }
  133. PSizeF = ^TSizeF;
  134. TSizeF =
  135. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  136. packed
  137. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  138. record
  139. cx,cy : Single;
  140. public
  141. function Add(const asz: TSize): TSizeF;
  142. function Add(const asz: TSizeF): TSizeF;
  143. function Distance(const asz : TSizeF) : Single;
  144. function IsZero : Boolean;
  145. function Subtract(const asz : TSizeF): TSizeF;
  146. function Subtract(const asz : TSize): TSizeF;
  147. function Scale (afactor:Single) : TSizeF;
  148. function Ceiling : TSize;
  149. function Truncate: TSize;
  150. function Floor : TSize;
  151. function Round : TSize;
  152. function Length : Single;
  153. class function Create(const ax, ay: Single): TSizeF; overload; static; inline;
  154. class function Create(const asz: TSize): TSizeF; overload; static; inline;
  155. class operator = (const asz1, asz2 : TSizeF) : Boolean;
  156. class operator <> (const asz1, asz2 : TSizeF): Boolean;
  157. class operator + (const asz1, asz2 : TSizeF): TSizeF;
  158. class operator - (const asz1, asz2 : TSizeF): TSizeF;
  159. class operator - (const asz1 : TSizeF): TSizeF;
  160. class operator * (const asz1: TSizeF; afactor: single): TSizeF;
  161. class operator * (afactor: single; const asz1: TSizeF): TSizeF;
  162. class operator := (const apt: TPointF): TSizeF;
  163. class operator := (const asz: TSize): TSizeF;
  164. class operator := (const asz: TSizeF): TPointF;
  165. property Width: Single read cx write cx;
  166. property Height: Single read cy write cy;
  167. end;
  168. { TRectF }
  169. PRectF = ^TRectF;
  170. TRectF =
  171. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  172. packed
  173. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  174. record
  175. private
  176. function GetLocation: TPointF;
  177. function GetSize: TSizeF;
  178. procedure SetSize(AValue: TSizeF);
  179. function GetHeight: Single; inline;
  180. function GetWidth: Single; inline;
  181. procedure SetHeight(AValue: Single);
  182. procedure SetWidth (AValue: Single);
  183. public
  184. constructor Create(Origin: TPointF); // empty rect at given origin
  185. constructor Create(Origin: TPointF; AWidth, AHeight: Single);
  186. constructor Create(ALeft, ATop, ARight, ABottom: Single);
  187. constructor Create(P1, P2: TPointF; Normalize: Boolean = False);
  188. constructor Create(R: TRectF; Normalize: Boolean = False);
  189. constructor Create(R: TRect; Normalize: Boolean = False);
  190. class operator = (L, R: TRectF): Boolean;
  191. class operator <> (L, R: TRectF): Boolean;
  192. class operator + (L, R: TRectF): TRectF; // union
  193. class operator * (L, R: TRectF): TRectF; // intersection
  194. class operator := (const arc: TRect): TRectF;
  195. class function Empty: TRectF; static;
  196. procedure NormalizeRect;
  197. function IsEmpty: Boolean;
  198. function Contains(Pt: TPointF): Boolean;
  199. function Contains(R: TRectF): Boolean;
  200. function IntersectsWith(R: TRectF): Boolean;
  201. class function Intersect(R1: TRectF; R2: TRectF): TRectF; static;
  202. procedure Intersect(R: TRectF);
  203. class function Union(R1, R2: TRectF): TRectF; static;
  204. class function Union(const Points: array of TPointF): TRectF; static;
  205. procedure SetLocation(X, Y: Single);
  206. procedure SetLocation(P: TPointF);
  207. procedure Inflate(DX, DY: Single);
  208. procedure Inflate(DL, DT, DR, DB: Single);
  209. function CenterPoint: TPointF;
  210. procedure Union (const r: TRectF); inline;
  211. procedure Offset (const dx,dy : Single); inline;
  212. procedure Offset (DP: TPointF); inline;
  213. property Width : Single read GetWidth write SetWidth;
  214. property Height : Single read GetHeight write SetHeight;
  215. property Size : TSizeF read getSize write SetSize;
  216. property Location: TPointF read getLocation write setLocation;
  217. case Integer of
  218. 0: (Left, Top, Right, Bottom: Single);
  219. 1: (TopLeft, BottomRight: TPointF);
  220. end;
  221. TDuplicates = (dupIgnore, dupAccept, dupError);
  222. TPoint3D =
  223. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  224. packed
  225. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  226. record
  227. public
  228. Type TSingle3Array = array[0..2] of single;
  229. constructor Create(const ax,ay,az:single);
  230. procedure Offset(const adeltax,adeltay,adeltaz:single); inline;
  231. procedure Offset(const adelta:TPoint3D); inline;
  232. public
  233. case Integer of
  234. 0: (data:TSingle3Array);
  235. 1: (x,y,z : single);
  236. end;
  237. type
  238. TOleChar = WideChar;
  239. POleStr = PWideChar;
  240. PPOleStr = ^POleStr;
  241. TListCallback = procedure(data,arg:pointer) of object;
  242. TListStaticCallback = procedure(data,arg:pointer);
  243. const
  244. GUID_NULL: TGUID = '{00000000-0000-0000-0000-000000000000}';
  245. STGTY_STORAGE = 1;
  246. STGTY_STREAM = 2;
  247. STGTY_LOCKBYTES = 3;
  248. STGTY_PROPERTY = 4;
  249. STREAM_SEEK_SET = 0;
  250. STREAM_SEEK_CUR = 1;
  251. STREAM_SEEK_END = 2;
  252. LOCK_WRITE = 1;
  253. LOCK_EXCLUSIVE = 2;
  254. LOCK_ONLYONCE = 4;
  255. STATFLAG_DEFAULT = 0;
  256. STATFLAG_NONAME = 1;
  257. STATFLAG_NOOPEN = 2;
  258. {$ifndef Wince}
  259. // in Wince these are in unit windows. Under 32/64 in ActiveX.
  260. // for now duplicate them. Not that bad for untyped constants.
  261. E_FAIL = HRESULT($80004005);
  262. E_INVALIDARG = HRESULT($80070057);
  263. STG_E_INVALIDFUNCTION = HRESULT($80030001);
  264. STG_E_FILENOTFOUND = HRESULT($80030002);
  265. STG_E_PATHNOTFOUND = HRESULT($80030003);
  266. STG_E_TOOMANYOPENFILES = HRESULT($80030004);
  267. STG_E_ACCESSDENIED = HRESULT($80030005);
  268. STG_E_INVALIDHANDLE = HRESULT($80030006);
  269. STG_E_INSUFFICIENTMEMORY = HRESULT($80030008);
  270. STG_E_INVALIDPOINTER = HRESULT($80030009);
  271. STG_E_NOMOREFILES = HRESULT($80030012);
  272. STG_E_DISKISWRITEPROTECTED = HRESULT($80030013);
  273. STG_E_SEEKERROR = HRESULT($80030019);
  274. STG_E_WRITEFAULT = HRESULT($8003001D);
  275. STG_E_READFAULT = HRESULT($8003001E);
  276. STG_E_SHAREVIOLATION = HRESULT($80030020);
  277. STG_E_LOCKVIOLATION = HRESULT($80030021);
  278. STG_E_FILEALREADYEXISTS = HRESULT($80030050);
  279. STG_E_INVALIDPARAMETER = HRESULT($80030057);
  280. STG_E_MEDIUMFULL = HRESULT($80030070);
  281. STG_E_PROPSETMISMATCHED = HRESULT($800300F0);
  282. STG_E_ABNORMALAPIEXIT = HRESULT($800300FA);
  283. STG_E_INVALIDHEADER = HRESULT($800300FB);
  284. STG_E_INVALIDNAME = HRESULT($800300FC);
  285. STG_E_UNKNOWN = HRESULT($800300FD);
  286. STG_E_UNIMPLEMENTEDFUNCTION = HRESULT($800300FE);
  287. STG_E_INVALIDFLAG = HRESULT($800300FF);
  288. STG_E_INUSE = HRESULT($80030100);
  289. STG_E_NOTCURRENT = HRESULT($80030101);
  290. STG_E_REVERTED = HRESULT($80030102);
  291. STG_E_CANTSAVE = HRESULT($80030103);
  292. STG_E_OLDFORMAT = HRESULT($80030104);
  293. STG_E_OLDDLL = HRESULT($80030105);
  294. STG_E_SHAREREQUIRED = HRESULT($80030106);
  295. STG_E_EXTANTMARSHALLINGS = HRESULT($80030108);
  296. STG_E_DOCFILECORRUPT = HRESULT($80030109);
  297. STG_E_BADBASEADDRESS = HRESULT($80030110);
  298. STG_E_INCOMPLETE = HRESULT($80030201);
  299. STG_E_TERMINATED = HRESULT($80030202);
  300. STG_S_CONVERTED = $00030200;
  301. STG_S_BLOCK = $00030201;
  302. STG_S_RETRYNOW = $00030202;
  303. STG_S_MONITORING = $00030203;
  304. {$endif}
  305. {$if (not defined(win32)) and (not defined(win64)) and (not defined(wince))}
  306. type
  307. PCLSID = PGUID;
  308. TCLSID = TGUID;
  309. PDWord = ^DWord;
  310. PDisplay = Pointer;
  311. PEvent = Pointer;
  312. TXrmOptionDescRec = record
  313. end;
  314. XrmOptionDescRec = TXrmOptionDescRec;
  315. PXrmOptionDescRec = ^TXrmOptionDescRec;
  316. Widget = Pointer;
  317. WidgetClass = Pointer;
  318. ArgList = Pointer;
  319. Region = Pointer;
  320. _FILETIME =
  321. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  322. packed
  323. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  324. record
  325. dwLowDateTime : DWORD;
  326. dwHighDateTime : DWORD;
  327. end;
  328. TFileTime = _FILETIME;
  329. FILETIME = _FILETIME;
  330. PFileTime = ^TFileTime;
  331. {$else}
  332. type
  333. PCLSID = Windows.PCLSID;
  334. TCLSID = Windows.CLSID;
  335. TFiletime = Windows.TFileTime;
  336. Filetime = Windows.FileTime;
  337. PFiletime = Windows.PFileTime;
  338. {$endif Windows}
  339. type
  340. tagSTATSTG = record
  341. pwcsName : POleStr;
  342. dwType : DWord;
  343. cbSize : Large_uint;
  344. mtime : TFileTime;
  345. ctime : TFileTime;
  346. atime : TFileTime;
  347. grfMode : DWord;
  348. grfLocksSupported : DWord;
  349. clsid : TCLSID;
  350. grfStateBits : DWord;
  351. reserved : DWord;
  352. end;
  353. TStatStg = tagSTATSTG;
  354. STATSTG = TStatStg;
  355. PStatStg = ^TStatStg;
  356. { classes depends on these interfaces, we can't use the activex unit in classes though }
  357. IClassFactory = Interface(IUnknown) ['{00000001-0000-0000-C000-000000000046}']
  358. Function CreateInstance(Const unkOuter : IUnknown;Const riid : TGUID;Out vObject) : HResult;StdCall;
  359. Function LockServer(fLock : LongBool) : HResult;StdCall;
  360. End;
  361. ISequentialStream = interface(IUnknown)
  362. ['{0c733a30-2a1c-11ce-ade5-00aa0044773d}']
  363. function Read(pv : Pointer;cb : DWORD;pcbRead : PDWORD) : HRESULT;stdcall;
  364. function Write(pv : Pointer;cb : DWORD;pcbWritten : PDWORD): HRESULT;stdcall;
  365. end;
  366. IStream = interface(ISequentialStream) ['{0000000C-0000-0000-C000-000000000046}']
  367. function Seek(dlibMove : LargeInt; dwOrigin : DWORD; out libNewPosition : LargeUInt) : HResult;stdcall;
  368. function SetSize(libNewSize : LargeUInt) : HRESULT;stdcall;
  369. function CopyTo(stm: IStream;cb : LargeUInt;out cbRead : LargeUInt; out cbWritten : LargeUInt) : HRESULT;stdcall;
  370. function Commit(grfCommitFlags : DWORD) : HRESULT;stdcall;
  371. function Revert : HRESULT;stdcall;
  372. function LockRegion(libOffset : LargeUInt;cb : LargeUInt; dwLockType : DWORD) : HRESULT;stdcall;
  373. function UnlockRegion(libOffset : LargeUInt;cb : LargeUInt; dwLockType : DWORD) : HRESULT;stdcall;
  374. Function Stat(out statstg : TStatStg;grfStatFlag : DWORD) : HRESULT;stdcall;
  375. function Clone(out stm : IStream) : HRESULT;stdcall;
  376. end;
  377. function EqualRect(const r1,r2 : TRect) : Boolean;
  378. function Rect(Left,Top,Right,Bottom : Integer) : TRect; inline;
  379. function RectF(Left,Top,Right,Bottom : Single) : TRectF; inline;
  380. function Bounds(ALeft,ATop,AWidth,AHeight : Integer) : TRect; inline;
  381. function Point(x,y : Integer) : TPoint; inline;
  382. function PointF(x,y: Single) : TPointF; inline;
  383. function PtInRect(const Rect : TRect; const p : TPoint) : Boolean;
  384. function IntersectRect(var Rect : TRect; const R1,R2 : TRect) : Boolean;
  385. function UnionRect(var Rect : TRect; const R1,R2 : TRect) : Boolean;
  386. function IsRectEmpty(const Rect : TRect) : Boolean;
  387. function OffsetRect(var Rect : TRect;DX : Integer;DY : Integer) : Boolean;
  388. function CenterPoint(const Rect: TRect): TPoint;
  389. function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;
  390. function Size(AWidth, AHeight: Integer): TSize; inline;
  391. function Size(const ARect: TRect): TSize;
  392. {$ifndef VER3_0}
  393. type
  394. TBitConverter = class
  395. generic class procedure UnsafeFrom<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0); static; {inline;}
  396. generic class procedure From<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0); static;
  397. generic class function UnsafeInTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T; static; {inline;}
  398. generic class function InTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T; static;
  399. end;
  400. {$endif}
  401. implementation
  402. Uses Math;
  403. {$if (not defined(win32)) and (not defined(win64)) and (not defined(wince))}
  404. {$i typshrd.inc}
  405. {$endif}
  406. function EqualRect(const r1,r2 : TRect) : Boolean;
  407. begin
  408. EqualRect:=(r1.left=r2.left) and (r1.right=r2.right) and (r1.top=r2.top) and (r1.bottom=r2.bottom);
  409. end;
  410. function Rect(Left,Top,Right,Bottom : Integer) : TRect; inline;
  411. begin
  412. Rect.Left:=Left;
  413. Rect.Top:=Top;
  414. Rect.Right:=Right;
  415. Rect.Bottom:=Bottom;
  416. end;
  417. function RectF(Left,Top,Right,Bottom : Single) : TRectF; inline;
  418. begin
  419. RectF.Left:=Left;
  420. RectF.Top:=Top;
  421. RectF.Right:=Right;
  422. RectF.Bottom:=Bottom;
  423. end;
  424. function Bounds(ALeft,ATop,AWidth,AHeight : Integer) : TRect; inline;
  425. begin
  426. Bounds.Left:=ALeft;
  427. Bounds.Top:=ATop;
  428. Bounds.Right:=ALeft+AWidth;
  429. Bounds.Bottom:=ATop+AHeight;
  430. end;
  431. function Point(x,y : Integer) : TPoint; inline;
  432. begin
  433. Point.x:=x;
  434. Point.y:=y;
  435. end;
  436. function PointF(x,y: Single) : TPointF; inline;
  437. begin
  438. PointF.x:=x;
  439. PointF.y:=y;
  440. end;
  441. function PtInRect(const Rect : TRect;const p : TPoint) : Boolean;
  442. begin
  443. PtInRect:=(p.y>=Rect.Top) and
  444. (p.y<Rect.Bottom) and
  445. (p.x>=Rect.Left) and
  446. (p.x<Rect.Right);
  447. end;
  448. function IntersectRect(var Rect : TRect;const R1,R2 : TRect) : Boolean;
  449. var
  450. lRect: TRect;
  451. begin
  452. lRect := R1;
  453. if R2.Left > R1.Left then
  454. lRect.Left := R2.Left;
  455. if R2.Top > R1.Top then
  456. lRect.Top := R2.Top;
  457. if R2.Right < R1.Right then
  458. lRect.Right := R2.Right;
  459. if R2.Bottom < R1.Bottom then
  460. lRect.Bottom := R2.Bottom;
  461. // The var parameter is only assigned in the end to avoid problems
  462. // when passing the same rectangle in the var and const parameters.
  463. // See http://bugs.freepascal.org/view.php?id=17722
  464. if IsRectEmpty(lRect) then
  465. begin
  466. FillChar(Rect,SizeOf(Rect),0);
  467. IntersectRect:=false;
  468. end
  469. else
  470. begin
  471. IntersectRect:=true;
  472. Rect := lRect;
  473. end;
  474. end;
  475. function UnionRect(var Rect : TRect;const R1,R2 : TRect) : Boolean;
  476. var
  477. lRect: TRect;
  478. begin
  479. lRect:=R1;
  480. if R2.Left<R1.Left then
  481. lRect.Left:=R2.Left;
  482. if R2.Top<R1.Top then
  483. lRect.Top:=R2.Top;
  484. if R2.Right>R1.Right then
  485. lRect.Right:=R2.Right;
  486. if R2.Bottom>R1.Bottom then
  487. lRect.Bottom:=R2.Bottom;
  488. if IsRectEmpty(lRect) then
  489. begin
  490. FillChar(Rect,SizeOf(Rect),0);
  491. UnionRect:=false;
  492. end
  493. else
  494. begin
  495. Rect:=lRect;
  496. UnionRect:=true;
  497. end;
  498. end;
  499. function IsRectEmpty(const Rect : TRect) : Boolean;
  500. begin
  501. IsRectEmpty:=(Rect.Right<=Rect.Left) or (Rect.Bottom<=Rect.Top);
  502. end;
  503. function OffsetRect(var Rect : TRect;DX : Integer;DY : Integer) : Boolean;
  504. begin
  505. if assigned(@Rect) then
  506. begin
  507. with Rect do
  508. begin
  509. inc(Left,dx);
  510. inc(Top,dy);
  511. inc(Right,dx);
  512. inc(Bottom,dy);
  513. end;
  514. OffsetRect:=true;
  515. end
  516. else
  517. OffsetRect:=false;
  518. end;
  519. function Avg(a, b: Longint): Longint;
  520. begin
  521. if a < b then
  522. Result := a + ((b - a) shr 1)
  523. else
  524. Result := b + ((a - b) shr 1);
  525. end;
  526. function CenterPoint(const Rect: TRect): TPoint;
  527. begin
  528. with Rect do
  529. begin
  530. Result.X := Avg(Left, Right);
  531. Result.Y := Avg(Top, Bottom);
  532. end;
  533. end;
  534. function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;
  535. begin
  536. if Assigned(@Rect) then
  537. begin
  538. with Rect do
  539. begin
  540. dec(Left, dx);
  541. dec(Top, dy);
  542. inc(Right, dx);
  543. inc(Bottom, dy);
  544. end;
  545. Result := True;
  546. end
  547. else
  548. Result := False;
  549. end;
  550. function Size(AWidth, AHeight: Integer): TSize; inline;
  551. begin
  552. Result.cx := AWidth;
  553. Result.cy := AHeight;
  554. end;
  555. function Size(const ARect: TRect): TSize; inline;
  556. begin
  557. Result.cx := ARect.Right - ARect.Left;
  558. Result.cy := ARect.Bottom - ARect.Top;
  559. end;
  560. { TPointF}
  561. function TPointF.Add(const apt: TPoint): TPointF;
  562. begin
  563. result.x:=x+apt.x;
  564. result.y:=y+apt.y;
  565. end;
  566. function TPointF.Add(const apt: TPointF): TPointF;
  567. begin
  568. result.x:=x+apt.x;
  569. result.y:=y+apt.y;
  570. end;
  571. function TPointF.Subtract(const apt : TPointF): TPointF;
  572. begin
  573. result.x:=x-apt.x;
  574. result.y:=y-apt.y;
  575. end;
  576. function TPointF.Subtract(const apt: TPoint): TPointF;
  577. begin
  578. result.x:=x-apt.x;
  579. result.y:=y-apt.y;
  580. end;
  581. function TPointF.Distance(const apt : TPointF) : Single;
  582. begin
  583. result:=sqrt(sqr(apt.x-x)+sqr(apt.y-y));
  584. end;
  585. function TPointF.DotProduct(const apt: TPointF): Single;
  586. begin
  587. result:=x*apt.x+y*apt.y;
  588. end;
  589. function TPointF.IsZero : Boolean;
  590. begin
  591. result:=SameValue(x,0.0) and SameValue(y,0.0);
  592. end;
  593. procedure TPointF.Offset(const apt :TPointF);
  594. begin
  595. x:=x+apt.x;
  596. y:=y+apt.y;
  597. end;
  598. procedure TPointF.Offset(const apt: TPoint);
  599. begin
  600. x:=x+apt.x;
  601. y:=y+apt.y;
  602. end;
  603. procedure TPointF.Offset(dx,dy : Longint);
  604. begin
  605. x:=x+dx;
  606. y:=y+dy;
  607. end;
  608. function TPointF.Scale(afactor: Single): TPointF;
  609. begin
  610. result.x:=afactor*x;
  611. result.y:=afactor*y;
  612. end;
  613. function TPointF.Ceiling: TPoint;
  614. begin
  615. result.x:=ceil(x);
  616. result.y:=ceil(y);
  617. end;
  618. function TPointF.Truncate: TPoint;
  619. begin
  620. result.x:=trunc(x);
  621. result.y:=trunc(y);
  622. end;
  623. function TPointF.Floor: TPoint;
  624. begin
  625. result.x:=Math.floor(x);
  626. result.y:=Math.floor(y);
  627. end;
  628. function TPointF.Round: TPoint;
  629. begin
  630. result.x:=System.round(x);
  631. result.y:=System.round(y);
  632. end;
  633. function TPointF.Length: Single;
  634. begin //distance(self) ?
  635. result:=sqrt(sqr(x)+sqr(y));
  636. end;
  637. class operator TPointF.= (const apt1, apt2 : TPointF) : Boolean;
  638. begin
  639. result:=SameValue(apt1.x,apt2.x) and SameValue(apt1.y,apt2.y);
  640. end;
  641. class operator TPointF.<> (const apt1, apt2 : TPointF): Boolean;
  642. begin
  643. result:=NOT (SameValue(apt1.x,apt2.x) and Samevalue(apt1.y,apt2.y));
  644. end;
  645. class operator TPointF. * (const apt1, apt2: TPointF): Single;
  646. begin
  647. result:=apt1.x*apt2.x + apt1.y*apt2.y;
  648. end;
  649. class operator TPointF. * (afactor: single; const apt1: TPointF): TPointF;
  650. begin
  651. result:=apt1.Scale(afactor);
  652. end;
  653. class operator TPointF. * (const apt1: TPointF; afactor: single): TPointF;
  654. begin
  655. result:=apt1.Scale(afactor);
  656. end;
  657. class operator TPointF.+ (const apt1, apt2 : TPointF): TPointF;
  658. begin
  659. result.x:=apt1.x+apt2.x;
  660. result.y:=apt1.y+apt2.y;
  661. end;
  662. class operator TPointF.- (const apt1, apt2 : TPointF): TPointF;
  663. begin
  664. result.x:=apt1.x-apt2.x;
  665. result.y:=apt1.y-apt2.y;
  666. end;
  667. class operator TPointF. - (const apt1: TPointF): TPointF;
  668. begin
  669. Result.x:=-apt1.x;
  670. Result.y:=-apt1.y;
  671. end;
  672. class operator TPointF. := (const apt: TPoint): TPointF;
  673. begin
  674. Result.x:=apt.x;
  675. Result.y:=apt.y;
  676. end;
  677. procedure TPointF.SetLocation(const apt :TPointF);
  678. begin
  679. x:=apt.x; y:=apt.y;
  680. end;
  681. procedure TPointF.SetLocation(const apt: TPoint);
  682. begin
  683. x:=apt.x; y:=apt.y;
  684. end;
  685. procedure TPointF.SetLocation(ax,ay : Longint);
  686. begin
  687. x:=ax; y:=ay;
  688. end;
  689. class function TPointF.Create(const ax, ay: Single): TPointF;
  690. begin
  691. Result.x := ax;
  692. Result.y := ay;
  693. end;
  694. class function TPointF.Create(const apt: TPoint): TPointF;
  695. begin
  696. Result.x := apt.X;
  697. Result.y := apt.Y;
  698. end;
  699. { TSizeF }
  700. function TSizeF.Add(const asz: TSize): TSizeF;
  701. begin
  702. result.cx:=cx+asz.cx;
  703. result.cy:=cy+asz.cy;
  704. end;
  705. function TSizeF.Add(const asz: TSizeF): TSizeF;
  706. begin
  707. result.cx:=cx+asz.cx;
  708. result.cy:=cy+asz.cy;
  709. end;
  710. function TSizeF.Subtract(const asz : TSizeF): TSizeF;
  711. begin
  712. result.cx:=cx-asz.cx;
  713. result.cy:=cy-asz.cy;
  714. end;
  715. function TSizeF.Subtract(const asz: TSize): TSizeF;
  716. begin
  717. result.cx:=cx-asz.cx;
  718. result.cy:=cy-asz.cy;
  719. end;
  720. function TSizeF.Distance(const asz : TSizeF) : Single;
  721. begin
  722. result:=sqrt(sqr(asz.cx-cx)+sqr(asz.cy-cy));
  723. end;
  724. function TSizeF.IsZero : Boolean;
  725. begin
  726. result:=SameValue(cx,0.0) and SameValue(cy,0.0);
  727. end;
  728. function TSizeF.Scale(afactor: Single): TSizeF;
  729. begin
  730. result.cx:=afactor*cx;
  731. result.cy:=afactor*cy;
  732. end;
  733. function TSizeF.Ceiling: TSize;
  734. begin
  735. result.cx:=ceil(cx);
  736. result.cy:=ceil(cy);
  737. end;
  738. function TSizeF.Truncate: TSize;
  739. begin
  740. result.cx:=trunc(cx);
  741. result.cy:=trunc(cy);
  742. end;
  743. function TSizeF.Floor: TSize;
  744. begin
  745. result.cx:=Math.floor(cx);
  746. result.cy:=Math.floor(cy);
  747. end;
  748. function TSizeF.Round: TSize;
  749. begin
  750. result.cx:=System.round(cx);
  751. result.cy:=System.round(cy);
  752. end;
  753. function TSizeF.Length: Single;
  754. begin //distance(self) ?
  755. result:=sqrt(sqr(cx)+sqr(cy));
  756. end;
  757. class operator TSizeF.= (const asz1, asz2 : TSizeF) : Boolean;
  758. begin
  759. result:=SameValue(asz1.cx,asz2.cx) and SameValue(asz1.cy,asz2.cy);
  760. end;
  761. class operator TSizeF.<> (const asz1, asz2 : TSizeF): Boolean;
  762. begin
  763. result:=NOT (SameValue(asz1.cx,asz2.cx) and Samevalue(asz1.cy,asz2.cy));
  764. end;
  765. class operator TSizeF. * (afactor: single; const asz1: TSizeF): TSizeF;
  766. begin
  767. result:=asz1.Scale(afactor);
  768. end;
  769. class operator TSizeF. * (const asz1: TSizeF; afactor: single): TSizeF;
  770. begin
  771. result:=asz1.Scale(afactor);
  772. end;
  773. class operator TSizeF.+ (const asz1, asz2 : TSizeF): TSizeF;
  774. begin
  775. result.cx:=asz1.cx+asz2.cx;
  776. result.cy:=asz1.cy+asz2.cy;
  777. end;
  778. class operator TSizeF.- (const asz1, asz2 : TSizeF): TSizeF;
  779. begin
  780. result.cx:=asz1.cx-asz2.cx;
  781. result.cy:=asz1.cy-asz2.cy;
  782. end;
  783. class operator TSizeF. - (const asz1: TSizeF): TSizeF;
  784. begin
  785. Result.cx:=-asz1.cx;
  786. Result.cy:=-asz1.cy;
  787. end;
  788. class operator TSizeF. := (const apt: TPointF): TSizeF;
  789. begin
  790. Result.cx:=apt.x;
  791. Result.cy:=apt.y;
  792. end;
  793. class operator TSizeF. := (const asz: TSize): TSizeF;
  794. begin
  795. Result.cx := asz.cx;
  796. Result.cy := asz.cy;
  797. end;
  798. class operator TSizeF. := (const asz: TSizeF): TPointF;
  799. begin
  800. Result.x := asz.cx;
  801. Result.y := asz.cy;
  802. end;
  803. class function TSizeF.Create(const ax, ay: Single): TSizeF;
  804. begin
  805. Result.cx := ax;
  806. Result.cy := ay;
  807. end;
  808. class function TSizeF.Create(const asz: TSize): TSizeF;
  809. begin
  810. Result.cx := asz.cX;
  811. Result.cy := asz.cY;
  812. end;
  813. { TRectF }
  814. class operator TRectF. * (L, R: TRectF): TRectF;
  815. begin
  816. Result := TRectF.Intersect(L, R);
  817. end;
  818. class operator TRectF. + (L, R: TRectF): TRectF;
  819. begin
  820. Result := TRectF.Union(L, R);
  821. end;
  822. class operator TRectF. := (const arc: TRect): TRectF;
  823. begin
  824. Result.Left:=arc.Left;
  825. Result.Top:=arc.Top;
  826. Result.Right:=arc.Right;
  827. Result.Bottom:=arc.Bottom;
  828. end;
  829. class operator TRectF. <> (L, R: TRectF): Boolean;
  830. begin
  831. Result := not(L=R);
  832. end;
  833. class operator TRectF. = (L, R: TRectF): Boolean;
  834. begin
  835. Result :=
  836. SameValue(L.Left,R.Left) and SameValue(L.Right,R.Right) and
  837. SameValue(L.Top,R.Top) and SameValue(L.Bottom,R.Bottom);
  838. end;
  839. constructor TRectF.Create(ALeft, ATop, ARight, ABottom: Single);
  840. begin
  841. Left := ALeft;
  842. Top := ATop;
  843. Right := ARight;
  844. Bottom := ABottom;
  845. end;
  846. constructor TRectF.Create(P1, P2: TPointF; Normalize: Boolean);
  847. begin
  848. TopLeft := P1;
  849. BottomRight := P2;
  850. if Normalize then
  851. NormalizeRect;
  852. end;
  853. constructor TRectF.Create(Origin: TPointF);
  854. begin
  855. TopLeft := Origin;
  856. BottomRight := Origin;
  857. end;
  858. constructor TRectF.Create(Origin: TPointF; AWidth, AHeight: Single);
  859. begin
  860. TopLeft := Origin;
  861. Width := AWidth;
  862. Height := AHeight;
  863. end;
  864. constructor TRectF.Create(R: TRectF; Normalize: Boolean);
  865. begin
  866. Self := R;
  867. if Normalize then
  868. NormalizeRect;
  869. end;
  870. constructor TRectF.Create(R: TRect; Normalize: Boolean);
  871. begin
  872. Self := R;
  873. if Normalize then
  874. NormalizeRect;
  875. end;
  876. function TRectF.CenterPoint: TPointF;
  877. begin
  878. Result.X := (Right-Left) / 2 + Left;
  879. Result.Y := (Bottom-Top) / 2 + Top;
  880. end;
  881. function TRectF.Contains(Pt: TPointF): Boolean;
  882. begin
  883. Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);
  884. end;
  885. function TRectF.Contains(R: TRectF): Boolean;
  886. begin
  887. Result := (Left <= R.Left) and (R.Right <= Right) and (Top <= R.Top) and (R.Bottom <= Bottom);
  888. end;
  889. class function TRectF.Empty: TRectF;
  890. begin
  891. Result := TRectF.Create(0,0,0,0);
  892. end;
  893. function TRectF.GetHeight: Single;
  894. begin
  895. result:=bottom-top;
  896. end;
  897. function TRectF.GetLocation: TPointF;
  898. begin
  899. result.x:=Left; result.y:=top;
  900. end;
  901. function TRectF.GetSize: TSizeF;
  902. begin
  903. result.cx:=width; result.cy:=height;
  904. end;
  905. function TRectF.GetWidth: Single;
  906. begin
  907. result:=right-left;
  908. end;
  909. procedure TRectF.Inflate(DX, DY: Single);
  910. begin
  911. Left:=Left-dx;
  912. Top:=Top-dy;
  913. Right:=Right+dx;
  914. Bottom:=Bottom+dy;
  915. end;
  916. procedure TRectF.Intersect(R: TRectF);
  917. begin
  918. Self := Intersect(Self, R);
  919. end;
  920. class function TRectF.Intersect(R1: TRectF; R2: TRectF): TRectF;
  921. begin
  922. Result := R1;
  923. if R2.Left > R1.Left then
  924. Result.Left := R2.Left;
  925. if R2.Top > R1.Top then
  926. Result.Top := R2.Top;
  927. if R2.Right < R1.Right then
  928. Result.Right := R2.Right;
  929. if R2.Bottom < R1.Bottom then
  930. Result.Bottom := R2.Bottom;
  931. end;
  932. function TRectF.IntersectsWith(R: TRectF): Boolean;
  933. begin
  934. Result := (Left < R.Right) and (R.Left < Right) and (Top < R.Bottom) and (R.Top < Bottom);
  935. end;
  936. function TRectF.IsEmpty: Boolean;
  937. begin
  938. Result := (CompareValue(Right,Left)<=0) or (CompareValue(Bottom,Top)<=0);
  939. end;
  940. procedure TRectF.NormalizeRect;
  941. var
  942. x: Single;
  943. begin
  944. if Top>Bottom then
  945. begin
  946. x := Top;
  947. Top := Bottom;
  948. Bottom := x;
  949. end;
  950. if Left>Right then
  951. begin
  952. x := Left;
  953. Left := Right;
  954. Right := x;
  955. end
  956. end;
  957. procedure TRectF.Inflate(DL, DT, DR, DB: Single);
  958. begin
  959. Left:=Left-dl;
  960. Top:=Top-dt;
  961. Right:=Right+dr;
  962. Bottom:=Bottom+db;
  963. end;
  964. procedure TRectF.Offset(const dx, dy: Single);
  965. begin
  966. left:=left+dx; right:=right+dx;
  967. bottom:=bottom+dy; top:=top+dy;
  968. end;
  969. procedure TRectF.Offset(DP: TPointF);
  970. begin
  971. left:=left+DP.x; right:=right+DP.x;
  972. bottom:=bottom+DP.y; top:=top+DP.y;
  973. end;
  974. procedure TRectF.SetHeight(AValue: Single);
  975. begin
  976. bottom:=top+avalue;
  977. end;
  978. procedure TRectF.SetLocation(X, Y: Single);
  979. begin
  980. Offset(X-Left, Y-Top);
  981. end;
  982. procedure TRectF.SetLocation(P: TPointF);
  983. begin
  984. SetLocation(P.X, P.Y);
  985. end;
  986. procedure TRectF.SetSize(AValue: TSizeF);
  987. begin
  988. bottom:=top+avalue.cy;
  989. right:=left+avalue.cx;
  990. end;
  991. procedure TRectF.SetWidth(AValue: Single);
  992. begin
  993. right:=left+avalue;
  994. end;
  995. class function TRectF.Union(const Points: array of TPointF): TRectF;
  996. var
  997. i: Integer;
  998. begin
  999. if Length(Points) > 0 then
  1000. begin
  1001. Result.TopLeft := Points[Low(Points)];
  1002. Result.BottomRight := Points[Low(Points)];
  1003. for i := Low(Points)+1 to High(Points) do
  1004. begin
  1005. if Points[i].X < Result.Left then Result.Left := Points[i].X;
  1006. if Points[i].X > Result.Right then Result.Right := Points[i].X;
  1007. if Points[i].Y < Result.Top then Result.Top := Points[i].Y;
  1008. if Points[i].Y > Result.Bottom then Result.Bottom := Points[i].Y;
  1009. end;
  1010. end else
  1011. Result := Empty;
  1012. end;
  1013. procedure TRectF.Union(const r: TRectF);
  1014. begin
  1015. left:=min(r.left,left);
  1016. top:=min(r.top,top);
  1017. right:=max(r.right,right);
  1018. bottom:=max(r.bottom,bottom);
  1019. end;
  1020. class function TRectF.Union(R1, R2: TRectF): TRectF;
  1021. begin
  1022. Result:=R1;
  1023. Result.Union(R2);
  1024. end;
  1025. { TPoint3D }
  1026. constructor TPoint3D.Create(const ax,ay,az:single);
  1027. begin
  1028. x:=ax; y:=ay; z:=az;
  1029. end;
  1030. procedure TPoint3D.Offset(const adeltax,adeltay,adeltaz:single);
  1031. begin
  1032. x:=x+adeltax; y:=y+adeltay; z:=z+adeltaz;
  1033. end;
  1034. procedure TPoint3D.Offset(const adelta:TPoint3D);
  1035. begin
  1036. x:=x+adelta.x; y:=y+adelta.y; z:=z+adelta.z;
  1037. end;
  1038. {$ifndef VER3_0}
  1039. generic class procedure TBitConverter.UnsafeFrom<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0);
  1040. begin
  1041. move(ASrcValue, ADestination[AOffset], SizeOf(T));
  1042. end;
  1043. generic class procedure TBitConverter.From<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0);
  1044. begin
  1045. if AOffset < 0 then
  1046. System.Error(reRangeError);
  1047. if IsManagedType(T) then
  1048. System.Error(reInvalidCast);
  1049. if Length(ADestination) < (SizeOf(T) + AOffset) then
  1050. System.Error(reRangeError);
  1051. TBitConverter.specialize UnsafeFrom<T>(ASrcValue, ADestination, AOffset);
  1052. end;
  1053. generic class function TBitConverter.UnsafeInTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T;
  1054. begin
  1055. move(ASource[AOffset], Result, SizeOf(T));
  1056. end;
  1057. generic class function TBitConverter.InTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T;
  1058. begin
  1059. if AOffset < 0 then
  1060. System.Error(reRangeError);
  1061. if IsManagedType(T) then
  1062. System.Error(reInvalidCast);
  1063. if Length(ASource) < (SizeOf(T) + AOffset) then
  1064. System.Error(reRangeError);
  1065. Result := TBitConverter.specialize UnsafeInTo<T>(ASource, AOffset);
  1066. end;
  1067. {$endif}
  1068. end.