// The JOURNAL PAGE as a DIARY lying open (12 Nov 2026): a navy cloth-bound// diary on the journal's bedside table (`FaceGround.bedsideTable`), its left// half folded back under it, so one cream page faces up inside the cloth with// the spine's round on its left, the page block's edges right and below, and// the strap lying out to the right with its small brass lock. The book, the// ribbon marker and the wax seals are PAINTED (`Media.xcassets`, painted by// `scripts/paint-diary.py`); the face draws live only what the card decides://// the DATE printed in the page's head in Caslon, as the core says the day// (`weekday_date`), and the day's WEATHER as a small glyph stamped after it.// the PLACE written by hand on the line under the date.// the SEAL in the page's top corner: the mood pressed in its wax — gold for// great, green for good, blue-grey for okay, indigo for low, oxblood for// awful — and, for a page with no mood, a plain unpressed disc.// the TITLE written by hand on the first rules, one line or two; a page// with no title writes its first sentence as one, as a note's page does.// the WORDS written by hand on the rules after it, as many whole lines as// the page holds, stopping at the last whole word that fits: the object// shows the opening, the reader the rest.// the RIBBON: tucked in, only its tail peeps out at the foot; pulled out// down the page by the spine and over the foot onto the table when the// page is starred.//// The date, the place, the title and the words are their parts; the seal and// the weather are theirs too, and while the card is held a tap on either// turns the fan into its choices (the core's tap map). Stepped by a tap, it// cuts (`FaceStep.cut`): the next page is simply there.//// Every word is the core's (`DeckCard`): the face only writes them on the page.importCottonPaperimportCottonValuesimportCoreTextimportSwiftUIenumJournalDiaryFaceKnobs {// THE BOOK, as painted (`diary-book`, 1260 by 1700 pixels): its width// over its height, and the page's place in it as shares of it.staticlet aspect: CGFloat = 1260.0 / 1700.0staticlet page = CGRect(x: 120.0 / 1260, y: 58.0 / 1700, width: 990.0 / 1260, height: 1472.0 / 1700)/// The page's painted rules, as shares of its height (`paint-diary.py`'s/// RULES), the head's double rule above the first.staticlet rules: [CGFloat] = [0.222, 0.2695, 0.317, 0.3645, 0.412, 0.4595, 0.507, 0.5545, 0.602, 0.6495, 0.697, 0.7445, 0.792, 0.8395,0.887, 0.9345, ]/// The book in the card's room: the most of its height and width.staticlet fill: CGFloat = 0.98/// The writing's room: from this far in from the page's spine edge/// (past the ribbon's run) to this far in from its fore-edge, as shares/// of its width.staticlet lead: CGFloat = 0.1staticlet trail: CGFloat = 0.06/// The head: the date's and the place's baselines as shares of the/// page's height, and how far across the page the head may run before/// the seal's corner.staticlet dateLine: CGFloat = 0.072staticlet placeLine: CGFloat = 0.128staticlet headWidth: CGFloat = 0.62/// The seal: its middle as shares of the page, its width as a share of/// the page's width, and the turn it was pressed at.staticlet sealAt = CGPoint(x: 0.835, y: 0.088)staticlet sealSize: CGFloat = 0.215staticlet sealTurn: Double = -9/// The type, as shares of the page's height (at a phone's page, about/// 12, 15, 19 and 16 points on rules 21 apart): print on an object/// scales with the object, never with Dynamic Type.staticlet dateSize: CGFloat = 0.029staticlet placeSize: CGFloat = 0.035staticlet titleSize: CGFloat = 0.045staticlet bodySize: CGFloat = 0.038staticlet weatherSize: CGFloat = 0.026/// How many rules the title may take.staticlet titleLines = 2/// A paragraph after the first starts this many of the words' points in.staticlet indent: CGFloat = 1.2}/// A page as the diary writes it: the head's date, weather and place, the/// title's lines and the words' lines and the rule each starts on, the seal/// and the ribbon.publicstructDiaryPage: Equatable, Sendable {publicvar date: String?publicvar weather: String?publicvar place: String?publicvar title: [TornLine]publicvar words: [TornLine]publicvar wordsRule: Int/// The seal's painting: `diary-seal-<mood>`, or the bare disc.publicvar seal: String/// The page is starred: the ribbon is pulled out.publicvar starred: Bool/// The rules the title is written on, one a line.publicvar titleRules: Range<Int> { 0..<title.count }/// The rules the words are written on, one a line.publicvar wordsRules: Range<Int> { wordsRule..<(wordsRule + words.count) }}/// How a journal page's card is set on the diary's page.publicenumDiarySetting {/// The moods the wax is pressed in, each its own painted seal.publicstaticlet moods = ["great", "good", "okay", "low", "awful"]/// The seal's painting for the card's mood: its own wax, or the bare/// disc for none, or for a word the diary keeps no wax for.publicstaticfunc seal(_ card: DeckCard) -> String {let mood = card.text("seal")?.trimmingCharacters(in: .whitespaces).lowercased()return mood.flatMap { moods.contains($0) ? "diary-seal-\($0)" : nil } ?? "diary-seal-bare" }/// The SF Symbol stamped for the day's weather, by its stored value; nil/// for none.publicstaticfunc weather(_ card: DeckCard) -> String? {switch card.text("weather")?.trimmingCharacters(in: .whitespaces).lowercased() {case"sunny": "sun.max"case"cloudy": "cloud"case"rain": "cloud.rain"case"storm": "cloud.bolt.rain"case"snow": "snowflake"case"wind": "wind"case"fog": "cloud.fog"default: nil } }/// The faces the page is written in, at a page `height` points tall.staticfunc fonts(height: CGFloat) -> (title: CTFont, words: CTFont) {let knobs = JournalDiaryFaceKnobs.self// Asked of CottonFonts first, so the bundled faces are registered// before CoreText is asked for them by name. _ = CottonFonts.hand(1)return ( CTFontCreateWithName(CottonFonts.handFace as CFString, drawable(knobs.titleSize * height), nil), CTFontCreateWithName(CottonFonts.handFace as CFString, drawable(knobs.bodySize * height), nil) ) }/// The writing's room across a page `size` points: where it starts and/// how wide it is.staticfunc column(_ size: CGSize) -> (lead: CGFloat, width: CGFloat) {let knobs = JournalDiaryFaceKnobs.selflet lead = knobs.lead * size.widthreturn (lead, size.width * (1 - knobs.trail) - lead) }/// `card` set on a page `size` points: the head, the title on the first/// rules, then the words from the next rule to the last.publicstaticfunc set(_ card: DeckCard, size: CGSize, theme: Theme) -> DiaryPage {let knobs = JournalDiaryFaceKnobs.selflet (title, body) = TornPageSetting.words(card)let raw = card.text("body") ?? ""let marked = FaceMarkdown.set(raw, theme: theme)let setBody = String(marked.characters) != raw ? marked : nillet faces = fonts(height: size.height)let column = column(size)let flat = title?.split(whereSeparator: \.isNewline).joined(separator: " ") ?? ""let titleLines = TornPageSetting.lines(flat, font: faces.title, width: column.width, limit: knobs.titleLines)let wordsRule = titleLines.countlet words = TornPageSetting.lines( body, font: faces.words, width: column.width, limit: max(0, knobs.rules.count - wordsRule), indent: knobs.indent * knobs.bodySize * size.height, set: setBody)let said = { (slot: String) in card.text(slot).map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.flatMap { $0.isEmpty ? nil : $0 } }returnDiaryPage( date: said("date"), weather: weather(card), place: said("place"), title: titleLines, words: words, wordsRule: wordsRule, seal: seal(card), starred: card.fields["star"] == .bool(true)) }}/// The size CottonFonts draws a face at, which a font measured by name must/// match: never under a point.privatefunc drawable(_ points: CGFloat) -> CGFloat { points.isFinite ? max(points, 1) : 1}publicstructJournalDiaryFace: View { @Environment(\.theme) privatevar themelet card: DeckCardpublicinit(card: DeckCard) {self.card = card }publicvar body: someView {let knobs = JournalDiaryFaceKnobs.selfGeometryReader { box inlet height = min(box.size.height * knobs.fill, box.size.width * knobs.fill / knobs.aspect)let size = CGSize(width: height * knobs.aspect, height: height)let pageSize = CGSize(width: knobs.page.width * size.width, height: knobs.page.height * size.height)DiaryBook(page: DiarySetting.set(card, size: pageSize, theme: theme), size: size) .frame(width: size.width, height: size.height) .frame(width: box.size.width, height: box.size.height) } }}/// A painted layer of the diary, by its catalogue name, at the scale it was/// painted for; nil where this build carries no pictures.privatefunc painted(_ name: String) -> Image? {CottonMedia.image(name, scale: 3)}/// The diary lying open: the painted book (or its stand-in), the ribbon, the/// head, the words on the rules, and the seal in the corner.privatestructDiaryBook: View {let page: DiaryPagelet size: CGSizevar body: someView {let knobs = JournalDiaryFaceKnobs.selflet origin = CGPoint(x: knobs.page.minX * size.width, y: knobs.page.minY * size.height)let w = knobs.page.width * size.widthlet h = knobs.page.height * size.heightlet column = DiarySetting.column(CGSize(width: w, height: h))let rule = { (index: Int) in knobs.rules[min(max(0, index), knobs.rules.count - 1)] * h }let ink = JournalMaterial.ink.colour ZStack(alignment: .topLeading) {iflet book = painted("diary-book") { book.resizable().interpolation(.high) .frame(width: size.width, height: size.height) .accessibilityHidden(true) } else {StandInBook(size: size) } ZStack(alignment: .topLeading) { head(width: w, height: h)Group {if !page.title.isEmpty {Written( lines: page.title, rules: page.titleRules.map(rule), font: CottonFonts.hand(knobs.titleSize * h), size: knobs.titleSize * h, ink: ink, width: column.width) .accessibilityRepresentation { Text(page.title.map(\.words).joined(separator: " ")) } .accessibilityIdentifier("deck-title") .facePart(.title) .padding(.leading, column.lead) .padding(.top, rule(page.titleRules.lowerBound) - knobs.titleSize * h * 1.1) }if !page.words.isEmpty {Written( lines: page.words, rules: page.wordsRules.map(rule), font: CottonFonts.hand(knobs.bodySize * h), size: knobs.bodySize * h, ink: ink, width: column.width, indent: knobs.indent * knobs.bodySize * h, setFont: DiarySetting.fonts(height: h).words) .accessibilityRepresentation { Text(page.words.map(\.words).joined(separator: " ")) } .accessibilityIdentifier("diary-words") .facePart(.body) .padding(.leading, column.lead) .padding(.top, rule(page.wordsRule) - knobs.bodySize * h * 1.1) } }// Ink soaks into the paper: the words multiply over the paint. .blendMode(.multiply) seal(width: w, height: h) } .frame(width: w, height: h, alignment: .topLeading) .offset(x: origin.x, y: origin.y)iflet ribbon = painted(page.starred ? "diary-ribbon-out" : "diary-ribbon-in") { ribbon.resizable().interpolation(.high) .frame(width: size.width, height: size.height) .allowsHitTesting(false) .accessibilityHidden(true) }Color.clear .frame(width: 1, height: 1) .accessibilityElement() .accessibilityLabel(Text(verbatim: page.starred ? ShellLines.line("cards", "faces.diary_starred") : ShellLines.line("cards", "faces.diary_not_starred"))) .accessibilityIdentifier("diary-ribbon") } .frame(width: size.width, height: size.height, alignment: .topLeading) .accessibilityElement(children: .contain) .accessibilityIdentifier("journal-diary") }/// The page's head: the date printed and the weather stamped after it,/// the place written under them.privatefunc head(width w: CGFloat, height h: CGFloat) -> someView {let knobs = JournalDiaryFaceKnobs.selflet lead = knobs.lead * wlet room = knobs.headWidth * w - leadlet print = JournalMaterial.print.colourreturn ZStack(alignment: .topLeading) {if page.date != nil || page.weather != nil { HStack(alignment: .firstTextBaseline, spacing: knobs.dateSize * h * 0.6) {iflet date = page.date {Text(date) .font(CottonFonts.caslon(knobs.dateSize * h)) .tracking(knobs.dateSize * h * 0.04) .foregroundStyle(print) .lineLimit(1) .minimumScaleFactor(0.6) .accessibilityIdentifier("diary-date") .facePart(.when) }iflet weather = page.weather {Image(systemName: weather) .font(CottonFonts.symbol(knobs.weatherSize * h, weight: .medium)) .foregroundStyle(print.opacity(0.85)) .accessibilityLabel(Text(verbatim: weather)) .accessibilityIdentifier("diary-weather") .facePart(.weather) } } .frame(width: room, alignment: .leading) .padding(.leading, lead) .padding(.top, knobs.dateLine * h - knobs.dateSize * h) }iflet place = page.place {Text(place) .font(CottonFonts.hand(knobs.placeSize * h)) .foregroundStyle(JournalMaterial.ink.colour) .lineLimit(1) .truncationMode(.tail) .frame(width: room, alignment: .leading) .accessibilityIdentifier("diary-place") .facePart(.place) .padding(.leading, lead) .padding(.top, knobs.placeLine * h - knobs.placeSize * h) .blendMode(.multiply) } } }/// The seal in the page's top corner, pressed in its mood's wax or bare.privatefunc seal(width w: CGFloat, height h: CGFloat) -> someView {let knobs = JournalDiaryFaceKnobs.selflet side = knobs.sealSize * wreturnGroup {iflet wax = painted(page.seal) { wax.resizable().interpolation(.high) } else {Circle().fill(DiarySeal.wax(page.seal)) } } .frame(width: side, height: side) .rotationEffect(.degrees(knobs.sealTurn)) .accessibilityElement() .accessibilityLabel(Text(verbatim: page.seal)) .accessibilityIdentifier("diary-seal") .facePart(.seal) .offset(x: knobs.sealAt.x * w - side / 2, y: knobs.sealAt.y * h - side / 2) }}/// The wax of a seal where this build carries no pictures, by its painting.enumDiarySeal {staticfunc wax(_ painting: String) -> Color {switch painting {case"diary-seal-great": JournalMaterial.great.colourcase"diary-seal-good": JournalMaterial.good.colourcase"diary-seal-okay": JournalMaterial.okay.colourcase"diary-seal-low": JournalMaterial.low.colourcase"diary-seal-awful": JournalMaterial.awful.colourdefault: JournalMaterial.bare.colour } }}/// The book where this build carries no pictures: its cloth, its page and/// the page's rules, flat.privatestructStandInBook: View {let size: CGSizevar body: someView {let knobs = JournalDiaryFaceKnobs.selfCanvas { context, room inlet cover = CGRect(x: room.width * 40 / 1260, y: room.height * 36 / 1700, width: room.width * 1110 / 1260, height: room.height * 1534 / 1700) context.fill(Path(roundedRect: cover, cornerRadius: room.width * 0.02), with: .color(JournalMaterial.cloth.colour))let page = CGRect(x: knobs.page.minX * room.width, y: knobs.page.minY * room.height, width: knobs.page.width * room.width, height: knobs.page.height * room.height) context.fill(Path(page), with: .color(JournalMaterial.paper.colour))for share in knobs.rules {let y = page.minY + share * page.height context.stroke(Path { $0.move(to: CGPoint(x: page.minX, y: y)); $0.addLine(to: CGPoint(x: page.maxX, y: y)) }, with: .color(JournalMaterial.rule.colour), lineWidth: 0.5) } } .frame(width: size.width, height: size.height) .accessibilityHidden(true) }}/// The diary, by its id, and the pages it is reviewed in: invented words/// only. The specimen world's today is Thursday 12 November 2026; a page's/// date is the day it is about, said as the core says it (`weekday_date`),/// the year only for another year's.publicenumJournalDiaryFaceCatalogue {publicstaticlet stock: FaceStock = .ownpublicstaticlet ground: FaceGround = .bedsideTablepublicstaticlet face = Face(id: "journal.diary", specimens: [Specimen("evening", evening), Specimen("starred", starred), Specimen("low", low),Specimen("untitled", untitled), Specimen("blank", blank), Specimen("old", old), ], steps: .cut) { card, _, _ inJournalDiaryFace(card: card) }staticfunc page(_ fields: [String: RecordValue]) -> DeckCard {DeckCard(face: "journal.diary", corner: nil, pressing: false, pace: nil, plate: nil, fields: fields) }/// A good day: its mood, its weather and its place.publicstaticlet evening = page(["title": .text("The walk up to the old water tank"),"date": .text("Thu 12 Nov"),"body": .text(""" We left after lunch and took the long way, past the school and up the steps nobody uses any more. The light was low and gold by the time we reached the tank, and the whole valley was quiet except for the goats. Mira found a feather as long as her arm and carried it all the way home like a sword. I want to remember how she laughed when the wind took her hat. """),"seal": .text("good"),"weather": .text("sunny"),"place": .text("Invented hills above the town"),"star": .bool(false), ])/// A great day, starred: the ribbon pulled out, the seal gold.publicstaticlet starred = page(["title": .text("The day the letter came"),"date": .text("Tue 10 Nov"),"body": .text("It came in the second post, in the thin blue envelope, and I read it standing in the hall with my coat still on. Yes. They said yes. I rang everyone and then sat on the stairs for a long time doing nothing at all."),"seal": .text("great"),"weather": .text("cloudy"),"place": .text("Home"),"star": .bool(true), ])/// A low day in the rain.publicstaticlet low = page(["title": .text("Tired"),"date": .text("Mon 9 Nov"),"body": .text("Slept badly. The rain did not stop all day and neither did the meetings. Made soup, which helped a little."),"seal": .text("low"),"weather": .text("rain"),"star": .bool(false), ])/// No title: the first sentence stands as one; an awful mood, a storm.publicstaticlet untitled = page(["date": .text("Sun 8 Nov"),"body": .text("The storm took the big branch off the neem in the night. It missed the car by a hand's width and took the wires down with it, so no power until the evening. Spent the day clearing the yard and the evening by candlelight, which would have been lovely if anything else had gone right."),"seal": .text("awful"),"weather": .text("storm"),"star": .bool(false), ])/// A page just opened with Plus: today's date, no mood, an unpressed disc.publicstaticlet blank = page(["date": .text("Thu 12 Nov"),"star": .bool(false), ])/// A page from a year ago, okay, in the fog: its date says the year.publicstaticlet old = page(["title": .text("First morning in the new flat"),"date": .text("Thu 12 Nov 2025"),"body": .text("Woke to fog so thick the neem outside the window was only a shape. The kettle is still in a box somewhere, so tea at the corner stall."),"seal": .text("okay"),"weather": .text("fog"),"place": .text("Invented Lane, flat 4"),"star": .bool(false), ])}