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 ReportHtmlFootnote 20 */ 21class ReportHtmlFootnote extends ReportBaseFootnote 22{ 23 /** 24 * HTML Footnotes number renderer 25 * 26 * @param ReportHtml $renderer 27 */ 28 public function render($renderer) 29 { 30 $renderer->setCurrentStyle('footnotenum'); 31 echo '<a href="#footnote', $this->num, '"><sup>'; 32 $renderer->write($renderer->entityRTL . $this->num); 33 echo "</sup></a>\n"; 34 } 35 36 /** 37 * Write the Footnote text 38 * Uses style name "footnote" by default 39 * 40 * @param ReportHtml $html 41 */ 42 public function renderFootnote($html) 43 { 44 if ($html->getCurrentStyle() != $this->styleName) { 45 $html->setCurrentStyle($this->styleName); 46 } 47 48 $temptext = str_replace('#PAGENUM#', $html->pageNo(), $this->text); 49 // underline «title» part of Source item 50 $temptext = str_replace([ 51 '«', 52 '»', 53 ], [ 54 '<u>', 55 '</u>', 56 ], $temptext); 57 echo "\n<div><a name=\"footnote", $this->num, '"></a>'; 58 $html->write($this->num . '. ' . $temptext); 59 echo '</div>'; 60 61 $html->setXy(0, $html->getY() + $this->getFootnoteHeight($html)); 62 } 63 64 /** 65 * Calculates the Footnotes height 66 * 67 * @param ReportHtml $html 68 * @param int $cellWidth The width of the cell to use it for text wraping 69 * 70 * @return int Footnote height in points 71 */ 72 public function getFootnoteHeight($html, $cellWidth = 0): int 73 { 74 if ($html->getCurrentStyle() != $this->styleName) { 75 $html->setCurrentStyle($this->styleName); 76 } 77 78 if ($cellWidth > 0) { 79 $this->text = $html->textWrap($this->text, $cellWidth); 80 } 81 $this->text = $this->text . "\n\n"; 82 $ct = substr_count($this->text, "\n"); 83 $fsize = $html->getCurrentStyleHeight(); 84 85 return ($fsize * $ct) * $html->cellHeightRatio; 86 } 87 88 /** 89 * Get the width of text 90 * Breaks up a text into lines if needed 91 * 92 * @param ReportHtml $html 93 * 94 * @return float|array 95 */ 96 public function getWidth($html) 97 { 98 // Setup the style name 99 $html->setCurrentStyle('footnotenum'); 100 101 // Check for the largest font size in the box 102 $fsize = $html->getCurrentStyleHeight(); 103 if ($fsize > $html->largestFontHeight) { 104 $html->largestFontHeight = $fsize; 105 } 106 107 // Returns the Object if already numbered else false 108 if (empty($this->num)) { 109 $html->checkFootnote($this); 110 } 111 112 // Get the line width for the text in points + a little margin 113 $lw = $html->getStringWidth($this->numText); 114 // Line Feed counter - Number of lines in the text 115 $lfct = $html->countLines($this->numText); 116 // If there is still remaining wrap width... 117 if ($this->wrapWidthRemaining > 0) { 118 // Check with line counter too! 119 if ($lw >= $this->wrapWidthRemaining || $lfct > 1) { 120 $newtext = ''; 121 $wrapWidthRemaining = $this->wrapWidthRemaining; 122 $lines = explode("\n", $this->numText); 123 // Go throught the text line by line 124 foreach ($lines as $line) { 125 // Line width in points + a little margin 126 $lw = $html->getStringWidth($line); 127 // If the line has to be wraped 128 if ($lw > $wrapWidthRemaining) { 129 $words = explode(' ', $line); 130 $addspace = count($words); 131 $lw = 0; 132 foreach ($words as $word) { 133 $addspace--; 134 $lw += $html->getStringWidth($word . ' '); 135 if ($lw <= $wrapWidthRemaining) { 136 $newtext .= $word; 137 if ($addspace != 0) { 138 $newtext .= ' '; 139 } 140 } else { 141 $lw = $html->getStringWidth($word . ' '); 142 $newtext .= "\n$word"; 143 if ($addspace != 0) { 144 $newtext .= ' '; 145 } 146 // Reset the wrap width to the cell width 147 $wrapWidthRemaining = $this->wrapWidthCell; 148 } 149 } 150 } else { 151 $newtext .= $line; 152 } 153 // Check the Line Feed counter 154 if ($lfct > 1) { 155 // Add a new line feed as long as it’s not the last line 156 $newtext .= "\n"; 157 // Reset the line width 158 $lw = 0; 159 // Reset the wrap width to the cell width 160 $wrapWidthRemaining = $this->wrapWidthCell; 161 } 162 $lfct--; 163 } 164 $this->numText = $newtext; 165 $lfct = substr_count($this->numText, "\n"); 166 167 return [ 168 $lw, 169 1, 170 $lfct, 171 ]; 172 } 173 } 174 $l = 0; 175 $lfct = substr_count($this->numText, "\n"); 176 if ($lfct > 0) { 177 $l = 2; 178 } 179 180 return [ 181 $lw, 182 $l, 183 $lfct, 184 ]; 185 } 186} 187