Cotton code
102 lines · 4.5 KB · SwiftRaw
// The reader's face and knobs, as Readium takes them.//// An EPUB's page is a web view Readium styles with Readium CSS: the face's// ground and ink become its background and text colours (never Readium's// own light, sepia or night themes, which are not these faces), the knobs// its size, line height, margins, font and layout. A PDF's page is PDFKit:// its ground is the face's, and on Night the page is dimmed through a// multiply overlay (`ReaderDimming`), never inverted, so a picture on the// page stays a picture.//// Serif is Libre Caslon, the book Caslon CottonPaper already carries;// Sans is the system's face; Dyslexic is OpenDyslexic, which Readium 3.11.1// carries and declares itself (its navigator's `fontFamilyDeclarations`),// so the app bundles no second copy (licence: `Resources/Licenses/OpenDyslexic.txt`).#if os(iOS)import CottonPaperimport ReadiumNavigatorimport ReadiumSharedimport SwiftUIimport UIKitenum ReaderLook {    /// Libre Caslon, as the EPUB's page names it: by its face's name.    static var caslon: FontFamily { FontFamily(rawValue: CottonFonts.caslonFace) }    /// What the EPUB navigator is made with: the page's preferences and the    /// one family it is handed (Readium declares OpenDyslexic itself).    static func epubConfiguration(_ settings: ReaderSettings, face: ReaderFace) -> EPUBNavigatorViewController.Configuration {        var config = EPUBNavigatorViewController.Configuration(            preferences: epub(settings, face: face),            // Copy stays; Look Up, Translate and Share leave the book's menu:            // a selection's own acts are the selection tier at the hub.            editingActions: [.copy],            preloadPreviousPositionCount: 2, preloadNextPositionCount: 6,            // A highlight is a pencil line under the words, never a fill.            decorationTemplates: HTMLDecorationTemplate.defaultTemplates(lineWeight: 2, alpha: 0)        )        if let file = CottonFonts.file(CottonFonts.caslonFace, extension: "ttf").flatMap(FileURL.init(url:)) {            config.fontFamilyDeclarations.append(                CSSFontFamilyDeclaration(fontFamily: caslon, fontFaces: [CSSFontFace(file: file, style: .normal, weight: .standard(.normal))])                    .eraseToAnyHTMLFontFamilyDeclaration())        }        return config    }    /// The EPUB's page in a face, set by the knobs.    static func epub(_ settings: ReaderSettings, face: ReaderFace) -> EPUBPreferences {        let knobs = settings.knobs        return EPUBPreferences(            backgroundColor: colour(face.material.ground),            fontFamily: family(knobs.typeface),            fontSize: knobs.size,            fontWeight: knobs.bold ? 1.6 : nil,            lineHeight: knobs.lineHeight,            pageMargins: knobs.margins,            publisherStyles: knobs.publisherStyles,            scroll: knobs.scroll,            textAlign: knobs.justify ? .justify : nil,            textColor: colour(face.material.ink)        )    }    /// The PDF's page: the face's ground around it, fitted as the row says.    static func pdf(_ settings: ReaderSettings, face: ReaderFace) -> PDFPreferences {        PDFPreferences(backgroundColor: colour(face.material.ground), fit: settings.knobs.pdfWholePage ? .page : .width,                       scroll: settings.knobs.scroll, spread: .never)    }    static func family(_ typeface: ReaderTypeface) -> FontFamily? {        switch typeface {        case .publisher: nil        case .serif: caslon        case .sans: .sansSerif        case .dyslexic: .openDyslexic        }    }    /// A material as Readium's colour, from its channels.    static func colour(_ material: BookFaceMaterial) -> ReadiumNavigator.Color {        let (red, green, blue) = material.channels        let byte = { (channel: Double) in Int((channel * 255).rounded()) }        return ReadiumNavigator.Color(rawValue: byte(red) << 16 | byte(green) << 8 | byte(blue))    }}/// Night on a PDF: a sheet over the page in the face's secondary ink,/// multiplied, so white paper reads as a dim warm grey and the type stays/// black. Untouchable: every touch goes to the page under it.final class ReaderDimming: UIView {    init() {        super.init(frame: .zero)        isUserInteractionEnabled = false        backgroundColor = UIColor(BookFaceMaterial.nightSecondary.colour)        layer.compositingFilter = "multiplyBlendMode"        accessibilityIdentifier = "reader-dimming"    }    @available(*, unavailable)    required init?(coder: NSCoder) { nil }}#endif