xref: /webtrees/app/Report/ReportHtmlFootnote.php (revision 589feda391b943b928aea0b4107591eb0e7bbf89)
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 */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roach/**
21a25f0a04SGreg Roach * class ReportHtmlFootnote
22a25f0a04SGreg Roach */
23c1010edaSGreg Roachclass ReportHtmlFootnote extends ReportBaseFootnote
24c1010edaSGreg Roach{
25a25f0a04SGreg Roach    /**
26a25f0a04SGreg Roach     * HTML Footnotes number renderer
27a25f0a04SGreg Roach     *
28a25f0a04SGreg Roach     * @param ReportHtml $renderer
29c7ff4153SGreg Roach     *
30c7ff4153SGreg Roach     * @return void
31a25f0a04SGreg Roach     */
32c1010edaSGreg Roach    public function render($renderer)
33c1010edaSGreg Roach    {
347a6ee1acSGreg Roach        $renderer->setCurrentStyle('footnotenum');
357a6ee1acSGreg Roach        echo '<a href="#footnote', $this->num, '"><sup>';
36a25f0a04SGreg Roach        $renderer->write($renderer->entityRTL . $this->num);
37a25f0a04SGreg Roach        echo "</sup></a>\n";
38a25f0a04SGreg Roach    }
39a25f0a04SGreg Roach
40a25f0a04SGreg Roach    /**
41a25f0a04SGreg Roach     * Write the Footnote text
42a25f0a04SGreg Roach     * Uses style name "footnote" by default
43a25f0a04SGreg Roach     *
44a25f0a04SGreg Roach     * @param ReportHtml $html
4518d7a90dSGreg Roach     *
4618d7a90dSGreg Roach     * @return void
47a25f0a04SGreg Roach     */
48c1010edaSGreg Roach    public function renderFootnote($html)
49c1010edaSGreg Roach    {
50a25f0a04SGreg Roach        if ($html->getCurrentStyle() != $this->styleName) {
51a25f0a04SGreg Roach            $html->setCurrentStyle($this->styleName);
52a25f0a04SGreg Roach        }
53a25f0a04SGreg Roach
547a6ee1acSGreg Roach        $temptext = str_replace('#PAGENUM#', $html->pageNo(), $this->text);
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);
637a6ee1acSGreg Roach        echo "\n<div><a name=\"footnote", $this->num, '"></a>';
647a6ee1acSGreg Roach        $html->write($this->num . '. ' . $temptext);
657a6ee1acSGreg Roach        echo '</div>';
66a25f0a04SGreg Roach
67a25f0a04SGreg Roach        $html->setXy(0, $html->getY() + $this->getFootnoteHeight($html));
68a25f0a04SGreg Roach    }
69a25f0a04SGreg Roach
70a25f0a04SGreg Roach    /**
71a25f0a04SGreg Roach     * Calculates the Footnotes height
72a25f0a04SGreg Roach     *
73a25f0a04SGreg Roach     * @param ReportHtml $html
74*589feda3SGreg Roach     * @param float      $cellWidth The width of the cell to use it for text wraping
75a25f0a04SGreg Roach     *
76*589feda3SGreg Roach     * @return float     Footnote height in points
77a25f0a04SGreg Roach     */
78*589feda3SGreg Roach    public function getFootnoteHeight($html, float $cellWidth = 0): float
79c1010edaSGreg Roach    {
80a25f0a04SGreg Roach        if ($html->getCurrentStyle() != $this->styleName) {
81a25f0a04SGreg Roach            $html->setCurrentStyle($this->styleName);
82a25f0a04SGreg Roach        }
83a25f0a04SGreg Roach
84a25f0a04SGreg Roach        if ($cellWidth > 0) {
85a25f0a04SGreg Roach            $this->text = $html->textWrap($this->text, $cellWidth);
86a25f0a04SGreg Roach        }
87a25f0a04SGreg Roach        $this->text = $this->text . "\n\n";
88a25f0a04SGreg Roach        $ct         = substr_count($this->text, "\n");
89a25f0a04SGreg Roach        $fsize      = $html->getCurrentStyleHeight();
90cbc1590aSGreg Roach
91a25f0a04SGreg Roach        return ($fsize * $ct) * $html->cellHeightRatio;
92a25f0a04SGreg Roach    }
93a25f0a04SGreg Roach
94a25f0a04SGreg Roach    /**
95a25f0a04SGreg Roach     * Get the width of text
96a25f0a04SGreg Roach     * Breaks up a text into lines if needed
97a25f0a04SGreg Roach     *
98a25f0a04SGreg Roach     * @param ReportHtml $html
99a25f0a04SGreg Roach     *
1008ba2e626SGreg Roach     * @return float|array
101a25f0a04SGreg Roach     */
1028ba2e626SGreg Roach    public function getWidth($html)
103c1010edaSGreg Roach    {
104a25f0a04SGreg Roach        // Setup the style name
1057a6ee1acSGreg Roach        $html->setCurrentStyle('footnotenum');
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach        // Check for the largest font size in the box
108a25f0a04SGreg Roach        $fsize = $html->getCurrentStyleHeight();
109a25f0a04SGreg Roach        if ($fsize > $html->largestFontHeight) {
110a25f0a04SGreg Roach            $html->largestFontHeight = $fsize;
111a25f0a04SGreg Roach        }
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach        // Returns the Object if already numbered else false
114a25f0a04SGreg Roach        if (empty($this->num)) {
115a25f0a04SGreg Roach            $html->checkFootnote($this);
116a25f0a04SGreg Roach        }
117a25f0a04SGreg Roach
118a25f0a04SGreg Roach        // Get the line width for the text in points + a little margin
1197820e4d7SGreg Roach        $lw = $html->getStringWidth($this->numText);
120a25f0a04SGreg Roach        // Line Feed counter - Number of lines in the text
121a25f0a04SGreg Roach        $lfct = $html->countLines($this->numText);
122a25f0a04SGreg Roach        // If there is still remaining wrap width...
123a25f0a04SGreg Roach        if ($this->wrapWidthRemaining > 0) {
124a25f0a04SGreg Roach            // Check with line counter too!
125102a585eSGreg Roach            if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
1267a6ee1acSGreg Roach                $newtext            = '';
127a25f0a04SGreg Roach                $wrapWidthRemaining = $this->wrapWidthRemaining;
128a25f0a04SGreg Roach                $lines              = explode("\n", $this->numText);
129a25f0a04SGreg Roach                // Go throught the text line by line
130a25f0a04SGreg Roach                foreach ($lines as $line) {
131a25f0a04SGreg Roach                    // Line width in points + a little margin
1327820e4d7SGreg Roach                    $lw = $html->getStringWidth($line);
133a25f0a04SGreg Roach                    // If the line has to be wraped
134a25f0a04SGreg Roach                    if ($lw > $wrapWidthRemaining) {
1357a6ee1acSGreg Roach                        $words    = explode(' ', $line);
136a25f0a04SGreg Roach                        $addspace = count($words);
137a25f0a04SGreg Roach                        $lw       = 0;
138a25f0a04SGreg Roach                        foreach ($words as $word) {
139a25f0a04SGreg Roach                            $addspace--;
1407a6ee1acSGreg Roach                            $lw += $html->getStringWidth($word . ' ');
141a25f0a04SGreg Roach                            if ($lw <= $wrapWidthRemaining) {
142a25f0a04SGreg Roach                                $newtext .= $word;
143a25f0a04SGreg Roach                                if ($addspace != 0) {
1447a6ee1acSGreg Roach                                    $newtext .= ' ';
145a25f0a04SGreg Roach                                }
146a25f0a04SGreg Roach                            } else {
1477a6ee1acSGreg Roach                                $lw = $html->getStringWidth($word . ' ');
148a25f0a04SGreg Roach                                $newtext .= "\n$word";
149a25f0a04SGreg Roach                                if ($addspace != 0) {
1507a6ee1acSGreg Roach                                    $newtext .= ' ';
151a25f0a04SGreg Roach                                }
152a25f0a04SGreg Roach                                // Reset the wrap width to the cell width
153a25f0a04SGreg Roach                                $wrapWidthRemaining = $this->wrapWidthCell;
154a25f0a04SGreg Roach                            }
155a25f0a04SGreg Roach                        }
156a25f0a04SGreg Roach                    } else {
157a25f0a04SGreg Roach                        $newtext .= $line;
158a25f0a04SGreg Roach                    }
159a25f0a04SGreg Roach                    // Check the Line Feed counter
160a25f0a04SGreg Roach                    if ($lfct > 1) {
161a25f0a04SGreg Roach                        // Add a new line feed as long as it’s not the last line
162a25f0a04SGreg Roach                        $newtext .= "\n";
163a25f0a04SGreg Roach                        // Reset the line width
164a25f0a04SGreg Roach                        $lw = 0;
165a25f0a04SGreg Roach                        // Reset the wrap width to the cell width
166a25f0a04SGreg Roach                        $wrapWidthRemaining = $this->wrapWidthCell;
167a25f0a04SGreg Roach                    }
168a25f0a04SGreg Roach                    $lfct--;
169a25f0a04SGreg Roach                }
170a25f0a04SGreg Roach                $this->numText = $newtext;
171a25f0a04SGreg Roach                $lfct          = substr_count($this->numText, "\n");
172cbc1590aSGreg Roach
173c1010edaSGreg Roach                return [
174c1010edaSGreg Roach                    $lw,
175c1010edaSGreg Roach                    1,
176c1010edaSGreg Roach                    $lfct,
177c1010edaSGreg Roach                ];
178a25f0a04SGreg Roach            }
179a25f0a04SGreg Roach        }
180a25f0a04SGreg Roach        $l    = 0;
181a25f0a04SGreg Roach        $lfct = substr_count($this->numText, "\n");
182a25f0a04SGreg Roach        if ($lfct > 0) {
183a25f0a04SGreg Roach            $l = 2;
184a25f0a04SGreg Roach        }
185cbc1590aSGreg Roach
186c1010edaSGreg Roach        return [
187c1010edaSGreg Roach            $lw,
188c1010edaSGreg Roach            $l,
189c1010edaSGreg Roach            $lfct,
190c1010edaSGreg Roach        ];
191a25f0a04SGreg Roach    }
192a25f0a04SGreg Roach}
193