@tobyt/expo-pdf-markup 1.2.0 → 2.0.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 +16 -0
- package/README.md +18 -0
- package/android/build.gradle +3 -0
- package/build/ExpoPdfMarkup.types.d.ts +7 -1
- 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 +28 -22
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/docs/annotation-coordinates.md +45 -0
- package/docs/annotation-validation.md +117 -0
- package/fixtures/annotations-v1.json +130 -0
- package/ios/AnnotationModels.swift +29 -30
- package/ios/AnnotationSerializer.swift +99 -12
- package/ios/ExpoPdfMarkupView+Ink.swift +13 -28
- package/ios/ExpoPdfMarkupView+Markup.swift +151 -0
- package/ios/ExpoPdfMarkupView+Move.swift +17 -6
- package/ios/ExpoPdfMarkupView.swift +30 -82
- package/ios/StampPDFAnnotation.swift +21 -3
- package/ios/Tests/AnnotationCoordinateTests.swift +337 -0
- package/ios/Tests/AnnotationSerializerTests.swift +43 -1
- package/ios/Tests/AreaMarkupTests.swift +279 -0
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +29 -0
- package/ios/Tests/RotatedAnnotationRenderingTests.swift +411 -0
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +7 -1
- package/src/ExpoPdfMarkupView.web.tsx +28 -18
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import PDFKit
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
extension ExpoPdfMarkupView {
|
|
5
|
+
/// Area input works on scanned and selectable pages alike. Navigation is explicit:
|
|
6
|
+
/// leave the tool for touch scrolling/zoom, or use the page prop/book hardware keys.
|
|
7
|
+
func setupAreaMarkupMode() {
|
|
8
|
+
guard markupOverlayView == nil else { return }
|
|
9
|
+
markupScrollWasEnabled = pdfScrollView?.isScrollEnabled
|
|
10
|
+
pdfScrollView?.isScrollEnabled = false
|
|
11
|
+
markupDisabledGestures = pdfView.gestureRecognizers?.filter(\.isEnabled) ?? []
|
|
12
|
+
markupDisabledGestures.forEach { $0.isEnabled = false }
|
|
13
|
+
pdfView.clearSelection()
|
|
14
|
+
|
|
15
|
+
let overlay = InkOverlayView(frame: bounds)
|
|
16
|
+
overlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
17
|
+
let preview = CAShapeLayer()
|
|
18
|
+
overlay.layer.addSublayer(preview)
|
|
19
|
+
markupPreviewLayer = preview
|
|
20
|
+
overlay.onBegan = { [weak self] in self?.beginAreaMarkup(at: $0) }
|
|
21
|
+
overlay.onMoved = { [weak self] in self?.updateAreaMarkup(at: $0) }
|
|
22
|
+
overlay.onEnded = { [weak self] in self?.finishAreaMarkup() }
|
|
23
|
+
overlay.onCancelled = { [weak self] in self?.cancelAreaMarkup() }
|
|
24
|
+
addSubview(overlay)
|
|
25
|
+
markupOverlayView = overlay
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func removeAreaMarkupMode() {
|
|
29
|
+
cancelAreaMarkup()
|
|
30
|
+
markupOverlayView?.removeFromSuperview()
|
|
31
|
+
markupOverlayView = nil
|
|
32
|
+
markupPreviewLayer = nil
|
|
33
|
+
if let wasEnabled = markupScrollWasEnabled { pdfScrollView?.isScrollEnabled = wasEnabled }
|
|
34
|
+
markupScrollWasEnabled = nil
|
|
35
|
+
markupDisabledGestures.forEach { $0.isEnabled = true }
|
|
36
|
+
markupDisabledGestures = []
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func cancelAreaMarkup() {
|
|
40
|
+
markupPage = nil
|
|
41
|
+
markupStartPoint = nil
|
|
42
|
+
markupEndPoint = nil
|
|
43
|
+
markupPreviewLayer?.path = nil
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private func beginAreaMarkup(at point: CGPoint) {
|
|
47
|
+
cancelAreaMarkup()
|
|
48
|
+
let location = pdfView.convert(point, from: self)
|
|
49
|
+
guard let page = pdfView.page(for: location, nearest: false) else { return }
|
|
50
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
51
|
+
let visible = pdfView.convert(location, to: page).applying(coordinates.nativeToVisible)
|
|
52
|
+
guard coordinates.visibleBounds.contains(visible) else { return }
|
|
53
|
+
markupPage = page
|
|
54
|
+
markupStartPoint = visible
|
|
55
|
+
markupEndPoint = visible
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private func updateAreaMarkup(at point: CGPoint) {
|
|
59
|
+
guard let page = markupPage, page.document === pdfView.document else { return }
|
|
60
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
61
|
+
let visible = pdfView.convert(pdfView.convert(point, from: self), to: page)
|
|
62
|
+
.applying(coordinates.nativeToVisible)
|
|
63
|
+
let bounds = coordinates.visibleBounds
|
|
64
|
+
markupEndPoint = CGPoint(x: min(max(visible.x, bounds.minX), bounds.maxX),
|
|
65
|
+
y: min(max(visible.y, bounds.minY), bounds.maxY))
|
|
66
|
+
guard let area = areaMarkupBounds else { return }
|
|
67
|
+
let native = area.applying(coordinates.visibleToNative)
|
|
68
|
+
let display = convert(pdfView.convert(native, from: page), from: pdfView)
|
|
69
|
+
let path = UIBezierPath()
|
|
70
|
+
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
71
|
+
if currentMode == "highlight" {
|
|
72
|
+
path.append(UIBezierPath(rect: display))
|
|
73
|
+
markupPreviewLayer?.fillColor = color.withAlphaComponent(0.5).cgColor
|
|
74
|
+
markupPreviewLayer?.strokeColor = nil
|
|
75
|
+
} else {
|
|
76
|
+
path.move(to: CGPoint(x: display.minX, y: display.maxY))
|
|
77
|
+
path.addLine(to: CGPoint(x: display.maxX, y: display.maxY))
|
|
78
|
+
markupPreviewLayer?.fillColor = nil
|
|
79
|
+
markupPreviewLayer?.strokeColor = color.cgColor
|
|
80
|
+
markupPreviewLayer?.lineWidth = AreaUnderlinePDFAnnotation.strokeWidth * pdfView.scaleFactor
|
|
81
|
+
}
|
|
82
|
+
markupPreviewLayer?.path = path.cgPath
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private var areaMarkupBounds: CGRect? {
|
|
86
|
+
guard let start = markupStartPoint, let end = markupEndPoint else { return nil }
|
|
87
|
+
return CGRect(x: min(start.x, end.x), y: min(start.y, end.y),
|
|
88
|
+
width: abs(end.x - start.x), height: abs(end.y - start.y))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private func finishAreaMarkup() {
|
|
92
|
+
defer { cancelAreaMarkup() }
|
|
93
|
+
guard currentMode == "highlight" || currentMode == "underline",
|
|
94
|
+
let page = markupPage, page.document === pdfView.document,
|
|
95
|
+
let bounds = areaMarkupBounds, bounds.width > 1, bounds.height > 1 else { return }
|
|
96
|
+
let model = AnnotationModel(id: UUID().uuidString, type: currentMode,
|
|
97
|
+
page: pdfView.document?.index(for: page) ?? 0, color: annotationColor,
|
|
98
|
+
alpha: currentMode == "highlight" ? 0.5 : nil,
|
|
99
|
+
bounds: AnnotationBounds(bounds), createdAt: Date().timeIntervalSince1970)
|
|
100
|
+
guard let annotation = AnnotationSerializer.toPDFAnnotation(
|
|
101
|
+
model, pageBounds: page.bounds(for: .cropBox), pageRotation: page.rotation
|
|
102
|
+
) else { return }
|
|
103
|
+
page.addAnnotation(annotation)
|
|
104
|
+
emitAnnotationsChanged()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// PDFKit's built-in underline offsets its stroke toward native page-up. On rotated
|
|
109
|
+
/// pages that can select the wrong edge or put the line outside the crop box.
|
|
110
|
+
/// Drawing the visible rectangle's lower edge also keeps edit and hit geometry aligned.
|
|
111
|
+
final class AreaUnderlinePDFAnnotation: PDFAnnotation {
|
|
112
|
+
static let strokeWidth: CGFloat = 2
|
|
113
|
+
|
|
114
|
+
required init?(coder: NSCoder) {
|
|
115
|
+
super.init(coder: coder)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
override init(bounds: CGRect, forType type: PDFAnnotationSubtype, withProperties properties: [AnyHashable: Any]?) {
|
|
119
|
+
super.init(
|
|
120
|
+
bounds: bounds.insetBy(
|
|
121
|
+
dx: -AnnotationCoordinates.appearancePadding,
|
|
122
|
+
dy: -AnnotationCoordinates.appearancePadding
|
|
123
|
+
),
|
|
124
|
+
forType: type,
|
|
125
|
+
withProperties: properties
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
var contentBounds: CGRect {
|
|
130
|
+
bounds.insetBy(dx: AnnotationCoordinates.appearancePadding, dy: AnnotationCoordinates.appearancePadding)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var renderedBounds: CGRect {
|
|
134
|
+
contentBounds.insetBy(dx: -Self.strokeWidth / 2, dy: -Self.strokeWidth / 2)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
override func draw(with box: PDFDisplayBox, in context: CGContext) {
|
|
138
|
+
let coordinates = page.map {
|
|
139
|
+
AnnotationCoordinates(bounds: $0.bounds(for: box), rotation: $0.rotation)
|
|
140
|
+
} ?? AnnotationCoordinates(bounds: .zero)
|
|
141
|
+
let visible = contentBounds.applying(coordinates.nativeToVisible)
|
|
142
|
+
context.saveGState()
|
|
143
|
+
coordinates.prepareForDrawing(in: context, annotationBounds: bounds)
|
|
144
|
+
context.setStrokeColor(color.cgColor)
|
|
145
|
+
context.setLineWidth(Self.strokeWidth)
|
|
146
|
+
context.move(to: CGPoint(x: visible.minX, y: visible.minY))
|
|
147
|
+
context.addLine(to: CGPoint(x: visible.maxX, y: visible.minY))
|
|
148
|
+
context.strokePath()
|
|
149
|
+
context.restoreGState()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -66,8 +66,11 @@ extension ExpoPdfMarkupView {
|
|
|
66
66
|
let startPoint = moveStartPoint else { return }
|
|
67
67
|
|
|
68
68
|
let pdfPoint = pdfView.convert(location, to: page)
|
|
69
|
-
let
|
|
70
|
-
let
|
|
69
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
70
|
+
let current = pdfPoint.applying(coordinates.nativeToVisible)
|
|
71
|
+
let start = startPoint.applying(coordinates.nativeToVisible)
|
|
72
|
+
let rawDeltaX = current.x - start.x
|
|
73
|
+
let rawDeltaY = current.y - start.y
|
|
71
74
|
let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
|
|
72
75
|
let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
|
|
73
76
|
guard translatedModel != movingPreviewModel,
|
|
@@ -173,9 +176,13 @@ extension ExpoPdfMarkupView {
|
|
|
173
176
|
}
|
|
174
177
|
guard !combinedBounds.isNull else { return nil }
|
|
175
178
|
let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
176
|
-
pdfRect = combinedBounds.
|
|
179
|
+
pdfRect = combinedBounds.offsetBy(dx: annotation.bounds.minX, dy: annotation.bounds.minY).insetBy(
|
|
180
|
+
dx: -padding,
|
|
181
|
+
dy: -padding
|
|
182
|
+
)
|
|
177
183
|
} else {
|
|
178
|
-
pdfRect = annotation
|
|
184
|
+
pdfRect = (annotation as? AreaUnderlinePDFAnnotation)?.renderedBounds ?? (annotation as? StampPDFAnnotation)?
|
|
185
|
+
.contentBounds ?? annotation.bounds
|
|
179
186
|
}
|
|
180
187
|
|
|
181
188
|
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
@@ -183,7 +190,11 @@ extension ExpoPdfMarkupView {
|
|
|
183
190
|
}
|
|
184
191
|
|
|
185
192
|
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
186
|
-
AnnotationSerializer.toPDFAnnotation(
|
|
193
|
+
AnnotationSerializer.toPDFAnnotation(
|
|
194
|
+
model,
|
|
195
|
+
pageBounds: page.bounds(for: pdfView.displayBox),
|
|
196
|
+
pageRotation: page.rotation
|
|
197
|
+
)
|
|
187
198
|
}
|
|
188
199
|
|
|
189
200
|
private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
|
|
@@ -223,7 +234,7 @@ extension ExpoPdfMarkupView {
|
|
|
223
234
|
return CGPoint(x: dx, y: dy)
|
|
224
235
|
}
|
|
225
236
|
|
|
226
|
-
let pageBounds = page
|
|
237
|
+
let pageBounds = AnnotationCoordinates(page: page).visibleBounds
|
|
227
238
|
let minDx = pageBounds.minX - outlineBounds.x
|
|
228
239
|
let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
|
|
229
240
|
let minDy = pageBounds.minY - outlineBounds.y
|
|
@@ -40,9 +40,13 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
40
40
|
var bookTapCount = 1
|
|
41
41
|
var bookTurnSnapshot: UIView?
|
|
42
42
|
var bookGestureDelegate: BookGestureDelegate?
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
var markupOverlayView: InkOverlayView?
|
|
44
|
+
var markupPreviewLayer: CAShapeLayer?
|
|
45
|
+
var markupPage: PDFPage?
|
|
46
|
+
var markupStartPoint: CGPoint?
|
|
47
|
+
var markupEndPoint: CGPoint?
|
|
48
|
+
var markupDisabledGestures: [UIGestureRecognizer] = []
|
|
49
|
+
var markupScrollWasEnabled: Bool?
|
|
46
50
|
var movingPage: PDFPage?
|
|
47
51
|
var movingOriginalModel: AnnotationModel?
|
|
48
52
|
var movingPreviewModel: AnnotationModel?
|
|
@@ -87,9 +91,6 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
87
91
|
|
|
88
92
|
deinit {
|
|
89
93
|
NotificationCenter.default.removeObserver(self)
|
|
90
|
-
if let selectionObserver {
|
|
91
|
-
NotificationCenter.default.removeObserver(selectionObserver)
|
|
92
|
-
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
override func layoutSubviews() {
|
|
@@ -125,6 +126,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
125
126
|
|
|
126
127
|
func loadPdf(from source: String) {
|
|
127
128
|
guard source != currentSource else { return }
|
|
129
|
+
cancelAreaMarkup()
|
|
128
130
|
currentSource = source
|
|
129
131
|
|
|
130
132
|
let fileURL = URL(fileURLWithPath: source)
|
|
@@ -155,6 +157,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
155
157
|
return
|
|
156
158
|
}
|
|
157
159
|
|
|
160
|
+
cancelAreaMarkup()
|
|
158
161
|
if isBookMode {
|
|
159
162
|
jumpToBookPage(page)
|
|
160
163
|
} else {
|
|
@@ -185,7 +188,11 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
185
188
|
guard model.page >= 0, model.page < document.pageCount else { continue }
|
|
186
189
|
guard let page = document.page(at: model.page) else { continue }
|
|
187
190
|
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
188
|
-
guard let pdfAnnotation = AnnotationSerializer.toPDFAnnotation(
|
|
191
|
+
guard let pdfAnnotation = AnnotationSerializer.toPDFAnnotation(
|
|
192
|
+
model,
|
|
193
|
+
pageBounds: pageBounds,
|
|
194
|
+
pageRotation: page.rotation
|
|
195
|
+
) else {
|
|
189
196
|
continue
|
|
190
197
|
}
|
|
191
198
|
page.addAnnotation(pdfAnnotation)
|
|
@@ -240,6 +247,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
240
247
|
func setDisplayMode(_ mode: String) {
|
|
241
248
|
let normalised = mode == "book" ? "book" : "continuous"
|
|
242
249
|
guard normalised != displayMode else { return }
|
|
250
|
+
cancelAreaMarkup()
|
|
243
251
|
displayMode = normalised
|
|
244
252
|
applyDisplayMode()
|
|
245
253
|
}
|
|
@@ -252,12 +260,15 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
252
260
|
|
|
253
261
|
// Clean up previous mode
|
|
254
262
|
if previousMode != mode {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
263
|
+
switch previousMode {
|
|
264
|
+
case "ink": removeInkMode()
|
|
265
|
+
case "eraser": removeEraserGesture()
|
|
266
|
+
case "move": removeMoveMode()
|
|
267
|
+
case "text": removeTextGesture()
|
|
268
|
+
case "stamp": removeStampGesture()
|
|
269
|
+
case "highlight", "underline": removeAreaMarkupMode()
|
|
270
|
+
default: break
|
|
271
|
+
}
|
|
261
272
|
}
|
|
262
273
|
|
|
263
274
|
switch mode {
|
|
@@ -268,7 +279,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
268
279
|
case "move":
|
|
269
280
|
setupMoveMode()
|
|
270
281
|
case "highlight", "underline":
|
|
271
|
-
|
|
282
|
+
setupAreaMarkupMode()
|
|
272
283
|
case "text":
|
|
273
284
|
setupTextGesture()
|
|
274
285
|
case "stamp":
|
|
@@ -325,83 +336,19 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
325
336
|
lineJoin: .round,
|
|
326
337
|
miterLimit: 0
|
|
327
338
|
)
|
|
328
|
-
|
|
339
|
+
let localPoint = CGPoint(x: point.x - annotation.bounds.minX, y: point.y - annotation.bounds.minY)
|
|
340
|
+
if hitPath.contains(localPoint) {
|
|
329
341
|
return annotation
|
|
330
342
|
}
|
|
331
343
|
}
|
|
332
|
-
} else if annotation
|
|
344
|
+
} else if ((annotation as? AreaUnderlinePDFAnnotation)?.renderedBounds ?? (annotation as? StampPDFAnnotation)?
|
|
345
|
+
.contentBounds ?? annotation.bounds).contains(point) {
|
|
333
346
|
return annotation
|
|
334
347
|
}
|
|
335
348
|
}
|
|
336
349
|
return nil
|
|
337
350
|
}
|
|
338
351
|
|
|
339
|
-
// MARK: - Highlight / Underline
|
|
340
|
-
|
|
341
|
-
private func setupSelectionObserver(markupType: String) {
|
|
342
|
-
guard selectionObserver == nil else { return }
|
|
343
|
-
currentMarkupType = markupType
|
|
344
|
-
|
|
345
|
-
// Observe selection changes with a debounce — the notification fires continuously
|
|
346
|
-
// during drag. We wait for the selection to settle before creating the annotation.
|
|
347
|
-
selectionObserver = NotificationCenter.default.addObserver(
|
|
348
|
-
forName: .PDFViewSelectionChanged,
|
|
349
|
-
object: pdfView,
|
|
350
|
-
queue: .main
|
|
351
|
-
) { [weak self] _ in
|
|
352
|
-
self?.scheduleSelectionHandler()
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
private func removeSelectionObserver() {
|
|
357
|
-
selectionDebounceTimer?.invalidate()
|
|
358
|
-
selectionDebounceTimer = nil
|
|
359
|
-
currentMarkupType = nil
|
|
360
|
-
if let observer = selectionObserver {
|
|
361
|
-
NotificationCenter.default.removeObserver(observer)
|
|
362
|
-
selectionObserver = nil
|
|
363
|
-
}
|
|
364
|
-
pdfView.clearSelection()
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
private func scheduleSelectionHandler() {
|
|
368
|
-
selectionDebounceTimer?.invalidate()
|
|
369
|
-
selectionDebounceTimer = Timer.scheduledTimer(
|
|
370
|
-
withTimeInterval: 0.5,
|
|
371
|
-
repeats: false
|
|
372
|
-
) { [weak self] _ in
|
|
373
|
-
self?.applyMarkupToSelection()
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private func applyMarkupToSelection() {
|
|
378
|
-
guard let markupType = currentMarkupType else { return }
|
|
379
|
-
guard let selection = pdfView.currentSelection else { return }
|
|
380
|
-
let selectionsByPage = selection.selectionsByLine()
|
|
381
|
-
|
|
382
|
-
var didAdd = false
|
|
383
|
-
for lineSelection in selectionsByPage {
|
|
384
|
-
guard let page = lineSelection.pages.first else { continue }
|
|
385
|
-
let bounds = lineSelection.bounds(for: page)
|
|
386
|
-
guard bounds.width > 0, bounds.height > 0 else { continue }
|
|
387
|
-
|
|
388
|
-
let subtype: PDFAnnotationSubtype = markupType == "highlight" ? .highlight : .underline
|
|
389
|
-
let annotation = PDFAnnotation(bounds: bounds, forType: subtype, withProperties: nil)
|
|
390
|
-
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .yellow
|
|
391
|
-
annotation.color = markupType == "highlight" ? color.withAlphaComponent(0.5) : color
|
|
392
|
-
|
|
393
|
-
let id = UUID().uuidString
|
|
394
|
-
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
395
|
-
page.addAnnotation(annotation)
|
|
396
|
-
didAdd = true
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
if didAdd {
|
|
400
|
-
pdfView.clearSelection()
|
|
401
|
-
emitAnnotationsChanged()
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
352
|
// MARK: - UIGestureRecognizerDelegate
|
|
406
353
|
|
|
407
354
|
func gestureRecognizer(
|
|
@@ -414,6 +361,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
414
361
|
// MARK: - Page change notification
|
|
415
362
|
|
|
416
363
|
@objc private func handlePageChanged() {
|
|
364
|
+
cancelAreaMarkup()
|
|
417
365
|
guard let document = pdfView.document,
|
|
418
366
|
let currentPage = pdfView.currentPage else { return }
|
|
419
367
|
|
|
@@ -16,7 +16,18 @@ final class StampPDFAnnotation: PDFAnnotation {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
override init(bounds: CGRect, forType type: PDFAnnotationSubtype, withProperties properties: [AnyHashable: Any]?) {
|
|
19
|
-
super.init(
|
|
19
|
+
super.init(
|
|
20
|
+
bounds: bounds.insetBy(
|
|
21
|
+
dx: -AnnotationCoordinates.appearancePadding,
|
|
22
|
+
dy: -AnnotationCoordinates.appearancePadding
|
|
23
|
+
),
|
|
24
|
+
forType: type,
|
|
25
|
+
withProperties: properties
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var contentBounds: CGRect {
|
|
30
|
+
bounds.insetBy(dx: AnnotationCoordinates.appearancePadding, dy: AnnotationCoordinates.appearancePadding)
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
func setStampText(_ text: String) {
|
|
@@ -27,12 +38,19 @@ final class StampPDFAnnotation: PDFAnnotation {
|
|
|
27
38
|
value(forAnnotationKey: Self.textKey) as? String
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
override func draw(with
|
|
41
|
+
override func draw(with box: PDFDisplayBox, in context: CGContext) {
|
|
31
42
|
guard let text = stampText else { return }
|
|
32
43
|
let colorHex = (value(forAnnotationKey: Self.colorKey) as? String) ?? "#000000"
|
|
33
44
|
guard let cgImage = Self.image(forText: text, colorHex: colorHex) else { return }
|
|
34
45
|
|
|
35
|
-
|
|
46
|
+
let coordinates = page.map {
|
|
47
|
+
AnnotationCoordinates(bounds: $0.bounds(for: box), rotation: $0.rotation)
|
|
48
|
+
} ?? AnnotationCoordinates(bounds: .zero)
|
|
49
|
+
let visibleBounds = contentBounds.applying(coordinates.nativeToVisible)
|
|
50
|
+
context.saveGState()
|
|
51
|
+
coordinates.prepareForDrawing(in: context, annotationBounds: bounds)
|
|
52
|
+
context.draw(cgImage, in: visibleBounds)
|
|
53
|
+
context.restoreGState()
|
|
36
54
|
}
|
|
37
55
|
|
|
38
56
|
private static func image(forText text: String, colorHex: String) -> CGImage? {
|