scripts/make-icon.swift
Swift source for Making faces, 75 lines. Raw file
#!/usr/bin/env swift
//
// make-icon.swift — scale Resources/AppIconSource.png to every macOS size.
// Family artwork is rendered by Tools/Icons/render_family.swift.
import AppKit
import Foundation
let root = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
let sourceURL = root.appendingPathComponent("Resources/AppIconSource.png")
let iconset = root.appendingPathComponent("build/AppIcon.iconset")
guard let source = NSImage(contentsOf: sourceURL) else {
fputs("missing \(sourceURL.path)\n", stderr)
exit(1)
}
try FileManager.default.createDirectory(at: iconset, withIntermediateDirectories: true)
struct Spec {
let name: String
let pixels: Int
}
let specs: [Spec] = [
Spec(name: "icon_16x16.png", pixels: 16),
Spec(name: "icon_16x16@2x.png", pixels: 32),
Spec(name: "icon_32x32.png", pixels: 32),
Spec(name: "icon_32x32@2x.png", pixels: 64),
Spec(name: "icon_128x128.png", pixels: 128),
Spec(name: "icon_128x128@2x.png", pixels: 256),
Spec(name: "icon_256x256.png", pixels: 256),
Spec(name: "icon_256x256@2x.png", pixels: 512),
Spec(name: "icon_512x512.png", pixels: 512),
Spec(name: "icon_512x512@2x.png", pixels: 1024),
]
func render(pixels: Int) -> NSBitmapImageRep {
let rep = NSBitmapImageRep(
bitmapDataPlanes: nil,
pixelsWide: pixels,
pixelsHigh: pixels,
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: .deviceRGB,
bytesPerRow: 0,
bitsPerPixel: 0
)!
rep.size = NSSize(width: pixels, height: pixels)
NSGraphicsContext.saveGraphicsState()
let ctx = NSGraphicsContext(bitmapImageRep: rep)!
ctx.imageInterpolation = .high
ctx.shouldAntialias = true
NSGraphicsContext.current = ctx
source.draw(
in: NSRect(x: 0, y: 0, width: pixels, height: pixels),
from: NSRect(origin: .zero, size: source.size),
operation: .copy,
fraction: 1)
NSGraphicsContext.restoreGraphicsState()
return rep
}
for spec in specs {
let rep = render(pixels: spec.pixels)
guard let data = rep.representation(using: .png, properties: [:]) else {
fputs("failed to encode \(spec.name)\n", stderr)
exit(1)
}
try data.write(to: iconset.appendingPathComponent(spec.name))
}
print("wrote \(iconset.path)")