feat: 支持加密 EPUB、试读墙、工具栏定制与朝向锁定
- 新增 RDEPUBResourceDataProvider 解密钩子协议 + 访问登记表,收敛章节 HTML/ 图片/内联 CSS/脚注/封面/图片查看器/正文取图等读取点,scheme handler 对加密书 禁用流式分支;新增 RDEPUBDecryptingImageAttachment 解密 DTCoreText 图片附件; RDEPUBReaderDependencies.live(resourceDataProvider:) 便捷注入。明文书零影响。 - 试读墙:configuration.trialPolicy + delegate epubReaderTrialWallView/ DidReachTrialWall,UI 由宿主提供(RDEPUBReaderController+Trial)。 - 工具栏定制:RDEPUBReaderTop/BottomToolViewProtocol 协议 + dependencies 工厂注入, 内置栏已 conform,configureTopToolView 改协议类型。 - 朝向锁定:RDEPUBMetadata.orientation(OPF rendition:orientation 主, iBooks display-options 兜底)+ RDEPUBReaderController+Orientation 重写支持朝向、 打开后主动转向。 注:RDEPUBReaderController+LocationResolution / PaginationCoordinator 为本次改动前 即存在的工作区修改,一并纳入。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
83dfa40299
commit
d5a7755702
@@ -59,13 +59,22 @@ public final class RDEPUBNoteResolver {
|
||||
guard let fragment,
|
||||
!fragment.isEmpty,
|
||||
let fileURL = resourceResolver.fileURL(forRelativePath: href),
|
||||
let html = try? String(contentsOf: fileURL, encoding: .utf8) else {
|
||||
let html = chapterHTML(at: fileURL) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return elementHTML(withID: fragment, in: html)
|
||||
}
|
||||
|
||||
/// 读取章节 HTML:优先经加密资源 provider 解密,明文文件保持原直读路径
|
||||
private func chapterHTML(at fileURL: URL) -> String? {
|
||||
if let provided = resourceResolver.providedResourceData(at: fileURL) {
|
||||
return String(data: provided, encoding: .utf8)
|
||||
?? String(data: provided, encoding: .utf16)
|
||||
}
|
||||
return try? String(contentsOf: fileURL, encoding: .utf8)
|
||||
}
|
||||
|
||||
private func elementHTML(withID id: String, in html: String) -> String? {
|
||||
let escapedID = NSRegularExpression.escapedPattern(for: id)
|
||||
let pattern = #"<([A-Za-z][A-Za-z0-9:_-]*)(?=[^>]*(?:id|xml:id)\s*=\s*(['"])"# + escapedID + #"\2)[^>]*>"#
|
||||
|
||||
@@ -24,6 +24,13 @@ public enum RDEPUBPageSpread: String, Codable {
|
||||
case center
|
||||
}
|
||||
|
||||
/// EPUB 渲染朝向(rendition:orientation / iBooks display-options)。
|
||||
public enum RDEPUBOrientation: String, Codable {
|
||||
case portrait
|
||||
case landscape
|
||||
case auto
|
||||
}
|
||||
|
||||
public struct RDEPUBMetadata: Codable, Equatable {
|
||||
|
||||
public var identifier: String?
|
||||
@@ -42,6 +49,10 @@ public struct RDEPUBMetadata: Codable, Equatable {
|
||||
|
||||
public var readingProgression: RDEPUBReadingProgression
|
||||
|
||||
/// 渲染朝向;nil = 未声明(不锁定,跟随系统)。
|
||||
/// 来源优先级:OPF rendition:orientation > META-INF/com.apple.ibooks.display-options.xml。
|
||||
public var orientation: RDEPUBOrientation?
|
||||
|
||||
public init(
|
||||
identifier: String? = nil,
|
||||
title: String = "",
|
||||
@@ -50,7 +61,8 @@ public struct RDEPUBMetadata: Codable, Equatable {
|
||||
version: String? = nil,
|
||||
layout: RDEPUBLayout = .reflowable,
|
||||
spread: String? = nil,
|
||||
readingProgression: RDEPUBReadingProgression = .auto
|
||||
readingProgression: RDEPUBReadingProgression = .auto,
|
||||
orientation: RDEPUBOrientation? = nil
|
||||
) {
|
||||
self.identifier = identifier
|
||||
self.title = title
|
||||
@@ -60,6 +72,7 @@ public struct RDEPUBMetadata: Codable, Equatable {
|
||||
self.layout = layout
|
||||
self.spread = spread
|
||||
self.readingProgression = readingProgression
|
||||
self.orientation = orientation
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -151,3 +151,48 @@ private final class ContainerXMLParserDelegate: NSObject, XMLParserDelegate {
|
||||
rootFilePath = attributeDict["full-path"]?.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析 com.apple.ibooks.display-options.xml,提取 orientation-lock 选项值。
|
||||
final class IBooksDisplayOptionsParserDelegate: NSObject, XMLParserDelegate {
|
||||
|
||||
private(set) var orientationLock: String?
|
||||
|
||||
private var isCapturingOrientationOption = false
|
||||
private var buffer = ""
|
||||
|
||||
func parser(
|
||||
_ parser: XMLParser,
|
||||
didStartElement elementName: String,
|
||||
namespaceURI: String?,
|
||||
qualifiedName qName: String?,
|
||||
attributes attributeDict: [String: String] = [:]
|
||||
) {
|
||||
let name = XMLName.localName(from: qName ?? elementName)
|
||||
guard name == "option" else { return }
|
||||
let optionName = (attributeDict["name"] ?? "").lowercased()
|
||||
if optionName == "orientation-lock" {
|
||||
isCapturingOrientationOption = true
|
||||
buffer = ""
|
||||
}
|
||||
}
|
||||
|
||||
func parser(_ parser: XMLParser, foundCharacters string: String) {
|
||||
guard isCapturingOrientationOption else { return }
|
||||
buffer += string
|
||||
}
|
||||
|
||||
func parser(
|
||||
_ parser: XMLParser,
|
||||
didEndElement elementName: String,
|
||||
namespaceURI: String?,
|
||||
qualifiedName qName: String?
|
||||
) {
|
||||
let name = XMLName.localName(from: qName ?? elementName)
|
||||
guard name == "option", isCapturingOrientationOption else { return }
|
||||
isCapturingOrientationOption = false
|
||||
let value = buffer.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if !value.isEmpty, orientationLock == nil {
|
||||
orientationLock = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +180,11 @@ final class OPFPackageParserDelegate: NSObject, XMLParserDelegate {
|
||||
property == "rendition:spread" {
|
||||
metadata.spread = attributeDict["content"]
|
||||
}
|
||||
if let property = currentMetaProperty?.lowercased(),
|
||||
property == "rendition:orientation",
|
||||
let value = attributeDict["content"] {
|
||||
metadata.orientation = orientation(from: value)
|
||||
}
|
||||
if let property = currentMetaProperty?.lowercased(),
|
||||
property == "title",
|
||||
let refinesID = currentMetaRefinesID,
|
||||
@@ -270,6 +275,8 @@ final class OPFPackageParserDelegate: NSObject, XMLParserDelegate {
|
||||
metadata.layout = layout(from: normalizedValue)
|
||||
case "rendition:spread":
|
||||
metadata.spread = normalizedValue
|
||||
case "rendition:orientation":
|
||||
metadata.orientation = orientation(from: normalizedValue)
|
||||
case "dcterms:identifier", "identifier":
|
||||
if metadata.identifier == nil {
|
||||
metadata.identifier = normalizedValue
|
||||
@@ -315,6 +322,19 @@ final class OPFPackageParserDelegate: NSObject, XMLParserDelegate {
|
||||
private func layout(from rawValue: String) -> RDEPUBLayout {
|
||||
rawValue.lowercased().contains("pre-paginated") ? .fixed : .reflowable
|
||||
}
|
||||
|
||||
private func orientation(from rawValue: String) -> RDEPUBOrientation? {
|
||||
switch rawValue.lowercased().trimmingCharacters(in: .whitespacesAndNewlines) {
|
||||
case "portrait":
|
||||
return .portrait
|
||||
case "landscape":
|
||||
return .landscape
|
||||
case "auto":
|
||||
return .auto
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum XMLSection {
|
||||
|
||||
@@ -62,7 +62,8 @@ extension RDEPUBParser {
|
||||
}
|
||||
for item in coverCandidates {
|
||||
if let fileURL = fileURL(forRelativePath: item.href),
|
||||
let image = UIImage(contentsOfFile: fileURL.path) {
|
||||
let data = resourceData(at: fileURL),
|
||||
let image = UIImage(data: data) {
|
||||
return image
|
||||
}
|
||||
}
|
||||
@@ -80,6 +81,10 @@ extension RDEPUBParser {
|
||||
guard let fileURL = fileURL(forRelativePath: relativePath) else {
|
||||
return nil
|
||||
}
|
||||
if let provided = providedResourceData(at: fileURL) {
|
||||
return String(data: provided, encoding: .utf8)
|
||||
?? String(data: provided, encoding: .utf16)
|
||||
}
|
||||
return try? String(contentsOf: fileURL)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,28 @@ public final class RDEPUBParser {
|
||||
opfURL?.deletingLastPathComponent()
|
||||
}
|
||||
|
||||
/// 加密资源数据提供者;nil 时所有资源按明文直读。
|
||||
/// 必须在 `parse(epubURL:)` 之前设置(推荐通过 `RDEPUBReaderDependencies.live(resourceDataProvider:)` 注入)。
|
||||
public var resourceDataProvider: RDEPUBResourceDataProvider? {
|
||||
didSet {
|
||||
guard resourceDataProvider != nil else { return }
|
||||
RDEPUBResourceAccessRegistry.register(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// 仅经 provider 获取解密数据;无 provider 或 provider 判定无需解密时返回 nil
|
||||
public func providedResourceData(at fileURL: URL) -> Data? {
|
||||
resourceDataProvider?.resourceData(at: fileURL)
|
||||
}
|
||||
|
||||
/// 统一资源读取入口:优先 provider 解密,其次磁盘明文
|
||||
public func resourceData(at fileURL: URL) -> Data? {
|
||||
if let provided = providedResourceData(at: fileURL) {
|
||||
return provided
|
||||
}
|
||||
return try? Data(contentsOf: fileURL)
|
||||
}
|
||||
|
||||
public init() {}
|
||||
|
||||
// MARK: - Cache Management
|
||||
@@ -101,6 +123,37 @@ public final class RDEPUBParser {
|
||||
}
|
||||
|
||||
try parseOPF(at: packageURL)
|
||||
|
||||
// 兜底:OPF 未声明朝向时,回退读取 iBooks display-options
|
||||
if metadata.orientation == nil {
|
||||
let displayOptionsURL = extractionURL
|
||||
.appendingPathComponent("META-INF/com.apple.ibooks.display-options.xml")
|
||||
if let orientation = parseIBooksOrientation(at: displayOptionsURL) {
|
||||
metadata.orientation = orientation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析 `com.apple.ibooks.display-options.xml` 中的 orientation-lock。
|
||||
/// 结构:platform[@name] / option[@name="orientation-lock"] 文本值。
|
||||
private func parseIBooksOrientation(at url: URL) -> RDEPUBOrientation? {
|
||||
guard FileManager.default.fileExists(atPath: url.path),
|
||||
let parser = XMLParser(contentsOf: url) else {
|
||||
return nil
|
||||
}
|
||||
let delegate = IBooksDisplayOptionsParserDelegate()
|
||||
parser.delegate = delegate
|
||||
guard parser.parse() else { return nil }
|
||||
switch delegate.orientationLock?.lowercased() {
|
||||
case "portrait-orientation", "portrait":
|
||||
return .portrait
|
||||
case "landscape-orientation", "landscape":
|
||||
return .landscape
|
||||
case "none":
|
||||
return .auto
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
public func parseOPF(at opfURL: URL) throws {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import Foundation
|
||||
|
||||
/// 加密 EPUB 资源数据提供者。
|
||||
///
|
||||
/// 宿主 App 实现该协议以支持“解压目录内单文件加密”的 EPUB:
|
||||
/// SDK 读取章节 HTML、图片、内联 CSS 等资源时会优先向 provider 索取解密后的数据,
|
||||
/// provider 返回 nil 表示该文件无需解密,SDK 回退为直接读取磁盘明文
|
||||
/// (明文书、或加密书内的明文文件均走该兜底路径,因此对明文书零影响)。
|
||||
///
|
||||
/// 密钥推导与解密算法完全由宿主实现,SDK 不持有任何密钥。
|
||||
///
|
||||
/// 注意:方法会在排版 / 渲染的后台队列被同步调用,实现必须线程安全,
|
||||
/// 且除必要的文件 I/O 与解密计算外不得阻塞(例如不要在其中发起网络请求)。
|
||||
public protocol RDEPUBResourceDataProvider: AnyObject {
|
||||
|
||||
/// 返回指定文件解密后的数据。
|
||||
/// - Parameter fileURL: 解压目录内资源文件的绝对路径
|
||||
/// - Returns: 解密后的数据;返回 nil 表示该文件不需要解密,由 SDK 直读磁盘
|
||||
func resourceData(at fileURL: URL) -> Data?
|
||||
}
|
||||
|
||||
/// 加密资源访问登记表。
|
||||
///
|
||||
/// DTCoreText 在解析 `<img>` 时由自己内部加载图片文件,拿不到 parser 实例,
|
||||
/// 因此这里以弱引用登记所有启用了 provider 的 parser,
|
||||
/// 供 `RDEPUBDecryptingImageAttachment` 按文件路径反查所属书籍的 provider。
|
||||
enum RDEPUBResourceAccessRegistry {
|
||||
|
||||
private struct WeakParserBox {
|
||||
weak var parser: RDEPUBParser?
|
||||
}
|
||||
|
||||
private static let lock = NSLock()
|
||||
|
||||
private static var boxes: [WeakParserBox] = []
|
||||
|
||||
/// 登记启用了 provider 的 parser(重复登记会被去重,弱引用失效项顺带清理)
|
||||
static func register(_ parser: RDEPUBParser) {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
boxes.removeAll { $0.parser == nil || $0.parser === parser }
|
||||
boxes.append(WeakParserBox(parser: parser))
|
||||
#if canImport(DTCoreText)
|
||||
RDEPUBDecryptingImageAttachment.registerTagClassIfNeeded()
|
||||
#endif
|
||||
}
|
||||
|
||||
/// 按文件路径反查 provider 并返回解密数据;
|
||||
/// 仅当文件位于某个已登记 parser 的解压目录内才会命中
|
||||
static func providedResourceData(at fileURL: URL) -> Data? {
|
||||
let targetPath = fileURL.resolvingSymlinksInPath().standardizedFileURL.path
|
||||
lock.lock()
|
||||
let parsers = boxes.compactMap(\.parser)
|
||||
lock.unlock()
|
||||
for parser in parsers {
|
||||
guard let rootURL = parser.extractionRootURL else { continue }
|
||||
let rootPath = rootURL.resolvingSymlinksInPath().standardizedFileURL.path
|
||||
guard targetPath.hasPrefix(rootPath + "/") else { continue }
|
||||
return parser.providedResourceData(at: fileURL)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,21 @@ public final class RDEPUBResourceResolver {
|
||||
parser.fileURL(forResourceURL: resourceURL)
|
||||
}
|
||||
|
||||
/// 是否配置了加密资源数据提供者
|
||||
public var hasResourceDataProvider: Bool {
|
||||
parser.resourceDataProvider != nil
|
||||
}
|
||||
|
||||
/// 仅经 provider 获取解密数据;无 provider 或 provider 判定无需解密时返回 nil
|
||||
public func providedResourceData(at fileURL: URL) -> Data? {
|
||||
parser.providedResourceData(at: fileURL)
|
||||
}
|
||||
|
||||
/// 统一资源读取入口:优先 provider 解密,其次磁盘明文
|
||||
public func resourceData(at fileURL: URL) -> Data? {
|
||||
parser.resourceData(at: fileURL)
|
||||
}
|
||||
|
||||
public func normalizedHref(_ href: String, relativeToSpineIndex spineIndex: Int? = nil) -> String? {
|
||||
guard let opfDirectoryURL else {
|
||||
return href.components(separatedBy: "#").first
|
||||
|
||||
@@ -87,7 +87,9 @@ public final class RDEPUBResourceURLSchemeHandler: NSObject, WKURLSchemeHandler
|
||||
RDEPUBWebViewDebug.logSchemeTask("ResourceScheme", requestURL: requestURL, fileURL: fileURL, event: "resolved")
|
||||
|
||||
let fileSize = resourceSize(for: fileURL)
|
||||
if let fileSize, fileSize > 524_288 {
|
||||
// 配置了加密资源 provider 时不能走流式分支:密文必须整体解密后再响应,
|
||||
// 统一改走内存分支(内部会先询问 provider,明文文件仍按磁盘直读)
|
||||
if let fileSize, fileSize > 524_288, parser.resourceDataProvider == nil {
|
||||
respondWithStreaming(fileURL: fileURL, requestURL: requestURL, taskID: taskID, urlSchemeTask: urlSchemeTask, fileSize: fileSize)
|
||||
} else {
|
||||
respondWithInMemoryData(fileURL: fileURL, requestURL: requestURL, taskID: taskID, urlSchemeTask: urlSchemeTask)
|
||||
@@ -133,7 +135,13 @@ public final class RDEPUBResourceURLSchemeHandler: NSObject, WKURLSchemeHandler
|
||||
return
|
||||
}
|
||||
do {
|
||||
let data = try Data(contentsOf: fileURL)
|
||||
// 优先向加密资源 provider 索取解密数据,provider 放行的明文文件仍直读磁盘
|
||||
let data: Data
|
||||
if let provided = self.parser?.providedResourceData(at: fileURL) {
|
||||
data = provided
|
||||
} else {
|
||||
data = try Data(contentsOf: fileURL)
|
||||
}
|
||||
guard self.isTaskActive(taskID) else {
|
||||
DispatchQueue.main.async {
|
||||
urlSchemeTask.didFailWithError(NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled))
|
||||
|
||||
Reference in New Issue
Block a user