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