xref: /webtrees/app/Report/ReportPdfFootnote.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17*fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
21a25f0a04SGreg Roach
22a25f0a04SGreg Roach/**
23a25f0a04SGreg Roach * Class ReportPdfFootnote
24a25f0a04SGreg Roach */
25c1010edaSGreg Roachclass ReportPdfFootnote extends ReportBaseFootnote
26c1010edaSGreg Roach{
27a25f0a04SGreg Roach    /**
28a25f0a04SGreg Roach     * PDF Footnotes number renderer
29a25f0a04SGreg Roach     *
30adc8b18aSGreg Roach     * @param ReportTcpdf $renderer
31c7ff4153SGreg Roach     *
32c7ff4153SGreg Roach     * @return void
33a25f0a04SGreg Roach     */
34c1010edaSGreg Roach    public function render($renderer)
35c1010edaSGreg Roach    {
367a6ee1acSGreg Roach        $renderer->setCurrentStyle('footnotenum');
37a25f0a04SGreg Roach        $renderer->Write($renderer->getCurrentStyleHeight(), $this->numText, $this->addlink); //source link numbers after name
38a25f0a04SGreg Roach    }
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach    /**
41a25f0a04SGreg Roach     * Write the Footnote text
42a25f0a04SGreg Roach     * Uses style name "footnote" by default
43a25f0a04SGreg Roach     *
4467c69ce5SGreg Roach     * @param ReportTcpdf $renderer
4518d7a90dSGreg Roach     *
4618d7a90dSGreg Roach     * @return void
47a25f0a04SGreg Roach     */
4867c69ce5SGreg Roach    public function renderFootnote($renderer)
49c1010edaSGreg Roach    {
5067c69ce5SGreg Roach        if ($renderer->getCurrentStyle() != $this->styleName) {
5167c69ce5SGreg Roach            $renderer->setCurrentStyle($this->styleName);
52a25f0a04SGreg Roach        }
5367c69ce5SGreg Roach        $temptext = str_replace('#PAGENUM#', (string) $renderer->PageNo(), $this->text);
54a25f0a04SGreg Roach        // Set the link to this y/page position
5567c69ce5SGreg Roach        $renderer->SetLink($this->addlink, -1, -1);
56a25f0a04SGreg Roach        // Print first the source number
57a25f0a04SGreg Roach        // working
5867c69ce5SGreg Roach        if ($renderer->getRTL()) {
5967c69ce5SGreg Roach            $renderer->writeHTML('<span> .' . $this->num . '</span>', false, false, false, false, '');
60a25f0a04SGreg Roach        } else {
617a6ee1acSGreg Roach            $temptext = '<span>' . $this->num . '. </span>' . $temptext;
62a25f0a04SGreg Roach        }
63a25f0a04SGreg Roach        // underline «title» part of Source item
64c1010edaSGreg Roach        $temptext = str_replace([
65c1010edaSGreg Roach            '«',
66c1010edaSGreg Roach            '»',
67c1010edaSGreg Roach        ], [
68c1010edaSGreg Roach            '<u>',
69c1010edaSGreg Roach            '</u>',
70c1010edaSGreg Roach        ], $temptext);
7167c69ce5SGreg Roach        $renderer->writeHTML($temptext, true, false, true, false, '');
72a25f0a04SGreg Roach    }
73a25f0a04SGreg Roach
74a25f0a04SGreg Roach    /**
75a25f0a04SGreg Roach     * Returns the height in points of the Footnote element
76a25f0a04SGreg Roach     *
77adc8b18aSGreg Roach     * @param ReportTcpdf $renderer
78a25f0a04SGreg Roach     *
79a25f0a04SGreg Roach     * @return float $h
80a25f0a04SGreg Roach     */
818f53f488SRico Sonntag    public function getFootnoteHeight($renderer): float
82c1010edaSGreg Roach    {
83a25f0a04SGreg Roach        return 0;
84a25f0a04SGreg Roach    }
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach    /**
87a25f0a04SGreg Roach     * Splits the text into lines to fit into a giving cell
88a25f0a04SGreg Roach     * and returns the last lines width
89a25f0a04SGreg Roach     *
9067c69ce5SGreg Roach     * @param ReportTcpdf $renderer
91a25f0a04SGreg Roach     *
928ba2e626SGreg Roach     * @return float|array
93a25f0a04SGreg Roach     */
9467c69ce5SGreg Roach    public function getWidth($renderer)
95c1010edaSGreg Roach    {
96a25f0a04SGreg Roach        // Setup the style name, a font must be selected to calculate the width
9767c69ce5SGreg Roach        $renderer->setCurrentStyle('footnotenum');
98a25f0a04SGreg Roach
99a25f0a04SGreg Roach        // Check for the largest font size in the box
10067c69ce5SGreg Roach        $fsize = $renderer->getCurrentStyleHeight();
10167c69ce5SGreg Roach        if ($fsize > $renderer->largestFontHeight) {
10267c69ce5SGreg Roach            $renderer->largestFontHeight = $fsize;
103a25f0a04SGreg Roach        }
104a25f0a04SGreg Roach
105a25f0a04SGreg Roach        // Returns the Object if already numbered else false
106a25f0a04SGreg Roach        if (empty($this->num)) {
10767c69ce5SGreg Roach            $renderer->checkFootnote($this);
108a25f0a04SGreg Roach        }
109a25f0a04SGreg Roach
110a25f0a04SGreg Roach        // Get the line width
11167c69ce5SGreg Roach        $lw = ceil($renderer->GetStringWidth($this->numText));
112a25f0a04SGreg Roach        // Line Feed counter - Number of lines in the text
113a25f0a04SGreg Roach        $lfct = substr_count($this->numText, "\n") + 1;
114a25f0a04SGreg Roach        // If there is still remaining wrap width...
115a25f0a04SGreg Roach        if ($this->wrapWidthRemaining > 0) {
116a25f0a04SGreg Roach            // Check with line counter too!
117589feda3SGreg Roach            $wrapWidthRemaining = $this->wrapWidthRemaining;
118102a585eSGreg Roach            if ($lw >= $wrapWidthRemaining || $lfct > 1) {
119102a585eSGreg Roach                $newtext = '';
120a25f0a04SGreg Roach                $lines   = explode("\n", $this->numText);
121a25f0a04SGreg Roach                // Go throught the text line by line
122a25f0a04SGreg Roach                foreach ($lines as $line) {
123a25f0a04SGreg Roach                    // Line width in points
12467c69ce5SGreg Roach                    $lw = ceil($renderer->GetStringWidth($line));
125a25f0a04SGreg Roach                    // If the line has to be wraped
126a25f0a04SGreg Roach                    if ($lw >= $wrapWidthRemaining) {
1277a6ee1acSGreg Roach                        $words    = explode(' ', $line);
128a25f0a04SGreg Roach                        $addspace = count($words);
129a25f0a04SGreg Roach                        $lw       = 0;
130a25f0a04SGreg Roach                        foreach ($words as $word) {
131a25f0a04SGreg Roach                            $addspace--;
13267c69ce5SGreg Roach                            $lw += ceil($renderer->GetStringWidth($word . ' '));
133a25f0a04SGreg Roach                            if ($lw < $wrapWidthRemaining) {
134a25f0a04SGreg Roach                                $newtext .= $word;
135a25f0a04SGreg Roach                                if ($addspace != 0) {
1367a6ee1acSGreg Roach                                    $newtext .= ' ';
137a25f0a04SGreg Roach                                }
138a25f0a04SGreg Roach                            } else {
13967c69ce5SGreg Roach                                $lw = $renderer->GetStringWidth($word . ' ');
140a25f0a04SGreg Roach                                $newtext .= "\n$word";
141a25f0a04SGreg Roach                                if ($addspace != 0) {
1427a6ee1acSGreg Roach                                    $newtext .= ' ';
143a25f0a04SGreg Roach                                }
144a25f0a04SGreg Roach                                // Reset the wrap width to the cell width
145a25f0a04SGreg Roach                                $wrapWidthRemaining = $this->wrapWidthCell;
146a25f0a04SGreg Roach                            }
147a25f0a04SGreg Roach                        }
148a25f0a04SGreg Roach                    } else {
149a25f0a04SGreg Roach                        $newtext .= $line;
150a25f0a04SGreg Roach                    }
151a25f0a04SGreg Roach                    // Check the Line Feed counter
152a25f0a04SGreg Roach                    if ($lfct > 1) {
153a25f0a04SGreg Roach                        // Add a new line feed as long as it’s not the last line
154a25f0a04SGreg Roach                        $newtext .= "\n";
155a25f0a04SGreg Roach                        // Reset the line width
156a25f0a04SGreg Roach                        $lw = 0;
157a25f0a04SGreg Roach                        // Reset the wrap width to the cell width
158a25f0a04SGreg Roach                        $wrapWidthRemaining = $this->wrapWidthCell;
159a25f0a04SGreg Roach                    }
160a25f0a04SGreg Roach                    $lfct--;
161a25f0a04SGreg Roach                }
162a25f0a04SGreg Roach                $this->numText = $newtext;
163a25f0a04SGreg Roach                $lfct          = substr_count($this->numText, "\n");
164a25f0a04SGreg Roach
165c1010edaSGreg Roach                return [
166c1010edaSGreg Roach                    $lw,
167c1010edaSGreg Roach                    1,
168c1010edaSGreg Roach                    $lfct,
169c1010edaSGreg Roach                ];
170a25f0a04SGreg Roach            }
171a25f0a04SGreg Roach        }
172a25f0a04SGreg Roach        $l    = 0;
173a25f0a04SGreg Roach        $lfct = substr_count($this->numText, "\n");
174a25f0a04SGreg Roach        if ($lfct > 0) {
175a25f0a04SGreg Roach            $l = 2;
176a25f0a04SGreg Roach        }
177a25f0a04SGreg Roach
178c1010edaSGreg Roach        return [
179c1010edaSGreg Roach            $lw,
180c1010edaSGreg Roach            $l,
181c1010edaSGreg Roach            $lfct,
182c1010edaSGreg Roach        ];
183a25f0a04SGreg Roach    }
184a25f0a04SGreg Roach}
185