xref: /webtrees/app/Report/ReportHtmlFootnote.php (revision 8ba2e6268c47e6eaf578c571d83b4eab25c35320)
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 ReportHtmlFootnote
20a25f0a04SGreg Roach */
21c1010edaSGreg Roachclass ReportHtmlFootnote extends ReportBaseFootnote
22c1010edaSGreg Roach{
23a25f0a04SGreg Roach    /**
24a25f0a04SGreg Roach     * HTML Footnotes number renderer
25a25f0a04SGreg Roach     *
26a25f0a04SGreg Roach     * @param ReportHtml $renderer
27a25f0a04SGreg Roach     */
28c1010edaSGreg Roach    public function render($renderer)
29c1010edaSGreg Roach    {
307a6ee1acSGreg Roach        $renderer->setCurrentStyle('footnotenum');
317a6ee1acSGreg Roach        echo '<a href="#footnote', $this->num, '"><sup>';
32a25f0a04SGreg Roach        $renderer->write($renderer->entityRTL . $this->num);
33a25f0a04SGreg Roach        echo "</sup></a>\n";
34a25f0a04SGreg Roach    }
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach    /**
37a25f0a04SGreg Roach     * Write the Footnote text
38a25f0a04SGreg Roach     * Uses style name "footnote" by default
39a25f0a04SGreg Roach     *
40a25f0a04SGreg Roach     * @param ReportHtml $html
41a25f0a04SGreg Roach     */
42c1010edaSGreg Roach    public function renderFootnote($html)
43c1010edaSGreg Roach    {
44a25f0a04SGreg Roach        if ($html->getCurrentStyle() != $this->styleName) {
45a25f0a04SGreg Roach            $html->setCurrentStyle($this->styleName);
46a25f0a04SGreg Roach        }
47a25f0a04SGreg Roach
487a6ee1acSGreg Roach        $temptext = str_replace('#PAGENUM#', $html->pageNo(), $this->text);
49a25f0a04SGreg Roach        // underline «title» part of Source item
50c1010edaSGreg Roach        $temptext = str_replace([
51c1010edaSGreg Roach            '«',
52c1010edaSGreg Roach            '»',
53c1010edaSGreg Roach        ], [
54c1010edaSGreg Roach            '<u>',
55c1010edaSGreg Roach            '</u>',
56c1010edaSGreg Roach        ], $temptext);
577a6ee1acSGreg Roach        echo "\n<div><a name=\"footnote", $this->num, '"></a>';
587a6ee1acSGreg Roach        $html->write($this->num . '. ' . $temptext);
597a6ee1acSGreg Roach        echo '</div>';
60a25f0a04SGreg Roach
61a25f0a04SGreg Roach        $html->setXy(0, $html->getY() + $this->getFootnoteHeight($html));
62a25f0a04SGreg Roach    }
63a25f0a04SGreg Roach
64a25f0a04SGreg Roach    /**
65a25f0a04SGreg Roach     * Calculates the Footnotes height
66a25f0a04SGreg Roach     *
67a25f0a04SGreg Roach     * @param ReportHtml $html
68cbc1590aSGreg Roach     * @param int        $cellWidth The width of the cell to use it for text wraping
69a25f0a04SGreg Roach     *
70cbc1590aSGreg Roach     * @return int       Footnote height in points
71a25f0a04SGreg Roach     */
728f53f488SRico Sonntag    public function getFootnoteHeight($html, $cellWidth = 0): int
73c1010edaSGreg Roach    {
74a25f0a04SGreg Roach        if ($html->getCurrentStyle() != $this->styleName) {
75a25f0a04SGreg Roach            $html->setCurrentStyle($this->styleName);
76a25f0a04SGreg Roach        }
77a25f0a04SGreg Roach
78a25f0a04SGreg Roach        if ($cellWidth > 0) {
79a25f0a04SGreg Roach            $this->text = $html->textWrap($this->text, $cellWidth);
80a25f0a04SGreg Roach        }
81a25f0a04SGreg Roach        $this->text = $this->text . "\n\n";
82a25f0a04SGreg Roach        $ct         = substr_count($this->text, "\n");
83a25f0a04SGreg Roach        $fsize      = $html->getCurrentStyleHeight();
84cbc1590aSGreg Roach
85a25f0a04SGreg Roach        return ($fsize * $ct) * $html->cellHeightRatio;
86a25f0a04SGreg Roach    }
87a25f0a04SGreg Roach
88a25f0a04SGreg Roach    /**
89a25f0a04SGreg Roach     * Get the width of text
90a25f0a04SGreg Roach     * Breaks up a text into lines if needed
91a25f0a04SGreg Roach     *
92a25f0a04SGreg Roach     * @param ReportHtml $html
93a25f0a04SGreg Roach     *
94*8ba2e626SGreg Roach     * @return float|array
95a25f0a04SGreg Roach     */
96*8ba2e626SGreg Roach    public function getWidth($html)
97c1010edaSGreg Roach    {
98a25f0a04SGreg Roach        // Setup the style name
997a6ee1acSGreg Roach        $html->setCurrentStyle('footnotenum');
100a25f0a04SGreg Roach
101a25f0a04SGreg Roach        // Check for the largest font size in the box
102a25f0a04SGreg Roach        $fsize = $html->getCurrentStyleHeight();
103a25f0a04SGreg Roach        if ($fsize > $html->largestFontHeight) {
104a25f0a04SGreg Roach            $html->largestFontHeight = $fsize;
105a25f0a04SGreg Roach        }
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach        // Returns the Object if already numbered else false
108a25f0a04SGreg Roach        if (empty($this->num)) {
109a25f0a04SGreg Roach            $html->checkFootnote($this);
110a25f0a04SGreg Roach        }
111a25f0a04SGreg Roach
112a25f0a04SGreg Roach        // Get the line width for the text in points + a little margin
1137820e4d7SGreg Roach        $lw = $html->getStringWidth($this->numText);
114a25f0a04SGreg Roach        // Line Feed counter - Number of lines in the text
115a25f0a04SGreg Roach        $lfct = $html->countLines($this->numText);
116a25f0a04SGreg Roach        // If there is still remaining wrap width...
117a25f0a04SGreg Roach        if ($this->wrapWidthRemaining > 0) {
118a25f0a04SGreg Roach            // Check with line counter too!
119102a585eSGreg Roach            if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
1207a6ee1acSGreg Roach                $newtext            = '';
121a25f0a04SGreg Roach                $wrapWidthRemaining = $this->wrapWidthRemaining;
122a25f0a04SGreg Roach                $lines              = explode("\n", $this->numText);
123a25f0a04SGreg Roach                // Go throught the text line by line
124a25f0a04SGreg Roach                foreach ($lines as $line) {
125a25f0a04SGreg Roach                    // Line width in points + a little margin
1267820e4d7SGreg Roach                    $lw = $html->getStringWidth($line);
127a25f0a04SGreg Roach                    // If the line has to be wraped
128a25f0a04SGreg Roach                    if ($lw > $wrapWidthRemaining) {
1297a6ee1acSGreg Roach                        $words    = explode(' ', $line);
130a25f0a04SGreg Roach                        $addspace = count($words);
131a25f0a04SGreg Roach                        $lw       = 0;
132a25f0a04SGreg Roach                        foreach ($words as $word) {
133a25f0a04SGreg Roach                            $addspace--;
1347a6ee1acSGreg Roach                            $lw += $html->getStringWidth($word . ' ');
135a25f0a04SGreg Roach                            if ($lw <= $wrapWidthRemaining) {
136a25f0a04SGreg Roach                                $newtext .= $word;
137a25f0a04SGreg Roach                                if ($addspace != 0) {
1387a6ee1acSGreg Roach                                    $newtext .= ' ';
139a25f0a04SGreg Roach                                }
140a25f0a04SGreg Roach                            } else {
1417a6ee1acSGreg Roach                                $lw      = $html->getStringWidth($word . ' ');
142a25f0a04SGreg Roach                                $newtext .= "\n$word";
143a25f0a04SGreg Roach                                if ($addspace != 0) {
1447a6ee1acSGreg Roach                                    $newtext .= ' ';
145a25f0a04SGreg Roach                                }
146a25f0a04SGreg Roach                                // Reset the wrap width to the cell width
147a25f0a04SGreg Roach                                $wrapWidthRemaining = $this->wrapWidthCell;
148a25f0a04SGreg Roach                            }
149a25f0a04SGreg Roach                        }
150a25f0a04SGreg Roach                    } else {
151a25f0a04SGreg Roach                        $newtext .= $line;
152a25f0a04SGreg Roach                    }
153a25f0a04SGreg Roach                    // Check the Line Feed counter
154a25f0a04SGreg Roach                    if ($lfct > 1) {
155a25f0a04SGreg Roach                        // Add a new line feed as long as it’s not the last line
156a25f0a04SGreg Roach                        $newtext .= "\n";
157a25f0a04SGreg Roach                        // Reset the line width
158a25f0a04SGreg Roach                        $lw = 0;
159a25f0a04SGreg Roach                        // Reset the wrap width to the cell width
160a25f0a04SGreg Roach                        $wrapWidthRemaining = $this->wrapWidthCell;
161a25f0a04SGreg Roach                    }
162a25f0a04SGreg Roach                    $lfct--;
163a25f0a04SGreg Roach                }
164a25f0a04SGreg Roach                $this->numText = $newtext;
165a25f0a04SGreg Roach                $lfct          = substr_count($this->numText, "\n");
166cbc1590aSGreg Roach
167c1010edaSGreg Roach                return [
168c1010edaSGreg Roach                    $lw,
169c1010edaSGreg Roach                    1,
170c1010edaSGreg Roach                    $lfct,
171c1010edaSGreg Roach                ];
172a25f0a04SGreg Roach            }
173a25f0a04SGreg Roach        }
174a25f0a04SGreg Roach        $l    = 0;
175a25f0a04SGreg Roach        $lfct = substr_count($this->numText, "\n");
176a25f0a04SGreg Roach        if ($lfct > 0) {
177a25f0a04SGreg Roach            $l = 2;
178a25f0a04SGreg Roach        }
179cbc1590aSGreg Roach
180c1010edaSGreg Roach        return [
181c1010edaSGreg Roach            $lw,
182c1010edaSGreg Roach            $l,
183c1010edaSGreg Roach            $lfct,
184c1010edaSGreg Roach        ];
185a25f0a04SGreg Roach    }
186a25f0a04SGreg Roach}
187