// A face's words measured: the characters a line holds and the lines a room // holds at a size and leading (`FaceRoom`), off the typewriter or as // CoreText sets a business's reading face (`FaceMeasure`) — the room the // core cuts a face's longer words into pages for (CottonUI's `FacePages`). import CottonPaper import CoreText import SwiftUI /// The characters a line of a face holds and the lines a room holds, at a /// size and leading. public enum FaceRoom { public static func measure( width: CGFloat, height: CGFloat, size: CGFloat, leading: CGFloat, face: ThemeFace = .courier ) -> (columns: Int, lines: Int) { // Any face but the typewriter is measured off CoreText: its line, and // the width a character takes on average (`FaceMeasure.advance`). guard face == .courier else { let line = FaceMeasure.lineHeight(face, size) + leading return (max(8, Int(width / FaceMeasure.advance(face, size))), max(1, Int(height / line))) } #if canImport(UIKit) let font = UIFont(name: CottonFonts.typeFace, size: size) ?? .monospacedSystemFont(ofSize: size, weight: .regular) let advance = ("M" as NSString).size(withAttributes: [.font: font]).width let line = font.lineHeight + leading #else let advance = size * 0.6 let line = size * 1.2 + leading #endif return (max(8, Int(width / advance)), max(1, Int(height / line))) } } public enum FaceMeasureKnobs { /// A frame tall enough that no face's words run out of it. static let tall: CGFloat = 100_000 /// The letters a word takes on average, and the space after it. public static let wordLetters: CGFloat = 4.7 /// A proportional face's average character, widened so a line the core /// cuts never holds more than the face draws. static let widen: CGFloat = 1.1 } /// A face's words measured as CoreText sets them: the lines a text wraps to /// at a width, a line's height, and a character's average width. public enum FaceMeasure { /// The letters a proportional face is averaged over. static let letters = "abcdefghijklmnopqrstuvwxyz" public static func lineHeight(_ face: ThemeFace, _ size: CGFloat) -> CGFloat { let font = face.measuring(size) return CTFontGetAscent(font) + CTFontGetDescent(font) + CTFontGetLeading(font) } /// The lines `text` wraps to at `width`; none for no words. public static func lines(_ text: String, _ face: ThemeFace, _ size: CGFloat, width: CGFloat) -> Int { guard !text.isEmpty, width > 0 else { return 0 } let string = NSAttributedString( string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): face.measuring(size)]) let setter = CTFramesetterCreateWithAttributedString(string) let path = CGPath(rect: CGRect(x: 0, y: 0, width: width, height: FaceMeasureKnobs.tall), transform: nil) let frame = CTFramesetterCreateFrame(setter, CFRange(location: 0, length: 0), path, nil) return CFArrayGetCount(CTFrameGetLines(frame)) } /// The width a character takes: a monospaced face's one, a proportional /// face's average over the letters and the space a word leaves. public static func advance(_ face: ThemeFace, _ size: CGFloat) -> CGFloat { let font = face.measuring(size) func width(_ text: String) -> CGFloat { let string = NSAttributedString(string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font]) return CGFloat(CTLineGetTypographicBounds(CTLineCreateWithAttributedString(string), nil, nil, nil)) } if CTFontGetSymbolicTraits(font).contains(.traitMonoSpace) { return width("M") } let letter = width(letters) / CGFloat(letters.count) let word = (letter * FaceMeasureKnobs.wordLetters + width(" ")) / (FaceMeasureKnobs.wordLetters + 1) return word * FaceMeasureKnobs.widen } }