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