Cotton code
286 lines · 12.2 KB · SwiftRaw
// The marks somebody made on the paper. All `Shape`s, no picture.//// The spec's rule: nothing else in the kit is a photograph. A tick, a ruled// line, an underline under a search hit, a ring round a line of a statement, a// goal's progress and a voice note's waveform are all drawings, and drawings// scale, recolour and animate where a photograph does none of the three.//// They are shapes rather than views on purpose — the caller strokes them in// whichever ink the state asks for (`PaperInk.red` for an exception, `.pencil`// for an annotation) and at whichever width the object's size earns.//// `MoodCircles` is NOT here: mood is being redesigned as moments, and drawing// the thing that is about to be replaced is how a kit grows furniture.import SwiftUI/// A hand-drawn tick: a short down-stroke and a long up-stroke, overshooting the/// way a pen does. Drawn in the shape's own unit square, at the theme's/// `marks_tick_*` points.public struct HandTick: Shape {    /// The theme's scale — a shape cannot read the environment, so the view    /// that draws it hands its worn theme over.    public let scale: ThemeScale    public init(theme: Theme) {        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        var path = Path()        path.move(            to: CGPoint(                x: rect.minX + rect.width * MarksKnobs.marksTickStartX,                y: rect.minY + rect.height * MarksKnobs.marksTickStartY))        path.addLine(            to: CGPoint(                x: rect.minX + rect.width * MarksKnobs.marksTickTurnX,                y: rect.minY + rect.height * MarksKnobs.marksTickTurnY))        path.addLine(            to: CGPoint(                x: rect.minX + rect.width * MarksKnobs.marksTickEndX,                y: rect.minY + rect.height * MarksKnobs.marksTickEndY))        return path    }}/// A ruled line drawn by hand — a hairline that wanders by a fraction of a/// point, so a column of them does not read as a table.public struct PencilRule: Shape {    public let seed: String    public let scale: ThemeScale    public init(seed: String = "rule", theme: Theme) {        self.seed = seed        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        let noise = PaperNoise(seed: seed, salt: PaperSalt.rule, count: MarksKnobs.marksRuleNoisePoints)        var path = Path()        let steps = max(MarksKnobs.marksRuleMinSteps, Int(rect.width / MarksKnobs.marksRuleSampleSpacing))        path.move(to: CGPoint(x: rect.minX, y: rect.midY))        for step in 1...steps {            let unit = CGFloat(step) / CGFloat(steps)            let wander = (CGFloat(noise.value(at: Double(unit))) - 0.5) * min(MarksKnobs.marksRuleWanderMax, rect.height)            path.addLine(to: CGPoint(x: rect.minX + rect.width * unit, y: rect.midY + wander))        }        return path    }}/// The stroke under a search hit — two passes of a pencil, the second shorter,/// the way somebody underlines in a hurry.public struct PencilUnderline: Shape {    public let seed: String    public let scale: ThemeScale    public init(seed: String = "underline", theme: Theme) {        self.seed = seed        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        let noise = PaperNoise(            seed: seed, salt: PaperSalt.underline, count: MarksKnobs.marksUnderlineNoisePoints)        var path = Path()        for pass in 0..<MarksKnobs.marksUnderlinePasses {            let inset = rect.width * (pass == 0 ? 0 : MarksKnobs.marksUnderlineSecondInset)            let drop = rect.height * (pass == 0 ? MarksKnobs.marksUnderlineFirstDrop : MarksKnobs.marksUnderlineSecondDrop)            let steps = max(                MarksKnobs.marksUnderlineMinSteps, Int(rect.width / MarksKnobs.marksUnderlineSampleSpacing))            path.move(to: CGPoint(x: rect.minX + inset, y: rect.minY + drop))            for step in 1...steps {                let unit = CGFloat(step) / CGFloat(steps)                let shift = Double(pass) * Double(MarksKnobs.marksUnderlinePassShift)                let wander = (CGFloat(noise.value(at: Double(unit) + shift)) - 0.5)                    * rect.height * MarksKnobs.marksUnderlineWander                path.addLine(                    to: CGPoint(                        x: rect.minX + inset + (rect.width - (inset + inset)) * unit,                        y: rect.minY + drop + wander                    )                )            }        }        return path    }}/// The ring round a line somebody has not read yet — one loop that overshoots/// where it started, because a hand drawing an ellipse never closes it.public struct PencilCircle: Shape {    public let seed: String    public let scale: ThemeScale    public init(seed: String = "circle", theme: Theme) {        self.seed = seed        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        let noise = PaperNoise(seed: seed, salt: PaperSalt.circle, count: MarksKnobs.marksCircleNoisePoints)        var path = Path()        let steps = MarksKnobs.marksCircleSteps        // A little past a full turn, so the ends cross.        let turn = (Double.pi + Double.pi) * Double(MarksKnobs.marksCircleOvershoot)        let start = Double.pi * Double(MarksKnobs.marksCircleStart)        let wobbleRange = Double(MarksKnobs.marksCircleWobble)        for step in 0...steps {            let unit = Double(step) / Double(steps)            let angle = start + turn * unit            let wobble = 1 + (noise.value(at: unit) - 0.5) * wobbleRange            let point = CGPoint(                x: rect.midX + cos(angle) * rect.width * 0.5 * wobble,                y: rect.midY + sin(angle) * rect.height * 0.5 * wobble            )            if step == 0 { path.move(to: point) } else { path.addLine(to: point) }        }        return path    }}/// A star drawn in one stroke — the five-point outline a pen makes without/// lifting, its points a little uneven and its end crossing its start. The/// starred mark on a mail strip (picture 13). Seeded, so a strip's star is the/// same on every launch.public struct HandStar: Shape {    public let seed: String    public let scale: ThemeScale    public init(seed: String = "star", theme: Theme) {        self.seed = seed        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        let noise = PaperNoise(seed: seed, salt: PaperSalt.star, count: MarksKnobs.marksStarNoisePoints)        let radius = min(rect.width, rect.height) * 0.5        let centre = CGPoint(x: rect.midX, y: rect.midY + radius * MarksKnobs.marksStarDrop)        // `marks_star_corners` corners, alternating a point and an inner        // notch, starting at the top point; one more overshoots past the        // first so the ends cross.        let corners = MarksKnobs.marksStarCorners        let overshoot = Double(MarksKnobs.marksStarOvershoot)        let notch = Double(MarksKnobs.marksStarNotch)        let wobbleRange = Double(MarksKnobs.marksStarWobble)        var path = Path()        for corner in 0...corners {            let unit = Double(corner) / Double(corners)            let angle = -Double.pi * 0.5 + (Double.pi + Double.pi) * unit + (corner == corners ? overshoot : 0)            // Even corners are points, odd ones notches.            let reach = corner & 1 == 0 ? 1.0 : notch            let wobble = 1 + (noise.value(at: unit) - 0.5) * wobbleRange            let point = CGPoint(                x: centre.x + cos(angle) * radius * reach * wobble,                y: centre.y + sin(angle) * radius * reach * wobble            )            if corner == 0 { path.move(to: point) } else { path.addLine(to: point) }        }        return path    }}/// A goal's progress: a pencil line drawn along the whole span, and the done/// part drawn over it. `fraction` is 0…1; the shape draws the DONE part, and the/// caller draws the same shape at 1 underneath in pencil.public struct ProgressLine: Shape {    public let fraction: Double    public let seed: String    public let scale: ThemeScale    public init(fraction: Double, seed: String = "progress", theme: Theme) {        self.fraction = min(1, max(0, fraction))        self.seed = seed        self.scale = theme.scale    }    public func path(in rect: CGRect) -> Path {        let noise = PaperNoise(            seed: seed, salt: PaperSalt.progress, count: MarksKnobs.marksProgressNoisePoints)        var path = Path()        let span = rect.width * CGFloat(fraction)        guard span > 0.5 else { return path }        let steps = max(MarksKnobs.marksProgressMinSteps, Int(span / MarksKnobs.marksProgressSampleSpacing))        path.move(to: CGPoint(x: rect.minX, y: rect.midY))        for step in 1...steps {            let unit = CGFloat(step) / CGFloat(steps)            let wander =                (CGFloat(noise.value(at: Double(unit))) - 0.5) * min(MarksKnobs.marksProgressWanderMax, rect.height)            path.addLine(to: CGPoint(x: rect.minX + span * unit, y: rect.midY + wander))        }        return path    }}/// A voice note's waveform — one bar per sample, mirrored about the middle./// `samples` are 0…1; an empty array draws nothing rather than a flat line,/// because a recording with no samples is a recording that has not been read.public struct Waveform: Shape {    public let samples: [Double]    /// How wide a bar is as a fraction of the space one sample gets — the    /// caller's, or the theme's `marks_waveform_bar_fraction`.    public let barFraction: CGFloat    public init(samples: [Double], barFraction: CGFloat? = nil, theme: Theme) {        self.samples = samples        self.barFraction = barFraction ?? MarksKnobs.marksWaveformBarFraction    }    public func path(in rect: CGRect) -> Path {        var path = Path()        guard !samples.isEmpty, rect.width > 1 else { return path }        let pitch = rect.width / CGFloat(samples.count)        let bar = max(1, pitch * barFraction)        for (index, sample) in samples.enumerated() {            let height = max(bar, rect.height * CGFloat(min(1, max(0, sample))))            let x = rect.minX + pitch * CGFloat(index) + (pitch - bar) * 0.5            path.addRoundedRect(                in: CGRect(x: x, y: rect.midY - height * 0.5, width: bar, height: height),                cornerSize: CGSize(width: bar * 0.5, height: bar * 0.5)            )        }        return path    }}/// The sizes, counts and timings Marks draws with: Swift's own, beside/// their reader — a theme sets only its tokens (`scale.tokens`).enum MarksKnobs {    static let marksCircleNoisePoints = 10    static let marksCircleOvershoot: CGFloat = 1.06    static let marksCircleStart: CGFloat = 0.9    static let marksCircleSteps = 64    static let marksCircleWobble: CGFloat = 0.075    static let marksProgressMinSteps = 2    static let marksProgressNoisePoints = 8    static let marksProgressSampleSpacing: CGFloat = 16    static let marksProgressWanderMax: CGFloat = 1.4    static let marksRuleMinSteps = 2    static let marksRuleNoisePoints = 8    static let marksRuleSampleSpacing: CGFloat = 18    static let marksRuleWanderMax: CGFloat = 1.2    static let marksStarCorners = 10    static let marksStarDrop: CGFloat = 0.06    static let marksStarNoisePoints = 10    static let marksStarNotch: CGFloat = 0.45    static let marksStarOvershoot: CGFloat = 0.12    static let marksStarWobble: CGFloat = 0.14    static let marksTickEndX: CGFloat = 0.98    static let marksTickEndY: CGFloat = 0.06    static let marksTickStartX: CGFloat = 0.08    static let marksTickStartY: CGFloat = 0.55    static let marksTickTurnX: CGFloat = 0.36    static let marksTickTurnY: CGFloat = 0.88    static let marksUnderlineFirstDrop: CGFloat = 0.35    static let marksUnderlineMinSteps = 2    static let marksUnderlineNoisePoints = 6    static let marksUnderlinePassShift: CGFloat = 0.37    static let marksUnderlinePasses = 2    static let marksUnderlineSampleSpacing: CGFloat = 14    static let marksUnderlineSecondDrop: CGFloat = 0.7    static let marksUnderlineSecondInset: CGFloat = 0.08    static let marksUnderlineWander: CGFloat = 0.25    static let marksWaveformBarFraction: CGFloat = 0.55}