@tobyt/expo-pdf-markup 1.0.0 → 1.1.0
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 +21 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +2 -2
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +1 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +2 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +36 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +3 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +34 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +8 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +8 -0
- package/build/ExpoPdfMarkup.types.d.ts +27 -2
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +34 -5
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +14 -0
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +1 -0
- package/ios/AnnotationSerializer.swift +20 -0
- package/ios/ExpoPdfMarkupModule.swift +8 -0
- package/ios/ExpoPdfMarkupView+Ink.swift +183 -0
- package/ios/ExpoPdfMarkupView+Move.swift +266 -0
- package/ios/ExpoPdfMarkupView.swift +23 -401
- package/ios/StampPDFAnnotation.swift +100 -0
- package/ios/Tests/AnnotationSerializerTests.swift +69 -6
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +40 -0
- package/ios/Tests/StampPDFAnnotationTests.swift +128 -0
- package/package.json +3 -3
- package/src/ExpoPdfMarkup.types.ts +34 -1
- package/src/ExpoPdfMarkupView.web.tsx +40 -2
- package/src/web/annotationUtils.ts +13 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import PDFKit
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
extension ExpoPdfMarkupView {
|
|
5
|
+
// MARK: - Ink drawing
|
|
6
|
+
|
|
7
|
+
func setupInkMode() {
|
|
8
|
+
guard inkOverlayView == nil else { return }
|
|
9
|
+
|
|
10
|
+
pdfScrollView?.isScrollEnabled = false
|
|
11
|
+
|
|
12
|
+
// Disable PDFKit's built-in gesture recognizers (text selection, long-press, etc.)
|
|
13
|
+
// so they don't fire during the touch before a pan gesture would have recognised.
|
|
14
|
+
disabledPdfGestures = pdfView.gestureRecognizers?.filter(\.isEnabled) ?? []
|
|
15
|
+
disabledPdfGestures.forEach { $0.isEnabled = false }
|
|
16
|
+
|
|
17
|
+
// Shape layer for live stroke feedback (added to self so it's above pdfView)
|
|
18
|
+
let shape = CAShapeLayer()
|
|
19
|
+
shape.fillColor = nil
|
|
20
|
+
shape.lineCap = .round
|
|
21
|
+
shape.lineJoin = .round
|
|
22
|
+
shape.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
23
|
+
shape.lineWidth = annotationLineWidth
|
|
24
|
+
shape.frame = bounds
|
|
25
|
+
layer.addSublayer(shape)
|
|
26
|
+
inkShapeLayer = shape
|
|
27
|
+
|
|
28
|
+
// Overlay view that captures touches via touchesBegan/Moved/Ended — unlike
|
|
29
|
+
// UIPanGestureRecognizer there is no minimum-distance threshold, so the very
|
|
30
|
+
// first contact point is always recorded.
|
|
31
|
+
let overlay = InkOverlayView(frame: bounds)
|
|
32
|
+
overlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
33
|
+
overlay.onBegan = { [weak self] point in
|
|
34
|
+
guard let self else { return }
|
|
35
|
+
inkPoints = [point]
|
|
36
|
+
updateInkShapeLayer()
|
|
37
|
+
}
|
|
38
|
+
overlay.onMoved = { [weak self] point in
|
|
39
|
+
guard let self else { return }
|
|
40
|
+
inkPoints.append(point)
|
|
41
|
+
updateInkShapeLayer()
|
|
42
|
+
}
|
|
43
|
+
overlay.onEnded = { [weak self] in
|
|
44
|
+
guard let self else { return }
|
|
45
|
+
finishInkStroke()
|
|
46
|
+
}
|
|
47
|
+
overlay.onCancelled = { [weak self] in
|
|
48
|
+
self?.clearInkStroke()
|
|
49
|
+
}
|
|
50
|
+
addSubview(overlay)
|
|
51
|
+
inkOverlayView = overlay
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
func removeInkMode() {
|
|
55
|
+
inkOverlayView?.removeFromSuperview()
|
|
56
|
+
inkOverlayView = nil
|
|
57
|
+
inkShapeLayer?.removeFromSuperlayer()
|
|
58
|
+
inkShapeLayer = nil
|
|
59
|
+
inkPoints = []
|
|
60
|
+
pdfScrollView?.isScrollEnabled = true
|
|
61
|
+
disabledPdfGestures.forEach { $0.isEnabled = true }
|
|
62
|
+
disabledPdfGestures = []
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private func updateInkShapeLayer() {
|
|
66
|
+
let path = UIBezierPath()
|
|
67
|
+
for (index, point) in inkPoints.enumerated() {
|
|
68
|
+
if index == 0 {
|
|
69
|
+
path.move(to: point)
|
|
70
|
+
} else {
|
|
71
|
+
path.addLine(to: point)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
inkShapeLayer?.path = path.cgPath
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private func finishInkStroke() {
|
|
78
|
+
defer { clearInkStroke() }
|
|
79
|
+
guard inkPoints.count >= 2 else { return }
|
|
80
|
+
|
|
81
|
+
// Determine which page the stroke is on using the midpoint
|
|
82
|
+
let midPoint = inkPoints[inkPoints.count / 2]
|
|
83
|
+
// Convert display point (in self's coordinate space) to pdfView coordinate space
|
|
84
|
+
let pdfViewMidPoint = pdfView.convert(midPoint, from: self)
|
|
85
|
+
guard let page = pdfView.page(for: pdfViewMidPoint, nearest: true) else { return }
|
|
86
|
+
|
|
87
|
+
// Convert all display points to PDF page coordinates
|
|
88
|
+
let pdfPoints = inkPoints.map { displayPoint -> CGPoint in
|
|
89
|
+
let pdfViewPoint = pdfView.convert(displayPoint, from: self)
|
|
90
|
+
return pdfView.convert(pdfViewPoint, to: page)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Use the full page bounds — PDFKit clips ink rendering to annotation bounds,
|
|
94
|
+
// and tight bounds calculations are fragile with coordinate transforms.
|
|
95
|
+
let annotationBounds = page.bounds(for: pdfView.displayBox)
|
|
96
|
+
|
|
97
|
+
let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
|
|
98
|
+
annotation.color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
99
|
+
|
|
100
|
+
let border = PDFBorder()
|
|
101
|
+
border.lineWidth = annotationLineWidth
|
|
102
|
+
annotation.border = border
|
|
103
|
+
|
|
104
|
+
let bezierPath = UIBezierPath()
|
|
105
|
+
for (index, point) in pdfPoints.enumerated() {
|
|
106
|
+
if index == 0 {
|
|
107
|
+
bezierPath.move(to: point)
|
|
108
|
+
} else {
|
|
109
|
+
bezierPath.addLine(to: point)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
annotation.add(bezierPath)
|
|
113
|
+
|
|
114
|
+
let id = UUID().uuidString
|
|
115
|
+
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
116
|
+
page.addAnnotation(annotation)
|
|
117
|
+
|
|
118
|
+
emitAnnotationsChanged()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private func clearInkStroke() {
|
|
122
|
+
inkPoints = []
|
|
123
|
+
inkShapeLayer?.path = nil
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// Transparent overlay view used in ink mode. Using touchesBegan/Moved/Ended directly
|
|
128
|
+
/// avoids the minimum-distance recognition threshold of UIPanGestureRecognizer, ensuring
|
|
129
|
+
/// the very first contact point is always captured. coalescedTouches provides all
|
|
130
|
+
/// sub-frame samples on high-refresh-rate devices for a more accurate stroke path.
|
|
131
|
+
class InkOverlayView: UIView {
|
|
132
|
+
var onBegan: ((CGPoint) -> Void)?
|
|
133
|
+
var onMoved: ((CGPoint) -> Void)?
|
|
134
|
+
var onEnded: (() -> Void)?
|
|
135
|
+
var onCancelled: (() -> Void)?
|
|
136
|
+
|
|
137
|
+
private var activeTouch: UITouch?
|
|
138
|
+
|
|
139
|
+
override init(frame: CGRect) {
|
|
140
|
+
super.init(frame: frame)
|
|
141
|
+
isMultipleTouchEnabled = true
|
|
142
|
+
isOpaque = false
|
|
143
|
+
backgroundColor = .clear
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
@available(*, unavailable)
|
|
147
|
+
required init?(coder _: NSCoder) {
|
|
148
|
+
fatalError("init(coder:) has not been implemented")
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
override func touchesBegan(_ touches: Set<UITouch>, with _: UIEvent?) {
|
|
152
|
+
guard activeTouch == nil else { return }
|
|
153
|
+
// Prefer a pencil touch over a finger so a resting palm doesn't steal the stroke.
|
|
154
|
+
let touch = touches.first(where: { $0.type == .pencil }) ?? touches.first
|
|
155
|
+
guard let touch else { return }
|
|
156
|
+
activeTouch = touch
|
|
157
|
+
onBegan?(touch.location(in: self))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
|
161
|
+
guard let active = activeTouch, touches.contains(active) else { return }
|
|
162
|
+
for t in event?.coalescedTouches(for: active) ?? [active] {
|
|
163
|
+
onMoved?(t.location(in: self))
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
|
168
|
+
guard let active = activeTouch, touches.contains(active) else { return }
|
|
169
|
+
// coalescedTouches includes the original touch as its last element, so all
|
|
170
|
+
// final sub-frame positions (including the lift-off point) are forwarded here.
|
|
171
|
+
for t in event?.coalescedTouches(for: active) ?? [active] {
|
|
172
|
+
onMoved?(t.location(in: self))
|
|
173
|
+
}
|
|
174
|
+
onEnded?()
|
|
175
|
+
activeTouch = nil
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
override func touchesCancelled(_ touches: Set<UITouch>, with _: UIEvent?) {
|
|
179
|
+
guard let active = activeTouch, touches.contains(active) else { return }
|
|
180
|
+
onCancelled?()
|
|
181
|
+
activeTouch = nil
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import PDFKit
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
extension ExpoPdfMarkupView {
|
|
5
|
+
// MARK: - Move
|
|
6
|
+
|
|
7
|
+
func setupMoveMode() {
|
|
8
|
+
guard movePanGesture == nil else { return }
|
|
9
|
+
pdfScrollView?.isScrollEnabled = false
|
|
10
|
+
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleMovePan(_:)))
|
|
11
|
+
pan.maximumNumberOfTouches = 1
|
|
12
|
+
pan.delegate = self
|
|
13
|
+
pdfView.addGestureRecognizer(pan)
|
|
14
|
+
movePanGesture = pan
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
func removeMoveMode() {
|
|
18
|
+
if let gesture = movePanGesture {
|
|
19
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
20
|
+
movePanGesture = nil
|
|
21
|
+
}
|
|
22
|
+
finishMove(commit: false)
|
|
23
|
+
pdfScrollView?.isScrollEnabled = true
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@objc func handleMovePan(_ gesture: UIPanGestureRecognizer) {
|
|
27
|
+
switch gesture.state {
|
|
28
|
+
case .began:
|
|
29
|
+
beginMove(at: gesture.location(in: pdfView))
|
|
30
|
+
case .changed:
|
|
31
|
+
updateMove(at: gesture.location(in: pdfView))
|
|
32
|
+
case .ended:
|
|
33
|
+
finishMove(commit: true)
|
|
34
|
+
case .cancelled, .failed:
|
|
35
|
+
finishMove(commit: false)
|
|
36
|
+
default:
|
|
37
|
+
break
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private func beginMove(at location: CGPoint) {
|
|
42
|
+
guard let document = pdfView.document,
|
|
43
|
+
let page = pdfView.page(for: location, nearest: true) else { return }
|
|
44
|
+
|
|
45
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
46
|
+
guard let annotation = hitTestAnnotation(at: pdfPoint, on: page) else { return }
|
|
47
|
+
|
|
48
|
+
let pageIndex = document.index(for: page)
|
|
49
|
+
guard let model = AnnotationSerializer.toModel(from: annotation, page: pageIndex),
|
|
50
|
+
let preview = annotationForModel(model, on: page) else { return }
|
|
51
|
+
|
|
52
|
+
movingPage = page
|
|
53
|
+
movingOriginalModel = model
|
|
54
|
+
movingPreviewModel = model
|
|
55
|
+
moveStartPoint = pdfPoint
|
|
56
|
+
|
|
57
|
+
page.removeAnnotation(annotation)
|
|
58
|
+
page.addAnnotation(preview)
|
|
59
|
+
movingPreviewAnnotation = preview
|
|
60
|
+
updateEditingOverlay()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private func updateMove(at location: CGPoint) {
|
|
64
|
+
guard let page = movingPage,
|
|
65
|
+
let originalModel = movingOriginalModel,
|
|
66
|
+
let startPoint = moveStartPoint else { return }
|
|
67
|
+
|
|
68
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
69
|
+
let rawDeltaX = pdfPoint.x - startPoint.x
|
|
70
|
+
let rawDeltaY = pdfPoint.y - startPoint.y
|
|
71
|
+
let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
|
|
72
|
+
let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
|
|
73
|
+
guard translatedModel != movingPreviewModel,
|
|
74
|
+
let replacement = annotationForModel(translatedModel, on: page) else { return }
|
|
75
|
+
|
|
76
|
+
if let currentPreview = movingPreviewAnnotation {
|
|
77
|
+
page.removeAnnotation(currentPreview)
|
|
78
|
+
}
|
|
79
|
+
page.addAnnotation(replacement)
|
|
80
|
+
movingPreviewAnnotation = replacement
|
|
81
|
+
movingPreviewModel = translatedModel
|
|
82
|
+
updateEditingOverlay()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private func finishMove(commit: Bool) {
|
|
86
|
+
guard let page = movingPage,
|
|
87
|
+
let originalModel = movingOriginalModel else {
|
|
88
|
+
clearMoveState()
|
|
89
|
+
updateEditingOverlay()
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if let preview = movingPreviewAnnotation {
|
|
94
|
+
page.removeAnnotation(preview)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let finalModel = commit ? (movingPreviewModel ?? originalModel) : originalModel
|
|
98
|
+
if let finalAnnotation = annotationForModel(finalModel, on: page) {
|
|
99
|
+
page.addAnnotation(finalAnnotation)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let didChange = commit && finalModel != originalModel
|
|
103
|
+
clearMoveState()
|
|
104
|
+
updateEditingOverlay()
|
|
105
|
+
|
|
106
|
+
if didChange {
|
|
107
|
+
emitAnnotationsChanged()
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private func clearMoveState() {
|
|
112
|
+
movingPage = nil
|
|
113
|
+
movingOriginalModel = nil
|
|
114
|
+
movingPreviewModel = nil
|
|
115
|
+
movingPreviewAnnotation = nil
|
|
116
|
+
moveStartPoint = nil
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// MARK: - Editing overlay
|
|
120
|
+
|
|
121
|
+
func updateEditingOverlay() {
|
|
122
|
+
guard currentMode == "move" || currentMode == "eraser" else {
|
|
123
|
+
editingOverlayLayer?.removeFromSuperlayer()
|
|
124
|
+
editingOverlayLayer = nil
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
guard let document = pdfView.document,
|
|
129
|
+
let documentView = pdfView.documentView else { return }
|
|
130
|
+
|
|
131
|
+
let overlay = editingOverlayLayer ?? {
|
|
132
|
+
let shape = CAShapeLayer()
|
|
133
|
+
shape.fillColor = UIColor.clear.cgColor
|
|
134
|
+
shape.strokeColor = UIColor.systemBlue.cgColor
|
|
135
|
+
shape.lineWidth = 1.5
|
|
136
|
+
shape.lineDashPattern = [6, 4]
|
|
137
|
+
documentView.layer.addSublayer(shape)
|
|
138
|
+
editingOverlayLayer = shape
|
|
139
|
+
return shape
|
|
140
|
+
}()
|
|
141
|
+
|
|
142
|
+
if overlay.superlayer !== documentView.layer {
|
|
143
|
+
overlay.removeFromSuperlayer()
|
|
144
|
+
documentView.layer.addSublayer(overlay)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
overlay.frame = documentView.bounds
|
|
148
|
+
let path = UIBezierPath()
|
|
149
|
+
|
|
150
|
+
for pageIndex in 0 ..< document.pageCount {
|
|
151
|
+
guard let page = document.page(at: pageIndex) else { continue }
|
|
152
|
+
for annotation in page.annotations where AnnotationSerializer.isModuleManaged(annotation) {
|
|
153
|
+
guard let outlineRect = annotationOutlineRect(for: annotation, on: page, in: documentView) else {
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
path.append(UIBezierPath(rect: outlineRect))
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
overlay.path = path.cgPath
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private func annotationOutlineRect(
|
|
164
|
+
for annotation: PDFAnnotation,
|
|
165
|
+
on page: PDFPage,
|
|
166
|
+
in documentView: UIView
|
|
167
|
+
) -> CGRect? {
|
|
168
|
+
let pdfRect: CGRect
|
|
169
|
+
if let paths = annotation.paths, !paths.isEmpty {
|
|
170
|
+
var combinedBounds = CGRect.null
|
|
171
|
+
for path in paths {
|
|
172
|
+
combinedBounds = combinedBounds.union(path.cgPath.boundingBoxOfPath)
|
|
173
|
+
}
|
|
174
|
+
guard !combinedBounds.isNull else { return nil }
|
|
175
|
+
let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
176
|
+
pdfRect = combinedBounds.insetBy(dx: -padding, dy: -padding)
|
|
177
|
+
} else {
|
|
178
|
+
pdfRect = annotation.bounds
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
182
|
+
return documentView.convert(pdfViewRect, from: pdfView)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
186
|
+
AnnotationSerializer.toPDFAnnotation(model, pageBounds: page.bounds(for: pdfView.displayBox))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
|
|
190
|
+
guard dx != 0 || dy != 0 else { return model }
|
|
191
|
+
|
|
192
|
+
var updated = model
|
|
193
|
+
switch model.type {
|
|
194
|
+
case "ink":
|
|
195
|
+
let translatedPaths = model.decodedPaths.map { stroke in
|
|
196
|
+
stroke.map { point in
|
|
197
|
+
AnnotationPoint(x: point.x + dx, y: point.y + dy)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
updated.paths = updated.encodedPaths(from: translatedPaths)
|
|
201
|
+
case "highlight", "underline", "text", "freeText", "stamp":
|
|
202
|
+
if let bounds = model.bounds {
|
|
203
|
+
updated.bounds = AnnotationBounds(
|
|
204
|
+
x: bounds.x + dx,
|
|
205
|
+
y: bounds.y + dy,
|
|
206
|
+
width: bounds.width,
|
|
207
|
+
height: bounds.height
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
default:
|
|
211
|
+
break
|
|
212
|
+
}
|
|
213
|
+
return updated
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
private func clampedTranslation(
|
|
217
|
+
for model: AnnotationModel,
|
|
218
|
+
dx: CGFloat,
|
|
219
|
+
dy: CGFloat,
|
|
220
|
+
on page: PDFPage
|
|
221
|
+
) -> CGPoint {
|
|
222
|
+
guard let outlineBounds = annotationOutlineBounds(for: model) else {
|
|
223
|
+
return CGPoint(x: dx, y: dy)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
227
|
+
let minDx = pageBounds.minX - outlineBounds.x
|
|
228
|
+
let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
|
|
229
|
+
let minDy = pageBounds.minY - outlineBounds.y
|
|
230
|
+
let maxDy = pageBounds.maxY - (outlineBounds.y + outlineBounds.height)
|
|
231
|
+
|
|
232
|
+
return CGPoint(
|
|
233
|
+
x: min(max(dx, minDx), maxDx),
|
|
234
|
+
y: min(max(dy, minDy), maxDy)
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private func annotationOutlineBounds(for model: AnnotationModel) -> AnnotationBounds? {
|
|
239
|
+
guard model.type == "ink" else { return model.bounds }
|
|
240
|
+
let paths = model.decodedPaths
|
|
241
|
+
var minX = CGFloat.greatestFiniteMagnitude
|
|
242
|
+
var minY = CGFloat.greatestFiniteMagnitude
|
|
243
|
+
var maxX = -CGFloat.greatestFiniteMagnitude
|
|
244
|
+
var maxY = -CGFloat.greatestFiniteMagnitude
|
|
245
|
+
|
|
246
|
+
for stroke in paths {
|
|
247
|
+
for point in stroke {
|
|
248
|
+
minX = min(minX, point.x)
|
|
249
|
+
minY = min(minY, point.y)
|
|
250
|
+
maxX = max(maxX, point.x)
|
|
251
|
+
maxY = max(maxY, point.y)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// If no points were visited the sentinels are still in place (minX > maxX).
|
|
256
|
+
guard minX <= maxX else { return nil }
|
|
257
|
+
|
|
258
|
+
let padding = max(model.lineWidth ?? 2.0, 10.0)
|
|
259
|
+
return AnnotationBounds(
|
|
260
|
+
x: minX - padding,
|
|
261
|
+
y: minY - padding,
|
|
262
|
+
width: maxX - minX + padding * 2,
|
|
263
|
+
height: maxY - minY + padding * 2
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
}
|