// A face's longer words a page at a time: the room the face leaves them is // measured off the typewriter at the face's size (`FaceRoom`), the page // cutter the app installed cuts the text into pages for it // (`facePageCutter`), and a sideways swipe turns them, the page's label // ("2 / 3") at the room's foot. A text that fits is one page, drawn as it // is, with no label and no swipe; with no cutter installed the whole text is // one page. // // THE CUTTER. A face does not know who cuts its pages: the app installs the // core's cut (CottonUI's `CorePageCutter`, wherever it installs the core), // the Loom app and the face shots a plain one of their own // (`PlainPageCutter`). Every word is the cutter's. import CottonPaper import SwiftUI enum FacePagesKnobs { /// How far a swipe travels before it turns a page. static let turn: CGFloat = 40 static let labelSize: CGFloat = 10.5 /// The label's line under the pages. static let labelRoom: CGFloat = 18 } /// Cuts a face's words into pages of `first` lines, then `rest`, of /// `columns` characters, labelled "2 / 3". public protocol FacePageCutter: Sendable { func cut(_ text: String, columns: Int, first: Int, rest: Int) async -> [NotePage] } extension EnvironmentValues { /// Who cuts a face's words into pages; nil leaves them one page. @Entry public var facePageCutter: (any FacePageCutter)? = nil } /// A cut by the words alone, with no core: each line wrapped at its /// characters, between words where it can, and the lines dealt into pages. /// What the specimens are cut by in the Loom app and the face shots. public struct PlainPageCutter: FacePageCutter { public init() {} public func cut(_ text: String, columns: Int, first: Int, rest: Int) async -> [NotePage] { Self.pages(text, columns: columns, first: first, rest: rest) } static func pages(_ text: String, columns: Int, first: Int, rest: Int) -> [NotePage] { let lines = text.components(separatedBy: "\n").flatMap { wrap($0, columns: max(1, columns)) } var cut: [[String]] = [] var left = lines[...] while !left.isEmpty { let room = max(1, cut.isEmpty ? first : rest) cut.append(Array(left.prefix(room))) left = left.dropFirst(room) } guard cut.count > 1 else { return [NotePage(text: text, label: "")] } return cut.enumerated().map { index, page in NotePage(text: page.joined(separator: "\n"), label: "\(index + 1) / \(cut.count)") } } /// One line wrapped at `columns` characters, between words where a word fits. static func wrap(_ line: String, columns: Int) -> [String] { var wrapped: [String] = [] var current = "" for word in line.split(separator: " ", omittingEmptySubsequences: false).map(String.init) { var word = word while word.count > columns { if !current.isEmpty { wrapped.append(current); current = "" } wrapped.append(String(word.prefix(columns))) word = String(word.dropFirst(columns)) } if current.isEmpty { current = word } else if current.count + 1 + word.count <= columns { current += " " + word } else { wrapped.append(current) current = word } } wrapped.append(current) return wrapped } } public struct FacePages: View { let text: String let size: CGFloat var leading: CGFloat = 4 var ink: PaperInk = .ink /// The face the words are set in (a business's reading face), the /// typewriter unless named. var face: ThemeFace = .courier /// A colour the words are set in instead of the ink (a business's body). var colour: Color? /// The words are Markdown: each page is set (`FaceMarkdown`), never /// typed with its marks. var markdown = false @Environment(\.facePageCutter) private var cutter @Environment(\.theme) private var theme @State private var pages: [NotePage] = [] @State private var page = 0 @Environment(\.deckPassOn) private var passOn public init( text: String, size: CGFloat, leading: CGFloat = 4, ink: PaperInk = .ink, face: ThemeFace = .courier, colour: Color? = nil, markdown: Bool = false ) { self.text = text self.size = size self.leading = leading self.ink = ink self.face = face self.colour = colour self.markdown = markdown } public var body: some View { GeometryReader { room in let shown = pages.isEmpty ? [NotePage(text: text, label: "")] : pages let current = shown[min(page, shown.count - 1)] VStack(alignment: .leading, spacing: 0) { Text(markdown ? FaceMarkdown.print(FaceMarkdown.Document(text, theme: theme).pages(shown.map(\.text))[min(page, shown.count - 1)], size: size, font: face == .courier ? CottonFonts.type(size) : face.font(size)) : AttributedString(current.text)) .font(face == .courier ? CottonFonts.type(size) : face.font(size)) .lineSpacing(leading) .modifier(PagesInk(ink: ink, colour: colour)) .frame(maxWidth: .infinity, alignment: .topLeading) .accessibilityIdentifier("face-page") Spacer(minLength: 0) if shown.count > 1 { Text(current.label) .font(CottonFonts.type(FacePagesKnobs.labelSize)) .faceInk(.pencil) .frame(maxWidth: .infinity, alignment: .trailing) .accessibilityIdentifier("face-page-label") } } .clipped() .contentShape(Rectangle()) // A tap turns to the next page, the last back to the first; a // swipe turns either way, and past either end moves card. .onTapGesture { if shown.count > 1 { page = (page + 1) % shown.count } } .facePages(shown.count > 1 ? turning(count: shown.count) : nil, at: "\(page)/\(shown.count)") { forward in let next = page + (forward ? 1 : -1) if (0.. 0 else { return } // The label takes its line only when there is more than one page. let whole = FaceRoom.measure(width: room.size.width, height: room.size.height, size: size, leading: leading, face: face) let paged = FaceRoom.measure( width: room.size.width, height: room.size.height - FacePagesKnobs.labelRoom, size: size, leading: leading, face: face) let lines = text.count <= whole.columns * whole.lines ? whole.lines : paged.lines let cut = await cutter.cut(text, columns: whole.columns, first: lines, rest: lines) // A cut for a room since left is never shown (`NoteCard`). guard !Task.isCancelled else { return } pages = cut page = min(page, max(0, pages.count - 1)) } } } private func turning(count: Int) -> some Gesture { DragGesture(minimumDistance: 16) .onEnded { end in let dx = end.translation.width guard abs(dx) > FacePagesKnobs.turn, abs(dx) > abs(end.translation.height) else { return } let next = page + (dx < 0 ? 1 : -1) if (0.. some View { if let colour { content.foregroundStyle(colour).blendMode(theme.printBlend) } else { content.faceInk(ink) } } }