@tobyt/expo-pdf-markup 0.4.0 โ 0.6.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 +22 -0
- package/README.md +12 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +49 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +20 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +52 -6
- package/build/ExpoPdfMarkup.types.d.ts +29 -1
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.d.ts +1 -1
- package/build/ExpoPdfMarkupView.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.js +15 -3
- package/build/ExpoPdfMarkupView.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +131 -43
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/pdfjs.d.ts.map +1 -1
- package/build/pdfjs.js +0 -1
- package/build/pdfjs.js.map +1 -1
- package/build/web/annotationUtils.d.ts +9 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +88 -5
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +185 -0
- package/ios/AnnotationSerializer.swift +39 -31
- package/ios/ExpoPdfMarkupModule.swift +16 -1
- package/ios/ExpoPdfMarkupView.swift +287 -74
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +37 -1
- package/src/ExpoPdfMarkupView.tsx +38 -5
- package/src/ExpoPdfMarkupView.web.tsx +170 -41
- package/src/pdfjs.ts +0 -1
- package/src/web/annotationUtils.ts +121 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @tobyt/expo-pdf-markup
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- fc961ab: Add support for a custom font for text markups
|
|
8
|
+
- 26bdae1: When in Move or Eraser mode, annotations are now surrounded by an outline to indicate the scope of the move/erasure
|
|
9
|
+
- 26bdae1: A Move tool was added for drag and drop repositioning of annotations
|
|
10
|
+
- 76f9b23: Text annotations can now be edited (breaking change, see example/docs)
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- dae52fb: Reset currentSource in close
|
|
15
|
+
- 8146a57: Add padding to Android freeText annotation bounds to prevent text from being clipped when rendered
|
|
16
|
+
|
|
17
|
+
## 0.5.0
|
|
18
|
+
|
|
19
|
+
### Minor Changes
|
|
20
|
+
|
|
21
|
+
- 049130a: Example app is now deployed to Github Pages
|
|
22
|
+
- 049130a: Default pdfjs worker source for web implementation is now relative
|
|
23
|
+
- e62d562: Support optionally rendering text input prompt on JavaScript side
|
|
24
|
+
|
|
3
25
|
## 0.4.0
|
|
4
26
|
|
|
5
27
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
An Expo module for displaying and annotating PDFs, with support for iOS, Android, and Web.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
**[๐ API Reference](https://tobyt42.github.io/expo-pdf-markup/api/)** ยท **[๐ Example App](https://tobyt42.github.io/expo-pdf-markup/example/)**
|
|
11
|
+
|
|
12
|
+
> **Status:** Early development โ actively tested in the [Choir](https://www.thechoirapp.com) app.
|
|
11
13
|
|
|
12
14
|
## Platform support
|
|
13
15
|
|
|
@@ -50,7 +52,7 @@ module.exports = withPdfMarkup(config);
|
|
|
50
52
|
`withPdfMarkup` does two things automatically:
|
|
51
53
|
|
|
52
54
|
1. **Patches `import.meta`** in pdfjs-dist so Metro can bundle it (pdfjs-dist v4 uses ESM syntax in a Node.js-only code path that is otherwise unreachable in a browser).
|
|
53
|
-
2. **Copies the pdfjs worker** to `public/pdf.worker.min.mjs` in your project root so it is served
|
|
55
|
+
2. **Copies the pdfjs worker** to `public/pdf.worker.min.mjs` in your project root so it is served alongside your app (same-origin, no CORS issues). The default worker URL is `./pdf.worker.min.mjs` (relative to the page), so it works whether your app is hosted at the root or a sub-path.
|
|
54
56
|
|
|
55
57
|
The `public/pdf.worker.min.mjs` file is regenerated on each Metro start if missing, so you can add it to `.gitignore`:
|
|
56
58
|
|
|
@@ -79,7 +81,7 @@ setPdfPath(asset.localUri.replace('file://', ''));
|
|
|
79
81
|
|
|
80
82
|
## Usage
|
|
81
83
|
|
|
82
|
-
Full API reference is available at **[tobyt42.github.io/expo-pdf-markup](https://tobyt42.github.io/expo-pdf-markup/types/ExpoPdfMarkupViewProps.html)**.
|
|
84
|
+
Full API reference is available at **[tobyt42.github.io/expo-pdf-markup](https://tobyt42.github.io/expo-pdf-markup/api/types/ExpoPdfMarkupViewProps.html)**.
|
|
83
85
|
|
|
84
86
|
```tsx
|
|
85
87
|
import { ExpoPdfMarkupView } from '@tobyt/expo-pdf-markup';
|
|
@@ -122,6 +124,13 @@ export default function App() {
|
|
|
122
124
|
}
|
|
123
125
|
```
|
|
124
126
|
|
|
127
|
+
### Custom text input UI
|
|
128
|
+
|
|
129
|
+
If you provide `onTextInputRequested`, the built-in native prompt is skipped and your callback is
|
|
130
|
+
used instead. The callback receives a request object with `mode`, `page`, `point`, and
|
|
131
|
+
`currentText` for edit flows, so you can prefill your own UI when the user taps an existing text
|
|
132
|
+
annotation while the text tool is active.
|
|
133
|
+
|
|
125
134
|
## Development
|
|
126
135
|
|
|
127
136
|
This module uses the [Expo Modules API](https://docs.expo.dev/modules/overview/). The `example` directory contains a test app.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
package dev.terhoeven.expopdfmarkup
|
|
2
|
+
|
|
3
|
+
object AnnotationGeometry {
|
|
4
|
+
|
|
5
|
+
fun outlineBounds(annotation: AnnotationModel): AnnotationBounds? = when (annotation.type) {
|
|
6
|
+
"ink" -> outlineBoundsForInk(annotation)
|
|
7
|
+
"highlight", "underline", "freeText", "text" -> annotation.bounds
|
|
8
|
+
else -> null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fun clampTranslation(
|
|
12
|
+
annotation: AnnotationModel,
|
|
13
|
+
deltaX: Float,
|
|
14
|
+
deltaY: Float,
|
|
15
|
+
pageWidth: Float,
|
|
16
|
+
pageHeight: Float
|
|
17
|
+
): AnnotationPoint {
|
|
18
|
+
val bounds = outlineBounds(annotation) ?: return AnnotationPoint(deltaX, deltaY)
|
|
19
|
+
val minDx = -bounds.x
|
|
20
|
+
val maxDx = pageWidth - (bounds.x + bounds.width)
|
|
21
|
+
val minDy = -bounds.y
|
|
22
|
+
val maxDy = pageHeight - (bounds.y + bounds.height)
|
|
23
|
+
return AnnotationPoint(
|
|
24
|
+
x = deltaX.coerceIn(minDx, maxDx),
|
|
25
|
+
y = deltaY.coerceIn(minDy, maxDy)
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fun translate(annotation: AnnotationModel, deltaX: Float, deltaY: Float): AnnotationModel {
|
|
30
|
+
if (deltaX == 0f && deltaY == 0f) return annotation
|
|
31
|
+
return when (annotation.type) {
|
|
32
|
+
"ink" ->
|
|
33
|
+
annotation.copy(
|
|
34
|
+
paths =
|
|
35
|
+
annotation.paths?.map { stroke ->
|
|
36
|
+
stroke.map { point ->
|
|
37
|
+
AnnotationPoint(point.x + deltaX, point.y + deltaY)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
"highlight", "underline", "freeText", "text" -> {
|
|
43
|
+
val bounds = annotation.bounds ?: return annotation
|
|
44
|
+
annotation.copy(
|
|
45
|
+
bounds =
|
|
46
|
+
AnnotationBounds(
|
|
47
|
+
x = bounds.x + deltaX,
|
|
48
|
+
y = bounds.y + deltaY,
|
|
49
|
+
width = bounds.width,
|
|
50
|
+
height = bounds.height
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
else -> annotation
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun outlineBoundsForInk(annotation: AnnotationModel): AnnotationBounds? {
|
|
60
|
+
val paths = annotation.paths ?: return null
|
|
61
|
+
var minX = Float.POSITIVE_INFINITY
|
|
62
|
+
var minY = Float.POSITIVE_INFINITY
|
|
63
|
+
var maxX = Float.NEGATIVE_INFINITY
|
|
64
|
+
var maxY = Float.NEGATIVE_INFINITY
|
|
65
|
+
|
|
66
|
+
for (stroke in paths) {
|
|
67
|
+
for (point in stroke) {
|
|
68
|
+
minX = minOf(minX, point.x)
|
|
69
|
+
minY = minOf(minY, point.y)
|
|
70
|
+
maxX = maxOf(maxX, point.x)
|
|
71
|
+
maxY = maxOf(maxY, point.y)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!minX.isFinite() || !minY.isFinite()) return null
|
|
76
|
+
|
|
77
|
+
val padding = maxOf(annotation.lineWidth ?: 2f, 10f)
|
|
78
|
+
return AnnotationBounds(
|
|
79
|
+
x = minX - padding,
|
|
80
|
+
y = minY - padding,
|
|
81
|
+
width = maxX - minX + padding * 2,
|
|
82
|
+
height = maxY - minY + padding * 2
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
package dev.terhoeven.expopdfmarkup
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
3
4
|
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.DashPathEffect
|
|
4
6
|
import android.graphics.Paint
|
|
5
7
|
import android.graphics.Path
|
|
8
|
+
import android.graphics.Typeface
|
|
6
9
|
|
|
7
10
|
object AnnotationRenderer {
|
|
8
11
|
|
|
@@ -12,7 +15,8 @@ object AnnotationRenderer {
|
|
|
12
15
|
pageIndex: Int,
|
|
13
16
|
pageYOffset: Float,
|
|
14
17
|
renderScale: Float,
|
|
15
|
-
pageHeight: Int
|
|
18
|
+
pageHeight: Int,
|
|
19
|
+
context: Context
|
|
16
20
|
) {
|
|
17
21
|
for (annotation in annotations) {
|
|
18
22
|
if (annotation.page != pageIndex) continue
|
|
@@ -40,12 +44,41 @@ object AnnotationRenderer {
|
|
|
40
44
|
annotation,
|
|
41
45
|
pageYOffset,
|
|
42
46
|
renderScale,
|
|
43
|
-
pageHeight
|
|
47
|
+
pageHeight,
|
|
48
|
+
context
|
|
44
49
|
)
|
|
45
50
|
}
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
|
|
54
|
+
fun drawEditingAffordances(
|
|
55
|
+
canvas: Canvas,
|
|
56
|
+
annotations: List<AnnotationModel>,
|
|
57
|
+
pageIndex: Int,
|
|
58
|
+
pageYOffset: Float,
|
|
59
|
+
renderScale: Float,
|
|
60
|
+
pageHeight: Int,
|
|
61
|
+
currentScale: Float
|
|
62
|
+
) {
|
|
63
|
+
val paint = Paint().apply {
|
|
64
|
+
color = 0xFF0A84FF.toInt()
|
|
65
|
+
strokeWidth = 2f / currentScale
|
|
66
|
+
style = Paint.Style.STROKE
|
|
67
|
+
isAntiAlias = true
|
|
68
|
+
pathEffect = DashPathEffect(floatArrayOf(10f / currentScale, 6f / currentScale), 0f)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (annotation in annotations) {
|
|
72
|
+
if (annotation.page != pageIndex) continue
|
|
73
|
+
val bounds = AnnotationGeometry.outlineBounds(annotation) ?: continue
|
|
74
|
+
val left = bounds.x * renderScale
|
|
75
|
+
val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
|
|
76
|
+
val right = left + bounds.width * renderScale
|
|
77
|
+
val bottom = top + bounds.height * renderScale
|
|
78
|
+
canvas.drawRect(left, top, right, bottom, paint)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
49
82
|
private fun drawInk(
|
|
50
83
|
canvas: Canvas,
|
|
51
84
|
annotation: AnnotationModel,
|
|
@@ -122,17 +155,30 @@ object AnnotationRenderer {
|
|
|
122
155
|
annotation: AnnotationModel,
|
|
123
156
|
pageYOffset: Float,
|
|
124
157
|
renderScale: Float,
|
|
125
|
-
pageHeight: Int
|
|
158
|
+
pageHeight: Int,
|
|
159
|
+
context: Context
|
|
126
160
|
) {
|
|
127
161
|
val bounds = annotation.bounds ?: return
|
|
128
162
|
val text = annotation.contents ?: return
|
|
129
163
|
val paint = Paint().apply {
|
|
130
164
|
color = AnnotationSerializer.colorFromHex(annotation.color)
|
|
131
165
|
textSize = (annotation.fontSize ?: 16f) * renderScale
|
|
166
|
+
typeface = resolveTypeface(context, annotation.fontFamily)
|
|
132
167
|
isAntiAlias = true
|
|
133
168
|
}
|
|
134
169
|
val left = bounds.x * renderScale
|
|
135
170
|
val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
|
|
136
171
|
canvas.drawText(text, left, top + paint.textSize, paint)
|
|
137
172
|
}
|
|
173
|
+
|
|
174
|
+
/** Resolve a typeface by family name. Tries app assets first (where expo-font places fonts),
|
|
175
|
+
* then falls back to system typeface resolution. */
|
|
176
|
+
fun resolveTypeface(context: Context, fontFamily: String?): Typeface {
|
|
177
|
+
if (fontFamily == null) return Typeface.DEFAULT
|
|
178
|
+
return try {
|
|
179
|
+
Typeface.createFromAsset(context.assets, "fonts/$fontFamily.ttf")
|
|
180
|
+
} catch (_: Exception) {
|
|
181
|
+
Typeface.create(fontFamily, Typeface.NORMAL)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
138
184
|
}
|
|
@@ -76,6 +76,7 @@ object AnnotationSerializer {
|
|
|
76
76
|
bounds = parseBounds(obj.optJSONObject("bounds")),
|
|
77
77
|
contents = if (obj.has("contents")) obj.getString("contents") else null,
|
|
78
78
|
fontSize = optFloat(obj, "fontSize"),
|
|
79
|
+
fontFamily = if (obj.has("fontFamily")) obj.getString("fontFamily") else null,
|
|
79
80
|
createdAt = if (obj.has("createdAt")) obj.optDouble("createdAt") else null
|
|
80
81
|
)
|
|
81
82
|
}
|
|
@@ -122,6 +123,7 @@ object AnnotationSerializer {
|
|
|
122
123
|
model.bounds?.let { obj.put("bounds", boundsToJson(it)) }
|
|
123
124
|
model.contents?.let { obj.put("contents", it) }
|
|
124
125
|
model.fontSize?.let { obj.put("fontSize", it.toDouble()) }
|
|
126
|
+
model.fontFamily?.let { obj.put("fontFamily", it) }
|
|
125
127
|
model.createdAt?.let { obj.put("createdAt", it) }
|
|
126
128
|
return obj
|
|
127
129
|
}
|
|
@@ -49,8 +49,27 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
49
49
|
invalidate()
|
|
50
50
|
}
|
|
51
51
|
var annotationMode: String = "none"
|
|
52
|
+
set(value) {
|
|
53
|
+
if (field == value) return
|
|
54
|
+
field = value
|
|
55
|
+
if (value != "ink") {
|
|
56
|
+
isDrawingInk = false
|
|
57
|
+
currentInkPoints.clear()
|
|
58
|
+
inkScreenPath.reset()
|
|
59
|
+
}
|
|
60
|
+
if (value != "highlight" && value != "underline") {
|
|
61
|
+
isDragging = false
|
|
62
|
+
dragStartPoint = null
|
|
63
|
+
dragCurrentPoint = null
|
|
64
|
+
}
|
|
65
|
+
if (value != "move") {
|
|
66
|
+
clearMovePreview()
|
|
67
|
+
}
|
|
68
|
+
invalidate()
|
|
69
|
+
}
|
|
52
70
|
var annotationColor: String = "#000000"
|
|
53
71
|
var annotationLineWidth: Float = 2f
|
|
72
|
+
var annotationFontFamily: String? = null
|
|
54
73
|
var onAnnotationsChangedListener: (() -> Unit)? = null
|
|
55
74
|
|
|
56
75
|
// Ink drawing state
|
|
@@ -63,8 +82,15 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
63
82
|
private var dragCurrentPoint: AnnotationPoint? = null
|
|
64
83
|
private var isDragging = false
|
|
65
84
|
|
|
85
|
+
// Move mode state
|
|
86
|
+
private var movingAnnotation: AnnotationModel? = null
|
|
87
|
+
private var movingPreviewAnnotation: AnnotationModel? = null
|
|
88
|
+
private var moveStartPdfPoint: AnnotationPoint? = null
|
|
89
|
+
|
|
66
90
|
// Text mode callback
|
|
67
|
-
var onTextInputRequested: (
|
|
91
|
+
var onTextInputRequested: (
|
|
92
|
+
(page: Int, point: AnnotationPoint, annotation: AnnotationModel?) -> Unit
|
|
93
|
+
)? = null
|
|
68
94
|
|
|
69
95
|
fun loadPages(renderer: PdfRenderer, viewWidth: Int) {
|
|
70
96
|
recycle()
|
|
@@ -113,6 +139,21 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
113
139
|
super.onDraw(canvas)
|
|
114
140
|
if (pageBitmaps.isEmpty()) return
|
|
115
141
|
|
|
142
|
+
val renderedAnnotations =
|
|
143
|
+
if (movingAnnotation != null && movingPreviewAnnotation != null) {
|
|
144
|
+
annotations.map { annotation ->
|
|
145
|
+
if (annotation.id ==
|
|
146
|
+
movingAnnotation!!.id
|
|
147
|
+
) {
|
|
148
|
+
movingPreviewAnnotation!!
|
|
149
|
+
} else {
|
|
150
|
+
annotation
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
annotations
|
|
155
|
+
}
|
|
156
|
+
|
|
116
157
|
canvas.save()
|
|
117
158
|
canvas.translate(-panX, -panY)
|
|
118
159
|
canvas.scale(currentScale, currentScale)
|
|
@@ -128,12 +169,24 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
128
169
|
canvas.drawBitmap(pageBitmaps[i], 0f, pageYOffsets[i], null)
|
|
129
170
|
AnnotationRenderer.drawAnnotations(
|
|
130
171
|
canvas,
|
|
131
|
-
|
|
172
|
+
renderedAnnotations,
|
|
132
173
|
i,
|
|
133
174
|
pageYOffsets[i],
|
|
134
175
|
renderScales[i],
|
|
135
|
-
pageHeights[i]
|
|
176
|
+
pageHeights[i],
|
|
177
|
+
context
|
|
136
178
|
)
|
|
179
|
+
if (annotationMode == "move" || annotationMode == "eraser") {
|
|
180
|
+
AnnotationRenderer.drawEditingAffordances(
|
|
181
|
+
canvas,
|
|
182
|
+
renderedAnnotations,
|
|
183
|
+
i,
|
|
184
|
+
pageYOffsets[i],
|
|
185
|
+
renderScales[i],
|
|
186
|
+
pageHeights[i],
|
|
187
|
+
currentScale
|
|
188
|
+
)
|
|
189
|
+
}
|
|
137
190
|
}
|
|
138
191
|
}
|
|
139
192
|
|
|
@@ -205,6 +258,13 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
205
258
|
return true
|
|
206
259
|
}
|
|
207
260
|
|
|
261
|
+
"move" -> {
|
|
262
|
+
if (event.pointerCount == 1) {
|
|
263
|
+
handleMoveTouch(event)
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
208
268
|
"text" -> {
|
|
209
269
|
if (event.action == MotionEvent.ACTION_UP && event.pointerCount == 1) {
|
|
210
270
|
handleTextTap(event)
|
|
@@ -369,57 +429,132 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
369
429
|
// --- Eraser mode ---
|
|
370
430
|
|
|
371
431
|
private fun handleEraserTap(event: MotionEvent) {
|
|
372
|
-
val
|
|
373
|
-
val
|
|
374
|
-
val pageIndex = pageIndexAtContentY(contentY)
|
|
375
|
-
if (pageIndex < 0) return
|
|
376
|
-
|
|
377
|
-
val localY = contentY - pageYOffsets[pageIndex]
|
|
378
|
-
val pdfX = contentX / renderScales[pageIndex]
|
|
379
|
-
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
380
|
-
|
|
381
|
-
val hit = AnnotationHitTester.hitTest(
|
|
382
|
-
AnnotationPoint(pdfX, pdfY),
|
|
383
|
-
annotations,
|
|
384
|
-
pageIndex
|
|
385
|
-
)
|
|
432
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
433
|
+
val hit = AnnotationHitTester.hitTest(pdfTouch.point, annotations, pdfTouch.pageIndex)
|
|
386
434
|
if (hit != null) {
|
|
387
435
|
annotations = annotations.filter { it.id != hit.id }
|
|
388
436
|
onAnnotationsChangedListener?.invoke()
|
|
389
437
|
}
|
|
390
438
|
}
|
|
391
439
|
|
|
440
|
+
private fun handleMoveTouch(event: MotionEvent) {
|
|
441
|
+
when (event.action) {
|
|
442
|
+
MotionEvent.ACTION_DOWN -> {
|
|
443
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
444
|
+
val hit = AnnotationHitTester.hitTest(
|
|
445
|
+
pdfTouch.point,
|
|
446
|
+
annotations,
|
|
447
|
+
pdfTouch.pageIndex
|
|
448
|
+
)
|
|
449
|
+
if (hit != null) {
|
|
450
|
+
movingAnnotation = hit
|
|
451
|
+
movingPreviewAnnotation = hit
|
|
452
|
+
moveStartPdfPoint = pdfTouch.point
|
|
453
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
454
|
+
invalidate()
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
MotionEvent.ACTION_MOVE -> {
|
|
459
|
+
val annotation = movingAnnotation ?: return
|
|
460
|
+
val startPoint = moveStartPdfPoint ?: return
|
|
461
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y, annotation.page) ?: return
|
|
462
|
+
val delta = AnnotationGeometry.clampTranslation(
|
|
463
|
+
annotation,
|
|
464
|
+
pdfTouch.point.x - startPoint.x,
|
|
465
|
+
pdfTouch.point.y - startPoint.y,
|
|
466
|
+
pageWidths[annotation.page].toFloat(),
|
|
467
|
+
pageHeights[annotation.page].toFloat()
|
|
468
|
+
)
|
|
469
|
+
movingPreviewAnnotation = AnnotationGeometry.translate(annotation, delta.x, delta.y)
|
|
470
|
+
invalidate()
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
|
474
|
+
val annotation = movingAnnotation
|
|
475
|
+
val preview = movingPreviewAnnotation
|
|
476
|
+
if (annotation != null && preview != null && preview != annotation) {
|
|
477
|
+
annotations = annotations.map { existing ->
|
|
478
|
+
if (existing.id == annotation.id) preview else existing
|
|
479
|
+
}
|
|
480
|
+
onAnnotationsChangedListener?.invoke()
|
|
481
|
+
}
|
|
482
|
+
clearMovePreview()
|
|
483
|
+
invalidate()
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
392
488
|
// --- Text mode ---
|
|
393
489
|
|
|
394
490
|
private fun handleTextTap(event: MotionEvent) {
|
|
395
|
-
val
|
|
396
|
-
val
|
|
397
|
-
val
|
|
398
|
-
if (
|
|
491
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
492
|
+
val point = pdfTouch.point
|
|
493
|
+
val hit = AnnotationHitTester.hitTest(point, annotations, pdfTouch.pageIndex)
|
|
494
|
+
val annotation = if (hit?.type == "text" || hit?.type == "freeText") hit else null
|
|
495
|
+
onTextInputRequested?.invoke(pdfTouch.pageIndex, point, annotation)
|
|
496
|
+
}
|
|
399
497
|
|
|
400
|
-
|
|
401
|
-
val
|
|
402
|
-
|
|
498
|
+
fun addTextAnnotation(page: Int, point: AnnotationPoint, text: String) {
|
|
499
|
+
val annotation = buildTextAnnotation(text = text, page = page, point = point)
|
|
500
|
+
annotations = annotations + annotation
|
|
501
|
+
onAnnotationsChangedListener?.invoke()
|
|
502
|
+
}
|
|
403
503
|
|
|
404
|
-
|
|
504
|
+
fun updateTextAnnotation(annotationId: String, text: String) {
|
|
505
|
+
val existing = annotations.find { it.id == annotationId } ?: return
|
|
506
|
+
val updated = buildTextAnnotation(text = text, page = existing.page, existing = existing)
|
|
507
|
+
annotations = annotations.map { annotation ->
|
|
508
|
+
if (annotation.id == annotationId) updated else annotation
|
|
509
|
+
}
|
|
510
|
+
onAnnotationsChangedListener?.invoke()
|
|
405
511
|
}
|
|
406
512
|
|
|
407
|
-
fun
|
|
408
|
-
|
|
409
|
-
|
|
513
|
+
private fun buildTextAnnotation(
|
|
514
|
+
text: String,
|
|
515
|
+
page: Int,
|
|
516
|
+
point: AnnotationPoint? = null,
|
|
517
|
+
existing: AnnotationModel? = null
|
|
518
|
+
): AnnotationModel {
|
|
519
|
+
val fontSize = existing?.fontSize ?: 16f
|
|
520
|
+
val padding = 4f
|
|
521
|
+
val paint = Paint().apply {
|
|
522
|
+
textSize = fontSize
|
|
523
|
+
typeface = AnnotationRenderer.resolveTypeface(
|
|
524
|
+
context,
|
|
525
|
+
existing?.fontFamily ?: annotationFontFamily
|
|
526
|
+
)
|
|
527
|
+
}
|
|
410
528
|
val textWidth = paint.measureText(text)
|
|
411
|
-
val
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
529
|
+
val height = fontSize * 1.2f + padding * 2
|
|
530
|
+
val bounds = if (existing != null && existing.bounds != null) {
|
|
531
|
+
AnnotationBounds(
|
|
532
|
+
existing.bounds.x,
|
|
533
|
+
existing.bounds.y + existing.bounds.height - height,
|
|
534
|
+
textWidth + padding * 2,
|
|
535
|
+
height
|
|
536
|
+
)
|
|
537
|
+
} else {
|
|
538
|
+
val anchorPoint = requireNotNull(point)
|
|
539
|
+
AnnotationBounds(
|
|
540
|
+
anchorPoint.x,
|
|
541
|
+
anchorPoint.y - fontSize - padding,
|
|
542
|
+
textWidth + padding * 2,
|
|
543
|
+
height
|
|
544
|
+
)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return AnnotationModel(
|
|
548
|
+
id = existing?.id ?: UUID.randomUUID().toString(),
|
|
549
|
+
type = existing?.type ?: "freeText",
|
|
550
|
+
page = existing?.page ?: page,
|
|
551
|
+
color = existing?.color ?: annotationColor,
|
|
552
|
+
bounds = bounds,
|
|
417
553
|
contents = text,
|
|
418
554
|
fontSize = fontSize,
|
|
419
|
-
|
|
555
|
+
fontFamily = existing?.fontFamily ?: annotationFontFamily,
|
|
556
|
+
createdAt = existing?.createdAt ?: System.currentTimeMillis() / 1000.0
|
|
420
557
|
)
|
|
421
|
-
annotations = annotations + annotation
|
|
422
|
-
onAnnotationsChangedListener?.invoke()
|
|
423
558
|
}
|
|
424
559
|
|
|
425
560
|
// --- Helpers ---
|
|
@@ -431,6 +566,30 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
431
566
|
return 0
|
|
432
567
|
}
|
|
433
568
|
|
|
569
|
+
private data class PdfTouchPoint(val pageIndex: Int, val point: AnnotationPoint)
|
|
570
|
+
|
|
571
|
+
private fun clearMovePreview() {
|
|
572
|
+
movingAnnotation = null
|
|
573
|
+
movingPreviewAnnotation = null
|
|
574
|
+
moveStartPdfPoint = null
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private fun screenToPdfPoint(
|
|
578
|
+
screenX: Float,
|
|
579
|
+
screenY: Float,
|
|
580
|
+
forcedPageIndex: Int? = null
|
|
581
|
+
): PdfTouchPoint? {
|
|
582
|
+
val contentX = (screenX + panX) / currentScale
|
|
583
|
+
val contentY = (screenY + panY) / currentScale
|
|
584
|
+
val pageIndex = forcedPageIndex ?: pageIndexAtContentY(contentY)
|
|
585
|
+
if (pageIndex < 0 || pageIndex >= pageYOffsets.size) return null
|
|
586
|
+
|
|
587
|
+
val localY = contentY - pageYOffsets[pageIndex]
|
|
588
|
+
val pdfX = contentX / renderScales[pageIndex]
|
|
589
|
+
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
590
|
+
return PdfTouchPoint(pageIndex, AnnotationPoint(pdfX, pdfY))
|
|
591
|
+
}
|
|
592
|
+
|
|
434
593
|
override fun computeScroll() {
|
|
435
594
|
if (scroller.computeScrollOffset()) {
|
|
436
595
|
panX = scroller.currX.toFloat()
|
|
@@ -481,6 +640,13 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
481
640
|
pageWidths.clear()
|
|
482
641
|
pageHeights.clear()
|
|
483
642
|
renderScales.clear()
|
|
643
|
+
currentInkPoints.clear()
|
|
644
|
+
inkScreenPath.reset()
|
|
645
|
+
dragStartPoint = null
|
|
646
|
+
dragCurrentPoint = null
|
|
647
|
+
isDragging = false
|
|
648
|
+
isDrawingInk = false
|
|
649
|
+
clearMovePreview()
|
|
484
650
|
totalHeight = 0f
|
|
485
651
|
lastNotifiedPage = -1
|
|
486
652
|
}
|
|
@@ -46,6 +46,11 @@ class ExpoPdfMarkupModule : Module() {
|
|
|
46
46
|
override fun definition() = ModuleDefinition {
|
|
47
47
|
Name("ExpoPdfMarkup")
|
|
48
48
|
|
|
49
|
+
AsyncFunction("provideTextInput") { viewTag: Int, text: String? ->
|
|
50
|
+
val view = appContext.findView<ExpoPdfMarkupView>(viewTag)
|
|
51
|
+
view?.provideTextInput(text)
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
View(ExpoPdfMarkupView::class) {
|
|
50
55
|
Prop("source") { view: ExpoPdfMarkupView, source: String ->
|
|
51
56
|
view.loadPdf(source)
|
|
@@ -75,7 +80,21 @@ class ExpoPdfMarkupModule : Module() {
|
|
|
75
80
|
view.setAnnotationLineWidth(width)
|
|
76
81
|
}
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
Prop("annotationFontFamily") { view: ExpoPdfMarkupView, font: String? ->
|
|
84
|
+
view.setAnnotationFontFamily(font)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
Prop("useJsTextDialog") { view: ExpoPdfMarkupView, use: Boolean? ->
|
|
88
|
+
view.useJsTextDialog = use ?: false
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Events(
|
|
92
|
+
"onPageChanged",
|
|
93
|
+
"onLoadComplete",
|
|
94
|
+
"onError",
|
|
95
|
+
"onAnnotationsChanged",
|
|
96
|
+
"onTextInputRequested"
|
|
97
|
+
)
|
|
79
98
|
}
|
|
80
99
|
}
|
|
81
100
|
}
|