|
5 | 5 | // Created by Kyle on 2025/3/25. |
6 | 6 | // |
7 | 7 |
|
8 | | -#include <OpenRenderBox/ORBPathStorage.h> |
| 8 | +#include <OpenRenderBox/ORBPath.h> |
| 9 | +#include <OpenRenderBox/ORBPathCallbacks.h> |
| 10 | + |
| 11 | +// Empty path callbacks (all null) - C++ internal linkage |
| 12 | +static const ORBPathCallbacks empty_path_callbacks = { |
| 13 | + nullptr, |
| 14 | + nullptr, |
| 15 | + nullptr, |
| 16 | + nullptr, |
| 17 | + nullptr, |
| 18 | + nullptr, |
| 19 | + nullptr, |
| 20 | + nullptr, |
| 21 | + nullptr, |
| 22 | + nullptr, |
| 23 | + nullptr, |
| 24 | +}; |
| 25 | + |
| 26 | +// Empty path (storage = null) |
| 27 | +const ORBPath ORBPathEmpty = { |
| 28 | + nullptr, |
| 29 | + &empty_path_callbacks, |
| 30 | +}; |
| 31 | + |
| 32 | +// Null path (storage = 0x1) |
| 33 | +const ORBPath ORBPathNull = { |
| 34 | + reinterpret_cast<ORBPathStorage *>(0x1), |
| 35 | + &empty_path_callbacks, |
| 36 | +}; |
9 | 37 |
|
10 | 38 | void ORBPathRetain(ORBPath path) { |
11 | | - // TODO |
| 39 | + auto retain = path.callbacks->retain; |
| 40 | + if (retain != nullptr) { |
| 41 | + retain(&path); |
| 42 | + } |
12 | 43 | } |
13 | 44 |
|
14 | 45 | void ORBPathRelease(ORBPath path) { |
15 | | - // TODO |
| 46 | + auto release = path.callbacks->release; |
| 47 | + if (release != nullptr) { |
| 48 | + release(&path); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#if ORB_TARGET_OS_DARWIN |
| 53 | + |
| 54 | +// MARK: - Path Creation |
| 55 | + |
| 56 | +// TODO: TO be implemented natively |
| 57 | + |
| 58 | +ORBPath ORBPathMakeWithCGPath(CGPathRef cgPath) { |
| 59 | + if (cgPath == nullptr) { |
| 60 | + return ORBPathNull; |
| 61 | + } |
| 62 | + CFRetain(cgPath); |
| 63 | + return ORBPath { |
| 64 | + reinterpret_cast<ORBPathStorage *>(const_cast<CGPath *>(cgPath)), |
| 65 | + &ORBPathCGPathCallbacks, |
| 66 | + }; |
16 | 67 | } |
| 68 | + |
| 69 | +ORBPath ORBPathMakeRect(CGRect rect, const CGAffineTransform *transform) { |
| 70 | + CGPathRef cgPath = CGPathCreateWithRect(rect, transform); |
| 71 | + ORBPath path = { |
| 72 | + reinterpret_cast<ORBPathStorage *>(const_cast<CGPath *>(cgPath)), |
| 73 | + &ORBPathCGPathCallbacks, |
| 74 | + }; |
| 75 | + return path; |
| 76 | +} |
| 77 | + |
| 78 | +ORBPath ORBPathMakeEllipse(CGRect rect, const CGAffineTransform *transform) { |
| 79 | + CGPathRef cgPath = CGPathCreateWithEllipseInRect(rect, transform); |
| 80 | + ORBPath path = { |
| 81 | + reinterpret_cast<ORBPathStorage *>(const_cast<CGPath *>(cgPath)), |
| 82 | + &ORBPathCGPathCallbacks, |
| 83 | + }; |
| 84 | + return path; |
| 85 | +} |
| 86 | + |
| 87 | +ORBPath ORBPathMakeRoundedRect(CGRect rect, CGFloat cornerWidth, CGFloat cornerHeight, ORBPathRoundedCornerStyle style, const CGAffineTransform *transform) { |
| 88 | + // TODO: Handle ORBRoundedCornerStyleContinuous with custom bezier curves |
| 89 | + CGPathRef cgPath = CGPathCreateWithRoundedRect(rect, cornerWidth, cornerHeight, transform); |
| 90 | + ORBPath path = { |
| 91 | + reinterpret_cast<ORBPathStorage *>(const_cast<CGPath *>(cgPath)), |
| 92 | + &ORBPathCGPathCallbacks, |
| 93 | + }; |
| 94 | + return path; |
| 95 | +} |
| 96 | + |
| 97 | +ORBPath ORBPathMakeUnevenRoundedRect(CGRect rect, CGFloat topLeftRadius, CGFloat bottomLeftRadius, CGFloat bottomRightRadius, CGFloat topRightRadius, ORBPathRoundedCornerStyle style, const CGAffineTransform *transform) { |
| 98 | + // TODO: Handle ORBRoundedCornerStyleContinuous with custom bezier curves |
| 99 | + CGMutablePathRef cgPath = CGPathCreateMutable(); |
| 100 | + |
| 101 | + CGFloat minX = CGRectGetMinX(rect); |
| 102 | + CGFloat minY = CGRectGetMinY(rect); |
| 103 | + CGFloat maxX = CGRectGetMaxX(rect); |
| 104 | + CGFloat maxY = CGRectGetMaxY(rect); |
| 105 | + |
| 106 | + // Start at top-left corner (after the rounded corner) |
| 107 | + CGPathMoveToPoint(cgPath, transform, minX + topLeftRadius, minY); |
| 108 | + |
| 109 | + // Top edge and top-right corner |
| 110 | + CGPathAddLineToPoint(cgPath, transform, maxX - topRightRadius, minY); |
| 111 | + CGPathAddArc(cgPath, transform, maxX - topRightRadius, minY + topRightRadius, topRightRadius, -M_PI_2, 0, false); |
| 112 | + |
| 113 | + // Right edge and bottom-right corner |
| 114 | + CGPathAddLineToPoint(cgPath, transform, maxX, maxY - bottomRightRadius); |
| 115 | + CGPathAddArc(cgPath, transform, maxX - bottomRightRadius, maxY - bottomRightRadius, bottomRightRadius, 0, M_PI_2, false); |
| 116 | + |
| 117 | + // Bottom edge and bottom-left corner |
| 118 | + CGPathAddLineToPoint(cgPath, transform, minX + bottomLeftRadius, maxY); |
| 119 | + CGPathAddArc(cgPath, transform, minX + bottomLeftRadius, maxY - bottomLeftRadius, bottomLeftRadius, M_PI_2, M_PI, false); |
| 120 | + |
| 121 | + // Left edge and top-left corner |
| 122 | + CGPathAddLineToPoint(cgPath, transform, minX, minY + topLeftRadius); |
| 123 | + CGPathAddArc(cgPath, transform, minX + topLeftRadius, minY + topLeftRadius, topLeftRadius, M_PI, M_PI + M_PI_2, false); |
| 124 | + |
| 125 | + CGPathCloseSubpath(cgPath); |
| 126 | + |
| 127 | + ORBPath path = { |
| 128 | + reinterpret_cast<ORBPathStorage *>(cgPath), |
| 129 | + &ORBPathCGPathCallbacks, |
| 130 | + }; |
| 131 | + return path; |
| 132 | +} |
| 133 | + |
| 134 | +CGPathRef ORBPathCopyCGPath(ORBPath path) { |
| 135 | + // TODO: Return a retained copy of the CGPath |
| 136 | + return nullptr; |
| 137 | +} |
| 138 | + |
| 139 | +bool ORBPathContainsPoint(ORBPath path, CGPoint point, bool eoFill) { |
| 140 | + return ORBPathContainsPoints(path, 1, &point, eoFill, nullptr); |
| 141 | +} |
| 142 | + |
| 143 | +bool ORBPathContainsPoints(ORBPath path, uint64_t count, const CGPoint *points, bool eoFill, const CGAffineTransform *transform) { |
| 144 | + // TODO: Implement point containment testing with winding rule |
| 145 | + return false; |
| 146 | +} |
| 147 | + |
| 148 | +#endif /* ORB_TARGET_OS_DARWIN */ |
0 commit comments