// 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.importSwiftUI/// 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.publicstructHandTick: Shape {/// The theme's scale — a shape cannot read the environment, so the view/// that draws it hands its worn theme over.publiclet scale: ThemeScalepublicinit(theme: Theme) {self.scale = theme.scale }publicfunc 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.publicstructPencilRule: Shape {publiclet seed: Stringpubliclet scale: ThemeScalepublicinit(seed: String = "rule", theme: Theme) {self.seed = seedself.scale = theme.scale }publicfunc 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 in1...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.publicstructPencilUnderline: Shape {publiclet seed: Stringpubliclet scale: ThemeScalepublicinit(seed: String = "underline", theme: Theme) {self.seed = seedself.scale = theme.scale }publicfunc path(in rect: CGRect) -> Path {let noise = PaperNoise( seed: seed, salt: PaperSalt.underline, count: MarksKnobs.marksUnderlineNoisePoints)var path = Path()for pass in0..<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 in1...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.publicstructPencilCircle: Shape {publiclet seed: Stringpubliclet scale: ThemeScalepublicinit(seed: String = "circle", theme: Theme) {self.seed = seedself.scale = theme.scale }publicfunc 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 in0...steps {let unit = Double(step) / Double(steps)let angle = start + turn * unitlet wobble = 1 + (noise.value(at: unit) - 0.5) * wobbleRangelet 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.publicstructHandStar: Shape {publiclet seed: Stringpubliclet scale: ThemeScalepublicinit(seed: String = "star", theme: Theme) {self.seed = seedself.scale = theme.scale }publicfunc path(in rect: CGRect) -> Path {let noise = PaperNoise(seed: seed, salt: PaperSalt.star, count: MarksKnobs.marksStarNoisePoints)let radius = min(rect.width, rect.height) * 0.5let 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.marksStarCornerslet overshoot = Double(MarksKnobs.marksStarOvershoot)let notch = Double(MarksKnobs.marksStarNotch)let wobbleRange = Double(MarksKnobs.marksStarWobble)var path = Path()for corner in0...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 : notchlet wobble = 1 + (noise.value(at: unit) - 0.5) * wobbleRangelet 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.publicstructProgressLine: Shape {publiclet fraction: Doublepubliclet seed: Stringpubliclet scale: ThemeScalepublicinit(fraction: Double, seed: String = "progress", theme: Theme) {self.fraction = min(1, max(0, fraction))self.seed = seedself.scale = theme.scale }publicfunc 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.5else { return path }let steps = max(MarksKnobs.marksProgressMinSteps, Int(span / MarksKnobs.marksProgressSampleSpacing)) path.move(to: CGPoint(x: rect.minX, y: rect.midY))for step in1...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.publicstructWaveform: Shape {publiclet 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`.publiclet barFraction: CGFloatpublicinit(samples: [Double], barFraction: CGFloat? = nil, theme: Theme) {self.samples = samplesself.barFraction = barFraction ?? MarksKnobs.marksWaveformBarFraction }publicfunc path(in rect: CGRect) -> Path {var path = Path()guard !samples.isEmpty, rect.width > 1else { 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`).enumMarksKnobs {staticlet marksCircleNoisePoints = 10staticlet marksCircleOvershoot: CGFloat = 1.06staticlet marksCircleStart: CGFloat = 0.9staticlet marksCircleSteps = 64staticlet marksCircleWobble: CGFloat = 0.075staticlet marksProgressMinSteps = 2staticlet marksProgressNoisePoints = 8staticlet marksProgressSampleSpacing: CGFloat = 16staticlet marksProgressWanderMax: CGFloat = 1.4staticlet marksRuleMinSteps = 2staticlet marksRuleNoisePoints = 8staticlet marksRuleSampleSpacing: CGFloat = 18staticlet marksRuleWanderMax: CGFloat = 1.2staticlet marksStarCorners = 10staticlet marksStarDrop: CGFloat = 0.06staticlet marksStarNoisePoints = 10staticlet marksStarNotch: CGFloat = 0.45staticlet marksStarOvershoot: CGFloat = 0.12staticlet marksStarWobble: CGFloat = 0.14staticlet marksTickEndX: CGFloat = 0.98staticlet marksTickEndY: CGFloat = 0.06staticlet marksTickStartX: CGFloat = 0.08staticlet marksTickStartY: CGFloat = 0.55staticlet marksTickTurnX: CGFloat = 0.36staticlet marksTickTurnY: CGFloat = 0.88staticlet marksUnderlineFirstDrop: CGFloat = 0.35staticlet marksUnderlineMinSteps = 2staticlet marksUnderlineNoisePoints = 6staticlet marksUnderlinePassShift: CGFloat = 0.37staticlet marksUnderlinePasses = 2staticlet marksUnderlineSampleSpacing: CGFloat = 14staticlet marksUnderlineSecondDrop: CGFloat = 0.7staticlet marksUnderlineSecondInset: CGFloat = 0.08staticlet marksUnderlineWander: CGFloat = 0.25staticlet marksWaveformBarFraction: CGFloat = 0.55}