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