xref: /webtrees/app/Report/ReportPdfFootnote.php (revision adc8b18ac5746a1eec1eeed9e4e3f8bf9b0de428)
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 ReportPdfFootnote
21 */
22class ReportPdfFootnote extends ReportBaseFootnote {
23	/**
24	 * PDF Footnotes number renderer
25	 *
26	 * @param ReportTcpdf $renderer
27	 */
28	public function render($renderer) {
29		$renderer->setCurrentStyle("footnotenum");
30		$renderer->Write($renderer->getCurrentStyleHeight(), $this->numText, $this->addlink); //source link numbers after name
31	}
32
33	/**
34	 * Write the Footnote text
35	 * Uses style name "footnote" by default
36	 *
37	 * @param ReportTcpdf $pdf
38	 */
39	public function renderFootnote($pdf) {
40		if ($pdf->getCurrentStyle() != $this->styleName) {
41			$pdf->setCurrentStyle($this->styleName);
42		}
43		$temptext = str_replace("#PAGENUM#", $pdf->PageNo(), $this->text);
44		// Set the link to this y/page position
45		$pdf->SetLink($this->addlink, -1, -1);
46		// Print first the source number
47		// working
48		if ($pdf->getRTL()) {
49			$pdf->writeHTML("<span> ." . $this->num . "</span>", false, false, false, false, "");
50		} else {
51			$temptext = "<span>" . $this->num . ". </span>" . $temptext;
52		}
53		// underline «title» part of Source item
54		$temptext = str_replace(array('«', '»'), array('<u>', '</u>'), $temptext);
55		$pdf->writeHTML($temptext, true, false, true, false, '');
56	}
57
58	/**
59	 * Returns the height in points of the Footnote element
60	 *
61	 * @param ReportTcpdf $renderer
62	 *
63	 * @return float $h
64	 */
65	public function getFootnoteHeight($renderer) {
66		return 0;
67	}
68
69	/**
70	 * Splits the text into lines to fit into a giving cell
71	 * and returns the last lines width
72	 *
73	 * @param ReportTcpdf $pdf
74	 *
75	 * @return array
76	 */
77	public function getWidth($pdf) {
78		// Setup the style name, a font must be selected to calculate the width
79		$pdf->setCurrentStyle("footnotenum");
80
81		// Check for the largest font size in the box
82		$fsize = $pdf->getCurrentStyleHeight();
83		if ($fsize > $pdf->largestFontHeight) {
84			$pdf->largestFontHeight = $fsize;
85		}
86
87		// Returns the Object if already numbered else false
88		if (empty($this->num)) {
89			$pdf->checkFootnote($this);
90		}
91
92		// Get the line width
93		$lw = ceil($pdf->GetStringWidth($this->numText));
94		// Line Feed counter - Number of lines in the text
95		$lfct = substr_count($this->numText, "\n") + 1;
96		// If there is still remaining wrap width...
97		if ($this->wrapWidthRemaining > 0) {
98			// Check with line counter too!
99			// but floor the $wrapWidthRemaining first to keep it bugfree!
100			$wrapWidthRemaining = (int) ($this->wrapWidthRemaining);
101			if ($lw >= $wrapWidthRemaining || $lfct > 1) {
102				$newtext = '';
103				$lines   = explode("\n", $this->numText);
104				// Go throught the text line by line
105				foreach ($lines as $line) {
106					// Line width in points
107					$lw = ceil($pdf->GetStringWidth($line));
108					// If the line has to be wraped
109					if ($lw >= $wrapWidthRemaining) {
110						$words    = explode(" ", $line);
111						$addspace = count($words);
112						$lw       = 0;
113						foreach ($words as $word) {
114							$addspace--;
115							$lw += ceil($pdf->GetStringWidth($word . " "));
116							if ($lw < $wrapWidthRemaining) {
117								$newtext .= $word;
118								if ($addspace != 0) {
119									$newtext .= " ";
120								}
121							} else {
122								$lw = $pdf->GetStringWidth($word . " ");
123								$newtext .= "\n$word";
124								if ($addspace != 0) {
125									$newtext .= " ";
126								}
127								// Reset the wrap width to the cell width
128								$wrapWidthRemaining = $this->wrapWidthCell;
129							}
130						}
131					} else {
132						$newtext .= $line;
133					}
134					// Check the Line Feed counter
135					if ($lfct > 1) {
136						// Add a new line feed as long as it’s not the last line
137						$newtext .= "\n";
138						// Reset the line width
139						$lw = 0;
140						// Reset the wrap width to the cell width
141						$wrapWidthRemaining = $this->wrapWidthCell;
142					}
143					$lfct--;
144				}
145				$this->numText = $newtext;
146				$lfct          = substr_count($this->numText, "\n");
147
148				return array($lw, 1, $lfct);
149			}
150		}
151		$l    = 0;
152		$lfct = substr_count($this->numText, "\n");
153		if ($lfct > 0) {
154			$l = 2;
155		}
156
157		return array($lw, $l, $lfct);
158	}
159}
160