xref: /webtrees/app/Report/ReportHtmlTextbox.php (revision 52f124b09c3bc79f2e314c64c3ad971d9a17c818)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
21a25f0a04SGreg Roach
22b6f35a76SGreg Roachuse function abs;
23b6f35a76SGreg Roachuse function count;
24b6f35a76SGreg Roachuse function is_object;
25b6f35a76SGreg Roachuse function ksort;
26b6f35a76SGreg Roachuse function str_replace;
27b6f35a76SGreg Roachuse function trim;
28b6f35a76SGreg Roach
29a25f0a04SGreg Roach/**
30a25f0a04SGreg Roach * Class ReportHtmlTextbox
31a25f0a04SGreg Roach */
32c1010edaSGreg Roachclass ReportHtmlTextbox extends ReportBaseTextbox
33c1010edaSGreg Roach{
34a25f0a04SGreg Roach    /**
3576692c8bSGreg Roach     * Render the elements.
3676692c8bSGreg Roach     *
37b6f35a76SGreg Roach     * @param HtmlRenderer $renderer
38c7ff4153SGreg Roach     *
39c7ff4153SGreg Roach     * @return void
40a25f0a04SGreg Roach     */
4177bab461SGreg Roach    public function render($renderer): void
42c1010edaSGreg Roach    {
43a25f0a04SGreg Roach        // checkFootnote
4413abd6f3SGreg Roach        $newelements      = [];
4513abd6f3SGreg Roach        $lastelement      = [];
4613abd6f3SGreg Roach        $footnote_element = [];
47a25f0a04SGreg Roach        // Element counter
48a25f0a04SGreg Roach        $cE = count($this->elements);
49a25f0a04SGreg Roach        //-- collapse duplicate elements
50a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
51a25f0a04SGreg Roach            $element = $this->elements[$i];
52044416d2SGreg Roach            if ($element instanceof ReportBaseElement) {
53a25f0a04SGreg Roach                if ($element instanceof ReportBaseText) {
54a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
55a25f0a04SGreg Roach                        ksort($footnote_element);
56a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
57a25f0a04SGreg Roach                            $newelements[] = $links;
58a25f0a04SGreg Roach                        }
5913abd6f3SGreg Roach                        $footnote_element = [];
60a25f0a04SGreg Roach                    }
61a25f0a04SGreg Roach                    if (empty($lastelement)) {
62a25f0a04SGreg Roach                        $lastelement = $element;
636dcdd572SGreg Roach                    } elseif ($element->getStyleName() === $lastelement->getStyleName()) {
64a25f0a04SGreg Roach                        // Checking if the Text has the same style
657a6ee1acSGreg Roach                        $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
666dcdd572SGreg Roach                    } else {
67a25f0a04SGreg Roach                        $newelements[] = $lastelement;
68a25f0a04SGreg Roach                        $lastelement   = $element;
69a25f0a04SGreg Roach                    }
7059597b37SGreg Roach                } elseif ($element instanceof ReportHtmlFootnote) {
71a25f0a04SGreg Roach                    // Check if the Footnote has been set with it’s link number
72a25f0a04SGreg Roach                    $renderer->checkFootnote($element);
73a25f0a04SGreg Roach                    // Save first the last element if any
74a25f0a04SGreg Roach                    if (!empty($lastelement)) {
75a25f0a04SGreg Roach                        $newelements[] = $lastelement;
7613abd6f3SGreg Roach                        $lastelement   = [];
77a25f0a04SGreg Roach                    }
78a25f0a04SGreg Roach                    // Save the Footnote with it’s link number as key for sorting later
79a25f0a04SGreg Roach                    $footnote_element[$element->num] = $element;
8059597b37SGreg Roach                } elseif (trim($element->getValue()) !== '') {
81ce15a17aSGreg Roach                    // Do not keep empty footnotes
82a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
83a25f0a04SGreg Roach                        ksort($footnote_element);
84a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
85a25f0a04SGreg Roach                            $newelements[] = $links;
86a25f0a04SGreg Roach                        }
8713abd6f3SGreg Roach                        $footnote_element = [];
88a25f0a04SGreg Roach                    }
89a25f0a04SGreg Roach                    if (!empty($lastelement)) {
90a25f0a04SGreg Roach                        $newelements[] = $lastelement;
9113abd6f3SGreg Roach                        $lastelement   = [];
92a25f0a04SGreg Roach                    }
93a25f0a04SGreg Roach                    $newelements[] = $element;
94a25f0a04SGreg Roach                }
95a25f0a04SGreg Roach            } else {
96a25f0a04SGreg Roach                if (!empty($lastelement)) {
97a25f0a04SGreg Roach                    $newelements[] = $lastelement;
9813abd6f3SGreg Roach                    $lastelement   = [];
99a25f0a04SGreg Roach                }
100a25f0a04SGreg Roach                if (!empty($footnote_element)) {
101a25f0a04SGreg Roach                    ksort($footnote_element);
102a25f0a04SGreg Roach                    foreach ($footnote_element as $links) {
103a25f0a04SGreg Roach                        $newelements[] = $links;
104a25f0a04SGreg Roach                    }
10513abd6f3SGreg Roach                    $footnote_element = [];
106a25f0a04SGreg Roach                }
107a25f0a04SGreg Roach                $newelements[] = $element;
108a25f0a04SGreg Roach            }
109a25f0a04SGreg Roach        }
110a25f0a04SGreg Roach        if (!empty($lastelement)) {
111a25f0a04SGreg Roach            $newelements[] = $lastelement;
112a25f0a04SGreg Roach        }
113a25f0a04SGreg Roach        if (!empty($footnote_element)) {
114a25f0a04SGreg Roach            ksort($footnote_element);
115a25f0a04SGreg Roach            foreach ($footnote_element as $links) {
116a25f0a04SGreg Roach                $newelements[] = $links;
117a25f0a04SGreg Roach            }
118a25f0a04SGreg Roach        }
119a25f0a04SGreg Roach        $this->elements = $newelements;
1208a4ee39cSGreg Roach        unset($footnote_element, $lastelement, $newelements);
121a25f0a04SGreg Roach
122a25f0a04SGreg Roach        $cP = 0; // Class Padding
123a25f0a04SGreg Roach
124a25f0a04SGreg Roach        // Used with line breaks and cell height calculation within this box only
125a25f0a04SGreg Roach        $renderer->largestFontHeight = 0;
126a25f0a04SGreg Roach
12767c69ce5SGreg Roach        // If current position (left)
128c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
1297820e4d7SGreg Roach            $cX = $renderer->getX();
130a25f0a04SGreg Roach        } else {
131a25f0a04SGreg Roach            $cX = $this->left;
1327820e4d7SGreg Roach            $renderer->setX($cX);
133a25f0a04SGreg Roach        }
13467c69ce5SGreg Roach        // If current position (top)
135c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
1363844edd4SGreg Roach            $this->top = $renderer->getY();
137a25f0a04SGreg Roach        } else {
1387820e4d7SGreg Roach            $renderer->setY($this->top);
139a25f0a04SGreg Roach        }
140a25f0a04SGreg Roach
14167c69ce5SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page width (margin)
14277bab461SGreg Roach        if ($this->width === 0.0 || $this->width > $renderer->getRemainingWidth()) {
143a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidth();
144a25f0a04SGreg Roach        }
145a25f0a04SGreg Roach        // Setup the CellPadding
146a25f0a04SGreg Roach        if ($this->padding) {
147a25f0a04SGreg Roach            $cP = $renderer->cPadding;
148a25f0a04SGreg Roach        }
149a25f0a04SGreg Roach
150a25f0a04SGreg Roach        // For padding, we have to use less wrap width
15177bab461SGreg Roach        $cW = $this->width - $cP * 2.0;
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach        //-- calculate the text box height
154a25f0a04SGreg Roach        // Number of lines, will be converted to height
155a25f0a04SGreg Roach        $cHT = 0;
15677bab461SGreg Roach        // Element height (except text)
15777bab461SGreg Roach        $eH = 0.0;
158a25f0a04SGreg Roach        // Footnote height (in points)
159a25f0a04SGreg Roach        $fH = 0;
160a25f0a04SGreg Roach        $w  = 0;
161a25f0a04SGreg Roach        //-- $lw is an array
162a25f0a04SGreg Roach        // 0 => last line width
163a25f0a04SGreg Roach        // 1 => 1 if text was wrapped, 0 if text did not wrap
164a25f0a04SGreg Roach        // 2 => number of LF
16513abd6f3SGreg Roach        $lw = [];
166a25f0a04SGreg Roach        // Element counter
167a25f0a04SGreg Roach        $cE = count($this->elements);
168a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
169a25f0a04SGreg Roach            if (is_object($this->elements[$i])) {
170a25f0a04SGreg Roach                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
17177bab461SGreg Roach                if ($ew === $cW) {
172a25f0a04SGreg Roach                    $w = 0;
173a25f0a04SGreg Roach                }
174a25f0a04SGreg Roach                $lw = $this->elements[$i]->getWidth($renderer);
175a25f0a04SGreg Roach                // Text is already gets the # LF
176a25f0a04SGreg Roach                $cHT += $lw[2];
17741cfb9e2SGreg Roach                if ($lw[1] === 1) {
178a25f0a04SGreg Roach                    $w = $lw[0];
17941cfb9e2SGreg Roach                } elseif ($lw[1] === 2) {
180a25f0a04SGreg Roach                    $w = 0;
181a25f0a04SGreg Roach                } else {
182a25f0a04SGreg Roach                    $w += $lw[0];
183a25f0a04SGreg Roach                }
184a25f0a04SGreg Roach                if ($w > $cW) {
185a25f0a04SGreg Roach                    $w = $lw[0];
186a25f0a04SGreg Roach                }
187a25f0a04SGreg Roach                // For anything else but text (images), get the height
188a25f0a04SGreg Roach                $eH += $this->elements[$i]->getHeight($renderer);
189a25f0a04SGreg Roach            } else {
190a25f0a04SGreg Roach                $fH += abs($renderer->getFootnotesHeight($cW));
191a25f0a04SGreg Roach            }
192a25f0a04SGreg Roach        }
19367c69ce5SGreg Roach
194a25f0a04SGreg Roach        // Add up what’s the final height
195a25f0a04SGreg Roach        $cH = $this->height;
196a25f0a04SGreg Roach        // If any element exist
197a25f0a04SGreg Roach        if ($cE > 0) {
198a25f0a04SGreg Roach            // Check if this is text or some other element, like images
19977bab461SGreg Roach            if ($eH === 0.0) {
200a25f0a04SGreg Roach                // Number of LF but at least one line
201a25f0a04SGreg Roach                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
202*52f124b0SAlejandro Criado-Pérez                // Calculate the cell height with the largest font size used
203e364afe4SGreg Roach                $cHT *= $renderer->largestFontHeight;
204a25f0a04SGreg Roach                if ($cH < $cHT) {
205a25f0a04SGreg Roach                    $cH = $cHT;
206a25f0a04SGreg Roach                }
207ce15a17aSGreg Roach            } else {
208ce15a17aSGreg Roach                // This is any other element
209a25f0a04SGreg Roach                if ($cH < $eH) {
210a25f0a04SGreg Roach                    $cH = $eH;
211a25f0a04SGreg Roach                }
212a25f0a04SGreg Roach                // Add Footnote height to the rest of the height
213a25f0a04SGreg Roach                $cH += $fH;
214a25f0a04SGreg Roach            }
215a25f0a04SGreg Roach        }
216a25f0a04SGreg Roach
217a25f0a04SGreg Roach        unset($lw, $cHT, $fH, $w);
218a25f0a04SGreg Roach
219*52f124b0SAlejandro Criado-Pérez        // Finally, check the last cells height
220a25f0a04SGreg Roach        if ($cH < $renderer->lastCellHeight) {
221a25f0a04SGreg Roach            $cH = $renderer->lastCellHeight;
222a25f0a04SGreg Roach        }
223a25f0a04SGreg Roach        // Update max Y in case of a pagebreak
224a25f0a04SGreg Roach        // We don't want to over write any images or other stuff
225a25f0a04SGreg Roach        $renderer->addMaxY($this->top + $cH);
226a25f0a04SGreg Roach
227a25f0a04SGreg Roach        // Start to print HTML
2287a6ee1acSGreg Roach        echo '<div style="position:absolute;top:', $this->top, 'pt;';
229a25f0a04SGreg Roach        // LTR (left) or RTL (right)
2307a6ee1acSGreg Roach        echo $renderer->alignRTL, ':', $cX, 'pt;';
231a25f0a04SGreg Roach        // Background color
232b6f35a76SGreg Roach        if ($this->fill && $this->bgcolor !== '') {
2337a6ee1acSGreg Roach            echo ' background-color:', $this->bgcolor, ';';
234a25f0a04SGreg Roach        }
235a25f0a04SGreg Roach        // Print padding only when it’s set
236a25f0a04SGreg Roach        if ($this->padding) {
237a25f0a04SGreg Roach            // Use Cell around padding to support RTL also
2387a6ee1acSGreg Roach            echo 'padding:', $cP, 'pt;';
239a25f0a04SGreg Roach        }
240a25f0a04SGreg Roach        // Border setup
241a25f0a04SGreg Roach        if ($this->border) {
2427a6ee1acSGreg Roach            echo ' border:solid black 1pt;';
24352135727SGreg Roach            echo 'width:', $this->width - 1 - $cP * 2, 'pt;height:', $cH - 1, 'pt;';
244a25f0a04SGreg Roach        } else {
24552135727SGreg Roach            echo 'width:', $this->width - $cP * 2, 'pt;height:', $cH, 'pt;';
246a25f0a04SGreg Roach        }
2477a6ee1acSGreg Roach        echo '">';
248a25f0a04SGreg Roach
249a25f0a04SGreg Roach        // Do a little "margin" trick before print
250a25f0a04SGreg Roach        // to get the correct current position => "."
2517820e4d7SGreg Roach        $cXT = $renderer->getX();
2527820e4d7SGreg Roach        $cYT = $renderer->getY();
2537820e4d7SGreg Roach        $renderer->setXy(0, 0);
254a25f0a04SGreg Roach
255a25f0a04SGreg Roach        // Print the text elements
256a25f0a04SGreg Roach        foreach ($this->elements as $element) {
257d9a53aa7SGreg Roach            if ($element instanceof ReportHtmlText) {
258d9a53aa7SGreg Roach                $element->render($renderer, false);
259d9a53aa7SGreg Roach            } elseif ($element instanceof ReportBaseElement) {
260d9a53aa7SGreg Roach                $element->render($renderer);
261044416d2SGreg Roach            } elseif ($element === 'footnotetexts') {
2627820e4d7SGreg Roach                $renderer->footnotes();
263044416d2SGreg Roach            } elseif ($element === 'addpage') {
2647820e4d7SGreg Roach                $renderer->addPage();
265a25f0a04SGreg Roach            }
266a25f0a04SGreg Roach        }
267a25f0a04SGreg Roach        echo "</div>\n";
268a25f0a04SGreg Roach
269a25f0a04SGreg Roach        // Reset "margins"
2707820e4d7SGreg Roach        $renderer->setXy($cXT, $cYT);
271a25f0a04SGreg Roach        // This will be mostly used to trick the multiple images last height
272a25f0a04SGreg Roach        if ($this->reseth) {
273a25f0a04SGreg Roach            $cH = 0;
274a25f0a04SGreg Roach        }
275a25f0a04SGreg Roach        // New line and some clean up
276a25f0a04SGreg Roach        if (!$this->newline) {
2777820e4d7SGreg Roach            $renderer->setXy($cX + $this->width, $this->top);
278a25f0a04SGreg Roach            $renderer->lastCellHeight = $cH;
279a25f0a04SGreg Roach        } else {
28052135727SGreg Roach            $renderer->setXy(0, $this->top + $cH + $cP * 2);
281a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
282a25f0a04SGreg Roach        }
283a25f0a04SGreg Roach    }
284a25f0a04SGreg Roach}
285