// The open book as the core answers it (`shell.reader`), and the answers to// the reader's reports (`reader.moved`, `reader.highlight`,// `reader.settings`). Every decision is the core's (`phone::reader`): where// the book opens, when a place is written, the first open's Reading, the// face worn, each knob's words, the tiers and the highlights. The reader// decodes these and draws them; nothing here decides.//// Nothing here needs Readium, so it is tested on the Mac with the package.importCottonKitimportCottonMailboximportCottonValuesimportFoundation/// The reader's methods (`CottonMethod`'s, named where the reader calls them).enumReaderCalls {staticlet reader = CottonMethod.shellReaderstaticlet moved = CottonMethod.readerMovedstaticlet highlight = CottonMethod.readerHighlightstaticlet settings = CottonMethod.readerSettingsstaticlet seek = CottonMethod.readerSeekstaticlet seekSay = CottonMethod.readerSeekSaystaticlet seekGo = CottonMethod.readerSeekGostaticlet seekBack = CottonMethod.readerSeekBack}/// What the reader opens: a book's record, and a place to open at instead of/// its own (`reader.open`'s `at`, a highlight's place); or a kept page's/// record (`table` `page`, Cotton Pages), which opens in the pages' reader.publicstructReaderOpening: Identifiable, Hashable, Sendable {publiclet id: Stringpubliclet at: String?/// The record's table, as `reader.open` names it: `book` or `page`.publiclet table: Stringpublicinit(id: String, at: String? = nil, table: String = "book") {self.id = idself.at = atself.table = table }}/// Where the page stands, as the core says it.structReaderPlace: Equatable, Sendable, Decodable {/// Readium's Locator, serialised.let locator: Stringlet chapter: String?let progress: Int?/// The page number at the foot: a PDF's printed label, else Readium's position.let pageLabel: String?enumCodingKeys: String, CodingKey {case locator, chapter, progresscase pageLabel = "page_label" }}/// One tier of the reader's fan, as the core deals it.structReaderTierAnswer: Equatable, Sendable, Decodable {structCard: Equatable, Sendable, Decodable {let id: Stringlet label: Stringvar red: Bool?/// A knob's: what it is set to, and its choices.var words: String?var choices: ReaderTierAnswer?/// A knob's choice: the value it sets (`reader.settings`).var value: RecordValue? }let cards: [Card]var raised: String?}/// The reader's fan: the one tier the hand opens (the faces, then the/// knobs), the selection's pencils and a drawn highlight's acts.structReaderFanAnswer: Equatable, Sendable, Decodable {let hand: ReaderTierAnswerlet selection: ReaderTierAnswerlet highlight: ReaderTierAnswerlet noteHint: StringenumCodingKeys: String, CodingKey {case hand, selection, highlightcase noteHint = "note_hint" }}/// `shell.reader`'s answer: the book whole, or the line said in its place.structReaderAnswer: Sendable, Decodable {structRefusals: Sendable, Decodable {let locked: Stringlet unopened: String }let book: String/// Said in place of the book (locked, or no file the reader opens); nothing else is answered then.let refusal: String?let title: String?let file: String?/// `epub` or `pdf`.let format: String?let at: ReaderPlace?let progressWords: String?let settings: ReaderSettings?let fan: ReaderFanAnswer?let highlights: [BookHighlight]?let refusals: Refusals?enumCodingKeys: String, CodingKey {case book, refusal, title, file, format, at, settings, fan, highlights, refusalscase progressWords = "progress_words" }var bookFormat: BookFormat? { format.flatMap(BookFormat.init(rawValue:)) }}/// `reader.moved`'s answer: where the page stands and when to report again.structReaderMovedAnswer: Sendable, Decodable {let at: ReaderPlace?let progressWords: String?let wrote: Bool/// Report again (with no place) this long from now: a place waits to be written.let againMs: Int?/// The seek line's Back as it now stands.var back: ReaderBackAnswer?enumCodingKeys: String, CodingKey {case at, wrote, backcase progressWords = "progress_words"case againMs = "again_ms" }}/// The seek line's one Back, while it stands (`reader.seek`): where it/// returns to on the line, the slip's words, and how long it still stands.structReaderBackAnswer: Equatable, Sendable, Decodable {let at: Doublelet words: Stringlet msLeft: IntenumCodingKeys: String, CodingKey {case at, wordscase msLeft = "ms_left" }}/// `reader.seek`'s answer: the line whole. Every place is a share of the/// book, 0 its start and 1 its end.structReaderSeekAnswer: Equatable, Sendable, Decodable {/// One chapter's start; `here` marks the one the page stands in.structTick: Equatable, Sendable, Decodable {let at: Doublelet title: Stringlet here: Bool }structWords: Equatable, Sendable, Decodable {/// The slip's words for where the page stands: the line's value.let here: String/// The line's name, and the contents' heading.let line: Stringlet contents: String }/// Where one adjustable step lands, each way; nil at the book's end.structSteps: Equatable, Sendable, Decodable {let previous: Double?let next: Double? }let here: Doublelet ticks: [Tick]let pages: Int?let back: ReaderBackAnswer?let words: Wordslet steps: Steps}/// `reader.seek.say`'s answer: the slip's words for a dragged place.structReaderSeekSaid: Sendable, Decodable {let words: String}/// `reader.seek.go`'s and `reader.seek.back`'s answer: the Locator the/// navigator goes to (nil where no Back stood) and the Back left.structReaderSeekWent: Sendable, Decodable {let locator: String?let back: ReaderBackAnswer?}/// What the seek line asks: the book, a place on the line (with the line's/// length in points when it was a tap), or a tick of the contents.structReaderSeekAsk: Encodable, Sendable {var book: Stringvar at: Double?var width: Double?var tick: Int?}/// What the reader measures of the open book for the seek line, from/// Readium's positions: where each resource of the reading order begins as/// a share of the book, and a PDF's page count. What they mean on the line/// is the core's (`reader.moved`'s `contents`).enumReaderSeekMeasures {/// `firsts` is each resource's first position's share, in reading/// order (nil for a resource with no position, which begins where the/// one before it did). No starts are said unless every resource is/// accounted for, and they never run backwards.staticfunc of(firsts: [Double?], resources: Int, positions: Int, pdf: Bool) -> (starts: [Double]?, pages: Int?) {var starts: [Double]?if resources > 0, firsts.count == resources {var last = 0.0 starts = firsts.map { first in last = max(last, min(1, max(0, first ?? last)))return last } }return (starts, pdf && positions > 0 ? positions : nil) }/// How many chapter starts lie at or before a place: a change of it/// while the finger drags is a tick crossed.staticfunc ticksPassed(_ ticks: [Double], at part: Double) -> Int { ticks.reduce(0) { $0 + ($1 <= part ? 1 : 0) } }}/// `reader.settings`'s answer: the settings and the fan as they now stand.structReaderSettingsAnswer: Sendable, Decodable {let settings: ReaderSettingslet fan: ReaderFanAnswer}/// What the reader reports: a request's values, as the core reads them.structReaderReport: Encodable, Sendable {var book: String?var id: String?var at: String?var dark: Bool?var active: Bool?var closing: Bool?var pageLabel: String??var contents: Contents?var pencil: String?var words: String?var note: String?var remove: Bool?var face: String?var knob: String?var value: RecordValue?/// The book's contents in reading order, children after their parent.structContents: Encodable, Sendable {structEntry: Encodable, Sendable {let title: Stringlet href: String }let toc: [Entry]let readingOrder: [String]/// Where each resource of the reading order begins, as a share of the book.var starts: [Double]?/// The book's page count, where it has page numbers (a PDF).var pages: Int?enumCodingKeys: String, CodingKey {case toc, starts, pagescase readingOrder = "reading_order" } }enumCodingKeys: String, CodingKey {case book, id, at, dark, active, closing, contents, pencil, words, note, remove, face, knob, valuecase pageLabel = "page_label" }func encode(to encoder: any Encoder) throws {var container = encoder.container(keyedBy: CodingKeys.self)try container.encodeIfPresent(book, forKey: .book)try container.encodeIfPresent(id, forKey: .id)try container.encodeIfPresent(at, forKey: .at)try container.encodeIfPresent(dark, forKey: .dark)try container.encodeIfPresent(active, forKey: .active)try container.encodeIfPresent(closing, forKey: .closing)// A page with no printed label says so (null); a report that names no page leaves it out.iflet pageLabel { try container.encode(pageLabel, forKey: .pageLabel) }try container.encodeIfPresent(contents, forKey: .contents)try container.encodeIfPresent(pencil, forKey: .pencil)try container.encodeIfPresent(words, forKey: .words)try container.encodeIfPresent(note, forKey: .note)try container.encodeIfPresent(remove, forKey: .remove)try container.encodeIfPresent(face, forKey: .face)try container.encodeIfPresent(knob, forKey: .knob)try container.encodeIfPresent(value, forKey: .value) }}