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