*/ private array $pages = []; /** Schräge Schnitte werden nur eingebunden, wenn das Dokument sie braucht (Belege bleiben unverändert). */ private bool $usesItalic = false; /** Breiten der Helvetica (Tausendstel der Schrifthöhe) für Zeichen 32 bis 126; schräg wie aufrecht. */ private const REGULAR = [278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 333, 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584]; /** Breiten der Helvetica-Bold für Zeichen 32 bis 126; schräg wie aufrecht. */ private const BOLD = [278, 333, 474, 556, 556, 889, 722, 238, 333, 333, 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333, 584, 584, 584, 611, 975, 722, 722, 722, 722, 667, 611, 778, 722, 278, 556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, 333, 278, 333, 584, 556, 333, 556, 611, 556, 611, 556, 333, 611, 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556, 333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584]; public function addPage(): void { $this->pages[] = ''; } public function text(float $x, float $y, string $text, float $size = 10, bool $bold = false, bool $italic = false): void { $font = $italic ? ($bold ? 'F4' : 'F3') : ($bold ? 'F2' : 'F1'); $this->usesItalic = $this->usesItalic || $italic; $this->append(sprintf("BT /%s %s Tf %s %s Td (%s) Tj ET\n", $font, $this->n($size), $this->n($x), $this->n($y), self::escape($text))); } /** * Genaue Breite in Punkt nach den Schriftbreiten der Helvetica, damit Textabschnitte mit verschiedenen Schnitten in * einer Zeile ohne Lücken aneinanderstoßen. Umlaute zählen wie ihr Grundbuchstabe, das ß wie ein kleines b, jedes * andere Zeichen außerhalb von ASCII wie eine Ziffer. */ public static function exactWidth(string $text, float $size = 10, bool $bold = false): float { $table = $bold ? self::BOLD : self::REGULAR; $units = 0; $text = strtr($text, ['ä' => 'a', 'ö' => 'o', 'ü' => 'u', 'Ä' => 'A', 'Ö' => 'O', 'Ü' => 'U', 'ß' => 'b', "\u{2019}" => "'", "\u{2026}" => '...', "\u{00A0}" => ' ']); foreach (mb_str_split($text) as $char) { $code = strlen($char) === 1 ? ord($char) : 0; $units += $code >= 32 && $code <= 126 ? $table[$code - 32] : 556; } return $units / 1000 * $size; } public function textRight(float $xRight, float $y, string $text, float $size = 10, bool $bold = false): void { $this->text($xRight - self::width($text, $size, $bold), $y, $text, $size, $bold); } public function line(float $x1, float $y1, float $x2, float $y2, float $width = 0.5): void { $this->append(sprintf("%s w %s %s m %s %s l S\n", $this->n($width), $this->n($x1), $this->n($y1), $this->n($x2), $this->n($y2))); } /** * Zeilenumbruch nach Wörtern. Die Breite ist eine bewusst großzügige Schätzung der Helvetica-Breiten, damit nichts * über den Rand läuft; ein einzelnes zu langes Wort wird hart getrennt. * * @return list */ public static function wrap(string $text, float $maxWidth, float $size = 10, bool $bold = false): array { $lines = []; foreach (preg_split('/\R/u', trim($text)) ?: [''] as $paragraph) { $current = ''; foreach (preg_split('/\s+/u', trim($paragraph)) ?: [] as $word) { while (mb_strlen($word) > 1 && self::width($word, $size, $bold) > $maxWidth) { $cut = mb_strlen($word) - 1; while ($cut > 1 && self::width(mb_substr($word, 0, $cut), $size, $bold) > $maxWidth) { $cut--; } if ($current !== '') { $lines[] = $current; $current = ''; } $lines[] = mb_substr($word, 0, $cut); $word = mb_substr($word, $cut); } $candidate = $current === '' ? $word : $current.' '.$word; if ($current !== '' && self::width($candidate, $size, $bold) > $maxWidth) { $lines[] = $current; $current = $word; } else { $current = $candidate; } } $lines[] = $current; } return $lines; } /** Geschätzte Textbreite in Punkt (Helvetica; Ziffern und Währung exakt, Buchstaben großzügig). */ public static function width(string $text, float $size = 10, bool $bold = false): float { $units = 0.0; foreach (mb_str_split($text) as $char) { $width = match (true) { $char === ' ', $char === ',', $char === '.', $char === ':', $char === ';', $char === "'", $char === '|', $char === 'i', $char === 'l', $char === 'I', $char === 'j', $char === 't', $char === 'f' => 0.3, $char === '-', $char === '(', $char === ')', $char === '/', $char === 'r' => 0.35, ctype_digit($char), $char === '€' => 0.556, mb_strtoupper($char) === $char && $char !== mb_strtolower($char) => 0.75, $char === 'm', $char === 'w', $char === 'M', $char === 'W' => 0.85, default => 0.57, }; // Fette Ziffern, Währung und Satzzeichen sind in Helvetica so breit wie normale; nur Buchstaben werden breiter. $units += $bold && ctype_alpha($char) ? $width * 1.08 : $width; } return $units * $size; } /** * @param string $title Nur ASCII (Dokumenttitel) */ public function output(string $title, ?string $keywords = null): string { $pages = $this->pages === [] ? [''] : $this->pages; $objects = []; $objects[1] = '<< /Type /Catalog /Pages 2 0 R >>'; $kids = []; $next = 6; foreach ($pages as $stream) { $kids[] = $next.' 0 R'; $objects[$next] = '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 '.self::WIDTH.' '.self::HEIGHT.'] /Resources << /Font << /F1 3 0 R /F2 4 0 R'.($this->usesItalic ? ' /F3 %ITALIC% 0 R /F4 %BOLDITALIC% 0 R' : '').' >> >> /Contents '.($next + 1).' 0 R >>'; $objects[$next + 1] = '<< /Length '.strlen($stream)." >>\nstream\n".$stream.'endstream'; $next += 2; } if ($this->usesItalic) { $objects[$next] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Oblique /Encoding /WinAnsiEncoding >>'; $objects[$next + 1] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-BoldOblique /Encoding /WinAnsiEncoding >>'; foreach ($objects as $number => $body) { $objects[$number] = str_replace(['%ITALIC%', '%BOLDITALIC%'], [(string) $next, (string) ($next + 1)], $body); } } $objects[2] = '<< /Type /Pages /Kids ['.implode(' ', $kids).'] /Count '.count($pages).' >>'; $objects[3] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>'; $objects[4] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>'; $ascii = fn (string $value): string => (string) preg_replace('/[^\x20-\x7E]/', '?', str_replace(['\\', '(', ')'], ['', '', ''], $value)); $objects[5] = '<< /Title ('.$ascii($title).')'.($keywords === null ? '' : ' /Keywords ('.$ascii($keywords).')').' /Producer (Lara und Felix) >>'; ksort($objects); $pdf = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; $offsets = []; foreach ($objects as $number => $body) { $offsets[$number] = strlen($pdf); $pdf .= $number." 0 obj\n".$body."\nendobj\n"; } $xref = strlen($pdf); $pdf .= 'xref'."\n0 ".(count($objects) + 1)."\n0000000000 65535 f \n"; foreach ($offsets as $offset) { $pdf .= sprintf("%010d 00000 n \n", $offset); } return $pdf.'trailer'."\n<< /Size ".(count($objects) + 1).' /Root 1 0 R /Info 5 0 R >>'."\nstartxref\n".$xref."\n%%EOF\n"; } private function append(string $operators): void { if ($this->pages === []) { $this->addPage(); } $this->pages[array_key_last($this->pages)] .= $operators; } private function n(float $value): string { return rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.') ?: '0'; } /** Windows-1252, geklammert für PDF-Zeichenketten. */ private static function escape(string $text): string { $text = strtr($text, ["\u{2212}" => '-', "\u{2019}" => "'", "\u{2026}" => '...', "\u{00A0}" => ' ', "\u{202F}" => ' ']); $text = (string) preg_replace('/[\x00-\x08\x0B-\x1F\x7F]/u', '', $text); $encoded = mb_convert_encoding($text, 'Windows-1252', 'UTF-8'); return str_replace(['\\', '(', ')'], ['\\\\', '\\(', '\\)'], $encoded); } }