@tobyt/expo-pdf-markup 0.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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/android/.editorconfig +2 -0
  4. package/android/build.gradle +30 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +76 -0
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +27 -0
  8. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +138 -0
  9. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +152 -0
  10. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +539 -0
  11. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +81 -0
  12. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +147 -0
  13. package/build/ExpoPdfMarkup.types.d.ts +96 -0
  14. package/build/ExpoPdfMarkup.types.d.ts.map +1 -0
  15. package/build/ExpoPdfMarkup.types.js +2 -0
  16. package/build/ExpoPdfMarkup.types.js.map +1 -0
  17. package/build/ExpoPdfMarkupModule.d.ts +2 -0
  18. package/build/ExpoPdfMarkupModule.d.ts.map +1 -0
  19. package/build/ExpoPdfMarkupModule.js +4 -0
  20. package/build/ExpoPdfMarkupModule.js.map +1 -0
  21. package/build/ExpoPdfMarkupModule.web.d.ts +2 -0
  22. package/build/ExpoPdfMarkupModule.web.d.ts.map +1 -0
  23. package/build/ExpoPdfMarkupModule.web.js +4 -0
  24. package/build/ExpoPdfMarkupModule.web.js.map +1 -0
  25. package/build/ExpoPdfMarkupView.d.ts +4 -0
  26. package/build/ExpoPdfMarkupView.d.ts.map +1 -0
  27. package/build/ExpoPdfMarkupView.js +7 -0
  28. package/build/ExpoPdfMarkupView.js.map +1 -0
  29. package/build/ExpoPdfMarkupView.web.d.ts +4 -0
  30. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -0
  31. package/build/ExpoPdfMarkupView.web.js +8 -0
  32. package/build/ExpoPdfMarkupView.web.js.map +1 -0
  33. package/build/index.d.ts +3 -0
  34. package/build/index.d.ts.map +1 -0
  35. package/build/index.js +3 -0
  36. package/build/index.js.map +1 -0
  37. package/expo-module.config.json +9 -0
  38. package/ios/AnnotationModels.swift +73 -0
  39. package/ios/AnnotationSerializer.swift +252 -0
  40. package/ios/ExpoPdfMarkup.podspec +28 -0
  41. package/ios/ExpoPdfMarkupModule.swift +39 -0
  42. package/ios/ExpoPdfMarkupView.swift +541 -0
  43. package/package.json +67 -0
  44. package/src/ExpoPdfMarkup.types.ts +80 -0
  45. package/src/ExpoPdfMarkupModule.ts +2 -0
  46. package/src/ExpoPdfMarkupModule.web.ts +2 -0
  47. package/src/ExpoPdfMarkupView.tsx +10 -0
  48. package/src/ExpoPdfMarkupView.web.tsx +12 -0
  49. package/src/index.ts +2 -0
@@ -0,0 +1,252 @@
1
+ import PDFKit
2
+ import UIKit
3
+
4
+ enum AnnotationSerializer {
5
+ static let ownershipKey = "_expoPdfMarkup"
6
+
7
+ // MARK: - JSON ↔ AnnotationsData
8
+
9
+ static func deserialize(from json: String) -> AnnotationsData? {
10
+ guard let data = json.data(using: .utf8) else { return nil }
11
+ return try? JSONDecoder().decode(AnnotationsData.self, from: data)
12
+ }
13
+
14
+ static func serialize(_ annotationsData: AnnotationsData) -> String? {
15
+ guard let data = try? JSONEncoder().encode(annotationsData) else { return nil }
16
+ return String(data: data, encoding: .utf8)
17
+ }
18
+
19
+ // MARK: - PDFAnnotation ↔ AnnotationModel
20
+
21
+ static func toPDFAnnotation(_ model: AnnotationModel, pageBounds: CGRect? = nil) -> PDFAnnotation? {
22
+ let color = colorFromHex(model.color) ?? .red
23
+
24
+ switch model.type {
25
+ case "ink":
26
+ return createInkAnnotation(model: model, color: color, pageBounds: pageBounds)
27
+ case "highlight":
28
+ return createMarkupAnnotation(model: model, color: color, subtype: .highlight)
29
+ case "underline":
30
+ return createMarkupAnnotation(model: model, color: color, subtype: .underline)
31
+ case "text", "freeText":
32
+ return createFreeTextAnnotation(model: model, color: color)
33
+ default:
34
+ return nil
35
+ }
36
+ }
37
+
38
+ static func toModel(from annotation: PDFAnnotation, page: Int) -> AnnotationModel? {
39
+ guard isModuleManaged(annotation) else { return nil }
40
+ guard let id = annotation.value(forAnnotationKey: PDFAnnotationKey(rawValue: "_id")) as? String else {
41
+ return nil
42
+ }
43
+ let createdAt = annotation.value(
44
+ forAnnotationKey: PDFAnnotationKey(rawValue: "_createdAt")
45
+ ) as? Double ?? Date().timeIntervalSince1970
46
+
47
+ let type: String
48
+ switch annotation.type {
49
+ case "Ink":
50
+ type = "ink"
51
+ case "Highlight":
52
+ type = "highlight"
53
+ case "Underline":
54
+ type = "underline"
55
+ case "Text":
56
+ type = "text"
57
+ case "FreeText":
58
+ type = "freeText"
59
+ default:
60
+ return nil
61
+ }
62
+
63
+ let colorHex = hexFromColor(annotation.color ?? .red)
64
+ var model = AnnotationModel(
65
+ id: id,
66
+ type: type,
67
+ page: page,
68
+ color: colorHex,
69
+ createdAt: createdAt
70
+ )
71
+
72
+ switch type {
73
+ case "ink":
74
+ model.lineWidth = annotation.border?.lineWidth ?? 2.0
75
+ if let bezierPaths = annotation.paths {
76
+ model.paths = bezierPaths.map { path in
77
+ pointsFromBezierPath(path).map { ["x": $0.x, "y": $0.y] }
78
+ }
79
+ }
80
+ case "highlight", "underline":
81
+ model.bounds = AnnotationBounds(annotation.bounds)
82
+ if type == "highlight" {
83
+ model.alpha = 0.5
84
+ }
85
+ case "text", "freeText":
86
+ model.bounds = AnnotationBounds(annotation.bounds)
87
+ model.contents = annotation.contents
88
+ if let font = annotation.font {
89
+ model.fontSize = font.pointSize
90
+ }
91
+ default:
92
+ break
93
+ }
94
+
95
+ return model
96
+ }
97
+
98
+ // MARK: - Ownership
99
+
100
+ static func isModuleManaged(_ annotation: PDFAnnotation) -> Bool {
101
+ annotation.value(forAnnotationKey: PDFAnnotationKey(rawValue: ownershipKey)) as? Bool == true
102
+ }
103
+
104
+ static func tagAsModuleManaged(_ annotation: PDFAnnotation, id: String, createdAt: Double) {
105
+ annotation.setValue(true, forAnnotationKey: PDFAnnotationKey(rawValue: ownershipKey))
106
+ annotation.setValue(id, forAnnotationKey: PDFAnnotationKey(rawValue: "_id"))
107
+ annotation.setValue(createdAt, forAnnotationKey: PDFAnnotationKey(rawValue: "_createdAt"))
108
+ }
109
+
110
+ // MARK: - Color helpers
111
+
112
+ static func colorFromHex(_ hex: String) -> UIColor? {
113
+ var hexStr = hex.trimmingCharacters(in: .whitespacesAndNewlines)
114
+ if hexStr.hasPrefix("#") {
115
+ hexStr.removeFirst()
116
+ }
117
+ guard hexStr.count == 6 || hexStr.count == 8 else { return nil }
118
+ guard hexStr.allSatisfy(\.isHexDigit) else { return nil }
119
+
120
+ var rgb: UInt64 = 0
121
+ Scanner(string: hexStr).scanHexInt64(&rgb)
122
+
123
+ if hexStr.count == 8 {
124
+ return UIColor(
125
+ red: CGFloat((rgb >> 24) & 0xFF) / 255.0,
126
+ green: CGFloat((rgb >> 16) & 0xFF) / 255.0,
127
+ blue: CGFloat((rgb >> 8) & 0xFF) / 255.0,
128
+ alpha: CGFloat(rgb & 0xFF) / 255.0
129
+ )
130
+ }
131
+ return UIColor(
132
+ red: CGFloat((rgb >> 16) & 0xFF) / 255.0,
133
+ green: CGFloat((rgb >> 8) & 0xFF) / 255.0,
134
+ blue: CGFloat(rgb & 0xFF) / 255.0,
135
+ alpha: 1.0
136
+ )
137
+ }
138
+
139
+ static func hexFromColor(_ color: UIColor) -> String {
140
+ var red: CGFloat = 0
141
+ var green: CGFloat = 0
142
+ var blue: CGFloat = 0
143
+ var alpha: CGFloat = 0
144
+ color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
145
+ return String(
146
+ format: "#%02X%02X%02X",
147
+ Int(round(red * 255)),
148
+ Int(round(green * 255)),
149
+ Int(round(blue * 255))
150
+ )
151
+ }
152
+
153
+ // MARK: - Private helpers
154
+
155
+ private static func createInkAnnotation(
156
+ model: AnnotationModel,
157
+ color: UIColor,
158
+ pageBounds: CGRect? = nil
159
+ ) -> PDFAnnotation {
160
+ let paths = model.decodedPaths
161
+ let lineWidth = model.lineWidth ?? 2.0
162
+
163
+ // Use full page bounds if available; otherwise compute from points with generous padding.
164
+ // PDFKit clips ink rendering to the annotation bounds rect, so tight bounds cause
165
+ // strokes to be partially or fully invisible.
166
+ let annotationBounds: CGRect
167
+ if let pageBounds {
168
+ annotationBounds = pageBounds
169
+ } else {
170
+ var combinedRect = CGRect.null
171
+ for path in paths {
172
+ for point in path {
173
+ combinedRect = combinedRect.union(CGRect(x: point.x, y: point.y, width: 0, height: 0))
174
+ }
175
+ }
176
+ let padding = max(lineWidth * 2, 10.0)
177
+ annotationBounds = combinedRect.insetBy(dx: -padding, dy: -padding)
178
+ }
179
+
180
+ let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
181
+ annotation.color = color
182
+
183
+ let border = PDFBorder()
184
+ border.lineWidth = lineWidth
185
+ annotation.border = border
186
+
187
+ for path in paths {
188
+ let bezierPath = UIBezierPath()
189
+ for (index, point) in path.enumerated() {
190
+ if index == 0 {
191
+ bezierPath.move(to: CGPoint(x: point.x, y: point.y))
192
+ } else {
193
+ bezierPath.addLine(to: CGPoint(x: point.x, y: point.y))
194
+ }
195
+ }
196
+ annotation.add(bezierPath)
197
+ }
198
+
199
+ let createdAt = model.createdAt ?? Date().timeIntervalSince1970
200
+ tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
201
+ return annotation
202
+ }
203
+
204
+ private static func createMarkupAnnotation(
205
+ model: AnnotationModel,
206
+ color: UIColor,
207
+ subtype: PDFAnnotationSubtype
208
+ ) -> PDFAnnotation? {
209
+ guard let bounds = model.bounds else { return nil }
210
+ let annotation = PDFAnnotation(bounds: bounds.cgRect, forType: subtype, withProperties: nil)
211
+ let alpha = model.alpha ?? (subtype == .highlight ? 0.5 : 1.0)
212
+ annotation.color = color.withAlphaComponent(alpha)
213
+
214
+ let createdAt = model.createdAt ?? Date().timeIntervalSince1970
215
+ tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
216
+ return annotation
217
+ }
218
+
219
+ private static func createFreeTextAnnotation(model: AnnotationModel, color: UIColor) -> PDFAnnotation? {
220
+ guard let bounds = model.bounds else { return nil }
221
+ let annotation = PDFAnnotation(bounds: bounds.cgRect, forType: .freeText, withProperties: nil)
222
+ let fontSize = model.fontSize ?? 16.0
223
+ annotation.font = UIFont.systemFont(ofSize: fontSize)
224
+ annotation.fontColor = color
225
+ annotation.color = .clear
226
+ annotation.contents = model.contents ?? ""
227
+
228
+ let createdAt = model.createdAt ?? Date().timeIntervalSince1970
229
+ tagAsModuleManaged(annotation, id: model.id, createdAt: createdAt)
230
+ return annotation
231
+ }
232
+
233
+ private static func pointsFromBezierPath(_ path: UIBezierPath) -> [CGPoint] {
234
+ var points: [CGPoint] = []
235
+ let cgPath = path.cgPath
236
+ cgPath.applyWithBlock { element in
237
+ switch element.pointee.type {
238
+ case .moveToPoint, .addLineToPoint:
239
+ points.append(element.pointee.points[0])
240
+ case .addQuadCurveToPoint:
241
+ points.append(element.pointee.points[1])
242
+ case .addCurveToPoint:
243
+ points.append(element.pointee.points[2])
244
+ case .closeSubpath:
245
+ break
246
+ @unknown default:
247
+ break
248
+ }
249
+ }
250
+ return points
251
+ }
252
+ }
@@ -0,0 +1,28 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'ExpoPdfMarkup'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.description = package['description']
10
+ s.license = package['license']
11
+ s.author = package['author']
12
+ s.homepage = package['homepage']
13
+ s.platforms = {
14
+ :ios => '15.1'
15
+ }
16
+ s.swift_version = '5.9'
17
+ s.source = { git: 'https://github.com/tobyt42/expo-pdf-markup' }
18
+ s.static_framework = true
19
+
20
+ s.dependency 'ExpoModulesCore'
21
+
22
+ # Swift/Objective-C compatibility
23
+ s.pod_target_xcconfig = {
24
+ 'DEFINES_MODULE' => 'YES',
25
+ }
26
+
27
+ s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
28
+ end
@@ -0,0 +1,39 @@
1
+ import ExpoModulesCore
2
+
3
+ public class ExpoPdfMarkupModule: Module {
4
+ public func definition() -> ModuleDefinition {
5
+ Name("ExpoPdfMarkup")
6
+
7
+ View(ExpoPdfMarkupView.self) {
8
+ Prop("source") { (view: ExpoPdfMarkupView, source: String) in
9
+ view.loadPdf(from: source)
10
+ }
11
+
12
+ Prop("page") { (view: ExpoPdfMarkupView, page: Int) in
13
+ view.goToPage(page)
14
+ }
15
+
16
+ Prop("backgroundColor") { (view: ExpoPdfMarkupView, color: UIColor?) in
17
+ view.setBackgroundColor(color)
18
+ }
19
+
20
+ Prop("annotations") { (view: ExpoPdfMarkupView, json: String?) in
21
+ view.loadAnnotations(from: json)
22
+ }
23
+
24
+ Prop("annotationMode") { (view: ExpoPdfMarkupView, mode: String?) in
25
+ view.setAnnotationMode(mode ?? "none")
26
+ }
27
+
28
+ Prop("annotationColor") { (view: ExpoPdfMarkupView, color: String?) in
29
+ view.annotationColor = color ?? "#FF0000"
30
+ }
31
+
32
+ Prop("annotationLineWidth") { (view: ExpoPdfMarkupView, width: Double?) in
33
+ view.annotationLineWidth = CGFloat(width ?? 2.0)
34
+ }
35
+
36
+ Events("onPageChanged", "onLoadComplete", "onError", "onAnnotationsChanged")
37
+ }
38
+ }
39
+ }