/AppKit/CoreGraphics/CGAffineTransform.j

http://github.com/cacaodev/cappuccino · Unknown · 226 lines · 192 code · 34 blank · 0 comment · 0 complexity · 48155aed13d3a48dc2489c114c45ca2f MD5 · raw file

  1. /*
  2. * CGAffineTransform.j
  3. * AppKit
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2008, 280 North, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. @import "CGGeometry.j"
  23. @typedef CGAffineTransform
  24. function CGAffineTransformMake(a, b, c, d, tx, ty)
  25. {
  26. return { a:a, b:b, c:c, d:d, tx:tx, ty:ty };
  27. }
  28. function CGAffineTransformMakeIdentity()
  29. {
  30. return { a:1.0, b:0.0, c:0.0, d:1.0, tx:0.0, ty:0.0 };
  31. }
  32. function CGAffineTransformMakeCopy(anAffineTransform)
  33. {
  34. return { a:anAffineTransform.a, b:anAffineTransform.b, c:anAffineTransform.c, d:anAffineTransform.d, tx:anAffineTransform.tx, ty:anAffineTransform.ty };
  35. }
  36. function CGAffineTransformMakeScale(sx, sy)
  37. {
  38. return { a:sx, b:0.0, c:0.0, d:sy, tx:0.0, ty:0.0 };
  39. }
  40. function CGAffineTransformMakeTranslation(tx, ty)
  41. {
  42. return { a:1.0, b:0.0, c:0.0, d:1.0, tx:tx, ty:ty };
  43. }
  44. function CGAffineTransformTranslate(aTransform, tx, ty)
  45. {
  46. return CGAffineTransformMake(aTransform.a, aTransform.b, aTransform.c, aTransform.d, aTransform.tx + aTransform.a * tx + aTransform.c * ty, aTransform.ty + aTransform.b * tx + aTransform.d * ty);
  47. }
  48. function CGAffineTransformScale(aTransform, sx, sy)
  49. {
  50. return CGAffineTransformMake(aTransform.a * sx, aTransform.b * sx, aTransform.c * sy, aTransform.d * sy, aTransform.tx, aTransform.ty);
  51. }
  52. function CGAffineTransformConcat(lhs, rhs)
  53. {
  54. return CGAffineTransformMake(lhs.a * rhs.a + lhs.b * rhs.c, lhs.a * rhs.b + lhs.b * rhs.d, lhs.c * rhs.a + lhs.d * rhs.c, lhs.c * rhs.b + lhs.d * rhs.d, lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx, lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty);
  55. }
  56. function CGAffineTransformConcatTo(lhs, rhs, to)
  57. {
  58. var tx = lhs.tx * rhs.a + lhs.ty * rhs.c + rhs.tx;
  59. to.ty = lhs.tx * rhs.b + lhs.ty * rhs.d + rhs.ty;
  60. to.tx = tx;
  61. var a = lhs.a * rhs.a + lhs.b * rhs.c,
  62. b = lhs.a * rhs.b + lhs.b * rhs.d,
  63. c = lhs.c * rhs.a + lhs.d * rhs.c;
  64. to.d = lhs.c * rhs.b + lhs.d * rhs.d;
  65. to.a = a;
  66. to.b = b;
  67. to.c = c;
  68. }
  69. function CGPointApplyAffineTransform(aPoint, aTransform)
  70. {
  71. return { x:aPoint.x * aTransform.a + aPoint.y * aTransform.c + aTransform.tx,
  72. y:aPoint.x * aTransform.b + aPoint.y * aTransform.d + aTransform.ty };
  73. }
  74. function CGSizeApplyAffineTransform(aSize, aTransform)
  75. {
  76. return { width:aSize.width * aTransform.a + aSize.height * aTransform.c,
  77. height:aSize.width * aTransform.b + aSize.height * aTransform.d };
  78. }
  79. function CGAffineTransformIsIdentity(aTransform)
  80. {
  81. return (aTransform.a === 1.0 &&
  82. aTransform.b === 0.0 &&
  83. aTransform.c === 0.0 &&
  84. aTransform.d === 1.0 &&
  85. aTransform.tx === 0.0 &&
  86. aTransform.ty === 0.0);
  87. }
  88. function CGAffineTransformEqualToTransform(lhs, rhs)
  89. {
  90. return (lhs.a === rhs.a &&
  91. lhs.b === rhs.b &&
  92. lhs.c === rhs.c &&
  93. lhs.d === rhs.d &&
  94. lhs.tx === rhs.tx &&
  95. lhs.ty === rhs.ty);
  96. }
  97. function CGStringCreateWithCGAffineTransform(aTransform)
  98. {
  99. return (" [[ " + aTransform.a + ", " + aTransform.b + ", 0 ], [ " + aTransform.c + ", " + aTransform.d + ", 0 ], [ " + aTransform.tx + ", " + aTransform.ty + ", 1]]");
  100. }
  101. /*
  102. FIXME: !!!!
  103. @return void
  104. @group CGAffineTransform
  105. */
  106. CGAffineTransformCreateCopy = CGAffineTransformMakeCopy;
  107. /*!
  108. Returns a transform that rotates a coordinate system.
  109. @param anAngle the amount in radians for the transform
  110. to rotate a coordinate system
  111. @return CGAffineTransform the transform with a specified
  112. rotation
  113. @group CGAffineTransform
  114. */
  115. function CGAffineTransformMakeRotation(anAngle)
  116. {
  117. var sin = SIN(anAngle),
  118. cos = COS(anAngle);
  119. return CGAffineTransformMake(cos, sin, -sin, cos, 0.0, 0.0);
  120. }
  121. /*!
  122. Rotates a transform.
  123. @param aTransform the transform to rotate
  124. @param anAngle the amount to rotate in radians
  125. @return void
  126. @group CGAffineTransform
  127. */
  128. function CGAffineTransformRotate(aTransform, anAngle)
  129. {
  130. var sin = SIN(anAngle),
  131. cos = COS(anAngle);
  132. return {
  133. a:aTransform.a * cos + aTransform.c * sin,
  134. b:aTransform.b * cos + aTransform.d * sin,
  135. c:aTransform.c * cos - aTransform.a * sin,
  136. d:aTransform.d * cos - aTransform.b * sin,
  137. tx:aTransform.tx,
  138. ty:aTransform.ty
  139. };
  140. }
  141. /*!
  142. Inverts a transform.
  143. @param aTransform the transform to invert
  144. @return CGAffineTransform an inverted transform
  145. @group CGAffineTransform
  146. */
  147. function CGAffineTransformInvert(aTransform)
  148. {
  149. var determinant = 1 / (aTransform.a * aTransform.d - aTransform.b * aTransform.c);
  150. return {
  151. a:determinant * aTransform.d,
  152. b:-determinant * aTransform.b,
  153. c:-determinant * aTransform.c,
  154. d:determinant * aTransform.a,
  155. tx:determinant * (aTransform.c * aTransform.ty - aTransform.d * aTransform.tx),
  156. ty:determinant * (aTransform.b * aTransform.tx - aTransform.a * aTransform.ty)
  157. };
  158. }
  159. /*!
  160. Applies a transform to the rectangle's points. The transformed rectangle
  161. will be the smallest box that contains the transformed points.
  162. @param aRect the rectangle to transform
  163. @param anAffineTransform the transform to apply
  164. @return CGRect the new transformed rectangle
  165. @group CGAffineTransform
  166. */
  167. function CGRectApplyAffineTransform(aRect, anAffineTransform)
  168. {
  169. var top = CGRectGetMinY(aRect),
  170. left = CGRectGetMinX(aRect),
  171. right = CGRectGetMaxX(aRect),
  172. bottom = CGRectGetMaxY(aRect),
  173. topLeft = CGPointApplyAffineTransform(CGPointMake(left, top), anAffineTransform),
  174. topRight = CGPointApplyAffineTransform(CGPointMake(right, top), anAffineTransform),
  175. bottomLeft = CGPointApplyAffineTransform(CGPointMake(left, bottom), anAffineTransform),
  176. bottomRight = CGPointApplyAffineTransform(CGPointMake(right, bottom), anAffineTransform),
  177. minX = MIN(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
  178. maxX = MAX(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x),
  179. minY = MIN(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y),
  180. maxY = MAX(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
  181. return CGRectMake(minX, minY, (maxX - minX), (maxY - minY));
  182. }
  183. /*!
  184. Creates and returns a string representation of an affine transform.
  185. @param anAffineTransform the transform to represent as a string
  186. @return CPString a string describing the transform
  187. @group CGAffineTransform
  188. */
  189. function CPStringFromCGAffineTransform(anAffineTransform)
  190. {
  191. return '{' + anAffineTransform.a + ", " + anAffineTransform.b + ", " + anAffineTransform.c + ", " + anAffineTransform.d + ", " + anAffineTransform.tx + ", " + anAffineTransform.ty + '}';
  192. }