Commit 2863037e46
Unsigned
Layout: unified · split
Packages/MoonriseKit/Sources/SkyRenderer/SkyView.swift +49 −5
| @@ -163,9 +163,11 @@ public struct SkyView: View { | |||
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | /// A culture's figures, named at the middle of their visible stars once zoomed in | 165 | /// A culture's figures, named at the middle of their visible stars once zoomed in |
| 166 | /// (closer for cultures with many small figures). | 166 | /// (closer for cultures with many small figures). Returns the names drawn. |
| 167 | func drawCulture(_ culture: SkyCulture, _ context: inout GraphicsContext, _ p: Projection) { | 167 | @discardableResult |
| 168 | func drawCulture(_ culture: SkyCulture, _ context: inout GraphicsContext, _ p: Projection) -> [String] { | ||
| 168 | var path = Path() | 169 | var path = Path() |
| 170 | var labelled: [String] = [] | ||
| 169 | let labelBelow = culture.constellations.count > 60 ? 60.0 : 110.0 | 171 | let labelBelow = culture.constellations.count > 60 ? 60.0 : 110.0 |
| 170 | for figure in culture.constellations { | 172 | for figure in culture.constellations { |
| 171 | var points: [CGPoint] = [] | 173 | var points: [CGPoint] = [] |
| @@ -181,10 +183,11 @@ public struct SkyView: View { | |||
| 181 | } | 183 | } |
| 182 | if fieldOfView <= labelBelow, points.count > 1 { | 184 | if fieldOfView <= labelBelow, points.count > 1 { |
| 183 | let c = CGPoint(x: points.map(\.x).reduce(0, +) / Double(points.count), y: points.map(\.y).reduce(0, +) / Double(points.count)) | 185 | let c = CGPoint(x: points.map(\.x).reduce(0, +) / Double(points.count), y: points.map(\.y).reduce(0, +) / Double(points.count)) |
| 184 | context.draw(Text(figure.name).font(.caption2).foregroundStyle(style.muted), at: c) | 186 | if label(&context, figure.name, at: c, anchor: .center, color: style.muted) { labelled.append(figure.name) } |
| 185 | } | 187 | } |
| 186 | } | 188 | } |
| 187 | context.stroke(path, with: .color(style.line), lineWidth: 0.6) | 189 | context.stroke(path, with: .color(style.line), lineWidth: 0.6) |
| 190 | return labelled | ||
| 188 | } | 191 | } |
| 189 | 192 | ||
| 190 | /// A constellation's IAU lines drawn bright and thick; a star or body ringed. | 193 | /// A constellation's IAU lines drawn bright and thick; a star or body ringed. |
| @@ -333,9 +336,50 @@ public struct SkyView: View { | |||
| 333 | } | 336 | } |
| 334 | } | 337 | } |
| 335 | 338 | ||
| 336 | func label(_ context: inout GraphicsContext, _ text: String, at point: CGPoint, anchor: UnitPoint, color: Color? = nil) { | 339 | /// Draws a label unless it falls wholly outside the canvas; returns whether it was drawn. |
| 337 | let t = Text(text).font(.caption2).foregroundStyle(color ?? style.label) | 340 | @discardableResult |
| 341 | func label(_ context: inout GraphicsContext, _ text: String, at point: CGPoint, anchor: UnitPoint, color: Color? = nil) -> Bool { | ||
| 342 | let t = LabelCache.shared.resolve(text, color: color ?? style.label, in: context) | ||
| 343 | let size = t.measure(in: CGSize(width: CGFloat.infinity, height: .infinity)) | ||
| 344 | let frame = CGRect(x: point.x - anchor.x * size.width, y: point.y - anchor.y * size.height, width: size.width, height: size.height) | ||
| 345 | guard frame.intersects(context.clipBoundingRect) else { return false } | ||
| 338 | context.draw(t, at: point, anchor: anchor) | 346 | context.draw(t, at: point, anchor: anchor) |
| 347 | return true | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | /// Labels resolved once and drawn again on later frames; laying out a `Text` is most of the cost of drawing it. | ||
| 352 | /// Resolved text depends on the environment, so the cache is emptied when the parts that change at run time do. | ||
| 353 | @MainActor | ||
| 354 | final class LabelCache { | ||
| 355 | static let shared = LabelCache() | ||
| 356 | |||
| 357 | private struct Key: Hashable { | ||
| 358 | var text: String | ||
| 359 | var color: Color | ||
| 360 | } | ||
| 361 | |||
| 362 | private struct TextEnvironment: Equatable { | ||
| 363 | var dynamicTypeSize: DynamicTypeSize | ||
| 364 | var legibilityWeight: LegibilityWeight? | ||
| 365 | var displayScale: CGFloat | ||
| 366 | } | ||
| 367 | |||
| 368 | private var texts: [Key: GraphicsContext.ResolvedText] = [:] | ||
| 369 | private var environment: TextEnvironment? | ||
| 370 | |||
| 371 | func resolve(_ text: String, color: Color, in context: GraphicsContext) -> GraphicsContext.ResolvedText { | ||
| 372 | let e = context.environment | ||
| 373 | let current = TextEnvironment(dynamicTypeSize: e.dynamicTypeSize, legibilityWeight: e.legibilityWeight, displayScale: e.displayScale) | ||
| 374 | if current != environment { | ||
| 375 | texts = [:] | ||
| 376 | environment = current | ||
| 377 | } | ||
| 378 | let key = Key(text: text, color: color) | ||
| 379 | if let t = texts[key] { return t } | ||
| 380 | let t = context.resolve(Text(text).font(.caption2).foregroundStyle(color)) | ||
| 381 | texts[key] = t | ||
| 382 | return t | ||
| 339 | } | 383 | } |
| 340 | } | 384 | } |
| 341 | 385 | ||
Packages/MoonriseKit/Tests/SkyRendererTests/SkyRenderTests.swift +35
| @@ -89,3 +89,38 @@ let evening = Date(timeIntervalSince1970: 1_792_202_400) // 2026-10-17T02:00:00Z | |||
| 89 | // The Norse Wolf's Mouth is the Hyades and includes Aldebaran (HR 1457). | 89 | // The Norse Wolf's Mouth is the Hyades and includes Aldebaran (HR 1457). |
| 90 | #expect(cultures.first { $0.id == "norse" }?.constellations.contains { $0.name == "Wolf's Mouth" && $0.lines.joined().contains(1457) } == true) | 90 | #expect(cultures.first { $0.id == "norse" }?.constellations.contains { $0.name == "Wolf's Mouth" && $0.lines.joined().contains(1457) } == true) |
| 91 | } | 91 | } |
| 92 | |||
| 93 | /// Figure labels are drawn exactly when some of the label falls on the canvas. | ||
| 94 | @MainActor | ||
| 95 | @Test func cultureLabelsDrawnAreTheOnScreenOnes() throws { | ||
| 96 | let scene = try SkyScene(date: evening, observer: chicago) | ||
| 97 | let culture = try #require(Catalog.skyCultures.first { $0.id == "chinese" }) | ||
| 98 | for (az, alt) in [(180.0, 45.0), (90, 30), (300, 70)] { | ||
| 99 | let view = SkyView(scene: scene, centerAzimuth: az, centerAltitude: alt, fieldOfView: 45, culture: culture) | ||
| 100 | var drawn: [String] = [], onScreen: [String] = [], labelled = 0 | ||
| 101 | let canvas = Canvas { context, size in | ||
| 102 | let p = Projection(centerAzimuth: az, centerAltitude: alt, fieldOfView: 45, size: size) | ||
| 103 | drawn = view.drawCulture(culture, &context, p) | ||
| 104 | for figure in culture.constellations { | ||
| 105 | var points: [CGPoint] = [] | ||
| 106 | for line in figure.lines { | ||
| 107 | for (a, b) in zip(line, line.dropFirst()) { | ||
| 108 | guard let ha = scene.starsByHR[a], let hb = scene.starsByHR[b], ha.altitude > -5 || hb.altitude > -5, | ||
| 109 | let pa = p.point(ha), let pb = p.point(hb) else { continue } | ||
| 110 | points += [pa, pb] | ||
| 111 | } | ||
| 112 | } | ||
| 113 | guard points.count > 1 else { continue } | ||
| 114 | labelled += 1 | ||
| 115 | let c = CGPoint(x: points.map(\.x).reduce(0, +) / Double(points.count), y: points.map(\.y).reduce(0, +) / Double(points.count)) | ||
| 116 | let s = context.resolve(Text(figure.name).font(.caption2)).measure(in: CGSize(width: CGFloat.infinity, height: .infinity)) | ||
| 117 | if CGRect(x: c.x - s.width / 2, y: c.y - s.height / 2, width: s.width, height: s.height).intersects(CGRect(origin: .zero, size: size)) { | ||
| 118 | onScreen.append(figure.name) | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | _ = ImageRenderer(content: canvas.frame(width: 393, height: 852)).cgImage | ||
| 123 | #expect(!drawn.isEmpty && drawn.count < labelled, "az \(az)") | ||
| 124 | #expect(drawn.sorted() == onScreen.sorted(), "az \(az)") | ||
| 125 | } | ||
| 126 | } | ||