-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
113 lines (97 loc) · 2.19 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rescript
import (
"math"
"time"
"github.com/akeil/rmtool/pkg/lines"
)
const (
speedFactor int64 = 200 * 100
strokeGap = 500 * 100
minSpeed = 0.01
)
// ConvertLayer convert a Layer from a reMarkable drawing to a MyScript stroke group.
func ConvertLayer(tOffset int64, l lines.Layer) (StrokeGroup, int64) {
t := tOffset
strokes := make([]Stroke, len(l.Strokes))
i := 0
for _, s := range l.Strokes {
if isTextStroke(s.BrushType) {
stroke, tx := convertStroke(t, s)
strokes[i] = stroke
// add some millis to t for each new stroke
t = tx + strokeGap
i++
}
}
return StrokeGroup{
Strokes: strokes[:i],
PenStyle: defaultPenStyle,
}, t
}
func convertStroke(tOffset int64, s lines.Stroke) (Stroke, int64) {
size := len(s.Dots)
x := make([]int, size)
y := make([]int, size)
ts := make([]int64, size)
p := make([]float64, size)
ms := tOffset
x0 := -1
y0 := -1
i := 0
for _, dot := range s.Dots {
x1 := int(math.Round(float64(dot.X)))
y1 := int(math.Round(float64(dot.Y)))
// avoid duplicate points
if x0 != x1 || y0 != y1 {
s := math.Max(minSpeed, float64(dot.Speed))
offset := float64(speedFactor) / s
ms += int64(math.Round(offset))
x[i] = int(math.Round(float64(dot.X)))
y[i] = int(math.Round(float64(dot.Y)))
ts[i] = ms
p[i] = coercePressure(dot.Pressure)
i++
}
x0 = x1
y0 = y1
}
return Stroke{
PointerType: lookupPointer(s.BrushType),
PointerID: singlePointerID,
X: x[:i],
Y: y[:i],
Timestamp: ts[:i],
Pressure: p[:i],
}, ms
}
func coercePressure(p float32) float64 {
return math.Max(0.0, math.Min(1.0, float64(p)))
}
func toMillis(t time.Time) int64 {
nanos := t.UnixNano()
return nanos / 1000000
}
func fromMillis(n int64) time.Time {
secs := int64(n / 1000)
nanos := (int64(n) - (secs * 1000)) * 1000000
return time.Unix(secs, nanos)
}
func isTextStroke(bt lines.BrushType) bool {
switch bt {
case lines.Eraser,
lines.EraseArea,
lines.Highlighter,
lines.HighlighterV5:
return false
default:
return true
}
}
func lookupPointer(bt lines.BrushType) PointerType {
switch bt {
case lines.Eraser, lines.EraseArea:
return Eraser
default:
return Pen
}
}