types.pp 38 KB

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