@g9tm/lynxcontrols-native 2.5.7 → 2.5.8

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.
Files changed (33) hide show
  1. package/README.md +5 -0
  2. package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +24 -0
  3. package/android/src/main/java/com/g9lynx/g9native/blurhash/G9BlurhashUI.kt +76 -0
  4. package/android/src/main/java/com/g9lynx/g9native/blurhash/core/G9Blurhash.kt +100 -0
  5. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +94 -0
  6. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +192 -0
  7. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasUI.kt +162 -0
  8. package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +1053 -0
  9. package/android/src/main/java/com/g9lynx/g9native/richtext/G9PictureSpan.kt +99 -0
  10. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichEditText.kt +458 -0
  11. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichStyle.kt +343 -0
  12. package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichTextUI.kt +94 -0
  13. package/catalog/catalog.json +1 -1
  14. package/catalog/index.md +1 -1
  15. package/dist/.tsbuildinfo +1 -1
  16. package/ios/g9lynx-native-swift.podspec +20 -0
  17. package/ios/g9lynx-native.podspec +6 -2
  18. package/ios/src/G9LCBlurhashUI.h +14 -0
  19. package/ios/src/G9LCBlurhashUI.m +28 -0
  20. package/ios/src/G9LCCanvasUI.h +18 -0
  21. package/ios/src/G9LCCanvasUI.m +81 -0
  22. package/ios/src/G9LCRichTextUI.h +16 -0
  23. package/ios/src/G9LCRichTextUI.m +71 -0
  24. package/ios/swift/blurhash/G9LCBlurhashView.swift +165 -0
  25. package/ios/swift/canvas/G9LCCanvasCore.swift +848 -0
  26. package/ios/swift/canvas/G9LCCanvasView.swift +482 -0
  27. package/ios/swift/richtext/G9LCRichStyle.swift +238 -0
  28. package/ios/swift/richtext/G9LCRichTextView.swift +647 -0
  29. package/ios/swift/shared/G9LCColors.swift +78 -0
  30. package/ios/swift/shared/G9LCFonts.swift +38 -0
  31. package/ios/swift/shared/G9LCImages.swift +28 -0
  32. package/ios/swift/shared/G9LCJson.swift +73 -0
  33. package/package.json +6 -4
@@ -0,0 +1,482 @@
1
+ import UIKit
2
+
3
+ /// The drawing surface of `<g9-canvas>` (G9CanvasUI; the Android host's G9LCCanvasView): draws the items the app sent
4
+ /// (SVG-path, text and picture ops) through the camera with Core Graphics, the previews and selection of the gesture
5
+ /// rules (G9LCCanvasCore), collaborators' pointers; reports gestures as `cv*` events. Excalidraw's dark mode
6
+ /// (`invert(93%) hue-rotate(180deg)`) is applied to every colour drawn (pictures are drawn as they are).
7
+ ///
8
+ /// Rendering: the whole visible scene is drawn on each change (items culled by their bounds; parsed paths are kept per
9
+ /// item version). Main thread.
10
+ @objc(G9LCCanvasView)
11
+ final class G9LCCanvasView: UIView {
12
+ /// `(event name, detail)` → the element (G9CanvasUI).
13
+ @objc var emitter: ((String, [String: Any]) -> Void)?
14
+
15
+ private let core = G9LCCanvasCore()
16
+ private var dark = false
17
+ private var background = G9LCColors.RGBA(r: 255, g: 255, b: 255, a: 255)
18
+ private var accent = G9LCColors.RGBA(r: 0x69, g: 0x65, b: 0xdb, a: 255)
19
+ private var strokeColor = G9LCColors.RGBA(r: 0x1e, g: 0x1e, b: 0x1e, a: 255)
20
+ private var strokeWidth: Double = 2
21
+ private var fillColor: G9LCColors.RGBA?
22
+ private var touchIds: [ObjectIdentifier: Int] = [:]
23
+ private var nextTouch = 0
24
+ private var images: [String: UIImage] = [:]
25
+ private var loading = Set<String>()
26
+
27
+ override init(frame: CGRect) {
28
+ super.init(frame: frame)
29
+ isOpaque = true
30
+ isMultipleTouchEnabled = true
31
+ contentMode = .redraw
32
+ core.emit = { [weak self] name, detail in self?.emitter?(name, detail) }
33
+ core.invalidate = { [weak self] in self?.setNeedsDisplay() }
34
+ }
35
+
36
+ required init?(coder: NSCoder) {
37
+ fatalError("init(coder:) is not used")
38
+ }
39
+
40
+ override func layoutSubviews() {
41
+ super.layoutSubviews()
42
+ core.setSize(Double(bounds.width), Double(bounds.height))
43
+ }
44
+
45
+ // MARK: - attributes and methods
46
+
47
+ @objc func applyTool(_ value: String?) { core.setTool((value ?? "").isEmpty ? "selection" : value!) }
48
+
49
+ @objc func applyEditable(_ value: Bool) {
50
+ core.editable = value
51
+ setNeedsDisplay()
52
+ }
53
+
54
+ @objc func applyTheme(_ value: String?) {
55
+ dark = value == "dark"
56
+ setNeedsDisplay()
57
+ }
58
+
59
+ @objc func applyBackground(_ value: String?) {
60
+ background = G9LCColors.parse(value) ?? G9LCColors.RGBA(r: 255, g: 255, b: 255, a: 255)
61
+ setNeedsDisplay()
62
+ }
63
+
64
+ @objc func applyAccent(_ value: String?) {
65
+ accent = G9LCColors.parse(value) ?? G9LCColors.RGBA(r: 0x69, g: 0x65, b: 0xdb, a: 255)
66
+ setNeedsDisplay()
67
+ }
68
+
69
+ @objc func applyStroke(_ json: String?) {
70
+ guard let o = G9LCJson.object(json) else { return }
71
+ strokeColor = G9LCColors.parse(G9LCJson.string(o, "color")) ?? G9LCColors.RGBA(r: 0, g: 0, b: 0, a: 255)
72
+ strokeWidth = G9LCJson.double(o, "width") ?? 2
73
+ fillColor = G9LCColors.parse(G9LCJson.string(o, "fill"))
74
+ }
75
+
76
+ /// The app's message (`invoke('apply', {json})`).
77
+ @objc func applyMessage(_ json: String?) {
78
+ guard let message = G9LCJson.object(json) else { return }
79
+ core.apply(message)
80
+ }
81
+
82
+ /// `exportImage({token, scale, padding, background, name})` → `cvexport {token, file | error}`.
83
+ @objc func exportImage(_ params: [String: Any]) {
84
+ let token = G9LCJson.string(params, "token") ?? ""
85
+ let scale = G9LCJson.double(params, "scale") ?? 2
86
+ let padding = G9LCJson.double(params, "padding") ?? 24
87
+ let withBackground = G9LCJson.bool(params, "background") ?? true
88
+ var name = G9LCJson.string(params, "name") ?? ""
89
+ if name.trimmingCharacters(in: .whitespaces).isEmpty { name = "whiteboard.png" }
90
+ guard let image = renderExport(scale: scale, padding: padding, withBackground: withBackground), let data = image.pngData() else {
91
+ emitter?("cvexport", ["token": token, "error": "empty"])
92
+ return
93
+ }
94
+ DispatchQueue.global(qos: .userInitiated).async {
95
+ do {
96
+ var ref = try G9LCCanvasFiles.register(data, name, "image/png")
97
+ ref["width"] = Int(image.size.width * image.scale)
98
+ ref["height"] = Int(image.size.height * image.scale)
99
+ DispatchQueue.main.async { self.emitter?("cvexport", ["token": token, "file": ref]) }
100
+ } catch {
101
+ DispatchQueue.main.async { self.emitter?("cvexport", ["token": token, "error": error.localizedDescription]) }
102
+ }
103
+ }
104
+ }
105
+
106
+ // MARK: - touches
107
+
108
+ private func id(of touch: UITouch) -> Int {
109
+ let key = ObjectIdentifier(touch)
110
+ if let id = touchIds[key] { return id }
111
+ nextTouch += 1
112
+ touchIds[key] = nextTouch
113
+ return nextTouch
114
+ }
115
+
116
+ override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
117
+ for touch in touches {
118
+ let p = touch.location(in: self)
119
+ let pen = touch.type == .pencil
120
+ let pressure = pen && touch.maximumPossibleForce > 0 ? Double(touch.force / touch.maximumPossibleForce) : nil
121
+ core.pointerDown(id: id(of: touch), x: Double(p.x), y: Double(p.y), pen: pen, pressure: pressure)
122
+ }
123
+ }
124
+
125
+ override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
126
+ for touch in touches {
127
+ let id = self.id(of: touch)
128
+ // Coalesced samples: a smooth freehand stroke at the display's touch rate.
129
+ for sample in event?.coalescedTouches(for: touch) ?? [touch] {
130
+ let p = sample.location(in: self)
131
+ let pressure = sample.type == .pencil && sample.maximumPossibleForce > 0 ? Double(sample.force / sample.maximumPossibleForce) : nil
132
+ core.pointerMove(id: id, x: Double(p.x), y: Double(p.y), pressure: pressure)
133
+ }
134
+ }
135
+ }
136
+
137
+ override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
138
+ for touch in touches {
139
+ let p = touch.location(in: self)
140
+ core.pointerUp(id: id(of: touch), x: Double(p.x), y: Double(p.y))
141
+ touchIds.removeValue(forKey: ObjectIdentifier(touch))
142
+ }
143
+ }
144
+
145
+ override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
146
+ for touch in touches {
147
+ core.pointerCancel(id: id(of: touch))
148
+ touchIds.removeValue(forKey: ObjectIdentifier(touch))
149
+ }
150
+ }
151
+
152
+ // MARK: - drawing
153
+
154
+ /// Excalidraw's dark mode on one colour: `invert(93%)` then `hue-rotate(180deg)` (CSS filter matrices).
155
+ private func paint(_ color: G9LCColors.RGBA, alpha: Double = 1) -> CGColor {
156
+ var r = Double(color.r) / 255, g = Double(color.g) / 255, b = Double(color.b) / 255
157
+ if dark {
158
+ let h: [Double] = [-0.574, 1.43, 0.144, 0.426, 0.43, 0.144, 0.426, 1.43, -0.856]
159
+ let nr = 0.93 - 0.86 * (h[0] * r + h[1] * g + h[2] * b)
160
+ let ng = 0.93 - 0.86 * (h[3] * r + h[4] * g + h[5] * b)
161
+ let nb = 0.93 - 0.86 * (h[6] * r + h[7] * g + h[8] * b)
162
+ r = min(max(nr, 0), 1)
163
+ g = min(max(ng, 0), 1)
164
+ b = min(max(nb, 0), 1)
165
+ }
166
+ return UIColor(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: CGFloat(Double(color.a) / 255 * alpha)).cgColor
167
+ }
168
+
169
+ override func draw(_ rect: CGRect) {
170
+ guard let context = UIGraphicsGetCurrentContext() else { return }
171
+ context.setFillColor(paint(background))
172
+ context.fill(bounds)
173
+ let view = core.view
174
+ context.saveGState()
175
+ context.scaleBy(x: CGFloat(view.zoom), y: CGFloat(view.zoom))
176
+ context.translateBy(x: CGFloat(view.scrollX), y: CGFloat(view.scrollY))
177
+
178
+ let topLeft = core.toScene(0, 0)
179
+ let bottomRight = core.toScene(Double(bounds.width), Double(bounds.height))
180
+ let visible = G9LCCanvasCore.Box(topLeft.0, topLeft.1, bottomRight.0, bottomRight.1)
181
+ let moving = core.movingIds()
182
+ for id in core.order {
183
+ guard !core.hidden.contains(id), !moving.contains(id), let item = core.items[id] else { continue }
184
+ guard item.b.x2 >= visible.x1, item.b.x1 <= visible.x2, item.b.y2 >= visible.y1, item.b.y1 <= visible.y2 else { continue }
185
+ drawItem(context, item, matrix: nil, alpha: core.faded.contains(id) ? 0.3 : 1)
186
+ }
187
+ if let matrix = core.liveMatrix() {
188
+ for id in core.order where moving.contains(id) && !core.hidden.contains(id) {
189
+ if let item = core.items[id] { drawItem(context, item, matrix: matrix, alpha: 1) }
190
+ }
191
+ }
192
+ for preview in core.activePreviews() { drawPreview(context, preview) }
193
+ drawSelection(context, moving: core.liveMatrix())
194
+ if let box = core.marquee() {
195
+ let r = CGRect(x: box.x1, y: box.y1, width: box.width, height: box.height)
196
+ context.setFillColor(paint(accent, alpha: 0.08))
197
+ context.fill(r)
198
+ context.setStrokeColor(paint(accent))
199
+ context.setLineWidth(CGFloat(1 / view.zoom))
200
+ context.stroke(r)
201
+ }
202
+ for peer in core.peers {
203
+ let color = peer.color ?? accent
204
+ for box in peer.boxes { strokeRotated(context, box.b, angle: box.a, color: color, width: 1.5 / view.zoom) }
205
+ }
206
+ context.restoreGState()
207
+
208
+ // Screen space: collaborators' pointers, the eraser trail.
209
+ for peer in core.peers {
210
+ guard let x = peer.x, let y = peer.y else { continue }
211
+ let p = core.toScreen(x, y)
212
+ let color = peer.color ?? accent
213
+ context.setFillColor(paint(color))
214
+ context.fillEllipse(in: CGRect(x: p.0 - 4, y: p.1 - 4, width: 8, height: 8))
215
+ let attributes: [NSAttributedString.Key: Any] = [.font: G9LCFonts.text(size: 12, weight: 600), .foregroundColor: UIColor.white]
216
+ let label = peer.name as NSString
217
+ let size = label.size(withAttributes: attributes)
218
+ let tag = CGRect(x: p.0 + 10, y: p.1 + 10, width: size.width + 10, height: size.height + 4)
219
+ context.addPath(UIBezierPath(roundedRect: tag, cornerRadius: 6).cgPath)
220
+ context.fillPath()
221
+ label.draw(at: CGPoint(x: tag.minX + 5, y: tag.minY + 2), withAttributes: attributes)
222
+ }
223
+ let trail = core.trail()
224
+ if trail.count > 1 {
225
+ context.setStrokeColor(UIColor(white: 0.5, alpha: 0.5).cgColor)
226
+ context.setLineWidth(4)
227
+ context.setLineCap(.round)
228
+ context.setLineJoin(.round)
229
+ context.move(to: CGPoint(x: trail[0].x, y: trail[0].y))
230
+ for point in trail.dropFirst() { context.addLine(to: CGPoint(x: point.x, y: point.y)) }
231
+ context.strokePath()
232
+ // The trail fades: draw again shortly.
233
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in self?.setNeedsDisplay() }
234
+ }
235
+ }
236
+
237
+ private func drawItem(_ context: CGContext, _ item: G9LCCanvasCore.Item, matrix: G9LCCanvasCore.Matrix?, alpha: Double) {
238
+ context.saveGState()
239
+ if let matrix = matrix { context.concatenate(G9LCCanvasCore.affine(matrix)) }
240
+ if item.a != 0 {
241
+ context.translateBy(x: CGFloat(item.cx), y: CGFloat(item.cy))
242
+ context.rotate(by: CGFloat(item.a))
243
+ context.translateBy(x: CGFloat(-item.cx), y: CGFloat(-item.cy))
244
+ }
245
+ context.translateBy(x: CGFloat(item.x), y: CGFloat(item.y))
246
+ let a = min(max(item.o * alpha, 0), 1)
247
+ for op in item.ops { drawOp(context, op, alpha: a) }
248
+ context.restoreGState()
249
+ }
250
+
251
+ private func drawOp(_ context: CGContext, _ op: G9LCCanvasCore.Op, alpha: Double) {
252
+ switch op {
253
+ case let .path(path, stroke, width, dash, fill, evenOdd):
254
+ if let fill = fill {
255
+ context.addPath(path)
256
+ context.setFillColor(paint(fill, alpha: alpha))
257
+ context.fillPath(using: evenOdd ? .evenOdd : .winding)
258
+ }
259
+ if let stroke = stroke {
260
+ context.addPath(path)
261
+ context.setStrokeColor(paint(stroke, alpha: alpha))
262
+ context.setLineWidth(CGFloat(width))
263
+ context.setLineCap(.round)
264
+ context.setLineJoin(.round)
265
+ context.setLineDash(phase: 0, lengths: dash.count >= 2 ? dash.map { CGFloat($0) } : [])
266
+ context.strokePath()
267
+ context.setLineDash(phase: 0, lengths: [])
268
+ }
269
+ case let .text(lines, x, y, lineHeight, size, font, color, align, _, _):
270
+ let face: UIFont
271
+ switch font {
272
+ case 6: face = G9LCFonts.font(file: "Nunito", size: CGFloat(size))
273
+ case 8, 3: face = G9LCFonts.font(file: "ComicShanns", size: CGFloat(size))
274
+ case 2, 9: face = UIFont.systemFont(ofSize: CGFloat(size))
275
+ default: face = G9LCFonts.font(file: "Excalifont", size: CGFloat(size))
276
+ }
277
+ let ink = UIColor(cgColor: paint(color ?? G9LCColors.RGBA(r: 0x1e, g: 0x1e, b: 0x1e, a: 255), alpha: alpha))
278
+ let attributes: [NSAttributedString.Key: Any] = [.font: face, .foregroundColor: ink]
279
+ UIGraphicsPushContext(context)
280
+ for (index, line) in lines.enumerated() {
281
+ let text = line as NSString
282
+ let measured = text.size(withAttributes: attributes).width
283
+ var left = CGFloat(x)
284
+ if align == "center" { left -= measured / 2 } else if align == "right" { left -= measured }
285
+ let baseline = CGFloat(y + Double(index) * lineHeight)
286
+ text.draw(at: CGPoint(x: left, y: baseline - face.ascender), withAttributes: attributes)
287
+ }
288
+ UIGraphicsPopContext()
289
+ case let .image(file, w, h, radius, crop):
290
+ let frame = CGRect(x: 0, y: 0, width: w, height: h)
291
+ guard let url = core.files[file], let image = picture(url) else {
292
+ context.setFillColor(UIColor(red: 0xe9 / 255, green: 0xec / 255, blue: 0xef / 255, alpha: CGFloat(alpha)).cgColor)
293
+ context.fill(frame)
294
+ return
295
+ }
296
+ context.saveGState()
297
+ if radius > 0 {
298
+ context.addPath(UIBezierPath(roundedRect: frame, cornerRadius: CGFloat(radius)).cgPath)
299
+ context.clip()
300
+ }
301
+ context.setAlpha(CGFloat(alpha))
302
+ var drawn = image
303
+ if let crop = crop, crop.count >= 4, let cg = image.cgImage,
304
+ let cut = cg.cropping(to: CGRect(x: crop[0], y: crop[1], width: crop[2], height: crop[3])) {
305
+ drawn = UIImage(cgImage: cut)
306
+ }
307
+ UIGraphicsPushContext(context)
308
+ drawn.draw(in: frame)
309
+ UIGraphicsPopContext()
310
+ context.restoreGState()
311
+ }
312
+ }
313
+
314
+ private func drawPreview(_ context: CGContext, _ preview: G9LCCanvasCore.Preview) {
315
+ let zoom = core.view.zoom
316
+ context.saveGState()
317
+ context.setStrokeColor(paint(strokeColor))
318
+ context.setLineWidth(CGFloat(strokeWidth))
319
+ context.setLineCap(.round)
320
+ context.setLineJoin(.round)
321
+ switch preview {
322
+ case let .stroke(points):
323
+ guard points.count >= 2 else { break }
324
+ context.move(to: CGPoint(x: points[0], y: points[1]))
325
+ var i = 2
326
+ while i + 1 < points.count {
327
+ context.addLine(to: CGPoint(x: points[i], y: points[i + 1]))
328
+ i += 2
329
+ }
330
+ if points.count == 2 { context.addLine(to: CGPoint(x: points[0] + 0.1, y: points[1])) }
331
+ context.strokePath()
332
+ case let .shape(tool, b):
333
+ let r = CGRect(x: b.x1, y: b.y1, width: b.width, height: b.height)
334
+ let path: CGPath
335
+ switch tool {
336
+ case "ellipse": path = CGPath(ellipseIn: r, transform: nil)
337
+ case "diamond":
338
+ let diamond = CGMutablePath()
339
+ diamond.move(to: CGPoint(x: r.midX, y: r.minY))
340
+ diamond.addLine(to: CGPoint(x: r.maxX, y: r.midY))
341
+ diamond.addLine(to: CGPoint(x: r.midX, y: r.maxY))
342
+ diamond.addLine(to: CGPoint(x: r.minX, y: r.midY))
343
+ diamond.closeSubpath()
344
+ path = diamond
345
+ default: path = CGPath(rect: r, transform: nil)
346
+ }
347
+ if let fill = fillColor, tool != "frame" {
348
+ context.addPath(path)
349
+ context.setFillColor(paint(fill))
350
+ context.fillPath()
351
+ }
352
+ if tool == "frame" { context.setLineDash(phase: 0, lengths: [CGFloat(6 / zoom), CGFloat(4 / zoom)]) }
353
+ context.addPath(path)
354
+ context.strokePath()
355
+ case let .line(tool, points):
356
+ guard points.count >= 2 else { break }
357
+ let a = CGPoint(x: points[0].0, y: points[0].1)
358
+ let b = CGPoint(x: points[points.count - 1].0, y: points[points.count - 1].1)
359
+ context.move(to: a)
360
+ context.addLine(to: b)
361
+ if tool == "arrow" {
362
+ let angle = atan2(b.y - a.y, b.x - a.x)
363
+ let size = max(10, CGFloat(strokeWidth) * 5)
364
+ for side in [CGFloat.pi * 0.85, -CGFloat.pi * 0.85] {
365
+ context.move(to: b)
366
+ context.addLine(to: CGPoint(x: b.x + cos(angle + side) * size, y: b.y + sin(angle + side) * size))
367
+ }
368
+ }
369
+ context.strokePath()
370
+ case let .point(points):
371
+ guard let first = points.first else { break }
372
+ context.setStrokeColor(paint(accent))
373
+ context.setLineWidth(CGFloat(1.5 / zoom))
374
+ context.setLineDash(phase: 0, lengths: [CGFloat(4 / zoom), CGFloat(3 / zoom)])
375
+ context.move(to: CGPoint(x: first.0, y: first.1))
376
+ for point in points.dropFirst() { context.addLine(to: CGPoint(x: point.0, y: point.1)) }
377
+ context.strokePath()
378
+ }
379
+ context.restoreGState()
380
+ }
381
+
382
+ private func drawSelection(_ context: CGContext, moving: G9LCCanvasCore.Matrix?) {
383
+ guard let selection = core.selection else { return }
384
+ let zoom = core.view.zoom
385
+ context.saveGState()
386
+ if let moving = moving { context.concatenate(G9LCCanvasCore.affine(moving)) }
387
+ for box in selection.boxes { strokeRotated(context, box.b, angle: box.a, color: accent, width: 1 / zoom) }
388
+ if let frame = selection.frame, selection.handles != "none" {
389
+ strokeRotated(context, frame.b, angle: frame.a, color: accent, width: 1 / zoom)
390
+ }
391
+ context.restoreGState()
392
+ guard moving == nil else { return }
393
+ let size = 10 / zoom
394
+ for handle in core.handles(touch: true) {
395
+ let r = CGRect(x: handle.x - size / 2, y: handle.y - size / 2, width: size, height: size)
396
+ context.setFillColor(UIColor.white.cgColor)
397
+ context.setStrokeColor(paint(accent))
398
+ context.setLineWidth(CGFloat(1.5 / zoom))
399
+ if handle.handle == "rotation" {
400
+ context.fillEllipse(in: r)
401
+ context.strokeEllipse(in: r)
402
+ } else {
403
+ context.fill(r)
404
+ context.stroke(r)
405
+ }
406
+ }
407
+ if core.editable, let points = selection.points {
408
+ for point in points {
409
+ let r = CGRect(x: point.0 - size / 2, y: point.1 - size / 2, width: size, height: size)
410
+ context.setFillColor(UIColor.white.cgColor)
411
+ context.fillEllipse(in: r)
412
+ context.setStrokeColor(paint(accent))
413
+ context.strokeEllipse(in: r)
414
+ }
415
+ }
416
+ }
417
+
418
+ private func strokeRotated(_ context: CGContext, _ b: G9LCCanvasCore.Box, angle: Double, color: G9LCColors.RGBA, width: Double) {
419
+ context.saveGState()
420
+ let (cx, cy) = b.centre
421
+ context.translateBy(x: CGFloat(cx), y: CGFloat(cy))
422
+ context.rotate(by: CGFloat(angle))
423
+ context.translateBy(x: CGFloat(-cx), y: CGFloat(-cy))
424
+ context.setStrokeColor(paint(color))
425
+ context.setLineWidth(CGFloat(width))
426
+ context.stroke(CGRect(x: b.x1, y: b.y1, width: b.width, height: b.height))
427
+ context.restoreGState()
428
+ }
429
+
430
+ // MARK: - pictures and export
431
+
432
+ /// A picture of the board (through the pinned session), or nil while it loads.
433
+ private func picture(_ url: String) -> UIImage? {
434
+ if let image = images[url] { return image }
435
+ guard !loading.contains(url), let target = URL(string: url) else { return nil }
436
+ if target.scheme?.lowercased() == "http", !G9LCImages.isLocal(target.host) { return nil }
437
+ loading.insert(url)
438
+ G9LCImages.session.dataTask(with: target) { [weak self] data, _, _ in
439
+ let image = data.flatMap { UIImage(data: $0) }
440
+ DispatchQueue.main.async {
441
+ guard let self = self else { return }
442
+ self.loading.remove(url)
443
+ if let image = image {
444
+ self.images[url] = image
445
+ self.setNeedsDisplay()
446
+ }
447
+ }
448
+ }.resume()
449
+ return nil
450
+ }
451
+
452
+ /// Every item to an image (`exportImage`), or nil for an empty board.
453
+ private func renderExport(scale: Double, padding: Double, withBackground: Bool) -> UIImage? {
454
+ let list = core.order.compactMap { core.items[$0] }
455
+ guard !list.isEmpty else { return nil }
456
+ var minX = Double.greatestFiniteMagnitude, minY = Double.greatestFiniteMagnitude
457
+ var maxX = -Double.greatestFiniteMagnitude, maxY = -Double.greatestFiniteMagnitude
458
+ for item in list {
459
+ minX = min(minX, item.b.x1)
460
+ minY = min(minY, item.b.y1)
461
+ maxX = max(maxX, item.b.x2)
462
+ maxY = max(maxY, item.b.y2)
463
+ }
464
+ let width = max(1, (maxX - minX + padding * 2) * scale)
465
+ let height = max(1, (maxY - minY + padding * 2) * scale)
466
+ let fit = min(1, 8192 / width, 8192 / height)
467
+ let format = UIGraphicsImageRendererFormat()
468
+ format.scale = 1
469
+ format.opaque = withBackground
470
+ let size = CGSize(width: width * fit, height: height * fit)
471
+ return UIGraphicsImageRenderer(size: size, format: format).image { renderer in
472
+ let context = renderer.cgContext
473
+ if withBackground {
474
+ context.setFillColor(paint(background))
475
+ context.fill(CGRect(origin: .zero, size: size))
476
+ }
477
+ context.scaleBy(x: CGFloat(scale * fit), y: CGFloat(scale * fit))
478
+ context.translateBy(x: CGFloat(padding - minX), y: CGFloat(padding - minY))
479
+ for item in list { drawItem(context, item, matrix: nil, alpha: 1) }
480
+ }
481
+ }
482
+ }