|
| 1 | +#!/usr/bin/env swift |
| 2 | +// Regenerate the macOS icon without changing the shared source or Windows/Linux icons. |
| 3 | +import Foundation |
| 4 | +import CoreGraphics |
| 5 | +import ImageIO |
| 6 | + |
| 7 | +let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() |
| 8 | +let icons = root.appendingPathComponent("packages/app/src-tauri/icons") |
| 9 | + |
| 10 | +guard let source = CGImageSourceCreateWithURL(icons.appendingPathComponent("icon.png") as CFURL, nil), |
| 11 | + let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else { |
| 12 | + fatalError("Cannot read icons/icon.png") |
| 13 | +} |
| 14 | + |
| 15 | +// Keep the rounded-square artwork inside an 856 px square on a 1024 px canvas. |
| 16 | +// The transparent inset prevents it from looking oversized in the Dock. |
| 17 | +let representations = [ |
| 18 | + (16, 1, "icp4"), (16, 2, "ic11"), |
| 19 | + (32, 1, "icp5"), (32, 2, "ic12"), |
| 20 | + (128, 1, "ic07"), (128, 2, "ic13"), |
| 21 | + (256, 1, "ic08"), (256, 2, "ic14"), |
| 22 | + (512, 1, "ic09"), (512, 2, "ic10"), |
| 23 | +] |
| 24 | + |
| 25 | +func chunkHeader(_ type: String, _ length: Int) -> Data { |
| 26 | + var size = UInt32(length).bigEndian |
| 27 | + var header = Data(type.utf8) |
| 28 | + withUnsafeBytes(of: &size) { header.append(contentsOf: $0) } |
| 29 | + return header |
| 30 | +} |
| 31 | + |
| 32 | +var chunks = Data() |
| 33 | +var tableOfContents = Data() |
| 34 | +for (points, scale, type) in representations { |
| 35 | + let pixels = points * scale |
| 36 | + guard let context = CGContext( |
| 37 | + data: nil, width: pixels, height: pixels, bitsPerComponent: 8, bytesPerRow: pixels * 4, |
| 38 | + space: CGColorSpace(name: CGColorSpace.sRGB)!, |
| 39 | + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue |
| 40 | + ) else { fatalError("Cannot create \(pixels) px canvas") } |
| 41 | + let canvas = CGFloat(pixels) |
| 42 | + let artwork = (canvas * 856 / 1024).rounded() |
| 43 | + let inset = (canvas - artwork) / 2 |
| 44 | + context.clear(CGRect(x: 0, y: 0, width: canvas, height: canvas)) |
| 45 | + context.interpolationQuality = .high |
| 46 | + context.draw(image, in: CGRect(x: inset, y: inset, width: artwork, height: artwork)) |
| 47 | + |
| 48 | + let png = NSMutableData() |
| 49 | + guard let result = context.makeImage(), |
| 50 | + let destination = CGImageDestinationCreateWithData(png, "public.png" as CFString, 1, nil) else { |
| 51 | + fatalError("Cannot encode \(pixels) px icon") |
| 52 | + } |
| 53 | + CGImageDestinationAddImage(destination, result, nil) |
| 54 | + guard CGImageDestinationFinalize(destination) else { fatalError("Cannot write \(pixels) px icon") } |
| 55 | + let header = chunkHeader(type, png.length + 8) |
| 56 | + tableOfContents.append(header) |
| 57 | + chunks.append(header) |
| 58 | + chunks.append(png as Data) |
| 59 | +} |
| 60 | + |
| 61 | +// ICNS stores PNG representations in typed chunks with big-endian lengths. |
| 62 | +var output = chunkHeader("icns", 8 + 8 + tableOfContents.count + chunks.count) |
| 63 | +output.append(chunkHeader("TOC ", tableOfContents.count + 8)) |
| 64 | +output.append(tableOfContents) |
| 65 | +output.append(chunks) |
| 66 | +try output.write(to: icons.appendingPathComponent("icon.icns"), options: .atomic) |
| 67 | +print("Generated icons/icon.icns (856/1024 artwork, transparent padding)") |
0 commit comments