types.pp 34 KB

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