From 1bf32830290640110260448f93e3dd1960fb3d0f Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 9 Nov 2026 16:12:01 +0100 Subject: [PATCH] =?UTF-8?q?UI=20verbessert,=20L+K=20Dienst=20von=20einer?= =?UTF-8?q?=20Person=20m=C3=B6glich?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/LektorenPlan/AppMetadata.swift | 27 + .../PDF/CompactPlanPDFRenderer.swift | 520 ++++++++++++++++++ 2 files changed, 547 insertions(+) create mode 100644 Sources/LektorenPlan/AppMetadata.swift create mode 100644 Sources/LektorenPlan/PDF/CompactPlanPDFRenderer.swift diff --git a/Sources/LektorenPlan/AppMetadata.swift b/Sources/LektorenPlan/AppMetadata.swift new file mode 100644 index 0000000..6e9c76f --- /dev/null +++ b/Sources/LektorenPlan/AppMetadata.swift @@ -0,0 +1,27 @@ +import Foundation + +/// Central version and build metadata for the UI and the About panel. +enum AppMetadata { + static let version = "0.9" + + static var buildDate: Date { + if let raw = Bundle.main.object(forInfoDictionaryKey: "BuildDate") as? String, + let date = ISO8601DateFormatter().date(from: raw) { + return date + } + // When launched from SwiftPM/Xcode there is no generated Info.plist key. + if let executable = Bundle.main.executableURL, + let attributes = try? FileManager.default.attributesOfItem(atPath: executable.path), + let date = attributes[.modificationDate] as? Date { + return date + } + return Date() + } + + static var buildDateLabel: String { + let formatter = DateFormatter() + formatter.locale = Locale(identifier: "de_DE") + formatter.dateFormat = "dd.MM.yyyy, HH:mm" + return formatter.string(from: buildDate) + } +} diff --git a/Sources/LektorenPlan/PDF/CompactPlanPDFRenderer.swift b/Sources/LektorenPlan/PDF/CompactPlanPDFRenderer.swift new file mode 100644 index 0000000..db8cc95 --- /dev/null +++ b/Sources/LektorenPlan/PDF/CompactPlanPDFRenderer.swift @@ -0,0 +1,520 @@ +import AppKit +import CoreGraphics +import LektorenPlanCore + +/// Renders the reduced A4 portrait list: only days with services, one row per +/// service, church/location and time on the left, last names per role beside it. +enum CompactPlanPDFRenderer { + private static let pageWidth: CGFloat = 595 + private static let pageHeight: CGFloat = 842 + private static let margin: CGFloat = 36 + private static let headerHeight: CGFloat = 70 + private static let columnHeaderHeight: CGFloat = 23 + private static let footerHeight: CGFloat = 24 + + private static let columnGap: CGFloat = 8 + private static let dateWidth: CGFloat = 72 + private static let serviceWidth: CGFloat = 196 + private static let lectorWidth: CGFloat = 116 + private static var helperWidth: CGFloat { + pageWidth - margin * 2 - dateWidth - serviceWidth - lectorWidth - columnGap * 3 + } + + // MARK: - Colours + + private static let ink = NSColor(calibratedRed: 0.12, green: 0.135, blue: 0.155, alpha: 1) + private static let secondaryInk = NSColor(calibratedRed: 0.38, green: 0.385, blue: 0.39, alpha: 1) + private static let tertiaryInk = NSColor(calibratedRed: 0.52, green: 0.51, blue: 0.49, alpha: 1) + private static let lineColor = NSColor(calibratedRed: 0.845, green: 0.825, blue: 0.79, alpha: 1) + private static let accent = NSColor(calibratedRed: 0.18, green: 0.36, blue: 0.34, alpha: 1) + private static let gold = NSColor(calibratedRed: 0.72, green: 0.53, blue: 0.23, alpha: 1) + private static let burgundy = NSColor(calibratedRed: 0.49, green: 0.24, blue: 0.29, alpha: 1) + private static let sundayWash = NSColor(calibratedRed: 0.985, green: 0.955, blue: 0.885, alpha: 0.55) + private static let feastWash = NSColor(calibratedRed: 0.998, green: 0.955, blue: 0.79, alpha: 0.65) + private static let openColor = NSColor(calibratedRed: 0.70, green: 0.42, blue: 0.13, alpha: 1) + + // MARK: - Fonts + + private static func serif(_ size: CGFloat, bold: Bool = false) -> NSFont { + let name = bold ? "NewYork-Semibold" : "NewYork-Regular" + return NSFont(name: name, size: size) ?? NSFont.systemFont(ofSize: size, weight: bold ? .semibold : .regular) + } + + private static func sans(_ size: CGFloat, weight: NSFont.Weight = .regular) -> NSFont { + NSFont.systemFont(ofSize: size, weight: weight) + } + + // MARK: - Render + + static func render( + range: DayRange, + rows: [DayPlanRow], + people: [String: Person], + generatedAt: Date + ) -> Data { + let data = NSMutableData() + var mediaBox = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight) + guard let consumer = CGDataConsumer(data: data as CFMutableData), + let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil) + else { + return Data() + } + storedContext = context + defer { storedContext = nil } + + let days = rows + .filter { !$0.services.isEmpty } + .sorted { $0.date < $1.date } + let contentTop = margin + headerHeight + columnHeaderHeight + 6 + let contentBottom = pageHeight - margin - footerHeight + + var pageIndex = 0 + + func beginPage() { + context.beginPDFPage(nil) + context.saveGState() + context.translateBy(x: 0, y: pageHeight) + context.scaleBy(x: 1, y: -1) + NSGraphicsContext.saveGraphicsState() + NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: true) + } + + func endPage() { + drawFooter(pageIndex: pageIndex, range: range) + NSGraphicsContext.restoreGraphicsState() + context.restoreGState() + context.endPDFPage() + pageIndex += 1 + } + + func startPage(continuation: Bool) { + beginPage() + drawHeader(range: range, generatedAt: generatedAt, continuation: continuation) + drawColumnHeader() + } + + startPage(continuation: false) + var cursorY = contentTop + + for day in days { + let services = day.services.sorted { + if $0.service.timeMinutes != $1.service.timeMinutes { + return $0.service.timeMinutes < $1.service.timeMinutes + } + return $0.church.sortIndex < $1.church.sortIndex + } + + for (serviceIndex, service) in services.enumerated() { + let height = rowHeight(service: service, people: people) + if cursorY + height > contentBottom { + endPage() + startPage(continuation: true) + cursorY = contentTop + } + drawServiceRow( + day: day, + service: service, + isFirstOfDay: serviceIndex == 0, + y: cursorY, + height: height, + people: people + ) + cursorY += height + } + + // A soft separator between days. + if day.id != days.last?.id { + context.setStrokeColor(lineColor.withAlphaComponent(0.55).cgColor) + context.setLineWidth(0.6) + context.move(to: CGPoint(x: margin, y: cursorY)) + context.addLine(to: CGPoint(x: pageWidth - margin, y: cursorY)) + context.strokePath() + } + } + + if days.isEmpty { + drawText( + "Für den gewählten Zeitraum sind keine Gottesdienste hinterlegt.", + in: CGRect(x: margin, y: contentTop + 16, width: pageWidth - margin * 2, height: 34), + font: serif(14), + color: secondaryInk, + maxLines: 2 + ) + } + + endPage() + context.closePDF() + return data as Data + } + + // MARK: - Page furniture + + private static func drawHeader(range: DayRange, generatedAt: Date, continuation: Bool) { + let top = margin + + drawText( + "LEKTORENPLAN · PFARREIENGEMEINSCHAFT BAD GODESBERG", + in: CGRect(x: margin, y: top, width: 330, height: 11), + font: sans(7, weight: .bold), + color: accent + ) + drawText( + continuation ? "Gottesdienstliste · Fortsetzung" : "Gottesdienstliste", + in: CGRect(x: margin, y: top + 12, width: 340, height: 23), + font: serif(19, bold: true), + color: ink + ) + drawText( + range.longTitle, + in: CGRect(x: margin, y: top + 38, width: 340, height: 13), + font: sans(9.5), + color: secondaryInk + ) + + let rightX = pageWidth - margin - 170 + drawText( + "A4 hoch · kompakte Liste", + in: CGRect(x: rightX, y: top, width: 170, height: 11), + font: sans(7.5, weight: .semibold), + color: tertiaryInk, + alignment: .right + ) + drawText( + "Erstellt am \(DayMath.mediumString(generatedAt))", + in: CGRect(x: rightX, y: top + 14, width: 170, height: 11), + font: sans(7.5), + color: tertiaryInk, + alignment: .right + ) + drawText( + "Daten von www.eucharistiefeier.de", + in: CGRect(x: rightX, y: top + 27, width: 170, height: 11), + font: sans(7), + color: tertiaryInk, + alignment: .right + ) + + contextLine(y: top + headerHeight - 6) + } + + private static func drawColumnHeader() { + let y = margin + headerHeight + contextFill( + CGRect(x: margin, y: y, width: pageWidth - margin * 2, height: columnHeaderHeight), + color: NSColor(calibratedWhite: 0.96, alpha: 1) + ) + + var x = margin + 6 + drawText("TAG", in: CGRect(x: x, y: y + 7, width: dateWidth - 6, height: 9), + font: sans(6.5, weight: .bold), color: tertiaryInk) + x += dateWidth + columnGap + drawText("GOTTESDIENST", in: CGRect(x: x, y: y + 7, width: serviceWidth, height: 9), + font: sans(6.5, weight: .bold), color: tertiaryInk) + x += serviceWidth + columnGap + drawText("LEKTOREN", in: CGRect(x: x, y: y + 7, width: lectorWidth, height: 9), + font: sans(6.5, weight: .bold), color: tertiaryInk) + x += lectorWidth + columnGap + drawText("KOMMUNIONHELFER", in: CGRect(x: x, y: y + 7, width: helperWidth, height: 9), + font: sans(6.5, weight: .bold), color: tertiaryInk) + + contextLine(y: y + columnHeaderHeight) + } + + private static func drawFooter(pageIndex: Int, range: DayRange) { + let y = pageHeight - margin - footerHeight + contextLine(y: y + 6) + drawText( + "LektorenPlan · kompakte Liste · \(range.shortTitle)", + in: CGRect(x: margin, y: y + 10, width: 330, height: 11), + font: sans(7), + color: tertiaryInk + ) + drawText( + "Seite \(pageIndex + 1)", + in: CGRect(x: pageWidth - margin - 80, y: y + 10, width: 80, height: 11), + font: sans(7), + color: tertiaryInk, + alignment: .right + ) + } + + // MARK: - Rows + + private static func rowHeight(service: ServiceWithAssignments, people: [String: Person]) -> CGFloat { + var contentHeight: CGFloat = 11 // time + church/location line + if !service.service.title.isEmpty { contentHeight += 10 } + + let lectors = names(for: .lector, service: service, people: people) + let helpers = names(for: .helper, service: service, people: people) + let lectorHeight = measuredHeight(lectors.text, font: sans(8), width: lectorWidth - 4, maxLines: 2) + let helperHeight = measuredHeight(helpers.text, font: sans(8), width: helperWidth - 4, maxLines: 2) + contentHeight = max(contentHeight, lectorHeight, helperHeight) + + return max(23, contentHeight + 7) + } + + private static func drawServiceRow( + day: DayPlanRow, + service: ServiceWithAssignments, + isFirstOfDay: Bool, + y: CGFloat, + height: CGFloat, + people: [String: Person] + ) { + let dayIsSunday = day.liturgicalDay?.isSunday == true || DayMath.isSunday(day.date) + let dayIsFeast = day.liturgicalDay?.isHighFeast == true + let hasSpecial = day.services.contains { $0.service.kind == .special } + + if dayIsFeast { + contextFill(CGRect(x: margin, y: y, width: pageWidth - margin * 2, height: height), color: feastWash) + } else if dayIsSunday { + contextFill(CGRect(x: margin, y: y, width: pageWidth - margin * 2, height: height), color: sundayWash) + } + + // Date cell, only on the first row of the day. + if isFirstOfDay { + drawDateCell(day: day, y: y, isSunday: dayIsSunday, isFeast: dayIsFeast, hasSpecial: hasSpecial) + } + + var x = margin + dateWidth + columnGap + + // Service cell + let color = NSColor(hexString: service.church.colorHex) ?? accent + let place = service.service.placeName(church: service.church) + drawText( + "\(service.service.timeLabel) · \(place)", + in: CGRect(x: x, y: y + 4, width: serviceWidth, height: 11), + font: sans(8.5, weight: .semibold), + color: service.service.location == nil ? color : burgundy + ) + if !service.service.title.isEmpty { + let title = service.service.kind == .special + ? "\(service.service.title) · Sondergottesdienst" + : service.service.title + drawText( + title, + in: CGRect(x: x, y: y + 14, width: serviceWidth, height: 10), + font: sans(7.5), + color: service.service.kind == .special ? burgundy : secondaryInk, + maxLines: 1 + ) + } + x += serviceWidth + columnGap + + // Role columns + drawRoleCell( + role: .lector, + service: service, + people: people, + x: x, + y: y, + width: lectorWidth + ) + x += lectorWidth + columnGap + drawRoleCell( + role: .helper, + service: service, + people: people, + x: x, + y: y, + width: helperWidth + ) + + // Row separator + context.setStrokeColor(lineColor.withAlphaComponent(0.38).cgColor) + context.setLineWidth(0.4) + context.move(to: CGPoint(x: margin, y: y + height)) + context.addLine(to: CGPoint(x: pageWidth - margin, y: y + height)) + context.strokePath() + } + + private static func drawDateCell( + day: DayPlanRow, + y: CGFloat, + isSunday: Bool, + isFeast: Bool, + hasSpecial: Bool + ) { + var textY = y + 5 + drawText( + DayMath.shortWeekdayName(day.date).uppercased(), + in: CGRect(x: margin + 5, y: textY, width: dateWidth - 5, height: 8), + font: sans(6.5, weight: .bold), + color: isSunday || isFeast ? burgundy : tertiaryInk + ) + textY += 8 + drawText( + "\(DayMath.dayNumber(day.date)).\(DayMath.month(day.date)).", + in: CGRect(x: margin + 5, y: textY, width: dateWidth - 5, height: 16), + font: serif(14, bold: true), + color: isFeast ? gold : (isSunday ? burgundy : ink) + ) + textY += 17 + + if isFeast { + let title = feastHint(day.liturgicalDay) + drawText( + title, + in: CGRect(x: margin + 5, y: textY, width: dateWidth - 8, height: 28), + font: sans(6.5, weight: .semibold), + color: gold, + maxLines: 3 + ) + } else if hasSpecial { + drawText( + "Sondergottesdienst", + in: CGRect(x: margin + 5, y: textY, width: dateWidth - 8, height: 20), + font: sans(6, weight: .semibold), + color: burgundy, + maxLines: 2 + ) + } + } + + /// Keeps the feast hint short enough for the narrow date column. + private static func feastHint(_ day: LiturgicalDay?) -> String { + guard let day, !day.title.isEmpty else { return "Hochfest" } + let title = day.title + if let separator = title.range(of: " – ") { + return String(title[separator.upperBound...]) + } + if title.count <= 30 { return title } + // Long official titles ("Hochfest des …") collapse to a generic hint. + if title.localizedCaseInsensitiveContains("Hochfest") { return "Hochfest" } + return String(title.prefix(28)) + "…" + } + + private static func drawRoleCell( + role: Role, + service: ServiceWithAssignments, + people: [String: Person], + x: CGFloat, + y: CGFloat, + width: CGFloat + ) { + let needed = service.service.requiredCount(for: role) + guard needed > 0 else { + drawText("—", in: CGRect(x: x + 2, y: y + 4, width: width - 4, height: 10), + font: sans(8), color: tertiaryInk) + return + } + + let result = names(for: role, service: service, people: people) + drawText( + result.text, + in: CGRect(x: x + 2, y: y + 4, width: width - 4, height: 24), + font: sans(8, weight: result.open > 0 ? .medium : .regular), + color: result.open > 0 ? openColor : ink, + maxLines: 2 + ) + } + + private static func names( + for role: Role, + service: ServiceWithAssignments, + people: [String: Person] + ) -> (text: String, open: Int) { + let needed = service.service.requiredCount(for: role) + let assigned = service.assignments(for: role) + let lastNames = assigned.compactMap { people[$0.personID]?.lastName } + let open = max(0, needed - lastNames.count) + + var parts = lastNames + if open > 0 { + parts.append("offen") + } + return (parts.isEmpty ? "offen" : parts.joined(separator: ", "), open) + } + + // MARK: - Drawing helpers + + @discardableResult + private static func drawText( + _ text: String, + in rect: CGRect, + font: NSFont, + color: NSColor, + alignment: NSTextAlignment = .left, + maxLines: Int = 1 + ) -> CGFloat { + guard !text.isEmpty else { return 0 } + let paragraph = NSMutableParagraphStyle() + paragraph.alignment = alignment + paragraph.lineBreakMode = maxLines > 1 ? .byWordWrapping : .byTruncatingTail + paragraph.lineSpacing = 0.5 + let attributes: [NSAttributedString.Key: Any] = [ + .font: font, + .foregroundColor: color, + .paragraphStyle: paragraph + ] + let attributed = NSAttributedString(string: text, attributes: attributes) + let measured = attributed.boundingRect( + with: CGSize(width: rect.width, height: .greatestFiniteMagnitude), + options: [.usesLineFragmentOrigin, .usesFontLeading] + ) + let lineHeight = font.pointSize * 1.22 + let maxHeight = lineHeight * CGFloat(maxLines) + let drawHeight = min(measured.height, maxHeight) + attributed.draw( + with: CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: max(drawHeight, lineHeight)), + options: [.usesLineFragmentOrigin, .usesFontLeading] + ) + return min(measured.height, maxHeight) + } + + private static func measuredHeight( + _ text: String, + font: NSFont, + width: CGFloat, + maxLines: Int + ) -> CGFloat { + guard !text.isEmpty else { return 0 } + let attributes: [NSAttributedString.Key: Any] = [.font: font] + let measured = (text as NSString).boundingRect( + with: CGSize(width: width, height: .greatestFiniteMagnitude), + options: [.usesLineFragmentOrigin, .usesFontLeading], + attributes: attributes + ) + return min(measured.height, font.pointSize * 1.22 * CGFloat(maxLines)) + } + + private static func contextLine(y: CGFloat) { + context.setStrokeColor(lineColor.withAlphaComponent(0.75).cgColor) + context.setLineWidth(0.7) + context.move(to: CGPoint(x: margin, y: y)) + context.addLine(to: CGPoint(x: pageWidth - margin, y: y)) + context.strokePath() + } + + private static func contextFill(_ rect: CGRect, color: NSColor) { + context.setFillColor(color.cgColor) + context.fill(rect) + } + + nonisolated(unsafe) private static var storedContext: CGContext? + + private static var context: CGContext { + storedContext ?? CGContext(data: nil, width: 1, height: 1, bitsPerComponent: 8, + bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! + } +} + +private extension DayRange { + var longTitle: String { + "\(DayMath.longString(start)) bis \(DayMath.longString(end)) · \(dayCount) Tage" + } +} + +private extension NSColor { + convenience init?(hexString: String) { + let cleaned = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) + guard cleaned.count == 6, let value = UInt64(cleaned, radix: 16) else { return nil } + self.init( + calibratedRed: CGFloat((value >> 16) & 0xFF) / 255, + green: CGFloat((value >> 8) & 0xFF) / 255, + blue: CGFloat(value & 0xFF) / 255, + alpha: 1 + ) + } +}