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