ARTShape.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/ARTShape.h>
  8. @implementation ARTShape
  9. - (void)setD:(CGPathRef)d
  10. {
  11. if (d == _d) {
  12. return;
  13. }
  14. [self invalidate];
  15. CGPathRelease(_d);
  16. _d = CGPathRetain(d);
  17. }
  18. - (void)dealloc
  19. {
  20. CGPathRelease(_d);
  21. }
  22. - (void)renderLayerTo:(CGContextRef)context
  23. {
  24. if ((!self.fill && !self.stroke) || !self.d) {
  25. return;
  26. }
  27. CGPathDrawingMode mode = kCGPathStroke;
  28. if (self.fill) {
  29. if ([self.fill applyFillColor:context]) {
  30. mode = kCGPathFill;
  31. } else {
  32. CGContextSaveGState(context);
  33. CGContextAddPath(context, self.d);
  34. CGContextClip(context);
  35. [self.fill paint:context];
  36. CGContextRestoreGState(context);
  37. if (!self.stroke) {
  38. return;
  39. }
  40. }
  41. }
  42. if (self.stroke) {
  43. CGContextSetStrokeColorWithColor(context, self.stroke);
  44. CGContextSetLineWidth(context, self.strokeWidth);
  45. CGContextSetLineCap(context, self.strokeCap);
  46. CGContextSetLineJoin(context, self.strokeJoin);
  47. ARTCGFloatArray dash = self.strokeDash;
  48. if (dash.count) {
  49. CGContextSetLineDash(context, 0, dash.array, dash.count);
  50. }
  51. if (mode == kCGPathFill) {
  52. mode = kCGPathFillStroke;
  53. }
  54. }
  55. CGContextAddPath(context, self.d);
  56. CGContextDrawPath(context, mode);
  57. }
  58. @end