forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbols.swift
More file actions
509 lines (468 loc) · 14.9 KB
/
Symbols.swift
File metadata and controls
509 lines (468 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// Symbols.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 23/04/2022.
// Copyright © 2022 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
// MARK: Symbols
typealias Getter = (EvaluationContext) throws -> Value
typealias Setter = (Value, EvaluationContext) throws -> Void
enum Symbol {
case command(ValueType, Setter)
case function(ValueType, (Value, EvaluationContext) throws -> Value)
case property(ValueType, Setter, Getter)
case block(BlockType, Getter)
case constant(Value)
}
extension Symbol {
var errorDescription: String {
switch self {
case .command, .block: return "command"
case .function: return "function"
case .property: return "property"
case .constant: return "constant"
}
}
}
typealias Symbols = [String: Symbol]
// MARK: Types
enum ValueType: CaseIterable {
case color
case texture
case boolean
case colorOrTexture // Hack to support either types
case font
case number
case vector
case size
case rotation
case string
case text
case path
case paths // Hack to support multiple paths
case mesh
case tuple
case point
case pair // Hack to support common math functions
case range
case void
case bounds
}
extension ValueType {
static let any = Set(Self.allCases)
var errorDescription: String {
switch self {
case .color: return "color"
case .texture: return "texture"
case .colorOrTexture: return "color or texture"
case .font: return "font"
case .boolean: return "boolean"
case .number: return "number"
case .vector: return "vector"
case .size: return "size"
case .rotation: return "rotation"
case .string: return "text"
case .text: return "text"
case .path: return "path"
case .paths: return "path"
case .mesh: return "mesh"
case .tuple: return "tuple"
case .point: return "point"
case .pair: return "pair"
case .range: return "range"
case .void: return "void"
case .bounds: return "bounds"
}
}
}
// MARK: Block Types
typealias Options = [String: ValueType]
enum BlockType {
case builder
case shape
case group
case path
case pathShape
case text
case user
indirect case custom(BlockType?, Options)
}
extension BlockType {
var options: Options {
switch self {
case let .custom(baseType, options):
return (baseType?.options ?? [:]).merging(options) { $1 }
case .builder, .group, .path, .shape, .pathShape, .text, .user:
return [:]
}
}
var childTypes: Set<ValueType> {
switch self {
case .builder: return [.path]
case .group: return [.mesh]
case .path: return [.point, .path]
case .text: return [.text]
case .shape, .pathShape, .user: return []
case let .custom(baseType, _):
return baseType?.childTypes ?? []
}
}
var symbols: Symbols {
switch self {
case .shape: return .shape
case .group: return .group
case .builder: return .builder
case .path: return .path
case .pathShape, .text: return .pathShape
case .user: return .user
case let .custom(baseType, _):
return baseType?.symbols ?? .node
}
}
}
// MARK: Values
enum Value {
case color(Color)
case texture(Texture?)
case boolean(Bool)
case number(Double)
case vector(Vector)
case size(Vector)
case rotation(Rotation)
case string(String)
case text(TextValue)
case path(Path)
case mesh(Geometry)
case point(PathPoint)
case tuple([Value])
case range(RangeValue)
case bounds(Bounds)
}
struct RangeValue: Hashable, Sequence {
var start, end, step: Double
init(from start: Double, to end: Double) {
self.init(from: start, to: end, step: 1)!
}
init?(from start: Double, to end: Double, step: Double) {
guard step != 0 else {
return nil
}
self.start = start
self.end = end
self.step = step
}
func makeIterator() -> StrideThrough<Double>.Iterator {
stride(from: start, through: end, by: step).makeIterator()
}
}
struct TextValue: Hashable {
var string: String
var font: String?
var color: Color?
var linespacing: Double?
}
extension Value {
static let void: Value = .tuple([])
static func angle(_ value: Angle) -> Value {
.number(value.radians / .pi)
}
static func colorOrTexture(_ value: MaterialProperty) -> Value {
switch value {
case let .color(color):
return .color(color)
case let .texture(texture):
return .texture(texture)
}
}
var value: AnyHashable {
switch self {
case let .color(color): return color
case let .texture(texture):
return texture.map { $0 as AnyHashable } ?? texture as AnyHashable
case let .boolean(boolean): return boolean
case let .number(number): return number
case let .vector(vector): return vector
case let .size(size): return size
case let .rotation(rotation): return rotation
case let .string(string): return string
case let .text(text): return text
case let .path(path): return path
case let .mesh(mesh): return mesh
case let .point(point): return point
case let .tuple(values) where values.count == 1: return values[0].value
case let .tuple(values): return values.map { $0.value }
case let .range(range): return range
case let .bounds(bounds): return bounds
}
}
var doubleValue: Double {
assert(value is Double)
return value as? Double ?? 0
}
var angleValue: Angle {
.radians(doubleValue * .pi)
}
var intValue: Int {
Int(truncating: doubleValue as NSNumber)
}
var boolValue: Bool {
assert(value is Bool)
return value as? Bool ?? false
}
var stringValue: String {
switch self {
case let .tuple(values):
var spaceNeeded = false
return values.map {
switch $0 {
case let .string(string):
spaceNeeded = false
return string
case let value:
defer { spaceNeeded = true }
let string = value.stringValue
return spaceNeeded ? " \(string)" : string
}
}.joined()
default:
assert(value is Loggable)
return (value as? Loggable)?.logDescription ?? ""
}
}
var textValue: TextValue {
switch self {
case let .text(text):
return text
default:
return TextValue(
string: stringValue,
font: nil,
color: nil,
linespacing: nil
)
}
}
var tupleValue: [AnyHashable] {
if case let .tuple(values) = self {
return values.map { $0.value }
}
return [value]
}
var sequenceValue: AnySequence<Value>? {
switch self {
case let .range(range):
return AnySequence(range.lazy.map { .number($0) })
case let .tuple(values):
if values.count == 1, let value = values.first?.sequenceValue {
return value
}
return AnySequence(values)
case .boolean, .vector, .size, .rotation, .color, .texture,
.number, .string, .text, .path, .mesh, .point, .bounds:
return nil
}
}
var colorValue: Color {
assert(value is Color)
return value as? Color ?? .white
}
var colorOrTextureValue: MaterialProperty? {
switch self {
case let .color(color):
return .color(color)
case let .texture(texture):
return texture.map { .texture($0) }
case .boolean, .vector, .size, .rotation, .range, .tuple,
.number, .string, .text, .path, .mesh, .point, .bounds:
return nil
}
}
var type: ValueType {
switch self {
case .color: return .color
case .texture: return .texture
case .boolean: return .boolean
case .number: return .number
case .vector: return .vector
case .size: return .size
case .rotation: return .rotation
case .string: return .string
case .text: return .text
case .path: return .path
case .mesh: return .mesh
case .point: return .point
case let .tuple(values) where values.count == 1:
// TODO: find better solution for this.
return values[0].type
case .tuple: return .tuple
case .range: return .range
case .bounds: return .bounds
}
}
func isConvertible(to type: ValueType) -> Bool {
switch (self, type) {
case (.boolean, .string),
(.boolean, .text),
(.number, .string),
(.number, .text),
(.number, .color),
(.string, .text):
return true
case let (.tuple(values), .string):
return values.allSatisfy { $0.isConvertible(to: .string) }
case let (.tuple(values), .text):
return values.allSatisfy { $0.isConvertible(to: .text) }
case let (.tuple(values), .color):
if values.count == 1 {
return values[0].isConvertible(to: .color)
}
return values.allSatisfy { $0.type == .number }
case let (.tuple(values), .void):
return values.isEmpty
default:
return self.type == type
}
}
var members: [String] {
switch self {
case .vector, .point:
return ["x", "y", "z"]
case .size:
return ["width", "height", "depth"]
case .rotation:
return ["roll", "yaw", "pitch"]
case .color:
return ["red", "green", "blue", "alpha"]
case let .tuple(values):
var members = Array(String.ordinals(upTo: values.count))
guard values.allSatisfy({ $0.type == .number }) else {
guard values.allSatisfy({ $0.type == .path }) else {
if values.count == 1 {
members += values[0].members
}
return members
}
return members + ["bounds"]
}
if values.count < 5 {
members += ["red", "green", "blue", "alpha"]
if values.count < 4 {
members += [
"x", "y", "z",
"width", "height", "depth",
"roll", "yaw", "pitch",
]
}
}
return members
case .range:
return ["start", "end", "step"]
case .path, .mesh:
return ["bounds"]
case .bounds:
return ["min", "max", "size", "center", "width", "height", "depth"]
case .texture, .boolean, .number, .string, .text:
return []
}
}
subscript(_ name: String) -> Value? {
switch self {
case let .vector(vector):
switch name {
case "x": return .number(vector.x)
case "y": return .number(vector.y)
case "z": return .number(vector.z)
default: return nil
}
case let .point(point):
return Value.vector(point.position)[name]
case let .size(size):
switch name {
case "width": return .number(size.x)
case "height": return .number(size.y)
case "depth": return .number(size.z)
default: return nil
}
case let .rotation(rotation):
switch name {
case "roll": return .number(rotation.roll.radians / .pi)
case "yaw": return .number(rotation.yaw.radians / .pi)
case "pitch": return .number(rotation.pitch.radians / .pi)
default: return nil
}
case let .color(color):
switch name {
case "red": return .number(color.r)
case "green": return .number(color.g)
case "blue": return .number(color.b)
case "alpha": return .number(color.a)
default: return nil
}
case let .tuple(values):
if let index = name.ordinalIndex {
return index < values.count ? values[index] : nil
}
guard values.allSatisfy({ $0.type == .number }) else {
guard values.allSatisfy({ $0.type == .path }) else {
if values.count == 1 {
return values[0][name]
}
return nil
}
let values = values.compactMap { $0.value as? Path }
switch name {
case "bounds":
return .bounds(values.bounds)
default:
return nil
}
}
let values = values.map { $0.doubleValue }
switch name {
case "x", "y", "z":
return values.count < 4 ? Value.vector(Vector(values))[name] : nil
case "width", "height", "depth":
return values.count < 4 ? Value.size(Vector(size: values))[name] : nil
case "roll", "yaw", "pitch":
return Rotation(rollYawPitchInHalfTurns: values).map(Value.rotation)?[name]
case "red", "green", "blue", "alpha":
return Color(values).map(Value.color)?[name]
default:
return nil
}
case let .range(range):
switch name {
case "start": return .number(range.start)
case "end": return .number(range.end)
case "step": return .number(range.step)
default: return nil
}
case let .path(path):
switch name {
case "bounds": return .bounds(path.bounds)
default: return nil
}
case let .mesh(mesh):
switch name {
case "bounds": return .bounds(mesh.bounds)
default: return nil
}
case let .bounds(bounds):
switch name {
case "min": return .vector(bounds.min)
case "max": return .vector(bounds.max)
case "size": return .size(bounds.size)
case "center": return .vector(bounds.center)
case "width": return .number(bounds.size.x)
case "height": return .number(bounds.size.y)
case "depth": return .number(bounds.size.z)
default: return nil
}
case .boolean, .texture, .number, .string, .text:
return nil
}
}
}