Bugfix: Umlaute falsch in den PDFs

This commit is contained in:
concierge committed 2026-11-16 15:54:44 +01:00
1 parent 66659d9a23
commit b3afa3ef32
3 files changed
+175 -27

No files matched your search

+40 -26
View File
@@ -108,7 +108,10 @@ func hexColor(value string, fallback [3]int) [3]int {
return [3]int{int(parsed >> 16 & 0xFF), int(parsed >> 8 & 0xFF), int(parsed & 0xFF)}
}
func fit(pdf *fpdf.Fpdf, text string, width float64) string {
// fitShorten truncates already encoded text to width. Both text and ellipsis
// must already be encoded for the current font, because core fonts are
// single-byte code pages and widths are measured per output byte.
func fitShorten(pdf *fpdf.Fpdf, text string, width float64, ellipsis string) string {
text = strings.TrimSpace(text)
if text == "" {
return ""
@@ -116,10 +119,9 @@ func fit(pdf *fpdf.Fpdf, text string, width float64) string {
if pdf.GetStringWidth(text) <= width {
return text
}
runes := []rune(text)
for len(runes) > 0 {
runes = runes[:len(runes)-1]
candidate := strings.TrimSpace(string(runes)) + "…"
for len(text) > 0 {
text = strings.TrimSpace(text[:len(text)-1])
candidate := text + ellipsis
if pdf.GetStringWidth(candidate) <= width {
return candidate
}
@@ -127,8 +129,18 @@ func fit(pdf *fpdf.Fpdf, text string, width float64) string {
return ""
}
// wrap splits text into at most maxLines lines fitting width.
func wrap(pdf *fpdf.Fpdf, text string, width float64, maxLines int) []string {
// fit translates text to the PDF code page and truncates it to width.
func fit(pdf *fpdf.Fpdf, tr func(string) string, text string, width float64) string {
return fitShorten(pdf, tr(text), width, tr("…"))
}
// wrap splits text into at most maxLines lines fitting width and returns them
// already encoded for the current font.
func wrap(pdf *fpdf.Fpdf, tr func(string) string, text string, width float64, maxLines int) []string {
return wrapEncoded(pdf, tr(text), width, maxLines, tr("…"))
}
func wrapEncoded(pdf *fpdf.Fpdf, text string, width float64, maxLines int, ellipsis string) []string {
text = strings.TrimSpace(text)
if text == "" || maxLines < 1 {
return nil
@@ -152,17 +164,19 @@ func wrap(pdf *fpdf.Fpdf, text string, width float64, maxLines int) []string {
}
current = word
} else {
current = fit(pdf, word, width)
current = fitShorten(pdf, word, width, ellipsis)
}
}
if len(lines) < maxLines && current != "" {
lines = append(lines, current)
} else if len(lines) == maxLines && current != "" {
lines[len(lines)-1] = fit(pdf, lines[len(lines)-1]+" "+current, width)
lines[len(lines)-1] = fitShorten(pdf, lines[len(lines)-1]+" "+current, width, ellipsis)
}
return lines
}
// writeLines writes lines that are already encoded for the current font; do
// not pass raw UTF-8 text here.
func writeLines(pdf *fpdf.Fpdf, lines []string, x, y, width, lineHeight float64) float64 {
for _, line := range lines {
pdf.SetXY(x, y)
@@ -327,11 +341,11 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
setColor(pdf, color)
pdf.SetFont("Times", "B", 8.2)
pdf.SetXY(x+1.3, y+1.2)
pdf.CellFormat(churchWidth-2.6, 4, tr(fit(pdf, church.Name, churchWidth-2.6)), "", 0, "L", false, 0, "")
pdf.CellFormat(churchWidth-2.6, 4, fit(pdf, tr, church.Name, churchWidth-2.6), "", 0, "L", false, 0, "")
setColor(pdf, colors.tertiary)
pdf.SetFont("Helvetica", "", 5.2)
pdf.SetXY(x+1.3, y+5.1)
pdf.CellFormat(churchWidth-2.6, 2.6, tr(fit(pdf, church.Address, churchWidth-2.6)), "", 0, "L", false, 0, "")
pdf.CellFormat(churchWidth-2.6, 2.6, fit(pdf, tr, church.Address, churchWidth-2.6), "", 0, "L", false, 0, "")
x += churchWidth + gap
}
setDraw(pdf, colors.line)
@@ -355,10 +369,10 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
rowHeight := func(row domain.DayPlanRow) float64 {
height := 15.0
if row.LiturgicalDay != nil {
titles := wrap(pdf, row.LiturgicalDay.DisplayTitle(), liturgyW-4, 3)
titles := wrap(pdf, tr, row.LiturgicalDay.DisplayTitle(), liturgyW-4, 3)
readings := []string{}
if !row.LiturgicalDay.Readings.IsEmpty() {
readings = wrap(pdf, row.LiturgicalDay.Readings.ShortLabel(), liturgyW-4, 3)
readings = wrap(pdf, tr, row.LiturgicalDay.Readings.ShortLabel(), liturgyW-4, 3)
}
lit := 4.5 + float64(len(titles))*3.4 + float64(len(readings))*2.5
if badge, _ := feastBadge(row.LiturgicalDay); badge != "" {
@@ -472,12 +486,12 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
fontStyle = ""
}
pdf.SetFont("Times", fontStyle, 8.2)
titleLines := wrap(pdf, day.DisplayTitle(), liturgyW-4, 3)
titleLines := wrap(pdf, tr, day.DisplayTitle(), liturgyW-4, 3)
textY = writeLines(pdf, titleLines, x+1.2, textY, liturgyW-4, 3.3)
if !day.Readings.IsEmpty() {
setColor(pdf, colors.secondary)
pdf.SetFont("Helvetica", "", 5.6)
readingLines := wrap(pdf, day.Readings.ShortLabel(), liturgyW-4, 3)
readingLines := wrap(pdf, tr, day.Readings.ShortLabel(), liturgyW-4, 3)
writeLines(pdf, readingLines, x+1.2, textY+0.4, liturgyW-4, 2.5)
}
}
@@ -491,15 +505,15 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
for _, service := range services {
setColor(pdf, color)
pdf.SetFont("Helvetica", "B", 6.2)
label := fit(pdf, service.Service.TimeLabel()+" "+service.Service.Title, churchWidth-4)
label := fit(pdf, tr, service.Service.TimeLabel()+" "+service.Service.Title, churchWidth-4)
pdf.SetXY(x+1.2, serviceY)
pdf.CellFormat(churchWidth-2.4, 3.6, tr(label), "", 0, "L", false, 0, "")
pdf.CellFormat(churchWidth-2.4, 3.6, label, "", 0, "L", false, 0, "")
serviceY += 3.4
if service.Service.Location != nil && strings.TrimSpace(*service.Service.Location) != "" {
setColor(pdf, colors.burgundy)
pdf.SetFont("Helvetica", "B", 5.4)
pdf.SetXY(x+1.6, serviceY)
pdf.CellFormat(churchWidth-3, 2.8, tr(fit(pdf, *service.Service.Location, churchWidth-3)), "", 0, "L", false, 0, "")
pdf.CellFormat(churchWidth-3, 2.8, fit(pdf, tr, *service.Service.Location, churchWidth-3), "", 0, "L", false, 0, "")
serviceY += 3.0
}
for _, role := range domain.AllRoles {
@@ -519,7 +533,7 @@ func FullPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, churches
setColor(pdf, colors.secondary)
pdf.SetFont("Helvetica", "", 5.4)
}
lines := wrap(pdf, text, churchWidth-3, 2)
lines := wrap(pdf, tr, text, churchWidth-3, 2)
serviceY = writeLines(pdf, lines, x+1.6, serviceY, churchWidth-3, 3.0)
}
serviceY += 1.5
@@ -652,8 +666,8 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
}
lectorNames, _ := namesFor(service, domain.RoleLector, peopleByID)
helperNames, _ := namesFor(service, domain.RoleHelper, peopleByID)
lectorLines := len(wrap(pdf, lectorNames, lectorW-2, 2))
helperLines := len(wrap(pdf, helperNames, helperW-2, 2))
lectorLines := len(wrap(pdf, tr, lectorNames, lectorW-2, 2))
helperLines := len(wrap(pdf, tr, helperNames, helperW-2, 2))
if lines := maxInt(lectorLines, helperLines); float64(lines)*3 > height {
height = float64(lines) * 3
}
@@ -727,7 +741,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
if row.IsHighFeast() {
setColor(pdf, colors.gold)
pdf.SetFont("Helvetica", "B", 5.2)
hint := wrap(pdf, feastHint(row.LiturgicalDay), dateW-4, 3)
hint := wrap(pdf, tr, feastHint(row.LiturgicalDay), dateW-4, 3)
writeLines(pdf, hint, margin+1.5, y+10, dateW-4, 2.4)
}
}
@@ -737,9 +751,9 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
setColor(pdf, churchColor)
pdf.SetFont("Helvetica", "B", 7.2)
place := service.Service.PlaceName(service.Church)
line := fit(pdf, service.Service.TimeLabel()+" · "+place, serviceW-2)
line := fit(pdf, tr, service.Service.TimeLabel()+" · "+place, serviceW-2)
pdf.SetXY(x, y+1.6)
pdf.CellFormat(serviceW-2, 3.4, tr(line), "", 0, "L", false, 0, "")
pdf.CellFormat(serviceW-2, 3.4, line, "", 0, "L", false, 0, "")
if service.Service.Title != "" {
setColor(pdf, colors.secondary)
if service.Service.Kind == domain.KindSpecial {
@@ -751,7 +765,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
title += " · Sondergottesdienst"
}
pdf.SetXY(x, y+4.8)
pdf.CellFormat(serviceW-2, 3, tr(fit(pdf, title, serviceW-2)), "", 0, "L", false, 0, "")
pdf.CellFormat(serviceW-2, 3, fit(pdf, tr, title, serviceW-2), "", 0, "L", false, 0, "")
}
x += serviceW + gap
@@ -772,7 +786,7 @@ func CompactPlan(w io.Writer, r domain.DayRange, rows []domain.DayPlanRow, peopl
setColor(pdf, colors.ink)
pdf.SetFont("Helvetica", "", 6.6)
}
lines := wrap(pdf, names, width, 2)
lines := wrap(pdf, tr, names, width, 2)
writeLines(pdf, lines, x, y+1.8, width, 3)
}
drawRole(domain.RoleLector, lectorW)