// 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. import CoreText import CottonFaces import CottonPaper import SwiftUI #if os(iOS) import UIKit typealias ReadFont = UIFont typealias ReadInk = UIColor #else import AppKit typealias ReadFont = NSFont typealias ReadInk = NSColor #endif struct ReadText: PlatformViewRepresentable { let text: String let ink: Color var set: AttributedString? = nil /// The words' face at the reader's size. static var font: ReadFont { // Asking CottonPaper for the face registers the bundled fonts. _ = CottonFonts.caslon(ReadPageKnobs.bodySize) return ReadFont(name: CottonFonts.caslonFace, size: ReadPageKnobs.bodySize) ?? .systemFont(ofSize: ReadPageKnobs.bodySize) } /// The running head's face: the typewriter, small. static var headFont: ReadFont { typewriter(ReadPageKnobs.headSize) } /// The byline's face: the typewriter, small. static var bylineFont: ReadFont { typewriter(ReadPageKnobs.bylineSize) } private static func typewriter(_ size: CGFloat) -> ReadFont { _ = CottonFonts.type(size) return ReadFont(name: CottonFonts.typeFace, size: size) ?? .monospacedSystemFont(ofSize: size, weight: .regular) } /// The distance from one line to the next. static var pitch: CGFloat { (ReadPageKnobs.bodySize * ReadPageKnobs.bodyPitch).rounded() } /// Caslon's average letter at the reader's size, over a to z. static var 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. static var headHeight: CGFloat { (ReadPageKnobs.headSize * 1.4).rounded() } static var 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. static func setting(_ text: String, ink: ReadInk, set: AttributedString? = nil) -> NSAttributedString { if let set { let value = NSMutableAttributedString(attributedString: FaceMarkdown.native(set, font: font as CTFont, ink: ink.cgColor)) let source = value.string as NSString var offset = 0 for 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. static func 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 = 0 if let 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.width let fitted = label.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)) return CGSize(width: width, height: fitted.height) } #else func 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 }