Repository Public
1// SP Lecture Studio — LectureCore2// Vector ink model: strokes are kept as time-coded points in page-normalized coordinates, never as a flattened raster.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation56/// sRGB colour with straight alpha. Encoded as `#RRGGBBAA`.7public struct RGBAColor: Hashable, Sendable, Codable {8 public var r: Double, g: Double, b: Double, a: Double9 public init(r: Double, g: Double, b: Double, a: Double = 1) { self.r = r; self.g = g; self.b = b; self.a = a }1011 public static let black = RGBAColor(r: 0.07, g: 0.07, b: 0.09)12 public static let red = RGBAColor(r: 0.86, g: 0.15, b: 0.15)13 public static let blue = RGBAColor(r: 0.11, g: 0.36, b: 0.85)14 public static let green = RGBAColor(r: 0.09, g: 0.56, b: 0.27)15 public static let orange = RGBAColor(r: 0.95, g: 0.52, b: 0.07)16 public static let yellow = RGBAColor(r: 1.0, g: 0.86, b: 0.0)17 public static let purple = RGBAColor(r: 0.55, g: 0.24, b: 0.78)18 public static let white = RGBAColor(r: 1, g: 1, b: 1)19 public static let palette: [RGBAColor] = [.black, .red, .blue, .green, .orange, .yellow, .purple, .white]2021 public var hex: String {22 func c(_ v: Double) -> Int { Int((min(max(v, 0), 1) * 255).rounded()) }23 return String(format: "#%02X%02X%02X%02X", c(r), c(g), c(b), c(a))24 }25 public init?(hex: String) {26 var s = hex; if s.hasPrefix("#") { s.removeFirst() }27 guard s.count == 6 || s.count == 8, let v = UInt64(s, radix: 16) else { return nil }28 let full = s.count == 8 ? v : (v << 8) | 0xFF29 r = Double((full >> 24) & 0xFF) / 255; g = Double((full >> 16) & 0xFF) / 25530 b = Double((full >> 8) & 0xFF) / 255; a = Double(full & 0xFF) / 25531 }32 public init(from decoder: Decoder) throws {33 let s = try decoder.singleValueContainer().decode(String.self)34 guard let c = RGBAColor(hex: s) else {35 throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "bad colour \(s)"))36 }37 self = c38 }39 public func encode(to encoder: Encoder) throws { var c = encoder.singleValueContainer(); try c.encode(hex) }40}4142public enum InkTool: String, Codable, Sendable, CaseIterable {43 case pen, marker, pencil44 case line, arrow, rectangle, ellipse45 public var isShape: Bool { self == .line || self == .arrow || self == .rectangle || self == .ellipse }46 /// Markers are translucent and composited as one layer so that overlapping segments do not darken.47 public var isTranslucent: Bool { self == .marker }48}4950/// One sample of a stroke.51/// - `x`, `y`: position normalized to the page crop box, origin at the top-left corner, x to the right, y down (0…1).52/// - `dt`: time since the beginning of the stroke, in nanoseconds.53/// - `w`: width multiplier (pressure / velocity), 1 = nominal width.54public struct StrokePoint: Hashable, Sendable {55 public var x: Double, y: Double56 public var dt: Int6457 public var w: Double58 public init(x: Double, y: Double, dt: Int64 = 0, w: Double = 1) { self.x = x; self.y = y; self.dt = dt; self.w = w }59}6061public struct Stroke: Hashable, Sendable, Codable, Identifiable {62 public var id: UUID63 /// Zero-based page index in the source PDF (the page identifier of a project: the PDF copy is immutable).64 public var page: Int65 public var tool: InkTool66 public var color: RGBAColor67 /// Nominal width normalized to the page width (0.004 ≈ 2.4 pt on a 612 pt wide page).68 public var width: Double69 /// Timeline time of the first point.70 public var begin: MediaTime71 public var points: [StrokePoint]7273 public init(id: UUID = UUID(), page: Int, tool: InkTool, color: RGBAColor, width: Double, begin: MediaTime, points: [StrokePoint]) {74 self.id = id; self.page = page; self.tool = tool; self.color = color; self.width = width; self.begin = begin; self.points = points75 }7677 public var duration: MediaTime { MediaTime(ns: points.last?.dt ?? 0) }78 public var end: MediaTime { begin + duration }7980 /// Number of points already drawn at timeline time `t` (0 when the stroke has not begun, `points.count` once finished).81 public func pointCount(at t: MediaTime) -> Int {82 if t < begin { return 0 }83 let rel = (t - begin).ns84 if let last = points.last, rel >= last.dt { return points.count }85 var lo = 0, hi = points.count // first index with dt > rel86 while lo < hi { let mid = (lo + hi) / 2; if points[mid].dt <= rel { lo = mid + 1 } else { hi = mid } }87 return lo88 }8990 public var bounds: (minX: Double, minY: Double, maxX: Double, maxY: Double)? {91 guard let f = points.first else { return nil }92 var b = (f.x, f.y, f.x, f.y)93 for p in points { b.0 = min(b.0, p.x); b.1 = min(b.1, p.y); b.2 = max(b.2, p.x); b.3 = max(b.3, p.y) }94 return (b.0, b.1, b.2, b.3)95 }9697 // Compact encoding: points are a flat array [x, y, dtMilliseconds, w, …] rounded to 1e-5 (sub-pixel at 8K).98 private enum CodingKeys: String, CodingKey { case id, page, tool, color, width, begin, p }99 public init(from decoder: Decoder) throws {100 let c = try decoder.container(keyedBy: CodingKeys.self)101 id = try c.decode(UUID.self, forKey: .id); page = try c.decode(Int.self, forKey: .page)102 tool = try c.decode(InkTool.self, forKey: .tool); color = try c.decode(RGBAColor.self, forKey: .color)103 width = try c.decode(Double.self, forKey: .width); begin = try c.decode(MediaTime.self, forKey: .begin)104 let flat = try c.decode([Double].self, forKey: .p)105 guard flat.count % 4 == 0 else {106 throw DecodingError.dataCorruptedError(forKey: .p, in: c, debugDescription: "stroke points must come in groups of 4")107 }108 points = stride(from: 0, to: flat.count, by: 4).map {109 StrokePoint(x: flat[$0], y: flat[$0 + 1], dt: Int64((flat[$0 + 2] * 1e6).rounded()), w: flat[$0 + 3])110 }111 }112 public func encode(to encoder: Encoder) throws {113 var c = encoder.container(keyedBy: CodingKeys.self)114 try c.encode(id, forKey: .id); try c.encode(page, forKey: .page); try c.encode(tool, forKey: .tool)115 try c.encode(color, forKey: .color); try c.encode(width, forKey: .width); try c.encode(begin, forKey: .begin)116 func r5(_ v: Double) -> Double { (v * 100_000).rounded() / 100_000 }117 var flat: [Double] = []; flat.reserveCapacity(points.count * 4)118 for p in points { flat.append(r5(p.x)); flat.append(r5(p.y)); flat.append((Double(p.dt) / 1e6 * 1000).rounded() / 1000); flat.append((p.w * 1000).rounded() / 1000) }119 try c.encode(flat, forKey: .p)120 }121}122123/// Zoom and pan of the stage. `zoom` ≥ 1; `cx`, `cy` is the page-normalized point shown at the centre of the stage.124public struct Viewport: Hashable, Sendable, Codable {125 public var zoom: Double, cx: Double, cy: Double126 public init(zoom: Double = 1, cx: Double = 0.5, cy: Double = 0.5) { self.zoom = zoom; self.cx = cx; self.cy = cy }127 public static let fit = Viewport()128 public var isFit: Bool { zoom <= 1.0001 }129}130