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 20/** 21 * Class ReportHtmlTextbox 22 */ 23class ReportHtmlTextbox extends ReportBaseTextbox 24{ 25 /** 26 * Render the elements. 27 * 28 * @param ReportHtml $renderer 29 * 30 * @return void 31 */ 32 public function render($renderer) 33 { 34 // checkFootnote 35 $newelements = []; 36 $lastelement = []; 37 $footnote_element = []; 38 // Element counter 39 $cE = count($this->elements); 40 //-- collapse duplicate elements 41 for ($i = 0; $i < $cE; $i++) { 42 $element = $this->elements[$i]; 43 if (is_object($element)) { 44 if ($element instanceof ReportBaseText) { 45 if (!empty($footnote_element)) { 46 ksort($footnote_element); 47 foreach ($footnote_element as $links) { 48 $newelements[] = $links; 49 } 50 $footnote_element = []; 51 } 52 if (empty($lastelement)) { 53 $lastelement = $element; 54 } else { 55 // Checking if the Text has the same style 56 if ($element->getStyleName() == $lastelement->getStyleName()) { 57 $lastelement->addText(str_replace("\n", '<br>', $element->getValue())); 58 } elseif (!empty($lastelement)) { 59 $newelements[] = $lastelement; 60 $lastelement = $element; 61 } 62 } 63 } elseif ($element instanceof ReportBaseFootnote) { 64 // Check if the Footnote has been set with it’s link number 65 $renderer->checkFootnote($element); 66 // Save first the last element if any 67 if (!empty($lastelement)) { 68 $newelements[] = $lastelement; 69 $lastelement = []; 70 } 71 // Save the Footnote with it’s link number as key for sorting later 72 $footnote_element[$element->num] = $element; 73 } elseif (!($element instanceof ReportBaseFootnote) || trim($element->getValue()) != '') { 74 // Do not keep empty footnotes 75 if (!empty($footnote_element)) { 76 ksort($footnote_element); 77 foreach ($footnote_element as $links) { 78 $newelements[] = $links; 79 } 80 $footnote_element = []; 81 } 82 if (!empty($lastelement)) { 83 $newelements[] = $lastelement; 84 $lastelement = []; 85 } 86 $newelements[] = $element; 87 } 88 } else { 89 if (!empty($lastelement)) { 90 $newelements[] = $lastelement; 91 $lastelement = []; 92 } 93 if (!empty($footnote_element)) { 94 ksort($footnote_element); 95 foreach ($footnote_element as $links) { 96 $newelements[] = $links; 97 } 98 $footnote_element = []; 99 } 100 $newelements[] = $element; 101 } 102 } 103 if (!empty($lastelement)) { 104 $newelements[] = $lastelement; 105 } 106 if (!empty($footnote_element)) { 107 ksort($footnote_element); 108 foreach ($footnote_element as $links) { 109 $newelements[] = $links; 110 } 111 } 112 $this->elements = $newelements; 113 unset($footnote_element, $lastelement, $newelements); 114 115 $cP = 0; // Class Padding 116 117 // Used with line breaks and cell height calculation within this box only 118 $renderer->largestFontHeight = 0; 119 120 // Current position 121 if ($this->left == '.') { 122 $cX = $renderer->getX(); 123 } else { 124 $cX = $this->left; 125 $renderer->setX($cX); 126 } 127 // Current position (top) 128 if ($this->top == '.') { 129 $this->top = $renderer->getY(); 130 } else { 131 $renderer->setY($this->top); 132 } 133 134 // Check the width if set to page wide OR set by xml to larger then page wide 135 if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) { 136 $this->width = $renderer->getRemainingWidth(); 137 } 138 // Setup the CellPadding 139 if ($this->padding) { 140 $cP = $renderer->cPadding; 141 } 142 143 // For padding, we have to use less wrap width 144 $cW = $this->width - ($cP * 2); 145 146 //-- calculate the text box height 147 // Number of lines, will be converted to height 148 $cHT = 0; 149 // Element height (exept text) 150 $eH = 0; 151 // Footnote height (in points) 152 $fH = 0; 153 $w = 0; 154 //-- $lw is an array 155 // 0 => last line width 156 // 1 => 1 if text was wrapped, 0 if text did not wrap 157 // 2 => number of LF 158 $lw = []; 159 // Element counter 160 $cE = count($this->elements); 161 for ($i = 0; $i < $cE; $i++) { 162 if (is_object($this->elements[$i])) { 163 $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW); 164 if ($ew == $cW) { 165 $w = 0; 166 } 167 $lw = $this->elements[$i]->getWidth($renderer); 168 // Text is already gets the # LF 169 $cHT += $lw[2]; 170 if ($lw[1] == 1) { 171 $w = $lw[0]; 172 } elseif ($lw[1] == 2) { 173 $w = 0; 174 } else { 175 $w += $lw[0]; 176 } 177 if ($w > $cW) { 178 $w = $lw[0]; 179 } 180 // For anything else but text (images), get the height 181 $eH += $this->elements[$i]->getHeight($renderer); 182 } else { 183 $fH += abs($renderer->getFootnotesHeight($cW)); 184 } 185 } 186 // Add up what’s the final height 187 $cH = $this->height; 188 // If any element exist 189 if ($cE > 0) { 190 // Check if this is text or some other element, like images 191 if ($eH == 0) { 192 // Number of LF but at least one line 193 $cHT = ($cHT + 1) * $renderer->cellHeightRatio; 194 // Calculate the cell hight with the largest font size used 195 $cHT = $cHT * $renderer->largestFontHeight; 196 if ($cH < $cHT) { 197 $cH = $cHT; 198 } 199 } else { 200 // This is any other element 201 if ($cH < $eH) { 202 $cH = $eH; 203 } 204 // Add Footnote height to the rest of the height 205 $cH += $fH; 206 } 207 } 208 209 unset($lw, $cHT, $fH, $w); 210 211 // Finaly, check the last cells height 212 if ($cH < $renderer->lastCellHeight) { 213 $cH = $renderer->lastCellHeight; 214 } 215 // Update max Y incase of a pagebreak 216 // We don't want to over write any images or other stuff 217 $renderer->addMaxY($this->top + $cH); 218 219 // Start to print HTML 220 echo '<div style="position:absolute;top:', $this->top, 'pt;'; 221 // LTR (left) or RTL (right) 222 echo $renderer->alignRTL, ':', $cX, 'pt;'; 223 // Background color 224 if ($this->fill) { 225 if (!empty($this->bgcolor)) { 226 echo ' background-color:', $this->bgcolor, ';'; 227 } 228 } 229 // Print padding only when it’s set 230 if ($this->padding) { 231 // Use Cell around padding to support RTL also 232 echo 'padding:', $cP, 'pt;'; 233 } 234 // Border setup 235 if ($this->border) { 236 echo ' border:solid black 1pt;'; 237 echo 'width:', ($this->width - 1 - ($cP * 2)), 'pt;height:', $cH - 1, 'pt;'; 238 } else { 239 echo 'width:', ($this->width - ($cP * 2)), 'pt;height:', $cH, 'pt;'; 240 } 241 echo '">'; 242 243 // Do a little "margin" trick before print 244 // to get the correct current position => "." 245 $cXT = $renderer->getX(); 246 $cYT = $renderer->getY(); 247 $renderer->setXy(0, 0); 248 249 // Print the text elements 250 foreach ($this->elements as $element) { 251 if (is_object($element)) { 252 $element->render($renderer, $cX, false); 253 } elseif (is_string($element) && $element == 'footnotetexts') { 254 $renderer->footnotes(); 255 } elseif (is_string($element) && $element == 'addpage') { 256 $renderer->addPage(); 257 } 258 } 259 echo "</div>\n"; 260 261 // Reset "margins" 262 $renderer->setXy($cXT, $cYT); 263 // This will be mostly used to trick the multiple images last height 264 if ($this->reseth) { 265 $cH = 0; 266 } 267 // New line and some clean up 268 if (!$this->newline) { 269 $renderer->setXy($cX + $this->width, $this->top); 270 $renderer->lastCellHeight = $cH; 271 } else { 272 $renderer->setXy(0, $this->top + $cH + ($cP * 2)); 273 $renderer->lastCellHeight = 0; 274 } 275 } 276} 277