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