types.pp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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 UnionRect(var Rect : TRect; const R1,R2 : TRect) : Boolean;
  424. function IsRectEmpty(const Rect : TRect) : Boolean;
  425. function OffsetRect(var Rect : TRect;DX : Integer;DY : Integer) : Boolean;
  426. function CenterPoint(const Rect: TRect): TPoint;
  427. function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;
  428. function Size(AWidth, AHeight: Integer): TSize; inline;
  429. function Size(const ARect: TRect): TSize;
  430. {$ifndef VER3_0}
  431. type
  432. TBitConverter = class
  433. generic class procedure UnsafeFrom<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0); static; {inline;}
  434. generic class procedure From<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0); static;
  435. generic class function UnsafeInTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T; static; {inline;}
  436. generic class function InTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T; static;
  437. end;
  438. {$endif}
  439. implementation
  440. Uses Math;
  441. {$if (not defined(win32)) and (not defined(win64)) and (not defined(wince))}
  442. {$i typshrd.inc}
  443. {$endif}
  444. function EqualRect(const r1,r2 : TRect) : Boolean;
  445. begin
  446. EqualRect:=(r1.left=r2.left) and (r1.right=r2.right) and (r1.top=r2.top) and (r1.bottom=r2.bottom);
  447. end;
  448. function Rect(Left,Top,Right,Bottom : Integer) : TRect; inline;
  449. begin
  450. Rect.Left:=Left;
  451. Rect.Top:=Top;
  452. Rect.Right:=Right;
  453. Rect.Bottom:=Bottom;
  454. end;
  455. function RectF(Left,Top,Right,Bottom : Single) : TRectF; inline;
  456. begin
  457. RectF.Left:=Left;
  458. RectF.Top:=Top;
  459. RectF.Right:=Right;
  460. RectF.Bottom:=Bottom;
  461. end;
  462. function Bounds(ALeft,ATop,AWidth,AHeight : Integer) : TRect; inline;
  463. begin
  464. Bounds.Left:=ALeft;
  465. Bounds.Top:=ATop;
  466. Bounds.Right:=ALeft+AWidth;
  467. Bounds.Bottom:=ATop+AHeight;
  468. end;
  469. function Point(x,y : Integer) : TPoint; inline;
  470. begin
  471. Point.x:=x;
  472. Point.y:=y;
  473. end;
  474. function PointF(x,y: Single) : TPointF; inline;
  475. begin
  476. PointF.x:=x;
  477. PointF.y:=y;
  478. end;
  479. function PtInRect(const Rect : TRect;const p : TPoint) : Boolean;
  480. begin
  481. PtInRect:=(p.y>=Rect.Top) and
  482. (p.y<Rect.Bottom) and
  483. (p.x>=Rect.Left) and
  484. (p.x<Rect.Right);
  485. end;
  486. function IntersectRect(var Rect : TRect;const R1,R2 : TRect) : Boolean;
  487. var
  488. lRect: TRect;
  489. begin
  490. lRect := R1;
  491. if R2.Left > R1.Left then
  492. lRect.Left := R2.Left;
  493. if R2.Top > R1.Top then
  494. lRect.Top := R2.Top;
  495. if R2.Right < R1.Right then
  496. lRect.Right := R2.Right;
  497. if R2.Bottom < R1.Bottom then
  498. lRect.Bottom := R2.Bottom;
  499. // The var parameter is only assigned in the end to avoid problems
  500. // when passing the same rectangle in the var and const parameters.
  501. // See http://bugs.freepascal.org/view.php?id=17722
  502. Result:=not IsRectEmpty(lRect);
  503. if Result then
  504. Rect := lRect
  505. else
  506. FillChar(Rect,SizeOf(Rect),0);
  507. end;
  508. function UnionRect(var Rect : TRect;const R1,R2 : TRect) : Boolean;
  509. var
  510. lRect: TRect;
  511. begin
  512. lRect:=R1;
  513. if R2.Left<R1.Left then
  514. lRect.Left:=R2.Left;
  515. if R2.Top<R1.Top then
  516. lRect.Top:=R2.Top;
  517. if R2.Right>R1.Right then
  518. lRect.Right:=R2.Right;
  519. if R2.Bottom>R1.Bottom then
  520. lRect.Bottom:=R2.Bottom;
  521. Result:=not IsRectEmpty(lRect);
  522. if Result then
  523. Rect := lRect
  524. else
  525. FillChar(Rect,SizeOf(Rect),0);
  526. end;
  527. function IsRectEmpty(const Rect : TRect) : Boolean;
  528. begin
  529. IsRectEmpty:=(Rect.Right<=Rect.Left) or (Rect.Bottom<=Rect.Top);
  530. end;
  531. function OffsetRect(var Rect : TRect;DX : Integer;DY : Integer) : Boolean;
  532. begin
  533. Result:=assigned(@Rect);
  534. if Result then
  535. with Rect do
  536. begin
  537. inc(Left,dx);
  538. inc(Top,dy);
  539. inc(Right,dx);
  540. inc(Bottom,dy);
  541. end;
  542. end;
  543. function Avg(a, b: Longint): Longint;
  544. begin
  545. if a < b then
  546. Result := a + ((b - a) shr 1)
  547. else
  548. Result := b + ((a - b) shr 1);
  549. end;
  550. function CenterPoint(const Rect: TRect): TPoint;
  551. begin
  552. with Rect do
  553. begin
  554. Result.X := Avg(Left, Right);
  555. Result.Y := Avg(Top, Bottom);
  556. end;
  557. end;
  558. function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;
  559. begin
  560. Result:=assigned(@Rect);
  561. if Result then
  562. with Rect do
  563. begin
  564. dec(Left, dx);
  565. dec(Top, dy);
  566. inc(Right, dx);
  567. inc(Bottom, dy);
  568. end;
  569. end;
  570. function Size(AWidth, AHeight: Integer): TSize; inline;
  571. begin
  572. Result.cx := AWidth;
  573. Result.cy := AHeight;
  574. end;
  575. function Size(const ARect: TRect): TSize; inline;
  576. begin
  577. Result.cx := ARect.Right - ARect.Left;
  578. Result.cy := ARect.Bottom - ARect.Top;
  579. end;
  580. { TPointF}
  581. function TPointF.Add(const apt: TPoint): TPointF;
  582. begin
  583. result.x:=x+apt.x;
  584. result.y:=y+apt.y;
  585. end;
  586. function TPointF.Add(const apt: TPointF): TPointF;
  587. begin
  588. result.x:=x+apt.x;
  589. result.y:=y+apt.y;
  590. end;
  591. function TPointF.Subtract(const apt : TPointF): TPointF;
  592. begin
  593. result.x:=x-apt.x;
  594. result.y:=y-apt.y;
  595. end;
  596. function TPointF.Subtract(const apt: TPoint): TPointF;
  597. begin
  598. result.x:=x-apt.x;
  599. result.y:=y-apt.y;
  600. end;
  601. function TPointF.Distance(const apt : TPointF) : Single;
  602. begin
  603. result:=sqrt(sqr(apt.x-x)+sqr(apt.y-y));
  604. end;
  605. function TPointF.DotProduct(const apt: TPointF): Single;
  606. begin
  607. result:=x*apt.x+y*apt.y;
  608. end;
  609. function TPointF.IsZero : Boolean;
  610. begin
  611. result:=SameValue(x,0.0) and SameValue(y,0.0);
  612. end;
  613. procedure TPointF.Offset(const apt :TPointF);
  614. begin
  615. x:=x+apt.x;
  616. y:=y+apt.y;
  617. end;
  618. procedure TPointF.Offset(const apt: TPoint);
  619. begin
  620. x:=x+apt.x;
  621. y:=y+apt.y;
  622. end;
  623. procedure TPointF.Offset(dx,dy : Single);
  624. begin
  625. x:=x+dx;
  626. y:=y+dy;
  627. end;
  628. function TPointF.EqualsTo(const apt: TPointF; const aEpsilon: Single): Boolean;
  629. function Eq(a,b : single) : boolean; inline;
  630. begin
  631. result:=abs(a-b)<=aEpsilon;
  632. end;
  633. begin
  634. Result:=Eq(X,apt.X) and Eq(Y,apt.Y);
  635. end;
  636. function TPointF.Scale(afactor: Single): TPointF;
  637. begin
  638. result.x:=afactor*x;
  639. result.y:=afactor*y;
  640. end;
  641. function TPointF.Ceiling: TPoint;
  642. begin
  643. result.x:=ceil(x);
  644. result.y:=ceil(y);
  645. end;
  646. function TPointF.Truncate: TPoint;
  647. begin
  648. result.x:=trunc(x);
  649. result.y:=trunc(y);
  650. end;
  651. function TPointF.Floor: TPoint;
  652. begin
  653. result.x:=Math.floor(x);
  654. result.y:=Math.floor(y);
  655. end;
  656. function TPointF.Round: TPoint;
  657. begin
  658. result.x:=System.round(x);
  659. result.y:=System.round(y);
  660. end;
  661. function TPointF.Length: Single;
  662. begin
  663. result:=sqrt(sqr(x)+sqr(y));
  664. end;
  665. function TPointF.Rotate(angle: single): TPointF;
  666. var
  667. sina, cosa: single;
  668. begin
  669. sincos(angle, sina, cosa);
  670. result.x := x * cosa - y * sina;
  671. result.y := x * sina + y * cosa;
  672. end;
  673. function TPointF.Reflect(const normal: TPointF): TPointF;
  674. begin
  675. result := self + (-2 * normal ** self) * normal;
  676. end;
  677. function TPointF.MidPoint(const b: TPointF): TPointF;
  678. begin
  679. result.x := 0.5 * (x + b.x);
  680. result.y := 0.5 * (y + b.y);
  681. end;
  682. class function TPointF.Zero: TPointF;
  683. begin
  684. Result.X:=0;
  685. Result.Y:=0;
  686. end;
  687. class function TPointF.PointInCircle(const pt, center: TPointF; radius: single): Boolean;
  688. begin
  689. result := sqr(center.x - pt.x) + sqr(center.y - pt.y) < sqr(radius);
  690. end;
  691. class function TPointF.PointInCircle(const pt, center: TPointF; radius: integer): Boolean;
  692. begin
  693. result := sqr(center.x - pt.x) + sqr(center.y - pt.y) < sqr(single(radius));
  694. end;
  695. function TPointF.Angle(const b: TPointF): Single;
  696. begin
  697. result := ArcTan2(y - b.y, x - b.x);
  698. end;
  699. function TPointF.AngleCosine(const b: TPointF): single;
  700. begin
  701. result := EnsureRange((self ** b) / sqrt((sqr(x) + sqr(y)) * (sqr(b.x) + sqr(b.y))), -1, 1);
  702. end;
  703. class operator TPointF.= (const apt1, apt2 : TPointF) : Boolean;
  704. begin
  705. result:=SameValue(apt1.x,apt2.x) and SameValue(apt1.y,apt2.y);
  706. end;
  707. class operator TPointF.<> (const apt1, apt2 : TPointF): Boolean;
  708. begin
  709. result:=NOT (SameValue(apt1.x,apt2.x) and Samevalue(apt1.y,apt2.y));
  710. end;
  711. class operator TPointF. * (const apt1, apt2: TPointF): TPointF;
  712. begin
  713. result.x:=apt1.x*apt2.x;
  714. result.y:=apt1.y*apt2.y;
  715. end;
  716. class operator TPointF. * (afactor: single; const apt1: TPointF): TPointF;
  717. begin
  718. result:=apt1.Scale(afactor);
  719. end;
  720. class operator TPointF. * (const apt1: TPointF; afactor: single): TPointF;
  721. begin
  722. result:=apt1.Scale(afactor);
  723. end;
  724. class operator TPointF. ** (const apt1, apt2: TPointF): Single;
  725. begin
  726. result:=apt1.x*apt2.x + apt1.y*apt2.y;
  727. end;
  728. class operator TPointF.+ (const apt1, apt2 : TPointF): TPointF;
  729. begin
  730. result.x:=apt1.x+apt2.x;
  731. result.y:=apt1.y+apt2.y;
  732. end;
  733. class operator TPointF.- (const apt1, apt2 : TPointF): TPointF;
  734. begin
  735. result.x:=apt1.x-apt2.x;
  736. result.y:=apt1.y-apt2.y;
  737. end;
  738. class operator TPointF. - (const apt1: TPointF): TPointF;
  739. begin
  740. Result.x:=-apt1.x;
  741. Result.y:=-apt1.y;
  742. end;
  743. class operator TPointF. / (const apt1: TPointF; afactor: single): TPointF;
  744. begin
  745. result:=apt1.Scale(1/afactor);
  746. end;
  747. class operator TPointF. := (const apt: TPoint): TPointF;
  748. begin
  749. Result.x:=apt.x;
  750. Result.y:=apt.y;
  751. end;
  752. procedure TPointF.SetLocation(const apt :TPointF);
  753. begin
  754. x:=apt.x; y:=apt.y;
  755. end;
  756. procedure TPointF.SetLocation(const apt: TPoint);
  757. begin
  758. x:=apt.x; y:=apt.y;
  759. end;
  760. procedure TPointF.SetLocation(ax,ay : Single);
  761. begin
  762. x:=ax; y:=ay;
  763. end;
  764. class function TPointF.Create(const ax, ay: Single): TPointF;
  765. begin
  766. Result.x := ax;
  767. Result.y := ay;
  768. end;
  769. class function TPointF.Create(const apt: TPoint): TPointF;
  770. begin
  771. Result.x := apt.X;
  772. Result.y := apt.Y;
  773. end;
  774. { TSizeF }
  775. function TSizeF.Add(const asz: TSize): TSizeF;
  776. begin
  777. result.cx:=cx+asz.cx;
  778. result.cy:=cy+asz.cy;
  779. end;
  780. function TSizeF.Add(const asz: TSizeF): TSizeF;
  781. begin
  782. result.cx:=cx+asz.cx;
  783. result.cy:=cy+asz.cy;
  784. end;
  785. function TSizeF.Subtract(const asz : TSizeF): TSizeF;
  786. begin
  787. result.cx:=cx-asz.cx;
  788. result.cy:=cy-asz.cy;
  789. end;
  790. function TSizeF.SwapDimensions:TSizeF;
  791. begin
  792. result.cx:=cy;
  793. result.cy:=cx;
  794. end;
  795. function TSizeF.Subtract(const asz: TSize): TSizeF;
  796. begin
  797. result.cx:=cx-asz.cx;
  798. result.cy:=cy-asz.cy;
  799. end;
  800. function TSizeF.Distance(const asz : TSizeF) : Single;
  801. begin
  802. result:=sqrt(sqr(asz.cx-cx)+sqr(asz.cy-cy));
  803. end;
  804. function TSizeF.IsZero : Boolean;
  805. begin
  806. result:=SameValue(cx,0.0) and SameValue(cy,0.0);
  807. end;
  808. function TSizeF.Scale(afactor: Single): TSizeF;
  809. begin
  810. result.cx:=afactor*cx;
  811. result.cy:=afactor*cy;
  812. end;
  813. function TSizeF.Ceiling: TSize;
  814. begin
  815. result.cx:=ceil(cx);
  816. result.cy:=ceil(cy);
  817. end;
  818. function TSizeF.Truncate: TSize;
  819. begin
  820. result.cx:=trunc(cx);
  821. result.cy:=trunc(cy);
  822. end;
  823. function TSizeF.Floor: TSize;
  824. begin
  825. result.cx:=Math.floor(cx);
  826. result.cy:=Math.floor(cy);
  827. end;
  828. function TSizeF.Round: TSize;
  829. begin
  830. result.cx:=System.round(cx);
  831. result.cy:=System.round(cy);
  832. end;
  833. function TSizeF.Length: Single;
  834. begin //distance(self) ?
  835. result:=sqrt(sqr(cx)+sqr(cy));
  836. end;
  837. class operator TSizeF.= (const asz1, asz2 : TSizeF) : Boolean;
  838. begin
  839. result:=SameValue(asz1.cx,asz2.cx) and SameValue(asz1.cy,asz2.cy);
  840. end;
  841. class operator TSizeF.<> (const asz1, asz2 : TSizeF): Boolean;
  842. begin
  843. result:=NOT (SameValue(asz1.cx,asz2.cx) and Samevalue(asz1.cy,asz2.cy));
  844. end;
  845. class operator TSizeF. * (afactor: single; const asz1: TSizeF): TSizeF;
  846. begin
  847. result:=asz1.Scale(afactor);
  848. end;
  849. class operator TSizeF. * (const asz1: TSizeF; afactor: single): TSizeF;
  850. begin
  851. result:=asz1.Scale(afactor);
  852. end;
  853. class operator TSizeF.+ (const asz1, asz2 : TSizeF): TSizeF;
  854. begin
  855. result.cx:=asz1.cx+asz2.cx;
  856. result.cy:=asz1.cy+asz2.cy;
  857. end;
  858. class operator TSizeF.- (const asz1, asz2 : TSizeF): TSizeF;
  859. begin
  860. result.cx:=asz1.cx-asz2.cx;
  861. result.cy:=asz1.cy-asz2.cy;
  862. end;
  863. class operator TSizeF. - (const asz1: TSizeF): TSizeF;
  864. begin
  865. Result.cx:=-asz1.cx;
  866. Result.cy:=-asz1.cy;
  867. end;
  868. class operator TSizeF. := (const apt: TPointF): TSizeF;
  869. begin
  870. Result.cx:=apt.x;
  871. Result.cy:=apt.y;
  872. end;
  873. class operator TSizeF. := (const asz: TSize): TSizeF;
  874. begin
  875. Result.cx := asz.cx;
  876. Result.cy := asz.cy;
  877. end;
  878. class operator TSizeF. := (const asz: TSizeF): TPointF;
  879. begin
  880. Result.x := asz.cx;
  881. Result.y := asz.cy;
  882. end;
  883. class function TSizeF.Create(const ax, ay: Single): TSizeF;
  884. begin
  885. Result.cx := ax;
  886. Result.cy := ay;
  887. end;
  888. class function TSizeF.Create(const asz: TSize): TSizeF;
  889. begin
  890. Result.cx := asz.cX;
  891. Result.cy := asz.cY;
  892. end;
  893. { TRectF }
  894. class operator TRectF. * (L, R: TRectF): TRectF;
  895. begin
  896. Result := TRectF.Intersect(L, R);
  897. end;
  898. class operator TRectF. + (L, R: TRectF): TRectF;
  899. begin
  900. Result := TRectF.Union(L, R);
  901. end;
  902. class operator TRectF. := (const arc: TRect): TRectF;
  903. begin
  904. Result.Left:=arc.Left;
  905. Result.Top:=arc.Top;
  906. Result.Right:=arc.Right;
  907. Result.Bottom:=arc.Bottom;
  908. end;
  909. class operator TRectF. <> (L, R: TRectF): Boolean;
  910. begin
  911. Result := not(L=R);
  912. end;
  913. class operator TRectF. = (L, R: TRectF): Boolean;
  914. begin
  915. Result :=
  916. SameValue(L.Left,R.Left) and SameValue(L.Right,R.Right) and
  917. SameValue(L.Top,R.Top) and SameValue(L.Bottom,R.Bottom);
  918. end;
  919. constructor TRectF.Create(ALeft, ATop, ARight, ABottom: Single);
  920. begin
  921. Left := ALeft;
  922. Top := ATop;
  923. Right := ARight;
  924. Bottom := ABottom;
  925. end;
  926. constructor TRectF.Create(P1, P2: TPointF; Normalize: Boolean);
  927. begin
  928. TopLeft := P1;
  929. BottomRight := P2;
  930. if Normalize then
  931. NormalizeRect;
  932. end;
  933. constructor TRectF.Create(Origin: TPointF);
  934. begin
  935. TopLeft := Origin;
  936. BottomRight := Origin;
  937. end;
  938. constructor TRectF.Create(Origin: TPointF; AWidth, AHeight: Single);
  939. begin
  940. TopLeft := Origin;
  941. Width := AWidth;
  942. Height := AHeight;
  943. end;
  944. constructor TRectF.Create(R: TRectF; Normalize: Boolean);
  945. begin
  946. Self := R;
  947. if Normalize then
  948. NormalizeRect;
  949. end;
  950. constructor TRectF.Create(R: TRect; Normalize: Boolean);
  951. begin
  952. Self := R;
  953. if Normalize then
  954. NormalizeRect;
  955. end;
  956. function TRectF.CenterPoint: TPointF;
  957. begin
  958. Result.X := (Right-Left) / 2 + Left;
  959. Result.Y := (Bottom-Top) / 2 + Top;
  960. end;
  961. function TRectF.Ceiling: TRectF;
  962. begin
  963. Result.BottomRight:=BottomRight.Ceiling;
  964. Result.TopLeft:=TopLeft.Ceiling;
  965. end;
  966. function TRectF.CenterAt(const Dest: TRectF): TRectF;
  967. begin
  968. end;
  969. function TRectF.Fit(const Dest: TRectF): Single;
  970. var
  971. R : TRectF;
  972. begin
  973. R:=FitInto(Dest,Result);
  974. Self:=R;
  975. end;
  976. function TRectF.FitInto(const Dest: TRectF; out Ratio: Single): TRectF;
  977. begin
  978. if (Dest.Width<=0) or (Dest.Height<=0) then
  979. begin
  980. Ratio:=1.0;
  981. exit(Self);
  982. end;
  983. Ratio:=Max(Self.Width / Dest.Width, Self.Height / Dest.Height);
  984. if Ratio=0 then
  985. exit(Self);
  986. Result.Width:=Self.Width / Ratio;
  987. Result.Height:=Self.Height / Ratio;
  988. Result.Left:=Self.Left + (Self.Width - Result.Width) / 2;
  989. Result.Top:=Self.Top + (Self.Height - Result.Height) / 2;
  990. end;
  991. function TRectF.FitInto(const Dest: TRectF): TRectF;
  992. var
  993. Ratio: Single;
  994. begin
  995. Result:=FitInto(Dest,Ratio);
  996. end;
  997. function TRectF.PlaceInto(const Dest: TRectF; const AHorzAlign: THorzRectAlign = THorzRectAlign.Center; const AVertAlign: TVertRectAlign = TVertRectAlign.Center): TRectF;
  998. var
  999. R : TRectF;
  1000. X,Y : Single;
  1001. D : TRectF absolute dest;
  1002. begin
  1003. if (Height>Dest.Height) or (Width>Dest.Width) then
  1004. R:=FitInto(Dest)
  1005. else
  1006. R:=Self;
  1007. case AHorzAlign of
  1008. THorzRectAlign.Left:
  1009. X:=D.Left;
  1010. THorzRectAlign.Center:
  1011. X:=(D.Left+D.Right-R.Width)/2;
  1012. THorzRectAlign.Right:
  1013. X:=D.Right-R.Width;
  1014. end;
  1015. case AVertAlign of
  1016. TVertRectAlign.Top:
  1017. Y:=D.Top;
  1018. TVertRectAlign.Center:
  1019. Y:=(D.Top+D.Bottom-R.Height)/2;
  1020. TVertRectAlign.Bottom:
  1021. Y:=D.Bottom-R.Height;
  1022. end;
  1023. R.SetLocation(PointF(X,Y));
  1024. Result:=R;
  1025. end;
  1026. function TRectF.SnapToPixel(AScale: Single; APlaceBetweenPixels: Boolean): TRectF;
  1027. function sc (S : single) : single; inline;
  1028. begin
  1029. Result:=System.Trunc(S*AScale)/AScale;
  1030. end;
  1031. var
  1032. R : TRectF;
  1033. Off: Single;
  1034. begin
  1035. if AScale<=0 then
  1036. AScale := 1;
  1037. R.Top:=Sc(Top);
  1038. R.Left:=Sc(Left);
  1039. R.Width:=Sc(Width);
  1040. R.Height:=Sc(Height);
  1041. if APlaceBetweenPixels then
  1042. begin
  1043. Off:=1/(2*aScale);
  1044. R.Offset(Off,Off);
  1045. end;
  1046. Result:=R;
  1047. end;
  1048. function TRectF.Contains(Pt: TPointF): Boolean;
  1049. begin
  1050. Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);
  1051. end;
  1052. function TRectF.Contains(R: TRectF): Boolean;
  1053. begin
  1054. Result := (Left <= R.Left) and (R.Right <= Right) and (Top <= R.Top) and (R.Bottom <= Bottom);
  1055. end;
  1056. class function TRectF.Empty: TRectF;
  1057. begin
  1058. Result := TRectF.Create(0,0,0,0);
  1059. end;
  1060. function TRectF.EqualsTo(const R: TRectF; const Epsilon: Single): Boolean;
  1061. begin
  1062. Result:=TopLeft.EqualsTo(R.TopLeft,Epsilon);
  1063. Result:=Result and BottomRight.EqualsTo(R.BottomRight,Epsilon);
  1064. end;
  1065. function TRectF.GetHeight: Single;
  1066. begin
  1067. result:=bottom-top;
  1068. end;
  1069. function TRectF.GetLocation: TPointF;
  1070. begin
  1071. result.x:=Left; result.y:=top;
  1072. end;
  1073. function TRectF.GetSize: TSizeF;
  1074. begin
  1075. result.cx:=width; result.cy:=height;
  1076. end;
  1077. function TRectF.GetWidth: Single;
  1078. begin
  1079. result:=right-left;
  1080. end;
  1081. procedure TRectF.Inflate(DX, DY: Single);
  1082. begin
  1083. Left:=Left-dx;
  1084. Top:=Top-dy;
  1085. Right:=Right+dx;
  1086. Bottom:=Bottom+dy;
  1087. end;
  1088. procedure TRectF.Intersect(R: TRectF);
  1089. begin
  1090. Self := Intersect(Self, R);
  1091. end;
  1092. class function TRectF.Intersect(R1: TRectF; R2: TRectF): TRectF;
  1093. begin
  1094. Result := R1;
  1095. if R2.Left > R1.Left then
  1096. Result.Left := R2.Left;
  1097. if R2.Top > R1.Top then
  1098. Result.Top := R2.Top;
  1099. if R2.Right < R1.Right then
  1100. Result.Right := R2.Right;
  1101. if R2.Bottom < R1.Bottom then
  1102. Result.Bottom := R2.Bottom;
  1103. end;
  1104. function TRectF.IntersectsWith(R: TRectF): Boolean;
  1105. begin
  1106. Result := (Left < R.Right) and (R.Left < Right) and (Top < R.Bottom) and (R.Top < Bottom);
  1107. end;
  1108. function TRectF.IsEmpty: Boolean;
  1109. begin
  1110. Result := (CompareValue(Right,Left)<=0) or (CompareValue(Bottom,Top)<=0);
  1111. end;
  1112. procedure TRectF.NormalizeRect;
  1113. var
  1114. x: Single;
  1115. begin
  1116. if Top>Bottom then
  1117. begin
  1118. x := Top;
  1119. Top := Bottom;
  1120. Bottom := x;
  1121. end;
  1122. if Left>Right then
  1123. begin
  1124. x := Left;
  1125. Left := Right;
  1126. Right := x;
  1127. end
  1128. end;
  1129. procedure TRectF.Inflate(DL, DT, DR, DB: Single);
  1130. begin
  1131. Left:=Left-dl;
  1132. Top:=Top-dt;
  1133. Right:=Right+dr;
  1134. Bottom:=Bottom+db;
  1135. end;
  1136. procedure TRectF.Offset(const dx, dy: Single);
  1137. begin
  1138. left:=left+dx; right:=right+dx;
  1139. bottom:=bottom+dy; top:=top+dy;
  1140. end;
  1141. procedure TRectF.Offset(DP: TPointF);
  1142. begin
  1143. left:=left+DP.x; right:=right+DP.x;
  1144. bottom:=bottom+DP.y; top:=top+DP.y;
  1145. end;
  1146. function TRectF.Truncate: TRect;
  1147. begin
  1148. Result.BottomRight:=BottomRight.Truncate;
  1149. Result.TopLeft:=TopLeft.Truncate;
  1150. end;
  1151. function TRectF.Round: TRect;
  1152. begin
  1153. Result.BottomRight:=BottomRight.Round;
  1154. Result.TopLeft:=TopLeft.Round;
  1155. end;
  1156. procedure TRectF.SetHeight(AValue: Single);
  1157. begin
  1158. bottom:=top+avalue;
  1159. end;
  1160. procedure TRectF.SetLocation(X, Y: Single);
  1161. begin
  1162. Offset(X-Left, Y-Top);
  1163. end;
  1164. procedure TRectF.SetLocation(P: TPointF);
  1165. begin
  1166. SetLocation(P.X, P.Y);
  1167. end;
  1168. procedure TRectF.SetSize(AValue: TSizeF);
  1169. begin
  1170. bottom:=top+avalue.cy;
  1171. right:=left+avalue.cx;
  1172. end;
  1173. procedure TRectF.SetWidth(AValue: Single);
  1174. begin
  1175. right:=left+avalue;
  1176. end;
  1177. class function TRectF.Union(const Points: array of TPointF): TRectF;
  1178. var
  1179. i: Integer;
  1180. begin
  1181. if Length(Points) > 0 then
  1182. begin
  1183. Result.TopLeft := Points[Low(Points)];
  1184. Result.BottomRight := Points[Low(Points)];
  1185. for i := Low(Points)+1 to High(Points) do
  1186. begin
  1187. if Points[i].X < Result.Left then Result.Left := Points[i].X;
  1188. if Points[i].X > Result.Right then Result.Right := Points[i].X;
  1189. if Points[i].Y < Result.Top then Result.Top := Points[i].Y;
  1190. if Points[i].Y > Result.Bottom then Result.Bottom := Points[i].Y;
  1191. end;
  1192. end else
  1193. Result := Empty;
  1194. end;
  1195. procedure TRectF.Union(const r: TRectF);
  1196. begin
  1197. left:=min(r.left,left);
  1198. top:=min(r.top,top);
  1199. right:=max(r.right,right);
  1200. bottom:=max(r.bottom,bottom);
  1201. end;
  1202. class function TRectF.Union(R1, R2: TRectF): TRectF;
  1203. begin
  1204. Result:=R1;
  1205. Result.Union(R2);
  1206. end;
  1207. { TPoint3D }
  1208. constructor TPoint3D.Create(const ax,ay,az:single);
  1209. begin
  1210. x:=ax; y:=ay; z:=az;
  1211. end;
  1212. procedure TPoint3D.Offset(const adeltax,adeltay,adeltaz:single);
  1213. begin
  1214. x:=x+adeltax; y:=y+adeltay; z:=z+adeltaz;
  1215. end;
  1216. procedure TPoint3D.Offset(const adelta:TPoint3D);
  1217. begin
  1218. x:=x+adelta.x; y:=y+adelta.y; z:=z+adelta.z;
  1219. end;
  1220. {$ifndef VER3_0}
  1221. generic class procedure TBitConverter.UnsafeFrom<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0);
  1222. begin
  1223. move(ASrcValue, ADestination[AOffset], SizeOf(T));
  1224. end;
  1225. generic class procedure TBitConverter.From<T>(const ASrcValue: T; var ADestination: Array of Byte; AOffset: Integer = 0);
  1226. begin
  1227. if AOffset < 0 then
  1228. System.Error(reRangeError);
  1229. if IsManagedType(T) then
  1230. System.Error(reInvalidCast);
  1231. if Length(ADestination) < (SizeOf(T) + AOffset) then
  1232. System.Error(reRangeError);
  1233. TBitConverter.specialize UnsafeFrom<T>(ASrcValue, ADestination, AOffset);
  1234. end;
  1235. generic class function TBitConverter.UnsafeInTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T;
  1236. begin
  1237. move(ASource[AOffset], Result, SizeOf(T));
  1238. end;
  1239. generic class function TBitConverter.InTo<T>(const ASource: Array of Byte; AOffset: Integer = 0): T;
  1240. begin
  1241. if AOffset < 0 then
  1242. System.Error(reRangeError);
  1243. if IsManagedType(T) then
  1244. System.Error(reInvalidCast);
  1245. if Length(ASource) < (SizeOf(T) + AOffset) then
  1246. System.Error(reRangeError);
  1247. Result := TBitConverter.specialize UnsafeInTo<T>(ASource, AOffset);
  1248. end;
  1249. {$endif}
  1250. end.