184 lines · 10.3 KB · SwiftRaw
// THE DRAWN PICTURE (CONTRACT-2, "The drawn picture"): a title with no picture, or one still loading or that failed,// is drawn from the core's `Item.art {a, b, shape}`: a 165° ground from `a` to `b`, one of nine shapes in `b`, a foot// shadow and a hairline, and on a poster its name. Every size is a share of the picture's width, so it scales.#if os(iOS)import SwiftUI/// The core's choice for a title's drawn picture.struct CinemasArt: Decodable, Equatable, Hashable, Sendable { var a: String var b: String var shape: String /// The art for a title the core sent none for (an older core): the same title draws the same art every time. static func standIn(_ key: String) -> CinemasArt { let hash = key.utf8.reduce(UInt64(0xcbf2_9ce4_8422_2325)) { ($0 ^ UInt64($1)) &* 0x0100_0000_01b3 } let pair = pairs[Int(hash % UInt64(pairs.count))] return CinemasArt(a: pair.0, b: pair.1, shape: shapes[Int((hash >> 32) % UInt64(shapes.count))]) } static let shapes = ["sun", "hills", "stripe", "ring", "rain", "horizon", "kite", "lines", "arch"] private static let pairs: [(String, String)] = [ ("#14302e", "#d99a4e"), ("#3b1342", "#f27b5c"), ("#0e1c28", "#7f9f7a"), ("#0a0e28", "#6a8be0"), ("#2b1a12", "#d8b47c"), ("#0c2828", "#46b0a8"), ("#0f1f2e", "#95bfd8"), ("#1a0c05", "#ff9d40"), ("#05202a", "#33b4cc"), ("#0f2230", "#cfe0ea"), ]}extension Color { /// "#rrggbb", or ink when it is not one. init(cinemasHex hex: String) { let digits = hex.trimmingCharacters(in: CharacterSet(charactersIn: "#")) guard digits.count == 6, let value = UInt32(digits, radix: 16) else { self = CinemasInk.raised return } self = Color(red: Double((value >> 16) & 0xff) / 255, green: Double((value >> 8) & 0xff) / 255, blue: Double(value & 0xff) / 255) }}/// The drawn picture, at any size: a poster carries its name; a backdrop, a featured title or a still does not.struct CinemasDrawn: View { let art: CinemasArt var name: String? var body: some View { GeometryReader { box in let w = box.size.width.drawableOrZero, h = box.size.height.drawableOrZero let a = Color(cinemasHex: art.a), b = Color(cinemasHex: art.b) let wide = w > h ZStack(alignment: .bottomLeading) { // The ground: 165° from a at 15% to b at 130%. LinearGradient(stops: [.init(color: a, location: 0.15), .init(color: b, location: 1.3)], startPoint: UnitPoint(x: 0.37, y: 0), endPoint: UnitPoint(x: 0.63, y: 1)) Canvas { context, size in Self.shape(art.shape, in: &context, size: size, b: b) } // The foot shadow (inner, from the foot up), then the hairline. LinearGradient(colors: [.clear, .black.opacity(0.6)], startPoint: UnitPoint(x: 0.5, y: 0.5), endPoint: .bottom) Rectangle().strokeBorder(Color.white.opacity(0.06), lineWidth: 1) if let name, w > 0 { Text(Self.breakable(name).uppercased()) .font(CinemasFonts.display(max(8, w * (wide ? 0.08 : 0.14)), black: false)) .tracking(w * (wide ? 0.08 : 0.14) * 0.01) // Tight lines for Latin letters; Telugu, Hindi and the like keep their marks above and below. .lineSpacing(Self.tall(name) ? 0 : -w * (wide ? 0.08 : 0.14) * 0.08) .foregroundStyle(.white) .multilineTextAlignment(.leading) .lineLimit(3) .minimumScaleFactor(0.5) .padding(.leading, w * (wide ? 0.05 : 0.08)) .padding(.trailing, w * (wide ? 0.05 : 0.08)) .padding(.bottom, w * (wide ? 0.06 : 0.09)) } } .frame(width: w, height: h) } } /// A name written beyond Latin, Greek and Cyrillic, whose letters reach above and below the line. static func tall(_ name: String) -> Bool { name.unicodeScalars.contains { $0.value > 0x052F && !(0x2000...0x206F).contains($0.value) && $0.properties.isAlphabetic } } /// A file's name ("glass.harbour.1080p") breaks at its dots and underscores, so it wraps instead of shrinking. static func breakable(_ name: String) -> String { name.replacingOccurrences(of: ".", with: ".\u{200B}").replacingOccurrences(of: "_", with: "_\u{200B}") } private static func shape(_ shape: String, in context: inout GraphicsContext, size: CGSize, b: Color) { let w = size.width, h = size.height func rect(_ x: CGFloat, _ y: CGFloat, _ rw: CGFloat, _ rh: CGFloat) -> CGRect { CGRect(x: w * x, y: h * y, width: w * rw, height: h * rh) } switch shape { case "sun": let d = w * 0.62 let circle = CGRect(x: w * 0.19, y: h * 0.14, width: d, height: d) context.fill(Path(ellipseIn: circle), with: .radialGradient( Gradient(stops: [.init(color: b, location: 0), .init(color: b, location: 0.45), .init(color: b.opacity(0), location: 0.7)]), center: CGPoint(x: circle.midX, y: circle.midY), startRadius: 0, endRadius: d / 2)) case "hills": var band = Path() let top = h * 0.48 func at(_ x: CGFloat, _ y: CGFloat) -> CGPoint { CGPoint(x: w * x, y: top + (h - top) * y) } band.move(to: at(0, 0.38)); band.addLine(to: at(0.26, 0.12)); band.addLine(to: at(0.48, 0.40)) band.addLine(to: at(0.74, 0)); band.addLine(to: at(1, 0.28)); band.addLine(to: CGPoint(x: w, y: h)); band.addLine(to: CGPoint(x: 0, y: h)) band.closeSubpath() context.fill(band, with: .color(.black.opacity(0.45))) let d = w * 0.16 context.fill(Path(ellipseIn: CGRect(x: w * 0.58, y: h * 0.12, width: d, height: d)), with: .color(b.opacity(0.9))) case "stripe": var stripes = context stripes.rotate(by: .degrees(45)) let step = max(w, h) * 0.13 var x = -max(w, h) * 2 while x < max(w, h) * 2 { stripes.fill(Path(CGRect(x: x + step * 0.69, y: -max(w, h) * 2, width: step * 0.31, height: max(w, h) * 4)), with: .color(.white.opacity(0.1))) x += step } var card = context let r = rect(0.2, 0.16, 0.6, 0.3) card.translateBy(x: r.midX, y: r.midY) card.rotate(by: .degrees(-6)) card.fill(Path(roundedRect: CGRect(x: -r.width / 2, y: -r.height / 2, width: r.width, height: r.height), cornerRadius: 4), with: .color(b.opacity(0.85))) case "ring": let d = w * 0.58 context.stroke(Path(ellipseIn: CGRect(x: w * 0.21, y: h * 0.13, width: d, height: d)), with: .color(b), lineWidth: w * 0.035) let dot = w * 0.16 context.fill(Path(ellipseIn: CGRect(x: w * 0.42, y: h * 0.30, width: dot, height: dot)), with: .color(b)) case "rain": var rain = context rain.rotate(by: .degrees(10)) let step = w * 0.068 var x = -w while x < w * 2 { rain.fill(Path(CGRect(x: x + step * 0.88, y: -h, width: max(0.5, step * 0.12), height: h * 3)), with: .color(.white.opacity(0.13))) x += step } for (i, opacity) in [(0, 1.0), (1, 0.25), (2, 0.12)] { let y = h * 0.20 + w * 0.06 * CGFloat(i) context.fill(Path(CGRect(x: w * 0.12, y: y, width: w * 0.76, height: 2)), with: .color(i == 0 ? b : .white.opacity(opacity))) } case "horizon": let top = h * 0.46 context.fill(Path(CGRect(x: 0, y: top, width: w, height: h - top)), with: .linearGradient(Gradient(colors: [.black.opacity(0.15), .black.opacity(0.55)]), startPoint: CGPoint(x: 0, y: top), endPoint: CGPoint(x: 0, y: h))) context.fill(Path(CGRect(x: 0, y: top, width: w, height: 2)), with: .color(b)) var half = Path() let disc = CGRect(x: w * 0.30, y: h * 0.24, width: w * 0.40, height: w * 0.40) half.addArc(center: CGPoint(x: disc.midX, y: disc.midY), radius: disc.width / 2, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false) half.closeSubpath() context.fill(half, with: .color(b.opacity(0.9))) case "kite": let side = w * 0.38 let centre = CGPoint(x: w * 0.31 + side / 2, y: h * 0.12 + side / 2) var kite = context kite.translateBy(x: centre.x, y: centre.y) kite.rotate(by: .degrees(45)) let square = Path(CGRect(x: -side / 2, y: -side / 2, width: side, height: side)) kite.fill(square, with: .linearGradient(Gradient(colors: [b, .white.opacity(0.2)]), startPoint: CGPoint(x: -side / 2, y: -side / 2), endPoint: CGPoint(x: side / 2, y: side / 2))) kite.stroke(square, with: .color(.white.opacity(0.12)), lineWidth: w * 0.015) var string = context string.translateBy(x: centre.x, y: h * 0.44) string.rotate(by: .degrees(12)) string.fill(Path(CGRect(x: 0, y: 0, width: 1, height: h * 0.26)), with: .color(.white.opacity(0.5))) case "lines": // From the top 10%, a band 42% of the height: a stripe in b from 9% to 11% of every 11%. let top = h * 0.10, band = h * 0.42, period = h * 0.11 var y = top while y < top + band { context.fill(Path(CGRect(x: 0, y: y + period * (0.09 / 0.11), width: w, height: period * (0.02 / 0.11))), with: .color(b.opacity(0.7))) y += period } default: // arch let r = rect(0.22, 0.12, 0.56, 0.44) var arch = Path() arch.move(to: CGPoint(x: r.minX, y: r.maxY)) arch.addLine(to: CGPoint(x: r.minX, y: r.minY + r.width / 2)) arch.addArc(center: CGPoint(x: r.midX, y: r.minY + r.width / 2), radius: r.width / 2, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false) arch.addLine(to: CGPoint(x: r.maxX, y: r.maxY)) context.stroke(arch, with: .color(b), lineWidth: w * 0.03) context.fill(Path(rect(0.44, 0.34, 0.12, 0.22)), with: .color(b.opacity(0.7))) } }}#endif