diff --git a/Sources/AssetKit/AppIcon/AppIconPlist.swift b/Sources/AssetKit/AppIcon/AppIconPlist.swift index 9a4f6b2..21c0fb3 100644 --- a/Sources/AssetKit/AppIcon/AppIconPlist.swift +++ b/Sources/AssetKit/AppIcon/AppIconPlist.swift @@ -28,7 +28,7 @@ enum AppIconPlistEmitter { guard FileManager.default.fileExists(atPath: src.path) else { throw XCAssetCompilerError.missingReferencedFile(asset: appIcon.name, filename: filename) } - guard let (w, _) = image.pointSize, let scale = image.scale?.factor else { + guard let (w, _) = image.pointSize, w > 0, let scale = image.scale?.factor else { throw XCAssetCompilerError.appIconSizeMissing(asset: appIcon.name, size: image.size) } let bundleName = "\(appIcon.name)\(formatSize(w))x\(formatSize(w))" diff --git a/Sources/AssetKit/CAR/BitmapKeys.swift b/Sources/AssetKit/CAR/BitmapKeys.swift index d3e31b4..d413364 100644 --- a/Sources/AssetKit/CAR/BitmapKeys.swift +++ b/Sources/AssetKit/CAR/BitmapKeys.swift @@ -4,18 +4,19 @@ import Foundation /// UIImage(named:) resolution for `.imageset` (and analogous) assets. /// /// Structure (verified against actool's reference Assets.car, Xcode 26 / -/// CoreUI 970): +/// CoreUI 970, and a Xcode 27.0 per-colorspace oracle): /// - The tree is `isPathInternal = true` and uses a `blockSize` of 1024. /// - Each leaf entry's "key" slot is an INLINE u32 NameIdentifier (not a /// block pointer like other trees). -/// - Each value is a 52-byte descriptor block. +/// - Each value is a 52-byte descriptor block (48 bytes for colorsets, +/// which carry one fewer trailing sentinel). /// /// Without this tree present, `UIImage(named:)` returns nil on device even /// though `assetutil --info` parses the file cleanly and FACETKEYS/RENDITIONS /// resolve correctly. SpringBoard's appicon-render path does NOT depend on /// BITMAPKEYS (the home icon still renders via the loose-PNG fallback). enum BitmapKeys { - /// The 52-byte descriptor. Layout was derived by diffing actool's outputs + /// The descriptor. Layout was derived by diffing actool's outputs /// for `.appiconset` vs `.imageset` (bitmap) vs `.imageset` (vector) /// renditions. The first 7 u32s are header-like; only slot 6 varies /// across asset kinds. The remaining 6 vary by asset kind. @@ -30,17 +31,21 @@ enum BitmapKeys { case image /// SVG `.imageset` assets — vector source. case vector + /// `.colorset` assets. actool 27.0 writes one BITMAPKEYS row per + /// colorset too (marker 0x02, variable section shaped like the + /// image one). + case color } /// Slot 6 of the header (the only header u32 that varies by kind). /// `0x04` for bitmap-source assets (PNG, JPG); `0x0e` for vector - /// sources (SVG). AppIcon keeps `0x0e` -- we have no reference dump - /// for appicon-only catalogs to confirm which value it expects, and - /// the current value is verified end-to-end on device. + /// sources (SVG) and appicons; `0x02` for colors (actool 27.0 + /// per-space oracle). private var assetKindMarker: UInt32 { switch kind { case .image: return 0x04 case .vector, .appIcon: return 0x0e + case .color: return 0x02 } } @@ -48,8 +53,10 @@ enum BitmapKeys { var w = ByteWriter() w.writeLE(UInt32(1)) w.writeLE(UInt32(0)) - w.writeLE(UInt32(0x28)) - w.writeLE(UInt32(9)) + // Slots 3/4 are length fields: 0x28/9 for 52-byte descriptors + // (icon/image/vector), 0x24/8 for 48-byte color descriptors. + w.writeLE(UInt32(kind == .color ? 0x24 : 0x28)) + w.writeLE(UInt32(kind == .color ? 8 : 9)) w.writeLE(UInt32(0xFFFFFFFF)) w.writeLE(UInt32(1)) w.writeLE(assetKindMarker) @@ -57,6 +64,7 @@ enum BitmapKeys { // AppIcon : [u32=2, u16=1, u16=1, u32=7] // Image : [u32=1, u16=1, u16=0, u32=1] // Vector : [u32=1, u16=1, u16=0, u32=1] (same shape as Image) + // Color : [u32=1, u16=1, u16=0, u32=1] (same shape as Image) // The exact semantics aren't fully reverse-engineered yet, so for // v1 we hardcode the templates per kind and pass through the // discovered (idiom, subtype) count. Field 7 in particular seems @@ -71,12 +79,20 @@ enum BitmapKeys { w.writeLE(UInt16(1)) w.writeLE(UInt16(0)) w.writeLE(UInt32(1)) + case .color: + // Colorsets end the variable section after the (u16, u16) + // pair: no trailing u32 (48-byte descriptor, actool 27.0 + // per-space oracle). + w.writeLE(UInt16(1)) + w.writeLE(UInt16(0)) } - // Three trailing -1 sentinels. + // Three trailing -1 sentinels for every kind. w.writeLE(UInt32(0xFFFFFFFF)) w.writeLE(UInt32(0xFFFFFFFF)) w.writeLE(UInt32(0xFFFFFFFF)) - precondition(w.offset == 52, "BITMAPKEYS descriptor must be 52 bytes; got \(w.offset)") + precondition( + w.offset == (kind == .color ? 48 : 52), + "BITMAPKEYS descriptor must be \(kind == .color ? 48 : 52) bytes; got \(w.offset)") return w.data } } @@ -92,29 +108,30 @@ enum BitmapKeys { } /// Derive the BITMAPKEYS descriptor for one asset from its rendition list. - /// Returns `nil` for color-only assets, which produce no BITMAPKEYS row. + /// Returns `nil` only for assets with no bitmap, preserved-source, or + /// color renditions (actool 27.0 writes a BITMAPKEYS row for colorsets + /// too, marker 0x02). /// /// `renditions` is the per-asset slice -- only the renditions whose /// `name` equals this asset's name. Caller is responsible for the /// grouping; this function does not re-filter. static func descriptor(forAsset name: String, renditions: [Rendition]) -> Descriptor? { - let hasBitmapOrPreservedSource = renditions.contains { rendition in + let hasDescribableRendition = renditions.contains { rendition in switch rendition.body { - case .bitmap, .preservedSource: return true - case .color: return false + case .bitmap, .preservedSource, .color: return true + // MultiSized containers only ever accompany bitmap icon + // renditions; they alone never justify a BITMAPKEYS row. + case .multiSized: return false } } - guard hasBitmapOrPreservedSource else { return nil } + guard hasDescribableRendition else { return nil } let kind = inferKind(from: renditions) - // (idiom << 16) | subtype packs each (idiom, subtype) pair into a - // single UInt32 for Set uniqueness. Subtype is always 0 today; the - // packing exists to match how CoreUI would distinguish (e.g.) iPhone - // 60pt vs iPhone 76pt if subtype were ever non-zero. + // (idiom << 16) | subtype packs each tuple into a single UInt32 for Set uniqueness. let idiomSubtypes = Set(renditions.map { rendition -> UInt32 in let idiom = UInt32(rendition.idiom.rawValueByte) - let subtype: UInt32 = 0 + let subtype = UInt32(rendition.subtype ?? 0) return (idiom << 16) | subtype }) @@ -125,7 +142,7 @@ enum BitmapKeys { /// an .appiconset is a distinct CoreUI category, and a vector source /// outranks plain bitmap because the rasterised PNG fallbacks coexist /// with the preserved SVG body. Mixed PNG/JPEG imagesets fall through - /// to `.image`. + /// to `.image`. Color-only assets (`.colorset`) classify as `.color`. /// /// The AppIcon arm relies on `ImageRenderer.appIconRenditions` only /// producing `.bitmap(.appIcon)` renditions (PNG-only at that entry @@ -142,6 +159,11 @@ enum BitmapKeys { return .vector } } + for rendition in renditions { + if case .color = rendition.body { + return .color + } + } return .image } } diff --git a/Sources/AssetKit/CAR/CARLayout.swift b/Sources/AssetKit/CAR/CARLayout.swift index 91a19b5..af1da3e 100644 --- a/Sources/AssetKit/CAR/CARLayout.swift +++ b/Sources/AssetKit/CAR/CARLayout.swift @@ -53,6 +53,10 @@ struct CARLayout { case .image: return .image } case .color: return .color + // MultiSized containers share the appicon facet; they are part of + // the same asset and only ever accompany .appIcon bitmap renditions, + // which classify the facet first anyway. + case .multiSized: return .appIcon // Preserved-source renditions live in the `.image` category at every // layer above the CSI body: FACETKEYS, BITMAPKEYS, and the rendition // key all reuse the same element/part as a generic PNG imageset. diff --git a/Sources/AssetKit/CAR/CARWriter.swift b/Sources/AssetKit/CAR/CARWriter.swift index 4288e0e..e898d17 100644 --- a/Sources/AssetKit/CAR/CARWriter.swift +++ b/Sources/AssetKit/CAR/CARWriter.swift @@ -21,12 +21,25 @@ struct CARWriter: Sendable { // KEYFORMAT, EXTENDED_METADATA, BITMAPKEYS. Reordering or omitting // a `setVariable` here breaks iOS lookups silently. + // KEYFORMAT is catalog-wide and actool only lists the attributes in + // use: the base eight, plus dimension2 when some rendition carries + // an Icon Index (app icons). Rendition keys pack exactly these + // tokens in this order (actool 27.0: icon cars 18-byte keys, color + // cars 16-byte keys). + let usesIconIndex = renditions.contains { rendition in + if case .bitmap(let body) = rendition.body, body.kind == .appIcon { + return true + } + return false + } + let keyFormat: [AttributeID] = usesIconIndex ? v1KeyFormat : baseKeyFormat + let headerBlockID = bom.addBlock(CARHeaderBlock.data(renditionCount: UInt32(renditions.count))) bom.setVariable("CARHEADER", blockID: headerBlockID) let renditionEntries: [BOMTree.Entry] = renditions.map { rendition in BOMTree.Entry( - key: RenditionKey(rendition: rendition).encode(), + key: RenditionKey(rendition: rendition).encode(format: keyFormat), value: csiData(for: rendition) ) } @@ -48,7 +61,7 @@ struct CARWriter: Sendable { ) bom.setVariable("APPEARANCEKEYS", blockID: appearanceTreeID) - let kfmtBlockID = bom.addBlock(KeyFormatBlock.data()) + let kfmtBlockID = bom.addBlock(KeyFormatBlock.data(attributes: keyFormat)) bom.setVariable("KEYFORMAT", blockID: kfmtBlockID) let extendedMetadataBlockID = bom.addBlock(ExtendedMetadata.data(deploymentTarget: deploymentTarget)) @@ -79,6 +92,8 @@ struct CARWriter: Sendable { case .bitmap(let body): let scaleFactor = UInt32(rendition.scale?.factor ?? 1) * 100 return CSIWriter.bitmap(name: rendition.name, body: body, scaleFactor: scaleFactor) + case .multiSized(let body): + return CSIWriter.multiSized(name: rendition.name, body: body) case .color(let body): return CSIWriter.color(name: rendition.name, body: body) case .preservedSource(let body): diff --git a/Sources/AssetKit/CAR/CSIHeader.swift b/Sources/AssetKit/CAR/CSIHeader.swift index ad38c74..da0fb41 100644 --- a/Sources/AssetKit/CAR/CSIHeader.swift +++ b/Sources/AssetKit/CAR/CSIHeader.swift @@ -35,6 +35,10 @@ enum CSIHeader { /// pixelFormat selects the decoder, not the layout). case bitmapIcon = 12 case namedColor = 1009 + /// MultiSized icon container. The body is a 'MSIS' record (version, + /// count, then per-size width/height/index); actool emits one such + /// rendition per (idiom, subtype) group of app-icon renditions. + case multiSized = 1010 /// Used by preserved-source SVG renditions in CoreUI 970. JPG keeps /// `bitmapIcon` because JPEG sits inside CoreUI's bitmap-asset /// category; SVG promotes to its own layout because vector diff --git a/Sources/AssetKit/CAR/CSIWriter.swift b/Sources/AssetKit/CAR/CSIWriter.swift index 2bdeace..0454b17 100644 --- a/Sources/AssetKit/CAR/CSIWriter.swift +++ b/Sources/AssetKit/CAR/CSIWriter.swift @@ -112,32 +112,86 @@ enum CSIWriter { return header + tvl + envelope } + /// MultiSized icon container (layout 1010). Header dimensions, scale, + /// pixelFormat and colorSpace are all zero like named colors; TVL is the + /// same all-zero 1004 + 1006:1 pair. The name is the ASSET name (not a + /// source filename), and the body lists point sizes; 83.5 pt truncates + /// to 83, as in the reference output. + static func multiSized(name: String, body: MultiSizedBody) -> Data { + let tvl = CSITVL.encode([.colorSlice, .bitmapFlag]) + var w = ByteWriter() + w.writeLE(UInt32(0x4D53_4953)) // 'MSIS' (file bytes SISM) + w.writeLE(UInt32(1)) // version + w.writeLE(UInt32(body.sizes.count)) + for size in body.sizes { + w.writeLE(size.pointWidth) + w.writeLE(size.pointHeight) + w.writeLE(size.iconIndex) + } + let payload = w.data + let header = CSIHeader.encode( + renditionFlags: 0, + width: 0, + height: 0, + scaleFactor: 0, + pixelFormat: 0, + colorSpace: 0, + layout: .multiSized, + name: name, + tvlLength: UInt32(tvl.count), + bitmapCount: 1, + renditionLength: UInt32(payload.count) + ) + return header + tvl + payload + } + + /// Named color, in the layout actool 27.0 writes: header scale, colorSpace + /// and pixelFormat all zero; TVL (1004: zeros, 1006: 1); one body. static func color(name: String, body: ColorBody) -> Data { + let tvl = CSITVL.encode([.colorSlice, .bitmapFlag]) let payload = colorBody(body: body) let header = CSIHeader.encode( renditionFlags: 0, width: 0, height: 0, - scaleFactor: 100, + scaleFactor: 0, pixelFormat: 0, - colorSpace: UInt32(body.colorSpaceID), + colorSpace: 0, layout: .namedColor, name: name, - tvlLength: 0, - bitmapCount: 0, + tvlLength: UInt32(tvl.count), + bitmapCount: 1, renditionLength: UInt32(payload.count) ) - return header + payload + return header + tvl + payload } + /// 'COLR' as an LE multi-char constant (file bytes R,L,O,C), version 1, + /// colorSpaceID with flag bits, component count, then Float64 components. + /// A system color is a gray fallback (colorspace 0x102, [white, alpha]) + /// followed by a second COLR record holding the name (length + UTF-8). private static func colorBody(body: ColorBody) -> Data { var w = ByteWriter() - w.writeFourCC("COLR") - w.writeLE(UInt32(0)) // version + w.writeLE(UInt32(0x434F_4C52)) + w.writeLE(UInt32(1)) // version + if let name = body.systemName { + w.writeLE(UInt32(0x102)) // gray gamma 2.2, as actool writes it + w.writeLE(UInt32(2)) + for component in body.components { + w.writeLE(component.bitPattern) + } + w.writeLE(UInt32(0x434F_4C52)) + w.writeLE(UInt32(1)) + w.writeLE(UInt32(name.utf8.count)) + w.write(Array(name.utf8)) + return w.data + } w.writeLE(UInt32(body.colorSpaceID)) // colorSpaceID with flag bits - w.writeLE(UInt32(4)) // numberOfComponents - for component in [body.red, body.green, body.blue, body.alpha] { - w.writeLE(component.bitPattern) + w.writeLE(UInt32(body.components.count)) + for component in body.components { + // actool quantizes each component to Float32 before storing it + // in the Float64 slot (Xcode 27.0 writes 1.1 as 0x3FF19999A0000000). + w.writeLE(Double(Float(component)).bitPattern) } return w.data } diff --git a/Sources/AssetKit/CAR/FacetKeys.swift b/Sources/AssetKit/CAR/FacetKeys.swift index 2a20b56..e8d59bb 100644 --- a/Sources/AssetKit/CAR/FacetKeys.swift +++ b/Sources/AssetKit/CAR/FacetKeys.swift @@ -44,6 +44,8 @@ enum FacetKeys { ] case .color: return [ + (UInt16(AttributeID.element.rawValue), RenditionKey.Element.bitmap.rawValue), + (UInt16(AttributeID.part.rawValue), RenditionKey.Part.color.rawValue), (UInt16(AttributeID.identifier.rawValue), identifier), ] } diff --git a/Sources/AssetKit/CAR/KeyFormat.swift b/Sources/AssetKit/CAR/KeyFormat.swift index 292ce33..b864266 100644 --- a/Sources/AssetKit/CAR/KeyFormat.swift +++ b/Sources/AssetKit/CAR/KeyFormat.swift @@ -17,10 +17,28 @@ enum AttributeID: UInt32 { case identifier = 17 } -/// Attribute order CoreUI 970 emits in `KEYFORMAT` (and which the rendition key +/// Attribute order CoreUI emits in `KEYFORMAT` (and which the rendition key /// tuple positions mirror exactly). Order is significant: CoreUI binary-searches /// rendition keys by raw byte comparison after packing them into this slot /// layout. +/// +/// KEYFORMAT is per-catalog: actool emits the base eight attributes and adds +/// `dimension2` only when some rendition uses it (app icons carry an Icon +/// Index there). Verified against actool 27.0: an icon-only catalog gets 9 +/// attributes, a color-only catalog gets 8, and rendition keys pack exactly +/// that many tokens in this order. +let baseKeyFormat: [AttributeID] = [ + .appearance, + .localization, + .scale, + .idiom, + .subtype, + .identifier, + .element, + .part, +] + +/// Key format for catalogs that use the Icon Index slot (app icons). let v1KeyFormat: [AttributeID] = [ .appearance, .localization, diff --git a/Sources/AssetKit/CAR/RenditionKey.swift b/Sources/AssetKit/CAR/RenditionKey.swift index 01dfb76..5986cf8 100644 --- a/Sources/AssetKit/CAR/RenditionKey.swift +++ b/Sources/AssetKit/CAR/RenditionKey.swift @@ -29,6 +29,13 @@ struct RenditionKey: Hashable, Sendable { enum Part: UInt16 { /// Used by SpringBoard's icon-render pipeline (`.appiconset`). case appIcon = 220 + /// Named colors (`.colorset`). actool keys every color rendition, + /// system references included, at element 85 / part 217. + case color = 217 + /// MultiSized icon container (CSI layout 1010, 'MSIS' body). actool + /// emits one per (idiom, subtype) group of icon renditions; CoreUI + /// resolves an icon request's point size through it. + case multiSized = 218 /// Used by UIImage(named:) for generic `.imageset` assets. case image = 181 /// Slot for preserved-source vector renditions (SVG). Reference @@ -43,7 +50,7 @@ struct RenditionKey: Hashable, Sendable { self.localization = 0 self.scale = rendition.scale?.rawValueByte ?? 0 self.idiom = rendition.idiom.rawValueByte - self.subtype = 0 + self.subtype = rendition.subtype ?? 0 self.identifier = UInt16(FacetKeys.nameHash(rendition.name) & 0xFFFF) switch rendition.body { case .bitmap(let body): @@ -51,19 +58,26 @@ struct RenditionKey: Hashable, Sendable { switch body.kind { case .appIcon: self.part = Part.appIcon.rawValue - // Dimension2 is the appicon "Icon Index" slot. v1 only - // emits one logical icon size per appiconset, so this is - // always 1. - self.dimension2 = 1 + // Dimension2 is the appicon "Icon Index" slot: the rank of + // this rendition's point size among the appiconset's + // distinct point sizes (assetutil surfaces it as + // "Icon Index"). + self.dimension2 = rendition.iconIndex ?? 0 case .image: self.part = Part.image.rawValue // Generic image assets don't use Dimension2 at all. self.dimension2 = 0 } + case .multiSized: + self.element = Element.bitmap.rawValue + self.part = Part.multiSized.rawValue + self.dimension2 = 0 case .color: - self.element = 0 - self.part = 0 + self.element = Element.bitmap.rawValue + self.part = Part.color.rawValue self.dimension2 = 0 + // actool keys named colors at scale 1 (assetutil: "Scale": 1). + self.scale = 1 case .preservedSource(let body): self.element = Element.bitmap.rawValue self.dimension2 = 0 @@ -104,20 +118,31 @@ struct RenditionKey: Hashable, Sendable { self.part = part } - func encode() -> Data { + /// Packs the key as little-endian UInt16 tokens, one per attribute in + /// `format` (the catalog's KEYFORMAT). Token count = format count: a + /// color-only catalog's 8-attribute format yields 16-byte keys, an + /// icon catalog's 9-attribute format yields 18-byte keys, matching + /// actool 27.0. + func encode(format: [AttributeID]) -> Data { var w = ByteWriter() - w.writeLE(appearance) - w.writeLE(localization) - w.writeLE(scale) - w.writeLE(idiom) - w.writeLE(subtype) - w.writeLE(dimension2) - w.writeLE(identifier) - w.writeLE(element) - w.writeLE(part) + for attribute in format { + switch attribute { + case .appearance: w.writeLE(appearance) + case .localization: w.writeLE(localization) + case .scale: w.writeLE(scale) + case .idiom: w.writeLE(idiom) + case .subtype: w.writeLE(subtype) + case .dimension2: w.writeLE(dimension2) + case .identifier: w.writeLE(identifier) + case .element: w.writeLE(element) + case .part: w.writeLE(part) + } + } return w.data } + /// Decodes a key packed in `v1KeyFormat` order. Returns nil for other + /// formats (token count and positions differ). static func decode(_ data: Data) -> RenditionKey? { guard data.count == 18 else { return nil } func u16(_ offset: Int) -> UInt16 { diff --git a/Sources/AssetKit/CAR/TVLEntry.swift b/Sources/AssetKit/CAR/TVLEntry.swift index f2ddb68..24d2e22 100644 --- a/Sources/AssetKit/CAR/TVLEntry.swift +++ b/Sources/AssetKit/CAR/TVLEntry.swift @@ -26,6 +26,10 @@ enum TVLEntry { /// Type 1004 (8-byte value): slice/scale pair. Reference is `(0, 1.0f)`. case sliceScale + /// Type 1004 (8-byte value) as named colors carry it: all zero. Seen in + /// actool 27.0 output for every `.colorset` rendition. + case colorSlice + /// Type 1006 (4-byte value): always 1 in the reference. Likely a /// bitmap-count / has-mipmap-stages flag. case bitmapFlag @@ -60,6 +64,11 @@ enum TVLEntry { w.writeLE(UInt32(8)) w.writeLE(UInt32(0)) w.writeLE(UInt32(Float(1).bitPattern)) + case .colorSlice: + w.writeLE(UInt32(1004)) + w.writeLE(UInt32(8)) + w.writeLE(UInt32(0)) + w.writeLE(UInt32(0)) case .bitmapFlag: w.writeLE(UInt32(1006)) w.writeLE(UInt32(4)) diff --git a/Sources/AssetKit/Rendition/ColorRenderer.swift b/Sources/AssetKit/Rendition/ColorRenderer.swift index 0bd50ba..b53e775 100644 --- a/Sources/AssetKit/Rendition/ColorRenderer.swift +++ b/Sources/AssetKit/Rendition/ColorRenderer.swift @@ -1,10 +1,52 @@ import Foundation +/// COLR colorspace IDs, verified against actool 27.0 (Xcode 27) with one +/// colorset per color space: srgb 1, gray-gamma-22 2, display-p3 3, +/// extended-srgb 4, extended-linear-srgb 5, extended-gray 6. (System colors +/// carry 0x102 for the gray fallback instead.) +enum COLRColorSpace: UInt8 { + case srgb = 1 + case grayGamma22 = 2 + case displayP3 = 3 + case extendedSrgb = 4 + case extendedLinearSrgb = 5 + case extendedGray = 6 + + /// Maps the Contents.json `color-space` string. Unlisted spaces fall + /// back to the declared display gamut (sRGB / display-P3), matching + /// actool's treatment of plain sRGB entries. + init(colorSpace: String?, gamut: Gamut) { + switch colorSpace { + case "srgb": self = .srgb + case "gray-gamma-22": self = .grayGamma22 + case "display-p3": self = .displayP3 + case "extended-srgb": self = .extendedSrgb + case "extended-linear-srgb": self = .extendedLinearSrgb + case "extended-gray": self = .extendedGray + default: self = gamut == .displayP3 ? .displayP3 : .srgb + } + } +} + enum ColorRenderer { static func renditions(for set: LoadedColorSet) throws -> [Rendition] { var out: [Rendition] = [] for entry in set.contents.colors { - let (r, g, b, a) = try entry.color.components.asDoubles() + let appearance = entry.appearances?.first { $0.darkLuminosity } + if let systemName = entry.color.reference { + // ponytail: gray fallback is black (light) / white (dark), which is exact for + // labelColor; CoreUI resolves the name itself, so the fallback rarely shows. + let white: Double = appearance == nil ? 0 : 1 + out.append(Rendition( + name: set.name, idiom: entry.idiom, scale: nil, appearance: appearance, gamut: .sRGB, + body: .color(ColorBody(components: [white, 1], colorSpaceID: COLRColorSpace.grayGamma22.rawValue, + systemName: systemName)) + )) + continue + } + guard let components = entry.color.components else { + throw XCAssetCompilerError.invalidColorComponent("missing components in \(set.name)") + } let gamut: Gamut = { if let declared = entry.displayGamut { return declared } switch entry.color.colorSpace { @@ -12,17 +54,22 @@ enum ColorRenderer { default: return .sRGB } }() - let appearance = entry.appearances?.first { $0.darkLuminosity } + let colorSpaceID = COLRColorSpace(colorSpace: entry.color.colorSpace, gamut: gamut).rawValue + let body: ColorBody + if components.isGray { + let (white, alpha) = try components.asGrayDoubles() + body = ColorBody(components: [white, alpha], colorSpaceID: colorSpaceID) + } else { + let (r, g, b, a) = try components.asRGBDoubles() + body = ColorBody(components: [r, g, b, a], colorSpaceID: colorSpaceID) + } out.append(Rendition( name: set.name, idiom: entry.idiom, scale: nil, appearance: appearance, gamut: gamut, - body: .color(ColorBody( - red: r, green: g, blue: b, alpha: a, - colorSpaceID: gamut.colorSpaceID - )) + body: .color(body) )) } return out diff --git a/Sources/AssetKit/Rendition/ImageRenderer.swift b/Sources/AssetKit/Rendition/ImageRenderer.swift index 05e6107..ab6ebb0 100644 --- a/Sources/AssetKit/Rendition/ImageRenderer.swift +++ b/Sources/AssetKit/Rendition/ImageRenderer.swift @@ -60,7 +60,8 @@ enum ImageRenderer { } static func appIconRenditions(for appIcon: LoadedAppIcon, files: [IconFile]) throws -> [Rendition] { - var out: [Rendition] = [] + // Decode each source once, keeping its IconFile for index assignment. + var decoded: [(file: IconFile, rendition: Rendition)] = [] for file in files { let filename = file.sourceURL.lastPathComponent guard SourceFormat.detect(filename: filename) == .png else { @@ -89,8 +90,86 @@ enum ImageRenderer { filename: filename, kind: .appIcon ) - out.append(contentsOf: try PNGSource.renditions(bytes: bytes, context: ctx)) + for rendition in try PNGSource.renditions(bytes: bytes, context: ctx) { + decoded.append((file, rendition)) + } + } + + // "Icon Index" = the rank of the rendition's point size among the + // appiconset's distinct point sizes, ascending; shared across idioms + // and scales (60 pt @2x and @3x are both index 1; an ipad 20 pt and + // an iphone 20 pt are also both index 1). Both oracle runs agree. + // + // When the set provides an iphone 60 pt @3x source, actool also keys + // it as the 90 pt large-phone home icon (subtype 1792, scale 2); the + // 90 pt size then joins the ranking. Without a 60 pt @3x source no + // 1792 variant is emitted (second oracle run). + let largePhonePointSize = 90.0 + let hasLargePhoneVariant = files.contains { + $0.idiom == .iphone && $0.pointSize == 60 && $0.scale == 3 + } + var sizes = Set(files.map(\.pointSize)) + if hasLargePhoneVariant { sizes.insert(largePhonePointSize) } + let iconIndexOfSize: [Double: UInt16] = Dictionary( + uniqueKeysWithValues: sizes.sorted().enumerated().map { ($0.element, UInt16($0.offset + 1)) } + ) + + var out: [Rendition] = [] + // (idiom, subtype) -> point size -> icon index. One MultiSized + // rendition is emitted per group, mirroring the reference output. + var groups: [MultiSizedGroup: [UInt32: UInt32]] = [:] + for (file, rendition) in decoded { + var icon = rendition + icon.iconIndex = iconIndexOfSize[file.pointSize] + out.append(icon) + addMultiSizedEntry( + &groups, idiom: file.idiom, subtype: 0, + pointSize: UInt32(file.pointSize), index: UInt32(icon.iconIndex!) + ) + if file.idiom == .iphone, file.pointSize == 60, file.scale == 3 { + // The 90 pt subtype-1792 home icon reuses the 60 pt @3x + // image verbatim (both are 180 px); only its key differs. + var large = rendition + large.scale = .x2 + large.subtype = 1792 + large.iconIndex = iconIndexOfSize[largePhonePointSize] + out.append(large) + addMultiSizedEntry( + &groups, idiom: .iphone, subtype: 1792, + pointSize: UInt32(largePhonePointSize), index: UInt32(large.iconIndex!) + ) + } + } + for (group, entries) in groups { + out.append(Rendition( + name: appIcon.name, + idiom: group.idiom, + scale: .x1, + appearance: nil, + gamut: nil, + subtype: group.subtype == 0 ? nil : group.subtype, + body: .multiSized(MultiSizedBody( + sizes: entries.sorted { $0.key < $1.key }.map { + MultiSizedBody.Size(pointWidth: $0.key, pointHeight: $0.key, iconIndex: $0.value) + } + )) + )) } return out } + + private struct MultiSizedGroup: Hashable { + var idiom: Idiom + var subtype: UInt16 + } + + private static func addMultiSizedEntry( + _ groups: inout [MultiSizedGroup: [UInt32: UInt32]], + idiom: Idiom, + subtype: UInt16, + pointSize: UInt32, + index: UInt32 + ) { + groups[MultiSizedGroup(idiom: idiom, subtype: subtype), default: [:]][pointSize] = index + } } diff --git a/Sources/AssetKit/Rendition/Rendition.swift b/Sources/AssetKit/Rendition/Rendition.swift index 2175002..6b83702 100644 --- a/Sources/AssetKit/Rendition/Rendition.swift +++ b/Sources/AssetKit/Rendition/Rendition.swift @@ -5,6 +5,7 @@ struct Rendition: Sendable { case bitmap(BitmapBody) case color(ColorBody) case preservedSource(PreservedSourceBody) + case multiSized(MultiSizedBody) } var name: String @@ -12,9 +13,33 @@ struct Rendition: Sendable { var scale: Scale? var appearance: Appearance? var gamut: Gamut? + /// Raw rendition-key subtype token. Non-zero only for icon variants + /// keyed on a device trait; actool keys the 90 pt large-phone home icon + /// (derived from the 60 pt @3x slot) at subtype 1792. + var subtype: UInt16? = nil + /// App-icon "Icon Index": the rank of this rendition's point size among + /// the appiconset's distinct point sizes, ascending. Only icon renditions + /// carry it; assetutil surfaces it as "Icon Index". + var iconIndex: UInt16? = nil var body: Body } +/// Body of a MultiSized icon rendition (CSI layout 1010, 'MSIS' payload). +/// actool emits one per (idiom, subtype) group of icon renditions; each +/// entry maps a point size to the Icon Index of the bitmap rendition that +/// satisfies it. Widths/heights are point sizes (83.5 pt truncates to 83, +/// as in the reference output). +struct MultiSizedBody: Sendable { + struct Size: Sendable { + var pointWidth: UInt32 + var pointHeight: UInt32 + var iconIndex: UInt32 + } + + /// One entry per distinct point size, ascending. + var sizes: [Size] +} + struct BitmapBody: Sendable { /// Which CoreUI rendition category this bitmap belongs to. Determines the /// `(element, part)` pair we encode in the rendition key and FACETKEYS @@ -47,11 +72,18 @@ struct BitmapBody: Sendable { } struct ColorBody: Sendable { - var red: Double - var green: Double - var blue: Double - var alpha: Double + /// Components in the color space's own order; RGB spaces carry 4 + /// (r, g, b, a), gray spaces carry 2 (white, alpha). actool quantizes + /// each to Float32 before storing it in the COLR body's Float64 slots + /// (verified: Xcode 27.0 writes 1.1 as 0x3FF19999A0000000). + var components: [Double] + /// COLR colorspace ID. actool 27.0 maps: srgb 1, gray-gamma-22 2, + /// display-p3 3, extended-srgb 4, extended-linear-srgb 5, extended-gray 6; + /// the gray fallback for system colors carries 0x102. var colorSpaceID: UInt8 + /// System color name ("labelColor"). The components then hold a gray + /// fallback for runtimes that cannot resolve it. + var systemName: String? = nil } /// Source file kept verbatim inside a DWAR envelope rather than rasterised diff --git a/Sources/AssetKit/Schema/Contents.swift b/Sources/AssetKit/Schema/Contents.swift index ac3271c..e19a423 100644 --- a/Sources/AssetKit/Schema/Contents.swift +++ b/Sources/AssetKit/Schema/Contents.swift @@ -137,35 +137,64 @@ struct ColorSetContents: Codable, Sendable { } struct Color: Codable, Sendable { - var colorSpace: String - var components: Components + /// Absent when the entry references a system color instead. + var colorSpace: String? + var components: Components? + /// A system color name such as "labelColor" (Xcode: Content > System color). + var reference: String? enum CodingKeys: String, CodingKey { case colorSpace = "color-space" - case components + case components, reference } struct Components: Codable, Sendable { - var red: String - var green: String - var blue: String - var alpha: String - - func asDoubles() throws -> (r: Double, g: Double, b: Double, a: Double) { - func parse(_ s: String) throws -> Double { - if s.hasPrefix("0x") || s.hasPrefix("0X") { - let hex = String(s.dropFirst(2)) - guard let n = UInt8(hex, radix: 16) else { - throw XCAssetCompilerError.invalidColorComponent(s) - } - return Double(n) / 255 - } - guard let n = Double(s) else { + var red: String? + var green: String? + var blue: String? + /// Gray color spaces (`gray-gamma-22`, `extended-gray`) carry a + /// single white level plus alpha instead of RGB. + var white: String? + var alpha: String? + + /// True when the entry is written in a gray color space (Xcode + /// keys it on `white`, not `red`). + var isGray: Bool { white != nil } + + /// `(white, alpha)` for gray spaces. Throws for RGB entries. + func asGrayDoubles() throws -> (white: Double, alpha: Double) { + guard let white, let alpha else { + throw XCAssetCompilerError.invalidColorComponent( + "gray components need white and alpha") + } + return (try parse(white), try parse(alpha)) + } + + /// `(r, g, b, a)` for RGB spaces. Throws for gray entries. + func asRGBDoubles() throws -> (r: Double, g: Double, b: Double, a: Double) { + guard let red, let green, let blue, let alpha else { + throw XCAssetCompilerError.invalidColorComponent( + "RGB components need red, green, blue and alpha") + } + return (try parse(red), try parse(green), try parse(blue), try parse(alpha)) + } + + /// Accepts Xcode's component forms: float ("0.500", "-0.100", + /// extended values beyond [0,1] included), 8-bit hex ("0xFF") + /// and 0-255 decimal ("255"). A decimal point means a direct + /// float; a plain integer means the 0-255 form. + func parse(_ s: String) throws -> Double { + if s.hasPrefix("0x") || s.hasPrefix("0X") { + let hex = String(s.dropFirst(2)) + guard let n = UInt8(hex, radix: 16) else { throw XCAssetCompilerError.invalidColorComponent(s) } - return n > 1 ? n / 255 : n + return Double(n) / 255 } - return (try parse(red), try parse(green), try parse(blue), try parse(alpha)) + guard let n = Double(s) else { + throw XCAssetCompilerError.invalidColorComponent(s) + } + return s.contains(".") || s.contains("e") || s.contains("E") || n == 1 ? n : n / 255 } } } diff --git a/Tests/AssetKitTests/AppIconIconIndexTests.swift b/Tests/AssetKitTests/AppIconIconIndexTests.swift new file mode 100644 index 0000000..824ff71 --- /dev/null +++ b/Tests/AssetKitTests/AppIconIconIndexTests.swift @@ -0,0 +1,282 @@ +import Foundation +import Testing +@testable import AssetKit + +/// Pins app-icon "Icon Index" assignment and MultiSized rendition emission +/// against the Xcode 27.0 actool oracle +/// (~/tmp/apple-dev/oracle/icon/{Assets.car,assetutil.json}). +/// +/// Oracle facts (two independent actool runs): +/// - Icon Index = rank of the rendition's point size among the appiconset's +/// distinct point sizes, ascending, shared across idioms and scales. +/// - An iphone 60 pt @3x source additionally keys the 90 pt large-phone home +/// icon (subtype 1792, scale 2, same image bytes). Without a 60 pt @3x +/// source no 1792 variant is emitted. +/// - One MultiSized rendition (key part=218, scale=1, dimension2=0) per +/// (idiom, subtype) group, sizes ascending, 83.5 pt truncated to 83. +@Suite("AppIconIconIndex") +struct AppIconIconIndexTests { + // MARK: - Fixtures + + private func loadAppIcon( + name: String, + images: [(filename: String, idiom: String, size: String, scale: String)], + workDir: URL + ) throws -> LoadedAppIcon { + let dir = workDir.appendingPathComponent("\(name).appiconset", isDirectory: true) + try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) + let entries = try images.map { image -> String in + let decoder = JSONDecoder() + let entry = """ + {"filename":"\(image.filename)","idiom":"\(image.idiom)","size":"\(image.size)","scale":"\(image.scale)"} + """ + // Re-decode each entry so a malformed fixture fails the test + // instead of reaching the compiler with silent nils. + _ = try decoder.decode(AppIconContents.self, from: Data("{\"images\":[\(entry)],\"info\":{\"author\":\"xcode\",\"version\":1}}".utf8)) + return entry + } + let json = "{\"images\":[\(entries.joined(separator: ","))],\"info\":{\"author\":\"xcode\",\"version\":1}}" + try Data(json.utf8).write(to: dir.appendingPathComponent("Contents.json")) + for image in images { + try storedPNG().write(to: dir.appendingPathComponent(image.filename)) + } + let contents = try JSONDecoder().decode( + AppIconContents.self, + from: Data(contentsOf: dir.appendingPathComponent("Contents.json")) + ) + return LoadedAppIcon(name: name, directory: dir, contents: contents) + } + + private func renditions(for appIcon: LoadedAppIcon) throws -> [Rendition] { + let files = try AppIconPlistEmitter.emit(appIcon).iconFiles + return try ImageRenderer.appIconRenditions(for: appIcon, files: files) + } + + /// Minimal valid 1x1 PNG (pixel content is irrelevant; only rendition + /// keys and MultiSized CSI bytes are pinned). Stored (uncompressed) + /// deflate keeps it dependency-free. + private func storedPNG() -> Data { + let ihdr: [UInt8] = [0, 0, 0, 0x0D] + Array("IHDR".utf8) + + [0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0] + + be32(crc32(Data(Array("IHDR".utf8)) + Data([0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0]))) + + // zlib stream with one stored deflate block of 5 bytes + // (filter byte + RGBA pixel). + let raw: [UInt8] = [0, 0xFF, 0x00, 0x00, 0xFF] + var deflate: [UInt8] = [0x01, UInt8(raw.count), 0, UInt8(~raw.count & 0xFF), 0xFF] + deflate.append(contentsOf: raw) + var zlib: [UInt8] = [0x78, 0x01] + zlib.append(contentsOf: deflate) + zlib.append(contentsOf: be32(adler32(raw))) + + var idat: [UInt8] = [0, 0, 0, 0] + Array("IDAT".utf8) + zlib + idat.replaceSubrange(0..<4, with: be32(UInt32(zlib.count))) + idat.append(contentsOf: be32(crc32(Data(idat[4...])))) + + var iend: [UInt8] = Array("IEND".utf8) + iend.append(contentsOf: be32(crc32(Data(iend)))) + iend.insert(contentsOf: [0, 0, 0, 0], at: 0) + + return Data([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] + ihdr + idat + iend) + } + + private func be32(_ value: UInt32) -> [UInt8] { + withUnsafeBytes(of: value.bigEndian) { Array($0) } + } + + private func crc32(_ data: Data) -> UInt32 { + var crc: UInt32 = 0xFFFFFFFF + for byte in data { + crc ^= UInt32(byte) + for _ in 0..<8 { + crc = (crc & 1 != 0) ? (crc >> 1) ^ 0xEDB88320 : crc >> 1 + } + } + return crc ^ 0xFFFFFFFF + } + + private func adler32(_ bytes: [UInt8]) -> UInt32 { + var a: UInt32 = 1, b: UInt32 = 0 + for byte in bytes { + a = (a + UInt32(byte)) % 65521 + b = (b + a) % 65521 + } + return (b << 16) | a + } + + // MARK: - Expected bytes + + private func bytes(_ hex: String) -> Data { + var data = Data() + var index = hex.startIndex + while index < hex.endIndex { + let next = hex.index(index, offsetBy: 2) + data.append(UInt8(hex[index.. Data { + var w = ByteWriter() + for token: UInt16 in [0, 0, scale, idiom, subtype, dimension2, + UInt16(FacetKeys.nameHash("AppIcon") & 0xFFFF), element, part] { + w.writeLE(token) + } + return w.data + } + + // MARK: - Tests + + @Test("Oracle appiconset: icon indices, 1792 variant, and MultiSized CSI match actool") + func oracleParity() throws { + let tmp = FileManager.default.temporaryDirectory + .appendingPathComponent("IconIndex-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: tmp) } + + let appIcon = try loadAppIcon( + name: "AppIcon", + images: [ + ("icon-120.png", "iphone", "60x60", "2x"), + ("icon-180.png", "iphone", "60x60", "3x"), + ("icon-152.png", "ipad", "76x76", "2x"), + ("icon-167.png", "ipad", "83.5x83.5", "2x"), + ("icon-1024.png", "ios-marketing", "1024x1024", "1x"), + ], + workDir: tmp + ) + let renditions = try renditions(for: appIcon) + #expect(renditions.count == 10) // 6 icon + 4 MultiSized + let descriptor = try #require(BitmapKeys.descriptor(forAsset: "AppIcon", renditions: renditions)) + #expect(descriptor.idiomSubtypeCount == 4) + + let expectedKeys = [ + // (scale, idiom, subtype, dimension2, part) per oracle RENDITIONS. + key(scale: 2, idiom: 1, subtype: 0, dimension2: 1, part: 220), // 60@2x + key(scale: 3, idiom: 1, subtype: 0, dimension2: 1, part: 220), // 60@3x + key(scale: 2, idiom: 2, subtype: 0, dimension2: 2, part: 220), // 76@2x + key(scale: 2, idiom: 2, subtype: 0, dimension2: 3, part: 220), // 83.5@2x + key(scale: 1, idiom: 6, subtype: 0, dimension2: 5, part: 220), // 1024 + key(scale: 2, idiom: 1, subtype: 1792, dimension2: 4, part: 220), // 90pt 1792 + key(scale: 1, idiom: 1, subtype: 0, dimension2: 0, part: 218), // phone + key(scale: 1, idiom: 1, subtype: 1792, dimension2: 0, part: 218), // phone 1792 + key(scale: 1, idiom: 2, subtype: 0, dimension2: 0, part: 218), // pad + key(scale: 1, idiom: 6, subtype: 0, dimension2: 0, part: 218), // marketing + ] + let actualKeys = renditions.map { RenditionKey(rendition: $0).encode(format: v1KeyFormat) } + for expected in expectedKeys { + #expect(actualKeys.contains(expected), "missing rendition key \(Array(expected))") + } + + // The 1792 icon is the 60@3x image re-keyed at scale 2 (same decoded + // pixels verbatim, as in the reference where both 180 px renditions + // carry identical MLEC bodies). + let large = renditions.first { $0.subtype == 1792 && RenditionKey(rendition: $0).part == 220 } + let phone3x = renditions.first { $0.scale == .x3 && $0.idiom == .iphone } + guard let large, case .bitmap(let largeBody) = large.body, + let phone3x, case .bitmap(let phone3xBody) = phone3x.body else { + Issue.record("missing 1792 icon rendition") + return + } + #expect(largeBody.pixelsBGRA == phone3xBody.pixelsBGRA) + #expect(large.scale == .x2 && large.iconIndex == 4) + + // MultiSized CSI records byte-match the oracle value blobs verbatim + // (the CSI value carries no identifier, so equality is exact). + let phone = CSIWriter.multiSized( + name: "AppIcon", + body: MultiSizedBody(sizes: [.init(pointWidth: 60, pointHeight: 60, iconIndex: 1)])) + #expect(phone == bytes("495354430100000000000000000000000000000000000000000000000000000000000000f203000041707049636f6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000018000000ec030000080000000000000000000000ee03000004000000010000005349534d01000000010000003c0000003c00000001000000")) + let phone1792 = CSIWriter.multiSized( + name: "AppIcon", + body: MultiSizedBody(sizes: [.init(pointWidth: 90, pointHeight: 90, iconIndex: 4)])) + #expect(phone1792 == bytes("495354430100000000000000000000000000000000000000000000000000000000000000f203000041707049636f6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000018000000ec030000080000000000000000000000ee03000004000000010000005349534d01000000010000005a0000005a00000004000000")) + let pad = CSIWriter.multiSized( + name: "AppIcon", + body: MultiSizedBody(sizes: [ + .init(pointWidth: 76, pointHeight: 76, iconIndex: 2), + .init(pointWidth: 83, pointHeight: 83, iconIndex: 3), + ])) + #expect(pad == bytes("495354430100000000000000000000000000000000000000000000000000000000000000f203000041707049636f6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000024000000ec030000080000000000000000000000ee03000004000000010000005349534d01000000020000004c0000004c00000002000000530000005300000003000000")) + let marketing = CSIWriter.multiSized( + name: "AppIcon", + body: MultiSizedBody(sizes: [.init(pointWidth: 1024, pointHeight: 1024, iconIndex: 5)])) + #expect(marketing == bytes("495354430100000000000000000000000000000000000000000000000000000000000000f203000041707049636f6e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000018000000ec030000080000000000000000000000ee03000004000000010000005349534d0100000001000000000400000004000005000000")) + } + + @Test("Full classic set: indices shared across idioms; no 1792 without iphone 60@3x") + func classicSetIndices() throws { + let tmp = FileManager.default.temporaryDirectory + .appendingPathComponent("IconIndexClassic-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: tmp) } + + let images: [(filename: String, idiom: String, size: String, scale: String)] = [ + ("icon-20@2x.png", "iphone", "20x20", "2x"), + ("icon-20@3x.png", "iphone", "20x20", "3x"), + ("icon-29@2x.png", "iphone", "29x29", "2x"), + ("icon-29@3x.png", "iphone", "29x29", "3x"), + ("icon-40@2x.png", "iphone", "40x40", "2x"), + ("icon-40@3x.png", "iphone", "40x40", "3x"), + ("icon-20@1x.png", "ipad", "20x20", "1x"), + ("icon-20@2x~ipad.png", "ipad", "20x20", "2x"), + ("icon-29@1x.png", "ipad", "29x29", "1x"), + ("icon-29@2x~ipad.png", "ipad", "29x29", "2x"), + ("icon-40@1x.png", "ipad", "40x40", "1x"), + ("icon-40@2x~ipad.png", "ipad", "40x40", "2x"), + ("icon-50@1x.png", "ipad", "50x50", "1x"), + ("icon-50@2x.png", "ipad", "50x50", "2x"), + ("icon-76.png", "ipad", "76x76", "1x"), + ("icon-167.png", "ipad", "83.5x83.5", "2x"), + ("icon-1024.png", "ios-marketing", "1024x1024", "1x"), + ] + let appIcon = try loadAppIcon(name: "AppIcon", images: images, workDir: tmp) + let renditions = try renditions(for: appIcon) + + func iconIndex(named: String) -> UInt16? { + renditions.first { rendition in + guard case .bitmap(let body) = rendition.body, body.kind == .appIcon, + body.renditionName == named else { return false } + return true + }?.iconIndex + } + + // Second oracle run: 20→1, 29→2, 40→3, 50→4, 76→5, 83.5→6, 1024→7, + // shared across idioms and scales. + #expect(iconIndex(named: "icon-20@2x.png") == 1) + #expect(iconIndex(named: "icon-20@3x.png") == 1) + #expect(iconIndex(named: "icon-20@1x.png") == 1) + #expect(iconIndex(named: "icon-20@2x~ipad.png") == 1) + #expect(iconIndex(named: "icon-29@2x.png") == 2) + #expect(iconIndex(named: "icon-29@1x.png") == 2) + #expect(iconIndex(named: "icon-40@3x.png") == 3) + #expect(iconIndex(named: "icon-50@2x.png") == 4) + #expect(iconIndex(named: "icon-76.png") == 5) + #expect(iconIndex(named: "icon-167.png") == 6) + #expect(iconIndex(named: "icon-1024.png") == 7) + + // No 1792 variant without an iphone 60@3x source: 17 icon renditions + // + 3 MultiSized groups (phone, pad, marketing). + #expect(!renditions.contains { $0.subtype == 1792 }) + #expect(renditions.count == 20) + + let phone = renditions.first { + RenditionKey(rendition: $0).part == RenditionKey.Part.multiSized.rawValue + && $0.idiom == .iphone + } + guard let phone, case .multiSized(let body) = phone.body else { + Issue.record("missing phone MultiSized rendition") + return + } + #expect(body.sizes.map(\.pointWidth) == [20, 29, 40]) + #expect(body.sizes.map(\.iconIndex) == [1, 2, 3]) + } +} diff --git a/Tests/AssetKitTests/AssetutilParseTests.swift b/Tests/AssetKitTests/AssetutilParseTests.swift index ec43691..535934d 100644 --- a/Tests/AssetKitTests/AssetutilParseTests.swift +++ b/Tests/AssetKitTests/AssetutilParseTests.swift @@ -64,7 +64,11 @@ struct AssetutilParseTests { try assertHeader(entries.first) let renditions = entries.dropFirst().filter { $0["AssetType"] as? String == "Icon Image" } - #expect(renditions.count == 2, "expected two raw Icon Image renditions (@2x and @3x)") + #expect(renditions.count == 3, "expected two source renditions and the derived 1792 rendition") + let derived = renditions.first { + ($0["Scale"] as? Int) == 2 && ($0["PixelWidth"] as? Int) == 180 + } + #expect(derived?["PixelHeight"] as? Int == 180) for (i, rendition) in renditions.enumerated() { #expect(rendition["Idiom"] as? String == "phone", "rendition[\(i)] idiom") @@ -75,8 +79,8 @@ struct AssetutilParseTests { #expect((rendition["Colorspace"] as? String)?.lowercased() == "srgb", "rendition[\(i)] Colorspace") } - let twoX = renditions.first { ($0["Scale"] as? Int) == 2 } - let threeX = renditions.first { ($0["Scale"] as? Int) == 3 } + let twoX = renditions.first { $0["RenditionName"] as? String == "icon@2x.png" } + let threeX = renditions.first { $0["RenditionName"] as? String == "icon@3x.png" && ($0["Scale"] as? Int) == 3 } #expect(twoX?["PixelWidth"] as? Int == 120) #expect(twoX?["PixelHeight"] as? Int == 120) #expect(twoX?["RenditionName"] as? String == "icon@2x.png") diff --git a/Tests/AssetKitTests/CSIWriterTests.swift b/Tests/AssetKitTests/CSIWriterTests.swift index 00c17a9..f50db90 100644 --- a/Tests/AssetKitTests/CSIWriterTests.swift +++ b/Tests/AssetKitTests/CSIWriterTests.swift @@ -50,14 +50,37 @@ struct CSIWriterTests { #expect(data.count >= 184) } - @Test("Color CSI body starts with COLR magic and four IEEE-754 doubles") + @Test("Color CSI matches actool 27.0's bytes for an sRGB color") func colorBody() { - let body = ColorBody(red: 1, green: 0, blue: 0.5, alpha: 1, colorSpaceID: 0) - let data = CSIWriter.color(name: "Accent", body: body) - // CSI header is 184 bytes; body starts at offset 184 - #expect(data.count >= 184 + 4 + 4 + 4 + 4 + 8 * 4) - let payloadStart = 184 - let magic = data.subdata(in: payloadStart..<(payloadStart + 4)) - #expect(Array(magic) == Array("COLR".utf8)) + // Captured from Xcode 27.0 actool (assetutil reads it as sRGB [1, 0, 0, 1]). + let referenceTVL: [UInt8] = [ + 0xEC, 0x03, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0xEE, 0x03, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, + ] + let referenceBody: [UInt8] = [ + 0x52, 0x4C, 0x4F, 0x43, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0xF0, 0x3F, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xF0, 0x3F, + ] + let body = ColorBody(components: [1, 0, 0, 1], colorSpaceID: 1) + let data = [UInt8](CSIWriter.color(name: "Dot", body: body)) + #expect(Array(data[184..<(184 + 28)]) == referenceTVL) + #expect(Array(data[(184 + 28)...]) == referenceBody) + // Header: scaleFactor (offset 20) and colorSpace (offset 28) zero, bitmap count 1. + #expect(data[20..<24].allSatisfy { $0 == 0 } && data[28..<32].allSatisfy { $0 == 0 }) + #expect(Array(data[172..<176]) == [1, 0, 0, 0]) + } + + @Test("System color reference matches actool 27.0's bytes (labelColor, light)") + func systemColorBody() { + // Captured from Xcode 27.0 actool; assetutil reads it as "System Color Name": "labelColor". + let referenceBody: [UInt8] = [ + 0x52, 0x4C, 0x4F, 0x43, 1, 0, 0, 0, 0x02, 0x01, 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xF0, 0x3F, + 0x52, 0x4C, 0x4F, 0x43, 1, 0, 0, 0, 10, 0, 0, 0, + ] + Array("labelColor".utf8) + let body = ColorBody(components: [0, 1], colorSpaceID: 1, systemName: "labelColor") + let data = [UInt8](CSIWriter.color(name: "label", body: body)) + #expect(Array(data[(184 + 28)...]) == referenceBody) } } diff --git a/Tests/AssetKitTests/ColorRendererTests.swift b/Tests/AssetKitTests/ColorRendererTests.swift index 2483c8d..7a0115d 100644 --- a/Tests/AssetKitTests/ColorRendererTests.swift +++ b/Tests/AssetKitTests/ColorRendererTests.swift @@ -34,10 +34,7 @@ struct ColorRendererTests { Issue.record("expected color body") return } - #expect(abs(body.red - 0.5) < 1e-9) - #expect(abs(body.green - 0.25) < 1e-9) - #expect(abs(body.blue - 0.75) < 1e-9) - #expect(abs(body.alpha - 1) < 1e-9) + #expect(body.components == [0.5, 0.25, 0.75, 1]) #expect(body.colorSpaceID == 1) } @@ -69,7 +66,9 @@ struct ColorRendererTests { Issue.record("expected color body") return } - #expect(body.colorSpaceID == 2) + // actool 27.0 maps display-p3 to COLR colorspace 3 (srgb 1, + // gray-gamma-22 2, display-p3 3, extended-srgb 4, ...). + #expect(body.colorSpaceID == 3) } @Test("Parses 0x-prefixed hex components") @@ -97,8 +96,8 @@ struct ColorRendererTests { Issue.record("expected color body") return } - #expect(abs(body.red - 1) < 1e-9) - #expect(abs(body.green - 0) < 1e-9) - #expect(abs(body.blue - 128 / 255) < 1e-9) + #expect(body.components[0] == 1) + #expect(body.components[1] == 0) + #expect(abs(body.components[2] - 128 / 255) < 1e-9) } } diff --git a/Tests/AssetKitTests/ColorSpaceTests.swift b/Tests/AssetKitTests/ColorSpaceTests.swift new file mode 100644 index 0000000..7137818 --- /dev/null +++ b/Tests/AssetKitTests/ColorSpaceTests.swift @@ -0,0 +1,266 @@ +import Foundation +import Testing +@testable import AssetKit + +/// Pins color rendition bytes against the Xcode 27.0 actool oracle: one +/// colorset per color space (srgb, extended-srgb, display-p3, +/// extended-linear-srgb, gray-gamma-22, extended-gray), light + dark. +/// +/// Oracle facts: +/// - COLR colorspace IDs: srgb 1, gray-gamma-22 2, display-p3 3, +/// extended-srgb 4, extended-linear-srgb 5, extended-gray 6. +/// - Gray spaces carry 2 components (white, alpha); RGB spaces carry 4. +/// - Components are quantized to Float32 in the Float64 slots +/// (actool writes 1.1 as 0x3FF19999A0000000). +/// - Color-only catalogs get an 8-attribute KEYFORMAT (no dimension2), so +/// rendition keys are 16 bytes; colors key at element 85 / part 217. +/// - actool writes one BITMAPKEYS row per colorset with marker 0x02. +@Suite("ColorSpace") +struct ColorSpaceTests { + private func loadColorSet( + name: String, space: String, lightComponents: String, darkComponents: String + ) throws -> LoadedColorSet { + let json = """ + { + "colors" : [ + { + "idiom" : "universal", + "color" : { "platform" : "ios", "color-space" : "\(space)", "components" : \(lightComponents) } + }, + { + "idiom" : "universal", + "appearances" : [ { "appearance" : "luminosity", "value" : "dark" } ], + "color" : { "platform" : "ios", "color-space" : "\(space)", "components" : \(darkComponents) } + } + ], + "info" : { "author" : "xcode", "version" : 1 } + } + """ + let contents = try JSONDecoder().decode(ColorSetContents.self, from: Data(json.utf8)) + return LoadedColorSet(name: name, directory: URL(fileURLWithPath: "/"), contents: contents) + } + + private func rgbComponents(_ values: [Double]) -> String { + let keys = ["red", "green", "blue", "alpha"] + let pairs = zip(keys, values).map { "\"\($0)\" : \"\(stringified([$1]))\"" } + return "{ \(pairs.joined(separator: ", ")) }" + } + + private func grayComponents(white: Double, alpha: Double = 1) -> String { + "{ \"white\" : \"\(stringified([white]))\", \"alpha\" : \"\(stringified([alpha]))\" }" + } + + private func bytes(_ hex: String) -> Data { + var data = Data() + var index = hex.startIndex + while index < hex.endIndex { + let next = hex.index(index, offsetBy: 2) + data.append(UInt8(hex[index.. Rendition? { + renditions.first { $0.appearance?.darkLuminosity == true } + } + + private func lightRendition(_ renditions: [Rendition]) -> Rendition? { + renditions.first { $0.appearance?.darkLuminosity != true } + } + + /// 16-byte color rendition key per the oracle: base 8-attribute format. + private func colorKey(appearance: UInt16, identifier: UInt16) -> Data { + var w = ByteWriter() + for token: UInt16 in [appearance, 0, 1, 0, 0, identifier, 85, 217] { + w.writeLE(token) + } + return w.data + } + + @Test("Every color space byte-matches actool 27.0 (light and dark)") + func oracleParity() throws { + // (set name, color-space, light components, COLR colorspace id, rgb?, oracle CSI) + let cases: [(String, String, [Double], UInt8, Bool, String)] = [ + ("Srgb", "srgb", [1, 0.5, 0.25, 1], 1, true, + "495354430100000000000000000000000000000000000000000000000000000000000000f103000053726762000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000030000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000100000004000000000000000000f03f000000000000e03f000000000000d03f000000000000f03f"), + ("ExtSrgb", "extended-srgb", [1.2, -0.1, 0.5, 1], 4, true, + "495354430100000000000000000000000000000000000000000000000000000000000000f103000045787453726762000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000030000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000400000004000000000000403333f33f000000a09999b9bf000000000000e03f000000000000f03f"), + ("P3", "display-p3", [1, 0.4, 0.7, 1], 3, true, + "495354430100000000000000000000000000000000000000000000000000000000000000f103000050330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000030000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000300000004000000000000000000f03f000000a09999d93f000000606666e63f000000000000f03f"), + ("ExtLinear", "extended-linear-srgb", [0.5, 0.25, 0.125, 1], 5, true, + "495354430100000000000000000000000000000000000000000000000000000000000000f10300004578744c696e65617200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000030000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000500000004000000000000000000e03f000000000000d03f000000000000c03f000000000000f03f"), + ("Gray22", "gray-gamma-22", [0.75, 1], 2, false, + "495354430100000000000000000000000000000000000000000000000000000000000000f103000047726179323200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000020000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000200000002000000000000000000e83f000000000000f03f"), + ("ExtGray", "extended-gray", [1.1, 1], 6, false, + "495354430100000000000000000000000000000000000000000000000000000000000000f103000045787447726179000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c000000010000000000000020000000ec030000080000000000000000000000ee0300000400000001000000524c4f43010000000600000002000000000000a09999f13f000000000000f03f"), + ] + for (name, space, components, colorSpaceID, rgb, oracleHex) in cases { + let set = try loadColorSet( + name: name, space: space, + lightComponents: rgb + ? rgbComponents(components) + : grayComponents(white: components[0]), + darkComponents: rgb + ? rgbComponents([0, 0, 0, 1]) + : grayComponents(white: 0)) + let renditions = try ColorRenderer.renditions(for: set) + #expect(renditions.count == 2, "\(name)") + + let light = try #require(lightRendition(renditions)) + guard case .color(let lightBody) = light.body else { + Issue.record("\(name): expected color body") + return + } + #expect(lightBody.components == components, "\(name)") + #expect(lightBody.colorSpaceID == colorSpaceID, "\(name)") + let csi = CSIWriter.color(name: name, body: lightBody) + if csi != bytes(oracleHex) { + let hex = csi.map { String(format: "%02x", $0) }.joined() + Issue.record("\(name): CSI mismatch\n ours: \(hex)\n oracle: \(oracleHex)") + } + + // 16-byte rendition keys in the 8-attribute base format. + let identifier = UInt16(FacetKeys.nameHash(name) & 0xFFFF) + #expect(RenditionKey(rendition: light).encode(format: baseKeyFormat) + == colorKey(appearance: 0, identifier: identifier), "\(name)") + let dark = try #require(darkRendition(renditions)) + #expect(RenditionKey(rendition: dark).encode(format: baseKeyFormat) + == colorKey(appearance: 1, identifier: identifier), "\(name)") + } + + // Dark COLR bodies for a gray space (white 0) and extended-gray + // (white -0.2, alpha 0.5), from the oracle. + let grayDark = try loadColorSet(name: "Gray22", space: "gray-gamma-22", + lightComponents: grayComponents(white: 0.75), + darkComponents: grayComponents(white: 0)) + let grayDarkBody = try #require(darkRendition(try ColorRenderer.renditions(for: grayDark))) + guard case .color(let darkBody) = grayDarkBody.body else { + Issue.record("expected color body") + return + } + #expect(Array(CSIWriter.color(name: "Gray22", body: darkBody)[212...]) + == Array(bytes("524c4f430100000002000000020000000000000000000000000000000000f03f"))) + + // Extended-gray dark with the oracle's -0.2 white and 0.5 alpha. + let extGrayJSON = """ + { + "colors" : [ + { + "idiom" : "universal", + "appearances" : [ { "appearance" : "luminosity", "value" : "dark" } ], + "color" : { "platform" : "ios", "color-space" : "extended-gray", + "components" : { "white" : "-0.200", "alpha" : "0.500" } } + } + ], + "info" : { "author" : "xcode", "version" : 1 } + } + """ + let extContents = try JSONDecoder().decode( + ColorSetContents.self, from: Data(extGrayJSON.utf8)) + let extSet = LoadedColorSet(name: "ExtGray", directory: URL(fileURLWithPath: "/"), contents: extContents) + let extDark = try #require(darkRendition(try ColorRenderer.renditions(for: extSet))) + guard case .color(let extBody) = extDark.body else { + Issue.record("expected color body") + return + } + #expect(extBody.components == [-0.2, 0.5]) + #expect(Array(CSIWriter.color(name: "ExtGray", body: extBody)[212...]) + == Array(bytes("524c4f43010000000600000002000000000000a09999c9bf000000000000e03f"))) + } + + /// Renders the numeric test components as JSON strings. + private func stringified(_ values: [Double]) -> String { + values.map { value in + let rounded = (value * 1000).rounded() / 1000 + return String(format: "%.3f", rounded) + }.joined(separator: ", ") + } + + @Test("Decodes the real fullScreenBackgroundColor colorset (gray-gamma-22 dark)") + func realGrayColorset() throws { + let json = """ + { + "info" : { "version" : 1, "author" : "xcode" }, + "colors" : [ + { + "idiom" : "universal", + "color" : { "platform" : "ios", "reference" : "systemBackgroundColor" } + }, + { + "idiom" : "universal", + "appearances" : [ { "appearance" : "luminosity", "value" : "dark" } ], + "color" : { + "platform" : "ios", + "color-space" : "gray-gamma-22", + "components" : { "white" : "0.000", "alpha" : "1.000" } + } + } + ] + } + """ + let contents = try JSONDecoder().decode(ColorSetContents.self, from: Data(json.utf8)) + let set = LoadedColorSet( + name: "fullScreenBackgroundColor", + directory: URL(fileURLWithPath: "/"), + contents: contents + ) + let renditions = try ColorRenderer.renditions(for: set) + #expect(renditions.count == 2) + let dark = try #require(darkRendition(renditions)) + guard case .color(let body) = dark.body else { + Issue.record("expected color body") + return + } + #expect(body.components == [0, 1]) + #expect(body.colorSpaceID == COLRColorSpace.grayGamma22.rawValue) + + // Rendition key: 16 bytes, dark, element 85 / part 217, gray fallback + // name stays a system reference. + let identifier = UInt16(FacetKeys.nameHash("fullScreenBackgroundColor") & 0xFFFF) + #expect(RenditionKey(rendition: dark).encode(format: baseKeyFormat) + == colorKey(appearance: 1, identifier: identifier)) + } + + @Test("BITMAPKEYS carries a colorset row with marker 0x02") + func colorBitmapKeysRow() throws { + let set = try loadColorSet(name: "Srgb", space: "srgb", + lightComponents: rgbComponents([1, 0.5, 0.25, 1]), + darkComponents: rgbComponents([0, 0, 0, 1])) + let renditions = try ColorRenderer.renditions(for: set) + let descriptor = try #require(BitmapKeys.descriptor( + forAsset: "Srgb", renditions: renditions)) + // actool 27.0 value for a universal light+dark colorset, verbatim. + let encoded = descriptor.encode() + if encoded != bytes("01000000000000002400000008000000ffffffff01000000020000000100000001000000ffffffffffffffffffffffff") { + Issue.record("descriptor mismatch: \(encoded.map { String(format: "%02x", $0) }.joined())") + } + } + + @Test("Hex and 0-255 decimal component forms decode like Xcode's") + func componentForms() throws { + let json = """ + { + "colors" : [ + { + "idiom" : "universal", + "color" : { + "platform" : "ios", + "color-space" : "srgb", + "components" : { "red" : "1", "green" : "0x80", "blue" : "0", "alpha" : "1" } + } + } + ], + "info" : { "author" : "xcode", "version" : 1 } + } + """ + let contents = try JSONDecoder().decode(ColorSetContents.self, from: Data(json.utf8)) + let set = LoadedColorSet(name: "Forms", directory: URL(fileURLWithPath: "/"), contents: contents) + let renditions = try ColorRenderer.renditions(for: set) + guard case .color(let body) = renditions[0].body else { + Issue.record("expected color body") + return + } + #expect(body.components == [1, 128.0 / 255, 0, 1]) + } +} diff --git a/Tests/AssetKitTests/RenditionKeyTests.swift b/Tests/AssetKitTests/RenditionKeyTests.swift index 64a24f1..f8a54c7 100644 --- a/Tests/AssetKitTests/RenditionKeyTests.swift +++ b/Tests/AssetKitTests/RenditionKeyTests.swift @@ -30,7 +30,7 @@ struct RenditionKeyTests { element: 85, part: 220 ) - let data = key.encode() + let data = key.encode(format: v1KeyFormat) #expect(data.count == 18) let decoded = RenditionKey.decode(data) #expect(decoded == key) @@ -54,7 +54,7 @@ struct RenditionKeyTests { element: 0x100f, part: 0x1211 ) - let bytes = [UInt8](key.encode()) + let bytes = [UInt8](key.encode(format: v1KeyFormat)) #expect(bytes == [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, @@ -66,16 +66,16 @@ struct RenditionKeyTests { func sortOrder() { // First differing byte determines the order. Scale lives at bytes 4-5; // with appearance/localization both zero, scale orders ascending. - let scale2 = RenditionKey(scale: 2, idiom: 1).encode() - let scale3 = RenditionKey(scale: 3, idiom: 1).encode() + let scale2 = RenditionKey(scale: 2, idiom: 1).encode(format: v1KeyFormat) + let scale3 = RenditionKey(scale: 3, idiom: 1).encode(format: v1KeyFormat) #expect(BOMTree.byteCompare(scale2, scale3) < 0) // Idiom is at bytes 6-7, so it only breaks ties when earlier slots match. - let idiom1 = RenditionKey(scale: 2, idiom: 1).encode() - let idiom2 = RenditionKey(scale: 2, idiom: 2).encode() + let idiom1 = RenditionKey(scale: 2, idiom: 1).encode(format: v1KeyFormat) + let idiom2 = RenditionKey(scale: 2, idiom: 2).encode(format: v1KeyFormat) #expect(BOMTree.byteCompare(idiom1, idiom2) < 0) // Appearance is the highest-priority slot. - let appAny = RenditionKey(appearance: 0, scale: 3).encode() - let appDark = RenditionKey(appearance: 1, scale: 0).encode() + let appAny = RenditionKey(appearance: 0, scale: 3).encode(format: v1KeyFormat) + let appDark = RenditionKey(appearance: 1, scale: 0).encode(format: v1KeyFormat) #expect(BOMTree.byteCompare(appAny, appDark) < 0) }