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