Biete Überschriften h3 bis h6 an und nimm sie ins Inhaltsverzeichnis auf
Ein Dokument gliedert sich vor allem durch Kapitel der ersten Ebene und Überschriften im Text. Der Editor bietet h3 bis h6; h1 und h2 aus eingefügtem Text werden h3. Alle Überschriften des Kapiteltexts stehen automatisch im Inhaltsverzeichnis der Seite und des PDF (eingerückt, ohne Nummer), mit Sprungmarken, im PDF mit Seitenzahl, Verweis und verschachtelten Lesezeichen. Die PDF-Gliederung kennt jetzt bis zu sechs Ebenen und zählt alle Nachkommen. Unter einem Unterkapitel rücken die Überschriften eine Stufe tiefer (höchstens h6). Der bestehende Test erwartete h4 für eine Zwischenüberschrift im Kapitel; das gilt bewusst nicht mehr (h3 bleibt h3 und trägt eine Sprungmarke). Keine Migration, der Änderungsnachweis bleibt unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58b68b768c
commit
b942b5331a
16 files changed
+421
-51
No files matched your search
@@ -138,6 +138,10 @@ private function pass(array $guide, ?PdfTemplate $template, string $licensee, st
|
||||
$this->closePage();
|
||||
$this->newPage();
|
||||
}, $images === null ? null : Closure::fromCallable($images));
|
||||
$this->flow->onHeading = function (array $block, float $top): void {
|
||||
$this->starts[$block['anchor']] = ['page' => $this->page, 'top' => $top];
|
||||
$this->pdf->bookmark((string) $block['text'], $this->page - 1, $top, (int) $block['level'] - 1);
|
||||
};
|
||||
$this->total = $total;
|
||||
$withCover = $template !== null && $template->show_cover;
|
||||
|
||||
@@ -155,8 +159,8 @@ private function pass(array $guide, ?PdfTemplate $template, string $licensee, st
|
||||
|
||||
$this->flow->write('Inhaltsverzeichnis', 14, true, 8, keep: 40);
|
||||
|
||||
foreach ($guide['chapters'] as $chapter) {
|
||||
$this->contentsLine($chapter, $known[$chapter['anchor']] ?? ['page' => 1, 'top' => $this->top]);
|
||||
foreach ($this->contents($guide) as $entry) {
|
||||
$this->contentsLine($entry, $known[$entry['anchor']] ?? ['page' => 1, 'top' => $this->top]);
|
||||
}
|
||||
|
||||
foreach ($guide['chapters'] as $chapter) {
|
||||
@@ -231,29 +235,57 @@ private function coverText(string $text, float $size, bool $bold, float &$y, flo
|
||||
}
|
||||
|
||||
/**
|
||||
* Eine Zeile des Inhaltsverzeichnisses: Nummer und Titel, Punktlinie, Seitenzahl; die ganze Zeile springt zum Kapitel.
|
||||
* Die Einträge des Inhaltsverzeichnisses in der Reihenfolge des Dokuments: Kapitel und Unterkapitel mit Nummer, darunter
|
||||
* ihre Zwischenüberschriften (h3 bis h6), eingerückt nach Stufe.
|
||||
*
|
||||
* @param array<string, mixed> $chapter
|
||||
* @param array<string, mixed> $guide
|
||||
* @return list<array{anchor: string, label: string, size: float, indent: float, bold: bool}>
|
||||
*/
|
||||
private function contents(array $guide): array
|
||||
{
|
||||
$entries = [];
|
||||
|
||||
foreach ($guide['chapters'] as $chapter) {
|
||||
$entries[] = [
|
||||
'anchor' => $chapter['anchor'],
|
||||
'label' => $chapter['number'].' '.$chapter['title'],
|
||||
'size' => $chapter['level'] === 1 ? 11.0 : 10.0,
|
||||
'indent' => $chapter['level'] === 1 ? 0.0 : 18.0,
|
||||
'bold' => $chapter['level'] === 1,
|
||||
];
|
||||
|
||||
foreach ($chapter['headings'] ?? [] as $heading) {
|
||||
$entries[] = ['anchor' => $heading['anchor'], 'label' => $heading['text'], 'size' => 9.5, 'indent' => 18.0 + ($heading['level'] - 3) * 12.0, 'bold' => false];
|
||||
}
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Eine Zeile des Inhaltsverzeichnisses: Titel, Punktlinie, Seitenzahl; die ganze Zeile springt zum Eintrag.
|
||||
*
|
||||
* @param array{anchor: string, label: string, size: float, indent: float, bold: bool} $entry
|
||||
* @param array{page: int, top: float} $target
|
||||
*/
|
||||
private function contentsLine(array $chapter, array $target): void
|
||||
private function contentsLine(array $entry, array $target): void
|
||||
{
|
||||
$level = $chapter['level'];
|
||||
$size = $level === 1 ? 11.0 : 10.0;
|
||||
$indent = $level === 1 ? 0.0 : 18.0;
|
||||
$label = PdfFlow::printable($chapter['number'].' '.$chapter['title']);
|
||||
$size = $entry['size'];
|
||||
$indent = $entry['indent'];
|
||||
$bold = $entry['bold'];
|
||||
$label = PdfFlow::printable($entry['label']);
|
||||
$pageText = (string) $target['page'];
|
||||
$numberWidth = PdfDocument::exactWidth($pageText, $size, false);
|
||||
$lines = PdfDocument::wrap($label, self::RIGHT - self::LEFT - $indent - $numberWidth - 14, $size, $level === 1);
|
||||
$lines = PdfDocument::wrap($label, self::RIGHT - self::LEFT - $indent - $numberWidth - 14, $size, $bold);
|
||||
$x = self::LEFT + $indent;
|
||||
$this->flow->room(count($lines) * ($size + 4) + 2);
|
||||
$top = $this->flow->y;
|
||||
|
||||
foreach ($lines as $index => $line) {
|
||||
$this->pdf->text($x, $this->flow->y, $line, $size, $level === 1);
|
||||
$this->pdf->text($x, $this->flow->y, $line, $size, $bold);
|
||||
|
||||
if ($index === count($lines) - 1) {
|
||||
$used = PdfDocument::exactWidth($line, $size, $level === 1) + 4;
|
||||
$used = PdfDocument::exactWidth($line, $size, $bold) + 4;
|
||||
$room = self::RIGHT - $numberWidth - 4 - ($x + $used);
|
||||
$dots = (int) floor($room / PdfDocument::exactWidth('.', 8, false));
|
||||
|
||||
|
||||
Reference in new issue
Block a user