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