/AppKit/CPGraphics.j

http://github.com/cacaodev/cappuccino · Unknown · 121 lines · 102 code · 19 blank · 0 comment · 0 complexity · e78c47a6b9e8e1480d4a0975c51efb48 MD5 · raw file

  1. /*
  2. * CPGraphics.j
  3. * AppKit
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2010, 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 "CPColor.j"
  23. @import "CPGraphicsContext.j"
  24. CPCalibratedWhiteColorSpace = @"CalibratedWhiteColorSpace";
  25. CPCalibratedBlackColorSpace = @"CalibratedBlackColorSpace";
  26. CPCalibratedRGBColorSpace = @"CalibratedRGBColorSpace";
  27. CPDeviceWhiteColorSpace = @"DeviceWhiteColorSpace";
  28. CPDeviceBlackColorSpace = @"DeviceBlackColorSpace";
  29. CPDeviceRGBColorSpace = @"DeviceRGBColorSpace";
  30. CPDeviceCMYKColorSpace = @"DeviceCMYKColorSpace";
  31. CPNamedColorSpace = @"NamedColorSpace";
  32. CPPatternColorSpace = @"PatternColorSpace";
  33. CPCustomColorSpace = @"CustomColorSpace";
  34. function CPDrawTiledRects(
  35. /* CGRect */ boundsRect,
  36. /* CGRect */ clipRect,
  37. /* CPRectEdge[] */ sides,
  38. /* float[] */ grays)
  39. {
  40. if (sides.length != grays.length)
  41. [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and grays (length: " + grays.length + ") must have the same length."];
  42. var colors = [];
  43. for (var i = 0; i < grays.length; ++i)
  44. colors.push([CPColor colorWithCalibratedWhite:grays[i] alpha:1.0]);
  45. return CPDrawColorTiledRects(boundsRect, clipRect, sides, colors);
  46. }
  47. function CPDrawColorTiledRects(
  48. /* CGRect */ boundsRect,
  49. /* CGRect */ clipRect,
  50. /* CPRectEdge[] */ sides,
  51. /* CPColor[] */ colors)
  52. {
  53. if (sides.length != colors.length)
  54. [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and colors (length: " + colors.length + ") must have the same length."];
  55. var resultRect = CGRectMakeCopy(boundsRect),
  56. slice = CGRectMakeZero(),
  57. remainder = CGRectMakeZero(),
  58. context = [[CPGraphicsContext currentContext] graphicsPort];
  59. CGContextSaveGState(context);
  60. CGContextSetLineWidth(context, 1.0);
  61. for (var sideIndex = 0; sideIndex < sides.length; ++sideIndex)
  62. {
  63. var side = sides[sideIndex];
  64. CGRectDivide(resultRect, slice, remainder, 1.0, side);
  65. resultRect = remainder;
  66. slice = CGRectIntersection(slice, clipRect);
  67. // Cocoa docs say that only slices that are within the clipRect are actually drawn
  68. if (CGRectIsEmpty(slice))
  69. continue;
  70. var minX,
  71. maxX,
  72. minY,
  73. maxY;
  74. if (side == CPMinXEdge || side == CPMaxXEdge)
  75. {
  76. // Make sure we have at least 1 pixel to draw a line
  77. if (CGRectGetWidth(slice) < 1.0)
  78. continue;
  79. minX = CGRectGetMinX(slice) + 0.5;
  80. maxX = minX;
  81. minY = CGRectGetMinY(slice);
  82. maxY = CGRectGetMaxY(slice);
  83. }
  84. else // CPMinYEdge || CPMaxYEdge
  85. {
  86. // Make sure we have at least 1 pixel to draw a line
  87. if (CGRectGetHeight(slice) < 1.0)
  88. continue;
  89. minX = CGRectGetMinX(slice);
  90. maxX = CGRectGetMaxX(slice);
  91. minY = CGRectGetMinY(slice) + 0.5;
  92. maxY = minY;
  93. }
  94. CGContextBeginPath(context);
  95. CGContextMoveToPoint(context, minX, minY);
  96. CGContextAddLineToPoint(context, maxX, maxY);
  97. CGContextSetStrokeColor(context, colors[sideIndex]);
  98. CGContextStrokePath(context);
  99. }
  100. CGContextRestoreGState(context);
  101. return resultRect;
  102. }