1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report; 2176692c8bSGreg Roach 22b6f35a76SGreg Roachuse function count; 23b6f35a76SGreg Roachuse function explode; 24b6f35a76SGreg Roachuse function hexdec; 25b6f35a76SGreg Roachuse function preg_match; 26b6f35a76SGreg Roachuse function str_replace; 27b6f35a76SGreg Roachuse function substr_count; 28b6f35a76SGreg Roach 29a25f0a04SGreg Roach/** 30a25f0a04SGreg Roach * Class ReportPdfText 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass ReportPdfText extends ReportBaseText 33c1010edaSGreg Roach{ 34a25f0a04SGreg Roach /** 35a25f0a04SGreg Roach * PDF Text renderer 36a25f0a04SGreg Roach * 37b6f35a76SGreg Roach * @param PdfRenderer $renderer 38c7ff4153SGreg Roach * 39c7ff4153SGreg Roach * @return void 40a25f0a04SGreg Roach */ 4177bab461SGreg Roach public function render($renderer): void 42c1010edaSGreg Roach { 43a25f0a04SGreg Roach // Set up the style 44b6f35a76SGreg Roach if ($renderer->getCurrentStyle() !== $this->styleName) { 45a25f0a04SGreg Roach $renderer->setCurrentStyle($this->styleName); 46a25f0a04SGreg Roach } 47b6f35a76SGreg Roach $temptext = str_replace('#PAGENUM#', (string) $renderer->tcpdf->PageNo(), $this->text); 48a25f0a04SGreg Roach // underline «title» part of Source item 49c1010edaSGreg Roach $temptext = str_replace([ 50c1010edaSGreg Roach '«', 51c1010edaSGreg Roach '»', 52c1010edaSGreg Roach ], [ 53c1010edaSGreg Roach '<u>', 54c1010edaSGreg Roach '</u>', 55c1010edaSGreg Roach ], $temptext); 56a25f0a04SGreg Roach 57a25f0a04SGreg Roach // Paint the text color or they might use inherited colors by the previous function 5813abd6f3SGreg Roach $match = []; 597a6ee1acSGreg Roach if (preg_match('/#?(..)(..)(..)/', $this->color, $match)) { 60a25f0a04SGreg Roach $r = hexdec($match[1]); 61a25f0a04SGreg Roach $g = hexdec($match[2]); 62a25f0a04SGreg Roach $b = hexdec($match[3]); 63b4c5c807SGreg Roach $renderer->tcpdf->setTextColor($r, $g, $b); 64a25f0a04SGreg Roach } else { 65b4c5c807SGreg Roach $renderer->tcpdf->setTextColor(0, 0, 0); 66a25f0a04SGreg Roach } 675183874fSGreg Roach $temptext = RightToLeftSupport::spanLtrRtl($temptext); 68a25f0a04SGreg Roach $temptext = str_replace( 69c1010edaSGreg Roach [ 70c1010edaSGreg Roach '<br><span dir="rtl">', 71c1010edaSGreg Roach '<br><span dir="ltr">', 72c1010edaSGreg Roach '> ', 73c1010edaSGreg Roach ' <', 74c1010edaSGreg Roach ], 75c1010edaSGreg Roach [ 76c1010edaSGreg Roach '<span dir="rtl" ><br>', 77c1010edaSGreg Roach '<span dir="ltr" ><br>', 78c1010edaSGreg Roach '> ', 79c1010edaSGreg Roach ' <', 80c1010edaSGreg Roach ], 81a25f0a04SGreg Roach $temptext 82a25f0a04SGreg Roach ); 83b6f35a76SGreg Roach $renderer->tcpdf->writeHTML( 84a25f0a04SGreg Roach $temptext, 85a25f0a04SGreg Roach false, 86a25f0a04SGreg Roach false, 87a25f0a04SGreg Roach true, 88a25f0a04SGreg Roach false, 897a6ee1acSGreg Roach '' 90a25f0a04SGreg Roach ); //change height - line break etc. - the form is mirror on rtl pages 91a25f0a04SGreg Roach // Reset the text color to black or it will be inherited 92b4c5c807SGreg Roach $renderer->tcpdf->setTextColor(0, 0, 0); 93a25f0a04SGreg Roach } 94a25f0a04SGreg Roach 95a25f0a04SGreg Roach /** 96a25f0a04SGreg Roach * Returns the height in points of the text element 97a25f0a04SGreg Roach * The height is already calculated in getWidth() 98a25f0a04SGreg Roach * 99b6f35a76SGreg Roach * @param PdfRenderer $renderer 100a25f0a04SGreg Roach * 10167c69ce5SGreg Roach * @return float 102a25f0a04SGreg Roach */ 10367c69ce5SGreg Roach public function getHeight($renderer): float 104c1010edaSGreg Roach { 105a25f0a04SGreg Roach return 0; 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach 108a25f0a04SGreg Roach /** 109a25f0a04SGreg Roach * Splits the text into lines if necessary to fit into a giving cell 110a25f0a04SGreg Roach * 111b6f35a76SGreg Roach * @param PdfRenderer $renderer 112a25f0a04SGreg Roach * 11377bab461SGreg Roach * @return array{0:float,1:int,2:float} 114a25f0a04SGreg Roach */ 11541cfb9e2SGreg Roach public function getWidth($renderer): array 116c1010edaSGreg Roach { 117a25f0a04SGreg Roach // Setup the style name, a font must be selected to calculate the width 118b6f35a76SGreg Roach if ($renderer->getCurrentStyle() !== $this->styleName) { 11967c69ce5SGreg Roach $renderer->setCurrentStyle($this->styleName); 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach 12267c69ce5SGreg Roach // Check for the largest font size in the box 12367c69ce5SGreg Roach $fsize = $renderer->getCurrentStyleHeight(); 12467c69ce5SGreg Roach if ($fsize > $renderer->largestFontHeight) { 12567c69ce5SGreg Roach $renderer->largestFontHeight = $fsize; 12667c69ce5SGreg Roach } 12767c69ce5SGreg Roach 12867c69ce5SGreg Roach // Get the line width for the text in points 129b6f35a76SGreg Roach $lw = $renderer->tcpdf->GetStringWidth($this->text); 130a25f0a04SGreg Roach // Line Feed counter - Number of lines in the text 131a25f0a04SGreg Roach $lfct = substr_count($this->text, "\n") + 1; 132a25f0a04SGreg Roach // If there is still remaining wrap width... 133589feda3SGreg Roach $wrapWidthRemaining = $this->wrapWidthRemaining; 13467c69ce5SGreg Roach if ($wrapWidthRemaining > 0) { 13567c69ce5SGreg Roach // Check with line counter too! 136102a585eSGreg Roach if ($lw >= $wrapWidthRemaining || $lfct > 1) { 1377a6ee1acSGreg Roach $newtext = ''; 138a25f0a04SGreg Roach $lines = explode("\n", $this->text); 139*52f124b0SAlejandro Criado-Pérez // Go through the text line by line 140a25f0a04SGreg Roach foreach ($lines as $line) { 141a25f0a04SGreg Roach // Line width in points + a little margin 142b6f35a76SGreg Roach $lw = $renderer->tcpdf->GetStringWidth($line); 143*52f124b0SAlejandro Criado-Pérez // If the line has to be wrapped 14467c69ce5SGreg Roach if ($lw > $wrapWidthRemaining) { 1457a6ee1acSGreg Roach $words = explode(' ', $line); 146a25f0a04SGreg Roach $addspace = count($words); 147a25f0a04SGreg Roach $lw = 0; 148a25f0a04SGreg Roach foreach ($words as $word) { 149a25f0a04SGreg Roach $addspace--; 150b6f35a76SGreg Roach $lw += $renderer->tcpdf->GetStringWidth($word . ' '); 151a25f0a04SGreg Roach if ($lw <= $wrapWidthRemaining) { 152a25f0a04SGreg Roach $newtext .= $word; 153b6f35a76SGreg Roach if ($addspace !== 0) { 1547a6ee1acSGreg Roach $newtext .= ' '; 155a25f0a04SGreg Roach } 156a25f0a04SGreg Roach } else { 157b6f35a76SGreg Roach $lw = $renderer->tcpdf->GetStringWidth($word . ' '); 158a25f0a04SGreg Roach $newtext .= "\n$word"; 159b6f35a76SGreg Roach if ($addspace !== 0) { 1607a6ee1acSGreg Roach $newtext .= ' '; 161a25f0a04SGreg Roach } 162a25f0a04SGreg Roach // Reset the wrap width to the cell width 163a25f0a04SGreg Roach $wrapWidthRemaining = $this->wrapWidthCell; 164a25f0a04SGreg Roach } 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach } else { 167a25f0a04SGreg Roach $newtext .= $line; 168a25f0a04SGreg Roach } 169a25f0a04SGreg Roach // Check the Line Feed counter 170a25f0a04SGreg Roach if ($lfct > 1) { 171a25f0a04SGreg Roach // Add a new line as long as it’s not the last line 172a25f0a04SGreg Roach $newtext .= "\n"; 173a25f0a04SGreg Roach // Reset the line width 174a25f0a04SGreg Roach $lw = 0; 175a25f0a04SGreg Roach // Reset the wrap width to the cell width 176a25f0a04SGreg Roach $wrapWidthRemaining = $this->wrapWidthCell; 177a25f0a04SGreg Roach } 178a25f0a04SGreg Roach $lfct--; 179a25f0a04SGreg Roach } 180a25f0a04SGreg Roach $this->text = $newtext; 181a25f0a04SGreg Roach $lfct = substr_count($this->text, "\n"); 182a25f0a04SGreg Roach 183c1010edaSGreg Roach return [ 184c1010edaSGreg Roach $lw, 185c1010edaSGreg Roach 1, 186c1010edaSGreg Roach $lfct, 187c1010edaSGreg Roach ]; 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach } 190a25f0a04SGreg Roach $l = 0; 191a25f0a04SGreg Roach $lfct = substr_count($this->text, "\n"); 192a25f0a04SGreg Roach if ($lfct > 0) { 193a25f0a04SGreg Roach $l = 2; 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach 196c1010edaSGreg Roach return [ 197c1010edaSGreg Roach $lw, 198c1010edaSGreg Roach $l, 199c1010edaSGreg Roach $lfct, 200c1010edaSGreg Roach ]; 201a25f0a04SGreg Roach } 202a25f0a04SGreg Roach} 203