// Keys a table of pictures reads, each drawn once for any table that names it// (Photos was the first: the library's prints, an album's cover print and// its members, a duplicate pair's two sides, a photo's own page)://// * `polaroid` — the words a polaroid face reads: the margin caption, the// title in the serif under the print and the line by hand under that;// * `opens` — a row that stands for another record (an album's picture,// a sighting's chat): a tap opens THAT record, through the opener whoever// holds the list or page sets. On a photo column, a tap on that picture;// * column `format` — a number read as a size ("2.1 MB"), a length ("0:42")// or a width with its height ("4032 × 3024"); column `labels` — a choice's// words; a date column's `until` — the two days as one range; a photo// column's `size` (its proportion on a page) and `caption` (its words),// with `caption_fallback` read when the caption says nothing.//// The reading lives here, pure, so the words are settled by tests; the faces// only draw them.importCottonKitimportCottonValuesimportFoundation/// Where a row's tap goes when the row stands for another record: the record/// whose id `column` holds, in `table`, else in the table `table_column`'s/// value names, else in the column's own `link_to`.publictypealiasRecordOpens = DefinitionRecordOpensextensionDefinitionRecordOpens {publicinit(column: String, table: String? = nil, tableColumn: String? = nil) {self.init(_generated: (), column: column)self.table = tableself.tableColumn = tableColumn }}/// The polaroid face's words, each a column or nothing.publictypealiasPolaroidLayout = DefinitionPolaroidLayoutextensionDefinitionPolaroidLayout {publicinit(caption: String? = nil, title: String? = nil, subtitle: String? = nil) {self.init(_generated: ())self.caption = captionself.title = titleself.subtitle = subtitle }}/// How a column's value reads (`"format"`), each format belonging to the/// types it names. A number reads as a size (`bytes`), a length in seconds/// (`duration`) or milliseconds (`duration_ms`), or a width with its height/// (`dimensions`); a text or choice as plain words (`words`); a text as its/// repeat preset (`repeat-rule`) or an app's name (`app-name`); a date/// against now (`ago`) or as a calendar date and time (`calendar-datetime`);/// a text fact as a figure over its capacity (`counted-pair`).publictypealiasColumnFormat = DefinitionColumnFormatextensionTableDefinition {/// The URL explicitly named by a slip; neither a secret nor a scalar/// coerced to words may become a navigation target.func slipAddress(_ record: TableRecord) -> String? {guardcase .address(let key)? = slip.opens, let column = column(key), column.type == .url, column.secret != true,case .text(let address) = record[key], !address.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { returnnil }return address }/// What a tap on the slip copies (`slip.tap`, a copy command naming a/// column), or nil when the slip's tap opens. A sealed copy is never a/// tap: it is opened from the vault on the page, behind its own flow.func tapCopy(_ record: TableRecord) -> (value: String, secret: Bool)? {guardlet tap = slip.tap, let command = commands?.first(where: { $0.id == tap }), command.kind == .copy,let key = command.copies, key != RecordCommand.copySealed, let column = column(key),let value = record[key].text, !value.isEmpty else { returnnil }return (value, column.isSecret) }/// The record a row opens in place of itself — its table and id — or nil/// when the table names no `opens`, or the column holds no id (a/// sighting shared from outside any chat opens nothing).publicfunc opening(_ record: TableRecord) -> (table: String, id: String)? {iflet opens { return target(of: opens, in: record) }ifcase .record(let opens)? = slip.opens { return target(of: opens, in: record) }returnnil }/// The record a column's tap opens — a photo's (a duplicate's side) or/// a read-only text's (a todo's Repeats, its rule) — or nil.publicfunc opening(_ record: TableRecord, column key: String?) -> (table: String, id: String)? { column(key)?.opens.flatMap { target(of: $0, in: record) } }privatefunc target(of opens: RecordOpens, in record: TableRecord) -> (table: String, id: String)? {guardlet id = record[opens.column].text?.trimmingCharacters(in: .whitespacesAndNewlines), !id.isEmpty,let table = opens.table ?? opens.tableColumn.flatMap({ record[$0].text }) ?? column(opens.column)?.linkTo, !table.isEmptyelse { returnnil }return (table, id) }/// A column's words on this record as a page or a hand line writes them:/// a date with its `until` as one range, a width with its height, else as/// any face writes the value (`FaceText.line`). Nil when it says nothing.publicfunc words( _ key: String?, of record: TableRecord, links: [String: String] = [:], calendar: Calendar = .current, now: Date = Date() ) -> String? {guardlet column = column(key) else { returnnil }if column.type == .date, column.until != nil {return record.dayParts(column.key)?.range }if column.type == .number, column.format == .dimensions {guardlet width = record[column.key].number, let height = record[column.pair].number, width > 0, height > 0else { returnnil }return"\(Int(width)) \u{00D7} \(Int(height))" }let line = FaceText.line(record[column.key], words: record.columnWords(column.key), column: column, links: links, currency: record[column.currency].text, calendar: calendar, readsPlaceholder: false, unitWords: record.said?.unitWords ?? [:])return line.flatMap { $0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : $0 } }/// The polaroid's margin caption: its `caption` column's words; without/// a `polaroid`, the old person rule — the first name of the title.publicfunc polaroidCaption(of record: TableRecord, links: [String: String] = [:]) -> String? {guardlet polaroid else { return record.said?.slip.name.map(CardPerson.firstName) }return words(polaroid.caption, of: record, links: links) }/// The polaroid's serif title and its line by hand, each nil without/// its column or a value.publicfunc polaroidTitle(of record: TableRecord, links: [String: String] = [:]) -> String? { words(polaroid?.title, of: record, links: links) }publicfunc polaroidSubtitle(of record: TableRecord, links: [String: String] = [:]) -> String? { words(polaroid?.subtitle, of: record, links: links) }/// A photo column's proportion (width ÷ height) from its `size`/// columns, or nil when either is unknown.publicfunc aspect(of key: String?, in record: TableRecord) -> Double? {guardlet size = column(key)?.size, size.count == 2,let width = record[size[0]].number, let height = record[size[1]].number, width > 0, height > 0else { returnnil }return width / height }/// A photo column's words (its `caption` column's, else its/// `caption_fallback`'s), or nil.publicfunc pictureCaption(of key: String?, in record: TableRecord, links: [String: String] = [:]) -> String? { pictureMargin(of: key, in: record, links: links).words }/// A picture's margin: its `caption` column's words, `own`; else its/// `caption_fallback`'s (a film with no caption reads its length), not/// own; else nothing, not own. A film's print draws the play mark/// whenever the words are not the owner's caption.publicfunc pictureMargin( of key: String?, in record: TableRecord, links: [String: String] = [:] ) -> (words: String?, own: Bool) {let picture = column(key)iflet caption = words(picture?.caption, of: record, links: links) { return (caption, true) }return (words(picture?.captionFallback, of: record, links: links), false) }/// Whether a list of this table offers the polaroid grid — its tile is/// the polaroid grid.publicvar drawsPolaroids: Bool { tile.face == .polaroidGrid }}