xref: /webtrees/app/Report/ReportHtmlTextbox.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1a25f0a04SGreg Roach<?php
2*3976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
20a25f0a04SGreg Roach
21a25f0a04SGreg Roach/**
22a25f0a04SGreg Roach * Class ReportHtmlTextbox
23a25f0a04SGreg Roach */
24c1010edaSGreg Roachclass ReportHtmlTextbox extends ReportBaseTextbox
25c1010edaSGreg Roach{
26a25f0a04SGreg Roach    /**
2776692c8bSGreg Roach     * Render the elements.
2876692c8bSGreg Roach     *
29a25f0a04SGreg Roach     * @param ReportHtml $renderer
30c7ff4153SGreg Roach     *
31c7ff4153SGreg Roach     * @return void
32a25f0a04SGreg Roach     */
33c1010edaSGreg Roach    public function render($renderer)
34c1010edaSGreg Roach    {
35a25f0a04SGreg Roach        // checkFootnote
3613abd6f3SGreg Roach        $newelements      = [];
3713abd6f3SGreg Roach        $lastelement      = [];
3813abd6f3SGreg Roach        $footnote_element = [];
39a25f0a04SGreg Roach        // Element counter
40a25f0a04SGreg Roach        $cE = count($this->elements);
41a25f0a04SGreg Roach        //-- collapse duplicate elements
42a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
43a25f0a04SGreg Roach            $element = $this->elements[$i];
44044416d2SGreg Roach            if ($element instanceof ReportBaseElement) {
45a25f0a04SGreg Roach                if ($element instanceof ReportBaseText) {
46a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
47a25f0a04SGreg Roach                        ksort($footnote_element);
48a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
49a25f0a04SGreg Roach                            $newelements[] = $links;
50a25f0a04SGreg Roach                        }
5113abd6f3SGreg Roach                        $footnote_element = [];
52a25f0a04SGreg Roach                    }
53a25f0a04SGreg Roach                    if (empty($lastelement)) {
54a25f0a04SGreg Roach                        $lastelement = $element;
55a25f0a04SGreg Roach                    } else {
56a25f0a04SGreg Roach                        // Checking if the Text has the same style
57a25f0a04SGreg Roach                        if ($element->getStyleName() == $lastelement->getStyleName()) {
587a6ee1acSGreg Roach                            $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
59a25f0a04SGreg Roach                        } elseif (!empty($lastelement)) {
60a25f0a04SGreg Roach                            $newelements[] = $lastelement;
61a25f0a04SGreg Roach                            $lastelement   = $element;
62a25f0a04SGreg Roach                        }
63a25f0a04SGreg Roach                    }
64ce15a17aSGreg Roach                } elseif ($element instanceof ReportBaseFootnote) {
65a25f0a04SGreg Roach                    // Check if the Footnote has been set with it’s link number
66a25f0a04SGreg Roach                    $renderer->checkFootnote($element);
67a25f0a04SGreg Roach                    // Save first the last element if any
68a25f0a04SGreg Roach                    if (!empty($lastelement)) {
69a25f0a04SGreg Roach                        $newelements[] = $lastelement;
7013abd6f3SGreg Roach                        $lastelement   = [];
71a25f0a04SGreg Roach                    }
72a25f0a04SGreg Roach                    // Save the Footnote with it’s link number as key for sorting later
73a25f0a04SGreg Roach                    $footnote_element[$element->num] = $element;
74ce15a17aSGreg Roach                } elseif (!($element instanceof ReportBaseFootnote) || trim($element->getValue()) != '') {
75ce15a17aSGreg Roach                    // Do not keep empty footnotes
76a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
77a25f0a04SGreg Roach                        ksort($footnote_element);
78a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
79a25f0a04SGreg Roach                            $newelements[] = $links;
80a25f0a04SGreg Roach                        }
8113abd6f3SGreg Roach                        $footnote_element = [];
82a25f0a04SGreg Roach                    }
83a25f0a04SGreg Roach                    if (!empty($lastelement)) {
84a25f0a04SGreg Roach                        $newelements[] = $lastelement;
8513abd6f3SGreg Roach                        $lastelement   = [];
86a25f0a04SGreg Roach                    }
87a25f0a04SGreg Roach                    $newelements[] = $element;
88a25f0a04SGreg Roach                }
89a25f0a04SGreg Roach            } else {
90a25f0a04SGreg Roach                if (!empty($lastelement)) {
91a25f0a04SGreg Roach                    $newelements[] = $lastelement;
9213abd6f3SGreg Roach                    $lastelement   = [];
93a25f0a04SGreg Roach                }
94a25f0a04SGreg Roach                if (!empty($footnote_element)) {
95a25f0a04SGreg Roach                    ksort($footnote_element);
96a25f0a04SGreg Roach                    foreach ($footnote_element as $links) {
97a25f0a04SGreg Roach                        $newelements[] = $links;
98a25f0a04SGreg Roach                    }
9913abd6f3SGreg Roach                    $footnote_element = [];
100a25f0a04SGreg Roach                }
101a25f0a04SGreg Roach                $newelements[] = $element;
102a25f0a04SGreg Roach            }
103a25f0a04SGreg Roach        }
104a25f0a04SGreg Roach        if (!empty($lastelement)) {
105a25f0a04SGreg Roach            $newelements[] = $lastelement;
106a25f0a04SGreg Roach        }
107a25f0a04SGreg Roach        if (!empty($footnote_element)) {
108a25f0a04SGreg Roach            ksort($footnote_element);
109a25f0a04SGreg Roach            foreach ($footnote_element as $links) {
110a25f0a04SGreg Roach                $newelements[] = $links;
111a25f0a04SGreg Roach            }
112a25f0a04SGreg Roach        }
113a25f0a04SGreg Roach        $this->elements = $newelements;
1148a4ee39cSGreg Roach        unset($footnote_element, $lastelement, $newelements);
115a25f0a04SGreg Roach
116a25f0a04SGreg Roach        $cP = 0; // Class Padding
117a25f0a04SGreg Roach
118a25f0a04SGreg Roach        // Used with line breaks and cell height calculation within this box only
119a25f0a04SGreg Roach        $renderer->largestFontHeight = 0;
120a25f0a04SGreg Roach
12167c69ce5SGreg Roach        // If current position (left)
122c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
1237820e4d7SGreg Roach            $cX = $renderer->getX();
124a25f0a04SGreg Roach        } else {
125a25f0a04SGreg Roach            $cX = $this->left;
1267820e4d7SGreg Roach            $renderer->setX($cX);
127a25f0a04SGreg Roach        }
12867c69ce5SGreg Roach        // If current position (top)
129c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
1303844edd4SGreg Roach            $this->top = $renderer->getY();
131a25f0a04SGreg Roach        } else {
1327820e4d7SGreg Roach            $renderer->setY($this->top);
133a25f0a04SGreg Roach        }
134a25f0a04SGreg Roach
13567c69ce5SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page width (margin)
136a25f0a04SGreg Roach        if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) {
137a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidth();
138a25f0a04SGreg Roach        }
139a25f0a04SGreg Roach        // Setup the CellPadding
140a25f0a04SGreg Roach        if ($this->padding) {
141a25f0a04SGreg Roach            $cP = $renderer->cPadding;
142a25f0a04SGreg Roach        }
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach        // For padding, we have to use less wrap width
145a25f0a04SGreg Roach        $cW = $this->width - ($cP * 2);
146a25f0a04SGreg Roach
147a25f0a04SGreg Roach        //-- calculate the text box height
148a25f0a04SGreg Roach        // Number of lines, will be converted to height
149a25f0a04SGreg Roach        $cHT = 0;
150a25f0a04SGreg Roach        // Element height (exept text)
151a25f0a04SGreg Roach        $eH = 0;
152a25f0a04SGreg Roach        // Footnote height (in points)
153a25f0a04SGreg Roach        $fH = 0;
154a25f0a04SGreg Roach        $w  = 0;
155a25f0a04SGreg Roach        //-- $lw is an array
156a25f0a04SGreg Roach        // 0 => last line width
157a25f0a04SGreg Roach        // 1 => 1 if text was wrapped, 0 if text did not wrap
158a25f0a04SGreg Roach        // 2 => number of LF
15913abd6f3SGreg Roach        $lw = [];
160a25f0a04SGreg Roach        // Element counter
161a25f0a04SGreg Roach        $cE = count($this->elements);
162a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
163a25f0a04SGreg Roach            if (is_object($this->elements[$i])) {
164a25f0a04SGreg Roach                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
165a25f0a04SGreg Roach                if ($ew == $cW) {
166a25f0a04SGreg Roach                    $w = 0;
167a25f0a04SGreg Roach                }
168a25f0a04SGreg Roach                $lw = $this->elements[$i]->getWidth($renderer);
169a25f0a04SGreg Roach                // Text is already gets the # LF
170a25f0a04SGreg Roach                $cHT += $lw[2];
171a25f0a04SGreg Roach                if ($lw[1] == 1) {
172a25f0a04SGreg Roach                    $w = $lw[0];
173a25f0a04SGreg Roach                } elseif ($lw[1] == 2) {
174a25f0a04SGreg Roach                    $w = 0;
175a25f0a04SGreg Roach                } else {
176a25f0a04SGreg Roach                    $w += $lw[0];
177a25f0a04SGreg Roach                }
178a25f0a04SGreg Roach                if ($w > $cW) {
179a25f0a04SGreg Roach                    $w = $lw[0];
180a25f0a04SGreg Roach                }
181a25f0a04SGreg Roach                // For anything else but text (images), get the height
182a25f0a04SGreg Roach                $eH += $this->elements[$i]->getHeight($renderer);
183a25f0a04SGreg Roach            } else {
184a25f0a04SGreg Roach                $fH += abs($renderer->getFootnotesHeight($cW));
185a25f0a04SGreg Roach            }
186a25f0a04SGreg Roach        }
18767c69ce5SGreg Roach
188a25f0a04SGreg Roach        // Add up what’s the final height
189a25f0a04SGreg Roach        $cH = $this->height;
190a25f0a04SGreg Roach        // If any element exist
191a25f0a04SGreg Roach        if ($cE > 0) {
192a25f0a04SGreg Roach            // Check if this is text or some other element, like images
193a25f0a04SGreg Roach            if ($eH == 0) {
194a25f0a04SGreg Roach                // Number of LF but at least one line
195a25f0a04SGreg Roach                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
196a25f0a04SGreg Roach                // Calculate the cell hight with the largest font size used
197e364afe4SGreg Roach                $cHT *= $renderer->largestFontHeight;
198a25f0a04SGreg Roach                if ($cH < $cHT) {
199a25f0a04SGreg Roach                    $cH = $cHT;
200a25f0a04SGreg Roach                }
201ce15a17aSGreg Roach            } else {
202ce15a17aSGreg Roach                // This is any other element
203a25f0a04SGreg Roach                if ($cH < $eH) {
204a25f0a04SGreg Roach                    $cH = $eH;
205a25f0a04SGreg Roach                }
206a25f0a04SGreg Roach                // Add Footnote height to the rest of the height
207a25f0a04SGreg Roach                $cH += $fH;
208a25f0a04SGreg Roach            }
209a25f0a04SGreg Roach        }
210a25f0a04SGreg Roach
211a25f0a04SGreg Roach        unset($lw, $cHT, $fH, $w);
212a25f0a04SGreg Roach
213a25f0a04SGreg Roach        // Finaly, check the last cells height
214a25f0a04SGreg Roach        if ($cH < $renderer->lastCellHeight) {
215a25f0a04SGreg Roach            $cH = $renderer->lastCellHeight;
216a25f0a04SGreg Roach        }
217a25f0a04SGreg Roach        // Update max Y incase of a pagebreak
218a25f0a04SGreg Roach        // We don't want to over write any images or other stuff
219a25f0a04SGreg Roach        $renderer->addMaxY($this->top + $cH);
220a25f0a04SGreg Roach
221a25f0a04SGreg Roach        // Start to print HTML
2227a6ee1acSGreg Roach        echo '<div style="position:absolute;top:', $this->top, 'pt;';
223a25f0a04SGreg Roach        // LTR (left) or RTL (right)
2247a6ee1acSGreg Roach        echo $renderer->alignRTL, ':', $cX, 'pt;';
225a25f0a04SGreg Roach        // Background color
226a25f0a04SGreg Roach        if ($this->fill) {
227a25f0a04SGreg Roach            if (!empty($this->bgcolor)) {
2287a6ee1acSGreg Roach                echo ' background-color:', $this->bgcolor, ';';
229a25f0a04SGreg Roach            }
230a25f0a04SGreg Roach        }
231a25f0a04SGreg Roach        // Print padding only when it’s set
232a25f0a04SGreg Roach        if ($this->padding) {
233a25f0a04SGreg Roach            // Use Cell around padding to support RTL also
2347a6ee1acSGreg Roach            echo 'padding:', $cP, 'pt;';
235a25f0a04SGreg Roach        }
236a25f0a04SGreg Roach        // Border setup
237a25f0a04SGreg Roach        if ($this->border) {
2387a6ee1acSGreg Roach            echo ' border:solid black 1pt;';
2397a6ee1acSGreg Roach            echo 'width:', ($this->width - 1 - ($cP * 2)), 'pt;height:', $cH - 1, 'pt;';
240a25f0a04SGreg Roach        } else {
2417a6ee1acSGreg Roach            echo 'width:', ($this->width - ($cP * 2)), 'pt;height:', $cH, 'pt;';
242a25f0a04SGreg Roach        }
2437a6ee1acSGreg Roach        echo '">';
244a25f0a04SGreg Roach
245a25f0a04SGreg Roach        // Do a little "margin" trick before print
246a25f0a04SGreg Roach        // to get the correct current position => "."
2477820e4d7SGreg Roach        $cXT = $renderer->getX();
2487820e4d7SGreg Roach        $cYT = $renderer->getY();
2497820e4d7SGreg Roach        $renderer->setXy(0, 0);
250a25f0a04SGreg Roach
251a25f0a04SGreg Roach        // Print the text elements
252a25f0a04SGreg Roach        foreach ($this->elements as $element) {
253044416d2SGreg Roach            if ($element instanceof ReportBaseElement) {
254a25f0a04SGreg Roach                $element->render($renderer, $cX, false);
255044416d2SGreg Roach            } elseif ($element === 'footnotetexts') {
2567820e4d7SGreg Roach                $renderer->footnotes();
257044416d2SGreg Roach            } elseif ($element === 'addpage') {
2587820e4d7SGreg Roach                $renderer->addPage();
259a25f0a04SGreg Roach            }
260a25f0a04SGreg Roach        }
261a25f0a04SGreg Roach        echo "</div>\n";
262a25f0a04SGreg Roach
263a25f0a04SGreg Roach        // Reset "margins"
2647820e4d7SGreg Roach        $renderer->setXy($cXT, $cYT);
265a25f0a04SGreg Roach        // This will be mostly used to trick the multiple images last height
266a25f0a04SGreg Roach        if ($this->reseth) {
267a25f0a04SGreg Roach            $cH = 0;
268a25f0a04SGreg Roach        }
269a25f0a04SGreg Roach        // New line and some clean up
270a25f0a04SGreg Roach        if (!$this->newline) {
2717820e4d7SGreg Roach            $renderer->setXy($cX + $this->width, $this->top);
272a25f0a04SGreg Roach            $renderer->lastCellHeight = $cH;
273a25f0a04SGreg Roach        } else {
2747820e4d7SGreg Roach            $renderer->setXy(0, $this->top + $cH + ($cP * 2));
275a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
276a25f0a04SGreg Roach        }
277a25f0a04SGreg Roach    }
278a25f0a04SGreg Roach}
279