#!/usr/bin/swift // // rasterize-svg.swift // Zyquo Cloud // // Author: Simon-Pierre Boucher // Mail: contact@spboucher.ai // // Renders an SVG to PNG at a given pixel size using AppKit's native SVG // support (CoreSVG) — used because librsvg isn't installed on this machine. // Usage: swift rasterize-svg.swift // import AppKit let args = CommandLine.arguments guard args.count == 4, let size = Int(args[3]) else { FileHandle.standardError.write(Data("usage: rasterize-svg.swift \n".utf8)) exit(64) } guard let image = NSImage(contentsOfFile: args[1]) else { FileHandle.standardError.write(Data("error: cannot load \(args[1])\n".utf8)) exit(1) } let pixels = CGFloat(size) guard let rep = NSBitmapImageRep( bitmapDataPlanes: nil, pixelsWide: size, pixelsHigh: size, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB, bytesPerRow: 0, bitsPerPixel: 0 ) else { exit(1) } NSGraphicsContext.saveGraphicsState() NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep) NSGraphicsContext.current?.imageInterpolation = .high image.draw( in: NSRect(x: 0, y: 0, width: pixels, height: pixels), from: .zero, operation: .copy, fraction: 1 ) NSGraphicsContext.restoreGraphicsState() guard let png = rep.representation(using: .png, properties: [:]) else { exit(1) } try! png.write(to: URL(fileURLWithPath: args[2]))