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