Epub阅读器0.0.1
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import UIKit
|
||||
import WebKit
|
||||
|
||||
extension RDEPUBWebView {
|
||||
public func loadFixedSpread(
|
||||
parser: RDEPUBParser,
|
||||
spread: EPUBFixedSpread,
|
||||
viewportSize: CGSize,
|
||||
contentInset: UIEdgeInsets,
|
||||
backgroundColor: UIColor?
|
||||
) {
|
||||
let request = RDEPUBRenderRequest.fixed(
|
||||
RDEPUBFixedRenderRequest(
|
||||
spread: spread,
|
||||
viewportSize: viewportSize,
|
||||
contentInset: contentInset,
|
||||
backgroundColorCSS: backgroundColor?.ss_hexString,
|
||||
fit: .page,
|
||||
searchPresentation: nil
|
||||
)
|
||||
)
|
||||
load(publication: parser.makePublication(), request: request)
|
||||
}
|
||||
|
||||
func handleFixedLayoutLoad(
|
||||
publication: RDEPUBPublication,
|
||||
request: RDEPUBFixedRenderRequest,
|
||||
loadSignature: String,
|
||||
in webView: WKWebView
|
||||
) {
|
||||
currentPageIndex = 0
|
||||
currentTotalPagesInChapter = 1
|
||||
viewportSize = request.viewportSize
|
||||
currentPadding = request.contentInset
|
||||
currentFontSize = 16
|
||||
currentLineHeightMultiple = 1.5
|
||||
currentThemeBackgroundColor = request.backgroundColorCSS
|
||||
currentThemeTextColor = nil
|
||||
targetLocation = nil
|
||||
pendingHighlights = []
|
||||
fixedSpread = request.spread
|
||||
isFixedLayout = true
|
||||
pendingProgressionRequest = false
|
||||
currentLoadSignature = loadSignature
|
||||
scheduleFixedLayoutReadyFallback()
|
||||
|
||||
let html = RDEPUBFixedLayoutTemplate.html(for: request, publication: publication)
|
||||
RDEPUBWebViewDebug.log(debugScope, message: "load fixed spread resources=\(request.spread.resources.map(\.href).joined(separator: ",")) fit=\(request.fit.rawValue)")
|
||||
webView.loadHTMLString(
|
||||
html,
|
||||
baseURL: URL(string: "\(RDEPUBResourceURLSchemeHandler.scheme)://\(RDEPUBResourceURLSchemeHandler.host)/")
|
||||
)
|
||||
}
|
||||
|
||||
func fixedLayoutLoadSignature(publicationKey: String, request: RDEPUBFixedRenderRequest) -> String {
|
||||
[
|
||||
publicationKey,
|
||||
"fixed",
|
||||
request.spread.resources.map(\.href).joined(separator: "|"),
|
||||
String(format: "%.2f", request.viewportSize.width),
|
||||
String(format: "%.2f", request.viewportSize.height),
|
||||
String(format: "%.2f", request.contentInset.top),
|
||||
String(format: "%.2f", request.contentInset.left),
|
||||
String(format: "%.2f", request.contentInset.bottom),
|
||||
String(format: "%.2f", request.contentInset.right),
|
||||
request.backgroundColorCSS ?? "",
|
||||
request.fit.rawValue
|
||||
].joined(separator: "#")
|
||||
}
|
||||
|
||||
func scheduleFixedLayoutReadyFallback() {
|
||||
let workItem = DispatchWorkItem { [weak self] in
|
||||
RDEPUBWebViewDebug.log(self?.debugScope ?? "ReaderWebView", message: "fixed ready fallback fired")
|
||||
self?.applySearchDecorationsIfNeeded {
|
||||
self?.rendered()
|
||||
}
|
||||
}
|
||||
fixedLayoutReadyWorkItem = workItem
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: workItem)
|
||||
}
|
||||
|
||||
func cancelFixedLayoutReadyFallback() {
|
||||
fixedLayoutReadyWorkItem?.cancel()
|
||||
fixedLayoutReadyWorkItem = nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user