Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
217 lines
15 KiB
Swift
217 lines
15 KiB
Swift
import Foundation
|
||
|
||
/// XMP namespaces as written by Photo Mechanic, Lightroom and Photoshop.
|
||
public enum XMPNamespace {
|
||
public static let dc = "http://purl.org/dc/elements/1.1/"
|
||
public static let photoshop = "http://ns.adobe.com/photoshop/1.0/"
|
||
public static let iptcCore = "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"
|
||
public static let iptcExt = "http://iptc.org/std/Iptc4xmpExt/2008-02-29/"
|
||
public static let xmpRights = "http://ns.adobe.com/xap/1.0/rights/"
|
||
public static let xmp = "http://ns.adobe.com/xap/1.0/"
|
||
public static let photomechanic = "http://ns.camerabits.com/photomechanic/1.0/"
|
||
public static let plus = "http://ns.useplus.org/ldf/xmp/1.0/"
|
||
public static let rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||
|
||
/// Prefix normally used when we write a template file back out.
|
||
public static let preferredPrefix: [String: String] = [
|
||
dc: "dc", photoshop: "photoshop", iptcCore: "Iptc4xmpCore", iptcExt: "Iptc4xmpExt",
|
||
xmpRights: "xmpRights", xmp: "xmp", photomechanic: "photomechanic", plus: "plus",
|
||
]
|
||
}
|
||
|
||
public enum IPTCFieldKind: String, Codable, Sendable {
|
||
case text // single line
|
||
case multiline // caption / instructions
|
||
case list // keywords etc. – stored as [String]
|
||
}
|
||
|
||
/// Sections in Photo Mechanic's IPTC window order. `column` places them like PM (left / right).
|
||
public enum IPTCSection: String, CaseIterable, Codable, Sendable {
|
||
case imageContent = "Image Content"
|
||
case imageRights = "Image Rights"
|
||
case contact = "Creator Contact Info"
|
||
case eventLocation = "Event and Location"
|
||
case locationCreated = "Locations Taken and Shown"
|
||
case status = "Status"
|
||
|
||
public var column: Int {
|
||
switch self {
|
||
case .imageContent, .imageRights, .contact: return 0
|
||
case .eventLocation, .locationCreated, .status: return 1
|
||
}
|
||
}
|
||
}
|
||
|
||
/// How an XMP property is stored inside rdf.
|
||
public enum XMPContainer: String, Codable, Sendable {
|
||
case simple, alt, seq, bag
|
||
}
|
||
|
||
/// One IPTC field. The order of `IPTCField.all` mirrors the Photo Mechanic
|
||
/// IPTC Stationery Pad so agency workflows stay the same.
|
||
public struct IPTCField: Identifiable, Hashable, Sendable {
|
||
public let id: String
|
||
public let label: String
|
||
/// Name as shown in Photo Mechanic (helps when switching).
|
||
public let pmLabel: String
|
||
public let section: IPTCSection
|
||
public let kind: IPTCFieldKind
|
||
/// exiftool tag for the legacy IPTC-IIM record (nil = XMP only).
|
||
public let iimTag: String?
|
||
/// exiftool tag for XMP.
|
||
public let xmpTag: String
|
||
/// XMP property (namespace URI + local name) used in PM templates.
|
||
public let xmpNamespace: String
|
||
public let xmpLocalName: String
|
||
public let xmpContainer: XMPContainer
|
||
/// Byte limit of the IIM dataset (UTF-8 bytes). Agencies truncate/reject above this.
|
||
public let iimMaxBytes: Int?
|
||
|
||
public static func == (a: IPTCField, b: IPTCField) -> Bool { a.id == b.id }
|
||
public func hash(into h: inout Hasher) { h.combine(id) }
|
||
}
|
||
|
||
public extension IPTCField {
|
||
private static func f(_ id: String, _ label: String, pm: String, _ section: IPTCSection, _ kind: IPTCFieldKind = .text,
|
||
iim: String?, xmp: String, ns: String, local: String, container: XMPContainer = .simple,
|
||
max: Int? = nil) -> IPTCField {
|
||
IPTCField(id: id, label: label, pmLabel: pm, section: section, kind: kind, iimTag: iim, xmpTag: xmp,
|
||
xmpNamespace: ns, xmpLocalName: local, xmpContainer: container, iimMaxBytes: max)
|
||
}
|
||
|
||
static let all: [IPTCField] = [
|
||
// --- imageContent ---
|
||
f("caption", "Beschreibung", pm: "Description/Caption", .imageContent, .multiline,
|
||
iim: "IPTC:Caption-Abstract", xmp: "XMP-dc:Description", ns: XMPNamespace.dc, local: "description", container: .alt, max: 2000),
|
||
f("altText", "Alt-Text", pm: "Alt Text", .imageContent, .multiline,
|
||
iim: nil, xmp: "XMP-iptcCore:AltTextAccessibility", ns: XMPNamespace.iptcCore, local: "AltTextAccessibility", container: .alt),
|
||
f("extDescription", "Erweiterte Beschreibung", pm: "Extended Desc", .imageContent, .multiline,
|
||
iim: nil, xmp: "XMP-iptcCore:ExtDescrAccessibility", ns: XMPNamespace.iptcCore, local: "ExtDescrAccessibility", container: .alt),
|
||
f("captionWriter", "Verfasser", pm: "Description Writers", .imageContent,
|
||
iim: "IPTC:Writer-Editor", xmp: "XMP-photoshop:CaptionWriter", ns: XMPNamespace.photoshop, local: "CaptionWriter", max: 32),
|
||
f("headline", "Überschrift", pm: "Headline", .imageContent, .multiline,
|
||
iim: "IPTC:Headline", xmp: "XMP-photoshop:Headline", ns: XMPNamespace.photoshop, local: "Headline", max: 256),
|
||
f("keywords", "Stichwörter", pm: "Keywords", .imageContent, .list,
|
||
iim: "IPTC:Keywords", xmp: "XMP-dc:Subject", ns: XMPNamespace.dc, local: "subject", container: .bag, max: 64),
|
||
f("personInImage", "Abgebildete Personen", pm: "Persons shown", .imageContent, .list,
|
||
iim: nil, xmp: "XMP-iptcExt:PersonInImage", ns: XMPNamespace.iptcExt, local: "PersonInImage", container: .bag),
|
||
f("orgName", "Organisation (Name)", pm: "Featured Org. Name", .imageContent, .list,
|
||
iim: nil, xmp: "XMP-iptcExt:OrganisationInImageName", ns: XMPNamespace.iptcExt, local: "OrganisationInImageName", container: .bag),
|
||
f("orgCode", "Organisation (Code)", pm: "Featured Org. Code", .imageContent, .list,
|
||
iim: nil, xmp: "XMP-iptcExt:OrganisationInImageCode", ns: XMPNamespace.iptcExt, local: "OrganisationInImageCode", container: .bag),
|
||
f("scene", "Szene", pm: "IPTC Scene", .imageContent, .list,
|
||
iim: nil, xmp: "XMP-iptcCore:Scene", ns: XMPNamespace.iptcCore, local: "Scene", container: .bag),
|
||
// --- imageRights ---
|
||
f("creator", "Fotograf", pm: "Creator/Photographer", .imageRights,
|
||
iim: "IPTC:By-line", xmp: "XMP-dc:Creator", ns: XMPNamespace.dc, local: "creator", container: .seq, max: 32),
|
||
f("creatorIdentity", "Urheber-ID", pm: "Creator's Identity", .imageRights,
|
||
iim: nil, xmp: "XMP-photomech:CreatorIdentity", ns: XMPNamespace.photomechanic, local: "CreatorIdentity"),
|
||
f("creatorJobTitle", "Position", pm: "Creator's Job Title", .imageRights,
|
||
iim: "IPTC:By-lineTitle", xmp: "XMP-photoshop:AuthorsPosition", ns: XMPNamespace.photoshop, local: "AuthorsPosition", max: 32),
|
||
f("credit", "Credit", pm: "Credit Line", .imageRights,
|
||
iim: "IPTC:Credit", xmp: "XMP-photoshop:Credit", ns: XMPNamespace.photoshop, local: "Credit", max: 32),
|
||
f("source", "Quelle", pm: "Source", .imageRights,
|
||
iim: "IPTC:Source", xmp: "XMP-photoshop:Source", ns: XMPNamespace.photoshop, local: "Source", max: 32),
|
||
f("copyright", "Copyright", pm: "Copyright Notice", .imageRights,
|
||
iim: "IPTC:CopyrightNotice", xmp: "XMP-dc:Rights", ns: XMPNamespace.dc, local: "rights", container: .alt, max: 128),
|
||
f("copyrightMarked", "Copyright-Status", pm: "Copyright Status (True/False)", .imageRights,
|
||
iim: nil, xmp: "XMP-xmpRights:Marked", ns: XMPNamespace.xmpRights, local: "Marked"),
|
||
f("usageTerms", "Nutzungsbedingungen", pm: "Rights Usage Terms", .imageRights, .multiline,
|
||
iim: nil, xmp: "XMP-xmpRights:UsageTerms", ns: XMPNamespace.xmpRights, local: "UsageTerms", container: .alt),
|
||
f("webStatement", "Copyright-URL", pm: "Copyright Info URL", .imageRights,
|
||
iim: nil, xmp: "XMP-xmpRights:WebStatement", ns: XMPNamespace.xmpRights, local: "WebStatement"),
|
||
f("dataMining", "Data Mining", pm: "Data Mining (PLUS)", .imageRights,
|
||
iim: nil, xmp: "XMP-plus:DataMining#", ns: XMPNamespace.plus, local: "DataMining"),
|
||
// --- contact ---
|
||
f("ciAddress", "Adresse", pm: "Address", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorAddress", ns: XMPNamespace.iptcCore, local: "CiAdrExtadr"),
|
||
f("ciCity", "Stadt", pm: "City", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorCity", ns: XMPNamespace.iptcCore, local: "CiAdrCity"),
|
||
f("ciRegion", "Bundesland", pm: "State/Province", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorRegion", ns: XMPNamespace.iptcCore, local: "CiAdrRegion"),
|
||
f("ciPostalCode", "PLZ", pm: "Postal Code", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorPostalCode", ns: XMPNamespace.iptcCore, local: "CiAdrPcode"),
|
||
f("ciCountry", "Land", pm: "Country", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorCountry", ns: XMPNamespace.iptcCore, local: "CiAdrCtry"),
|
||
f("ciPhone", "Telefon", pm: "Phone", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorWorkTelephone", ns: XMPNamespace.iptcCore, local: "CiTelWork"),
|
||
f("ciEmail", "E-Mail", pm: "E-Mail", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorWorkEmail", ns: XMPNamespace.iptcCore, local: "CiEmailWork"),
|
||
f("ciURL", "Website", pm: "Web URL", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorWorkURL", ns: XMPNamespace.iptcCore, local: "CiUrlWork"),
|
||
// --- eventLocation ---
|
||
f("event", "Ereignis", pm: "Event", .eventLocation,
|
||
iim: nil, xmp: "XMP-iptcExt:Event", ns: XMPNamespace.iptcExt, local: "Event", container: .alt),
|
||
f("dateCreated", "Datum", pm: "Date", .eventLocation,
|
||
iim: "IPTC:DateCreated", xmp: "XMP-photoshop:DateCreated", ns: XMPNamespace.photoshop, local: "DateCreated", max: 8),
|
||
f("city", "Stadt", pm: "City", .eventLocation,
|
||
iim: "IPTC:City", xmp: "XMP-photoshop:City", ns: XMPNamespace.photoshop, local: "City", max: 32),
|
||
f("sublocation", "Ort", pm: "Location", .eventLocation,
|
||
iim: "IPTC:Sub-location", xmp: "XMP-iptcCore:Location", ns: XMPNamespace.iptcCore, local: "Location", max: 32),
|
||
f("state", "Bundesland", pm: "State/Province", .eventLocation,
|
||
iim: "IPTC:Province-State", xmp: "XMP-photoshop:State", ns: XMPNamespace.photoshop, local: "State", max: 32),
|
||
f("country", "Land", pm: "Country", .eventLocation,
|
||
iim: "IPTC:Country-PrimaryLocationName", xmp: "XMP-photoshop:Country", ns: XMPNamespace.photoshop, local: "Country", max: 64),
|
||
f("countryCode", "Ländercode", pm: "ISO Country Code", .eventLocation,
|
||
iim: "IPTC:Country-PrimaryLocationCode", xmp: "XMP-iptcCore:CountryCode", ns: XMPNamespace.iptcCore, local: "CountryCode", max: 3),
|
||
f("subjectCode", "Themen-Code", pm: "IPTC Subject Code", .eventLocation, .list,
|
||
iim: "IPTC:SubjectReference", xmp: "XMP-iptcCore:SubjectCode", ns: XMPNamespace.iptcCore, local: "SubjectCode", container: .bag, max: 236),
|
||
f("intellectualGenre", "Genre", pm: "Intellectual Genre", .eventLocation,
|
||
iim: "IPTC:ObjectAttributeReference", xmp: "XMP-iptcCore:IntellectualGenre", ns: XMPNamespace.iptcCore, local: "IntellectualGenre", max: 68),
|
||
// --- locationCreated ---
|
||
f("lcSublocation", "Ortsdetail", pm: "Sublocation", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedSublocation", ns: XMPNamespace.iptcExt, local: "Sublocation"),
|
||
f("lcCity", "Stadt", pm: "City", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedCity", ns: XMPNamespace.iptcExt, local: "City"),
|
||
f("lcState", "Bundesland", pm: "State", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedProvinceState", ns: XMPNamespace.iptcExt, local: "ProvinceState"),
|
||
f("lcCountry", "Land", pm: "Country", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedCountryName", ns: XMPNamespace.iptcExt, local: "CountryName"),
|
||
f("lcCountryCode", "Ländercode", pm: "Country Code", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedCountryCode", ns: XMPNamespace.iptcExt, local: "CountryCode"),
|
||
f("lcWorldRegion", "Weltregion", pm: "World Region", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedWorldRegion", ns: XMPNamespace.iptcExt, local: "WorldRegion"),
|
||
f("lcLocationId", "Orts-ID", pm: "Location ID", .locationCreated,
|
||
iim: nil, xmp: "XMP-iptcExt:LocationCreatedLocationId", ns: XMPNamespace.iptcExt, local: "LocationId"),
|
||
// --- status ---
|
||
f("objectName", "Titel", pm: "Title", .status,
|
||
iim: "IPTC:ObjectName", xmp: "XMP-dc:Title", ns: XMPNamespace.dc, local: "title", container: .alt, max: 64),
|
||
f("transmissionReference", "Job-ID", pm: "Job Identifier", .status,
|
||
iim: "IPTC:OriginalTransmissionReference", xmp: "XMP-photoshop:TransmissionReference", ns: XMPNamespace.photoshop, local: "TransmissionReference", max: 32),
|
||
f("instructions", "Anweisungen", pm: "Instructions", .status, .multiline,
|
||
iim: "IPTC:SpecialInstructions", xmp: "XMP-photoshop:Instructions", ns: XMPNamespace.photoshop, local: "Instructions", max: 256),
|
||
f("urgency", "Dringlichkeit", pm: "Urgency", .status,
|
||
iim: "IPTC:Urgency", xmp: "XMP-photoshop:Urgency", ns: XMPNamespace.photoshop, local: "Urgency", max: 1),
|
||
f("category", "Kategorie", pm: "Category", .status,
|
||
iim: "IPTC:Category", xmp: "XMP-photoshop:Category", ns: XMPNamespace.photoshop, local: "Category", max: 3),
|
||
f("supplementalCategories", "Zusatzkategorien", pm: "Supplemental Categories", .status, .list,
|
||
iim: "IPTC:SupplementalCategories", xmp: "XMP-photoshop:SupplementalCategories", ns: XMPNamespace.photoshop, local: "SupplementalCategories", container: .bag, max: 32),
|
||
]
|
||
|
||
/// Key in exiftool's JSON output (a trailing "#" only disables print conversion).
|
||
var xmpReadKey: String { xmpTag.hasSuffix("#") ? String(xmpTag.dropLast()) : xmpTag }
|
||
|
||
/// IIM dataset (record 2) per field. Photo Mechanic's .snap lists applied fields as
|
||
/// 0x200 + dataset (e.g. 0x25a = 2:90 City).
|
||
static let iimDataset: [String: Int] = [
|
||
"objectName": 5, "urgency": 10, "category": 15, "supplementalCategories": 20, "keywords": 25,
|
||
"instructions": 40, "dateCreated": 55, "creator": 80, "creatorJobTitle": 85, "city": 90,
|
||
"sublocation": 92, "state": 95, "countryCode": 100, "country": 101, "transmissionReference": 103,
|
||
"headline": 105, "credit": 110, "source": 115, "copyright": 116, "caption": 120, "captionWriter": 122,
|
||
"intellectualGenre": 4, "subjectCode": 12,
|
||
]
|
||
|
||
static let byID: [String: IPTCField] = Dictionary(uniqueKeysWithValues: all.map { ($0.id, $0) })
|
||
|
||
/// Lookup by XMP namespace + local name (used by the PM template importer).
|
||
static func forXMP(namespace: String, localName: String) -> IPTCField? {
|
||
all.first { $0.xmpNamespace == namespace && $0.xmpLocalName == localName }
|
||
}
|
||
|
||
/// Byte length check against the IIM limit. Returns overflow in bytes (0 = ok).
|
||
func overflow(of value: String) -> Int {
|
||
guard let max = iimMaxBytes else { return 0 }
|
||
return Swift.max(0, value.utf8.count - max)
|
||
}
|
||
}
|