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