#!/usr/bin/env swift // Generates App/Resources/Assets.xcassets/AppIcon.appiconset from vector geometry. // // Run with `make icon` (or `swift scripts/generate-app-icon.swift`). PNGs are committed, so // `make verify` does not depend on this script. // // Family geometry follows Vorleser and Schriftleser: 1024 canvas, 824 opaque indigo tile, // transparent margin, soft shadow. The translation motif is two facing speech bubbles carrying // text bars; small sizes simplify to one bubble for legibility. import CoreGraphics import Foundation import ImageIO import UniformTypeIdentifiers private enum Design { static let canvas: CGFloat = 1024 static let tileInset: CGFloat = 100 static let tileSize = canvas - tileInset * 2 static let tileRadius = tileSize * 0.2245 // Same indigo family tile as Vorleser and Schriftleser. static let gradientTop = srgb(0x5B, 0x7C, 0xFA) static let gradientBottom = srgb(0x2E, 0x3F, 0xC7) static let glyph = srgb(0xFF, 0xFF, 0xFF) static let bar = srgb(0x2F, 0x3E, 0xC4) static func srgb(_ r: UInt8, _ g: UInt8, _ b: UInt8) -> CGColor { CGColor( colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, components: [CGFloat(r) / 255, CGFloat(g) / 255, CGFloat(b) / 255, 1] )! } } private struct Bubble { let rect: CGRect let radius: CGFloat let tail: [CGPoint] let bars: [CGFloat] let barHeight: CGFloat let barGap: CGFloat let fill: CGColor let barColor: CGColor } private struct Layout { let size: Int let scale: CGFloat let tile: CGRect let tileRadius: CGFloat let bubbles: [Bubble] let shadow: (offset: CGSize, blur: CGFloat, opacity: CGFloat)? init(size: Int) { self.size = size scale = CGFloat(size) / Design.canvas tile = CGRect(x: Design.tileInset, y: Design.tileInset, width: Design.tileSize, height: Design.tileSize) tileRadius = Design.tileRadius let simplified = size <= 32 let barThickness = Design.canvas * (simplified ? 0.075 : 0.045) let barGap = barThickness * (simplified ? 1.15 : 0.85) let tailStroke = Design.canvas * (simplified ? 0.05 : 0.030) if simplified { let width = tile.width * 0.76 let height = tile.height * 0.56 let rect = CGRect( x: tile.midX - width / 2, y: tile.minY + tile.height * 0.52 - height / 2, width: width, height: height ) let tail = [ CGPoint(x: rect.minX + width * 0.24, y: rect.minY + barThickness * 0.8), CGPoint(x: rect.minX + width * 0.10, y: rect.minY - height * 0.13), CGPoint(x: rect.minX + width * 0.44, y: rect.minY + barThickness * 0.8), ] bubbles = [ Bubble( rect: rect, radius: height * 0.26, tail: tail, bars: [width * 0.62, width * 0.40], barHeight: barThickness, barGap: barGap, fill: Design.glyph, barColor: Design.bar ), ] } else { let backWidth = tile.width * 0.56 let backHeight = tile.height * 0.33 let backRect = CGRect( x: tile.minX + tile.width * 0.06, y: tile.minY + tile.height * 0.34, width: backWidth, height: backHeight ) let backTail = [ CGPoint(x: backRect.minX + backWidth * 0.18, y: backRect.minY + tailStroke * 0.7), CGPoint(x: backRect.minX + backWidth * 0.02, y: backRect.minY - backHeight * 0.16), CGPoint(x: backRect.minX + backWidth * 0.40, y: backRect.minY + tailStroke * 0.7), ] let back = Bubble( rect: backRect, radius: backHeight * 0.26, tail: backTail, bars: [backWidth * 0.62, backWidth * 0.45, backWidth * 0.30], barHeight: barThickness, barGap: barGap, fill: Design.bar, barColor: Design.glyph ) let frontWidth = tile.width * 0.48 let frontHeight = tile.height * 0.30 let frontRect = CGRect( x: tile.maxX - tile.width * 0.06 - frontWidth, y: tile.minY + tile.height * 0.18, width: frontWidth, height: frontHeight ) let frontTail = [ CGPoint(x: frontRect.maxX - frontWidth * 0.18, y: frontRect.minY + tailStroke * 0.7), CGPoint(x: frontRect.maxX - frontWidth * 0.02, y: frontRect.minY - frontHeight * 0.16), CGPoint(x: frontRect.maxX - frontWidth * 0.40, y: frontRect.minY + tailStroke * 0.7), ] let front = Bubble( rect: frontRect, radius: frontHeight * 0.26, tail: frontTail, bars: [frontWidth * 0.62, frontWidth * 0.40], barHeight: barThickness, barGap: barGap, fill: Design.glyph, barColor: Design.bar ) bubbles = [back, front] } shadow = size >= 128 ? (CGSize(width: 0, height: -8), 26, 0.32) : nil } } private func roundedRect(_ rect: CGRect, _ radius: CGFloat) -> CGPath { CGPath(roundedRect: rect, cornerWidth: radius, cornerHeight: radius, transform: nil) } private func render(size: Int) -> CGImage? { let layout = Layout(size: size) let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)! guard let context = CGContext( data: nil, width: size, height: size, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue ) else { return nil } context.setAllowsAntialiasing(true) context.setShouldAntialias(true) context.interpolationQuality = .high context.clear(CGRect(x: 0, y: 0, width: size, height: size)) context.scaleBy(x: layout.scale, y: layout.scale) let tilePath = roundedRect(layout.tile, layout.tileRadius) context.saveGState() if let shadow = layout.shadow { context.setShadow( offset: shadow.offset, blur: shadow.blur, color: CGColor(colorSpace: colorSpace, components: [0, 0, 0, shadow.opacity]) ) } context.addPath(tilePath) context.setFillColor(Design.gradientBottom) context.fillPath() context.restoreGState() let gradient = CGGradient( colorsSpace: colorSpace, colors: [Design.gradientTop, Design.gradientBottom] as CFArray, locations: [0, 1] )! context.saveGState() context.addPath(tilePath) context.clip() context.drawLinearGradient( gradient, start: CGPoint(x: layout.tile.midX, y: layout.tile.maxY), end: CGPoint(x: layout.tile.midX, y: layout.tile.minY), options: [] ) context.restoreGState() for bubble in layout.bubbles { context.setFillColor(bubble.fill) context.beginPath() context.addLines(between: bubble.tail) context.closePath() context.fillPath() context.addPath(roundedRect(bubble.rect, bubble.radius)) context.fillPath() let totalHeight = CGFloat(bubble.bars.count) * bubble.barHeight + CGFloat(bubble.bars.count - 1) * bubble.barGap var y = bubble.rect.midY + totalHeight / 2 - bubble.barHeight let x = bubble.rect.minX + bubble.rect.width * 0.16 context.setFillColor(bubble.barColor) for width in bubble.bars { context.addPath(roundedRect( CGRect(x: x, y: y, width: width, height: bubble.barHeight), bubble.barHeight / 2 )) context.fillPath() y -= bubble.barHeight + bubble.barGap } } return context.makeImage() } private let slots: [(name: String, pixels: Int)] = [ ("icon_16x16.png", 16), ("icon_16x16@2x.png", 32), ("icon_32x32.png", 32), ("icon_32x32@2x.png", 64), ("icon_128x128.png", 128), ("icon_128x128@2x.png", 256), ("icon_256x256.png", 256), ("icon_256x256@2x.png", 512), ("icon_512x512.png", 512), ("icon_512x512@2x.png", 1024), ] private let contents = """ { "images" : [ { "filename" : "icon_16x16.png", "idiom" : "mac", "scale" : "1x", "size" : "16x16" }, { "filename" : "icon_16x16@2x.png", "idiom" : "mac", "scale" : "2x", "size" : "16x16" }, { "filename" : "icon_32x32.png", "idiom" : "mac", "scale" : "1x", "size" : "32x32" }, { "filename" : "icon_32x32@2x.png", "idiom" : "mac", "scale" : "2x", "size" : "32x32" }, { "filename" : "icon_128x128.png", "idiom" : "mac", "scale" : "1x", "size" : "128x128" }, { "filename" : "icon_128x128@2x.png", "idiom" : "mac", "scale" : "2x", "size" : "128x128" }, { "filename" : "icon_256x256.png", "idiom" : "mac", "scale" : "1x", "size" : "256x256" }, { "filename" : "icon_256x256@2x.png", "idiom" : "mac", "scale" : "2x", "size" : "256x256" }, { "filename" : "icon_512x512.png", "idiom" : "mac", "scale" : "1x", "size" : "512x512" }, { "filename" : "icon_512x512@2x.png", "idiom" : "mac", "scale" : "2x", "size" : "512x512" } ], "info" : { "author" : "xcode", "version" : 1 } } """ private let assetCatalogContents = """ { "info" : { "author" : "xcode", "version" : 1 } } """ private func writePNG(_ image: CGImage, to url: URL) throws { guard let destination = CGImageDestinationCreateWithURL( url as CFURL, UTType.png.identifier as CFString, 1, nil ) else { throw NSError(domain: "icon", code: 1, userInfo: [NSLocalizedDescriptionKey: "cannot write \(url.path)"]) } CGImageDestinationAddImage(destination, image, nil) guard CGImageDestinationFinalize(destination) else { throw NSError(domain: "icon", code: 2, userInfo: [NSLocalizedDescriptionKey: "cannot finish \(url.path)"]) } } private func writePreview(to url: URL) throws { let width = 1240 let height = 640 let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)! guard let context = CGContext( data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue ) else { return } context.setFillColor(Design.srgb(0xEC, 0xEC, 0xEE)) context.fill(CGRect(x: 0, y: 0, width: width, height: height / 2)) context.setFillColor(Design.srgb(0x1C, 0x1C, 0x1E)) context.fill(CGRect(x: 0, y: height / 2, width: width, height: height / 2)) var x = 40 for size in [16, 32, 64, 128, 256] { if let image = render(size: size) { context.draw(image, in: CGRect(x: x, y: height / 2 + 40, width: size, height: size)) context.draw(image, in: CGRect(x: x, y: 40, width: size, height: size)) } x += size + 50 } x += 20 for size in [16, 32] { guard let image = render(size: size) else { continue } let scale = 8 let side = size * scale context.saveGState() context.interpolationQuality = .none context.draw(image, in: CGRect(x: x, y: height / 2 - side / 2, width: side, height: side)) context.restoreGState() x += side + 40 } guard let image = context.makeImage() else { return } try writePNG(image, to: url) } private let root = URL(fileURLWithPath: #filePath) .deletingLastPathComponent() .deletingLastPathComponent() private let assetCatalog = root .appendingPathComponent("App/Resources/Assets.xcassets", isDirectory: true) private let iconSet = assetCatalog.appendingPathComponent("AppIcon.appiconset", isDirectory: true) do { try FileManager.default.createDirectory(at: iconSet, withIntermediateDirectories: true) try assetCatalogContents.write( to: assetCatalog.appendingPathComponent("Contents.json"), atomically: true, encoding: .utf8 ) try contents.write(to: iconSet.appendingPathComponent("Contents.json"), atomically: true, encoding: .utf8) for slot in slots { guard let image = render(size: slot.pixels) else { throw NSError(domain: "icon", code: 3, userInfo: [NSLocalizedDescriptionKey: "render failed at \(slot.pixels)"]) } try writePNG(image, to: iconSet.appendingPathComponent(slot.name)) } let previewDirectory = root.appendingPathComponent(".build", isDirectory: true) try FileManager.default.createDirectory(at: previewDirectory, withIntermediateDirectories: true) try writePreview(to: previewDirectory.appendingPathComponent("icon-preview.png")) print("wrote \(slots.count) icons to \(iconSet.path)") print("preview: \(previewDirectory.appendingPathComponent("icon-preview.png").path)") } catch { FileHandle.standardError.write(Data("error: \(error.localizedDescription)\n".utf8)) exit(1) }