Rasterize SVG icons once in background on Mac, so views can draw bitmap (fast) instead of the vector.

This commit is contained in:
Brent Simmons committed 2026-11-03 16:18:49 -08:00
1 parent b24d552c81
commit 03d7367772
3 files changed
+71 -17

No files matched your search

@@ -510,7 +510,7 @@ extension DownloadedImageCache {
return files
}
/// The original data when it’s already small enough, or can’t be scaled, such as SVG.
/// The original data when it’s already small enough or can’t be read.
@concurrent static func scaledIcon(from data: Data) async -> Data {
RSImage.scaledImageData(data, maxPixelSize: RSImage.maxIconPixelSize) ?? data
}
+51 -16
View File
@@ -194,16 +194,18 @@ public extension RSImage {
/// Scales image data down to maxPixelSize and returns the result encoded as PNG.
/// If the image is a single frame already at or below maxPixelSize, returns
/// the original data unchanged. For multi-image sources (like ICO), extracts
/// the largest frame. Returns nil if the data can't be decoded as an image.
/// the largest frame. On the Mac, an image ImageIO can’t read, such as SVG, is rasterized
/// so its longer side is maxPixelSize. Returns nil if the data can't be decoded as an image.
static func scaledImageData(_ data: Data, maxPixelSize: Int) -> Data? {
guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil) else {
guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil), let largestFrame = largestFrame(in: imageSource) else {
#if os(macOS)
return rasterizedVectorImageData(data, maxPixelSize: maxPixelSize)
#else
return nil
#endif
}
let count = CGImageSourceGetCount(imageSource)
guard let largestFrame = largestFrame(in: imageSource) else {
return nil
}
let largestIndex = largestFrame.index
let largestWidth = largestFrame.width
let largestHeight = largestFrame.height
@@ -231,16 +233,7 @@ public extension RSImage {
]
let cgImage = CGImageSourceCreateThumbnailAtIndex(imageSource, largestIndex, options as CFDictionary)
guard let cgImage else {
return nil
}
guard let mutableData = CFDataCreateMutable(nil, 0),
let destination = CGImageDestinationCreateWithData(mutableData, UTType.png.identifier as CFString, 1, nil) else {
return nil
}
CGImageDestinationAddImage(destination, cgImage, nil)
guard CGImageDestinationFinalize(destination) else {
guard let cgImage, let scaledData = pngData(cgImage) else {
return nil
}
@@ -250,6 +243,48 @@ public extension RSImage {
RSImageLogger.info("Extracted largest frame (\(largestWidth, privacy: .public)x\(largestHeight, privacy: .public)) from multi-image source (\(count, privacy: .public) frames)")
}
return mutableData as Data
return scaledData
}
}
private extension RSImage {
static func pngData(_ cgImage: CGImage) -> Data? {
guard let mutableData = CFDataCreateMutable(nil, 0), let destination = CGImageDestinationCreateWithData(mutableData, UTType.png.identifier as CFString, 1, nil) else {
return nil
}
CGImageDestinationAddImage(destination, cgImage, nil)
guard CGImageDestinationFinalize(destination) else {
return nil
}
return mutableData as Data
}
#if os(macOS)
static let rasterizedBitsPerComponent = 8
/// Draws a vector image, such as SVG, into a bitmap, so views don’t rasterize it on the main thread each time they draw.
static func rasterizedVectorImageData(_ data: Data, maxPixelSize: Int) -> Data? {
guard let image = NSImage(data: data), image.size.width > 0, image.size.height > 0 else {
return nil
}
let scale = CGFloat(maxPixelSize) / max(image.size.width, image.size.height)
let width = max(1, Int((image.size.width * scale).rounded()))
let height = max(1, Int((image.size.height * scale).rounded()))
guard let colorSpace = CGColorSpace(name: CGColorSpace.sRGB), let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: rasterizedBitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
return nil
}
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
image.draw(in: NSRect(x: 0, y: 0, width: width, height: height))
NSGraphicsContext.restoreGraphicsState()
guard let cgImage = context.makeImage(), let rasterizedData = pngData(cgImage) else {
return nil
}
RSImageLogger.info("Rasterized vector image at \(width, privacy: .public)x\(height, privacy: .public)")
return rasterizedData
}
#endif
}
@@ -32,10 +32,29 @@ import UniformTypeIdentifiers
#expect(RSImage.largestFramePixelSize(scaledData)?.width == 288)
#expect(RSImage.largestFramePixelSize(scaledData)?.height == 216)
}
#if os(macOS)
@Test func svgIsRasterizedToPNG() throws {
let scaledData = try #require(RSImage.scaledImageData(svgData, maxPixelSize: 64))
#expect(imageType(scaledData) == UTType.png.identifier)
#expect(RSImage.largestFramePixelSize(scaledData)?.width == 64)
#expect(RSImage.largestFramePixelSize(scaledData)?.height == 32)
}
@Test func svgRasterizingIsDeterministic() throws {
let scaledData = try #require(RSImage.scaledImageData(svgData, maxPixelSize: 64))
let scaledAgainData = try #require(RSImage.scaledImageData(svgData, maxPixelSize: 64))
#expect(scaledData == scaledAgainData)
}
#endif
}
// MARK: - Helpers
private let svgData = Data(##"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100"><rect width="200" height="100" fill="#336699"/><circle cx="100" cy="50" r="40" fill="#ffcc00"/></svg>"##.utf8)
private func pngData(width: Int, height: Int) -> Data? {
imageData(width: width, height: height, type: UTType.png, embedsThumbnail: false)
}