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