// How a span is drawn: the fonts, colours and paragraph shapes the theme and// the typography make, and the attribute keys the layout reads.#if canImport(UIKit)import UIKitpublicextension NSAttributedString.Key {/// The face bits a run carries (`WritingFace`), resolved into its font.staticlet writingFace = NSAttributedString.Key("cotton.writing.face")/// A tag: its chip is drawn behind it.staticlet writingChip = NSAttributedString.Key("cotton.writing.chip")/// A mark drawn as a glyph instead of its characters: `bullet`, `box`,/// `ticked`, `rule`.staticlet writingGlyph = NSAttributedString.Key("cotton.writing.glyph")/// A quote: a bar is drawn at its left.staticlet writingQuote = NSAttributedString.Key("cotton.writing.quote")/// A link's or a picture's address.staticlet writingLink = NSAttributedString.Key("cotton.writing.link")/// A picture's or a table's text: hidden, its block drawn in its place./// The value is the block's key.staticlet writingBlock = NSAttributedString.Key("cotton.writing.block")/// Words folded away with their lines: no glyphs, no height.staticlet writingFolded = NSAttributedString.Key("cotton.writing.folded")/// A line that folds: `open` or `closed`, its chevron drawn in the margin beside it.staticlet writingFold = NSAttributedString.Key("cotton.writing.fold")}/// The face bits of a run: what a host asks a font of the look for.publicstructWritingFace: OptionSet, Hashable, Sendable {publiclet rawValue: Intpublicinit(rawValue: Int) { self.rawValue = rawValue }publicstaticlet bold = WritingFace(rawValue: 1)publicstaticlet italic = WritingFace(rawValue: 2)publicstaticlet mono = WritingFace(rawValue: 4)publicstaticlet title = WritingFace(rawValue: 8)publicstaticlet h1 = WritingFace(rawValue: 16)publicstaticlet h2 = WritingFace(rawValue: 32)publicstaticlet h3 = WritingFace(rawValue: 64)staticfunc of(_ span: WritingSpan) -> WritingFace? {switch span.style {case"bold": .boldcase"italic": .italiccase"code", "code_block": .monocase"title": .titlecase"heading": heading(span.level ?? 1)default: nil } }/// A heading level's face: 1, 2, and 3 or deeper.publicstaticfunc heading(_ level: Int) -> WritingFace { level <= 1 ? .h1 : level == 2 ? .h2 : .h3 }var heading: Bool { !intersection([.title, .h1, .h2, .h3]).isEmpty }}/// The theme and typography made into what UIKit draws.@MainActorpublicstructWritingLook {publicvar theme: WritingThemepublicvar type: WritingType/// Marks hidden away from the cursor (the default); false draws every mark dim instead.publicvar hideMarks: Boolvar traits: UITraitCollection?publicinit(theme: WritingTheme, type: WritingType, hideMarks: Bool = true, traits: UITraitCollection? = nil) {self.theme = themeself.type = typeself.hideMarks = hideMarksself.traits = traits }/// The size a heading level is drawn at, as a multiple of the body's (the core's `scale`).func scale(_ face: WritingFace) -> CGFloat {let scale = type.scaleif face.contains(.title) { return CGFloat(scale.title) }if face.contains(.h1) { return CGFloat(scale.h1) }if face.contains(.h2) { return CGFloat(scale.h2) }if face.contains(.h3) { return CGFloat(scale.h3) }return1 }publicvar size: CGFloat { CGFloat(type.size) }publicfunc font(_ face: WritingFace) -> UIFont {let size = size * scale(face)let weight: UIFont.Weight = face.heading ? .bold : face.contains(.bold) ? .bold : .regularif face.contains(.mono) {returnWritingFonts.font(type.code, size: size * 0.92, weight: weight, italic: face.contains(.italic), traits: traits) }returnWritingFonts.font(type.face, size: size, weight: weight, italic: face.contains(.italic), traits: traits) }publicvar body: UIFont { font([]) }/// A mark hidden away from the cursor: no ink and all but no width.var hidden: UIFont { body.withSize(0.01) }func paragraph(_ face: WritingFace, indent: CGFloat = 0, hang: CGFloat = 0) -> NSParagraphStyle {let font = font(face)let p = NSMutableParagraphStyle()// Air between lines without stretching the caret: the extra height// goes below each line.let multiple = face.heading ? max(1.15, type.lineHeight - 0.25) : type.lineHeight p.lineSpacing = max(0, font.pointSize * CGFloat(multiple) - font.lineHeight) p.paragraphSpacingBefore = face.heading ? font.pointSize * 0.35 : 0 p.paragraphSpacing = font.pointSize * CGFloat(type.paragraph) p.firstLineHeadIndent = indent p.headIndent = indent + hangreturn p }var base: [NSAttributedString.Key: Any] { [.font: body, .foregroundColor: theme.ink, .paragraphStyle: paragraph([])] }}#endif