// A page's words: Libre Caslon at the reader's size, RANGED LEFT (the// centred page, 29 Oct 2026: the reader's narrow column justified gave loose// lines), with its lines a fixed pitch apart — a UIKit label's, so the pitch// and a list's hang are the paragraph style's. A LIST'S ITEM is set as a book// sets one: its run-on lines hung under its words, not under its dash// (`ReadBlock.isItem`). The running head and the byline are the typewriter's// (Courier Prime), small: facts, not the words.//// The setting is the platform's text system on both destinations; only the// label it is drawn in is the phone's (`UILabel`), and the package's macOS// tests compile the rest against AppKit's font and colour.importCoreTextimportCottonFacesimportCottonPaperimportSwiftUI#if os(iOS)import UIKittypealiasReadFont = UIFonttypealiasReadInk = UIColor#elseimportAppKittypealiasReadFont = NSFonttypealiasReadInk = NSColor#endifstructReadText: PlatformViewRepresentable {let text: Stringlet ink: Colorvar set: AttributedString? = nil/// The words' face at the reader's size.staticvar font: ReadFont {// Asking CottonPaper for the face registers the bundled fonts. _ = CottonFonts.caslon(ReadPageKnobs.bodySize)returnReadFont(name: CottonFonts.caslonFace, size: ReadPageKnobs.bodySize) ?? .systemFont(ofSize: ReadPageKnobs.bodySize) }/// The running head's face: the typewriter, small.staticvar headFont: ReadFont { typewriter(ReadPageKnobs.headSize) }/// The byline's face: the typewriter, small.staticvar bylineFont: ReadFont { typewriter(ReadPageKnobs.bylineSize) }privatestaticfunc typewriter(_ size: CGFloat) -> ReadFont { _ = CottonFonts.type(size)returnReadFont(name: CottonFonts.typeFace, size: size) ?? .monospacedSystemFont(ofSize: size, weight: .regular) }/// The distance from one line to the next.staticvar pitch: CGFloat { (ReadPageKnobs.bodySize * ReadPageKnobs.bodyPitch).rounded() }/// Caslon's average letter at the reader's size, over a to z.staticvar advance: CGFloat {let letters = "abcdefghijklmnopqrstuvwxyz"return (letters as NSString).size(withAttributes: [.font: font]).width / CGFloat(letters.count) }/// The running head's line, and the byline's.staticvar headHeight: CGFloat { (ReadPageKnobs.headSize * 1.4).rounded() }staticvar bylineHeight: CGFloat { (ReadPageKnobs.bylineSize * 1.4).rounded() }/// `text` as the page sets it: each line of it its own paragraph, ranged/// left, a list's item with its run-on lines hung.staticfunc setting(_ text: String, ink: ReadInk, set: AttributedString? = nil) -> NSAttributedString {iflet set {let value = NSMutableAttributedString(attributedString: FaceMarkdown.native(set, font: font as CTFont, ink: ink.cgColor))let source = value.string as NSStringvar offset = 0for line in value.string.components(separatedBy: "\n") {let count = min(line.utf16.count + 1, source.length - offset) value.addAttribute(.paragraphStyle, value: style(of: line), range: NSRange(location: offset, length: count)) offset += count }return value }let set = NSMutableAttributedString()let lines = text.components(separatedBy: "\n")for (index, line) in lines.enumerated() {let words = index < lines.count - 1 ? line + "\n" : line set.append(NSAttributedString(string: words, attributes: [.font: font, .foregroundColor: ink, .paragraphStyle: style(of: line)])) }return set }/// One line's paragraph style: ranged left at the page's pitch, a list's/// item hung at the width of its marker.staticfunc style(of line: String) -> NSParagraphStyle {let style = NSMutableParagraphStyle() style.minimumLineHeight = pitch style.maximumLineHeight = pitch style.lineBreakMode = .byWordWrapping style.alignment = .natural// A blank line between paragraphs, as the record holds them, keeps// its pitch: no extra space is added after a paragraph. style.paragraphSpacing = 0iflet marker = line.range(of: #"^\s*([-*•–☐☑]|\d+[.)])\s+"#, options: .regularExpression), ReadBlock.isItem(line) { style.headIndent = (String(line[marker]) as NSString).size(withAttributes: [.font: font]).width }return style } #if os(iOS)func makeUIView(context: Context) -> UILabel {let label = UILabel() label.numberOfLines = 0 label.isAccessibilityElement = false label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)return label }func updateUIView(_ label: UILabel, context: Context) { label.attributedText = Self.setting(text, ink: UIColor(ink), set: set) }func sizeThatFits(_ proposal: ProposedViewSize, uiView label: UILabel, context: Context) -> CGSize? {let width = proposal.width ?? UIView.layoutFittingExpandedSize.widthlet fitted = label.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))return CGSize(width: width, height: fitted.height) } #elsefunc makeNSView(context: Context) -> NSTextField {let label = NSTextField(labelWithString: "") label.maximumNumberOfLines = 0 label.setAccessibilityElement(false)return label }func updateNSView(_ label: NSTextField, context: Context) { label.attributedStringValue = Self.setting(text, ink: NSColor(ink), set: set) } #endif}