ReadViewSDK/Sources/RDReaderView/EPUBCore/RDEPUBPaginator.swift
shen 54798ba578 refactor: 添加中文注释 + 优化模块结构
- 给全部 78 个 Swift 源文件添加详细的中文注释(文件级、类级、方法级)
- 删除 LegacyRDReaderController/ 死代码目录(16 文件 4592 行)
- 根目录翻页容器文件移入 ReaderView/ 目录
- Resources/ 移入 EPUBCore/Resources/(与使用者归属一致)
- RDEPUBTextIndexTable.swift 移入 EPUBTextRendering/(消除反向依赖)
- RDURLReaderController.swift 移入 EPUBUI/(入口控制器归入 UI 层)
- 更新 podspec 资源路径
2026-05-25 10:19:14 +08:00

384 lines
15 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// RDEPUBPaginator.swift
// EPUB
// 使 WKWebView spine JS scrollWidth
// 0ms/80ms/180ms
// Fixed Layout 1
import UIKit
import WebKit
/// EPUB WebView spine
public final class RDEPUBPaginator: NSObject {
///
private var parser: RDEPUBParser?
/// WebView
private weak var hostingView: UIView?
///
private var presentation = RDEPUBPresentationStyle(
viewportSize: .zero,
contentInsets: .zero,
fontSize: 16,
lineHeightMultiple: 1.5
)
/// spine
private var completion: (([Int]) -> Void)?
/// spine
private var singlePageCountCompletion: ((Int) -> Void)?
/// spine
private var pageCounts: [Int] = []
/// spine
private var measurementIndices: [Int] = []
/// measurementIndices
private var currentMeasurementOffset = 0
/// spine
private var pendingMeasurementValue = 1
/// 0/1/2 0ms/80ms/180ms
private var measurementPass = 0
/// ID
private var activeSessionID = UUID()
///
private let debugScope = "PaginatorWebView"
/// WebView
private lazy var webView: WKWebView = {
let configuration = WKWebViewConfiguration()
configuration.websiteDataStore = .nonPersistent()
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = self
webView.isOpaque = false
webView.backgroundColor = .clear
webView.scrollView.isScrollEnabled = false
webView.scrollView.bounces = false
webView.scrollView.showsHorizontalScrollIndicator = false
webView.scrollView.showsVerticalScrollIndicator = false
webView.isUserInteractionEnabled = false
if #available(iOS 16.4, *) {
webView.isInspectable = true
} else {
// Fallback on earlier versions
}
RDEPUBWebViewDebug.log("PaginatorWebView", message: "configured webView=\(RDEPUBWebViewDebug.webViewID(webView))")
return webView
}()
public override init() {
super.init()
}
deinit {
cleanupMeasurementState()
webView.navigationDelegate = nil
webView.stopLoading()
webView.removeFromSuperview()
}
/// spine
/// - Parameters:
/// - parser: EPUB
/// - hostingView: WebView
/// - presentation:
/// - completion: spine
public func calculate(
parser: RDEPUBParser,
hostingView: UIView,
presentation: RDEPUBPresentationStyle,
completion: @escaping ([Int]) -> Void
) {
self.parser = parser
self.hostingView = hostingView
self.presentation = presentation
self.completion = completion
self.singlePageCountCompletion = nil
self.pageCounts = Array(repeating: 1, count: parser.spine.count)
self.measurementIndices = Array(parser.spine.indices)
self.currentMeasurementOffset = 0
self.activeSessionID = UUID()
RDEPUBWebViewDebug.log(debugScope, message: "calculate session=\(activeSessionID) spineCount=\(parser.spine.count)")
guard parser.metadata.layout != .fixed else {
completion(self.pageCounts)
return
}
webView.navigationDelegate = self
if webView.superview == nil {
hostingView.addSubview(webView)
}
webView.frame = CGRect(origin: .zero, size: presentation.viewportSize)
measureNextSpineItem()
}
/// 便使 spine
public func calculate(
parser: RDEPUBParser,
hostingView: UIView,
viewportSize: CGSize,
padding: UIEdgeInsets,
fontSize: CGFloat,
lineHeightMultiple: CGFloat,
completion: @escaping ([Int]) -> Void
) {
calculate(
parser: parser,
hostingView: hostingView,
presentation: RDEPUBPresentationStyle(
viewportSize: viewportSize,
contentInsets: padding,
fontSize: fontSize,
lineHeightMultiple: lineHeightMultiple
),
completion: completion
)
}
/// spine
/// - Parameters:
/// - parser: EPUB
/// - spineIndex: spine
/// - hostingView: WebView
/// - presentation:
/// - completion: spine
public func calculateSingleSpinePageCount(
parser: RDEPUBParser,
spineIndex: Int,
hostingView: UIView,
presentation: RDEPUBPresentationStyle,
completion: @escaping (Int) -> Void
) {
self.parser = parser
self.hostingView = hostingView
self.presentation = presentation
self.completion = nil
self.singlePageCountCompletion = completion
self.pageCounts = Array(repeating: 1, count: parser.spine.count)
self.measurementIndices = parser.spine.indices.contains(spineIndex) ? [spineIndex] : []
self.currentMeasurementOffset = 0
self.activeSessionID = UUID()
RDEPUBWebViewDebug.log(debugScope, message: "calculateSingle session=\(activeSessionID) spineIndex=\(spineIndex)")
guard parser.metadata.layout != .fixed, !measurementIndices.isEmpty else {
completion(1)
return
}
webView.navigationDelegate = self
if webView.superview == nil {
hostingView.addSubview(webView)
}
webView.frame = CGRect(origin: .zero, size: presentation.viewportSize)
measureNextSpineItem()
}
/// 便使 spine
public func calculateSingleSpinePageCount(
parser: RDEPUBParser,
spineIndex: Int,
hostingView: UIView,
viewportSize: CGSize,
padding: UIEdgeInsets,
fontSize: CGFloat,
lineHeightMultiple: CGFloat,
completion: @escaping (Int) -> Void
) {
calculateSingleSpinePageCount(
parser: parser,
spineIndex: spineIndex,
hostingView: hostingView,
presentation: RDEPUBPresentationStyle(
viewportSize: viewportSize,
contentInsets: padding,
fontSize: fontSize,
lineHeightMultiple: lineHeightMultiple
),
completion: completion
)
}
/// CSS HTML
public static func injectPaginationCSS(
into html: String,
viewportSize: CGSize,
padding: UIEdgeInsets,
fontSize: CGFloat,
lineHeightMultiple: CGFloat,
themeBackgroundColor: String? = nil,
themeTextColor: String? = nil
) -> String {
RDEPUBStyleSheetBuilder.injectPaginationCSS(
into: html,
presentation: RDEPUBPresentationStyle(
viewportSize: viewportSize,
contentInsets: padding,
fontSize: fontSize,
lineHeightMultiple: lineHeightMultiple,
themeBackgroundColor: themeBackgroundColor,
themeTextColor: themeTextColor
)
)
}
/// spine WebView
private func measureNextSpineItem() {
guard let parser else {
finishIfNeeded()
return
}
guard currentMeasurementOffset < measurementIndices.count else {
finishIfNeeded()
return
}
let currentSpineIndex = measurementIndices[currentMeasurementOffset]
let item = parser.spine[currentSpineIndex]
guard isRenderablePage(item: item),
let opfDirectoryURL = parser.opfDirectoryURL else {
pageCounts[currentSpineIndex] = 1
currentMeasurementOffset += 1
measureNextSpineItem()
return
}
let fileURL = opfDirectoryURL.appendingPathComponent(item.href)
guard FileManager.default.fileExists(atPath: fileURL.path) else {
pageCounts[currentSpineIndex] = 1
currentMeasurementOffset += 1
measureNextSpineItem()
return
}
pendingMeasurementValue = 1
measurementPass = 0
let readAccessURL = parser.extractionRootURL ?? opfDirectoryURL
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "measure-load", url: fileURL)
webView.loadFileURL(fileURL, allowingReadAccessTo: readAccessURL)
}
/// spine
private func currentSpineIndexForMeasurement() -> Int? {
measurementIndices[safe: currentMeasurementOffset]
}
/// spine
private func finishIfNeeded() {
if let singlePageCountCompletion {
let measuredSpineIndex = measurementIndices.first ?? 0
singlePageCountCompletion(max(1, pageCounts[safe: measuredSpineIndex] ?? 1))
self.singlePageCountCompletion = nil
cleanupMeasurementState()
return
}
guard let completion else { return }
completion(pageCounts)
self.completion = nil
cleanupMeasurementState()
}
/// WebView
private func cleanupMeasurementState() {
parser = nil
hostingView = nil
completion = nil
singlePageCountCompletion = nil
measurementIndices = []
currentMeasurementOffset = 0
pendingMeasurementValue = 1
measurementPass = 0
activeSessionID = UUID()
webView.stopLoading()
webView.navigationDelegate = nil
webView.removeFromSuperview()
}
/// spine HTML/XHTML/XML
private func isRenderablePage(item: RDEPUBSpineItem) -> Bool {
let mediaType = item.mediaType.lowercased()
return mediaType.contains("html") || mediaType.contains("xhtml") || mediaType.contains("xml")
}
/// 0ms/80ms/180ms
private func scheduleMeasurementPass() {
let sessionID = activeSessionID
let delays: [TimeInterval] = [0.0, 0.08, 0.18]
guard measurementPass < delays.count else {
guard sessionID == activeSessionID,
let currentSpineIndex = currentSpineIndexForMeasurement() else {
return
}
pageCounts[currentSpineIndex] = max(1, pendingMeasurementValue)
currentMeasurementOffset += 1
measureNextSpineItem()
return
}
let delay = delays[measurementPass]
measurementPass += 1
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
guard let self, self.activeSessionID == sessionID else { return }
self.measureCurrentDocument(sessionID: sessionID)
}
}
/// JS scrollWidth
private func measureCurrentDocument(sessionID: UUID) {
let script = RDEPUBStyleSheetBuilder.measurementScript(for: presentation)
RDEPUBWebViewDebug.logJavaScript(debugScope, webView: webView, action: "measure", details: "session=\(sessionID) pass=\(measurementPass)")
webView.evaluateJavaScript(script) { [weak self] value, _ in
guard let self else { return }
guard self.activeSessionID == sessionID,
self.currentSpineIndexForMeasurement() != nil else {
return
}
if let number = value as? NSNumber {
self.pendingMeasurementValue = max(self.pendingMeasurementValue, number.intValue)
} else if let intValue = value as? Int {
self.pendingMeasurementValue = max(self.pendingMeasurementValue, intValue)
}
RDEPUBWebViewDebug.log(self.debugScope, message: "measurement value=\(self.pendingMeasurementValue) session=\(sessionID)")
self.scheduleMeasurementPass()
}
}
}
extension RDEPUBPaginator: WKNavigationDelegate {
public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "didStart", url: webView.url)
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "didFinish", url: webView.url)
guard currentSpineIndexForMeasurement() != nil else { return }
scheduleMeasurementPass()
}
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "didFail", url: webView.url, error: error)
let currentSpineIndex = measurementIndices[safe: currentMeasurementOffset] ?? 0
pageCounts[currentSpineIndex] = 1
currentMeasurementOffset += 1
measureNextSpineItem()
}
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "didFailProvisional", url: webView.url, error: error)
let currentSpineIndex = measurementIndices[safe: currentMeasurementOffset] ?? 0
pageCounts[currentSpineIndex] = 1
currentMeasurementOffset += 1
measureNextSpineItem()
}
public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
RDEPUBWebViewDebug.logNavigationEvent(debugScope, webView: webView, event: "processTerminated", url: webView.url)
}
}
/// Array nil
private extension Array {
subscript(safe index: Int) -> Element? {
indices.contains(index) ? self[index] : nil
}
}