122 lines · 4.9 KB · SwiftRaw
// The faces' fixture pictures: invented photographs cut from the design's// own references and two invented PDFs (`Resources/Fixtures/cards/`), which// the specimens show, the Loom app and the face shots draw, and the app's// gallery hands its staged faces. Every name and fact in them is invented.//// Read through `bundle`, never `Bundle.module` directly: the Mac's built// app finds the SwiftPM resource bundle by path, as `CottonValuesResources`// does.import Foundationimport SwiftUI#if canImport(UIKit) import UIKit#else import AppKit#endifpublic enum FixturePictures { /// The SwiftPM resource bundle's name: `<package>_<target>.bundle`. static let bundleName = "Loom_CottonFaces.bundle" /// This module's resource bundle, wherever this build put it — or nil /// when this build has none to find. static let bundle: Bundle? = { #if canImport(UIKit) return Bundle.module #else return locate(from: Bundle.main) ?? locate(from: Bundle(for: Marker.self)) #endif }() private final class Marker {} static func locate(from main: Bundle) -> Bundle? { var candidates: [URL] = [] if let resources = main.resourceURL { candidates.append(resources.appendingPathComponent(bundleName)) } var directory = main.bundleURL for _ in 0..<4 { candidates.append(directory.appendingPathComponent(bundleName)) directory.deleteLastPathComponent() } for candidate in candidates where FileManager.default.fileExists(atPath: candidate.path) { if let found = Bundle(url: candidate) { return found } } return nil } /// A fixture file by its name and extension (`bill`, `pdf`). public static func url(_ name: String, _ ext: String) -> URL? { bundle?.url(forResource: name, withExtension: ext, subdirectory: "Fixtures/cards") } /// A fixture photograph by name (`verandah`), nil when there is none. public static func image(_ name: String) -> Image? { #if canImport(UIKit) guard let url = url(name, "jpg"), let image = UIImage(contentsOfFile: url.path) else { return nil } return Image(uiImage: image) #else guard let url = url(name, "jpg"), let image = NSImage(contentsOf: url) else { return nil } return Image(nsImage: image) #endif } /// A fixture photograph's shape, its width over its height, read off /// its file once (an image made from a file is not decoded until it is /// drawn); nil for a key with none. public static let aspect: FacePictureAspect = { key in if let kept = shapes[key] { return kept } guard let url = url(key, "jpg") else { return nil } #if canImport(UIKit) guard let size = UIImage(contentsOfFile: url.path)?.size else { return nil } #else guard let size = NSImage(contentsOf: url)?.size else { return nil } #endif guard size.width > 0, size.height > 0 else { return nil } shapes[key] = size.width / size.height return size.width / size.height } @MainActor private static var shapes: [String: CGFloat] = [:] /// A fixture photograph's colour, the mean of its pixels: the file drawn /// once into one pixel, kept; nil for a key with none. public static let tint: FacePictureTint = { key in if let kept = means[key] { return kept } guard let url = url(key, "jpg") else { return nil } #if canImport(UIKit) guard let picture = UIImage(contentsOfFile: url.path)?.cgImage else { return nil } #else guard let picture = NSImage(contentsOf: url)?.cgImage(forProposedRect: nil, context: nil, hints: nil) else { return nil } #endif var pixel = [UInt8](repeating: 0, count: 4) guard let context = CGContext( data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } context.interpolationQuality = .medium context.draw(picture, in: CGRect(x: 0, y: 0, width: 1, height: 1)) let mean = Color(red: Double(pixel[0]) / 255, green: Double(pixel[1]) / 255, blue: Double(pixel[2]) / 255) means[key] = mean return mean } @MainActor private static var means: [String: Color] = [:] /// A `FacePicture` drawn from the fixtures: the photograph filling its /// frame, as a deck's picture does; nothing for a key with none. public static let drawer: FacePictureDrawer = { key, _ in AnyView(Fixture(name: key)) } /// A fixture photograph filling its frame. private struct Fixture: View { let name: String var body: some View { if let image = FixturePictures.image(name) { image.resizable().interpolation(.high).scaledToFill() } } }}