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