Ollin

PoemaPlastico

A poem in three invented hands, iron signs on a yellow wall with the sun going over, after Goeritz's Poema plástico (1953).

Run
swift run --package-path Examples Example-Recreations-MathiasGoeritz-PoemaPlastico
Source
Examples/Recreations/MathiasGoeritz/PoemaPlastico/Sketch.swift
//  Recreation after Mathias Goeritz - the Poema plástico (1953), the poem of
//  iron signs he had set on the tall yellow wall of the Museo Experimental
//  El Eco, Sullivan 43, Mexico City. A copy made in 1987 hangs at the school
//  of architecture of the Universidad Nacional Autónoma de México. A homage,
//  not a reproduction, and not affiliated with or endorsed by the artist's
//  estate, the museum, or the university.
//  https://eleco.unam.mx/?p=3324
//  https://www.gaceta.unam.mx/el-poema-plastico-de-mathias-goeritz/
//  https://eleco.unam.mx/storage/2021/06/2017_PUBLICACION_La-disonancia-de-El-eco.pdf
//
//  An original Ollin interpretation, written from published descriptions and
//  from pictures of the work. The form is his: nine rows, in two stanzas of
//  four and a last row alone, in three ways of writing. The count of signs
//  to a row, the spacing of the rows, and the weight of the iron are read
//  off photographs of the wall and are approximate. The signs themselves are
//  this sketch's own, dealt from the seed in his three hands. None is copied
//  from his poem. No source read for this says which way the wall faces, so
//  the sun here is carried round the wall in an arc of its own. The long
//  shadows are the ones in Marianne Gast's photograph of 1953.

import Foundation
import Ollin

/// The *Poema plástico* (Mathias Goeritz, Mexico City, 1953): a poem nobody
/// can read, in iron signs on a yellow wall, with the sun going over.
/// **Move the pointer** to carry the sun.
///
/// He had it placed on one face of the tall yellow wall of El Eco, and
/// called it "una composición visual de tipografía abstracta que se dirige
/// sólo a la sensibilidad del espectador". That is a visual composition of
/// abstract typography, addressed only to the feelings of the person
/// looking. It keeps the shape of a poem. The historian Jennifer Josten
/// counts "nine rows of characters", set as "two four-line stanzas followed
/// by a single line coda", in alphabets he made up.
///
/// The three stanzas are in three hands, as Rita Eder tells them apart.
/// The first is heavy signs, which some have read as the wedge writing of
/// Mesopotamia. The second draws on Arabic calligraphy. The last row is a
/// chain of linked forms. In the photographs the first hand is solid slabs
/// of iron, the second is a line as thin as rod whose rows end in rings
/// and dots, and the third is the same thin line bent into loops. The
/// sketch writes a new poem in those three hands for every seed, and keeps
/// his count of rows.
///
/// The iron is thick, so every sign throws a shadow that starts at its own
/// edge. The sun comes round the left edge of the wall, climbs in front of
/// it, and leaves by the right edge, once every `seconds`. While it is low
/// the shadows are long and the rough plaster shows every bump. Then the
/// wall is in shade until the sun comes round again.
///
/// Try it: a press deals another poem. `depth` is how far the iron stands
/// out from the wall, as a share of a sign's height. `climb` is how high
/// the sun gets, `weight` is how heavy the iron is, and `rough` is how
/// coarse the plaster reads. `--export-svg` gives the poem as outlines, a
/// pattern for whoever makes the iron.
@main
final class PoemaPlastico: Sketch {
    /// How far the iron stands out from the wall, as a share of a sign's
    /// height.
    @Param(0.05 ... 0.6, icon: "square.stack.3d.up") var depth = 0.3
    /// How high the sun climbs in front of the wall, in degrees.
    @Param(15 ... 75, icon: "sun.max") var climb = 48.0
    /// How heavy the iron is: how thick the thin line is, and the strokes
    /// of the solid signs.
    @Param(0.6 ... 1.5, icon: "bold") var weight = 1.0
    /// How coarse the plaster reads when the light is low over it.
    @Param(0 ... 1, icon: "circle.dotted") var rough = 0.6
    /// One crossing of the sun and the shade after it, in seconds.
    @Param(12 ... 30, icon: "timer") var seconds = 24.0

    override var canvasSize: CanvasSize { .square(1080) }
    override var loopDuration: Double? { seconds }

    // MARK: - The page

    // The poem's place on the wall, in pixels. The proportions are read off
    // a photograph of the wall: the rows start on one left edge, the second
    // stanza is the widest, and the last row stands well apart.
    static let left = 276.0
    /// A stanza: where its first row starts, how tall a sign is, how far
    /// apart the rows are, and how wide a full row runs.
    struct Stanza {
        var top: Double
        var tall: Double
        var pitch: Double
        var wide: Double
    }
    static let solid = Stanza(top: 92, tall: 50, pitch: 60, wide: 452)
    static let thin = Stanza(top: 440, tall: 48, pitch: 68, wide: 516)
    static let chain = Stanza(top: 904, tall: 42, pitch: 0, wide: 448)

    /// One piece of iron: its outline on the wall, the height of the signs
    /// in its row, which its depth is a share of, and the thinnest it gets,
    /// in pixels.
    struct Sign {
        var shape: Shape
        var tall: Double
        var thin: Double
    }

    /// One bump of plaster: where it is, and how big.
    struct Bump {
        var at: Vector2
        var size: Double
    }

    private var signs: [Sign] = []
    private let bumps: [Bump] = PoemaPlastico.plaster()
    private var built = ""
    private var dealt = 0
    private var lastMoved = -100.0
    /// How much of the sun the pointer has, 0 to 1.
    private var carried = 0.0

    override func mousePressed() {
        dealt += 1
    }

    // MARK: - The frame

    override func draw() {
        let key = "\(variation)/\(dealt)/\(weight)"
        if key != built {
            signs = PoemaPlastico.deal(seed: variation &+ dealt &* 7919, weight: weight)
            built = key
        }
        if isVectorExporting {
            drawThePlan()
            return
        }

        let sun = sunNow()
        // The wall takes the sky's light all day and the sun's when the sun
        // is in front of it. The eye gets used to the shade, so the picture
        // is lifted a little as the light goes.
        let direct = PoemaPlastico.sunlight * max(0, sun.z)
        let eye = 1 / (0.36 + 0.62 * (0.5 + max(0, sun.z)))
        let lit = PoemaPlastico.paint * (PoemaPlastico.skylight + direct) * eye
        let shaded = PoemaPlastico.paint * PoemaPlastico.skylight * eye
        background(Color(linear: lit))
        drawThePlaster(sun, lit: lit, shaded: shaded)

        // A thick sign on a wall throws a shadow that starts under it and
        // runs away from the sun. The lower the sun is over the wall, the
        // farther it runs. It is the sign's own outline dragged along the
        // wall, so it is laid here as the outline drawn again and again, in
        // steps half as long as the thinnest part of the sign is wide.
        // Shadows that cross are no darker than one. With no sun, the sky
        // still leaves a little shade under each sign.
        noStroke()
        blendMode(.darkest)
        fill(Color(linear: shaded * 0.84))
        for sign in signs {
            withState {
                translate(0.7, 1.5)
                drawShape(sign.shape)
            }
        }
        if sun.z > 0.002 {
            for sign in signs {
                // A shadow that would run longer than a few signs is the sun
                // nearly level with the wall, when it has little light left
                // to block. Such a shadow is held to that length and drawn
                // fainter the farther past it would run. At twice the
                // length it is gone.
                let wanted = depth * sign.tall / sun.z, limit = PoemaPlastico.longest * sign.tall
                let faint = max(0, min(1, wanted / limit - 1))
                guard faint < 1 else { continue }
                fill(Color(linear: shaded + (lit - shaded) * faint))
                let run = Vector2(-sun.x, sun.y) * min(wanted, limit)
                let far = run.length
                let steps = max(1, Int((far / (0.5 * sign.thin)).rounded(.up)))
                for k in 1 ... steps {
                    withState {
                        translate(run * (Double(k) / Double(steps)))
                        drawShape(sign.shape)
                    }
                }
            }
        }
        blendMode(.normal)

        fill(Color(linear: PoemaPlastico.iron * (PoemaPlastico.skylight + direct) * eye))
        for sign in signs { drawShape(sign.shape) }
    }

    /// The longest shadow drawn, in sign heights.
    static let longest = 3.0
    private static let paint = SIMD3<Double>(0.86, 0.66, 0.045)
    private static let iron = SIMD3<Double>(0.03, 0.03, 0.028)
    private static let sunlight = SIMD3<Double>(1.0, 0.95, 0.86)
    private static let skylight = SIMD3<Double>(0.46, 0.5, 0.56)

    // MARK: - The sun

    /// How far round the lap the clock is, 0 to 1.
    private var lap: Double {
        let t = time / seconds
        return t - t.rounded(.down)
    }

    /// The direction from the wall toward the sun: x to the right along the
    /// wall, y up it, and z straight out of it. When z is 0 or less the sun
    /// is behind the wall and the wall is in shade.
    private func sunNow() -> SIMD3<Double> {
        // The sun is in front of the wall for most of the lap and behind it
        // for the rest, so the lap begins and ends in the same shade.
        var sun = SIMD3<Double>(0, 0, 0)
        let day = (lap - 0.06) / 0.88
        if day > 0, day < 1 {
            let turn = Double.pi * day, high = climb * Double.pi / 180
            sun = SIMD3<Double>(-cos(turn), sin(turn) * sin(high), sin(turn) * cos(high))
        }

        // The pointer takes the sun while it moves, and gives it back a few
        // seconds after it stops. It is where the sun is, on a dome in front
        // of the wall: at the middle the sun shines straight on, and toward
        // an edge it comes in low from that side.
        if mouse != previousMouse { lastMoved = time }
        let wanted = time - lastMoved < 4 ? 1.0 : 0.0
        carried += (wanted - carried) * (1 - exp(-deltaTime * 4))
        guard carried > 0.001 else { return sun }
        let reach = 0.56 * width
        var x = (mouse.x - width / 2) / reach, y = (height / 2 - mouse.y) / reach
        let far = (x * x + y * y).squareRoot()
        if far > 0.995 {
            x *= 0.995 / far
            y *= 0.995 / far
        }
        let held = SIMD3<Double>(x, y, (1 - x * x - y * y).squareRoot())
        return sun + (held - sun) * carried
    }

    // MARK: - The plaster

    /// The wall was plastered rough, and the trowel left it all bumps. They
    /// lie in loose swirls, the way the plasterer's arm moved. The bumps are
    /// the same for every poem.
    static func plaster() -> [Bump] {
        var rng = SplitMix64(seed: 1953)
        func roll(_ low: Double, _ high: Double) -> Double { Double.random(in: low ... high, using: &rng) }
        var bumps: [Bump] = []
        for _ in 0 ..< 3600 {
            // A stroke of the trowel leaves a few bumps close together,
            // most of them small.
            let at = Vector2(roll(-20, 1100), roll(-20, 1100))
            let swirl = sin(at.x * 0.011 + 1.3) * 1.2 + cos(at.y * 0.013 - 0.4) * 1.4 + sin((at.x + at.y) * 0.006) * 1.1
            let angle = swirl + roll(-0.8, 0.8)
            let along = Vector2(cos(angle), sin(angle))
            for _ in 0 ..< 1 + Int(roll(0, 3.99)) {
                let size = 0.8 + 3.0 * pow(roll(0, 1), 2.6)
                bumps.append(Bump(at: at + along * roll(-7, 7) + along.perpendicular * roll(-2.5, 2.5), size: size))
            }
        }
        return bumps
    }

    /// Every bump has a side that faces the light and throws a small shadow
    /// away from it. The lower the sun, the longer that shadow. With no sun
    /// the sky overhead still picks the bumps out a little.
    private func drawThePlaster(_ sun: SIMD3<Double>, lit: SIMD3<Double>, shaded: SIMD3<Double>) {
        guard rough > 0 else { return }
        var toward = Vector2(0, -1)
        var strength = 0.32, reach = 0.25
        if sun.z > 0 {
            let flat = (sun.x * sun.x + sun.y * sun.y).squareRoot()
            if flat > 1e-6 { toward = Vector2(sun.x, -sun.y) / flat }
            // How much of the light is the sun's, and how low it comes in.
            let share = min(1, sun.z * 5)
            strength = 0.32 + 0.68 * share * flat
            reach = 0.25 + share * min(2.2, 0.55 * flat / max(sun.z, 0.05))
        }
        var bright: [Circle] = [], dark: [Circle] = []
        bright.reserveCapacity(bumps.count)
        dark.reserveCapacity(bumps.count)
        for bump in bumps {
            bright.append(Circle(center: bump.at + toward * (0.35 * bump.size), radius: 0.8 * bump.size))
            dark.append(Circle(center: bump.at - toward * ((0.4 + reach) * bump.size), radius: 0.9 * bump.size))
        }
        noStroke()
        fill(Color(linear: shaded * 0.8, alpha: 0.42 * rough * strength))
        drawCircles(dark)
        fill(Color(linear: lit * 1.28, alpha: 0.34 * rough * strength))
        drawCircles(bright)
    }

    // MARK: - Writing the poem

    /// Deal a poem: four rows of solid signs, four rows of thin line, and a
    /// row of chain. Everything comes from the seed.
    static func deal(seed: Int, weight: Double) -> [Sign] {
        var rng = SplitMix64(seed: UInt64(bitPattern: Int64(seed &* 1_000_003 &+ 1953)))
        func roll(_ low: Double, _ high: Double) -> Double { Double.random(in: low ... high, using: &rng) }
        func pick(_ count: Int) -> Int { Int.random(in: 0 ..< count, using: &rng) }
        func chance(_ odds: Double) -> Bool { roll(0, 1) < odds }
        /// A point set down by hand: a little off where it was meant.
        func hand(_ x: Double, _ y: Double, _ loose: Double = 0.035) -> Vector2 {
            Vector2(x + roll(-loose, loose), y + roll(-loose, loose))
        }
        func ring(_ center: Vector2, _ rx: Double, _ ry: Double, wobble: Double = 0.06, points: Int = 18) -> [Vector2] {
            let turn = roll(0, 6.28)
            return (0 ..< points).map { k in
                let a = 2 * Double.pi * Double(k) / Double(points)
                let r = 1 + wobble * sin(2 * a + turn) + roll(-wobble, wobble) * 0.4
                return center + Vector2(cos(a) * rx, sin(a) * ry) * r
            }
        }
        /// A stroke with a thickness that changes along it: its middle line
        /// and half its width at each point.
        typealias Stroke = (line: [Vector2], half: [Double])
        /// The outline of a stroke, made `heavy` times as thick.
        func brush(_ stroke: Stroke, heavy: Double) -> [Vector2] {
            let line = stroke.line, half = stroke.half
            var near: [Vector2] = [], far: [Vector2] = []
            for i in line.indices {
                let a = line[max(i - 1, 0)], b = line[min(i + 1, line.count - 1)]
                let square = (b - a).normalized.perpendicular * (half[i] * heavy)
                near.append(line[i] + square)
                far.append(line[i] - square)
            }
            return near + far.reversed()
        }

        var signs: [Sign] = []

        // The first hand: solid signs. Each is drawn in a box one unit
        // tall, with y down. An outline given as `cut` keeps its corners,
        // eased off a little, like a shape cut from a sheet. One given as
        // `brushed` flows, and so does a stroke.
        struct Plate {
            var cut: [[Vector2]] = []
            var brushed: [[Vector2]] = []
            var strokes: [Stroke] = []
            var holes: [[Vector2]] = []
            /// Small signs keep their size. The rest fill the row.
            var small = false
        }
        func mirror(_ points: [Vector2], _ wide: Double) -> [Vector2] { points.map { Vector2(wide - $0.x, $0.y) } }
        let forms: [() -> Plate] = [
            { // a long wedge
                let wide = roll(1.6, 2.3)
                let points = [hand(0, 0.22), hand(wide, 0.62), hand(0.1, 1.0)]
                return Plate(cut: [chance(0.5) ? mirror(points, wide) : points])
            },
            { Plate(cut: [[hand(0, 0.05), hand(1.05, 0.22), hand(0.86, 0.98), hand(0.2, 0.9)]]) },
            { // a stem with a belly
                let points = [hand(0, 0), hand(0.3, 0), hand(0.32, 0.44), hand(0.74, 0.5), hand(0.84, 1), hand(0, 1)]
                return Plate(cut: [chance(0.4) ? mirror(points, 0.84) : points])
            },
            { Plate(strokes: [([hand(0.12, 0.0), hand(0.14, 0.62), hand(0.5, 0.62), hand(0.92, 0.48), hand(0.96, 1.0)],
                               [0.14, 0.16, 0.15, 0.14, 0.11])]) },
            { // a blob with an eye
                let center = Vector2(0.62, 0.5)
                return Plate(brushed: [ring(center, 0.62, 0.5, wobble: 0.1)],
                             holes: [ring(center + hand(-0.2, -0.1), 0.1, 0.045, points: 8)])
            },
            { // a blob with a tail
                let body = ring(Vector2(0.52, 0.42), 0.52, 0.42, wobble: 0.08)
                let tail: Stroke = ([hand(0.86, 0.4), hand(0.98, 0.72), hand(0.8, 1.02)], [0.16, 0.13, 0.08])
                return Plate(brushed: [body], strokes: [tail], holes: [ring(Vector2(0.36, 0.36), 0.1, 0.045, points: 8)])
            },
            { // a crescent
                let open = roll(-0.4, 0.4)
                let line = (0 ... 8).map { k -> Vector2 in
                    let a = open + 0.7 + 4.9 * Double(k) / 8
                    return Vector2(0.5 + 0.4 * cos(a), 0.5 + 0.4 * sin(a))
                }
                return Plate(strokes: [(line, [0.07, 0.12, 0.17, 0.19, 0.19, 0.18, 0.15, 0.11, 0.07])])
            },
            { Plate(strokes: [([hand(0, 1.0), hand(0.55, 0.12), hand(1.1, 1.0)], [0.09, 0.17, 0.09])]) },
            { Plate(cut: [[hand(0, 0.2), hand(0.26, 0.5), hand(0.52, 0.12), hand(0.78, 0.5), hand(1.24, 0.1),
                           hand(1.38, 0.5), hand(1.0, 0.98), hand(0.3, 1.0), hand(0, 0.6)]]) },
            { // a cup, sometimes with a dot in it
                let cup: Stroke = ([hand(0.02, 0.05), hand(0.06, 0.68), hand(0.46, 0.96), hand(0.86, 0.72), hand(1.0, 0.1)],
                                   [0.11, 0.17, 0.18, 0.16, 0.1])
                return Plate(brushed: chance(0.5) ? [ring(Vector2(0.5, 0.36), 0.12, 0.12, points: 10)] : [], strokes: [cup])
            },
            { Plate(cut: [[hand(0, 0.08), hand(0.55, 0.36), hand(1.3, 0.0), hand(1.16, 0.52), hand(1.36, 1.0),
                           hand(0.6, 0.68), hand(0.1, 1.0), hand(0.24, 0.5)]], holes: [ring(Vector2(0.5, 0.52), 0.07, 0.06, points: 8)]) },
            { // a short stem and a tall one
                Plate(cut: [[hand(0, 0.4), hand(0.22, 0.42), hand(0.22, 0.96), hand(0.02, 0.94)],
                            [hand(0.36, 0.0), hand(0.62, 0.02), hand(0.64, 1.0), hand(0.36, 0.98)]])
            },
            { Plate(cut: [[hand(0.36, 0.0), hand(0.7, 0.4), hand(0.2, 0.42)],
                          [hand(0, 0.54), hand(0.92, 0.52), hand(0.98, 1), hand(0, 0.98)]]) },
            { Plate(brushed: [ring(Vector2(0.46, 0.5), 0.46, 0.5, wobble: 0.1)]) },
            { Plate(strokes: [([hand(0, 0.3), hand(0.32, 0.08), hand(0.62, 0.26), hand(0.56, 0.58), hand(0.92, 0.86), hand(1.24, 0.78)],
                               [0.11, 0.15, 0.16, 0.15, 0.16, 0.11])]) },
            { Plate(cut: [[hand(0, 0.0), hand(0.22, 0.46), hand(1.3, 0.4), hand(0.78, 0.72), hand(0.62, 1.04),
                           hand(0.5, 0.7), hand(0, 0.76)]]) },
            { // a hook with a foot
                Plate(strokes: [([hand(0.7, 0.3), hand(0.4, 0.06), hand(0.12, 0.3), hand(0.14, 0.98)], [0.12, 0.17, 0.17, 0.14]),
                                ([hand(0.5, 0.96), hand(0.9, 0.6)], [0.09, 0.1])])
            },
            // The small signs: a dot, a bar, and a slanted stroke.
            { Plate(brushed: [ring(Vector2(0.15, roll(0.4, 0.75)), 0.15, 0.15, points: 10)], small: true) },
            { Plate(cut: [[hand(0, 0.5), hand(0.58, 0.47), hand(0.6, 0.7), hand(0.02, 0.73)]], small: true) },
            { Plate(strokes: [([hand(0, 0.92), hand(0.32, 0.42)], [0.09, 0.1])], small: true) },
        ]
        let small = 3
        for row in 0 ..< 4 {
            let unit = solid.tall
            let full = solid.wide / unit * roll(0.86, 1.0)
            var x = roll(0, 0.3)
            var last = -1, lastSmall = true
            while x < full {
                // About one sign in six is a small one, and never two in a row.
                var which = !lastSmall && chance(0.17) ? forms.count - 1 - pick(small) : pick(forms.count - small)
                while which == last { which = pick(forms.count - small) }
                last = which
                let plate = forms[which]()
                lastSmall = plate.small
                // Fit the sign to the row's height, and stand it on the row.
                // It is measured at its plain weight, so a heavier or a
                // lighter hand writes the same poem in the same places.
                let all = (plate.cut + plate.brushed + plate.strokes.map { brush($0, heavy: 1) }).flatMap { $0 }
                let minX = all.map(\.x).min() ?? 0, maxX = all.map(\.x).max() ?? 1
                let minY = all.map(\.y).min() ?? 0, maxY = all.map(\.y).max() ?? 1
                let size = plate.small ? 1.0 : roll(0.86, 1.0) / max(maxY - minY, 0.2)
                let lift = plate.small ? 0.0 : 1 - (maxY - minY) * size + roll(-0.05, 0.03)
                let origin = Vector2(left + x * unit, solid.top + Double(row) * solid.pitch)
                func place(_ points: [Vector2]) -> [Vector2] {
                    points.map { origin + Vector2(($0.x - minX) * size, plate.small ? $0.y : ($0.y - minY) * size + lift) * unit }
                }
                var contours = plate.cut.map { Contour(place($0), closed: true).rounded(0.07 * unit) }
                contours += (plate.brushed + plate.strokes.map { brush($0, heavy: weight) })
                    .map { Contour(place($0), closed: true).smoothed(iterations: 3) }
                var shape = Shape(contours: [contours[0]], winding: .nonZero)
                for more in contours.dropFirst() { shape = shape.union(Shape(contours: [more], winding: .nonZero)) }
                for hole in plate.holes {
                    shape = shape.subtracting(Shape(contours: [Contour(place(hole), closed: true).smoothed(iterations: 2)], winding: .nonZero))
                }
                signs.append(Sign(shape: shape, tall: unit, thin: 0.11 * unit))
                x += (maxX - minX) * size + roll(0.16, 0.3)
            }
        }

        // The second hand: a thin line. A word is a few open strokes, with rings
        // and dots, drawn on a line 0.62 of the way down its box. The rows
        // take turns: one that curls, then one struck straight and angular.
        let line = 0.62
        struct Word {
            var strokes: [[Vector2]] = []
            var loops: [[Vector2]] = []
            var solids: [[Vector2]] = []
            var wide = 0.0
        }
        func curled() -> Word {
            var word = Word()
            var x = 0.0
            var pen: [Vector2] = []
            if chance(0.45) {
                word.loops.append(ring(Vector2(0.2, line), 0.2, 0.2, wobble: 0.05, points: 14))
                x = 0.4
            }
            pen.append(hand(x, line, 0.02))
            for _ in 0 ..< 3 + pick(3) {
                switch pick(8) {
                case 0, 1: // a loop over the line
                    pen += [hand(x + 0.25, line), hand(x + 0.52, line - 0.1), hand(x + 0.58, line - 0.5), hand(x + 0.3, line - 0.62),
                            hand(x + 0.12, line - 0.34), hand(x + 0.36, line - 0.02), hand(x + 0.72, line)]
                    x += 0.72
                case 2: // a hump
                    pen += [hand(x + 0.1, line), hand(x + 0.2, line - 0.5), hand(x + 0.48, line - 0.5), hand(x + 0.52, line), hand(x + 0.64, line)]
                    x += 0.64
                case 3: // a spike
                    pen += [hand(x + 0.14, line), hand(x + 0.2, line - 0.62), hand(x + 0.27, line), hand(x + 0.4, line)]
                    x += 0.4
                case 4: // a tail that drops and hooks back, and the line goes on
                    pen += [hand(x + 0.2, line), hand(x + 0.38, line + 0.3), hand(x + 0.16, line + 0.44), hand(x - 0.06, line + 0.3)]
                    word.strokes.append(pen)
                    pen = [hand(x + 0.44, line, 0.02)]
                    x += 0.44
                case 5: // a long tail swept back under the word
                    pen += [hand(x + 0.2, line), hand(x + 0.34, line + 0.26), hand(x - 0.1, line + 0.46), hand(x - 0.6, line + 0.36)]
                    word.strokes.append(pen)
                    pen = [hand(x + 0.42, line, 0.02)]
                    x += 0.42
                case 6: // a small ring threaded on the line
                    pen += [hand(x + 0.2, line)]
                    word.loops.append(ring(Vector2(x + 0.34, line - 0.02), 0.14, 0.14, wobble: 0.05, points: 12))
                    word.strokes.append(pen)
                    pen = [hand(x + 0.48, line, 0.02)]
                    x += 0.48
                default: // a wave
                    pen += [hand(x + 0.3, line - 0.1), hand(x + 0.6, line + 0.05)]
                    x += 0.6
                }
            }
            // The word ends in a curl or an upstroke.
            if chance(0.6) {
                pen += [hand(x + 0.3, line - 0.05), hand(x + 0.55, line - 0.3), hand(x + 0.4, line - 0.5),
                        hand(x + 0.25, line - 0.36), hand(x + 0.38, line - 0.22)]
                x += 0.6
            } else {
                pen += [hand(x + 0.22, line), hand(x + 0.3, line - 0.3)]
                x += 0.34
            }
            word.strokes.append(pen)
            // A stroke or two across it, and sometimes a dot over it.
            for _ in 0 ..< pick(3) {
                let at = roll(0.3, max(0.4, x - 0.3))
                word.strokes.append([hand(at, line - roll(0.3, 0.56)), hand(at + 0.02, line + roll(0.06, 0.16))])
            }
            if chance(0.4) { word.solids.append(ring(Vector2(roll(0.2, max(0.3, x - 0.2)), line - 0.62), 0.07, 0.07, points: 8)) }
            word.strokes = word.strokes.map { Contour($0, closed: false).smoothed(iterations: 3).points }
            word.wide = x
            return word
        }
        func struck(atMost: Double) -> Word {
            var word = Word()
            let wide = min(roll(2.0, 4.2), atMost)
            // The strike: level, with a square step up or down on the way.
            var strike = [hand(0, line, 0.015)]
            var level = line, x = 0.0
            while x < wide - 0.9 {
                x += roll(0.6, 1.3)
                if chance(0.55) {
                    let step = (chance(0.5) ? -1.0 : 1.0) * roll(0.12, 0.24)
                    strike += [Vector2(x, level), Vector2(x, level + step)]
                    level += step
                    if abs(level - line) > 0.26 { level = line + (level > line ? 0.1 : -0.1) }
                }
            }
            strike.append(Vector2(wide, level))
            word.strokes.append(strike)
            if chance(0.5) { word.solids.append([Vector2(-0.04, line), Vector2(0.3, line - 0.15), Vector2(0.3, line + 0.15)]) }
            // What stands on the strike: spikes, wedges, and short crossings.
            var at = roll(0.4, 0.8)
            while at < wide - 0.3 {
                switch pick(5) {
                case 0: // a tall spike, drawn as two strokes that meet
                    word.strokes.append([hand(at - 0.14, line + 0.04, 0.02), hand(at, line - 0.74), hand(at + 0.16, line + 0.04, 0.02)])
                case 1: // a wedge lying along the strike
                    let long = roll(0.7, 1.3)
                    word.solids.append([hand(at, line + 0.02, 0.02), hand(at + long, line + 0.06, 0.02), hand(at + long, line + 0.3)])
                    at += long * 0.6
                case 2: // two or three short crossings
                    for k in 0 ..< 2 + pick(2) {
                        let cross = at + Double(k) * 0.13
                        word.strokes.append([Vector2(cross, line - roll(0.2, 0.34)), Vector2(cross, line + roll(0.1, 0.2))])
                    }
                case 3: // a triangle through the strike, drawn in outline
                    word.loops.append([hand(at, line - 0.34, 0.02), hand(at, line + 0.38, 0.02), hand(at + roll(0.5, 1.0), line, 0.02)])
                default: // a small flag
                    word.solids.append([hand(at, line, 0.02), hand(at + 0.24, line - 0.36, 0.02), hand(at + 0.3, line, 0.02)])
                }
                at += roll(0.5, 1.1)
            }
            if chance(0.5) { word.strokes.append([Vector2(wide, level), hand(wide + 0.1, level - 0.26, 0.02)]) }
            word.wide = wide + 0.1
            return word
        }
        for row in 0 ..< 4 {
            let unit = thin.tall
            let origin = Vector2(left, thin.top + Double(row) * thin.pitch)
            let full = thin.wide / unit
            var strokes: [Contour] = [], loops: [Contour] = [], solids: [Contour] = []
            var x = roll(0, 0.25)
            func set(_ word: Word) {
                let at = origin + Vector2(x * unit, 0)
                strokes += word.strokes.map { Contour($0.map { at + $0 * unit }, closed: false) }
                loops += word.loops.map { Contour($0.map { at + $0 * unit }, closed: true) }
                solids += word.solids.map { Contour($0.map { at + $0 * unit }, closed: true) }
                x += word.wide
            }
            // Leave room at the end of the row for its rings and dots.
            let room = full - 1.3
            while x < room * roll(0.9, 1.0) {
                // A word that would run past the row's end is not set. A
                // struck word is made to fit instead, if there is room.
                if row % 2 == 1 {
                    if room - x < 1.3 { break }
                    set(struck(atMost: room - x))
                } else {
                    let word = curled()
                    if x + word.wide > room + 0.4 { break }
                    set(word)
                }
                x += roll(0.3, 0.5)
            }
            // The row's end: a dot, a ring and a dot, or a dot on each side
            // of a ring.
            let stops = [[false], [true, false], [false, true, false]][pick(3)]
            for stop in stops {
                var mark = Word()
                if stop {
                    mark.loops.append(ring(Vector2(0.22, line + 0.02), 0.2, 0.2, wobble: 0.04, points: 14))
                    mark.wide = 0.56
                } else {
                    mark.solids.append(ring(Vector2(0.09, line + 0.2), 0.085, 0.085, points: 10))
                    mark.wide = 0.3
                }
                set(mark)
            }
            let thick = 0.13 * unit * weight
            var iron = Shape(contours: strokes + loops, winding: .nonZero).stroked(width: thick, join: .round, cap: .round)
            if !solids.isEmpty {
                iron = iron.union(Shape(contours: solids, winding: .nonZero).smoothed(iterations: 1))
            }
            signs.append(Sign(shape: iron, tall: unit, thin: thick))
        }

        // The third hand: one row of the thin line bent into rings and
        // links, each hooked through the one before it or standing just
        // clear of it.
        do {
            let unit = chain.tall
            let origin = Vector2(left, chain.top)
            let full = chain.wide / unit
            var strokes: [Contour] = [], loops: [Contour] = []
            var x = roll(0, 0.2)
            func link(_ points: [Vector2], rounds: Int) {
                loops.append(Contour(points.map { origin + $0 * unit }, closed: true).smoothed(iterations: rounds))
            }
            var slashed = false, links = 0
            while x < full - 0.5 {
                var wide = 0.0
                links += 1
                switch pick(7) {
                case 0, 1: // a link with square shoulders
                    wide = roll(0.9, 1.5)
                    let high = roll(0.56, 0.72)
                    let top = 0.5 - high / 2, low = 0.5 + high / 2
                    link([hand(x, top), hand(x + wide, top), hand(x + wide, low), hand(x, low)], rounds: 3)
                case 2: // a ring
                    wide = roll(0.6, 0.86)
                    link(ring(Vector2(x + wide / 2, 0.5), wide / 2, wide / 2, wobble: 0.05, points: 16), rounds: 1)
                case 3: // a small ring
                    wide = roll(0.3, 0.38)
                    link(ring(Vector2(x + wide / 2, roll(0.45, 0.6)), wide / 2, wide / 2, wobble: 0.05, points: 12), rounds: 1)
                case 4: // a long link with a ring through one end
                    wide = roll(1.7, 2.3)
                    link([hand(x, 0.3), hand(x + wide, 0.34), hand(x + wide, 0.7), hand(x, 0.72)], rounds: 3)
                    link(ring(Vector2(x + 0.26, 0.42), 0.2, 0.2, wobble: 0.05, points: 12), rounds: 1)
                case 5: // a loose loop, bent by eye
                    wide = roll(0.7, 1.1)
                    link(ring(Vector2(x + wide / 2, 0.5), wide / 2, roll(0.3, 0.4), wobble: 0.2, points: 12), rounds: 2)
                default: // a bar leaning across the row, once, and not first
                    if slashed || links < 3 { continue }
                    slashed = true
                    wide = 0.5
                    strokes.append(Contour([origin + hand(x, 0.98, 0.02) * unit, origin + hand(x + wide, 0.0, 0.02) * unit], closed: false))
                }
                // Hooked through the next, or a little clear of it.
                x += wide + (chance(0.62) ? -roll(0.12, 0.3) : roll(0.12, 0.26))
            }
            let thick = 0.15 * unit * weight
            signs.append(Sign(shape: Shape(contours: strokes + loops, winding: .nonZero).stroked(width: thick, join: .round, cap: .round),
                              tall: unit, thin: thick))
        }
        return signs
    }

    // MARK: - The plan

    /// The poem as outlines, black on white: a pattern for the iron.
    private func drawThePlan() {
        background(.white)
        noFill()
        stroke(.black)
        strokeWeight(0.6)
        for sign in signs { drawShape(sign.shape) }
    }
}

↑↓ move ↩ open