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