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