Cotton code
49 lines · 2.1 KB · SwiftRaw
// What the object faces share in setting words on a painted thing (an open// book's tag, a video's sticker): a line typed with its baseline where the// object puts it, a day as it is written on one, and a place's label read as// its kind's when the face's id is not its kind's.import CottonPaperimport CottonValuesimport SwiftUI/// A day as it is written on an object: the core's day words without the/// weekday they open with ("Mon 12 Oct" — "12 Oct"; "Mon 27 Oct 2025" —/// "27 Oct 2025"), and as a stamp strikes it, in capitals ("12 OCT").public enum DayStamp {    public static func bare(_ day: String) -> String {        let words = day.split(separator: " ")        let kept = words.count > 1 && words[0].allSatisfy(\.isLetter) && words[1].allSatisfy(\.isNumber) ? words.dropFirst() : words[...]        return kept.joined(separator: " ")    }    public static func of(_ day: String) -> String {        bare(day).uppercased()    }}extension DeckCard {    /// The label over a place as the card's own words or its kind's line    /// (`FactLabel`), for a face whose id is not its kind's.    func label(_ slot: String, kind: String) -> String? {        if case .object(let own)? = fields["labels"], let said = own[slot]?.text, !said.isEmpty { return said }        return FactLabel.of(kind, slot)    }}/// Words set on a painted object.enum ObjectWriting {    /// `words` drawn with their baseline at `at`, from its x, in `font` and    /// `ink`; squeezed to `width` when `fits` and it is wider, else drawn whole.    static func type(_ context: inout GraphicsContext, _ words: String, font: Font, ink: Color, at: CGPoint, width: CGFloat, fits: Bool = false) {        let text = context.resolve(Text(words).font(font).foregroundStyle(ink))        let far = CGSize(width: 10_000, height: 10_000)        let ideal = text.measure(in: far)        let base = text.firstBaseline(in: far)        let scale = fits && ideal.width > width ? width / ideal.width : 1        var inner = context        inner.translateBy(x: at.x, y: at.y)        inner.scaleBy(x: scale, y: scale)        inner.draw(text, at: CGPoint(x: 0, y: -base), anchor: .topLeading)    }}