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