import { readFileSync } from "node:fs"; export type Theme = { type: Record; scale: { tokens: Record } }; const CARD_CONSTANTS = [ "cardContentInset", "cardLineSizeMin", "cardLineSizeMax", "cardLineSizeFraction", "cardLineWidthInsetMultiplier", "cardKeySizeMin", "cardKeySizeMax", "cardKeySizeFraction", "cardKeyWidthInsetMultiplier", ] as const; type CardGeometry = Record<(typeof CARD_CONSTANTS)[number], number>; /** Read the literal constants beside the app's card reader, never guessed defaults. */ export function parseCardGeometry(source: string): CardGeometry { const body = source.match(/enum CardStockKnobs\s*\{([^}]+)\}/)?.[1]; if (!body) throw new Error("card label measurer: missing CardStockKnobs in CardStock.swift"); return Object.fromEntries(CARD_CONSTANTS.map((name) => { const matches = [...body.matchAll(new RegExp(`^\\s*static let ${name}: CGFloat = (\\d+(?:\\.\\d+)?)\\s*$`, "gm"))]; const value = Number(matches[0]?.[1]); if (matches.length !== 1 || !Number.isFinite(value) || value <= 0) { throw new Error(`card label measurer: CardStockKnobs.${name} must be one positive numeric literal in CardStock.swift`); } return [name, value]; })) as CardGeometry; } const CARD_GEOMETRY = parseCardGeometry(readFileSync( new URL("../../apps/cotton/Sources/CottonUI/Cards/CardStock.swift", import.meta.url), "utf8", )); /** * CardStock sets acts in type.title and field names in type.meta. At the * narrow card width (cardLineSizeMin / cardLineSizeFraction, currently 84pt), * available width is width * (1 - width_inset_multiplier * content_inset). * Divide by font size * advance: Courier is exactly 0.6em; Fraunces Text * and system use a conservative 0.6em character estimate. This yields 11 * characters per act line and 13 per field-name line in the shipped themes. * Wrap WHOLE words into two lines, never count words: "Serve from this Mac" * fits as "Serve from" / "this Mac". CardStock passes explicit Knobs sizes * to the theme fonts; the type sizes in scale.tokens are for other readers. */ export function cardTextFault(text: string, role: "line" | "key", themes: Theme[], geometry = CARD_GEOMETRY): string | null { if (!text.trim()) return "has no label"; for (const theme of themes) { const prefix = role === "line" ? "cardLine" : "cardKey"; const width = geometry.cardLineSizeMin / geometry.cardLineSizeFraction; const size = Math.min(geometry[`${prefix}SizeMax`], Math.max(geometry[`${prefix}SizeMin`], width * geometry[`${prefix}SizeFraction`])); const font = theme.type[role === "line" ? "title" : "meta"]; if (!["fraunces_text", "courier_italic", "system", "system_semibold"].includes(font)) { throw new Error(`derive the card character advance for theme font ${font}`); } const letters = Math.floor(width * (1 - geometry[`${prefix}WidthInsetMultiplier`] * geometry.cardContentInset) / (size * 0.6)); const lines: string[] = []; for (const word of text.trim().split(/\s+/)) { if ([...word].length > letters) return `"${text}": "${word}" is wider than a card's line`; const last = lines.at(-1); if (last !== undefined && [...last].length + 1 + [...word].length <= letters) lines[lines.length - 1] = `${last} ${word}`; else lines.push(word); } if (lines.length > 2) return `"${text}" takes ${lines.length} lines — a card's label takes two`; } return null; } /** English act verbs, including auxiliaries for negative imperatives. Extend * this vocabulary when an act introduces another verb; labels remain prose. * "not" opens the owner's own refusal on an arrivals card ("Not this", lane * brief askedarrivals): a negative imperative with its verb understood. */ const ACT_VERBS = new Set(`accept achieve achieved add admit agree allow approve refuse archive back block book build cancel change check choose classify clear close complete confirm connect cook cooked copy create decline delete disconnect dismiss do don't download drop edit enable end erase export favourite fetch file find hand hide hold import invite join junk keep leave link load lock make mark measure merge move mute name not note open pause pay pin play print publish push put rate re-key read recolour record refresh release remove rename reopen repeat reply report request reset restore resume retry review revoke run save say scale search select send serve set share shipped show sign snooze sort star start stop store submit subscribe summarise sync take test try turn unarchive unblock unconfirm undo unfile unmute unpin unstar unsubscribe update upload use view wait write`.split(/\s+/)); /** * The acts whose label is a name, not a verb, each with why the name says the * act better than a verb would. Keyed `.` with the label as it * stands, so a renamed label is judged by the verb rule again; an entry that * names no dealt act is reported by `gen:page-cards --check`. */ export const NAMED_ACTS: Record = { "book.finish": { label: "Finished", why: "a shelf name a reader already knows; the act sets the book's status to that shelf" }, "book.want": { label: "Want to read", why: "a shelf name a reader already knows; the act sets the book's status to that shelf" }, "todo.invite": { label: "People", why: "names the part of the card the act edits, the todo's people" }, "todo.edit-notes": { label: "Notes", why: "names the part of the card the act edits, the todo's notes" }, }; export function actLabelFault(label: string, themes: Theme[], act?: string): string | null { // An act is a verb on the whole record: which one is a fact, a field line // on the card, never a placeholder in the verb (architect, 17 Oct 2026: // "Sign in on {serving_mac}" — the act is "Sign in on the Mac"). const placeholder = label.match(/\{[^}]*\}/)?.[0]; if (placeholder) return `"${label}" names ${placeholder} — an act's label carries no placeholder`; const fault = cardTextFault(label, "line", themes); if (fault) return fault; if (act !== undefined && NAMED_ACTS[act]?.label === label) return null; const first = label.trim().split(/\s+/)[0].toLowerCase(); return ACT_VERBS.has(first) ? null : `"${label}" begins with "${first}" — an act's label begins with a verb`; }