xref: /webtrees/app/Report/ReportHtmlFootnote.php (revision d812eb6b3dd6c33a96d07cd964cb4c3b88cec447)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Report;
20
21/**
22 * Class ReportHtmlFootnote
23 */
24class ReportHtmlFootnote extends ReportBaseFootnote
25{
26    /**
27     * HTML Footnotes number renderer
28     *
29     * @param ReportHtml $renderer
30     *
31     * @return void
32     */
33    public function render($renderer)
34    {
35        $renderer->setCurrentStyle('footnotenum');
36        echo '<a href="#footnote', $this->num, '"><sup>';
37        $renderer->write($renderer->entityRTL . $this->num);
38        echo "</sup></a>\n";
39    }
40
41    /**
42     * Write the Footnote text
43     * Uses style name "footnote" by default
44     *
45     * @param ReportHtml $renderer
46     *
47     * @return void
48     */
49    public function renderFootnote($renderer)
50    {
51        if ($renderer->getCurrentStyle() != $this->styleName) {
52            $renderer->setCurrentStyle($this->styleName);
53        }
54
55        $temptext = str_replace('#PAGENUM#', (string) $renderer->pageNo(), $this->text);
56        // underline «title» part of Source item
57        $temptext = str_replace([
58            '«',
59            '»',
60        ], [
61            '<u>',
62            '</u>',
63        ], $temptext);
64        echo "\n<div><a name=\"footnote", $this->num, '"></a>';
65        $renderer->write($this->num . '. ' . $temptext);
66        echo '</div>';
67
68        $renderer->setXy(0, $renderer->getY() + $this->getFootnoteHeight($renderer));
69    }
70
71    /**
72     * Calculates the Footnotes height
73     *
74     * @param ReportHtml $html
75     * @param float      $cellWidth The width of the cell to use it for text wraping
76     *
77     * @return float     Footnote height in points
78     */
79    public function getFootnoteHeight($html, float $cellWidth = 0): float
80    {
81        if ($html->getCurrentStyle() != $this->styleName) {
82            $html->setCurrentStyle($this->styleName);
83        }
84
85        if ($cellWidth > 0) {
86            $this->text = $html->textWrap($this->text, $cellWidth);
87        }
88
89        $this->text .= "\n\n";
90        $ct         = substr_count($this->text, "\n");
91        $fsize      = $html->getCurrentStyleHeight();
92
93        return ($fsize * $ct) * $html->cellHeightRatio;
94    }
95
96    /**
97     * Get the width of text
98     * Breaks up a text into lines if needed
99     *
100     * @param ReportHtml $renderer
101     *
102     * @return float|array
103     */
104    public function getWidth($renderer)
105    {
106        // Setup the style name
107        $renderer->setCurrentStyle('footnotenum');
108
109        // Check for the largest font size in the box
110        $fsize = $renderer->getCurrentStyleHeight();
111        if ($fsize > $renderer->largestFontHeight) {
112            $renderer->largestFontHeight = $fsize;
113        }
114
115        // Returns the Object if already numbered else false
116        if (empty($this->num)) {
117            $renderer->checkFootnote($this);
118        }
119
120        // Get the line width for the text in points + a little margin
121        $lw = $renderer->getStringWidth($this->numText);
122        // Line Feed counter - Number of lines in the text
123        $lfct = $renderer->countLines($this->numText);
124        // If there is still remaining wrap width...
125        if ($this->wrapWidthRemaining > 0) {
126            // Check with line counter too!
127            if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
128                $newtext            = '';
129                $wrapWidthRemaining = $this->wrapWidthRemaining;
130                $lines              = explode("\n", $this->numText);
131                // Go throught the text line by line
132                foreach ($lines as $line) {
133                    // Line width in points + a little margin
134                    $lw = $renderer->getStringWidth($line);
135                    // If the line has to be wraped
136                    if ($lw > $wrapWidthRemaining) {
137                        $words    = explode(' ', $line);
138                        $addspace = count($words);
139                        $lw       = 0;
140                        foreach ($words as $word) {
141                            $addspace--;
142                            $lw += $renderer->getStringWidth($word . ' ');
143                            if ($lw <= $wrapWidthRemaining) {
144                                $newtext .= $word;
145                                if ($addspace != 0) {
146                                    $newtext .= ' ';
147                                }
148                            } else {
149                                $lw = $renderer->getStringWidth($word . ' ');
150                                $newtext .= "\n$word";
151                                if ($addspace != 0) {
152                                    $newtext .= ' ';
153                                }
154                                // Reset the wrap width to the cell width
155                                $wrapWidthRemaining = $this->wrapWidthCell;
156                            }
157                        }
158                    } else {
159                        $newtext .= $line;
160                    }
161                    // Check the Line Feed counter
162                    if ($lfct > 1) {
163                        // Add a new line feed as long as it’s not the last line
164                        $newtext .= "\n";
165                        // Reset the line width
166                        $lw = 0;
167                        // Reset the wrap width to the cell width
168                        $wrapWidthRemaining = $this->wrapWidthCell;
169                    }
170                    $lfct--;
171                }
172                $this->numText = $newtext;
173                $lfct          = substr_count($this->numText, "\n");
174
175                return [
176                    $lw,
177                    1,
178                    $lfct,
179                ];
180            }
181        }
182        $l    = 0;
183        $lfct = substr_count($this->numText, "\n");
184        if ($lfct > 0) {
185            $l = 2;
186        }
187
188        return [
189            $lw,
190            $l,
191            $lfct,
192        ];
193    }
194}
195