@tobyt/expo-pdf-markup 1.0.0 → 1.0.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 +10 -0
- package/ios/ExpoPdfMarkupView+Ink.swift +183 -0
- package/ios/ExpoPdfMarkupView+Move.swift +266 -0
- package/ios/ExpoPdfMarkupView.swift +17 -401
- package/ios/Tests/AnnotationSerializerTests.swift +6 -6
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +40 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @tobyt/expo-pdf-markup
|
|
2
2
|
|
|
3
|
+
## 1.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 16e0e87: Fix iOS ink strokes missing their first contact point
|
|
8
|
+
|
|
9
|
+
Replaces `UIPanGestureRecognizer` with direct `touchesBegan/Moved/Ended` handling for ink drawing. The pan gesture had a minimum-distance threshold that silently dropped the start of every stroke — most noticeable with third-party styluses (e.g. Metapen) where initial contact is slow and deliberate.
|
|
10
|
+
|
|
11
|
+
Also disables PDFKit's built-in gesture recognizers while in ink mode, preventing text selection from firing during what was previously the gesture recognition dead zone.
|
|
12
|
+
|
|
3
13
|
## 1.0.0
|
|
4
14
|
|
|
5
15
|
### Major Changes
|
|
@@ -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":
|
|
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
|
+
}
|
|
@@ -11,7 +11,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
11
11
|
|
|
12
12
|
private var currentSource: String?
|
|
13
13
|
private var currentAnnotationsJSON: String?
|
|
14
|
-
private var currentMode: String = "none"
|
|
14
|
+
private(set) var currentMode: String = "none"
|
|
15
15
|
var annotationColor: String = "#FF0000" {
|
|
16
16
|
didSet {
|
|
17
17
|
inkShapeLayer?.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
@@ -22,26 +22,31 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
22
22
|
var annotationFontFamily: String?
|
|
23
23
|
var useJsTextDialog: Bool = false
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
var inkOverlayView: InkOverlayView?
|
|
26
|
+
var disabledPdfGestures: [UIGestureRecognizer] = []
|
|
27
|
+
var inkShapeLayer: CAShapeLayer?
|
|
28
|
+
var inkPoints: [CGPoint] = []
|
|
28
29
|
private var eraserTapGesture: UITapGestureRecognizer?
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
var movePanGesture: UIPanGestureRecognizer?
|
|
31
|
+
var editingOverlayLayer: CAShapeLayer?
|
|
31
32
|
var textTapGesture: UITapGestureRecognizer?
|
|
32
33
|
private var selectionObserver: NSObjectProtocol?
|
|
33
34
|
private var selectionDebounceTimer: Timer?
|
|
34
35
|
private var currentMarkupType: String?
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
var movingPage: PDFPage?
|
|
37
|
+
var movingOriginalModel: AnnotationModel?
|
|
38
|
+
var movingPreviewModel: AnnotationModel?
|
|
39
|
+
var movingPreviewAnnotation: PDFAnnotation?
|
|
40
|
+
var moveStartPoint: CGPoint?
|
|
40
41
|
|
|
41
42
|
var pendingTextPage: PDFPage?
|
|
42
43
|
var pendingTextPoint: CGPoint?
|
|
43
44
|
var pendingTextAnnotation: PDFAnnotation?
|
|
44
45
|
|
|
46
|
+
var pdfScrollView: UIScrollView? {
|
|
47
|
+
pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
|
|
48
|
+
}
|
|
49
|
+
|
|
45
50
|
required init(appContext: AppContext? = nil) {
|
|
46
51
|
super.init(appContext: appContext)
|
|
47
52
|
clipsToBounds = true
|
|
@@ -225,130 +230,6 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
225
230
|
updateEditingOverlay()
|
|
226
231
|
}
|
|
227
232
|
|
|
228
|
-
// MARK: - Ink drawing
|
|
229
|
-
|
|
230
|
-
private var pdfScrollView: UIScrollView? {
|
|
231
|
-
pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private func setupInkMode() {
|
|
235
|
-
guard inkPanGesture == nil else { return }
|
|
236
|
-
|
|
237
|
-
// Disable scrolling so pan gestures go to our recognizer, not the scroll view
|
|
238
|
-
pdfScrollView?.isScrollEnabled = false
|
|
239
|
-
|
|
240
|
-
// Shape layer for live stroke feedback (added to self so it's above pdfView)
|
|
241
|
-
let shape = CAShapeLayer()
|
|
242
|
-
shape.fillColor = nil
|
|
243
|
-
shape.lineCap = .round
|
|
244
|
-
shape.lineJoin = .round
|
|
245
|
-
shape.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
246
|
-
shape.lineWidth = annotationLineWidth
|
|
247
|
-
shape.frame = bounds
|
|
248
|
-
layer.addSublayer(shape)
|
|
249
|
-
inkShapeLayer = shape
|
|
250
|
-
|
|
251
|
-
// Pan gesture on pdfView — points come in pdfView's coordinate space
|
|
252
|
-
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleInkPan(_:)))
|
|
253
|
-
pan.maximumNumberOfTouches = 1
|
|
254
|
-
pdfView.addGestureRecognizer(pan)
|
|
255
|
-
inkPanGesture = pan
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private func removeInkMode() {
|
|
259
|
-
if let gesture = inkPanGesture {
|
|
260
|
-
pdfView.removeGestureRecognizer(gesture)
|
|
261
|
-
inkPanGesture = nil
|
|
262
|
-
}
|
|
263
|
-
inkShapeLayer?.removeFromSuperlayer()
|
|
264
|
-
inkShapeLayer = nil
|
|
265
|
-
inkPoints = []
|
|
266
|
-
pdfScrollView?.isScrollEnabled = true
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
@objc private func handleInkPan(_ gesture: UIPanGestureRecognizer) {
|
|
270
|
-
// Get point in self's coordinate space for visual feedback
|
|
271
|
-
let displayPoint = gesture.location(in: self)
|
|
272
|
-
|
|
273
|
-
switch gesture.state {
|
|
274
|
-
case .began:
|
|
275
|
-
inkPoints = [displayPoint]
|
|
276
|
-
updateInkShapeLayer()
|
|
277
|
-
case .changed:
|
|
278
|
-
inkPoints.append(displayPoint)
|
|
279
|
-
updateInkShapeLayer()
|
|
280
|
-
case .ended:
|
|
281
|
-
inkPoints.append(displayPoint)
|
|
282
|
-
updateInkShapeLayer()
|
|
283
|
-
finishInkStroke()
|
|
284
|
-
case .cancelled, .failed:
|
|
285
|
-
clearInkStroke()
|
|
286
|
-
default:
|
|
287
|
-
break
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
private func updateInkShapeLayer() {
|
|
292
|
-
let path = UIBezierPath()
|
|
293
|
-
for (index, point) in inkPoints.enumerated() {
|
|
294
|
-
if index == 0 {
|
|
295
|
-
path.move(to: point)
|
|
296
|
-
} else {
|
|
297
|
-
path.addLine(to: point)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
inkShapeLayer?.path = path.cgPath
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private func finishInkStroke() {
|
|
304
|
-
defer { clearInkStroke() }
|
|
305
|
-
guard inkPoints.count >= 2 else { return }
|
|
306
|
-
|
|
307
|
-
// Determine which page the stroke is on using the midpoint
|
|
308
|
-
let midPoint = inkPoints[inkPoints.count / 2]
|
|
309
|
-
// Convert display point (in self's coordinate space) to pdfView coordinate space
|
|
310
|
-
let pdfViewMidPoint = pdfView.convert(midPoint, from: self)
|
|
311
|
-
guard let page = pdfView.page(for: pdfViewMidPoint, nearest: true) else { return }
|
|
312
|
-
|
|
313
|
-
// Convert all display points to PDF page coordinates
|
|
314
|
-
let pdfPoints = inkPoints.map { displayPoint -> CGPoint in
|
|
315
|
-
let pdfViewPoint = pdfView.convert(displayPoint, from: self)
|
|
316
|
-
return pdfView.convert(pdfViewPoint, to: page)
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// Use the full page bounds — PDFKit clips ink rendering to annotation bounds,
|
|
320
|
-
// and tight bounds calculations are fragile with coordinate transforms.
|
|
321
|
-
let annotationBounds = page.bounds(for: pdfView.displayBox)
|
|
322
|
-
|
|
323
|
-
let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
|
|
324
|
-
annotation.color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
325
|
-
|
|
326
|
-
let border = PDFBorder()
|
|
327
|
-
border.lineWidth = annotationLineWidth
|
|
328
|
-
annotation.border = border
|
|
329
|
-
|
|
330
|
-
let bezierPath = UIBezierPath()
|
|
331
|
-
for (index, point) in pdfPoints.enumerated() {
|
|
332
|
-
if index == 0 {
|
|
333
|
-
bezierPath.move(to: point)
|
|
334
|
-
} else {
|
|
335
|
-
bezierPath.addLine(to: point)
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
annotation.add(bezierPath)
|
|
339
|
-
|
|
340
|
-
let id = UUID().uuidString
|
|
341
|
-
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
342
|
-
page.addAnnotation(annotation)
|
|
343
|
-
|
|
344
|
-
emitAnnotationsChanged()
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
private func clearInkStroke() {
|
|
348
|
-
inkPoints = []
|
|
349
|
-
inkShapeLayer?.path = nil
|
|
350
|
-
}
|
|
351
|
-
|
|
352
233
|
// MARK: - Eraser
|
|
353
234
|
|
|
354
235
|
private func setupEraserGesture() {
|
|
@@ -381,11 +262,10 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
381
262
|
}
|
|
382
263
|
}
|
|
383
264
|
|
|
384
|
-
|
|
265
|
+
func hitTestAnnotation(at point: CGPoint, on page: PDFPage) -> PDFAnnotation? {
|
|
385
266
|
for annotation in page.annotations.reversed() {
|
|
386
267
|
guard AnnotationSerializer.isModuleManaged(annotation) else { continue }
|
|
387
268
|
|
|
388
|
-
// For ink annotations, test against the actual paths
|
|
389
269
|
if let paths = annotation.paths {
|
|
390
270
|
let tolerance = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
391
271
|
for path in paths {
|
|
@@ -400,7 +280,6 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
400
280
|
}
|
|
401
281
|
}
|
|
402
282
|
} else if annotation.bounds.contains(point) {
|
|
403
|
-
// For non-ink annotations (highlight, underline, text), bounds check is fine
|
|
404
283
|
return annotation
|
|
405
284
|
}
|
|
406
285
|
}
|
|
@@ -500,266 +379,3 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
500
379
|
updateEditingOverlay()
|
|
501
380
|
}
|
|
502
381
|
}
|
|
503
|
-
|
|
504
|
-
extension ExpoPdfMarkupView {
|
|
505
|
-
// MARK: - Move
|
|
506
|
-
|
|
507
|
-
func setupMoveMode() {
|
|
508
|
-
guard movePanGesture == nil else { return }
|
|
509
|
-
pdfScrollView?.isScrollEnabled = false
|
|
510
|
-
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleMovePan(_:)))
|
|
511
|
-
pan.maximumNumberOfTouches = 1
|
|
512
|
-
pan.delegate = self
|
|
513
|
-
pdfView.addGestureRecognizer(pan)
|
|
514
|
-
movePanGesture = pan
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
func removeMoveMode() {
|
|
518
|
-
if let gesture = movePanGesture {
|
|
519
|
-
pdfView.removeGestureRecognizer(gesture)
|
|
520
|
-
movePanGesture = nil
|
|
521
|
-
}
|
|
522
|
-
finishMove(commit: false)
|
|
523
|
-
pdfScrollView?.isScrollEnabled = true
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
@objc func handleMovePan(_ gesture: UIPanGestureRecognizer) {
|
|
527
|
-
switch gesture.state {
|
|
528
|
-
case .began:
|
|
529
|
-
beginMove(at: gesture.location(in: pdfView))
|
|
530
|
-
case .changed:
|
|
531
|
-
updateMove(at: gesture.location(in: pdfView))
|
|
532
|
-
case .ended:
|
|
533
|
-
finishMove(commit: true)
|
|
534
|
-
case .cancelled, .failed:
|
|
535
|
-
finishMove(commit: false)
|
|
536
|
-
default:
|
|
537
|
-
break
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
private func beginMove(at location: CGPoint) {
|
|
542
|
-
guard let document = pdfView.document,
|
|
543
|
-
let page = pdfView.page(for: location, nearest: true) else { return }
|
|
544
|
-
|
|
545
|
-
let pdfPoint = pdfView.convert(location, to: page)
|
|
546
|
-
guard let annotation = hitTestAnnotation(at: pdfPoint, on: page) else { return }
|
|
547
|
-
|
|
548
|
-
let pageIndex = document.index(for: page)
|
|
549
|
-
guard let model = AnnotationSerializer.toModel(from: annotation, page: pageIndex),
|
|
550
|
-
let preview = annotationForModel(model, on: page) else { return }
|
|
551
|
-
|
|
552
|
-
movingPage = page
|
|
553
|
-
movingOriginalModel = model
|
|
554
|
-
movingPreviewModel = model
|
|
555
|
-
moveStartPoint = pdfPoint
|
|
556
|
-
|
|
557
|
-
page.removeAnnotation(annotation)
|
|
558
|
-
page.addAnnotation(preview)
|
|
559
|
-
movingPreviewAnnotation = preview
|
|
560
|
-
updateEditingOverlay()
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
private func updateMove(at location: CGPoint) {
|
|
564
|
-
guard let page = movingPage,
|
|
565
|
-
let originalModel = movingOriginalModel,
|
|
566
|
-
let startPoint = moveStartPoint else { return }
|
|
567
|
-
|
|
568
|
-
let pdfPoint = pdfView.convert(location, to: page)
|
|
569
|
-
let rawDeltaX = pdfPoint.x - startPoint.x
|
|
570
|
-
let rawDeltaY = pdfPoint.y - startPoint.y
|
|
571
|
-
let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
|
|
572
|
-
let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
|
|
573
|
-
guard translatedModel != movingPreviewModel,
|
|
574
|
-
let replacement = annotationForModel(translatedModel, on: page) else { return }
|
|
575
|
-
|
|
576
|
-
if let currentPreview = movingPreviewAnnotation {
|
|
577
|
-
page.removeAnnotation(currentPreview)
|
|
578
|
-
}
|
|
579
|
-
page.addAnnotation(replacement)
|
|
580
|
-
movingPreviewAnnotation = replacement
|
|
581
|
-
movingPreviewModel = translatedModel
|
|
582
|
-
updateEditingOverlay()
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
private func finishMove(commit: Bool) {
|
|
586
|
-
guard let page = movingPage,
|
|
587
|
-
let originalModel = movingOriginalModel else {
|
|
588
|
-
clearMoveState()
|
|
589
|
-
updateEditingOverlay()
|
|
590
|
-
return
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if let preview = movingPreviewAnnotation {
|
|
594
|
-
page.removeAnnotation(preview)
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
let finalModel = commit ? (movingPreviewModel ?? originalModel) : originalModel
|
|
598
|
-
if let finalAnnotation = annotationForModel(finalModel, on: page) {
|
|
599
|
-
page.addAnnotation(finalAnnotation)
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
let didChange = commit && finalModel != originalModel
|
|
603
|
-
clearMoveState()
|
|
604
|
-
updateEditingOverlay()
|
|
605
|
-
|
|
606
|
-
if didChange {
|
|
607
|
-
emitAnnotationsChanged()
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
private func clearMoveState() {
|
|
612
|
-
movingPage = nil
|
|
613
|
-
movingOriginalModel = nil
|
|
614
|
-
movingPreviewModel = nil
|
|
615
|
-
movingPreviewAnnotation = nil
|
|
616
|
-
moveStartPoint = nil
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
// MARK: - Editing overlay
|
|
620
|
-
|
|
621
|
-
func updateEditingOverlay() {
|
|
622
|
-
guard currentMode == "move" || currentMode == "eraser" else {
|
|
623
|
-
editingOverlayLayer?.removeFromSuperlayer()
|
|
624
|
-
editingOverlayLayer = nil
|
|
625
|
-
return
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
guard let document = pdfView.document,
|
|
629
|
-
let documentView = pdfView.documentView else { return }
|
|
630
|
-
|
|
631
|
-
let overlay = editingOverlayLayer ?? {
|
|
632
|
-
let shape = CAShapeLayer()
|
|
633
|
-
shape.fillColor = UIColor.clear.cgColor
|
|
634
|
-
shape.strokeColor = UIColor.systemBlue.cgColor
|
|
635
|
-
shape.lineWidth = 1.5
|
|
636
|
-
shape.lineDashPattern = [6, 4]
|
|
637
|
-
documentView.layer.addSublayer(shape)
|
|
638
|
-
editingOverlayLayer = shape
|
|
639
|
-
return shape
|
|
640
|
-
}()
|
|
641
|
-
|
|
642
|
-
if overlay.superlayer !== documentView.layer {
|
|
643
|
-
overlay.removeFromSuperlayer()
|
|
644
|
-
documentView.layer.addSublayer(overlay)
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
overlay.frame = documentView.bounds
|
|
648
|
-
let path = UIBezierPath()
|
|
649
|
-
|
|
650
|
-
for pageIndex in 0 ..< document.pageCount {
|
|
651
|
-
guard let page = document.page(at: pageIndex) else { continue }
|
|
652
|
-
for annotation in page.annotations where AnnotationSerializer.isModuleManaged(annotation) {
|
|
653
|
-
guard let outlineRect = annotationOutlineRect(for: annotation, on: page, in: documentView) else {
|
|
654
|
-
continue
|
|
655
|
-
}
|
|
656
|
-
path.append(UIBezierPath(rect: outlineRect))
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
overlay.path = path.cgPath
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
private func annotationOutlineRect(
|
|
664
|
-
for annotation: PDFAnnotation,
|
|
665
|
-
on page: PDFPage,
|
|
666
|
-
in documentView: UIView
|
|
667
|
-
) -> CGRect? {
|
|
668
|
-
let pdfRect: CGRect
|
|
669
|
-
if let paths = annotation.paths, !paths.isEmpty {
|
|
670
|
-
var combinedBounds = CGRect.null
|
|
671
|
-
for path in paths {
|
|
672
|
-
combinedBounds = combinedBounds.union(path.cgPath.boundingBoxOfPath)
|
|
673
|
-
}
|
|
674
|
-
guard !combinedBounds.isNull else { return nil }
|
|
675
|
-
let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
676
|
-
pdfRect = combinedBounds.insetBy(dx: -padding, dy: -padding)
|
|
677
|
-
} else {
|
|
678
|
-
pdfRect = annotation.bounds
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
682
|
-
return documentView.convert(pdfViewRect, from: pdfView)
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
686
|
-
AnnotationSerializer.toPDFAnnotation(model, pageBounds: page.bounds(for: pdfView.displayBox))
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
|
|
690
|
-
guard dx != 0 || dy != 0 else { return model }
|
|
691
|
-
|
|
692
|
-
var updated = model
|
|
693
|
-
switch model.type {
|
|
694
|
-
case "ink":
|
|
695
|
-
let translatedPaths = model.decodedPaths.map { stroke in
|
|
696
|
-
stroke.map { point in
|
|
697
|
-
AnnotationPoint(x: point.x + dx, y: point.y + dy)
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
updated.paths = updated.encodedPaths(from: translatedPaths)
|
|
701
|
-
case "highlight", "underline", "text", "freeText":
|
|
702
|
-
if let bounds = model.bounds {
|
|
703
|
-
updated.bounds = AnnotationBounds(
|
|
704
|
-
x: bounds.x + dx,
|
|
705
|
-
y: bounds.y + dy,
|
|
706
|
-
width: bounds.width,
|
|
707
|
-
height: bounds.height
|
|
708
|
-
)
|
|
709
|
-
}
|
|
710
|
-
default:
|
|
711
|
-
break
|
|
712
|
-
}
|
|
713
|
-
return updated
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
private func clampedTranslation(
|
|
717
|
-
for model: AnnotationModel,
|
|
718
|
-
dx: CGFloat,
|
|
719
|
-
dy: CGFloat,
|
|
720
|
-
on page: PDFPage
|
|
721
|
-
) -> CGPoint {
|
|
722
|
-
guard let outlineBounds = annotationOutlineBounds(for: model) else {
|
|
723
|
-
return CGPoint(x: dx, y: dy)
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
727
|
-
let minDx = pageBounds.minX - outlineBounds.x
|
|
728
|
-
let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
|
|
729
|
-
let minDy = pageBounds.minY - outlineBounds.y
|
|
730
|
-
let maxDy = pageBounds.maxY - (outlineBounds.y + outlineBounds.height)
|
|
731
|
-
|
|
732
|
-
return CGPoint(
|
|
733
|
-
x: min(max(dx, minDx), maxDx),
|
|
734
|
-
y: min(max(dy, minDy), maxDy)
|
|
735
|
-
)
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
private func annotationOutlineBounds(for model: AnnotationModel) -> AnnotationBounds? {
|
|
739
|
-
guard model.type == "ink" else { return model.bounds }
|
|
740
|
-
let paths = model.decodedPaths
|
|
741
|
-
var minX = CGFloat.greatestFiniteMagnitude
|
|
742
|
-
var minY = CGFloat.greatestFiniteMagnitude
|
|
743
|
-
var maxX = -CGFloat.greatestFiniteMagnitude
|
|
744
|
-
var maxY = -CGFloat.greatestFiniteMagnitude
|
|
745
|
-
|
|
746
|
-
for stroke in paths {
|
|
747
|
-
for point in stroke {
|
|
748
|
-
minX = min(minX, point.x)
|
|
749
|
-
minY = min(minY, point.y)
|
|
750
|
-
maxX = max(maxX, point.x)
|
|
751
|
-
maxY = max(maxY, point.y)
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
guard minX.isFinite, minY.isFinite, maxX.isFinite, maxY.isFinite else { return nil }
|
|
756
|
-
|
|
757
|
-
let padding = max(model.lineWidth ?? 2.0, 10.0)
|
|
758
|
-
return AnnotationBounds(
|
|
759
|
-
x: minX - padding,
|
|
760
|
-
y: minY - padding,
|
|
761
|
-
width: maxX - minX + padding * 2,
|
|
762
|
-
height: maxY - minY + padding * 2
|
|
763
|
-
)
|
|
764
|
-
}
|
|
765
|
-
}
|
|
@@ -73,7 +73,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
73
73
|
XCTAssertEqual(annotation?.color, .clear)
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
func testCreateFreeTextAnnotationExpandsNarrowBoundsToFitText() {
|
|
76
|
+
func testCreateFreeTextAnnotationExpandsNarrowBoundsToFitText() throws {
|
|
77
77
|
let originalBounds = AnnotationBounds(x: 100, y: 200, width: 40, height: 24)
|
|
78
78
|
let model = AnnotationModel(
|
|
79
79
|
id: "txt-narrow-1",
|
|
@@ -88,7 +88,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
88
88
|
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
89
89
|
XCTAssertNotNil(annotation)
|
|
90
90
|
XCTAssertGreaterThan(annotation?.bounds.width ?? 0, originalBounds.width)
|
|
91
|
-
XCTAssertEqual(annotation
|
|
91
|
+
XCTAssertEqual(try XCTUnwrap(annotation).bounds.maxY, originalBounds.cgRect.maxY, accuracy: 0.01)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
func testUnknownTypeReturnsNil() {
|
|
@@ -105,7 +105,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
105
105
|
|
|
106
106
|
// MARK: - Model extraction
|
|
107
107
|
|
|
108
|
-
func testExtractModelFromModuleManagedAnnotation() {
|
|
108
|
+
func testExtractModelFromModuleManagedAnnotation() throws {
|
|
109
109
|
let model = AnnotationModel(
|
|
110
110
|
id: "ext-1",
|
|
111
111
|
type: "ink",
|
|
@@ -127,7 +127,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
127
127
|
XCTAssertEqual(result.color, "#FF0000")
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
func testFreeTextColorRoundTrip() {
|
|
130
|
+
func testFreeTextColorRoundTrip() throws {
|
|
131
131
|
let model = AnnotationModel(
|
|
132
132
|
id: "ft-color-1",
|
|
133
133
|
type: "freeText",
|
|
@@ -179,7 +179,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
179
179
|
|
|
180
180
|
// MARK: - Color helpers
|
|
181
181
|
|
|
182
|
-
func testColorFromHex() {
|
|
182
|
+
func testColorFromHex() throws {
|
|
183
183
|
let red = try XCTUnwrap(AnnotationSerializer.colorFromHex("#FF0000"))
|
|
184
184
|
|
|
185
185
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
@@ -189,7 +189,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
189
189
|
XCTAssertEqual(b, 0.0, accuracy: 0.01)
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
-
func testColorFromHexWithoutHash() {
|
|
192
|
+
func testColorFromHexWithoutHash() throws {
|
|
193
193
|
let blue = try XCTUnwrap(AnnotationSerializer.colorFromHex("0000FF"))
|
|
194
194
|
|
|
195
195
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
@@ -124,4 +124,44 @@ final class ExpoPdfMarkupViewTests: XCTestCase {
|
|
|
124
124
|
|
|
125
125
|
XCTAssertEqual(view.pdfView.frame, view.bounds)
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
// MARK: - Ink mode
|
|
129
|
+
|
|
130
|
+
func testInkModeAddsOverlayAndDisablesPdfGestures() throws {
|
|
131
|
+
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
|
|
132
|
+
|
|
133
|
+
// Add a known gesture so the disabled-gesture assertions are non-trivial.
|
|
134
|
+
let knownGesture = UITapGestureRecognizer()
|
|
135
|
+
view.pdfView.addGestureRecognizer(knownGesture)
|
|
136
|
+
|
|
137
|
+
view.setAnnotationMode("ink")
|
|
138
|
+
|
|
139
|
+
XCTAssertNotNil(view.inkOverlayView)
|
|
140
|
+
XCTAssertTrue(try view.subviews.contains(XCTUnwrap(view.inkOverlayView)))
|
|
141
|
+
XCTAssertTrue(view.disabledPdfGestures.contains(knownGesture))
|
|
142
|
+
XCTAssertFalse(knownGesture.isEnabled)
|
|
143
|
+
if let scrollView = view.pdfScrollView {
|
|
144
|
+
XCTAssertFalse(scrollView.isScrollEnabled)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
func testExitingInkModeRemovesOverlayAndRestoresGestures() {
|
|
149
|
+
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
|
|
150
|
+
|
|
151
|
+
// Add a known gesture so we can verify it is actually re-enabled on exit.
|
|
152
|
+
let knownGesture = UITapGestureRecognizer()
|
|
153
|
+
view.pdfView.addGestureRecognizer(knownGesture)
|
|
154
|
+
|
|
155
|
+
view.setAnnotationMode("ink")
|
|
156
|
+
XCTAssertFalse(knownGesture.isEnabled)
|
|
157
|
+
|
|
158
|
+
view.setAnnotationMode("none")
|
|
159
|
+
|
|
160
|
+
XCTAssertNil(view.inkOverlayView)
|
|
161
|
+
XCTAssertTrue(view.disabledPdfGestures.isEmpty)
|
|
162
|
+
XCTAssertTrue(knownGesture.isEnabled)
|
|
163
|
+
if let scrollView = view.pdfScrollView {
|
|
164
|
+
XCTAssertTrue(scrollView.isScrollEnabled)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
127
167
|
}
|