rpg-westreiche/westreiche_website/app/sheet.php

474 lines
20 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/**
* Westreiche – Zeichnet den Charakterbogen als PDF (A4, Pergament-Optik mit
* Gold-Rahmen). Quelle sind die Datensätze aus characters/character_*.
*/
/** @return array{0:float,1:float,2:float} */
function pdf_color(string $hex): array
{
$hex = ltrim($hex, '#');
return [
hexdec(substr($hex, 0, 2)) / 255,
hexdec(substr($hex, 2, 2)) / 255,
hexdec(substr($hex, 4, 2)) / 255,
];
}
/**
* Baut den kompletten Charakterbogen.
*
* @param array $c Charakterdatensatz
* @param array $extra ['skills' => [...], 'spells' => ['arkan'=>[], 'klerikal'=>[]], 'items' => [...]]
*/
function character_pdf(array $c, array $extra): string
{
$ink = pdf_color('#1a1712');
$soft = pdf_color('#4a4336');
$gold = pdf_color('#a8862a');
$goldLight = pdf_color('#d9be6a');
$parchment = pdf_color('#faf5e6');
$parchment2 = pdf_color('#f2e9d2');
$pdf = new WsPdf();
$W = WsPdf::width();
$H = WsPdf::height();
$M = 34.0;
$inner = $W - 2 * $M;
$pageNo = 0;
$y = 0.0;
$frame = static function (WsPdf $pdf, int $pageNo) use ($W, $H, $M, $gold, $goldLight, $parchment, $parchment2, $ink, $soft): void {
$pdf->rect(0, 0, $W, $H, $parchment);
$pdf->rect(0, 0, $W, 16, $parchment2);
$pdf->rect(0, $H - 16, $W, 16, $parchment2);
$pdf->rect($M - 12, 18, $W - 2 * ($M - 12), $H - 36, null, $gold, 1.2);
$pdf->rect($M - 8, 22, $W - 2 * ($M - 8), $H - 44, null, $goldLight, 0.4);
// Ecken-Ornamente
foreach ([[$M - 12, 18], [$W - $M + 12 - 14, 18], [$M - 12, $H - 32], [$W - $M + 12 - 14, $H - 32]] as [$cx, $cy]) {
$pdf->rect($cx, $cy, 14, 14, $gold);
}
$pdf->textCenter($W / 2, $H - 22, 7.5, 'F1', $soft, 'Westreiche · Chronik der Zauberturm-Runde · zauberturm.de');
$pdf->textRight($W - $M, $H - 40, 7.5, 'F1', $soft, 'Seite ' . $pageNo);
};
$newPage = static function () use (&$pageNo, &$y, $pdf, $frame): void {
if ($pageNo > 0) {
$pdf->addPage();
}
$pageNo++;
$frame($pdf, $pageNo);
$y = 52.0;
};
$newPage();
$ensure = static function (float $needed) use (&$y, $newPage, $H): void {
if ($y + $needed > $H - 70) {
$newPage();
}
};
$sectionTitle = static function (string $label) use (&$y, $pdf, $gold, $ink, $ensure, $M, $inner): void {
$ensure(34);
$y += 12;
$pdf->rect($M, $y, 3.2, 10, $gold);
$pdf->text($M + 8, $y + 9, 11.5, 'F4', $ink, mb_strtoupper($label, 'UTF-8'));
$pdf->line($M, $y + 14, $M + $inner, $y + 14, $gold, 0.4);
$y += 22;
};
$paragraph = static function (string $text, float $size = 8.6) use (&$y, $pdf, $ink, $ensure, $M, $inner): void {
if (trim($text) === '') {
return;
}
foreach (preg_split('/\R{2,}/', trim($text)) ?: [] as $block) {
foreach (WsPdf::wrap(str_replace("\n", ' ', $block), $size, 'F3', $inner) as $line) {
$ensure(12);
$pdf->text($M, $y + 9, $size, 'F3', $ink, $line);
$y += 11.4;
}
$y += 3;
}
};
/* ---------------------------------------------------------------- Kopf */
$portraitW = 74.0;
$portraitH = 94.0;
$pX = $W - $M - $portraitW;
$textRight = $pX - 12;
$pdf->text($M, $y, 7.5, 'F2', $gold, 'CHARAKTERBOGEN');
$nameSize = 24.0;
$nameText = (string) $c['name'];
while ($nameText !== '' && WsPdf::textWidth($nameText, $nameSize, 'F4') > $textRight - $M) {
$nameText = mb_substr($nameText, 0, -1, 'UTF-8');
}
if ($nameText !== (string) $c['name']) {
$nameText = rtrim($nameText) . '…';
}
$pdf->text($M, $y + 30, $nameSize, 'F4', $ink, $nameText);
$titleLine = [];
if ((string) $c['title'] !== '') {
$titleLine[] = (string) $c['title'];
}
$titleLine[] = (string) $c['class_name'];
$titleLine[] = 'Stufe ' . (int) $c['level'];
if ((string) $c['alignment'] !== '' && (string) $c['alignment'] !== '–') {
$titleLine[] = 'Gesinnung: ' . (string) $c['alignment'];
}
$titleLine[] = character_status_label((string) $c['status']);
$titleText = implode(' · ', $titleLine);
while ($titleText !== '' && WsPdf::textWidth($titleText, 9.6, 'F5') > $textRight - $M) {
$titleText = mb_substr($titleText, 0, -1, 'UTF-8');
}
$pdf->text($M, $y + 46, 9.6, 'F5', $soft, $titleText);
// Porträt rechts (sonst Monogramm)
$pH = $y + 2;
$portraitBytes = portrait_jpeg_bytes($c);
if ($portraitBytes === '' || !$pdf->imageJpeg($portraitBytes, $pX, $pH, $portraitW, $portraitH, true)) {
$cgX = $W - $M - 26;
$cgY = $y + 26;
$pdf->rect($cgX - 26, $cgY - 26, 52, 52, null, $gold, 1.0);
$pdf->rect($cgX - 22, $cgY - 22, 44, 44, null, $goldLight, 0.4);
$pdf->textCenter($cgX, $cgY + 8, 20, 'F4', $gold, mb_strtoupper(mb_substr((string) $c['name'], 0, 1), 'UTF-8'));
}
$pdf->rect($pX - 2.5, $pH - 2.5, $portraitW + 5, $portraitH + 5, null, $gold, 1.0);
$y += 78;
/* ---------------------------------------------------------- Attribute */
$sectionTitle('Attribute');
$boxW = 60.0;
$boxH = 54.0;
$gap = 6.0;
$x = $M;
foreach (WS_ATTRIBUTES as $field => [$short, $label]) {
$value = (int) ($c[$field] ?? 10);
$pdf->rect($x, $y, $boxW, $boxH, $parchment2, $gold, 0.5);
$pdf->textCenter($x + $boxW / 2, $y + 13, 7.2, 'F2', $soft, $short);
$pdf->textCenter($x + $boxW / 2, $y + 34, 17, 'F4', $ink, (string) $value);
$pdf->textCenter($x + $boxW / 2, $y + 46, 8, 'F2', $gold, signed(attribute_bonus($value)));
$x += $boxW + $gap;
}
// Kampfblock rechts
$combatX = $x + 4;
$combatW = $W - $M - $combatX;
$pdf->rect($combatX, $y, $combatW, $boxH, null, $gold, 0.6);
$combatRows = [
['Trefferpunkte', (int) $c['hp_current'] . ' / ' . (int) $c['hp_max']],
['Rüstungsklasse', (string) (int) ($extra['ac']['total'] ?? 10)],
['Angriff', signed((int) $c['attack_bonus'])],
['Gold', (int) $c['gold_coins'] . ' GM'],
];
$ry = $y + 12;
$rowStep = 12.0;
foreach ($combatRows as $i => [$label, $value]) {
$pdf->text($combatX + 7, $ry, 7.0, 'F1', $soft, $label);
$pdf->textRight($combatX + $combatW - 7, $ry, 9, 'F2', $ink, $value);
$ry += $rowStep;
}
$y += $boxH + 6;
/* ----------------------------------------------------- Rettungswürfe */
$saves = [];
foreach (WS_SAVES as $field => $label) {
$saves[$label] = save_display($c[$field] ?? null);
}
$sectionTitle('Rettungswürfe');
$sw = ($inner - 4 * 5) / 5;
$sx = $M;
foreach ($saves as $label => $value) {
$pdf->rect($sx, $y, $sw, 32, $parchment2, $gold, 0.4);
$pdf->textCenter($sx + $sw / 2, $y + 12, 6.8, 'F1', $soft, $label);
$pdf->textCenter($sx + $sw / 2, $y + 27, 13, 'F4', $ink, $value);
$sx += $sw + 5;
}
$y += 38;
/* ------------------------------------------------- Bewaffnung / Rüstung */
$weapons = $extra['weapons'] ?? [];
if ($weapons !== []) {
$sectionTitle('Bewaffnung');
$ensure(14);
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'WAFFE');
$pdf->text($M + 200, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner - 74, $y + 8, 6.8, 'F2', $soft, 'TREFFER');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'SCHADEN');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($weapons as $weapon) {
$ensure(13);
$weaponName = shorten((string) $weapon['name'], 40);
$pdf->text($M, $y + 8.5, 8.6, 'F3', $ink, $weaponName);
$weaponNote = trim((string) ($weapon['note'] ?? ''));
if ($weaponNote !== '') {
$noteX = $M + WsPdf::textWidth($weaponName, 8.6, 'F3') + 6;
$noteLimit = $M + 194;
if ($noteLimit - $noteX > 26) {
$pdf->text($noteX, $y + 8.5, 6.8, 'F1', $soft, WsPdf::fit($weaponNote, 6.8, 'F1', $noteLimit - $noteX));
}
}
$pdf->text($M + 200, $y + 8.5, 7.6, 'F1', $soft, (string) $weapon['category']);
$pdf->textRight($M + $inner - 74, $y + 8.5, 8.6, 'F2', $ink, signed(weapon_attack_total($c, $weapon)));
$pdf->textRight($M + $inner, $y + 8.5, 8.6, 'F3', $ink, weapon_damage_total($c, $weapon));
$pdf->line($M, $y + 10.8, $M + $inner, $y + 10.8, $goldLight, 0.22);
$y += 13;
}
$y += 4;
}
$armors = $extra['armors'] ?? [];
if ($armors !== []) {
$sectionTitle('Rüstung');
$ensure(14);
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'TEIL');
$pdf->text($M + 200, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner - 60, $y + 8, 6.8, 'F2', $soft, 'RK-BONUS');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'STATUS');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($armors as $armor) {
$ensure(13);
$armorName = shorten((string) $armor['name'], 40);
$pdf->text($M, $y + 8.5, 8.6, 'F3', $ink, $armorName);
$armorNote = trim((string) ($armor['note'] ?? ''));
if ($armorNote !== '') {
$noteX = $M + WsPdf::textWidth($armorName, 8.6, 'F3') + 6;
$noteLimit = $M + 194;
if ($noteLimit - $noteX > 26) {
$pdf->text($noteX, $y + 8.5, 6.8, 'F1', $soft, WsPdf::fit($armorNote, 6.8, 'F1', $noteLimit - $noteX));
}
}
$pdf->text($M + 200, $y + 8.5, 7.6, 'F1', $soft, (string) $armor['kind']);
$pdf->textRight($M + $inner - 60, $y + 8.5, 8.6, 'F2', $ink, signed((int) $armor['ac_bonus']));
$pdf->textRight($M + $inner, $y + 8.5, 7.6, 'F1', $soft, (int) $armor['equipped'] === 1 ? 'angelegt' : 'abgelegt');
$pdf->line($M, $y + 10.8, $M + $inner, $y + 10.8, $goldLight, 0.22);
$y += 13;
}
$acInfo = $extra['ac'] ?? null;
if (is_array($acInfo)) {
$ensure(14);
$pdf->text($M, $y + 9, 8.2, 'F5', $soft, sprintf(
'Rüstungsklasse gesamt: 10 %s Geschick %s Rüstung = %d%s',
signed((int) $acInfo['dexterity']),
signed((int) $acInfo['armor']),
(int) $acInfo['total'],
$acInfo['manual_active'] ? ' (manuell überschrieben)' : ''
));
$y += 14;
}
$y += 2;
}
/* -------------------------------------------------------- Fertigkeiten */
$skills = $extra['skills'] ?? [];
if ($skills !== []) {
$sectionTitle('Fertigkeiten');
foreach (skills_grouped($skills) as $group => $rows) {
$ensure(26);
$pdf->text($M, $y + 9, 7.0, 'F2', $gold, mb_strtoupper($group, 'UTF-8'));
$pdf->line($M, $y + 12.5, $M + $inner, $y + 12.5, $goldLight, 0.3);
$y += 15;
$colW = ($inner - 16) / 2;
$perColumn = (int) ceil(count($rows) / 2);
for ($i = 0; $i < $perColumn; $i++) {
$ensure(13);
foreach ([0, 1] as $col) {
$index = $i + $col * $perColumn;
if (!isset($rows[$index])) {
continue;
}
$skill = $rows[$index];
$cx = $M + $col * ($colW + 16);
$mod = skill_attribute_modifier($c, $skill);
$skillName = shorten((string) $skill['name'], 34);
$pdf->text($cx, $y + 8.5, 8.4, 'F3', $ink, $skillName);
$skillNote = trim((string) ($skill['note'] ?? ''));
if ($skillNote !== '') {
$noteX = $cx + WsPdf::textWidth($skillName, 8.4, 'F3') + 6;
$noteLimit = $cx + $colW - 84;
if ($noteLimit - $noteX > 26) {
$pdf->text($noteX, $y + 8.4, 6.8, 'F1', $soft, WsPdf::fit($skillNote, 6.8, 'F1', $noteLimit - $noteX));
}
}
$pdf->text($cx + $colW - 64, $y + 8.2, 6.6, 'F1', $soft, trim((string) ($skill['attribute'] ?? '')));
$pdf->textRight($cx + $colW - 28, $y + 8.2, 6.8, 'F1', $soft, $mod === null ? '–' : signed($mod));
$pdf->textRight($cx + $colW, $y + 8.5, 8.4, 'F2', $gold, (string) max(0, (int) $skill['bonus']));
$pdf->line($cx, $y + 10.4, $cx + $colW, $y + 10.4, $goldLight, 0.22);
}
$y += 12.6;
}
$y += 4;
}
}
/* ------------------------------------------------- Zauber & Liturgien */
$spells = $extra['spells'] ?? ['arkan' => [], 'klerikal' => []];
$spellBlocks = [
'arkan' => ['Arkane Zauber', $spells['arkan'] ?? []],
'klerikal' => ['Göttliche Liturgien', $spells['klerikal'] ?? []],
];
// Spalten des Zauberverzeichnisses (A4-Innenbreite rund 527 pt)
$spellCols = [
'name' => [$M, 142.0],
'range' => [$M + 146.0, 78.0],
'duration' => [$M + 228.0, 100.0],
'summary' => [$M + 332.0, $inner - 332.0],
];
// Zelleninhalt auf n Zeilen umbrechen, letzte Zeile bei Bedarf kürzen.
$cellLines = static function (string $text, float $width, int $maxLines): array {
$text = trim(preg_replace('/\s+/u', ' ', $text) ?? '');
if ($text === '') {
return ['–'];
}
$wrapped = WsPdf::wrap($text, 7.4, 'F1', $width);
if (count($wrapped) > $maxLines) {
$lines = array_slice($wrapped, 0, $maxLines - 1);
$lines[] = WsPdf::fit(implode(' ', array_slice($wrapped, $maxLines - 1)), 7.4, 'F1', $width);
return $lines;
}
return $wrapped;
};
// Kopf je Grad – wird nach einem Seitenumbruch erneut gesetzt.
$spellHead = static function (int $grade, string $firstHead) use (&$y, $pdf, $gold, $soft, $spellCols, $M, $inner): void {
$pdf->text($M, $y + 8, 7.8, 'F4', $gold, 'Grad ' . $grade);
$y += 11;
foreach ([[$firstHead, 'name'], ['REICHWEITE', 'range'], ['DAUER', 'duration'], ['BESCHREIBUNG', 'summary']] as [$headLabel, $colKey]) {
$pdf->text($spellCols[$colKey][0], $y + 6.5, 6.4, 'F2', $soft, $headLabel);
}
$y += 9;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 3.5;
};
foreach ($spellBlocks as $key => [$label, $list]) {
if ($list === []) {
continue;
}
$sectionTitle($label);
$byGrade = [];
foreach ($list as $spell) {
$byGrade[(int) $spell['grade']][] = $spell;
}
ksort($byGrade);
$firstHead = $key === 'klerikal' ? 'LITURGIE' : 'ZAUBER';
foreach ($byGrade as $grade => $entries) {
$ensure(44);
$spellHead((int) $grade, $firstHead);
foreach ($entries as $entry) {
$range = trim((string) ($entry['range_text'] ?? ''));
$duration = trim((string) ($entry['duration'] ?? ''));
$summary = trim((string) ($entry['summary'] ?? ''));
$note = trim((string) ($entry['note'] ?? ''));
if ($note !== '') {
$summary = $summary === '' ? $note : $summary . ' · ' . $note;
}
// Reichweite und Dauer zwei Zeilen, Beschreibung drei Zeilen.
$rangeLines = $cellLines($range, $spellCols['range'][1] - 6, 2);
$durationLines = $cellLines($duration, $spellCols['duration'][1] - 6, 2);
$descLines = $cellLines($summary, $spellCols['summary'][1], 3);
$lineCount = max(count($rangeLines), count($durationLines), count($descLines));
$rowHeight = max(12.0, $lineCount * 8.6 + 4.0);
if ($y + $rowHeight > $H - 70) {
$newPage();
$spellHead((int) $grade, $firstHead);
}
$pdf->text($spellCols['name'][0], $y + 8, 8.2, 'F3', $ink, WsPdf::fit((string) $entry['name'], 8.2, 'F3', $spellCols['name'][1] - 6));
foreach ($rangeLines as $i => $line) {
$pdf->text($spellCols['range'][0], $y + 8 + $i * 8.6, 7.4, 'F1', $soft, $line);
}
foreach ($durationLines as $i => $line) {
$pdf->text($spellCols['duration'][0], $y + 8 + $i * 8.6, 7.4, 'F1', $soft, $line);
}
foreach ($descLines as $i => $line) {
$pdf->text($spellCols['summary'][0], $y + 8 + $i * 8.6, 7.4, 'F1', $soft, $line);
}
$y += $rowHeight;
$pdf->line($M, $y - 1.6, $M + $inner, $y - 1.6, $goldLight, 0.22);
}
$y += 6;
}
$y += 2;
}
/* -------------------------------------------------------------- Inventar */
$items = $extra['items'] ?? [];
if ($items !== []) {
$sectionTitle('Inventar');
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'ANZAHL');
$pdf->text($M + 52, $y + 8, 6.8, 'F2', $soft, 'GEGENSTAND');
$pdf->text($M + $inner - 78, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'WERT');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($items as $item) {
$ensure(12);
$itemName = shorten((string) $item['name'], 56);
$pdf->text($M, $y + 8, 8.4, 'F1', $ink, ((int) $item['qty']) . '×');
$pdf->text($M + 52, $y + 8, 8.6, 'F3', $ink, $itemName);
$itemNote = trim((string) ($item['note'] ?? ''));
if ($itemNote !== '') {
$noteX = $M + 52 + WsPdf::textWidth($itemName, 8.6, 'F3') + 6;
$noteLimit = $M + $inner - 96;
if ($noteLimit - $noteX > 26) {
$pdf->text($noteX, $y + 8, 6.8, 'F1', $soft, WsPdf::fit($itemNote, 6.8, 'F1', $noteLimit - $noteX));
}
}
$pdf->text($M + $inner - 78, $y + 8, 7.4, 'F1', $soft, (string) $item['category']);
$pdf->textRight($M + $inner, $y + 8, 8.2, 'F3', $ink, (string) ($item['value'] !== '' ? $item['value'] : '–'));
$pdf->line($M, $y + 10.4, $M + $inner, $y + 10.4, $goldLight, 0.22);
$y += 12.6;
}
$y += 4;
}
/* ------------------------------------------------------ Person & Notizen */
$facts = [];
if ((string) ($c['gender'] ?? '') !== '') {
$facts[] = 'Geschlecht: ' . $c['gender'];
}
if ((string) $c['deity'] !== '') {
$facts[] = 'Gottheit: ' . $c['deity'];
}
if ((string) $c['age'] !== '') {
$facts[] = 'Alter: ' . $c['age'];
}
if ((string) $c['height'] !== '') {
$facts[] = 'Größe: ' . $c['height'];
}
if ((string) $c['weight'] !== '') {
$facts[] = 'Gewicht: ' . $c['weight'];
}
if ($facts !== []) {
$sectionTitle('Person');
$paragraph(implode(' · ', $facts), 9);
}
foreach (['appearance' => 'Erscheinung', 'background' => 'Hintergrund', 'notes' => 'Notizen'] as $field => $label) {
if (trim((string) $c[$field]) === '') {
continue;
}
$sectionTitle($label);
$paragraph((string) $c[$field]);
}
return $pdf->output();
}