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 18/** 19 * Class ReportHtmlText 20 */ 21class ReportHtmlText extends ReportBaseText 22{ 23 /** 24 * Render the elements. 25 * 26 * @param ReportHtml $renderer 27 * @param int $curx 28 * @param bool $attrib Is is called from a different element? 29 */ 30 public function render($renderer, $curx = 0, $attrib = true) 31 { 32 33 // Setup the style name 34 if ($renderer->getCurrentStyle() != $this->styleName) { 35 $renderer->setCurrentStyle($this->styleName); 36 } 37 $temptext = str_replace('#PAGENUM#', $renderer->pageNo(), $this->text); 38 // underline «title» part of Source item 39 $temptext = str_replace([ 40 '«', 41 '»', 42 ], [ 43 '<u>', 44 '</u>', 45 ], $temptext); 46 47 // If any text at all 48 if (!empty($temptext)) { 49 // If called by an other element 50 if (!$attrib) { 51 $renderer->write($temptext, $this->color); 52 } else { 53 // Save the start positions 54 $startX = $renderer->getX(); 55 $startY = $renderer->getY(); 56 $width = $renderer->getRemainingWidth(); 57 // If text is wider then page width then wrap it 58 if ($renderer->getStringWidth($temptext) > $width) { 59 $lines = explode("\n", $temptext); 60 foreach ($lines as $line) { 61 echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">'; 62 $line = $renderer->textWrap($line, $width); 63 $startY += $renderer->getTextCellHeight($line); 64 $renderer->setY($startY); 65 $renderer->write($line, $this->color); 66 echo "</div>\n"; 67 } 68 } else { 69 echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">'; 70 $renderer->write($temptext, $this->color); 71 echo "</div>\n"; 72 $renderer->setX($startX + $renderer->getStringWidth($temptext)); 73 if ($renderer->countLines($temptext) != 1) { 74 $renderer->setXy(0, ($startY + $renderer->getTextCellHeight($temptext))); 75 } 76 } 77 } 78 } 79 } 80 81 /** 82 * Returns the height in points of the text element 83 * The height is already calculated in getWidth() 84 * 85 * @param ReportHtml $html 86 * 87 * @return float 88 */ 89 public function getHeight($html) 90 { 91 $ct = substr_count($this->text, "\n"); 92 if ($ct > 0) { 93 $ct += 1; 94 } 95 $style = $html->getStyle($this->styleName); 96 97 return ($style['size'] * $ct) * $html->cellHeightRatio; 98 } 99 100 /** 101 * Get the width of text and wrap it too 102 * 103 * @param ReportHtml $html 104 * 105 * @return array 106 */ 107 public function getWidth($html) 108 { 109 // Setup the style name 110 if ($html->getCurrentStyle() != $this->styleName) { 111 $html->setCurrentStyle($this->styleName); 112 } 113 114 // Check for the largest font size in the box 115 $fsize = $html->getCurrentStyleHeight(); 116 if ($fsize > $html->largestFontHeight) { 117 $html->largestFontHeight = $fsize; 118 } 119 120 // Get the line width for the text in points 121 $lw = $html->getStringWidth($this->text); 122 // Line Feed counter - Number of lines in the text 123 $lfct = $html->countLines($this->text); 124 // If there is still remaining wrap width... 125 if ($this->wrapWidthRemaining > 0) { 126 // Check with line counter too! 127 if ($lw >= $this->wrapWidthRemaining || $lfct > 1) { 128 $newtext = ''; 129 $wrapWidthRemaining = $this->wrapWidthRemaining; 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 = $html->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 += $html->getStringWidth($word . ' '); 143 if ($lw <= $wrapWidthRemaining) { 144 $newtext .= $word; 145 if ($addspace != 0) { 146 $newtext .= ' '; 147 } 148 } else { 149 $lw = $html->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 feed 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