/core/externals/google-toolbox-for-mac/Foundation/GTMGeometryUtils.h

http://macfuse.googlecode.com/ · C Header · 463 lines · 175 code · 49 blank · 239 comment · 6 complexity · 710094c19cbe474d0ae2ea291cace39b MD5 · raw file

  1. //
  2. // GTMGeometryUtils.h
  3. //
  4. // Utilities for geometrical utilities such as conversions
  5. // between different types.
  6. //
  7. // Copyright 2006-2008 Google Inc.
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  10. // use this file except in compliance with the License. You may obtain a copy
  11. // of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  18. // License for the specific language governing permissions and limitations under
  19. // the License.
  20. //
  21. #import <Foundation/Foundation.h>
  22. #import "GTMDefines.h"
  23. #if GTM_IPHONE_SDK
  24. #import <CoreGraphics/CoreGraphics.h>
  25. #endif // GTM_IPHONE_SDK
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. enum {
  30. GTMScaleProportionally = 0, // Fit proportionally
  31. GTMScaleToFit, // Forced fit (distort if necessary)
  32. GTMScaleNone, // Don't scale (clip)
  33. GTMScaleToFillProportionally = 101 // Scale proportionally to fill area
  34. };
  35. typedef NSUInteger GTMScaling;
  36. enum {
  37. GTMRectAlignCenter = 0,
  38. GTMRectAlignTop,
  39. GTMRectAlignTopLeft,
  40. GTMRectAlignTopRight,
  41. GTMRectAlignLeft,
  42. GTMRectAlignBottom,
  43. GTMRectAlignBottomLeft,
  44. GTMRectAlignBottomRight,
  45. GTMRectAlignRight
  46. };
  47. typedef NSUInteger GTMRectAlignment;
  48. #pragma mark -
  49. #pragma mark CG - Point On Rect
  50. /// Return middle of min X side of rectangle
  51. //
  52. // Args:
  53. // rect - rectangle
  54. //
  55. // Returns:
  56. // point located in the middle of min X side of rect
  57. GTM_INLINE CGPoint GTMCGMidMinX(CGRect rect) {
  58. return CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
  59. }
  60. /// Return middle of max X side of rectangle
  61. //
  62. // Args:
  63. // rect - rectangle
  64. //
  65. // Returns:
  66. // point located in the middle of max X side of rect
  67. GTM_INLINE CGPoint GTMCGMidMaxX(CGRect rect) {
  68. return CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));
  69. }
  70. /// Return middle of max Y side of rectangle
  71. //
  72. // Args:
  73. // rect - rectangle
  74. //
  75. // Returns:
  76. // point located in the middle of max Y side of rect
  77. GTM_INLINE CGPoint GTMCGMidMaxY(CGRect rect) {
  78. return CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
  79. }
  80. /// Return middle of min Y side of rectangle
  81. //
  82. // Args:
  83. // rect - rectangle
  84. //
  85. // Returns:
  86. // point located in the middle of min Y side of rect
  87. GTM_INLINE CGPoint GTMCGMidMinY(CGRect rect) {
  88. return CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
  89. }
  90. /// Return center of rectangle
  91. //
  92. // Args:
  93. // rect - rectangle
  94. //
  95. // Returns:
  96. // point located in the center of rect
  97. GTM_INLINE CGPoint GTMCGCenter(CGRect rect) {
  98. return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  99. }
  100. #pragma mark -
  101. #pragma mark CG - Rect-Size Conversion
  102. /// Return size of rectangle
  103. //
  104. // Args:
  105. // rect - rectangle
  106. //
  107. // Returns:
  108. // size of rectangle
  109. GTM_INLINE CGSize GTMCGRectSize(CGRect rect) {
  110. return CGSizeMake(CGRectGetWidth(rect), CGRectGetHeight(rect));
  111. }
  112. /// Return rectangle of size
  113. //
  114. // Args:
  115. // size - size
  116. //
  117. // Returns:
  118. // rectangle of size (origin 0,0)
  119. GTM_INLINE CGRect GTMCGRectOfSize(CGSize size) {
  120. return CGRectMake(0.0f, 0.0f, size.width, size.height);
  121. }
  122. #pragma mark -
  123. #pragma mark CG - Rect Scaling and Alignment
  124. /// Scales an CGRect
  125. //
  126. // Args:
  127. // inRect: Rect to scale
  128. // xScale: fraction to scale (1.0 is 100%)
  129. // yScale: fraction to scale (1.0 is 100%)
  130. //
  131. // Returns:
  132. // Converted Rect
  133. GTM_INLINE CGRect GTMCGRectScale(CGRect inRect, CGFloat xScale, CGFloat yScale) {
  134. return CGRectMake(inRect.origin.x, inRect.origin.y,
  135. inRect.size.width * xScale, inRect.size.height * yScale);
  136. }
  137. /// Align rectangles
  138. //
  139. // Args:
  140. // alignee - rect to be aligned
  141. // aligner - rect to be aligned from
  142. // alignment - way to align the rectangles
  143. CGRect GTMCGAlignRectangles(CGRect alignee, CGRect aligner,
  144. GTMRectAlignment alignment);
  145. /// Scale rectangle
  146. //
  147. // Args:
  148. // scalee - rect to be scaled
  149. // size - size to scale to
  150. // scaling - way to scale the rectangle
  151. CGRect GTMCGScaleRectangleToSize(CGRect scalee, CGSize size,
  152. GTMScaling scaling);
  153. #pragma mark -
  154. #pragma mark CG - Miscellaneous
  155. /// Calculate the distance between two points.
  156. //
  157. // Args:
  158. // pt1 first point
  159. // pt2 second point
  160. //
  161. // Returns:
  162. // Distance
  163. GTM_INLINE CGFloat GTMCGDistanceBetweenPoints(CGPoint pt1, CGPoint pt2) {
  164. CGFloat dX = pt1.x - pt2.x;
  165. CGFloat dY = pt1.y - pt2.y;
  166. #if CGFLOAT_IS_DOUBLE
  167. return sqrt(dX * dX + dY * dY);
  168. #else
  169. return sqrtf(dX * dX + dY * dY);
  170. #endif
  171. }
  172. #if !GTM_IPHONE_SDK
  173. // iPhone does not have NSTypes defined, only CGTypes. So no NSRect, NSPoint etc.
  174. #pragma mark -
  175. // All of the conversion routines below are basically copied from the
  176. // NSGeometry header in the 10.5 sdk.
  177. #pragma mark NS <-> CG Point Conversion
  178. /// Quickly convert from a CGPoint to a NSPoint.
  179. //
  180. /// CGPoints are relative to 0,0 in lower left;
  181. /// NSPoints are relative to 0,0 in lower left
  182. //
  183. // Args:
  184. // inPoint: CGPoint to convert
  185. //
  186. // Returns:
  187. // Converted NSPoint
  188. GTM_INLINE NSPoint GTMCGPointToNSPoint(CGPoint inPoint) {
  189. _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size);
  190. union convertUnion {NSPoint ns; CGPoint cg;};
  191. return ((union convertUnion *)&inPoint)->ns;
  192. }
  193. /// Quickly convert from a NSPoint to a CGPoint.
  194. //
  195. /// CGPoints are relative to 0,0 in lower left;
  196. /// NSPoints are relative to 0,0 in lower left
  197. //
  198. // Args:
  199. // inPoint: NSPoint to convert
  200. //
  201. // Returns:
  202. // Converted CGPoint
  203. GTM_INLINE CGPoint GTMNSPointToCGPoint(NSPoint inPoint) {
  204. _GTMCompileAssert(sizeof(NSPoint) == sizeof(CGPoint), NSPoint_and_CGPoint_must_be_the_same_size);
  205. union convertUnion {NSPoint ns; CGPoint cg;};
  206. return ((union convertUnion *)&inPoint)->cg;
  207. }
  208. #pragma mark -
  209. #pragma mark NS <-> CG Rect Conversion
  210. /// Convert from a CGRect to a NSRect.
  211. //
  212. /// NSRect are relative to 0,0 in lower left;
  213. /// CGRect are relative to 0,0 in lower left
  214. //
  215. // Args:
  216. // inRect: CGRect to convert
  217. //
  218. // Returns:
  219. // Converted NSRect
  220. GTM_INLINE NSRect GTMCGRectToNSRect(CGRect inRect) {
  221. _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size);
  222. union convertUnion {NSRect ns; CGRect cg;};
  223. return ((union convertUnion *)&inRect)->ns;
  224. }
  225. /// Convert from a NSRect to a CGRect.
  226. //
  227. /// NSRect are relative to 0,0 in lower left;
  228. /// CGRect are relative to 0,0 in lower left
  229. //
  230. // Args:
  231. // inRect: NSRect to convert
  232. //
  233. // Returns:
  234. // Converted CGRect
  235. GTM_INLINE CGRect GTMNSRectToCGRect(NSRect inRect) {
  236. _GTMCompileAssert(sizeof(NSRect) == sizeof(CGRect), NSRect_and_CGRect_must_be_the_same_size);
  237. union convertUnion {NSRect ns; CGRect cg;};
  238. return ((union convertUnion *)&inRect)->cg;
  239. }
  240. #pragma mark -
  241. #pragma mark NS <-> CG Size Conversion
  242. /// Convert from a CGSize to an NSSize.
  243. //
  244. // Args:
  245. // inSize: CGSize to convert
  246. //
  247. // Returns:
  248. // Converted NSSize
  249. GTM_INLINE NSSize GTMCGSizeToNSSize(CGSize inSize) {
  250. _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size);
  251. union convertUnion {NSSize ns; CGSize cg;};
  252. return ((union convertUnion *)&inSize)->ns;
  253. }
  254. /// Convert from a NSSize to a CGSize.
  255. //
  256. // Args:
  257. // inSize: NSSize to convert
  258. //
  259. // Returns:
  260. // Converted CGSize
  261. GTM_INLINE CGSize GTMNSSizeToCGSize(NSSize inSize) {
  262. _GTMCompileAssert(sizeof(NSSize) == sizeof(CGSize), NSSize_and_CGSize_must_be_the_same_size);
  263. union convertUnion {NSSize ns; CGSize cg;};
  264. return ((union convertUnion *)&inSize)->cg;
  265. }
  266. #pragma mark -
  267. #pragma mark NS - Point On Rect
  268. /// Return middle of min X side of rectangle
  269. //
  270. // Args:
  271. // rect - rectangle
  272. //
  273. // Returns:
  274. // point located in the middle of min X side of rect
  275. GTM_INLINE NSPoint GTMNSMidMinX(NSRect rect) {
  276. return NSMakePoint(NSMinX(rect), NSMidY(rect));
  277. }
  278. /// Return middle of max X side of rectangle
  279. //
  280. // Args:
  281. // rect - rectangle
  282. //
  283. // Returns:
  284. // point located in the middle of max X side of rect
  285. GTM_INLINE NSPoint GTMNSMidMaxX(NSRect rect) {
  286. return NSMakePoint(NSMaxX(rect), NSMidY(rect));
  287. }
  288. /// Return middle of max Y side of rectangle
  289. //
  290. // Args:
  291. // rect - rectangle
  292. //
  293. // Returns:
  294. // point located in the middle of max Y side of rect
  295. GTM_INLINE NSPoint GTMNSMidMaxY(NSRect rect) {
  296. return NSMakePoint(NSMidX(rect), NSMaxY(rect));
  297. }
  298. /// Return middle of min Y side of rectangle
  299. //
  300. // Args:
  301. // rect - rectangle
  302. //
  303. // Returns:
  304. // point located in the middle of min Y side of rect
  305. GTM_INLINE NSPoint GTMNSMidMinY(NSRect rect) {
  306. return NSMakePoint(NSMidX(rect), NSMinY(rect));
  307. }
  308. /// Return center of rectangle
  309. //
  310. // Args:
  311. // rect - rectangle
  312. //
  313. // Returns:
  314. // point located in the center of rect
  315. GTM_INLINE NSPoint GTMNSCenter(NSRect rect) {
  316. return NSMakePoint(NSMidX(rect), NSMidY(rect));
  317. }
  318. #pragma mark -
  319. #pragma mark NS - Rect-Size Conversion
  320. /// Return size of rectangle
  321. //
  322. // Args:
  323. // rect - rectangle
  324. //
  325. // Returns:
  326. // size of rectangle
  327. GTM_INLINE NSSize GTMNSRectSize(NSRect rect) {
  328. return NSMakeSize(NSWidth(rect), NSHeight(rect));
  329. }
  330. /// Return rectangle of size
  331. //
  332. // Args:
  333. // size - size
  334. //
  335. // Returns:
  336. // rectangle of size (origin 0,0)
  337. GTM_INLINE NSRect GTMNSRectOfSize(NSSize size) {
  338. return NSMakeRect(0.0f, 0.0f, size.width, size.height);
  339. }
  340. #pragma mark -
  341. #pragma mark NS - Rect Scaling and Alignment
  342. /// Scales an NSRect
  343. //
  344. // Args:
  345. // inRect: Rect to scale
  346. // xScale: fraction to scale (1.0 is 100%)
  347. // yScale: fraction to scale (1.0 is 100%)
  348. //
  349. // Returns:
  350. // Converted Rect
  351. GTM_INLINE NSRect GTMNSRectScale(NSRect inRect, CGFloat xScale, CGFloat yScale) {
  352. return NSMakeRect(inRect.origin.x, inRect.origin.y,
  353. inRect.size.width * xScale, inRect.size.height * yScale);
  354. }
  355. /// Align rectangles
  356. //
  357. // Args:
  358. // alignee - rect to be aligned
  359. // aligner - rect to be aligned from
  360. GTM_INLINE NSRect GTMNSAlignRectangles(NSRect alignee, NSRect aligner,
  361. GTMRectAlignment alignment) {
  362. return GTMCGRectToNSRect(GTMCGAlignRectangles(GTMNSRectToCGRect(alignee),
  363. GTMNSRectToCGRect(aligner),
  364. alignment));
  365. }
  366. /// Align a rectangle to another
  367. //
  368. // Args:
  369. // scalee - rect to be scaled
  370. // scaler - rect to scale to
  371. // scaling - way to scale the rectangle
  372. // alignment - way to align the scaled rectangle
  373. GTM_INLINE NSRect GTMNSScaleRectToRect(NSRect scalee,
  374. NSRect scaler,
  375. GTMScaling scaling,
  376. GTMRectAlignment alignment) {
  377. return GTMCGRectToNSRect(
  378. GTMCGAlignRectangles(
  379. GTMCGScaleRectangleToSize(GTMNSRectToCGRect(scalee),
  380. GTMNSSizeToCGSize(scaler.size),
  381. scaling),
  382. GTMNSRectToCGRect(scaler),
  383. alignment));
  384. }
  385. /// Scale rectangle
  386. //
  387. // Args:
  388. // scalee - rect to be scaled
  389. // size - size to scale to
  390. // scaling - way to scale the rectangle
  391. GTM_INLINE NSRect GTMNSScaleRectangleToSize(NSRect scalee, NSSize size,
  392. GTMScaling scaling) {
  393. return GTMCGRectToNSRect(GTMCGScaleRectangleToSize(GTMNSRectToCGRect(scalee),
  394. GTMNSSizeToCGSize(size),
  395. scaling));
  396. }
  397. #pragma mark -
  398. #pragma mark NS - Miscellaneous
  399. /// Calculate the distance between two points.
  400. //
  401. // Args:
  402. // pt1 first point
  403. // pt2 second point
  404. //
  405. // Returns:
  406. // Distance
  407. GTM_INLINE CGFloat GTMNSDistanceBetweenPoints(NSPoint pt1, NSPoint pt2) {
  408. return GTMCGDistanceBetweenPoints(GTMNSPointToCGPoint(pt1),
  409. GTMNSPointToCGPoint(pt2));
  410. }
  411. #endif // !GTM_IPHONE_SDK
  412. #ifdef __cplusplus
  413. }
  414. #endif