@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Toby Terhoeven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @tobyt/expo-pdf-markup
2
+
3
+ An Expo module for displaying and annotating PDFs, with support for iOS, Android, and Web.
4
+
5
+ > **Status:** Early development — actively being tested. Web support is planned and will be added soon.
6
+
7
+ ## Platform support
8
+
9
+ | Platform | PDF rendering | Annotations |
10
+ | -------- | ------------- | ----------- |
11
+ | iOS | ✅ | ✅ |
12
+ | Android | ✅ | ✅ |
13
+ | Web | 🔜 | 🔜 |
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ npm install @tobyt/expo-pdf-markup
19
+ ```
20
+
21
+ ### iOS
22
+
23
+ ```sh
24
+ npx pod-install
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Full API reference is available at **[tobyt42.github.io/expo-pdf-markup](https://tobyt42.github.io/expo-pdf-markup/types/ExpoPdfMarkupViewProps.html)**.
30
+
31
+ ```tsx
32
+ import { ExpoPdfMarkupView } from '@tobyt/expo-pdf-markup';
33
+
34
+ export default function App() {
35
+ return <ExpoPdfMarkupView source={{ uri: 'https://example.com/document.pdf' }} />;
36
+ }
37
+ ```
38
+
39
+ ## Development
40
+
41
+ This module uses the [Expo Modules API](https://docs.expo.dev/modules/overview/). The `example` directory contains a test app.
42
+
43
+ ```sh
44
+ # Build the module
45
+ npm run build
46
+
47
+ # Run the example app
48
+ cd example
49
+ npx expo start
50
+ ```
51
+
52
+ ## License
53
+
54
+ MIT
@@ -0,0 +1,2 @@
1
+ [*.kt]
2
+ ktlint_code_style = android_studio
@@ -0,0 +1,30 @@
1
+ plugins {
2
+ id 'com.android.library'
3
+ id 'expo-module-gradle-plugin'
4
+ }
5
+
6
+ group = 'dev.terhoeven.expopdfmarkup'
7
+ version = '0.1.0'
8
+
9
+ android {
10
+ namespace "dev.terhoeven.expopdfmarkup"
11
+ defaultConfig {
12
+ versionCode 1
13
+ versionName "0.1.0"
14
+ }
15
+ lintOptions {
16
+ abortOnError false
17
+ }
18
+ }
19
+
20
+ dependencies {
21
+ testImplementation 'junit:junit:4.13.2'
22
+ testImplementation 'org.json:json:20231013'
23
+ }
24
+
25
+ tasks.withType(Test) {
26
+ testLogging {
27
+ events "passed", "failed", "skipped"
28
+ showStandardStreams = false
29
+ }
30
+ }
@@ -0,0 +1,2 @@
1
+ <manifest>
2
+ </manifest>
@@ -0,0 +1,76 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ import kotlin.math.abs
4
+ import kotlin.math.max
5
+ import kotlin.math.sqrt
6
+
7
+ object AnnotationHitTester {
8
+
9
+ fun hitTest(
10
+ point: AnnotationPoint,
11
+ annotations: List<AnnotationModel>,
12
+ pageIndex: Int
13
+ ): AnnotationModel? {
14
+ // Return last (topmost) matching annotation
15
+ for (annotation in annotations.reversed()) {
16
+ if (annotation.page != pageIndex) continue
17
+ when (annotation.type) {
18
+ "ink" -> {
19
+ val tolerance = max(annotation.lineWidth ?: 2f, 10f)
20
+ if (hitTestInk(point, annotation, tolerance)) return annotation
21
+ }
22
+
23
+ "highlight", "underline", "freeText", "text" -> {
24
+ val bounds = annotation.bounds ?: continue
25
+ if (bounds.contains(point)) return annotation
26
+ }
27
+ }
28
+ }
29
+ return null
30
+ }
31
+
32
+ private fun hitTestInk(
33
+ point: AnnotationPoint,
34
+ annotation: AnnotationModel,
35
+ tolerance: Float
36
+ ): Boolean {
37
+ val paths = annotation.paths ?: return false
38
+ for (stroke in paths) {
39
+ for (i in 0 until stroke.size - 1) {
40
+ if (distanceToSegment(point, stroke[i], stroke[i + 1]) <= tolerance) {
41
+ return true
42
+ }
43
+ }
44
+ // Also check single-point strokes
45
+ if (stroke.size == 1) {
46
+ val dx = point.x - stroke[0].x
47
+ val dy = point.y - stroke[0].y
48
+ if (sqrt(dx * dx + dy * dy) <= tolerance) return true
49
+ }
50
+ }
51
+ return false
52
+ }
53
+
54
+ internal fun distanceToSegment(
55
+ point: AnnotationPoint,
56
+ a: AnnotationPoint,
57
+ b: AnnotationPoint
58
+ ): Float {
59
+ val dx = b.x - a.x
60
+ val dy = b.y - a.y
61
+ val lenSq = dx * dx + dy * dy
62
+ if (lenSq == 0f) {
63
+ // a and b are the same point
64
+ val px = point.x - a.x
65
+ val py = point.y - a.y
66
+ return sqrt(px * px + py * py)
67
+ }
68
+ var t = ((point.x - a.x) * dx + (point.y - a.y) * dy) / lenSq
69
+ t = t.coerceIn(0f, 1f)
70
+ val projX = a.x + t * dx
71
+ val projY = a.y + t * dy
72
+ val ex = point.x - projX
73
+ val ey = point.y - projY
74
+ return sqrt(ex * ex + ey * ey)
75
+ }
76
+ }
@@ -0,0 +1,27 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ data class AnnotationPoint(val x: Float, val y: Float)
4
+
5
+ data class AnnotationBounds(val x: Float, val y: Float, val width: Float, val height: Float) {
6
+ fun contains(point: AnnotationPoint): Boolean =
7
+ point.x >= x && point.x <= x + width && point.y >= y && point.y <= y + height
8
+ }
9
+
10
+ data class AnnotationModel(
11
+ val id: String,
12
+ val type: String,
13
+ val page: Int,
14
+ val color: String,
15
+ val lineWidth: Float? = null,
16
+ val alpha: Float? = null,
17
+ val paths: List<List<AnnotationPoint>>? = null,
18
+ val bounds: AnnotationBounds? = null,
19
+ val contents: String? = null,
20
+ val fontSize: Float? = null,
21
+ val createdAt: Double? = null
22
+ )
23
+
24
+ data class AnnotationsData(
25
+ val version: Int = 1,
26
+ val annotations: List<AnnotationModel> = emptyList()
27
+ )
@@ -0,0 +1,138 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ import android.graphics.Canvas
4
+ import android.graphics.Paint
5
+ import android.graphics.Path
6
+
7
+ object AnnotationRenderer {
8
+
9
+ fun drawAnnotations(
10
+ canvas: Canvas,
11
+ annotations: List<AnnotationModel>,
12
+ pageIndex: Int,
13
+ pageYOffset: Float,
14
+ renderScale: Float,
15
+ pageHeight: Int
16
+ ) {
17
+ for (annotation in annotations) {
18
+ if (annotation.page != pageIndex) continue
19
+ when (annotation.type) {
20
+ "ink" -> drawInk(canvas, annotation, pageYOffset, renderScale, pageHeight)
21
+
22
+ "highlight" -> drawHighlight(
23
+ canvas,
24
+ annotation,
25
+ pageYOffset,
26
+ renderScale,
27
+ pageHeight
28
+ )
29
+
30
+ "underline" -> drawUnderline(
31
+ canvas,
32
+ annotation,
33
+ pageYOffset,
34
+ renderScale,
35
+ pageHeight
36
+ )
37
+
38
+ "freeText", "text" -> drawFreeText(
39
+ canvas,
40
+ annotation,
41
+ pageYOffset,
42
+ renderScale,
43
+ pageHeight
44
+ )
45
+ }
46
+ }
47
+ }
48
+
49
+ private fun drawInk(
50
+ canvas: Canvas,
51
+ annotation: AnnotationModel,
52
+ pageYOffset: Float,
53
+ renderScale: Float,
54
+ pageHeight: Int
55
+ ) {
56
+ val paths = annotation.paths ?: return
57
+ val paint = Paint().apply {
58
+ color = AnnotationSerializer.colorFromHex(annotation.color)
59
+ strokeWidth = (annotation.lineWidth ?: 2f) * renderScale
60
+ style = Paint.Style.STROKE
61
+ isAntiAlias = true
62
+ strokeCap = Paint.Cap.ROUND
63
+ strokeJoin = Paint.Join.ROUND
64
+ }
65
+ for (stroke in paths) {
66
+ if (stroke.isEmpty()) continue
67
+ val path = Path()
68
+ for ((i, pt) in stroke.withIndex()) {
69
+ val cx = pt.x * renderScale
70
+ val cy = (pageHeight - pt.y) * renderScale + pageYOffset
71
+ if (i == 0) path.moveTo(cx, cy) else path.lineTo(cx, cy)
72
+ }
73
+ canvas.drawPath(path, paint)
74
+ }
75
+ }
76
+
77
+ private fun drawHighlight(
78
+ canvas: Canvas,
79
+ annotation: AnnotationModel,
80
+ pageYOffset: Float,
81
+ renderScale: Float,
82
+ pageHeight: Int
83
+ ) {
84
+ val bounds = annotation.bounds ?: return
85
+ val alpha = annotation.alpha ?: 0.5f
86
+ val color = AnnotationSerializer.colorFromHex(annotation.color)
87
+ val paint = Paint().apply {
88
+ this.color = color
89
+ this.alpha = (alpha * 255).toInt()
90
+ style = Paint.Style.FILL
91
+ isAntiAlias = true
92
+ }
93
+ val left = bounds.x * renderScale
94
+ val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
95
+ val right = left + bounds.width * renderScale
96
+ val bottom = top + bounds.height * renderScale
97
+ canvas.drawRect(left, top, right, bottom, paint)
98
+ }
99
+
100
+ private fun drawUnderline(
101
+ canvas: Canvas,
102
+ annotation: AnnotationModel,
103
+ pageYOffset: Float,
104
+ renderScale: Float,
105
+ pageHeight: Int
106
+ ) {
107
+ val bounds = annotation.bounds ?: return
108
+ val paint = Paint().apply {
109
+ color = AnnotationSerializer.colorFromHex(annotation.color)
110
+ strokeWidth = 1f * renderScale
111
+ style = Paint.Style.STROKE
112
+ isAntiAlias = true
113
+ }
114
+ val left = bounds.x * renderScale
115
+ val bottom = (pageHeight - bounds.y) * renderScale + pageYOffset
116
+ val right = left + bounds.width * renderScale
117
+ canvas.drawLine(left, bottom, right, bottom, paint)
118
+ }
119
+
120
+ private fun drawFreeText(
121
+ canvas: Canvas,
122
+ annotation: AnnotationModel,
123
+ pageYOffset: Float,
124
+ renderScale: Float,
125
+ pageHeight: Int
126
+ ) {
127
+ val bounds = annotation.bounds ?: return
128
+ val text = annotation.contents ?: return
129
+ val paint = Paint().apply {
130
+ color = AnnotationSerializer.colorFromHex(annotation.color)
131
+ textSize = (annotation.fontSize ?: 16f) * renderScale
132
+ isAntiAlias = true
133
+ }
134
+ val left = bounds.x * renderScale
135
+ val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
136
+ canvas.drawText(text, left, top + paint.textSize, paint)
137
+ }
138
+ }
@@ -0,0 +1,152 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ import org.json.JSONArray
4
+ import org.json.JSONObject
5
+
6
+ object AnnotationSerializer {
7
+
8
+ fun deserialize(json: String): AnnotationsData? {
9
+ return try {
10
+ val obj = JSONObject(json)
11
+ val version = obj.optInt("version", 1)
12
+ val arr = obj.optJSONArray("annotations") ?: return AnnotationsData(version)
13
+ val annotations = mutableListOf<AnnotationModel>()
14
+ for (i in 0 until arr.length()) {
15
+ parseAnnotation(arr.getJSONObject(i))?.let { annotations.add(it) }
16
+ }
17
+ AnnotationsData(version, annotations)
18
+ } catch (_: Exception) {
19
+ null
20
+ }
21
+ }
22
+
23
+ fun serialize(data: AnnotationsData): String {
24
+ val obj = JSONObject()
25
+ obj.put("version", data.version)
26
+ val arr = JSONArray()
27
+ for (annotation in data.annotations) {
28
+ arr.put(annotationToJson(annotation))
29
+ }
30
+ obj.put("annotations", arr)
31
+ return obj.toString()
32
+ }
33
+
34
+ fun colorFromHex(hex: String): Int {
35
+ val cleaned = hex.removePrefix("#")
36
+ return when (cleaned.length) {
37
+ 6 -> {
38
+ val rgb = cleaned.toLong(16).toInt()
39
+ (0xFF shl 24) or rgb
40
+ }
41
+
42
+ 8 -> {
43
+ val r = cleaned.substring(0, 2).toInt(16)
44
+ val g = cleaned.substring(2, 4).toInt(16)
45
+ val b = cleaned.substring(4, 6).toInt(16)
46
+ val a = cleaned.substring(6, 8).toInt(16)
47
+ (a shl 24) or (r shl 16) or (g shl 8) or b
48
+ }
49
+
50
+ else -> (0xFF shl 24) // fallback to opaque black
51
+ }
52
+ }
53
+
54
+ fun hexFromColor(color: Int): String {
55
+ val r = (color shr 16) and 0xFF
56
+ val g = (color shr 8) and 0xFF
57
+ val b = color and 0xFF
58
+ return "#%02X%02X%02X".format(r, g, b)
59
+ }
60
+
61
+ private fun parseAnnotation(obj: JSONObject): AnnotationModel? {
62
+ val id = obj.optString("id", "").ifEmpty { return null }
63
+ val type = obj.optString("type", "").ifEmpty { return null }
64
+ val page = obj.optInt("page", -1)
65
+ if (page < 0) return null
66
+ val color = obj.optString("color", "#000000")
67
+
68
+ return AnnotationModel(
69
+ id = id,
70
+ type = type,
71
+ page = page,
72
+ color = color,
73
+ lineWidth = optFloat(obj, "lineWidth"),
74
+ alpha = optFloat(obj, "alpha"),
75
+ paths = parsePaths(obj.optJSONArray("paths")),
76
+ bounds = parseBounds(obj.optJSONObject("bounds")),
77
+ contents = if (obj.has("contents")) obj.getString("contents") else null,
78
+ fontSize = optFloat(obj, "fontSize"),
79
+ createdAt = if (obj.has("createdAt")) obj.optDouble("createdAt") else null
80
+ )
81
+ }
82
+
83
+ private fun optFloat(obj: JSONObject, key: String): Float? =
84
+ if (obj.has(key)) obj.optDouble(key).toFloat() else null
85
+
86
+ private fun parsePaths(arr: JSONArray?): List<List<AnnotationPoint>>? {
87
+ arr ?: return null
88
+ val paths = mutableListOf<List<AnnotationPoint>>()
89
+ for (i in 0 until arr.length()) {
90
+ val stroke = arr.getJSONArray(i)
91
+ val points = mutableListOf<AnnotationPoint>()
92
+ for (j in 0 until stroke.length()) {
93
+ val pt = stroke.getJSONObject(j)
94
+ points.add(
95
+ AnnotationPoint(pt.getDouble("x").toFloat(), pt.getDouble("y").toFloat())
96
+ )
97
+ }
98
+ paths.add(points)
99
+ }
100
+ return paths
101
+ }
102
+
103
+ private fun parseBounds(obj: JSONObject?): AnnotationBounds? {
104
+ obj ?: return null
105
+ return AnnotationBounds(
106
+ x = obj.getDouble("x").toFloat(),
107
+ y = obj.getDouble("y").toFloat(),
108
+ width = obj.getDouble("width").toFloat(),
109
+ height = obj.getDouble("height").toFloat()
110
+ )
111
+ }
112
+
113
+ private fun annotationToJson(model: AnnotationModel): JSONObject {
114
+ val obj = JSONObject()
115
+ obj.put("id", model.id)
116
+ obj.put("type", model.type)
117
+ obj.put("page", model.page)
118
+ obj.put("color", model.color)
119
+ model.lineWidth?.let { obj.put("lineWidth", it.toDouble()) }
120
+ model.alpha?.let { obj.put("alpha", it.toDouble()) }
121
+ model.paths?.let { obj.put("paths", pathsToJson(it)) }
122
+ model.bounds?.let { obj.put("bounds", boundsToJson(it)) }
123
+ model.contents?.let { obj.put("contents", it) }
124
+ model.fontSize?.let { obj.put("fontSize", it.toDouble()) }
125
+ model.createdAt?.let { obj.put("createdAt", it) }
126
+ return obj
127
+ }
128
+
129
+ private fun pathsToJson(paths: List<List<AnnotationPoint>>): JSONArray {
130
+ val arr = JSONArray()
131
+ for (stroke in paths) {
132
+ val strokeArr = JSONArray()
133
+ for (pt in stroke) {
134
+ val ptObj = JSONObject()
135
+ ptObj.put("x", pt.x.toDouble())
136
+ ptObj.put("y", pt.y.toDouble())
137
+ strokeArr.put(ptObj)
138
+ }
139
+ arr.put(strokeArr)
140
+ }
141
+ return arr
142
+ }
143
+
144
+ private fun boundsToJson(bounds: AnnotationBounds): JSONObject {
145
+ val obj = JSONObject()
146
+ obj.put("x", bounds.x.toDouble())
147
+ obj.put("y", bounds.y.toDouble())
148
+ obj.put("width", bounds.width.toDouble())
149
+ obj.put("height", bounds.height.toDouble())
150
+ return obj
151
+ }
152
+ }