types.pp 36 KB

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