*/ private array $starts = []; /** * @param array $guide Ergebnis von {@see DocumentGuide::build} */ public function render(array $guide, string $licensee, string $copyId, ?PdfImages $images = null): string { $starts = []; $total = 0; $bytes = ''; for ($pass = 0; $pass < 5; $pass++) { $bytes = $this->pass($guide, $licensee, $copyId, $images, $starts, $total); if ($this->starts === $starts && $this->total === $total) { break; } $starts = $this->starts; $total = $this->total; } return $bytes; } /** * @param array $guide * @param array $known Kapitelanfänge aus dem vorigen Durchgang */ private function pass(array $guide, string $licensee, string $copyId, ?PdfImages $images, array $known, int $total): string { $this->pdf = new PdfDocument; $this->page = 0; $this->total = 0; $this->starts = []; $this->footer = PdfFlow::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId); $this->flow = new PdfFlow($this->pdf, self::LEFT, self::RIGHT, self::TOP, self::BOTTOM, function (): void { $this->closePage(); $this->newPage(); }, $images === null ? null : Closure::fromCallable($images)); $knownTotal = $total; $this->total = $knownTotal; $this->newPage(); $this->flow->write(PdfFlow::printable((string) $guide['title']), 24, true, 10); $this->flow->write('Inhaltsverzeichnis', 14, true, 8, keep: 40); foreach ($guide['chapters'] as $chapter) { $this->contentsLine($chapter, $known[$chapter['anchor']] ?? ['page' => 1, 'top' => self::TOP]); } foreach ($guide['chapters'] as $chapter) { $this->chapter($chapter); } $this->closePage(); $total = $this->page; $starts = $this->starts; $bytes = $this->pdf->output($this->asciiTitle((string) $guide['title']), 'Lizenzkopie '.$copyId); $this->total = $total; $this->starts = $starts; return $bytes; } /** * Eine Zeile des Inhaltsverzeichnisses: Nummer und Titel, Punktlinie, Seitenzahl; die ganze Zeile springt zum Kapitel. * * @param array $chapter * @param array{page: int, top: float} $target */ private function contentsLine(array $chapter, 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']); $pageText = (string) $target['page']; $numberWidth = PdfDocument::exactWidth($pageText, $size, false); $lines = PdfDocument::wrap($label, self::RIGHT - self::LEFT - $indent - $numberWidth - 14, $size, $level === 1); $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); if ($index === count($lines) - 1) { $used = PdfDocument::exactWidth($line, $size, $level === 1) + 4; $room = self::RIGHT - $numberWidth - 4 - ($x + $used); $dots = (int) floor($room / PdfDocument::exactWidth('.', 8, false)); if ($dots > 2) { $this->pdf->text($x + $used, $this->flow->y, str_repeat('.', $dots), 8); } $this->pdf->textRight(self::RIGHT, $this->flow->y, $pageText, $size); } $this->flow->y -= $size + 4; } $this->pdf->link(self::LEFT, $this->flow->y + 2, self::RIGHT - self::LEFT, $top - $this->flow->y, max(0, $target['page'] - 1), $target['top']); $this->flow->y -= 2; } /** * @param array $chapter */ private function chapter(array $chapter): void { $level = $chapter['level']; if ($level === 1) { $this->closePage(); $this->newPage(); } $size = $level === 1 ? 18.0 : 13.5; $title = PdfFlow::printable($chapter['number'].' '.$chapter['title']); $this->flow->room(($size + 4) + 60); $top = $this->flow->y + $size; $this->starts[$chapter['anchor']] = ['page' => $this->page, 'top' => $top]; $this->pdf->bookmark($chapter['number'].' '.$chapter['title'], $this->page - 1, $top, $level); $this->flow->y -= $level === 1 ? 0 : 6; $this->flow->write($title, $size, true, $level === 1 ? 10 : 5, keep: 40); if (! $chapter['allowed']) { $this->flow->write('Dieses Kapitel ist derzeit nicht verfügbar.', 10.5, false, 6); return; } $this->flow->formatted($chapter['blocks'] ?? []); $this->flow->y -= 4; } private function newPage(): void { $this->pdf->addPage(); $this->page++; $this->flow->restart(); } private function closePage(): void { $this->pdf->line(self::LEFT, 54, self::RIGHT, 54, 0.4); $this->pdf->text(self::LEFT, 40, $this->footer, 8); $this->pdf->textRight(self::RIGHT, 40, 'Seite '.$this->page.($this->total > 0 ? ' von '.$this->total : ''), 8); } private function asciiTitle(string $title): string { $ascii = Transliterator::create('Any-Latin; Latin-ASCII'); return $ascii?->transliterate($title) ?: 'Dokument'; } }