Files
ICCery-CPU/Sources/Printing/ColorMatching.swift
Local Admin fe68c5c01d
Build macOS Packages / Build macOS (Intel) (push) Successful in 38s
Build macOS Packages / Build macOS (Apple Silicon) (push) Successful in 38s
Build macOS Packages / Build macOS (Universal) (push) Failing after 9s
build fixes
2026-09-07 06:20:29 -07:00

99 lines
3.7 KiB
Swift

import Foundation
import AppKit
/// SPEC §7.1 — Quartz / ColorSync layer (`NSPrintInfo` + CorePrinting).
enum ColorMatchingMode: String, CaseIterable {
case unmanaged
case colorsync
case driver
/// `PMColorMatchingMode` values from SPEC §7.1.
var pmValue: String {
switch self {
case .unmanaged: return "APCustomColorMatching"
case .colorsync: return "APColorSync"
case .driver: return "APPrinterExtension"
}
}
var title: String {
switch self {
case .unmanaged: return "No Colour Management"
case .colorsync: return "ColorSync"
case .driver: return "Driver Managed"
}
}
static func from(forceUnmanagedColor: Bool) -> ColorMatchingMode {
forceUnmanagedColor ? .unmanaged : .colorsync
}
}
enum ColorMatching {
static let modeKey = "PMColorMatchingMode"
static let profileKey = "PMCustomColorMatchingProfile"
static let legacyModeKey = "com.apple.print.PrintSettings.PMColorMatchingMode"
/// Inject CorePrinting colour-matching keys into `NSPrintInfo`.
static func apply(_ mode: ColorMatchingMode, to printInfo: NSPrintInfo) {
let dict = printInfo.dictionary()
dict[NSPrintInfo.AttributeKey(rawValue: modeKey)] = mode.pmValue
dict[NSPrintInfo.AttributeKey(rawValue: profileKey)] = mode == .unmanaged ? "" : "System"
dict[NSPrintInfo.AttributeKey(rawValue: legacyModeKey)] = mode.pmValue
let nestedKey = NSPrintInfo.AttributeKey(rawValue: "com.apple.print.printSettings")
let nested: NSMutableDictionary
if let existing = dict[nestedKey] as? NSMutableDictionary {
nested = existing
} else if let existing = dict[nestedKey] as? [AnyHashable: Any] {
nested = NSMutableDictionary(dictionary: existing)
dict[nestedKey] = nested
} else {
nested = NSMutableDictionary()
dict[nestedKey] = nested
}
nested[modeKey] = mode.pmValue
nested[legacyModeKey] = mode.pmValue
if mode == .unmanaged {
nested[profileKey] = ""
}
TPLog.info("Color matching mode \(mode.pmValue)")
}
/// SPEC §7.2 — omit Colour Matching accessory when `lockColorManagement`.
static func configurePanel(_ panel: NSPrintPanel, lockColorManagement: Bool, allowBasicDriverChanges: Bool) {
var options: NSPrintPanel.Options = [
.showsCopies,
.showsPageRange,
.showsPreview,
]
if allowBasicDriverChanges {
options.insert(.showsPaperSize)
options.insert(.showsOrientation)
options.insert(.showsScaling)
}
panel.options = options
if lockColorManagement {
stripColorMatchingAccessories(from: panel)
}
}
static func stripColorMatchingAccessories(from panel: NSPrintPanel) {
let accessories = panel.accessoryControllers
for ac in accessories {
let cls = NSStringFromClass(type(of: ac))
let title = (ac.title ?? "") + " " + (ac.nibName ?? "") + " " + cls
let hay = title.lowercased()
if hay.contains("color matching") || hay.contains("colour matching") || hay.contains("colormatch") {
// SDK types this as NSViewController; removeAccessoryController
// requires NSPrintPanelAccessorizing (SPEC §7.2).
if let accessor = ac as? (NSViewController & NSPrintPanelAccessorizing) {
panel.removeAccessoryController(accessor)
TPLog.info("Removed print-panel accessory: \(title)")
}
}
}
}
}