💄 hide running headings on title pages

This commit is contained in:
TC
2026-06-22 21:56:50 +10:00
parent 7c0b993d7e
commit ff40d424aa

View File

@@ -624,26 +624,28 @@ export function registerIpcHandlers(): void {
const mergedPdf = await PDFDocument.create()
const headerStandardFont = opts.font === 'courier' ? StandardFonts.Courier : StandardFonts.TimesRoman
const headerFont = await mergedPdf.embedFont(headerStandardFont)
// pageOwners[i] tracks the section title and whether the page is a cover page
const pageOwners: { title: string; isCover: boolean }[] = []
// pageOwners[i] tracks the section title, whether the page is a cover page,
// and whether it is the first page of its section (chapter/part opener).
const pageOwners: { title: string; isCover: boolean; isSectionOpener: boolean }[] = []
for (const { title, buffer, isCover } of sectionPdfs) {
const srcPdf = await PDFDocument.load(buffer)
const copied = await mergedPdf.copyPages(srcPdf, srcPdf.getPageIndices())
for (const page of copied) {
for (const [pageIdx, page] of copied.entries()) {
mergedPdf.addPage(page)
pageOwners.push({ title, isCover })
pageOwners.push({ title, isCover, isSectionOpener: pageIdx === 0 })
}
}
// Stamp the running header on body pages only (cover page gets no header).
// Stamp the running header on body pages only (cover page and section opener
// pages get no header — opener pages show the chapter/part title instead).
// Page numbers count from 1 starting with the first non-cover page.
const allPages = mergedPdf.getPages()
let bodyPageNum = 0
for (let i = 0; i < allPages.length; i++) {
if (pageOwners[i].isCover) continue // no header/number on cover page
bodyPageNum++
if (bodyPageNum === 1) continue // no running header on first body page
if (pageOwners[i].isSectionOpener) continue // no running header on chapter/part opener pages
const page = allPages[i]
const { width, height } = page.getSize()
const chapterUpper = pageOwners[i].title.toUpperCase()