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