$guide Ergebnis von {@see ExerciseGuide::build} * @return list>|null */ public function sections(array $guide): ?array { $included = []; foreach ($guide['entries'] as $entry) { if ($entry['class'] === 'text') { if ($entry['may_download'] && $entry['allowed']) { $included[] = $entry; } elseif ($entry['requirement'] === CompositionMember::REQUIRED) { return null; } continue; } if ($entry['allowed']) { $included[] = $entry; } } return array_filter($included, fn (array $entry): bool => $entry['class'] === 'text') === [] ? null : $included; } /** * @param array $guide * @param list> $sections */ public function render(array $guide, array $sections, string $licensee, string $copyId, string $variantName, string $guideUrl): string { $this->pdf = new PdfDocument; $this->page = 0; $this->footer = self::printable('Lizenziert für '.$licensee.' · Lizenzkopie '.$copyId); $this->newPage(); $this->write(self::printable((string) $guide['title']), 18, true, 8); if ($variantName !== '') { $this->write(self::printable('Variante: '.$variantName), 11, true, 6); } $this->fact('Lernziel', $guide['learning_goal']); if ($guide['duration_minutes'] !== null) { $this->write(self::printable('Dauer: '.$guide['duration_minutes'].($guide['duration_max'] !== null ? ' bis '.$guide['duration_max'] : '').' Minuten'), 10.5, false, 4); } if (filled($guide['prerequisites'])) { $this->write(self::printable('Voraussetzungen: '.$guide['prerequisites']), 10.5, false, 4); } $this->fact('Ausstattung', $guide['material_note']); $this->y -= 6; foreach ($sections as $section) { $this->section($section, $guideUrl); } $this->closePage(); $ascii = Transliterator::create('Any-Latin; Latin-ASCII'); return $this->pdf->output($ascii?->transliterate((string) $guide['title']) ?: 'Uebung', 'Lizenzkopie '.$copyId); } /** * Zeichen außerhalb von Windows-1252 werden, wo möglich, lesbar ersetzt (Pfeil, Ungleichheitszeichen, andere * lateinische Buchstaben); sonst bleibt „?“. */ public static function printable(string $text): string { $text = strtr($text, ['→' => '->', '←' => '<-', '≥' => '>=', '≤' => '<=', '✓' => '+', '✗' => 'x', '≠' => '!=', '≈' => '~']); $latin = Transliterator::create('Any-Latin; Latin-ASCII'); $out = ''; foreach (mb_str_split($text) as $char) { $encoded = mb_convert_encoding($char, 'Windows-1252', 'UTF-8'); $out .= $encoded === '?' && $char !== '?' ? ($latin?->transliterate($char) ?: '?') : $char; } return $out; } /** * @param array $section */ private function section(array $section, string $guideUrl): void { $title = self::printable((string) $section['title']); if ($section['class'] !== 'text') { $label = in_array($section['class'], ['film', 'audio'], true) ? 'Film: '.$title.' - online abrufbar im Mitgliederbereich (geschützte Wiedergabe): '.$guideUrl : 'Material: '.$title.' - im Mitgliederbereich abrufbar: '.$guideUrl; $this->write(self::printable($label), 10, false, 6); return; } $this->write($title, 12.5, true, 5); $this->formatted($section['blocks'] ?? []); $this->y -= 4; } /** Angabe mit Bezeichnung; formatierte Angaben stehen unter der Bezeichnung, einfache dahinter. */ private function fact(string $label, ?string $value): void { $blocks = FormattedText::blocks($value); if ($blocks === []) { return; } if (count($blocks) === 1 && $blocks[0]['type'] === 'paragraph') { $this->runs([['text' => $label.': ', 'bold' => true, 'italic' => false, 'href' => null], ...$blocks[0]['runs']], self::LEFT, 10.5, after: 4); return; } $this->write(self::printable($label.':'), 10.5, true, 2); $this->formatted($blocks); } /** * Blöcke aus {@see FormattedText::blocks()}: Absatz, Zwischenüberschrift, Zitat und Listen mit fett und kursiv. * * @param list> $blocks */ private function formatted(array $blocks): void { foreach ($blocks as $block) { match ($block['type']) { 'heading' => $this->runs($block['runs'], self::LEFT, 11, bold: true, after: 4), 'quote' => $this->runs($block['runs'], self::LEFT + 14, 10.5, italic: true, after: 5), 'bullets' => $this->listItems($block['items'], fn (int $n): string => '•'), 'numbered' => $this->listItems($block['items'], fn (int $n): string => $n.'.'), default => $this->runs($block['runs'] ?? [], self::LEFT, 10.5, after: 5), }; } } /** * @param list> $items */ private function listItems(array $items, callable $marker): void { foreach ($items as $index => $item) { $this->room(14); $this->pdf->text(self::LEFT + 4, $this->y, $marker($index + 1), 10.5); $this->runs($item, self::LEFT + 18, 10.5, after: 1); } $this->y -= 3; } /** * Setzt Textabschnitte mit fett und kursiv in Zeilen um. Die Breite kommt aus den Schriftbreiten der Helvetica * ({@see PdfDocument::exactWidth()}), damit verschiedene Schnitte in einer Zeile ohne Lücken aneinanderstoßen. * Links erscheinen mit ausgeschriebener Adresse, weil sie auf Papier sonst verloren gehen. * * @param list $runs */ private function runs(array $runs, float $x, float $size, bool $bold = false, bool $italic = false, float $after = 0): void { $maxWidth = self::RIGHT - $x; $lines = []; $line = []; $width = 0.0; $pendingSpace = false; $count = count($runs); $flush = function () use (&$lines, &$line, &$width, &$pendingSpace): void { if ($line !== []) { $lines[] = $line; } $line = []; $width = 0.0; $pendingSpace = false; }; $place = function (string $word, bool $isBold, bool $isItalic) use (&$line, &$width, &$pendingSpace, $maxWidth, $size, $flush): void { $space = $pendingSpace && $line !== [] ? ' ' : ''; $need = PdfDocument::exactWidth($space.$word, $size, $isBold); if ($line !== [] && $width + $need > $maxWidth) { $flush(); $space = ''; $need = PdfDocument::exactWidth($word, $size, $isBold); } while ($need > $maxWidth && mb_strlen($word) > 1) { $cut = mb_strlen($word) - 1; while ($cut > 1 && PdfDocument::exactWidth(mb_substr($word, 0, $cut), $size, $isBold) > $maxWidth) { $cut--; } $line[] = [mb_substr($word, 0, $cut), $isBold, $isItalic]; $flush(); $word = mb_substr($word, $cut); $need = PdfDocument::exactWidth($word, $size, $isBold); } $last = $line === [] ? null : $line[array_key_last($line)]; if ($last !== null && $last[1] === $isBold && $last[2] === $isItalic) { $line[array_key_last($line)][0] .= $space.$word; } else { $line[] = [$space.$word, $isBold, $isItalic]; } $width += $need; $pendingSpace = false; }; foreach ($runs as $index => $run) { $isBold = $bold || $run['bold']; $isItalic = $italic || $run['italic']; foreach (preg_split('/(\s+)/u', self::printable($run['text']), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) ?: [] as $token) { if (trim($token) !== '') { $place($token, $isBold, $isItalic); continue; } if (str_contains($token, "\n")) { $flush(); continue; } $pendingSpace = true; } $nextHref = $index + 1 < $count ? $runs[$index + 1]['href'] : null; if ($run['href'] !== null && $nextHref !== $run['href'] && ! str_contains($run['text'], $run['href'])) { $pendingSpace = true; $place('('.self::printable($run['href']).')', false, false); } } $flush(); foreach ($lines as $row) { $this->room($size + 4); $cursor = $x; foreach ($row as [$text, $isBold, $isItalic]) { $this->pdf->text($cursor, $this->y, $text, $size, $isBold, $isItalic); $cursor += PdfDocument::exactWidth($text, $size, $isBold); } $this->y -= $size + 4; } $this->y -= $after; } private function write(string $text, float $size, bool $bold, float $after): void { foreach (PdfDocument::wrap($text, self::RIGHT - self::LEFT, $size, $bold) as $line) { $this->room($size + 4); $this->pdf->text(self::LEFT, $this->y, $line, $size, $bold); $this->y -= $size + 4; } $this->y -= $after; } private function room(float $needed): void { if ($this->y - $needed < self::BOTTOM) { $this->closePage(); $this->newPage(); } } private function newPage(): void { $this->pdf->addPage(); $this->page++; $this->y = self::TOP; } 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, 8); } }