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