// 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 UIKit public extension NSAttributedString.Key { /// The face bits a run carries (`WritingFace`), resolved into its font. static let writingFace = NSAttributedString.Key("cotton.writing.face") /// A tag: its chip is drawn behind it. static let writingChip = NSAttributedString.Key("cotton.writing.chip") /// A mark drawn as a glyph instead of its characters: `bullet`, `box`, /// `ticked`, `rule`. static let writingGlyph = NSAttributedString.Key("cotton.writing.glyph") /// A quote: a bar is drawn at its left. static let writingQuote = NSAttributedString.Key("cotton.writing.quote") /// A link's or a picture's address. static let 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. static let writingBlock = NSAttributedString.Key("cotton.writing.block") /// Words folded away with their lines: no glyphs, no height. static let writingFolded = NSAttributedString.Key("cotton.writing.folded") /// A line that folds: `open` or `closed`, its chevron drawn in the margin beside it. static let writingFold = NSAttributedString.Key("cotton.writing.fold") } /// The face bits of a run: what a host asks a font of the look for. public struct WritingFace: OptionSet, Hashable, Sendable { public let rawValue: Int public init(rawValue: Int) { self.rawValue = rawValue } public static let bold = WritingFace(rawValue: 1) public static let italic = WritingFace(rawValue: 2) public static let mono = WritingFace(rawValue: 4) public static let title = WritingFace(rawValue: 8) public static let h1 = WritingFace(rawValue: 16) public static let h2 = WritingFace(rawValue: 32) public static let h3 = WritingFace(rawValue: 64) static func of(_ span: WritingSpan) -> WritingFace? { switch span.style { case "bold": .bold case "italic": .italic case "code", "code_block": .mono case "title": .title case "heading": heading(span.level ?? 1) default: nil } } /// A heading level's face: 1, 2, and 3 or deeper. public static func 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. @MainActor public struct WritingLook { public var theme: WritingTheme public var type: WritingType /// Marks hidden away from the cursor (the default); false draws every mark dim instead. public var hideMarks: Bool var traits: UITraitCollection? public init(theme: WritingTheme, type: WritingType, hideMarks: Bool = true, traits: UITraitCollection? = nil) { self.theme = theme self.type = type self.hideMarks = hideMarks self.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.scale if 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) } return 1 } public var size: CGFloat { CGFloat(type.size) } public func font(_ face: WritingFace) -> UIFont { let size = size * scale(face) let weight: UIFont.Weight = face.heading ? .bold : face.contains(.bold) ? .bold : .regular if face.contains(.mono) { return WritingFonts.font(type.code, size: size * 0.92, weight: weight, italic: face.contains(.italic), traits: traits) } return WritingFonts.font(type.face, size: size, weight: weight, italic: face.contains(.italic), traits: traits) } public var 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 + hang return p } var base: [NSAttributedString.Key: Any] { [.font: body, .foregroundColor: theme.ink, .paragraphStyle: paragraph([])] } } #endif