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