LOTBezierData.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // LOTBezierData.m
  3. // Lottie
  4. //
  5. // Created by brandon_withrow on 7/10/17.
  6. // Copyright © 2017 Airbnb. All rights reserved.
  7. //
  8. #import "LOTBezierData.h"
  9. #import "CGGeometry+LOTAdditions.h"
  10. @implementation LOTBezierData {
  11. CGPoint *_vertices;
  12. CGPoint *_inTangents;
  13. CGPoint *_outTangents;
  14. }
  15. - (instancetype)initWithData:(NSDictionary *)bezierData
  16. {
  17. self = [super init];
  18. if (self) {
  19. [self initializeData:bezierData];
  20. }
  21. return self;
  22. }
  23. - (void)dealloc {
  24. free(_vertices);
  25. free(_inTangents);
  26. free(_outTangents);
  27. }
  28. - (CGPoint)vertexAtIndex:(NSInteger)index {
  29. NSAssert((index < _count &&
  30. index >= 0),
  31. @"Lottie: Index out of bounds");
  32. return _vertices[index];
  33. }
  34. - (CGPoint)inTangentAtIndex:(NSInteger)index {
  35. NSAssert((index < _count &&
  36. index >= 0),
  37. @"Lottie: Index out of bounds");
  38. return _inTangents[index];
  39. }
  40. - (CGPoint)outTangentAtIndex:(NSInteger)index {
  41. NSAssert((index < _count &&
  42. index >= 0),
  43. @"Lottie: Index out of bounds");
  44. return _outTangents[index];
  45. }
  46. - (void)initializeData:(NSDictionary *)bezierData {
  47. NSArray *pointsArray = bezierData[@"v"];
  48. NSArray *inTangents = bezierData[@"i"];
  49. NSArray *outTangents = bezierData[@"o"];
  50. if (pointsArray.count == 0) {
  51. NSLog(@"%s: Warning: shape has no vertices", __PRETTY_FUNCTION__);
  52. return ;
  53. }
  54. NSAssert((pointsArray.count == inTangents.count &&
  55. pointsArray.count == outTangents.count),
  56. @"Lottie: Incorrect number of points and tangents");
  57. _count = pointsArray.count;
  58. _vertices = (CGPoint *)malloc(sizeof(CGPoint) * pointsArray.count);
  59. _inTangents = (CGPoint *)malloc(sizeof(CGPoint) * pointsArray.count);
  60. _outTangents = (CGPoint *)malloc(sizeof(CGPoint) * pointsArray.count);
  61. if (bezierData[@"c"]) {
  62. _closed = [bezierData[@"c"] boolValue];
  63. }
  64. for (int i = 0; i < pointsArray.count; i ++) {
  65. CGPoint vertex = [self _vertexAtIndex:i inArray:pointsArray];
  66. CGPoint outTan = LOT_PointAddedToPoint(vertex, [self _vertexAtIndex:i inArray:outTangents]);
  67. CGPoint inTan = LOT_PointAddedToPoint(vertex, [self _vertexAtIndex:i inArray:inTangents]);
  68. // BW BUG Straight Lines - Test Later
  69. // Bake fix for lines here
  70. _vertices[i] = vertex;
  71. _inTangents[i] = inTan;
  72. _outTangents[i] = outTan;
  73. }
  74. }
  75. - (CGPoint)_vertexAtIndex:(NSInteger)idx inArray:(NSArray *)points {
  76. NSAssert((idx < points.count),
  77. @"Lottie: Vertex Point out of bounds");
  78. NSArray *pointArray = points[idx];
  79. NSAssert((pointArray.count >= 2 &&
  80. [pointArray.firstObject isKindOfClass:[NSNumber class]]),
  81. @"Lottie: Point Data Malformed");
  82. return CGPointMake([(NSNumber *)pointArray[0] floatValue], [(NSNumber *)pointArray[1] floatValue]);
  83. }
  84. @end