|
| 1 | +import Foundation // for sinf and cosf |
| 2 | + |
| 3 | +public enum StrokeCap { |
| 4 | + /// The stroke ends square exactly at the last point. |
| 5 | + case butt |
| 6 | + /// The stroke ends with a semicircle. |
| 7 | + case round |
| 8 | + /// The stroke ends square half of the stroke width past the last point. |
| 9 | + case square |
| 10 | +} |
| 11 | + |
| 12 | +public enum StrokeJoin { |
| 13 | + /// Corners are sharp, unless they are longer than `limit` times half the stroke width, |
| 14 | + /// in which case they are beveled. |
| 15 | + case miter(limit: Float) |
| 16 | + /// Corners are rounded. |
| 17 | + case round |
| 18 | + /// Corners are beveled. |
| 19 | + case bevel |
| 20 | +} |
| 21 | + |
| 22 | +public struct StrokeStyle { |
| 23 | + public var width: Float = 1.0 |
| 24 | + public var cap: StrokeCap = .butt |
| 25 | + public var join: StrokeJoin = .miter(limit: 10.0) |
| 26 | + |
| 27 | + public static let none = StrokeStyle(width: 0.0) |
| 28 | +} |
| 29 | + |
| 30 | +/// An enum describing how a path is shaded. |
| 31 | +public enum FillRule { |
| 32 | + /// A region is shaded if it is enclosed an odd number of times. |
| 33 | + case evenOdd |
| 34 | + /// A region is shaded if it is enclosed at all. |
| 35 | + /// |
| 36 | + /// This is also known as the "non-zero" rule. |
| 37 | + case winding |
| 38 | +} |
| 39 | + |
| 40 | +/// A type representing an affine transformation on a 2-D point. |
| 41 | +/// |
| 42 | +/// Performing an affine transform consists of translating by ``translation`` followed by |
| 43 | +/// multiplying by ``linearTransform``. |
| 44 | +public struct AffineTransform: Equatable { |
| 45 | + /// The linear transformation. This is a 2x2 matrix stored in row-major order. |
| 46 | + /// |
| 47 | + /// The four properties (`x`, `y`, `z`, `w`) correspond to the 2x2 matrix as follows: |
| 48 | + /// ``` |
| 49 | + /// [ x y ] |
| 50 | + /// [ z w ] |
| 51 | + /// ``` |
| 52 | + public var linearTransform: SIMD4<Float> |
| 53 | + /// The translation applied after the linear transformation. |
| 54 | + public var translation: SIMD2<Float> |
| 55 | + |
| 56 | + public init(linearTransform: SIMD4<Float>, translation: SIMD2<Float>) { |
| 57 | + self.linearTransform = linearTransform |
| 58 | + self.translation = translation |
| 59 | + } |
| 60 | + |
| 61 | + public static func translate(x: Float, y: Float) -> AffineTransform { |
| 62 | + AffineTransform( |
| 63 | + linearTransform: SIMD4(x: 1.0, y: 0.0, z: 0.0, w: 1.0), |
| 64 | + translation: SIMD2(x: x, y: y) |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + public static func scale(by factor: Float) -> AffineTransform { |
| 69 | + AffineTransform( |
| 70 | + linearTransform: SIMD4(x: factor, y: 0.0, z: 0.0, w: factor), |
| 71 | + translation: .zero |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + public static func rotate( |
| 76 | + radians: Float, |
| 77 | + center: SIMD2<Float> = .zero |
| 78 | + ) -> AffineTransform { |
| 79 | + let sine = sinf(radians) |
| 80 | + let cosine = cosf(radians) |
| 81 | + return AffineTransform( |
| 82 | + linearTransform: SIMD4(x: cosine, y: -sine, z: sine, w: cosine), |
| 83 | + translation: SIMD2( |
| 84 | + x: -center.x * cosine - center.y * sine + center.x, |
| 85 | + y: center.x * sine - center.y * cosine + center.y |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + public static func rotate( |
| 91 | + degrees: Float, |
| 92 | + center: SIMD2<Float> = .zero |
| 93 | + ) -> AffineTransform { |
| 94 | + rotate(radians: degrees * (.pi / 180.0), center: center) |
| 95 | + } |
| 96 | + |
| 97 | + public static let identity = AffineTransform( |
| 98 | + linearTransform: SIMD4(x: 1.0, y: 0.0, z: 0.0, w: 1.0), |
| 99 | + translation: .zero |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +public struct Path { |
| 104 | + public struct Rect: Equatable { |
| 105 | + public var origin: SIMD2<Float> |
| 106 | + public var size: SIMD2<Float> |
| 107 | + |
| 108 | + public init(origin: SIMD2<Float>, size: SIMD2<Float>) { |
| 109 | + self.origin = origin |
| 110 | + self.size = size |
| 111 | + } |
| 112 | + |
| 113 | + public var x: Float { origin.x } |
| 114 | + public var y: Float { origin.y } |
| 115 | + public var width: Float { size.x } |
| 116 | + public var height: Float { size.y } |
| 117 | + |
| 118 | + public init(x: Float, y: Float, width: Float, height: Float) { |
| 119 | + origin = SIMD2(x: x, y: y) |
| 120 | + size = SIMD2(x: width, y: height) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// The types of actions that can be performed on a path. |
| 125 | + public enum Action: Equatable { |
| 126 | + case moveTo(SIMD2<Float>) |
| 127 | + case lineTo(SIMD2<Float>) |
| 128 | + case quadCurve(control: SIMD2<Float>, end: SIMD2<Float>) |
| 129 | + case cubicCurve(control1: SIMD2<Float>, control2: SIMD2<Float>, end: SIMD2<Float>) |
| 130 | + case rectangle(Rect) |
| 131 | + case circle(center: SIMD2<Float>, radius: Float) |
| 132 | + case arc( |
| 133 | + center: SIMD2<Float>, |
| 134 | + radius: Float, |
| 135 | + startAngle: Float, |
| 136 | + endAngle: Float, |
| 137 | + clockwise: Bool |
| 138 | + ) |
| 139 | + case transform(AffineTransform) |
| 140 | + // case subpath([Action], FillRule) |
| 141 | + } |
| 142 | + |
| 143 | + /// A list of every action that has been performed on this path. |
| 144 | + /// |
| 145 | + /// This property is meant for backends implementing paths. If the backend has a similar |
| 146 | + /// path type built-in (such as `UIBezierPath` or `GskPathBuilder`), constructing the |
| 147 | + /// path should consist of looping over this array and calling the method that corresponds |
| 148 | + /// to each action. |
| 149 | + public private(set) var actions: [Action] = [] |
| 150 | + public private(set) var fillRule: FillRule = .evenOdd |
| 151 | + public private(set) var strokeStyle: StrokeStyle = .none |
| 152 | + |
| 153 | + public init() {} |
| 154 | + |
| 155 | + public consuming func move(to point: SIMD2<Float>) -> Path { |
| 156 | + actions.append(.moveTo(point)) |
| 157 | + return self |
| 158 | + } |
| 159 | + |
| 160 | + public consuming func addLine(to point: SIMD2<Float>) -> Path { |
| 161 | + actions.append(.lineTo(point)) |
| 162 | + return self |
| 163 | + } |
| 164 | + |
| 165 | + public consuming func addQuadCurve( |
| 166 | + control: SIMD2<Float>, |
| 167 | + to endPoint: SIMD2<Float> |
| 168 | + ) -> Path { |
| 169 | + actions.append(.quadCurve(control: control, end: endPoint)) |
| 170 | + return self |
| 171 | + } |
| 172 | + |
| 173 | + public consuming func addCubicCurve( |
| 174 | + control1: SIMD2<Float>, |
| 175 | + control2: SIMD2<Float>, |
| 176 | + to endPoint: SIMD2<Float> |
| 177 | + ) -> Path { |
| 178 | + actions.append(.cubicCurve(control1: control1, control2: control2, end: endPoint)) |
| 179 | + return self |
| 180 | + } |
| 181 | + |
| 182 | + public consuming func addRectangle(_ rect: Rect) -> Path { |
| 183 | + actions.append(.rectangle(rect)) |
| 184 | + return self |
| 185 | + } |
| 186 | + |
| 187 | + public consuming func addCircle(center: SIMD2<Float>, radius: Float) -> Path { |
| 188 | + actions.append(.circle(center: center, radius: radius)) |
| 189 | + return self |
| 190 | + } |
| 191 | + |
| 192 | + public consuming func addArc( |
| 193 | + center: SIMD2<Float>, |
| 194 | + radius: Float, |
| 195 | + startAngle: Float, |
| 196 | + endAngle: Float, |
| 197 | + clockwise: Bool |
| 198 | + ) -> Path { |
| 199 | + actions.append( |
| 200 | + .arc( |
| 201 | + center: center, |
| 202 | + radius: radius, |
| 203 | + startAngle: startAngle, |
| 204 | + endAngle: endAngle, |
| 205 | + clockwise: clockwise |
| 206 | + ) |
| 207 | + ) |
| 208 | + return self |
| 209 | + } |
| 210 | + |
| 211 | + public consuming func transform(_ transform: AffineTransform) -> Path { |
| 212 | + actions.append(.transform(transform)) |
| 213 | + return self |
| 214 | + } |
| 215 | + |
| 216 | + public consuming func stroke(style: StrokeStyle) -> Path { |
| 217 | + strokeStyle = style |
| 218 | + return self |
| 219 | + } |
| 220 | + |
| 221 | + public consuming func fillRule(_ rule: FillRule) -> Path { |
| 222 | + fillRule = rule |
| 223 | + return self |
| 224 | + } |
| 225 | +} |
0 commit comments