Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
169 lines
11 KiB
Swift
169 lines
11 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 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",
|
||
]
|
||
}
|
||
|
||
public enum IPTCFieldKind: String, Codable, Sendable {
|
||
case text // single line
|
||
case multiline // caption / instructions
|
||
case list // keywords etc. – stored as [String]
|
||
}
|
||
|
||
public enum IPTCSection: String, CaseIterable, Codable, Sendable {
|
||
case description = "Beschreibung"
|
||
case location = "Ort"
|
||
case creator = "Urheber & Rechte"
|
||
case contact = "Kontakt des Urhebers"
|
||
case extended = "Erweitert (IPTC Core/Ext)"
|
||
}
|
||
|
||
/// 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] = [
|
||
// --- Beschreibung ---
|
||
f("caption", "Bildunterschrift", pm: "Caption", .description, .multiline,
|
||
iim: "IPTC:Caption-Abstract", xmp: "XMP-dc:Description", ns: XMPNamespace.dc, local: "description", container: .alt, max: 2000),
|
||
f("captionWriter", "Verfasser Bildunterschrift", pm: "Caption Writer", .description,
|
||
iim: "IPTC:Writer-Editor", xmp: "XMP-photoshop:CaptionWriter", ns: XMPNamespace.photoshop, local: "CaptionWriter", max: 32),
|
||
f("headline", "Überschrift", pm: "Headline", .description,
|
||
iim: "IPTC:Headline", xmp: "XMP-photoshop:Headline", ns: XMPNamespace.photoshop, local: "Headline", max: 256),
|
||
f("instructions", "Anweisungen", pm: "Special Instructions", .description, .multiline,
|
||
iim: "IPTC:SpecialInstructions", xmp: "XMP-photoshop:Instructions", ns: XMPNamespace.photoshop, local: "Instructions", max: 256),
|
||
f("transmissionReference", "Job-ID / Übertragungsreferenz", pm: "Transmission Reference", .description,
|
||
iim: "IPTC:OriginalTransmissionReference", xmp: "XMP-photoshop:TransmissionReference", ns: XMPNamespace.photoshop, local: "TransmissionReference", max: 32),
|
||
f("objectName", "Titel (Objektname)", pm: "Object Name", .description,
|
||
iim: "IPTC:ObjectName", xmp: "XMP-dc:Title", ns: XMPNamespace.dc, local: "title", container: .alt, max: 64),
|
||
f("keywords", "Stichwörter", pm: "Keywords", .description, .list,
|
||
iim: "IPTC:Keywords", xmp: "XMP-dc:Subject", ns: XMPNamespace.dc, local: "subject", container: .bag, max: 64),
|
||
f("category", "Kategorie", pm: "Category", .description,
|
||
iim: "IPTC:Category", xmp: "XMP-photoshop:Category", ns: XMPNamespace.photoshop, local: "Category", max: 3),
|
||
f("supplementalCategories", "Zusatzkategorien", pm: "Supplemental Categories", .description, .list,
|
||
iim: "IPTC:SupplementalCategories", xmp: "XMP-photoshop:SupplementalCategories", ns: XMPNamespace.photoshop, local: "SupplementalCategories", container: .bag, max: 32),
|
||
f("urgency", "Dringlichkeit", pm: "Urgency", .description,
|
||
iim: "IPTC:Urgency", xmp: "XMP-photoshop:Urgency", ns: XMPNamespace.photoshop, local: "Urgency", max: 1),
|
||
f("dateCreated", "Erstellungsdatum (JJJJ-MM-TT)", pm: "Date Created", .description,
|
||
iim: "IPTC:DateCreated", xmp: "XMP-photoshop:DateCreated", ns: XMPNamespace.photoshop, local: "DateCreated", max: 8),
|
||
|
||
// --- Ort ---
|
||
f("event", "Ereignis", pm: "Event", .location,
|
||
iim: nil, xmp: "XMP-iptcExt:Event", ns: XMPNamespace.iptcExt, local: "Event", container: .alt),
|
||
f("sublocation", "Ortsdetail", pm: "Location / Sublocation", .location,
|
||
iim: "IPTC:Sub-location", xmp: "XMP-iptcCore:Location", ns: XMPNamespace.iptcCore, local: "Location", max: 32),
|
||
f("city", "Stadt", pm: "City", .location,
|
||
iim: "IPTC:City", xmp: "XMP-photoshop:City", ns: XMPNamespace.photoshop, local: "City", max: 32),
|
||
f("state", "Bundesland", pm: "State / Province", .location,
|
||
iim: "IPTC:Province-State", xmp: "XMP-photoshop:State", ns: XMPNamespace.photoshop, local: "State", max: 32),
|
||
f("country", "Land", pm: "Country", .location,
|
||
iim: "IPTC:Country-PrimaryLocationName", xmp: "XMP-photoshop:Country", ns: XMPNamespace.photoshop, local: "Country", max: 64),
|
||
f("countryCode", "Ländercode (ISO)", pm: "Country Code", .location,
|
||
iim: "IPTC:Country-PrimaryLocationCode", xmp: "XMP-iptcCore:CountryCode", ns: XMPNamespace.iptcCore, local: "CountryCode", max: 3),
|
||
|
||
// --- Urheber & Rechte ---
|
||
f("creator", "Fotograf", pm: "Photographer / Creator", .creator,
|
||
iim: "IPTC:By-line", xmp: "XMP-dc:Creator", ns: XMPNamespace.dc, local: "creator", container: .seq, max: 32),
|
||
f("creatorJobTitle", "Position des Fotografen", pm: "Photographer's Title", .creator,
|
||
iim: "IPTC:By-lineTitle", xmp: "XMP-photoshop:AuthorsPosition", ns: XMPNamespace.photoshop, local: "AuthorsPosition", max: 32),
|
||
f("credit", "Credit / Anbieter", pm: "Credit", .creator,
|
||
iim: "IPTC:Credit", xmp: "XMP-photoshop:Credit", ns: XMPNamespace.photoshop, local: "Credit", max: 32),
|
||
f("source", "Quelle", pm: "Source", .creator,
|
||
iim: "IPTC:Source", xmp: "XMP-photoshop:Source", ns: XMPNamespace.photoshop, local: "Source", max: 32),
|
||
f("copyright", "Copyright-Vermerk", pm: "Copyright Notice", .creator,
|
||
iim: "IPTC:CopyrightNotice", xmp: "XMP-dc:Rights", ns: XMPNamespace.dc, local: "rights", container: .alt, max: 128),
|
||
f("usageTerms", "Nutzungsbedingungen", pm: "Rights Usage Terms", .creator, .multiline,
|
||
iim: nil, xmp: "XMP-xmpRights:UsageTerms", ns: XMPNamespace.xmpRights, local: "UsageTerms", container: .alt),
|
||
f("webStatement", "Copyright-URL", pm: "Copyright Info URL", .creator,
|
||
iim: nil, xmp: "XMP-xmpRights:WebStatement", ns: XMPNamespace.xmpRights, local: "WebStatement"),
|
||
|
||
// --- Kontakt (IPTC Core CreatorContactInfo) ---
|
||
f("ciAddress", "Adresse", pm: "Address", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorAddress", ns: XMPNamespace.iptcCore, local: "CiAdrExtadr"),
|
||
f("ciCity", "Stadt", pm: "City (Contact)", .contact,
|
||
iim: nil, xmp: "XMP-iptcCore:CreatorCity", ns: XMPNamespace.iptcCore, local: "CiAdrCity"),
|
||
f("ciRegion", "Bundesland", pm: "State (Contact)", .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)", .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"),
|
||
|
||
// --- Erweitert ---
|
||
f("intellectualGenre", "Genre", pm: "Intellectual Genre", .extended,
|
||
iim: "IPTC:ObjectAttributeReference", xmp: "XMP-iptcCore:IntellectualGenre", ns: XMPNamespace.iptcCore, local: "IntellectualGenre", max: 68),
|
||
f("scene", "Szenen-Code", pm: "IPTC Scene", .extended, .list,
|
||
iim: nil, xmp: "XMP-iptcCore:Scene", ns: XMPNamespace.iptcCore, local: "Scene", container: .bag),
|
||
f("subjectCode", "Themen-Code", pm: "IPTC Subject Code", .extended, .list,
|
||
iim: "IPTC:SubjectReference", xmp: "XMP-iptcCore:SubjectCode", ns: XMPNamespace.iptcCore, local: "SubjectCode", container: .bag, max: 236),
|
||
f("personInImage", "Abgebildete Personen", pm: "Person Shown", .extended, .list,
|
||
iim: nil, xmp: "XMP-iptcExt:PersonInImage", ns: XMPNamespace.iptcExt, local: "PersonInImage", container: .bag),
|
||
]
|
||
|
||
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)
|
||
}
|
||
}
|