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