react-native-pdf-sdk 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/PdfSdk.podspec +32 -0
- package/README.md +270 -2
- package/android/CMakeLists.txt +24 -0
- package/android/build.gradle +132 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +9 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfDocument.kt +72 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfSdk.kt +17 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/HybridPdfView.kt +194 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfDocumentLayout.kt +109 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfFailure.kt +36 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfPageSource.kt +141 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSdkPackage.kt +35 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSourceLoader.kt +117 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfSurface.kt +839 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfTileStore.kt +210 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/PdfViewport.kt +255 -0
- package/android/src/main/java/com/margelo/nitro/pdfsdk/TileGrid.kt +276 -0
- package/ios/HybridPdfDocument.swift +71 -0
- package/ios/HybridPdfSdk.swift +10 -0
- package/ios/HybridPdfView.swift +202 -0
- package/ios/PdfDocumentLayout.swift +174 -0
- package/ios/PdfFailure.swift +46 -0
- package/ios/PdfImageCache.swift +83 -0
- package/ios/PdfPageSource.swift +221 -0
- package/ios/PdfScrollPlan.swift +108 -0
- package/ios/PdfSourceLoader.swift +120 -0
- package/ios/PdfSurface.swift +711 -0
- package/ios/PdfTileCanvas.swift +148 -0
- package/ios/PdfTileGrid.swift +518 -0
- package/ios/PdfTileStore.swift +346 -0
- package/ios/PdfViewport.swift +200 -0
- package/lib/commonjs/PdfView.js +149 -0
- package/lib/commonjs/PdfView.js.map +1 -0
- package/lib/commonjs/document.js +38 -0
- package/lib/commonjs/document.js.map +1 -0
- package/lib/commonjs/index.js +26 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/specs/PdfSdk.nitro.js +6 -0
- package/lib/commonjs/specs/PdfSdk.nitro.js.map +1 -0
- package/lib/commonjs/specs/PdfView.nitro.js +6 -0
- package/lib/commonjs/specs/PdfView.nitro.js.map +1 -0
- package/lib/module/PdfView.js +143 -0
- package/lib/module/PdfView.js.map +1 -0
- package/lib/module/document.js +34 -0
- package/lib/module/document.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/specs/PdfSdk.nitro.js +4 -0
- package/lib/module/specs/PdfSdk.nitro.js.map +1 -0
- package/lib/module/specs/PdfView.nitro.js +4 -0
- package/lib/module/specs/PdfView.nitro.js.map +1 -0
- package/lib/typescript/src/PdfView.d.ts +65 -0
- package/lib/typescript/src/PdfView.d.ts.map +1 -0
- package/lib/typescript/src/document.d.ts +22 -0
- package/lib/typescript/src/document.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/specs/PdfSdk.nitro.d.ts +60 -0
- package/lib/typescript/src/specs/PdfSdk.nitro.d.ts.map +1 -0
- package/lib/typescript/src/specs/PdfView.nitro.d.ts +131 -0
- package/lib/typescript/src/specs/PdfView.nitro.d.ts.map +1 -0
- package/nitro.json +40 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/PdfSdk+autolinking.cmake +87 -0
- package/nitrogen/generated/android/PdfSdk+autolinking.gradle +27 -0
- package/nitrogen/generated/android/PdfSdkOnLoad.cpp +84 -0
- package/nitrogen/generated/android/PdfSdkOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfErrorEvent.hpp +80 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfLoadEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfPageEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfTapEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JFunc_void_PdfZoomEvent.hpp +77 -0
- package/nitrogen/generated/android/c++/JHybridNativePdfViewSpec.cpp +266 -0
- package/nitrogen/generated/android/c++/JHybridNativePdfViewSpec.hpp +92 -0
- package/nitrogen/generated/android/c++/JHybridPdfDocumentSpec.cpp +87 -0
- package/nitrogen/generated/android/c++/JHybridPdfDocumentSpec.hpp +65 -0
- package/nitrogen/generated/android/c++/JHybridPdfSdkSpec.cpp +58 -0
- package/nitrogen/generated/android/c++/JHybridPdfSdkSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/JPdfErrorCode.hpp +67 -0
- package/nitrogen/generated/android/c++/JPdfErrorEvent.hpp +63 -0
- package/nitrogen/generated/android/c++/JPdfFit.hpp +64 -0
- package/nitrogen/generated/android/c++/JPdfLoadEvent.hpp +65 -0
- package/nitrogen/generated/android/c++/JPdfPageEvent.hpp +61 -0
- package/nitrogen/generated/android/c++/JPdfPageSize.hpp +61 -0
- package/nitrogen/generated/android/c++/JPdfRenderRequest.hpp +86 -0
- package/nitrogen/generated/android/c++/JPdfTapEvent.hpp +65 -0
- package/nitrogen/generated/android/c++/JPdfViewSource.hpp +84 -0
- package/nitrogen/generated/android/c++/JPdfZoomEvent.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNativePdfViewStateUpdater.cpp +108 -0
- package/nitrogen/generated/android/c++/views/JHybridNativePdfViewStateUpdater.hpp +49 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfErrorEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfLoadEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfPageEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfTapEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/Func_void_PdfZoomEvent.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridNativePdfViewSpec.kt +185 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridPdfDocumentSpec.kt +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/HybridPdfSdkSpec.kt +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfErrorCode.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfErrorEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfFit.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfLoadEvent.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfPageEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfPageSize.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfRenderRequest.kt +86 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfSdkOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfTapEvent.kt +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfViewSource.kt +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/PdfZoomEvent.kt +56 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/views/HybridNativePdfViewManager.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/pdfsdk/views/HybridNativePdfViewStateUpdater.kt +23 -0
- package/nitrogen/generated/ios/PdfSdk+autolinking.rb +62 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Bridge.cpp +123 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Bridge.hpp +474 -0
- package/nitrogen/generated/ios/PdfSdk-Swift-Cxx-Umbrella.hpp +88 -0
- package/nitrogen/generated/ios/PdfSdkAutolinking.mm +41 -0
- package/nitrogen/generated/ios/PdfSdkAutolinking.swift +38 -0
- package/nitrogen/generated/ios/c++/HybridNativePdfViewSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNativePdfViewSpecSwift.hpp +209 -0
- package/nitrogen/generated/ios/c++/HybridPdfDocumentSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridPdfDocumentSpecSwift.hpp +105 -0
- package/nitrogen/generated/ios/c++/HybridPdfSdkSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridPdfSdkSpecSwift.hpp +86 -0
- package/nitrogen/generated/ios/c++/views/HybridNativePdfViewComponent.mm +187 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfErrorEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfLoadEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfPageEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfTapEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_PdfZoomEvent.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridNativePdfViewSpec.swift +70 -0
- package/nitrogen/generated/ios/swift/HybridNativePdfViewSpec_cxx.swift +438 -0
- package/nitrogen/generated/ios/swift/HybridPdfDocumentSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridPdfDocumentSpec_cxx.swift +173 -0
- package/nitrogen/generated/ios/swift/HybridPdfSdkSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridPdfSdkSpec_cxx.swift +148 -0
- package/nitrogen/generated/ios/swift/PdfErrorCode.swift +52 -0
- package/nitrogen/generated/ios/swift/PdfErrorEvent.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfFit.swift +48 -0
- package/nitrogen/generated/ios/swift/PdfLoadEvent.swift +39 -0
- package/nitrogen/generated/ios/swift/PdfPageEvent.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfPageSize.swift +34 -0
- package/nitrogen/generated/ios/swift/PdfRenderRequest.swift +129 -0
- package/nitrogen/generated/ios/swift/PdfTapEvent.swift +39 -0
- package/nitrogen/generated/ios/swift/PdfViewSource.swift +97 -0
- package/nitrogen/generated/ios/swift/PdfZoomEvent.swift +34 -0
- package/nitrogen/generated/shared/c++/HybridNativePdfViewSpec.cpp +51 -0
- package/nitrogen/generated/shared/c++/HybridNativePdfViewSpec.hpp +112 -0
- package/nitrogen/generated/shared/c++/HybridPdfDocumentSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridPdfDocumentSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridPdfSdkSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridPdfSdkSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/PdfErrorCode.hpp +88 -0
- package/nitrogen/generated/shared/c++/PdfErrorEvent.hpp +89 -0
- package/nitrogen/generated/shared/c++/PdfFit.hpp +84 -0
- package/nitrogen/generated/shared/c++/PdfLoadEvent.hpp +91 -0
- package/nitrogen/generated/shared/c++/PdfPageEvent.hpp +87 -0
- package/nitrogen/generated/shared/c++/PdfPageSize.hpp +87 -0
- package/nitrogen/generated/shared/c++/PdfRenderRequest.hpp +112 -0
- package/nitrogen/generated/shared/c++/PdfTapEvent.hpp +91 -0
- package/nitrogen/generated/shared/c++/PdfViewSource.hpp +97 -0
- package/nitrogen/generated/shared/c++/PdfZoomEvent.hpp +87 -0
- package/nitrogen/generated/shared/c++/views/HybridNativePdfViewComponent.cpp +226 -0
- package/nitrogen/generated/shared/c++/views/HybridNativePdfViewComponent.hpp +130 -0
- package/nitrogen/generated/shared/json/NativePdfViewConfig.json +23 -0
- package/package.json +80 -4
- package/react-native.config.js +10 -0
- package/src/PdfView.tsx +221 -0
- package/src/document.ts +34 -0
- package/src/index.ts +16 -0
- package/src/specs/PdfSdk.nitro.ts +60 -0
- package/src/specs/PdfView.nitro.ts +149 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import CoreGraphics
|
|
2
|
+
import Foundation
|
|
3
|
+
#if canImport(UIKit)
|
|
4
|
+
import UIKit
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Draws tiles away from the screen's thread and keeps the ones drawn.
|
|
9
|
+
*
|
|
10
|
+
* Several at a time, through a renderer each. `CGPDFDocument` keeps nothing
|
|
11
|
+
* between draws: a page's content is read again on every call, which on a
|
|
12
|
+
* structural drawing is a tenth of a second whatever size is asked for. Drawn
|
|
13
|
+
* one after another, a screenful of that arrives a tile at a time over several
|
|
14
|
+
* seconds, which is what it looked like. Drawn through several open copies of
|
|
15
|
+
* the same file, the same screenful arrives in a couple of passes. The copies
|
|
16
|
+
* cost almost nothing: the file is mapped once by the system, and what is not
|
|
17
|
+
* shared is the parsing, which is the part being done in parallel.
|
|
18
|
+
*
|
|
19
|
+
* Which tile comes next is the one nearest the middle of what the reader is
|
|
20
|
+
* looking at, so the middle fills in first. A tile that has left the view
|
|
21
|
+
* before its turn came is dropped rather than drawn.
|
|
22
|
+
*
|
|
23
|
+
* Thumbnails are the same work at a lower priority: a whole page drawn small,
|
|
24
|
+
* kept so that a page coming into view is never a blank rectangle, and so that
|
|
25
|
+
* there is something under a zoom that has outrun its tiles. They are drawn
|
|
26
|
+
* only once nothing sharper is waiting.
|
|
27
|
+
*
|
|
28
|
+
* The Kotlin twin is `PdfTileStore.kt`, which draws on one thread because there
|
|
29
|
+
* the platform keeps the open page parsed and a second thread would only wait
|
|
30
|
+
* for it. `NSCache` stands in for Android's `LruCache`, and lets the system
|
|
31
|
+
* take the images back under memory pressure, which costs nothing here: a tile
|
|
32
|
+
* that has gone is asked for again.
|
|
33
|
+
*/
|
|
34
|
+
final class PdfTileStore {
|
|
35
|
+
|
|
36
|
+
private struct Thumbnail {
|
|
37
|
+
let page: Int
|
|
38
|
+
let size: PageSize
|
|
39
|
+
let outWidth: Int
|
|
40
|
+
let outHeight: Int
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private struct Job {
|
|
44
|
+
let renderer: Int
|
|
45
|
+
let era: Int
|
|
46
|
+
let tile: TileSpec?
|
|
47
|
+
let thumbnail: Thumbnail?
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private let onTileReady: () -> Void
|
|
51
|
+
private let onError: (Error) -> Void
|
|
52
|
+
|
|
53
|
+
private let renderers: [PdfPageSource]
|
|
54
|
+
private let renderQueue = DispatchQueue(
|
|
55
|
+
label: "pdf-sdk-tiles",
|
|
56
|
+
qos: .userInitiated,
|
|
57
|
+
attributes: .concurrent
|
|
58
|
+
)
|
|
59
|
+
private let lock = NSLock()
|
|
60
|
+
|
|
61
|
+
private let tiles = PdfImageCache<String>(limit: PdfTileStore.cacheBytes())
|
|
62
|
+
/// A page's whole content, drawn small. Far cheaper to keep than a tile.
|
|
63
|
+
private let thumbnails = PdfImageCache<Int>(limit: PdfTileStore.thumbnailCache)
|
|
64
|
+
|
|
65
|
+
private var idle: [Int]
|
|
66
|
+
private var queue: [String: TileSpec] = [:]
|
|
67
|
+
private var thumbnailQueue: [Thumbnail] = []
|
|
68
|
+
private var focusX: CGFloat = 0
|
|
69
|
+
private var focusY: CGFloat = 0
|
|
70
|
+
private var era = 0
|
|
71
|
+
private var closed = false
|
|
72
|
+
|
|
73
|
+
init(
|
|
74
|
+
path: String,
|
|
75
|
+
password: String? = nil,
|
|
76
|
+
onTileReady: @escaping () -> Void,
|
|
77
|
+
onError: @escaping (Error) -> Void
|
|
78
|
+
) throws {
|
|
79
|
+
self.onTileReady = onTileReady
|
|
80
|
+
self.onError = onError
|
|
81
|
+
|
|
82
|
+
// The first one is the document itself: if the file is missing, malformed
|
|
83
|
+
// or locked, this is where that is found out.
|
|
84
|
+
var pool = [try PdfPageSource(path: path, password: password)]
|
|
85
|
+
while pool.count < PdfTileStore.renderers {
|
|
86
|
+
guard let extra = try? PdfPageSource(path: path, password: password) else { break }
|
|
87
|
+
pool.append(extra)
|
|
88
|
+
}
|
|
89
|
+
renderers = pool
|
|
90
|
+
idle = Array(pool.indices)
|
|
91
|
+
|
|
92
|
+
#if canImport(UIKit)
|
|
93
|
+
// What is held is worth holding, but not worth being killed for.
|
|
94
|
+
NotificationCenter.default.addObserver(
|
|
95
|
+
forName: UIApplication.didReceiveMemoryWarningNotification,
|
|
96
|
+
object: nil,
|
|
97
|
+
queue: .main
|
|
98
|
+
) { [weak self] _ in
|
|
99
|
+
self?.tiles.trim(toShare: 0.25)
|
|
100
|
+
self?.thumbnails.trim(toShare: 0.25)
|
|
101
|
+
}
|
|
102
|
+
#endif
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
deinit {
|
|
106
|
+
NotificationCenter.default.removeObserver(self)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
var pageCount: Int { renderers[0].pageCount }
|
|
110
|
+
|
|
111
|
+
/// Every page's size, read before the document is shown.
|
|
112
|
+
func pageSizes() throws -> [PageSize] {
|
|
113
|
+
try (0..<pageCount).map { try renderers[0].pageSize($0) }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
func image(_ key: String) -> CGImage? { tiles.image(key) }
|
|
117
|
+
|
|
118
|
+
func thumbnail(_ page: Int) -> CGImage? { thumbnails.image(page) }
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Takes the tiles the view wants. Anything asked for before and no longer
|
|
122
|
+
* wanted is forgotten, so a drag does not leave a trail of work behind it.
|
|
123
|
+
*/
|
|
124
|
+
func request(_ specs: [TileSpec], focusX: CGFloat, focusY: CGFloat) {
|
|
125
|
+
lock.lock()
|
|
126
|
+
if closed {
|
|
127
|
+
lock.unlock()
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
self.focusX = focusX
|
|
131
|
+
self.focusY = focusY
|
|
132
|
+
|
|
133
|
+
var wanted = Set<String>(minimumCapacity: specs.count)
|
|
134
|
+
for spec in specs {
|
|
135
|
+
wanted.insert(spec.key)
|
|
136
|
+
if tiles.image(spec.key) != nil { continue }
|
|
137
|
+
queue[spec.key] = spec
|
|
138
|
+
}
|
|
139
|
+
queue = queue.filter { wanted.contains($0.key) }
|
|
140
|
+
lock.unlock()
|
|
141
|
+
|
|
142
|
+
pump()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Asks for the small drawing of a page, if it is not already held.
|
|
147
|
+
*
|
|
148
|
+
* Small, but not as small as it sounds: the drawing costs what reading the
|
|
149
|
+
* page costs, and almost nothing on top of that for the pixels, so it is
|
|
150
|
+
* asked for at a size worth looking at while the tiles are on their way.
|
|
151
|
+
*/
|
|
152
|
+
func requestThumbnail(_ page: Int, size: PageSize) {
|
|
153
|
+
lock.lock()
|
|
154
|
+
let held = closed
|
|
155
|
+
|| thumbnails.image(page) != nil
|
|
156
|
+
|| thumbnailQueue.contains { $0.page == page }
|
|
157
|
+
if held {
|
|
158
|
+
lock.unlock()
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let aspect = size.height > 0 ? size.width / size.height : 1
|
|
163
|
+
var height = (PdfTileStore.thumbnailPixels / aspect).squareRoot()
|
|
164
|
+
var width = height * aspect
|
|
165
|
+
// A sheet twenty times wider than it is tall would ask for an image wider
|
|
166
|
+
// than the GPU will hold, so the longer side is capped and the other
|
|
167
|
+
// follows it.
|
|
168
|
+
if width > PdfTileStore.thumbnailSide {
|
|
169
|
+
width = PdfTileStore.thumbnailSide
|
|
170
|
+
height = width / aspect
|
|
171
|
+
}
|
|
172
|
+
if height > PdfTileStore.thumbnailSide {
|
|
173
|
+
height = PdfTileStore.thumbnailSide
|
|
174
|
+
width = height * aspect
|
|
175
|
+
}
|
|
176
|
+
thumbnailQueue.append(
|
|
177
|
+
Thumbnail(
|
|
178
|
+
page: page,
|
|
179
|
+
size: size,
|
|
180
|
+
outWidth: max(1, Int(width.rounded())),
|
|
181
|
+
outHeight: max(1, Int(height.rounded()))
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
// Scrolling quickly through a long document asks for a page's drawing and
|
|
185
|
+
// leaves it behind in the same breath. Only the pages asked for most
|
|
186
|
+
// recently are near enough to the view to be worth a renderer.
|
|
187
|
+
if thumbnailQueue.count > PdfTileStore.thumbnailsWaiting {
|
|
188
|
+
thumbnailQueue.removeFirst(thumbnailQueue.count - PdfTileStore.thumbnailsWaiting)
|
|
189
|
+
}
|
|
190
|
+
lock.unlock()
|
|
191
|
+
|
|
192
|
+
pump()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Everything drawn so far belongs to another document.
|
|
196
|
+
func clear() {
|
|
197
|
+
lock.lock()
|
|
198
|
+
era += 1
|
|
199
|
+
queue.removeAll()
|
|
200
|
+
thumbnailQueue.removeAll()
|
|
201
|
+
lock.unlock()
|
|
202
|
+
tiles.removeAll()
|
|
203
|
+
thumbnails.removeAll()
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
func close() {
|
|
207
|
+
lock.lock()
|
|
208
|
+
closed = true
|
|
209
|
+
queue.removeAll()
|
|
210
|
+
thumbnailQueue.removeAll()
|
|
211
|
+
lock.unlock()
|
|
212
|
+
tiles.removeAll()
|
|
213
|
+
thumbnails.removeAll()
|
|
214
|
+
renderers.forEach { $0.close() }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/// Hands out as much of what is waiting as there are renderers free for it.
|
|
218
|
+
private func pump() {
|
|
219
|
+
var started: [Job] = []
|
|
220
|
+
|
|
221
|
+
lock.lock()
|
|
222
|
+
while !closed, let renderer = idle.last {
|
|
223
|
+
var tile: TileSpec?
|
|
224
|
+
var thumbnail: Thumbnail?
|
|
225
|
+
|
|
226
|
+
if let next = nearest() {
|
|
227
|
+
queue.removeValue(forKey: next.key)
|
|
228
|
+
tile = next
|
|
229
|
+
} else if !thumbnailQueue.isEmpty {
|
|
230
|
+
// Nothing sharp is waiting, so a page's small drawing can have a turn.
|
|
231
|
+
thumbnail = thumbnailQueue.removeFirst()
|
|
232
|
+
} else {
|
|
233
|
+
break
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
idle.removeLast()
|
|
237
|
+
started.append(Job(renderer: renderer, era: era, tile: tile, thumbnail: thumbnail))
|
|
238
|
+
}
|
|
239
|
+
lock.unlock()
|
|
240
|
+
|
|
241
|
+
for job in started {
|
|
242
|
+
renderQueue.async { [weak self] in
|
|
243
|
+
guard let self else { return }
|
|
244
|
+
do {
|
|
245
|
+
if !self.isStale(job.era) {
|
|
246
|
+
if let tile = job.tile {
|
|
247
|
+
try self.drawTile(tile, through: job.renderer, job.era)
|
|
248
|
+
} else if let thumbnail = job.thumbnail {
|
|
249
|
+
try self.drawThumbnail(thumbnail, through: job.renderer, job.era)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
} catch {
|
|
253
|
+
self.onError(error)
|
|
254
|
+
}
|
|
255
|
+
self.lock.lock()
|
|
256
|
+
self.idle.append(job.renderer)
|
|
257
|
+
self.lock.unlock()
|
|
258
|
+
self.pump()
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private func drawTile(_ spec: TileSpec, through renderer: Int, _ era: Int) throws {
|
|
264
|
+
let image = try renderers[renderer].image(
|
|
265
|
+
page: spec.page,
|
|
266
|
+
rect: spec.source,
|
|
267
|
+
pixelWidth: spec.outWidth,
|
|
268
|
+
pixelHeight: spec.outHeight
|
|
269
|
+
)
|
|
270
|
+
guard !isStale(era) else { return }
|
|
271
|
+
tiles.put(image, for: spec.key)
|
|
272
|
+
ready()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private func drawThumbnail(_ thumbnail: Thumbnail, through renderer: Int, _ era: Int) throws {
|
|
276
|
+
let image = try renderers[renderer].image(
|
|
277
|
+
page: thumbnail.page,
|
|
278
|
+
rect: PageRect(x: 0, y: 0, width: thumbnail.size.width, height: thumbnail.size.height),
|
|
279
|
+
pixelWidth: thumbnail.outWidth,
|
|
280
|
+
pixelHeight: thumbnail.outHeight
|
|
281
|
+
)
|
|
282
|
+
guard !isStale(era) else { return }
|
|
283
|
+
thumbnails.put(image, for: thumbnail.page)
|
|
284
|
+
ready()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/// The view is drawn on the screen's thread, wherever the tile came from.
|
|
288
|
+
private func ready() {
|
|
289
|
+
DispatchQueue.main.async { [onTileReady] in onTileReady() }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private func isStale(_ era: Int) -> Bool {
|
|
293
|
+
lock.lock()
|
|
294
|
+
defer { lock.unlock() }
|
|
295
|
+
return closed || self.era != era
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/// The tile nearest what is being looked at. Called with the lock held.
|
|
299
|
+
private func nearest() -> TileSpec? {
|
|
300
|
+
var best: TileSpec?
|
|
301
|
+
var bestCost = CGFloat.greatestFiniteMagnitude
|
|
302
|
+
for spec in queue.values {
|
|
303
|
+
let dx = spec.rect.x + spec.rect.width / 2 - focusX
|
|
304
|
+
let dy = spec.rect.y + spec.rect.height / 2 - focusY
|
|
305
|
+
let cost = dx * dx + dy * dy
|
|
306
|
+
if cost < bestCost {
|
|
307
|
+
bestCost = cost
|
|
308
|
+
best = spec
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return best
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* How many copies of the document are drawn through at once.
|
|
316
|
+
*
|
|
317
|
+
* Two of a phone's cores are the fast ones, and a third renderer is there to
|
|
318
|
+
* keep them fed rather than to add a third core; beyond that the passes get
|
|
319
|
+
* longer without the screen filling any sooner, and each copy is another
|
|
320
|
+
* page being parsed into memory.
|
|
321
|
+
*/
|
|
322
|
+
private static var renderers: Int {
|
|
323
|
+
max(1, min(3, ProcessInfo.processInfo.activeProcessorCount - 1))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* A share of what the device has, and never more than this.
|
|
328
|
+
*
|
|
329
|
+
* A tile is six and a half megabytes and the worst screenful measured is
|
|
330
|
+
* fifteen of them, so this is about twice what is on the screen at once, plus
|
|
331
|
+
* the coarser levels drawn under it. Less than that and a tile being read
|
|
332
|
+
* every frame can be thrown out to make room for the tile next to it, which
|
|
333
|
+
* is the one thing a cache must never do.
|
|
334
|
+
*/
|
|
335
|
+
private static let cacheCeiling = 192 * 1024 * 1024
|
|
336
|
+
|
|
337
|
+
private static let thumbnailsWaiting = 4
|
|
338
|
+
private static let thumbnailCache = 32 * 1024 * 1024
|
|
339
|
+
private static let thumbnailPixels: CGFloat = 2_000_000
|
|
340
|
+
private static let thumbnailSide: CGFloat = 8192
|
|
341
|
+
|
|
342
|
+
private static func cacheBytes() -> Int {
|
|
343
|
+
let share = Int(ProcessInfo.processInfo.physicalMemory / 12)
|
|
344
|
+
return min(max(share, 48 * 1024 * 1024), cacheCeiling)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import CoreGraphics
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How a document is sized when it opens, which is also what a zoom of 1 means.
|
|
5
|
+
*
|
|
6
|
+
* The twin of the `PdfFit` union in the JavaScript API, kept apart from it so
|
|
7
|
+
* that everything deciding what the reader sees stays free of the bridge and
|
|
8
|
+
* can be tested on a Mac.
|
|
9
|
+
*/
|
|
10
|
+
enum PdfFitPolicy {
|
|
11
|
+
case auto
|
|
12
|
+
case width
|
|
13
|
+
case height
|
|
14
|
+
case page
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Where the document sits on the screen, and everything that follows from it.
|
|
19
|
+
*
|
|
20
|
+
* Deliberately free of UIKit: this is the arithmetic that decides what the
|
|
21
|
+
* reader sees, and it is worth being able to test on its own, on a Mac, without
|
|
22
|
+
* a simulator. `PdfSdkTests.swift` does exactly that.
|
|
23
|
+
*
|
|
24
|
+
* `scale` is screen points to the document point, and the offsets are where the
|
|
25
|
+
* document's top left corner sits in the view, so a point maps to
|
|
26
|
+
* `point * scale + offset`.
|
|
27
|
+
*
|
|
28
|
+
* The one difference from the Kotlin twin: there, a scale is in device pixels,
|
|
29
|
+
* because an Android view is measured in them. Here it is in screen points, the
|
|
30
|
+
* unit UIKit lays out in, so the constants below are the Kotlin ones divided by
|
|
31
|
+
* the three pixels a point of the screen has on the phones this runs on. The
|
|
32
|
+
* zoom a reader gets is the same on both.
|
|
33
|
+
*
|
|
34
|
+
* Unlike the Kotlin twin, this does not hold the document against its edges or
|
|
35
|
+
* move it: on iOS a `UIScrollView` owns the position, the momentum and the
|
|
36
|
+
* pinch, and hands them here through `place`.
|
|
37
|
+
*/
|
|
38
|
+
final class PdfViewport {
|
|
39
|
+
|
|
40
|
+
/// The document's bounding box: the widest page, and every page stacked up.
|
|
41
|
+
private(set) var contentWidth: CGFloat = 0
|
|
42
|
+
private(set) var contentHeight: CGFloat = 0
|
|
43
|
+
|
|
44
|
+
/// The first page, which is what decides how the document opens.
|
|
45
|
+
private(set) var firstPage = PageSize(width: 0, height: 0)
|
|
46
|
+
|
|
47
|
+
private(set) var viewWidth: CGFloat = 0
|
|
48
|
+
private(set) var viewHeight: CGFloat = 0
|
|
49
|
+
|
|
50
|
+
private(set) var scale: CGFloat = 1
|
|
51
|
+
private(set) var offsetX: CGFloat = 0
|
|
52
|
+
private(set) var offsetY: CGFloat = 0
|
|
53
|
+
|
|
54
|
+
/// What the document is fitted to when it opens.
|
|
55
|
+
var fit: PdfFitPolicy = .auto
|
|
56
|
+
|
|
57
|
+
/// How far it may be zoomed, in multiples of the zoom it opened at.
|
|
58
|
+
var minZoom: CGFloat?
|
|
59
|
+
var maxZoom: CGFloat?
|
|
60
|
+
var doubleTapZoom: CGFloat?
|
|
61
|
+
|
|
62
|
+
func setDocument(width: CGFloat, height: CGFloat, firstPage: PageSize) {
|
|
63
|
+
contentWidth = width
|
|
64
|
+
contentHeight = height
|
|
65
|
+
self.firstPage = firstPage
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func setDocument(_ layout: PdfDocumentLayout) {
|
|
69
|
+
setDocument(width: layout.width, height: layout.height, firstPage: layout.firstPage)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// A document of a single page, which is what most of the tests are about.
|
|
73
|
+
func setPage(width: CGFloat, height: CGFloat) {
|
|
74
|
+
setDocument(width: width, height: height, firstPage: PageSize(width: width, height: height))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
func setView(width: CGFloat, height: CGFloat) {
|
|
78
|
+
viewWidth = width
|
|
79
|
+
viewHeight = height
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var ready: Bool {
|
|
83
|
+
contentWidth > 0 && contentHeight > 0 && viewWidth > 0 && viewHeight > 0
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The zoom that fits the document's width.
|
|
88
|
+
*
|
|
89
|
+
* Width rather than the whole document: a document is read by scrolling, and
|
|
90
|
+
* fitting twelve pages on a phone would show twelve stamps.
|
|
91
|
+
*/
|
|
92
|
+
func widthScale() -> CGFloat {
|
|
93
|
+
guard ready else { return 1 }
|
|
94
|
+
return viewWidth / contentWidth
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// The zoom that fills the height with the first page.
|
|
98
|
+
func heightScale() -> CGFloat {
|
|
99
|
+
guard ready, firstPage.height > 0 else { return widthScale() }
|
|
100
|
+
return viewHeight / firstPage.height
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/// The zoom that shows the whole of the first page.
|
|
104
|
+
func pageScale() -> CGFloat {
|
|
105
|
+
min(widthScale(), heightScale())
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The zoom a document opens at.
|
|
110
|
+
*
|
|
111
|
+
* Left to itself it fits the width, unless the first page is a strip rather
|
|
112
|
+
* than a page: the formwork plans of a building arrive as one sheet thirteen
|
|
113
|
+
* metres wide, and fitted to the width that is a line across the middle of
|
|
114
|
+
* the screen. Those open filling the height instead, read by panning
|
|
115
|
+
* sideways.
|
|
116
|
+
*/
|
|
117
|
+
func restScale() -> CGFloat {
|
|
118
|
+
guard ready else { return 1 }
|
|
119
|
+
switch fit {
|
|
120
|
+
case .width:
|
|
121
|
+
return widthScale()
|
|
122
|
+
case .height:
|
|
123
|
+
return heightScale()
|
|
124
|
+
case .page:
|
|
125
|
+
return pageScale()
|
|
126
|
+
case .auto:
|
|
127
|
+
let width = widthScale()
|
|
128
|
+
guard firstPage.height > 0 else { return width }
|
|
129
|
+
return firstPage.height * width < viewHeight * PdfViewport.stripHeightShare
|
|
130
|
+
? heightScale()
|
|
131
|
+
: width
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The furthest a document can be zoomed in, in screen points to the point.
|
|
137
|
+
*
|
|
138
|
+
* A point is a 72nd of an inch, so this is about thirteen times the printed
|
|
139
|
+
* sheet. It sounds excessive, and on an ordinary page it is; on a formwork
|
|
140
|
+
* plan drawn at 1:50 it is where a dimension written along a beam becomes
|
|
141
|
+
* comfortable rather than merely possible, and nobody is forced to go there.
|
|
142
|
+
*/
|
|
143
|
+
func maxScale() -> CGFloat {
|
|
144
|
+
let rest = restScale()
|
|
145
|
+
if let maxZoom { return max(rest, rest * maxZoom) }
|
|
146
|
+
return max(rest, max(PdfViewport.targetPointSize, widthScale() * PdfViewport.minZoomRange))
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* As far out as it goes. Left to itself, far enough to see the widest page
|
|
151
|
+
* whole, whatever zoom the document opened at.
|
|
152
|
+
*/
|
|
153
|
+
func minScale() -> CGFloat {
|
|
154
|
+
let rest = restScale()
|
|
155
|
+
if let minZoom { return min(rest, rest * minZoom) }
|
|
156
|
+
return min(rest, widthScale())
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Where a double tap should take the reader.
|
|
161
|
+
*
|
|
162
|
+
* Zoomed in at all, it goes back out to where the document opened; otherwise
|
|
163
|
+
* it goes far enough in one tap to read what is written on the page, rather
|
|
164
|
+
* than a polite step that leaves the reader pinching afterwards.
|
|
165
|
+
*/
|
|
166
|
+
func doubleTapTarget() -> CGFloat {
|
|
167
|
+
let rest = restScale()
|
|
168
|
+
if scale > rest * 1.05 { return rest }
|
|
169
|
+
if let doubleTapZoom { return min(maxScale(), rest * doubleTapZoom) }
|
|
170
|
+
return min(maxScale(), max(rest * 3, PdfViewport.doubleTapScale))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Takes the position the scroll view has arrived at.
|
|
174
|
+
func place(scale: CGFloat, offsetX: CGFloat, offsetY: CGFloat) {
|
|
175
|
+
self.scale = scale
|
|
176
|
+
self.offsetX = offsetX
|
|
177
|
+
self.offsetY = offsetY
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
func documentToScreenX(_ point: CGFloat) -> CGFloat { point * scale + offsetX }
|
|
181
|
+
func documentToScreenY(_ point: CGFloat) -> CGFloat { point * scale + offsetY }
|
|
182
|
+
func screenToDocumentX(_ pixel: CGFloat) -> CGFloat { (pixel - offsetX) / scale }
|
|
183
|
+
func screenToDocumentY(_ pixel: CGFloat) -> CGFloat { (pixel - offsetY) / scale }
|
|
184
|
+
|
|
185
|
+
/// What is on screen, in document points, clipped to the document.
|
|
186
|
+
func visible() -> PageRect {
|
|
187
|
+
let left = max(0, screenToDocumentX(0))
|
|
188
|
+
let top = max(0, screenToDocumentY(0))
|
|
189
|
+
let right = min(contentWidth, screenToDocumentX(viewWidth))
|
|
190
|
+
let bottom = min(contentHeight, screenToDocumentY(viewHeight))
|
|
191
|
+
return PageRect(x: left, y: top, width: max(0, right - left), height: max(0, bottom - top))
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static let stripHeightShare: CGFloat = 0.3
|
|
195
|
+
static let targetPointSize: CGFloat = 13
|
|
196
|
+
static let minZoomRange: CGFloat = 8
|
|
197
|
+
|
|
198
|
+
/// Where one double tap lands when the document is at rest, at the least.
|
|
199
|
+
static let doubleTapScale: CGFloat = 4
|
|
200
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.PdfView = void 0;
|
|
7
|
+
exports.isPdfViewAvailable = isPdfViewAvailable;
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _reactNative = require("react-native");
|
|
10
|
+
var _reactNativeNitroModules = require("react-native-nitro-modules");
|
|
11
|
+
var _NativePdfViewConfig = _interopRequireDefault(require("../nitrogen/generated/shared/json/NativePdfViewConfig.json"));
|
|
12
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
/**
|
|
15
|
+
* What the view is handed when the caller says nothing.
|
|
16
|
+
*
|
|
17
|
+
* Every property the native view takes is required, and this is where the gaps
|
|
18
|
+
* are filled. The reason is not style: nitro reads a property as empty only
|
|
19
|
+
* when JavaScript passes `undefined`, while React hands native a `null`
|
|
20
|
+
* whenever a property is taken away, and the bridge throws on that. Required
|
|
21
|
+
* properties cannot be taken away, so `maxZoom={maybeUndefined}` is safe.
|
|
22
|
+
*/
|
|
23
|
+
const DECIDE = 0;
|
|
24
|
+
|
|
25
|
+
/** The gap between pages in PDF points, about a fifth of an inch. */
|
|
26
|
+
const DEFAULT_PAGE_GAP = 14;
|
|
27
|
+
const NativePdfView = (0, _reactNativeNitroModules.getHostComponent)('NativePdfView', () => _NativePdfViewConfig.default);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether this platform has the viewer, which both of the ones React Native
|
|
31
|
+
* ships for do: Android draws it with `PdfRenderer`, iOS with `CGPDFDocument`.
|
|
32
|
+
*
|
|
33
|
+
* Worth asking before reaching for it all the same, so that a platform without
|
|
34
|
+
* it falls back rather than showing an empty box.
|
|
35
|
+
*/
|
|
36
|
+
function isPdfViewAvailable() {
|
|
37
|
+
return _reactNative.Platform.OS === 'android' || _reactNative.Platform.OS === 'ios';
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A PDF, drawn by the platform, in tiles.
|
|
41
|
+
*
|
|
42
|
+
* Everything that happens while a finger is on the screen happens in the native
|
|
43
|
+
* view: the zoom, the movement, the tile grid and the drawing are read from the
|
|
44
|
+
* same numbers in the same pass. JavaScript says which document and which page,
|
|
45
|
+
* and hears back what was found in it.
|
|
46
|
+
*
|
|
47
|
+
* ```tsx
|
|
48
|
+
* <PdfView
|
|
49
|
+
* source={{ uri: 'https://example.com/plan.pdf' }}
|
|
50
|
+
* onPageChange={({ page, pageCount }) => setLabel(`${page + 1} / ${pageCount}`)}
|
|
51
|
+
* style={StyleSheet.absoluteFill}
|
|
52
|
+
* />
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const PdfView = exports.PdfView = /*#__PURE__*/(0, _react.forwardRef)(function PdfView({
|
|
56
|
+
source,
|
|
57
|
+
page = 0,
|
|
58
|
+
fit,
|
|
59
|
+
minZoom,
|
|
60
|
+
maxZoom,
|
|
61
|
+
doubleTapZoom,
|
|
62
|
+
pageGap,
|
|
63
|
+
backdropColor,
|
|
64
|
+
scrollIndicators,
|
|
65
|
+
style,
|
|
66
|
+
onLoad,
|
|
67
|
+
onPageChange,
|
|
68
|
+
onZoom,
|
|
69
|
+
onError,
|
|
70
|
+
onTap
|
|
71
|
+
}, ref) {
|
|
72
|
+
const native = (0, _react.useRef)(null);
|
|
73
|
+
|
|
74
|
+
/** Native hands the view over once; it is the same object from then on. */
|
|
75
|
+
const handleRef = (0, _react.useCallback)(view => {
|
|
76
|
+
native.current = view;
|
|
77
|
+
}, []);
|
|
78
|
+
const plain = typeof source === 'string';
|
|
79
|
+
const uri = plain ? source : source.uri;
|
|
80
|
+
const password = plain ? undefined : source.password;
|
|
81
|
+
const cache = plain ? undefined : source.cache;
|
|
82
|
+
const headers = plain ? undefined : source.headers;
|
|
83
|
+
const headerNames = JSON.stringify(headers ?? null);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Held steady across renders on purpose: the native view opens the document
|
|
87
|
+
* again whenever this changes, and an object literal in the caller's render
|
|
88
|
+
* body is a different object every time. The headers are compared by what
|
|
89
|
+
* is in them for the same reason.
|
|
90
|
+
*/
|
|
91
|
+
const resolved = (0, _react.useMemo)(() => ({
|
|
92
|
+
uri,
|
|
93
|
+
headers,
|
|
94
|
+
password,
|
|
95
|
+
cache
|
|
96
|
+
}),
|
|
97
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
98
|
+
[uri, password, cache, headerNames]);
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Transparent when nothing was asked for, which is what the view drew
|
|
102
|
+
* before this property existed.
|
|
103
|
+
*/
|
|
104
|
+
const backdrop = (0, _react.useMemo)(() => {
|
|
105
|
+
if (backdropColor == null) return 0;
|
|
106
|
+
const processed = (0, _reactNative.processColor)(backdropColor);
|
|
107
|
+
return typeof processed === 'number' ? processed : 0;
|
|
108
|
+
}, [backdropColor]);
|
|
109
|
+
(0, _react.useImperativeHandle)(ref, () => ({
|
|
110
|
+
goToPage: (to, animated = true) => native.current?.goToPage(to, animated),
|
|
111
|
+
setZoom: (zoom, animated = true) => native.current?.setZoom(zoom, animated),
|
|
112
|
+
coverage: () => native.current?.coverage() ?? 0
|
|
113
|
+
}), []);
|
|
114
|
+
const handleLoad = (0, _react.useCallback)(event => onLoad?.(event), [onLoad]);
|
|
115
|
+
const handlePageChange = (0, _react.useCallback)(event => onPageChange?.(event), [onPageChange]);
|
|
116
|
+
const handleZoom = (0, _react.useCallback)(event => onZoom?.(event), [onZoom]);
|
|
117
|
+
const handleError = (0, _react.useCallback)(event => onError?.(event), [onError]);
|
|
118
|
+
const handleTap = (0, _react.useCallback)(event => onTap?.(event), [onTap]);
|
|
119
|
+
const flattened = (0, _react.useMemo)(() => _reactNative.StyleSheet.flatten([styles.fill, style]), [style]);
|
|
120
|
+
if (!isPdfViewAvailable()) {
|
|
121
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
|
|
122
|
+
style: flattened
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(NativePdfView, {
|
|
126
|
+
hybridRef: (0, _reactNativeNitroModules.callback)(handleRef),
|
|
127
|
+
source: resolved,
|
|
128
|
+
page: page,
|
|
129
|
+
fit: fit ?? 'auto',
|
|
130
|
+
minZoom: minZoom ?? DECIDE,
|
|
131
|
+
maxZoom: maxZoom ?? DECIDE,
|
|
132
|
+
doubleTapZoom: doubleTapZoom ?? DECIDE,
|
|
133
|
+
pageGap: pageGap ?? DEFAULT_PAGE_GAP,
|
|
134
|
+
backdropColor: backdrop,
|
|
135
|
+
scrollIndicators: scrollIndicators ?? true,
|
|
136
|
+
onLoad: (0, _reactNativeNitroModules.callback)(handleLoad),
|
|
137
|
+
onPageChange: (0, _reactNativeNitroModules.callback)(handlePageChange),
|
|
138
|
+
onZoom: (0, _reactNativeNitroModules.callback)(handleZoom),
|
|
139
|
+
onError: (0, _reactNativeNitroModules.callback)(handleError),
|
|
140
|
+
onTap: (0, _reactNativeNitroModules.callback)(handleTap),
|
|
141
|
+
style: flattened
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
const styles = _reactNative.StyleSheet.create({
|
|
145
|
+
fill: {
|
|
146
|
+
flex: 1
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
//# sourceMappingURL=PdfView.js.map
|