Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / apple / Notes / PencilKitMapping.swift

PencilKitMapping.swift76 lines · 3.8 KiBRaw
1// EverySync Notes — PencilKit <-> shared ink-model mapping.
2//
3// PencilKit has no shared counterpart, so the stroke converter lives here,
4// but it maps 1:1 onto `InkPoint`/`InkStroke` and performs NO merge / hash /
5// crypto. Serialization of the mapped strokes (`serializeNoteBody`) stays in
6// the XCFramework. Canvas height maps to the ink block's `heightDp`
7// (default 300).
8//
9// Mapping (verbatim from `Notes/README.md`):
10// | `PKStrokePoint` | `InkPoint` |
11// | `location.x / .y` | `x / y` |
12// | `timeOffset + stroke start` | `timeMillis` |
13// | `force` (fallback `size`)| `pressure` |
14// | `pi/2 - altitude` | `tiltRadians` |
15// | `azimuth` | `orientationRadians` |
16// | `PKStroke` / `PKInk` | `InkStroke` |
17// | `ink.inkType` | `brushFamily` |
18// | `ink` line width | `brushSize` |
19// | `ink.color` -> argb int | `brushColor` |
20// | constant | `brushEpsilon = 0.1` |
21//
22// MAC-CHECK (needs xcodebuild on a Mac):
23// - `PKStrokePoint` property names (`location`, `timeOffset`, `size`,
24// `force`, `azimuth`, `altitude`) against the linked iOS SDK.
25// - `PKInk.InkType` -> `brushFamily` string mapping: confirm the exact raw
26// strings the Mac wants (`"pressurePen"` default; pen/pencil/marker map to
27// documented family names — keep the mapping table in ONE place, here).
28
29import PencilKit
30import UIKit
31import EverysyncCore
32
33/// Thin platform layer: PencilKit in, shared-model strokes out (and back for
34/// rendering stored ink onto the canvas). No logic beyond unit conversion.
35public enum PencilKitMapping {
36 /// Default when the ink type has no family mapping.
37 public static let defaultBrushFamily = "pressurePen"
38 public static let defaultBrushEpsilon: Float = 0.1
39
40 // TODO(Mac): implement against the generated XCFramework `InkStroke` /
41 // `InkPoint` Kotlin types:
42 //
43 // static func inkStroke(from stroke: PKStroke, startMillis: Int64) -> InkStroke:
44 // - brushFamily = familyName(for: stroke.ink.inkType) ?? defaultBrushFamily
45 // - brushSize = Float(stroke.ink.lineWidth(for: ...)) (line-width API
46 // varies by SDK — confirm on the Mac; store the rendered width)
47 // - brushColor = argb int of stroke.ink.color (see InkColorHelp)
48 // - brushEpsilon = 0.1 (constant — matches the shared default)
49 // - points = stroke.path.map { inkPoint(from: $0, startMillis:) }
50 //
51 // static func inkPoint(from point: PKStrokePoint, startMillis: Int64) -> InkPoint:
52 // - x = Float(point.location.x), y = Float(point.location.y)
53 // - timeMillis = startMillis + Int64((point.timeOffset * 1000).rounded())
54 // - pressure = Float(point.size.width > 0 ? point.force : fallback)
55 // (PencilKit reports force 0 for finger touches — fall back to 1.0 so
56 // strokes stay visible; the fallback is display-only, stored verbatim)
57 // - tiltRadians = Float(.pi / 2 - point.altitude)
58 // - orientationRadians = Float(point.azimuth)
59 //
60 // static func pkStroke(from ink: InkStroke) -> PKStroke (render path):
61 // - rebuild PKStrokePath from points; PKInk with the mapped type/width/color.
62 // - Lossy where PencilKit has no equivalent (epsilon, exact family) —
63 // acceptable: the stored InkStroke is source of truth, the PKStroke is
64 // a render projection only.
65
66 /// Maps a PencilKit ink type to the shared `brushFamily` string.
67 /// TODO(Mac): confirm exact family strings; keep `"pressurePen"` default.
68 public static func familyName(for inkType: PKInk.InkType) -> String {
69 switch inkType {
70 case .pen: return "pressurePen"
71 case .pencil: return "pencil"
72 case .marker: return "marker"
73 default: return defaultBrushFamily
74 }
75 }
76}