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