xref: /webtrees/app/Report/ReportHtmlFootnote.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
1<?php
2namespace Fisharebest\Webtrees\Report;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * class ReportHtmlFootnote
21 */
22class ReportHtmlFootnote extends ReportBaseFootnote {
23	/**
24	 * HTML Footnotes number renderer
25	 *
26	 * @param ReportHtml $renderer
27	 */
28	public function render($renderer) {
29		$renderer->setCurrentStyle("footnotenum");
30		echo "<a href=\"#footnote", $this->num, "\"><sup>";
31		$renderer->write($renderer->entityRTL . $this->num);
32		echo "</sup></a>\n";
33	}
34
35	/**
36	 * Write the Footnote text
37	 * Uses style name "footnote" by default
38	 *
39	 * @param ReportHtml $html
40	 */
41	public function renderFootnote($html) {
42
43		if ($html->getCurrentStyle() != $this->styleName) {
44			$html->setCurrentStyle($this->styleName);
45		}
46
47		$temptext = str_replace("#PAGENUM#", $html->pageNo(), $this->text);
48		// underline «title» part of Source item
49		$temptext = str_replace(array('«', '»'), array('<u>', '</u>'), $temptext);
50		echo "\n<div><a name=\"footnote", $this->num, "\"></a>";
51		$html->write($this->num . ". " . $temptext);
52		echo "</div>";
53
54		$html->setXy(0, $html->getY() + $this->getFootnoteHeight($html));
55	}
56
57	/**
58	 * Calculates the Footnotes height
59	 *
60	 * @param ReportHtml $html
61	 * @param int        $cellWidth The width of the cell to use it for text wraping
62	 *
63	 * @return int       Footnote height in points
64	 */
65	public function getFootnoteHeight($html, $cellWidth = 0) {
66		if ($html->getCurrentStyle() != $this->styleName) {
67			$html->setCurrentStyle($this->styleName);
68		}
69
70		if ($cellWidth > 0) {
71			$this->text = $html->textWrap($this->text, $cellWidth);
72		}
73		$this->text = $this->text . "\n\n";
74		$ct         = substr_count($this->text, "\n");
75		$fsize      = $html->getCurrentStyleHeight();
76
77		return ($fsize * $ct) * $html->cellHeightRatio;
78	}
79
80	/**
81	 * Get the width of text
82	 * Breaks up a text into lines if needed
83	 *
84	 * @param ReportHtml $html
85	 *
86	 * @return array
87	 */
88	public function getWidth($html) {
89		// Setup the style name
90		$html->setCurrentStyle("footnotenum");
91
92		// Check for the largest font size in the box
93		$fsize = $html->getCurrentStyleHeight();
94		if ($fsize > $html->largestFontHeight) {
95			$html->largestFontHeight = $fsize;
96		}
97
98		// Returns the Object if already numbered else false
99		if (empty($this->num)) {
100			$html->checkFootnote($this);
101		}
102
103		// Get the line width for the text in points + a little margin
104		$lw = $html->GetStringWidth($this->numText);
105		// Line Feed counter - Number of lines in the text
106		$lfct = $html->countLines($this->numText);
107		// If there is still remaining wrap width...
108		if ($this->wrapWidthRemaining > 0) {
109			// Check with line counter too!
110			if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
111				$newtext            = "";
112				$wrapWidthRemaining = $this->wrapWidthRemaining;
113				$lines              = explode("\n", $this->numText);
114				// Go throught the text line by line
115				foreach ($lines as $line) {
116					// Line width in points + a little margin
117					$lw = $html->GetStringWidth($line);
118					// If the line has to be wraped
119					if ($lw > $wrapWidthRemaining) {
120						$words    = explode(" ", $line);
121						$addspace = count($words);
122						$lw       = 0;
123						foreach ($words as $word) {
124							$addspace--;
125							$lw += $html->GetStringWidth($word . " ");
126							if ($lw <= $wrapWidthRemaining) {
127								$newtext .= $word;
128								if ($addspace != 0) {
129									$newtext .= " ";
130								}
131							} else {
132								$lw = $html->GetStringWidth($word . " ");
133								$newtext .= "\n$word";
134								if ($addspace != 0) {
135									$newtext .= " ";
136								}
137								// Reset the wrap width to the cell width
138								$wrapWidthRemaining = $this->wrapWidthCell;
139							}
140						}
141					} else {
142						$newtext .= $line;
143					}
144					// Check the Line Feed counter
145					if ($lfct > 1) {
146						// Add a new line feed as long as it’s not the last line
147						$newtext .= "\n";
148						// Reset the line width
149						$lw = 0;
150						// Reset the wrap width to the cell width
151						$wrapWidthRemaining = $this->wrapWidthCell;
152					}
153					$lfct--;
154				}
155				$this->numText = $newtext;
156				$lfct          = substr_count($this->numText, "\n");
157
158				return array($lw, 1, $lfct);
159			}
160		}
161		$l    = 0;
162		$lfct = substr_count($this->numText, "\n");
163		if ($lfct > 0) {
164			$l = 2;
165		}
166
167		return array($lw, $l, $lfct);
168	}
169}
170