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,148 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the reader sees: the pages, and the tiles drawn for them.
|
|
5
|
+
*
|
|
6
|
+
* One layer per page and one per tile, positioned from the viewport on every
|
|
7
|
+
* pass. Layers rather than a `draw(_:)` of its own because the images are
|
|
8
|
+
* already drawn by then: handing them to the compositor means a drag moves
|
|
9
|
+
* finished pictures on the GPU, where redrawing them into a context would
|
|
10
|
+
* refill the whole screen on the main thread at every frame.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here decides anything. Where each tile goes is `PdfTileGrid`'s
|
|
13
|
+
* answer, which is arithmetic and tested; this puts it on the screen.
|
|
14
|
+
*/
|
|
15
|
+
final class PdfTileCanvas: UIView {
|
|
16
|
+
|
|
17
|
+
private var pageLayers: [Int: CALayer] = [:]
|
|
18
|
+
private var tileLayers: [String: CALayer] = [:]
|
|
19
|
+
|
|
20
|
+
override init(frame: CGRect) {
|
|
21
|
+
super.init(frame: frame)
|
|
22
|
+
isUserInteractionEnabled = false
|
|
23
|
+
backgroundColor = .clear
|
|
24
|
+
layer.masksToBounds = true
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@available(*, unavailable)
|
|
28
|
+
required init?(coder: NSCoder) {
|
|
29
|
+
fatalError("PdfTileCanvas is not built from a storyboard")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func update(
|
|
33
|
+
viewport: PdfViewport,
|
|
34
|
+
layout: PdfDocumentLayout,
|
|
35
|
+
grid: PdfTileGrid,
|
|
36
|
+
store: PdfTileStore?
|
|
37
|
+
) {
|
|
38
|
+
guard let store, viewport.ready, viewport.scale > 0 else {
|
|
39
|
+
clear()
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
CATransaction.begin()
|
|
44
|
+
CATransaction.setDisableActions(true)
|
|
45
|
+
defer { CATransaction.commit() }
|
|
46
|
+
|
|
47
|
+
let visible = viewport.visible()
|
|
48
|
+
|
|
49
|
+
// The pages themselves: a white sheet each, with the page's own small
|
|
50
|
+
// drawing on it until the tiles for this zoom arrive. What falls between
|
|
51
|
+
// two pages is left to whatever is behind this view.
|
|
52
|
+
var livePages = Set<Int>()
|
|
53
|
+
for page in layout.pagesIn(top: visible.y, bottom: visible.bottom) {
|
|
54
|
+
let whole = CGRect(
|
|
55
|
+
x: viewport.documentToScreenX(page.x),
|
|
56
|
+
y: viewport.documentToScreenY(page.y),
|
|
57
|
+
width: page.width * viewport.scale,
|
|
58
|
+
height: page.height * viewport.scale
|
|
59
|
+
)
|
|
60
|
+
guard whole.width > 0, whole.height > 0 else { continue }
|
|
61
|
+
|
|
62
|
+
// Clipped to the view, so that a sheet metres wide is not handed to the
|
|
63
|
+
// compositor as a layer metres wide.
|
|
64
|
+
let shown = whole.intersection(bounds)
|
|
65
|
+
guard !shown.isNull, shown.width >= 0.5, shown.height >= 0.5 else { continue }
|
|
66
|
+
|
|
67
|
+
livePages.insert(page.index)
|
|
68
|
+
let layer = pageLayers[page.index] ?? makeLayer(white: true, key: page.index.description)
|
|
69
|
+
pageLayers[page.index] = layer
|
|
70
|
+
layer.frame = shown
|
|
71
|
+
layer.zPosition = 0
|
|
72
|
+
|
|
73
|
+
if let thumbnail = store.thumbnail(page.index) {
|
|
74
|
+
layer.contents = thumbnail
|
|
75
|
+
layer.contentsRect = share(of: shown, in: whole)
|
|
76
|
+
} else if layer.contents != nil {
|
|
77
|
+
layer.contents = nil
|
|
78
|
+
layer.contentsRect = CGRect(x: 0, y: 0, width: 1, height: 1)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// What was drawn for the zoom before this one keeps the screen covered
|
|
83
|
+
// until the tiles for this one have arrived, so both are drawn, oldest
|
|
84
|
+
// underneath.
|
|
85
|
+
var liveTiles = Set<String>()
|
|
86
|
+
let planned = grid.toDraw(viewport, layout) { store.image($0) != nil }
|
|
87
|
+
for (order, tile) in planned.enumerated() {
|
|
88
|
+
guard let image = store.image(tile.spec.key) else { continue }
|
|
89
|
+
let frame = tile.destination.rect
|
|
90
|
+
guard frame.width > 0, frame.height > 0, frame.intersects(bounds) else { continue }
|
|
91
|
+
|
|
92
|
+
liveTiles.insert(tile.spec.key)
|
|
93
|
+
let layer = tileLayers[tile.spec.key] ?? makeLayer(white: false, key: tile.spec.key)
|
|
94
|
+
tileLayers[tile.spec.key] = layer
|
|
95
|
+
layer.frame = frame
|
|
96
|
+
layer.contents = image
|
|
97
|
+
layer.zPosition = CGFloat(order + 1)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (page, layer) in pageLayers where !livePages.contains(page) {
|
|
101
|
+
layer.removeFromSuperlayer()
|
|
102
|
+
pageLayers.removeValue(forKey: page)
|
|
103
|
+
}
|
|
104
|
+
for (key, layer) in tileLayers where !liveTiles.contains(key) {
|
|
105
|
+
layer.removeFromSuperlayer()
|
|
106
|
+
tileLayers.removeValue(forKey: key)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
func clear() {
|
|
111
|
+
CATransaction.begin()
|
|
112
|
+
CATransaction.setDisableActions(true)
|
|
113
|
+
pageLayers.values.forEach { $0.removeFromSuperlayer() }
|
|
114
|
+
tileLayers.values.forEach { $0.removeFromSuperlayer() }
|
|
115
|
+
pageLayers.removeAll()
|
|
116
|
+
tileLayers.removeAll()
|
|
117
|
+
CATransaction.commit()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Which part of a page's picture is left after clipping it to the view.
|
|
121
|
+
private func share(of shown: CGRect, in whole: CGRect) -> CGRect {
|
|
122
|
+
CGRect(
|
|
123
|
+
x: max(0, (shown.minX - whole.minX) / whole.width),
|
|
124
|
+
y: max(0, (shown.minY - whole.minY) / whole.height),
|
|
125
|
+
width: min(1, shown.width / whole.width),
|
|
126
|
+
height: min(1, shown.height / whole.height)
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private func makeLayer(white: Bool, key: String) -> CALayer {
|
|
131
|
+
let layer = CALayer()
|
|
132
|
+
layer.name = key
|
|
133
|
+
layer.anchorPoint = .zero
|
|
134
|
+
layer.contentsGravity = .resize
|
|
135
|
+
layer.magnificationFilter = .linear
|
|
136
|
+
layer.minificationFilter = .linear
|
|
137
|
+
layer.isOpaque = true
|
|
138
|
+
// Two tiles meet on a fraction of a point. Left to antialias its edges,
|
|
139
|
+
// each one fades into the other and the join shows as a seam.
|
|
140
|
+
layer.allowsEdgeAntialiasing = false
|
|
141
|
+
layer.edgeAntialiasingMask = []
|
|
142
|
+
if white {
|
|
143
|
+
layer.backgroundColor = UIColor.white.cgColor
|
|
144
|
+
}
|
|
145
|
+
self.layer.addSublayer(layer)
|
|
146
|
+
return layer
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import CoreGraphics
|
|
2
|
+
|
|
3
|
+
/// A rectangle on the screen, in screen points.
|
|
4
|
+
struct ScreenRect: Equatable {
|
|
5
|
+
let left: CGFloat
|
|
6
|
+
let top: CGFloat
|
|
7
|
+
let right: CGFloat
|
|
8
|
+
let bottom: CGFloat
|
|
9
|
+
|
|
10
|
+
var width: CGFloat { right - left }
|
|
11
|
+
var height: CGFloat { bottom - top }
|
|
12
|
+
var rect: CGRect { CGRect(x: left, y: top, width: width, height: height) }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One tile: which page it belongs to, where it is, and how large it is drawn.
|
|
17
|
+
*
|
|
18
|
+
* `rect` is in document points, which is where it is put on screen; `source` is
|
|
19
|
+
* the same rectangle in the page's own points, which is what the renderer is
|
|
20
|
+
* asked for; `outWidth` and `outHeight` are in device pixels, which is what the
|
|
21
|
+
* image is made at.
|
|
22
|
+
*/
|
|
23
|
+
struct TileSpec {
|
|
24
|
+
let page: Int
|
|
25
|
+
let level: CGFloat
|
|
26
|
+
let column: Int
|
|
27
|
+
let row: Int
|
|
28
|
+
let rect: PageRect
|
|
29
|
+
let source: PageRect
|
|
30
|
+
let outWidth: Int
|
|
31
|
+
let outHeight: Int
|
|
32
|
+
|
|
33
|
+
var key: String { "\(level.bitPattern):\(page):\(column):\(row)" }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// A tile and where it goes on the screen.
|
|
37
|
+
struct PlannedTile {
|
|
38
|
+
let spec: TileSpec
|
|
39
|
+
let destination: ScreenRect
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Which tiles the view needs, which of them to draw, and which to ask for next.
|
|
44
|
+
*
|
|
45
|
+
* All of it is arithmetic over the viewport, the document's layout and a set of
|
|
46
|
+
* keys already drawn, so it can be exercised on its own: whether a drag leaves a
|
|
47
|
+
* gap, whether a change of zoom leaves the screen bare, and in what order the
|
|
48
|
+
* missing ones are asked for are questions that can be answered without a
|
|
49
|
+
* device, a renderer or a single pixel.
|
|
50
|
+
*
|
|
51
|
+
* Three things here are not in the Kotlin twin, and all three are answers to a
|
|
52
|
+
* tile costing a tenth of a second to draw on this platform however small it is:
|
|
53
|
+
*
|
|
54
|
+
* - Tiles are drawn at zooms from a fixed ladder, the opening zoom times 1.45
|
|
55
|
+
* to some power, rather than at whatever zoom the reader happened to stop at.
|
|
56
|
+
* A zoom that comes back to where it was finds the tiles it drew last time
|
|
57
|
+
* instead of asking for the same picture under a different name.
|
|
58
|
+
* - What is drawn is every level held, coarsest first, not just the last one.
|
|
59
|
+
* Coming out of a deep zoom and moving sideways used to leave the page's own
|
|
60
|
+
* small drawing showing, twenty times too coarse; the tiles from the zooms on
|
|
61
|
+
* the way in are still held, and one of them covers that ground.
|
|
62
|
+
* - A page small enough to be drawn in one go is drawn in one go. Most
|
|
63
|
+
* documents in this app are reports, and at the zoom they are read at a whole
|
|
64
|
+
* page fits in a tile, so a page costs one drawing rather than six.
|
|
65
|
+
*/
|
|
66
|
+
final class PdfTileGrid {
|
|
67
|
+
|
|
68
|
+
private let tilePixels: CGFloat
|
|
69
|
+
private let levelDrift: CGFloat
|
|
70
|
+
private let lookaheadSeconds: CGFloat
|
|
71
|
+
private let pixelRatio: CGFloat
|
|
72
|
+
private let tilePoints: CGFloat
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* `tilePixels` is three times the Kotlin twin's 512, and `lookaheadSeconds`
|
|
76
|
+
* longer, for the same reason.
|
|
77
|
+
*
|
|
78
|
+
* There, a tile costs what its pixels cost, because the platform keeps the
|
|
79
|
+
* page parsed between draws. Here every draw reads the page's content again,
|
|
80
|
+
* which on a structural drawing is a tenth of a second whether the tile is a
|
|
81
|
+
* postage stamp or a screenful: measured, a screenful is 15 draws at 512 and
|
|
82
|
+
* 6 at 1280, which is 1.6 seconds against a quarter of one. Larger still is
|
|
83
|
+
* quicker again, but the tiles are then most of a screen across, and the
|
|
84
|
+
* worst screenful measured goes from 79 MB at 1280 to 113 at 1536 and 189 at
|
|
85
|
+
* the margins this used to reach with.
|
|
86
|
+
*
|
|
87
|
+
* A tile also arrives a quarter of a second after it is asked for, so what is
|
|
88
|
+
* asked for has to be where the document will be by then rather than where it
|
|
89
|
+
* is now.
|
|
90
|
+
*/
|
|
91
|
+
init(
|
|
92
|
+
tilePixels: CGFloat = 1280,
|
|
93
|
+
levelDrift: CGFloat = 1.45,
|
|
94
|
+
lookaheadSeconds: CGFloat = 0.5,
|
|
95
|
+
pixelRatio: CGFloat = 1
|
|
96
|
+
) {
|
|
97
|
+
self.pixelRatio = max(1, pixelRatio)
|
|
98
|
+
self.tilePixels = tilePixels
|
|
99
|
+
self.tilePoints = tilePixels / self.pixelRatio
|
|
100
|
+
self.levelDrift = levelDrift
|
|
101
|
+
self.lookaheadSeconds = lookaheadSeconds
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// The zoom the document opens at, which is the first rung of the ladder.
|
|
105
|
+
private var ladder: CGFloat = 0
|
|
106
|
+
|
|
107
|
+
/// Which rung the tiles being asked for are drawn at, and which the last were.
|
|
108
|
+
private var rung = 0
|
|
109
|
+
private var lastRung = 0
|
|
110
|
+
private var hasLevel = false
|
|
111
|
+
|
|
112
|
+
/// The zoom the tiles being asked for are drawn at.
|
|
113
|
+
private(set) var levelScale: CGFloat = 0
|
|
114
|
+
|
|
115
|
+
/// The tiles wanted at the level being read at, with what is coming into view.
|
|
116
|
+
private(set) var current: [TileSpec] = []
|
|
117
|
+
|
|
118
|
+
/// Set while a zoom is on its way to a level that is already known.
|
|
119
|
+
private var aimed = false
|
|
120
|
+
|
|
121
|
+
/// Told what the document opens at, since every level is measured from it.
|
|
122
|
+
func setLadder(base: CGFloat) {
|
|
123
|
+
guard base > 0, base != ladder else { return }
|
|
124
|
+
ladder = base
|
|
125
|
+
clear()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// The rung of the ladder nearest a zoom.
|
|
129
|
+
func level(for scale: CGFloat) -> CGFloat {
|
|
130
|
+
guard ladder > 0, scale > 0 else { return scale }
|
|
131
|
+
return self.scale(atRung: rung(for: scale))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private func rung(for scale: CGFloat) -> Int {
|
|
135
|
+
Int((log(scale / ladder) / log(levelDrift)).rounded())
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* A rung's zoom, always worked out this way and from the same base, so that a
|
|
140
|
+
* level reached twice is the same number to the last bit and the tiles drawn
|
|
141
|
+
* for it the first time are found again rather than drawn again.
|
|
142
|
+
*/
|
|
143
|
+
private func scale(atRung rung: Int) -> CGFloat {
|
|
144
|
+
ladder * pow(levelDrift, CGFloat(rung))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Works out what the view needs now. `velocityX` and `velocityY` are how fast
|
|
149
|
+
* the document is moving in screen points a second, which is used to ask for
|
|
150
|
+
* tiles along the way rather than only where the view already is. `margin` is
|
|
151
|
+
* how far outside the view to reach, in screen points.
|
|
152
|
+
*
|
|
153
|
+
* Answers whether the level changed.
|
|
154
|
+
*/
|
|
155
|
+
@discardableResult
|
|
156
|
+
func update(
|
|
157
|
+
_ viewport: PdfViewport,
|
|
158
|
+
_ layout: PdfDocumentLayout,
|
|
159
|
+
velocityX: CGFloat = 0,
|
|
160
|
+
velocityY: CGFloat = 0,
|
|
161
|
+
margin: CGFloat = PdfTileGrid.tileMargin
|
|
162
|
+
) -> Bool {
|
|
163
|
+
guard viewport.ready, layout.pageCount > 0 else { return false }
|
|
164
|
+
if ladder <= 0 { ladder = viewport.scale }
|
|
165
|
+
|
|
166
|
+
var changed = false
|
|
167
|
+
if !aimed {
|
|
168
|
+
// The level holds until the zoom has moved far enough from it to show,
|
|
169
|
+
// and then it snaps to a rung rather than to wherever the fingers were.
|
|
170
|
+
let drift = hasLevel ? viewport.scale / levelScale : .greatestFiniteMagnitude
|
|
171
|
+
if drift > levelDrift || drift < 1 / levelDrift {
|
|
172
|
+
settle(on: rung(for: viewport.scale))
|
|
173
|
+
changed = true
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let ahead = lookaheadSeconds / viewport.scale
|
|
178
|
+
current = specs(
|
|
179
|
+
at: levelScale,
|
|
180
|
+
viewport: viewport,
|
|
181
|
+
layout: layout,
|
|
182
|
+
margin: margin,
|
|
183
|
+
aheadX: -velocityX * ahead,
|
|
184
|
+
aheadY: -velocityY * ahead
|
|
185
|
+
)
|
|
186
|
+
return changed
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Draws for a zoom the view has not reached yet.
|
|
191
|
+
*
|
|
192
|
+
* A double tap knows where it is going before it starts, and a tile takes
|
|
193
|
+
* about as long to arrive as the movement does. Asked for at the start, they
|
|
194
|
+
* are there as it lands, instead of the page sitting coarse for a quarter of
|
|
195
|
+
* a second after every tap. Until then they are drawn small, which costs
|
|
196
|
+
* nothing and is never worse than what they replace.
|
|
197
|
+
*/
|
|
198
|
+
func aim(at scale: CGFloat) {
|
|
199
|
+
guard ladder > 0, scale > 0 else { return }
|
|
200
|
+
let wanted = rung(for: scale)
|
|
201
|
+
if !hasLevel || wanted != rung {
|
|
202
|
+
settle(on: wanted)
|
|
203
|
+
current = []
|
|
204
|
+
}
|
|
205
|
+
aimed = true
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private func settle(on wanted: Int) {
|
|
209
|
+
lastRung = hasLevel ? rung : wanted
|
|
210
|
+
rung = wanted
|
|
211
|
+
levelScale = scale(atRung: wanted)
|
|
212
|
+
hasLevel = true
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Back to choosing the level from the zoom the reader is at.
|
|
216
|
+
func stopAiming() {
|
|
217
|
+
aimed = false
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Everything to draw, coarsest first, skipping what has not been drawn yet.
|
|
222
|
+
*
|
|
223
|
+
* The levels below the one being read at are what keeps the screen covered:
|
|
224
|
+
* they were drawn on the way into the zoom, they are still held, and one of
|
|
225
|
+
* them covers whatever the reader has just moved onto. A level above is drawn
|
|
226
|
+
* last, on top, because it is sharper than what was asked for.
|
|
227
|
+
*/
|
|
228
|
+
func toDraw(
|
|
229
|
+
_ viewport: PdfViewport,
|
|
230
|
+
_ layout: PdfDocumentLayout,
|
|
231
|
+
_ drawn: (String) -> Bool
|
|
232
|
+
) -> [PlannedTile] {
|
|
233
|
+
guard levelScale > 0, viewport.ready else { return [] }
|
|
234
|
+
|
|
235
|
+
let here = specs(at: levelScale, viewport: viewport, layout: layout, margin: 0)
|
|
236
|
+
var planned: [PlannedTile] = []
|
|
237
|
+
|
|
238
|
+
// With the level being read at complete, it covers the screen by itself,
|
|
239
|
+
// and the levels under it would be drawn only to be hidden. That is real
|
|
240
|
+
// work: eight screenfuls of pixels a frame, on a screen being dragged.
|
|
241
|
+
let complete = here.allSatisfy { drawn($0.key) }
|
|
242
|
+
|
|
243
|
+
if !complete {
|
|
244
|
+
for level in levels() where level < levelScale {
|
|
245
|
+
planned.append(contentsOf: held(level, viewport, layout, drawn))
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
for spec in here where drawn(spec.key) {
|
|
250
|
+
planned.append(place(viewport, spec))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// A level above the one being read at is sharper than what was asked for,
|
|
254
|
+
// so it goes over the top rather than under.
|
|
255
|
+
if !complete {
|
|
256
|
+
for level in levels() where level > levelScale {
|
|
257
|
+
planned.append(contentsOf: held(level, viewport, layout, drawn))
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return planned
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private func held(
|
|
264
|
+
_ level: CGFloat,
|
|
265
|
+
_ viewport: PdfViewport,
|
|
266
|
+
_ layout: PdfDocumentLayout,
|
|
267
|
+
_ drawn: (String) -> Bool
|
|
268
|
+
) -> [PlannedTile] {
|
|
269
|
+
specs(at: level, viewport: viewport, layout: layout, margin: 0)
|
|
270
|
+
.filter { drawn($0.key) }
|
|
271
|
+
.map { place(viewport, $0) }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Whether a page is drawn in one go at the level being read at.
|
|
276
|
+
*
|
|
277
|
+
* When it is, that drawing is the page's small drawing as well, and asking
|
|
278
|
+
* for both would be reading the page twice for the same picture.
|
|
279
|
+
*/
|
|
280
|
+
func drawsWholePage(_ page: PageBox) -> Bool {
|
|
281
|
+
guard levelScale > 0 else { return false }
|
|
282
|
+
let pixels = page.width * page.height * levelScale * levelScale * pixelRatio * pixelRatio
|
|
283
|
+
return pixels <= PdfTileGrid.wholePagePixels
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/// What is still to be drawn, nearest the middle of the view first.
|
|
287
|
+
func missing(_ viewport: PdfViewport, _ drawn: (String) -> Bool) -> [TileSpec] {
|
|
288
|
+
let visible = viewport.visible()
|
|
289
|
+
let centreX = visible.x + visible.width / 2
|
|
290
|
+
let centreY = visible.y + visible.height / 2
|
|
291
|
+
func distance(_ spec: TileSpec) -> CGFloat {
|
|
292
|
+
let dx = spec.rect.x + spec.rect.width / 2 - centreX
|
|
293
|
+
let dy = spec.rect.y + spec.rect.height / 2 - centreY
|
|
294
|
+
return dx * dx + dy * dy
|
|
295
|
+
}
|
|
296
|
+
return current
|
|
297
|
+
.filter { !drawn($0.key) }
|
|
298
|
+
.sorted { distance($0) < distance($1) }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* The share of the screen covered by tiles drawn for the level it is shown
|
|
303
|
+
* at, between zero and one. What is left is covered by something coarser.
|
|
304
|
+
* What falls between two pages counts as covered: there is nothing to draw
|
|
305
|
+
* there.
|
|
306
|
+
*/
|
|
307
|
+
func coverage(
|
|
308
|
+
_ viewport: PdfViewport,
|
|
309
|
+
_ layout: PdfDocumentLayout,
|
|
310
|
+
_ drawn: (String) -> Bool
|
|
311
|
+
) -> CGFloat {
|
|
312
|
+
coverageOf(viewport, layout, current.filter { drawn($0.key) })
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/// The same, counting every level held however coarse.
|
|
316
|
+
func coarseCoverage(
|
|
317
|
+
_ viewport: PdfViewport,
|
|
318
|
+
_ layout: PdfDocumentLayout,
|
|
319
|
+
_ drawn: (String) -> Bool
|
|
320
|
+
) -> CGFloat {
|
|
321
|
+
coverageOf(viewport, layout, toDraw(viewport, layout, drawn).map(\.spec))
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
func clear() {
|
|
325
|
+
levelScale = 0
|
|
326
|
+
rung = 0
|
|
327
|
+
lastRung = 0
|
|
328
|
+
hasLevel = false
|
|
329
|
+
current = []
|
|
330
|
+
aimed = false
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ─── Working out ────────────────────────────────────────────────────────────
|
|
334
|
+
|
|
335
|
+
/// The levels worth drawing, coarsest first.
|
|
336
|
+
private func levels() -> [CGFloat] {
|
|
337
|
+
guard hasLevel else { return [] }
|
|
338
|
+
var rungs = Set((1...PdfTileGrid.coarseLevels).map { rung - $0 })
|
|
339
|
+
rungs.insert(rung)
|
|
340
|
+
// Zoomed out of a level, what it drew is sharper than what is wanted now.
|
|
341
|
+
rungs.insert(lastRung)
|
|
342
|
+
return rungs.sorted().map { scale(atRung: $0) }
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/// The tiles of one level covering what is on screen, plus what is coming.
|
|
346
|
+
private func specs(
|
|
347
|
+
at level: CGFloat,
|
|
348
|
+
viewport: PdfViewport,
|
|
349
|
+
layout: PdfDocumentLayout,
|
|
350
|
+
margin: CGFloat,
|
|
351
|
+
aheadX: CGFloat = 0,
|
|
352
|
+
aheadY: CGFloat = 0
|
|
353
|
+
) -> [TileSpec] {
|
|
354
|
+
guard level > 0 else { return [] }
|
|
355
|
+
|
|
356
|
+
// In screen points rather than in tiles: a tile is most of a screen across,
|
|
357
|
+
// and a margin measured in tiles reaches so far beyond the view that a
|
|
358
|
+
// screenful becomes twenty tiles and two hundred megabytes.
|
|
359
|
+
let reach = margin / viewport.scale
|
|
360
|
+
let visible = viewport.visible()
|
|
361
|
+
let left = min(visible.x, visible.x + aheadX) - reach
|
|
362
|
+
let top = min(visible.y, visible.y + aheadY) - reach
|
|
363
|
+
let right = max(visible.right, visible.right + aheadX) + reach
|
|
364
|
+
let bottom = max(visible.bottom, visible.bottom + aheadY) + reach
|
|
365
|
+
|
|
366
|
+
var specs: [TileSpec] = []
|
|
367
|
+
for page in layout.pagesIn(top: top, bottom: bottom) {
|
|
368
|
+
specs.append(
|
|
369
|
+
contentsOf: tilesOf(
|
|
370
|
+
page,
|
|
371
|
+
level: level,
|
|
372
|
+
left: left,
|
|
373
|
+
top: top,
|
|
374
|
+
right: right,
|
|
375
|
+
bottom: bottom
|
|
376
|
+
)
|
|
377
|
+
)
|
|
378
|
+
}
|
|
379
|
+
return specs
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/// The tiles of one page that fall inside the band being drawn.
|
|
383
|
+
private func tilesOf(
|
|
384
|
+
_ page: PageBox,
|
|
385
|
+
level: CGFloat,
|
|
386
|
+
left: CGFloat,
|
|
387
|
+
top: CGFloat,
|
|
388
|
+
right: CGFloat,
|
|
389
|
+
bottom: CGFloat
|
|
390
|
+
) -> [TileSpec] {
|
|
391
|
+
let fromX = max(page.x, left)
|
|
392
|
+
let toX = min(page.right, right)
|
|
393
|
+
let fromY = max(page.y, top)
|
|
394
|
+
let toY = min(page.bottom, bottom)
|
|
395
|
+
guard toX > fromX, toY > fromY else { return [] }
|
|
396
|
+
|
|
397
|
+
// A page that fits in one drawing is drawn in one, since what a drawing
|
|
398
|
+
// costs is reading the page, not the pixels it puts out.
|
|
399
|
+
let whole = page.width * page.height * level * level * pixelRatio * pixelRatio
|
|
400
|
+
let tilePt = whole <= PdfTileGrid.wholePagePixels
|
|
401
|
+
? max(page.width, page.height)
|
|
402
|
+
: tilePoints / level
|
|
403
|
+
guard tilePt > 0 else { return [] }
|
|
404
|
+
|
|
405
|
+
let firstColumn = max(0, Int(((fromX - page.x) / tilePt).rounded(.down)))
|
|
406
|
+
let lastColumn = min(
|
|
407
|
+
Int((page.width / tilePt).rounded(.up)) - 1,
|
|
408
|
+
Int(((toX - page.x) / tilePt).rounded(.down))
|
|
409
|
+
)
|
|
410
|
+
let firstRow = max(0, Int(((fromY - page.y) / tilePt).rounded(.down)))
|
|
411
|
+
let lastRow = min(
|
|
412
|
+
Int((page.height / tilePt).rounded(.up)) - 1,
|
|
413
|
+
Int(((toY - page.y) / tilePt).rounded(.down))
|
|
414
|
+
)
|
|
415
|
+
guard lastColumn >= firstColumn, lastRow >= firstRow else { return [] }
|
|
416
|
+
|
|
417
|
+
var specs: [TileSpec] = []
|
|
418
|
+
for column in firstColumn...lastColumn {
|
|
419
|
+
for row in firstRow...lastRow {
|
|
420
|
+
let x = CGFloat(column) * tilePt
|
|
421
|
+
let y = CGFloat(row) * tilePt
|
|
422
|
+
let width = min(tilePt, page.width - x)
|
|
423
|
+
let height = min(tilePt, page.height - y)
|
|
424
|
+
if width <= 0 || height <= 0 { continue }
|
|
425
|
+
|
|
426
|
+
specs.append(
|
|
427
|
+
TileSpec(
|
|
428
|
+
page: page.index,
|
|
429
|
+
level: level,
|
|
430
|
+
column: column,
|
|
431
|
+
row: row,
|
|
432
|
+
rect: PageRect(x: page.x + x, y: page.y + y, width: width, height: height),
|
|
433
|
+
source: PageRect(x: x, y: y, width: width, height: height),
|
|
434
|
+
outWidth: max(1, Int((width * level * pixelRatio).rounded())),
|
|
435
|
+
outHeight: max(1, Int((height * level * pixelRatio).rounded()))
|
|
436
|
+
)
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return specs
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private func coverageOf(
|
|
444
|
+
_ viewport: PdfViewport,
|
|
445
|
+
_ layout: PdfDocumentLayout,
|
|
446
|
+
_ tiles: [TileSpec]
|
|
447
|
+
) -> CGFloat {
|
|
448
|
+
guard viewport.ready else { return 0 }
|
|
449
|
+
let visible = viewport.visible()
|
|
450
|
+
guard visible.width > 0, visible.height > 0 else { return 0 }
|
|
451
|
+
|
|
452
|
+
// Sampled rather than measured: the tiles of one level never overlap, but
|
|
453
|
+
// those of two levels do, and adding areas would count the overlap twice.
|
|
454
|
+
let steps = 40
|
|
455
|
+
var counted = 0
|
|
456
|
+
var covered = 0
|
|
457
|
+
for row in 0..<steps {
|
|
458
|
+
for column in 0..<steps {
|
|
459
|
+
let x = visible.x + visible.width * (CGFloat(column) + 0.5) / CGFloat(steps)
|
|
460
|
+
let y = visible.y + visible.height * (CGFloat(row) + 0.5) / CGFloat(steps)
|
|
461
|
+
|
|
462
|
+
let onPage = layout.pages.contains { x >= $0.x && x < $0.right && y >= $0.y && y < $0.bottom }
|
|
463
|
+
if !onPage { continue }
|
|
464
|
+
|
|
465
|
+
counted += 1
|
|
466
|
+
if tiles.contains(where: {
|
|
467
|
+
x >= $0.rect.x && x < $0.rect.right && y >= $0.rect.y && y < $0.rect.bottom
|
|
468
|
+
}) {
|
|
469
|
+
covered += 1
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return counted == 0 ? 1 : CGFloat(covered) / CGFloat(counted)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private func place(_ viewport: PdfViewport, _ spec: TileSpec) -> PlannedTile {
|
|
477
|
+
PlannedTile(
|
|
478
|
+
spec: spec,
|
|
479
|
+
destination: ScreenRect(
|
|
480
|
+
left: viewport.documentToScreenX(spec.rect.x),
|
|
481
|
+
top: viewport.documentToScreenY(spec.rect.y),
|
|
482
|
+
right: viewport.documentToScreenX(spec.rect.right),
|
|
483
|
+
bottom: viewport.documentToScreenY(spec.rect.bottom)
|
|
484
|
+
)
|
|
485
|
+
)
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/// Tiles are also drawn this far outside the view, in screen points.
|
|
489
|
+
static let tileMargin: CGFloat = 120
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* And this far once the document has come to rest, so that the next small
|
|
493
|
+
* movement is already drawn. It is a second of drawing that nobody is
|
|
494
|
+
* waiting on, which is the only kind worth spending.
|
|
495
|
+
*/
|
|
496
|
+
static let restMargin: CGFloat = 280
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* A page is drawn in one go while it is no larger than this. Worth more than
|
|
500
|
+
* a tile's worth of pixels, because it saves the four or five drawings the
|
|
501
|
+
* same page would otherwise be, and an ordinary page read at the zoom it
|
|
502
|
+
* opens at is just inside it.
|
|
503
|
+
*/
|
|
504
|
+
static let wholePagePixels: CGFloat = 2_600_000
|
|
505
|
+
|
|
506
|
+
/// How many levels below the one being read at are drawn underneath it.
|
|
507
|
+
static let coarseLevels = 6
|
|
508
|
+
|
|
509
|
+
/// Handy for tests and for callers that keep their own set of keys.
|
|
510
|
+
static func drawnFrom(_ keys: Set<String>) -> (String) -> Bool {
|
|
511
|
+
{ keys.contains($0) }
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
static func overlaps(_ a: ScreenRect, _ b: ScreenRect) -> Bool {
|
|
515
|
+
max(a.left, b.left) < min(a.right, b.right) - 0.01
|
|
516
|
+
&& max(a.top, b.top) < min(a.bottom, b.bottom) - 0.01
|
|
517
|
+
}
|
|
518
|
+
}
|