Repository Public
1// SP Notes iOS2// File: Tests/InkConversionTests.swift3// Description: `PKDrawing` ⇄ `spn-ink-1`: tool, colour and width mapping, the round trip PencilKit → ink → PencilKit → ink within the4// stated tolerances (0.75 pt on positions, 0.05 on pressures, 1 pt on bounds), strokes cut by the bitmap eraser, the5// canvas placement, and a document written by the web editor (fixture produced by the web serializer).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import PencilKit10import Testing11import UIKit12import SPNotesCore13@testable import SPNotes1415private final class InkBundleMarker {}1617/// Geometry helpers shared by the ink tests.18enum InkMetrics {19 /// Distance from a point to a polyline, and the pressure interpolated at the nearest position.20 static func nearest(_ p: InkPoint, on line: [InkPoint]) -> (distance: Double, pressure: Double) {21 guard line.count > 1 else { return line.first.map { (hypot(p.x - $0.x, p.y - $0.y), $0.pressure) } ?? (.infinity, 0) }22 var best = (Double.infinity, 0.0)23 for i in 1..<line.count {24 let a = line[i - 1], b = line[i]25 let dx = b.x - a.x, dy = b.y - a.y, len2 = dx * dx + dy * dy26 let t = len2 > 0 ? min(1, max(0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)) : 027 let d = hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy))28 if d < best.0 { best = (d, a.pressure + (b.pressure - a.pressure) * t) }29 }30 return best31 }3233 /// Largest distance between two polylines (both directions) and largest pressure difference at matching positions.34 static func deviation(_ a: InkStroke, _ b: InkStroke) -> (position: Double, pressure: Double) {35 var position = 0.0, pressure = 0.036 for (from, to) in [(a.points, b.points), (b.points, a.points)] {37 for p in from {38 let n = nearest(p, on: to)39 position = max(position, n.distance)40 pressure = max(pressure, abs(n.pressure - p.pressure))41 }42 }43 return (position, pressure)44 }45}4647@MainActor48enum InkSamples {49 static func point(_ x: CGFloat, _ y: CGFloat, size: CGFloat = 4, force: CGFloat = 1, index: Int = 0) -> PKStrokePoint {50 PKStrokePoint(location: CGPoint(x: x, y: y), timeOffset: Double(index) * 0.008, size: CGSize(width: size, height: size), opacity: 1, force: force, azimuth: 0, altitude: .pi / 2)51 }5253 static func stroke(_ type: PKInk.InkType, color: UIColor, count: Int = 60, size: (Double) -> CGFloat = { _ in 4 }, _ curve: (Double) -> CGPoint) -> PKStroke {54 let points = (0..<count).map { i -> PKStrokePoint in55 let t = Double(i) / Double(count - 1), p = curve(t)56 return point(p.x, p.y, size: size(t), force: CGFloat(t), index: i)57 }58 return PKStroke(ink: PKInk(type, color: color), path: PKStrokePath(controlPoints: points, creationDate: Date(timeIntervalSince1970: 1_700_000_000)))59 }6061 /// A drawing with every kind of stroke: pressure-varying pen, pencil, monoline, opaque marker, translucent marker (highlighter).62 static func drawing() -> PKDrawing {63 let pen = stroke(.pen, color: UIColor(red: 0.07, green: 0.09, blue: 0.15, alpha: 1), count: 70, size: { 2 + 5 * CGFloat(sin($0 * .pi)) }) {64 CGPoint(x: 60 + 380 * $0, y: 160 + 70 * sin($0 * 2 * .pi))65 }66 let pencil = stroke(.pencil, color: UIColor(red: 0.15, green: 0.39, blue: 0.92, alpha: 1), count: 80, size: { 3 + 2 * CGFloat($0) }) {67 CGPoint(x: 260 + 120 * cos($0 * 1.5 * .pi), y: 420 + 90 * sin($0 * 1.5 * .pi))68 }69 let mono = stroke(.monoline, color: .red, count: 30) { CGPoint(x: 500 + 200 * $0, y: 80 + 200 * $0 * $0) }70 let marker = stroke(.marker, color: UIColor(red: 0.86, green: 0.15, blue: 0.15, alpha: 1), count: 25, size: { _ in 12 }) { CGPoint(x: 80 + 300 * $0, y: 600 + 20 * sin($0 * 6)) }71 let highlighter = stroke(.marker, color: UIColor(red: 234 / 255, green: 179 / 255, blue: 8 / 255, alpha: 0.35), count: 20, size: { _ in 26 }) { CGPoint(x: 460 + 300 * $0, y: 520) }72 return PKDrawing(strokes: [pen, pencil, mono, marker, highlighter])73 }74}7576@MainActor77@Suite("Ink — conversion PencilKit ⇄ spn-ink-1") struct InkConversionTests {78 @Test func toolsColoursAndWidthsFollowTheDocumentedMapping() throws {79 let ink = InkConversion.inkDocument(from: InkSamples.drawing(), canvasSize: CGSize(width: 900, height: 700))80 #expect(ink.width == 900 && ink.height == 700)81 #expect(ink.strokes.map(\.tool) == [.pen, .pen, .pen, .marker, .highlighter])82 #expect(ink.strokes[0].color.hex == "#121726ff")83 #expect(ink.strokes[2].color.hex == "#ff0000ff")84 #expect(ink.strokes[4].color.hex == "#eab30859") // alpha 0.35 → 0x59, as the web highlighter85 // the width is the widest point of the stroke; pressures span 0…1 against it86 #expect(abs(ink.strokes[0].width - 7) < 0.05)87 #expect(ink.strokes[0].points.map(\.pressure).max() ?? 0 > 0.97)88 #expect(ink.strokes[0].points.first?.pressure ?? 1 < 0.05) // size 2 of 7 → below the 35 % floor → 089 #expect(ink.strokes[2].points.allSatisfy { $0.pressure == 1 }) // monoline90 #expect(ink.strokes[3].width == 12 && ink.strokes[4].width == 26)91 // the PencilKit ink is remembered in an unknown key, the JSON is valid spn-ink-1 that Core reads back identically92 #expect(ink.strokes[1][InkConversion.inkTypeField]?.stringValue == PKInk.InkType.pencil.rawValue)93 let reread = try InkDocument(data: ink.jsonData())94 #expect(reread == ink)95 #expect(String(decoding: ink.jsonData(), as: UTF8.self).contains("\"format\":\"spn-ink-1\""))96 // simplification keeps the files small: far fewer points than the 0.25-step sampling97 let sampled = InkSamples.drawing().strokes.reduce(0) { $0 + ($1.path.count - 1) * 4 + 1 }98 #expect(ink.pointCount < sampled / 2)99 }100101 @Test func roundTripStaysWithinTheStatedTolerances() throws {102 let first = InkSamples.drawing()103 let ink1 = InkConversion.inkDocument(from: first, canvasSize: CGSize(width: 900, height: 700))104 let second = InkConversion.drawing(from: ink1)105 let ink2 = InkConversion.inkDocument(from: second, canvasSize: CGSize(width: 900, height: 700))106 #expect(second.strokes.count == first.strokes.count)107 #expect(ink2.strokes.count == ink1.strokes.count)108 var worstPosition = 0.0, worstPressure = 0.0, worstWidth = 0.0, worstBounds = 0.0109 for (a, b) in zip(ink1.strokes, ink2.strokes) {110 #expect(a.tool == b.tool)111 #expect(a.color == b.color)112 #expect(a[InkConversion.inkTypeField] == b[InkConversion.inkTypeField])113 let d = InkMetrics.deviation(a, b)114 worstPosition = max(worstPosition, d.position)115 worstPressure = max(worstPressure, d.pressure)116 worstWidth = max(worstWidth, abs(a.width - b.width) / a.width)117 let ba = a.boundingBox, bb = b.boundingBox118 worstBounds = max(worstBounds, Double(max(abs(ba.minX - bb.minX), abs(ba.minY - bb.minY), abs(ba.maxX - bb.maxX), abs(ba.maxY - bb.maxY))))119 }120 // the PencilKit ink types survive without the pk file121 #expect(second.strokes.map(\.ink.inkType) == [.pen, .pencil, .monoline, .marker, .marker])122 // sampled geometry of the two PencilKit drawings123 var worstCurve = 0.0124 for (a, b) in zip(first.strokes, second.strokes) {125 let line = b.path.interpolatedPoints(by: .parametricStep(0.1)).map { InkPoint(x: $0.location.x, y: $0.location.y) }126 for p in a.path.interpolatedPoints(by: .parametricStep(0.1)) { worstCurve = max(worstCurve, InkMetrics.nearest(InkPoint(x: p.location.x, y: p.location.y), on: line).distance) }127 }128 let fb = first.bounds, sb = second.bounds129 let drawingBounds = Double(max(abs(fb.minX - sb.minX), abs(fb.minY - sb.minY), abs(fb.maxX - sb.maxX), abs(fb.maxY - sb.maxY)))130 let report = String(format: "ink1→ink2 position %.3f pt, pressure %.3f, width %.2f %%, ink bounds %.3f pt; PK1→PK2 curve %.3f pt, PKDrawing.bounds %.3f pt; points %d → %d\n",131 worstPosition, worstPressure, worstWidth * 100, worstBounds, worstCurve, drawingBounds, ink1.pointCount, ink2.pointCount)132 try? report.write(toFile: "/tmp/spnote-next/pencil-work/ink-roundtrip.txt", atomically: true, encoding: .utf8)133 #expect(worstPosition <= 0.75)134 #expect(worstCurve <= 0.75)135 #expect(worstPressure <= 0.05)136 #expect(worstWidth <= 0.03)137 #expect(worstBounds <= 1)138 #expect(drawingBounds <= 1)139 }140141 @Test func aMovedStrokeIsWrittenWhereItIsShown() {142 var stroke = InkSamples.stroke(.pen, color: .black, count: 10) { CGPoint(x: 100 * $0, y: 0) }143 stroke.transform = CGAffineTransform(translationX: 40, y: 30).scaledBy(x: 2, y: 2)144 let ink = InkConversion.inkStrokes(from: stroke)145 #expect(ink.count == 1)146 #expect(ink[0].points.first.map { abs($0.x - 40) < 0.01 && abs($0.y - 30) < 0.01 } == true)147 #expect(ink[0].points.last.map { abs($0.x - 240) < 0.01 } == true)148 #expect(abs(ink[0].width - 8) < 0.01) // size 4 scaled by 2149 }150151 @Test func aStrokeCutByTheBitmapEraserBecomesOneInkStrokePerVisiblePart() {152 let base = InkSamples.stroke(.pen, color: .black, count: 21) { CGPoint(x: 200 * $0, y: 50) }153 let mask = UIBezierPath(rect: CGRect(x: -10, y: 0, width: 70, height: 100))154 mask.append(UIBezierPath(rect: CGRect(x: 140, y: 0, width: 80, height: 100)))155 let cut = PKStroke(ink: base.ink, path: base.path, transform: .identity, mask: mask)156 let parts = InkConversion.inkStrokes(from: cut)157 #expect(parts.count == cut.maskedPathRanges.count)158 #expect(parts.count == 2)159 let xs = parts.map { ($0.points.map(\.x).min() ?? 0, $0.points.map(\.x).max() ?? 0) }.sorted { $0.0 < $1.0 }160 if xs.count == 2 {161 #expect(xs[0].0 < 1 && abs(xs[0].1 - 60) < 6)162 #expect(abs(xs[1].0 - 140) < 6 && xs[1].1 > 199)163 }164 }165166 @Test func aNewDrawingIsCroppedAndAnExistingCanvasOnlyGrows() {167 let drawing = PKDrawing(strokes: [InkSamples.stroke(.pen, color: .black, count: 12, size: { _ in 2 }) { CGPoint(x: 300 + 100 * $0, y: 500 + 40 * $0) }])168 let fresh = InkConversion.placement(of: drawing, existingCanvas: nil)169 let moved = drawing.transformed(using: CGAffineTransform(translationX: fresh.offset.width, y: fresh.offset.height))170 #expect(abs(moved.bounds.minX - InkConversion.margin) <= 1 && abs(moved.bounds.minY - InkConversion.margin) <= 1)171 #expect(abs(fresh.size.width - (drawing.bounds.width + 48)) <= 1.5 && abs(fresh.size.height - (drawing.bounds.height + 48)) <= 1.5)172 // an existing 1200 × 800 canvas (a web drawing) keeps its origin and its size173 #expect(InkConversion.placement(of: drawing, existingCanvas: CGSize(width: 1200, height: 800)) == .init(offset: .zero, size: CGSize(width: 1200, height: 800)))174 // …and grows when strokes go past it175 let grown = InkConversion.placement(of: drawing, existingCanvas: CGSize(width: 320, height: 400))176 #expect(grown.offset == .zero && grown.size.width >= drawing.bounds.maxX + 24 && grown.size.height >= drawing.bounds.maxY + 24)177 #expect(InkConversion.placement(of: PKDrawing(), existingCanvas: nil).size == InkConversion.minimumCanvas)178 }179180 // MARK: a drawing made by the web editor181182 static func webFixture() throws -> Data {183 let url = try #require(Bundle(for: InkBundleMarker.self).url(forResource: "web-drawing.ink", withExtension: "json"))184 return try Data(contentsOf: url)185 }186187 @Test func aDocumentWrittenByTheWebRendersAndRebuilds() throws {188 let data = try Self.webFixture()189 let text = String(decoding: data, as: UTF8.self)190 #expect(text.hasPrefix("{\"format\":\"spn-ink-1\",\"width\":1200,\"height\":800,\"strokes\":[{\"tool\":\"pen\",\"color\":\"#111827ff\",\"width\":5,\"points\":[[120,200,0.5],"))191 let web = try InkDocument(data: data)192 #expect(web.strokes.map(\.tool) == [.pen, .pen, .marker, .highlighter, .pen])193 #expect(web.size == CGSize(width: 1200, height: 800))194 // renders with the neutral renderer (the preview a native would upload)195 let image = try #require(InkRenderer.image(web, scale: 1, background: .white))196 #expect(image.width == 1200 && image.height == 800)197 // rebuilds as PencilKit strokes: same count, inks, colours and geometry198 let drawing = InkConversion.drawing(from: web)199 #expect(drawing.strokes.count == 5)200 #expect(drawing.strokes.map(\.ink.inkType) == [.pen, .pen, .marker, .marker, .pen])201 #expect(InkConversion.inkColor(drawing.strokes[3].ink.color).hex == "#eab30859")202 let back = InkConversion.inkDocument(from: drawing, canvasSize: web.size)203 #expect(back.strokes.map(\.tool) == web.strokes.map(\.tool))204 #expect(back.strokes.map(\.color) == web.strokes.map(\.color))205 for (a, b) in zip(web.strokes, back.strokes) {206 #expect(InkMetrics.deviation(a, b).position <= 0.75)207 // the web's constant 0.5 pressure is the same for every point: PencilKit sees a constant width, which is full pressure208 #expect(abs(Double(drawing.strokes[0].path.interpolatedPoint(at: 3).size.width) - Double(InkRenderer.penWidth(5, 0.5))) < 0.01)209 _ = b210 }211 // the dot (one point) survives212 #expect(drawing.strokes[4].path.count == 1)213 #expect(back.strokes[4].points.count == 1)214 // PencilKit's bounds include the width of each ink (26 for the highlighter): the two boxes agree to that215 let wb = web.boundingBox, pb = drawing.bounds216 #expect(abs(wb.minX - pb.minX) <= 14 && abs(wb.minY - pb.minY) <= 14 && abs(wb.maxX - pb.maxX) <= 14 && abs(wb.maxY - pb.maxY) <= 14)217 }218}219