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