generate-app-icons.swift
scripts/generate-app-icons.swift
1// Resize the yōkan default icon and draw the optional Torii artwork on macOS:2// swift -module-cache-path /private/tmp/yokan-icon-cache scripts/generate-app-icons.swift3// The original default artwork is kept in packaging/icons/yokan-source.png.4import AppKit5import Foundation67let root = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)8func writePNG(_ path: String, size: Int, draw: (CGContext) -> Void) throws {9 let context = CGContext(data: nil, width: size, height: size, bitsPerComponent: 8,10 bytesPerRow: size * 4, space: CGColorSpace(name: CGColorSpace.sRGB)!,11 bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!12 draw(context)13 let bitmap = NSBitmapImageRep(cgImage: context.makeImage()!)14 let target = root.appendingPathComponent(path)15 try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)16 try bitmap.representation(using: .png, properties: [:])!.write(to: target)17}18func color(_ hex: UInt32) -> CGColor {19 CGColor(colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!,20 components: [CGFloat((hex >> 16) & 255) / 255, CGFloat((hex >> 8) & 255) / 255,21 CGFloat(hex & 255) / 255, 1])!22}23let primary = NSImage(contentsOf: root.appendingPathComponent("packaging/icons/yokan-source.png"))!24let original = primary.cgImage(forProposedRect: nil, context: nil, hints: nil)!25func originalPNG(_ path: String, _ size: Int) throws {26 try writePNG(path, size: size) { context in27 context.setFillColor(color(0xFFFFFF)); context.fill(CGRect(x: 0, y: 0, width: size, height: size))28 context.interpolationQuality = .high29 context.draw(original, in: CGRect(x: 0, y: 0, width: size, height: size))30 }31}32func toriiPNG(_ path: String, background: UInt32, foreground: UInt32) throws {33 try writePNG(path, size: 1024) { context in34 context.scaleBy(x: 10.24, y: 10.24)35 context.translateBy(x: 0, y: 100); context.scaleBy(x: 1, y: -1)36 context.setFillColor(color(background)); context.fill(CGRect(x: 0, y: 0, width: 100, height: 100))37 context.setFillColor(color(foreground))38 let beam = CGMutablePath()39 beam.move(to: CGPoint(x: 22, y: 26)); beam.addQuadCurve(to: CGPoint(x: 78, y: 26), control: CGPoint(x: 50, y: 34))40 beam.addLine(to: CGPoint(x: 78, y: 34)); beam.addQuadCurve(to: CGPoint(x: 22, y: 34), control: CGPoint(x: 50, y: 42)); beam.closeSubpath()41 context.addPath(beam); context.fillPath()42 for rect in [CGRect(x: 32, y: 37, width: 8, height: 41), CGRect(x: 60, y: 37, width: 8, height: 41),43 CGRect(x: 26, y: 44, width: 48, height: 6), CGRect(x: 47, y: 52, width: 6, height: 12)] {44 context.fill(rect)45 }46 }47}48let catalog = "iosApp/Assets.xcassets"49try originalPNG("composeApp/src/commonMain/composeResources/drawable/icon_red.png", 1024)50try originalPNG("composeApp/src/commonMain/composeResources/drawable/mangayomi_icon.png", 1024)51try originalPNG("composeApp/src/androidMain/res/mipmap-anydpi/ic_launcher.png", 192)52try originalPNG("\(catalog)/AppIcon.appiconset/icon.png", 1024)53try toriiPNG("\(catalog)/ToriiLight.appiconset/icon.png", background: 0xFFF6EB, foreground: 0xB81932)54try toriiPNG("\(catalog)/ToriiDark.appiconset/icon.png", background: 0x211B24, foreground: 0xFFBBC4)55try toriiPNG("\(catalog)/ToriiSystem.appiconset/icon.png", background: 0xFFF6EB, foreground: 0xB81932)56try toriiPNG("\(catalog)/ToriiSystem.appiconset/dark.png", background: 0x211B24, foreground: 0xFFBBC4)57try toriiPNG("\(catalog)/ToriiSystem.appiconset/tinted.png", background: 0x202020, foreground: 0xCCCCCC)58try originalPNG("composeApp/src/wasmJsMain/resources/favicon.png", 192)59try originalPNG("packaging/icons/yokan.png", 256)60// ICO container with one PNG image. Windows/jpackage accept the standard compressed image format.61let png = try Data(contentsOf: root.appendingPathComponent("packaging/icons/yokan.png"))62var ico = Data([0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 24, 0])63func appendLE(_ value: UInt32) { ico.append(contentsOf: (0..<4).map { UInt8((value >> ($0 * 8)) & 255) }) }64appendLE(UInt32(png.count)); appendLE(22); ico.append(png)65try ico.write(to: root.appendingPathComponent("packaging/icons/yokan.ico"))66// Temporary macOS iconset source for the ICNS container below.67for size in [16, 32, 128, 256, 512] {68 try originalPNG("packaging/icons/Yokan.iconset/icon_\(size)x\(size).png", size)69 try originalPNG("packaging/icons/Yokan.iconset/icon_\(size)x\(size)@2x.png", size * 2)70}71// An ICNS container can embed standard PNG chunks directly; no iconutil service is needed.72var icnsBody = Data()73for (type, file) in [("icp4", "icon_16x16.png"), ("icp5", "icon_32x32.png"), ("icp6", "icon_32x32@2x.png"),74 ("ic07", "icon_128x128.png"), ("ic08", "icon_256x256.png"),75 ("ic09", "icon_512x512.png"), ("ic10", "icon_512x512@2x.png")] {76 let data = try Data(contentsOf: root.appendingPathComponent("packaging/icons/Yokan.iconset/\(file)"))77 icnsBody.append(contentsOf: type.utf8)78 let length = UInt32(data.count + 8)79 icnsBody.append(contentsOf: (0..<4).reversed().map { UInt8((length >> ($0 * 8)) & 255) })80 icnsBody.append(data)81}82var icns = Data("icns".utf8)83let icnsLength = UInt32(icnsBody.count + 8)84icns.append(contentsOf: (0..<4).reversed().map { UInt8((icnsLength >> ($0 * 8)) & 255) })85icns.append(icnsBody)86try icns.write(to: root.appendingPathComponent("packaging/icons/Yokan.icns"))87try FileManager.default.removeItem(at: root.appendingPathComponent("packaging/icons/Yokan.iconset"))88