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