1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Report; 19 20use Fisharebest\Webtrees\Functions\FunctionsRtl; 21 22/** 23 * Class ReportPdfText 24 */ 25class ReportPdfText extends ReportBaseText 26{ 27 /** 28 * PDF Text renderer 29 * 30 * @param ReportTcpdf $renderer 31 * 32 * @return void 33 */ 34 public function render($renderer) 35 { 36 // Set up the style 37 if ($renderer->getCurrentStyle() != $this->styleName) { 38 $renderer->setCurrentStyle($this->styleName); 39 } 40 $temptext = str_replace('#PAGENUM#', $renderer->PageNo(), $this->text); 41 // underline «title» part of Source item 42 $temptext = str_replace([ 43 '«', 44 '»', 45 ], [ 46 '<u>', 47 '</u>', 48 ], $temptext); 49 50 // Paint the text color or they might use inherited colors by the previous function 51 $match = []; 52 if (preg_match('/#?(..)(..)(..)/', $this->color, $match)) { 53 $r = hexdec($match[1]); 54 $g = hexdec($match[2]); 55 $b = hexdec($match[3]); 56 $renderer->SetTextColor($r, $g, $b); 57 } else { 58 $renderer->SetTextColor(0, 0, 0); 59 } 60 $temptext = FunctionsRtl::spanLtrRtl($temptext); 61 $temptext = str_replace( 62 [ 63 '<br><span dir="rtl" >', 64 '<br><span dir="ltr" >', 65 '> ', 66 ' <', 67 ], 68 [ 69 '<span dir="rtl" ><br>', 70 '<span dir="ltr" ><br>', 71 '> ', 72 ' <', 73 ], 74 $temptext 75 ); 76 $renderer->writeHTML( 77 $temptext, 78 false, 79 false, 80 true, 81 false, 82 '' 83 ); //change height - line break etc. - the form is mirror on rtl pages 84 // Reset the text color to black or it will be inherited 85 $renderer->SetTextColor(0, 0, 0); 86 } 87 88 /** 89 * Returns the height in points of the text element 90 * 91 * The height is already calculated in getWidth() 92 * 93 * @param ReportTcpdf $pdf 94 * 95 * @return float 0 96 */ 97 public function getHeight($pdf): float 98 { 99 return 0; 100 } 101 102 /** 103 * Splits the text into lines if necessary to fit into a giving cell 104 * 105 * @param ReportTcpdf $pdf 106 * 107 * @return float|array 108 */ 109 public function getWidth($pdf) 110 { 111 // Setup the style name, a font must be selected to calculate the width 112 if ($pdf->getCurrentStyle() != $this->styleName) { 113 $pdf->setCurrentStyle($this->styleName); 114 } 115 // Check for the largest font size in the box 116 $fsize = $pdf->getCurrentStyleHeight(); 117 if ($fsize > $pdf->largestFontHeight) { 118 $pdf->largestFontHeight = $fsize; 119 } 120 121 // Get the line width 122 $lw = $pdf->GetStringWidth($this->text); 123 // Line Feed counter - Number of lines in the text 124 $lfct = substr_count($this->text, "\n") + 1; 125 // If there is still remaining wrap width... 126 if ($this->wrapWidthRemaining > 0) { 127 // Check with line counter too! 128 // but floor the $wrapWidthRemaining first to keep it bugfree! 129 $wrapWidthRemaining = (int) ($this->wrapWidthRemaining); 130 if ($lw >= $wrapWidthRemaining || $lfct > 1) { 131 $newtext = ''; 132 $lines = explode("\n", $this->text); 133 // Go throught the text line by line 134 foreach ($lines as $line) { 135 // Line width in points + a little margin 136 $lw = $pdf->GetStringWidth($line); 137 // If the line has to be wraped 138 if ($lw >= $wrapWidthRemaining) { 139 $words = explode(' ', $line); 140 $addspace = count($words); 141 $lw = 0; 142 foreach ($words as $word) { 143 $addspace--; 144 $lw += $pdf->GetStringWidth($word . ' '); 145 if ($lw <= $wrapWidthRemaining) { 146 $newtext .= $word; 147 if ($addspace != 0) { 148 $newtext .= ' '; 149 } 150 } else { 151 $lw = $pdf->GetStringWidth($word . ' '); 152 $newtext .= "\n$word"; 153 if ($addspace != 0) { 154 $newtext .= ' '; 155 } 156 // Reset the wrap width to the cell width 157 $wrapWidthRemaining = $this->wrapWidthCell; 158 } 159 } 160 } else { 161 $newtext .= $line; 162 } 163 // Check the Line Feed counter 164 if ($lfct > 1) { 165 // Add a new line as long as it’s not the last line 166 $newtext .= "\n"; 167 // Reset the line width 168 $lw = 0; 169 // Reset the wrap width to the cell width 170 $wrapWidthRemaining = $this->wrapWidthCell; 171 } 172 $lfct--; 173 } 174 $this->text = $newtext; 175 $lfct = substr_count($this->text, "\n"); 176 177 return [ 178 $lw, 179 1, 180 $lfct, 181 ]; 182 } 183 } 184 $l = 0; 185 $lfct = substr_count($this->text, "\n"); 186 if ($lfct > 0) { 187 $l = 2; 188 } 189 190 return [ 191 $lw, 192 $l, 193 $lfct, 194 ]; 195 } 196} 197