// 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`).importCottonPaperimportCoreTextimportSwiftUI/// The characters a line of a face holds and the lines a room holds, at a/// size and leading.publicenumFaceRoom {publicstaticfunc 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) + leadingreturn (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]).widthlet line = font.lineHeight + leading #elselet advance = size * 0.6let line = size * 1.2 + leading #endifreturn (max(8, Int(width / advance)), max(1, Int(height / line))) }}publicenumFaceMeasureKnobs {/// A frame tall enough that no face's words run out of it.staticlet tall: CGFloat = 100_000/// The letters a word takes on average, and the space after it.publicstaticlet wordLetters: CGFloat = 4.7/// A proportional face's average character, widened so a line the core/// cuts never holds more than the face draws.staticlet 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.publicenumFaceMeasure {/// The letters a proportional face is averaged over.staticlet letters = "abcdefghijklmnopqrstuvwxyz"publicstaticfunc 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.publicstaticfunc lines(_ text: String, _ face: ThemeFace, _ size: CGFloat, width: CGFloat) -> Int {guard !text.isEmpty, width > 0else { return0 }let string = NSAttributedString( string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName asString): 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.publicstaticfunc 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 asString): 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 }}