xref: /webtrees/app/Report/ReportHtmlTextbox.php (revision c21bdddc89b941099aac7bad64af11da73acab4e)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
19a25f0a04SGreg Roach
20a25f0a04SGreg Roach/**
21a25f0a04SGreg Roach * Class ReportHtmlTextbox
22a25f0a04SGreg Roach */
23c1010edaSGreg Roachclass ReportHtmlTextbox extends ReportBaseTextbox
24c1010edaSGreg Roach{
25a25f0a04SGreg Roach    /**
2676692c8bSGreg Roach     * Render the elements.
2776692c8bSGreg Roach     *
28a25f0a04SGreg Roach     * @param ReportHtml $renderer
29c7ff4153SGreg Roach     *
30c7ff4153SGreg Roach     * @return void
31a25f0a04SGreg Roach     */
32c1010edaSGreg Roach    public function render($renderer)
33c1010edaSGreg Roach    {
34a25f0a04SGreg Roach        // checkFootnote
3513abd6f3SGreg Roach        $newelements      = [];
3613abd6f3SGreg Roach        $lastelement      = [];
3713abd6f3SGreg Roach        $footnote_element = [];
38a25f0a04SGreg Roach        // Element counter
39a25f0a04SGreg Roach        $cE = count($this->elements);
40a25f0a04SGreg Roach        //-- collapse duplicate elements
41a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
42a25f0a04SGreg Roach            $element = $this->elements[$i];
43a25f0a04SGreg Roach            if (is_object($element)) {
44a25f0a04SGreg Roach                if ($element instanceof ReportBaseText) {
45a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
46a25f0a04SGreg Roach                        ksort($footnote_element);
47a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
48a25f0a04SGreg Roach                            $newelements[] = $links;
49a25f0a04SGreg Roach                        }
5013abd6f3SGreg Roach                        $footnote_element = [];
51a25f0a04SGreg Roach                    }
52a25f0a04SGreg Roach                    if (empty($lastelement)) {
53a25f0a04SGreg Roach                        $lastelement = $element;
54a25f0a04SGreg Roach                    } else {
55a25f0a04SGreg Roach                        // Checking if the Text has the same style
56a25f0a04SGreg Roach                        if ($element->getStyleName() == $lastelement->getStyleName()) {
577a6ee1acSGreg Roach                            $lastelement->addText(str_replace("\n", '<br>', $element->getValue()));
58a25f0a04SGreg Roach                        } elseif (!empty($lastelement)) {
59a25f0a04SGreg Roach                            $newelements[] = $lastelement;
60a25f0a04SGreg Roach                            $lastelement   = $element;
61a25f0a04SGreg Roach                        }
62a25f0a04SGreg Roach                    }
63ce15a17aSGreg Roach                } elseif ($element instanceof ReportBaseFootnote) {
64a25f0a04SGreg Roach                    // Check if the Footnote has been set with it’s link number
65a25f0a04SGreg Roach                    $renderer->checkFootnote($element);
66a25f0a04SGreg Roach                    // Save first the last element if any
67a25f0a04SGreg Roach                    if (!empty($lastelement)) {
68a25f0a04SGreg Roach                        $newelements[] = $lastelement;
6913abd6f3SGreg Roach                        $lastelement   = [];
70a25f0a04SGreg Roach                    }
71a25f0a04SGreg Roach                    // Save the Footnote with it’s link number as key for sorting later
72a25f0a04SGreg Roach                    $footnote_element[$element->num] = $element;
73ce15a17aSGreg Roach                } elseif (!($element instanceof ReportBaseFootnote) || trim($element->getValue()) != '') {
74ce15a17aSGreg Roach                    // Do not keep empty footnotes
75a25f0a04SGreg Roach                    if (!empty($footnote_element)) {
76a25f0a04SGreg Roach                        ksort($footnote_element);
77a25f0a04SGreg Roach                        foreach ($footnote_element as $links) {
78a25f0a04SGreg Roach                            $newelements[] = $links;
79a25f0a04SGreg Roach                        }
8013abd6f3SGreg Roach                        $footnote_element = [];
81a25f0a04SGreg Roach                    }
82a25f0a04SGreg Roach                    if (!empty($lastelement)) {
83a25f0a04SGreg Roach                        $newelements[] = $lastelement;
8413abd6f3SGreg Roach                        $lastelement   = [];
85a25f0a04SGreg Roach                    }
86a25f0a04SGreg Roach                    $newelements[] = $element;
87a25f0a04SGreg Roach                }
88a25f0a04SGreg Roach            } else {
89a25f0a04SGreg Roach                if (!empty($lastelement)) {
90a25f0a04SGreg Roach                    $newelements[] = $lastelement;
9113abd6f3SGreg Roach                    $lastelement   = [];
92a25f0a04SGreg Roach                }
93a25f0a04SGreg Roach                if (!empty($footnote_element)) {
94a25f0a04SGreg Roach                    ksort($footnote_element);
95a25f0a04SGreg Roach                    foreach ($footnote_element as $links) {
96a25f0a04SGreg Roach                        $newelements[] = $links;
97a25f0a04SGreg Roach                    }
9813abd6f3SGreg Roach                    $footnote_element = [];
99a25f0a04SGreg Roach                }
100a25f0a04SGreg Roach                $newelements[] = $element;
101a25f0a04SGreg Roach            }
102a25f0a04SGreg Roach        }
103a25f0a04SGreg Roach        if (!empty($lastelement)) {
104a25f0a04SGreg Roach            $newelements[] = $lastelement;
105a25f0a04SGreg Roach        }
106a25f0a04SGreg Roach        if (!empty($footnote_element)) {
107a25f0a04SGreg Roach            ksort($footnote_element);
108a25f0a04SGreg Roach            foreach ($footnote_element as $links) {
109a25f0a04SGreg Roach                $newelements[] = $links;
110a25f0a04SGreg Roach            }
111a25f0a04SGreg Roach        }
112a25f0a04SGreg Roach        $this->elements = $newelements;
1138a4ee39cSGreg Roach        unset($footnote_element, $lastelement, $newelements);
114a25f0a04SGreg Roach
115a25f0a04SGreg Roach        $cP = 0; // Class Padding
116a25f0a04SGreg Roach
117a25f0a04SGreg Roach        // Used with line breaks and cell height calculation within this box only
118a25f0a04SGreg Roach        $renderer->largestFontHeight = 0;
119a25f0a04SGreg Roach
120a25f0a04SGreg Roach        // Current position
121*c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
1227820e4d7SGreg Roach            $cX = $renderer->getX();
123a25f0a04SGreg Roach        } else {
124a25f0a04SGreg Roach            $cX = $this->left;
1257820e4d7SGreg Roach            $renderer->setX($cX);
126a25f0a04SGreg Roach        }
127a25f0a04SGreg Roach        // Current position (top)
128*c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
1293844edd4SGreg Roach            $this->top = $renderer->getY();
130a25f0a04SGreg Roach        } else {
1317820e4d7SGreg Roach            $renderer->setY($this->top);
132a25f0a04SGreg Roach        }
133a25f0a04SGreg Roach
134a25f0a04SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page wide
135a25f0a04SGreg Roach        if ($this->width == 0 || $this->width > $renderer->getRemainingWidth()) {
136a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidth();
137a25f0a04SGreg Roach        }
138a25f0a04SGreg Roach        // Setup the CellPadding
139a25f0a04SGreg Roach        if ($this->padding) {
140a25f0a04SGreg Roach            $cP = $renderer->cPadding;
141a25f0a04SGreg Roach        }
142a25f0a04SGreg Roach
143a25f0a04SGreg Roach        // For padding, we have to use less wrap width
144a25f0a04SGreg Roach        $cW = $this->width - ($cP * 2);
145a25f0a04SGreg Roach
146a25f0a04SGreg Roach        //-- calculate the text box height
147a25f0a04SGreg Roach        // Number of lines, will be converted to height
148a25f0a04SGreg Roach        $cHT = 0;
149a25f0a04SGreg Roach        // Element height (exept text)
150a25f0a04SGreg Roach        $eH = 0;
151a25f0a04SGreg Roach        // Footnote height (in points)
152a25f0a04SGreg Roach        $fH = 0;
153a25f0a04SGreg Roach        $w  = 0;
154a25f0a04SGreg Roach        //-- $lw is an array
155a25f0a04SGreg Roach        // 0 => last line width
156a25f0a04SGreg Roach        // 1 => 1 if text was wrapped, 0 if text did not wrap
157a25f0a04SGreg Roach        // 2 => number of LF
15813abd6f3SGreg Roach        $lw = [];
159a25f0a04SGreg Roach        // Element counter
160a25f0a04SGreg Roach        $cE = count($this->elements);
161a25f0a04SGreg Roach        for ($i = 0; $i < $cE; $i++) {
162a25f0a04SGreg Roach            if (is_object($this->elements[$i])) {
163a25f0a04SGreg Roach                $ew = $this->elements[$i]->setWrapWidth($cW - $w - 2, $cW);
164a25f0a04SGreg Roach                if ($ew == $cW) {
165a25f0a04SGreg Roach                    $w = 0;
166a25f0a04SGreg Roach                }
167a25f0a04SGreg Roach                $lw = $this->elements[$i]->getWidth($renderer);
168a25f0a04SGreg Roach                // Text is already gets the # LF
169a25f0a04SGreg Roach                $cHT += $lw[2];
170a25f0a04SGreg Roach                if ($lw[1] == 1) {
171a25f0a04SGreg Roach                    $w = $lw[0];
172a25f0a04SGreg Roach                } elseif ($lw[1] == 2) {
173a25f0a04SGreg Roach                    $w = 0;
174a25f0a04SGreg Roach                } else {
175a25f0a04SGreg Roach                    $w += $lw[0];
176a25f0a04SGreg Roach                }
177a25f0a04SGreg Roach                if ($w > $cW) {
178a25f0a04SGreg Roach                    $w = $lw[0];
179a25f0a04SGreg Roach                }
180a25f0a04SGreg Roach                // For anything else but text (images), get the height
181a25f0a04SGreg Roach                $eH += $this->elements[$i]->getHeight($renderer);
182a25f0a04SGreg Roach            } else {
183a25f0a04SGreg Roach                $fH += abs($renderer->getFootnotesHeight($cW));
184a25f0a04SGreg Roach            }
185a25f0a04SGreg Roach        }
186a25f0a04SGreg Roach        // Add up what’s the final height
187a25f0a04SGreg Roach        $cH = $this->height;
188a25f0a04SGreg Roach        // If any element exist
189a25f0a04SGreg Roach        if ($cE > 0) {
190a25f0a04SGreg Roach            // Check if this is text or some other element, like images
191a25f0a04SGreg Roach            if ($eH == 0) {
192a25f0a04SGreg Roach                // Number of LF but at least one line
193a25f0a04SGreg Roach                $cHT = ($cHT + 1) * $renderer->cellHeightRatio;
194a25f0a04SGreg Roach                // Calculate the cell hight with the largest font size used
195a25f0a04SGreg Roach                $cHT = $cHT * $renderer->largestFontHeight;
196a25f0a04SGreg Roach                if ($cH < $cHT) {
197a25f0a04SGreg Roach                    $cH = $cHT;
198a25f0a04SGreg Roach                }
199ce15a17aSGreg Roach            } else {
200ce15a17aSGreg Roach                // This is any other element
201a25f0a04SGreg Roach                if ($cH < $eH) {
202a25f0a04SGreg Roach                    $cH = $eH;
203a25f0a04SGreg Roach                }
204a25f0a04SGreg Roach                // Add Footnote height to the rest of the height
205a25f0a04SGreg Roach                $cH += $fH;
206a25f0a04SGreg Roach            }
207a25f0a04SGreg Roach        }
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach        unset($lw, $cHT, $fH, $w);
210a25f0a04SGreg Roach
211a25f0a04SGreg Roach        // Finaly, check the last cells height
212a25f0a04SGreg Roach        if ($cH < $renderer->lastCellHeight) {
213a25f0a04SGreg Roach            $cH = $renderer->lastCellHeight;
214a25f0a04SGreg Roach        }
215a25f0a04SGreg Roach        // Update max Y incase of a pagebreak
216a25f0a04SGreg Roach        // We don't want to over write any images or other stuff
217a25f0a04SGreg Roach        $renderer->addMaxY($this->top + $cH);
218a25f0a04SGreg Roach
219a25f0a04SGreg Roach        // Start to print HTML
2207a6ee1acSGreg Roach        echo '<div style="position:absolute;top:', $this->top, 'pt;';
221a25f0a04SGreg Roach        // LTR (left) or RTL (right)
2227a6ee1acSGreg Roach        echo $renderer->alignRTL, ':', $cX, 'pt;';
223a25f0a04SGreg Roach        // Background color
224a25f0a04SGreg Roach        if ($this->fill) {
225a25f0a04SGreg Roach            if (!empty($this->bgcolor)) {
2267a6ee1acSGreg Roach                echo ' background-color:', $this->bgcolor, ';';
227a25f0a04SGreg Roach            }
228a25f0a04SGreg Roach        }
229a25f0a04SGreg Roach        // Print padding only when it’s set
230a25f0a04SGreg Roach        if ($this->padding) {
231a25f0a04SGreg Roach            // Use Cell around padding to support RTL also
2327a6ee1acSGreg Roach            echo 'padding:', $cP, 'pt;';
233a25f0a04SGreg Roach        }
234a25f0a04SGreg Roach        // Border setup
235a25f0a04SGreg Roach        if ($this->border) {
2367a6ee1acSGreg Roach            echo ' border:solid black 1pt;';
2377a6ee1acSGreg Roach            echo 'width:', ($this->width - 1 - ($cP * 2)), 'pt;height:', $cH - 1, 'pt;';
238a25f0a04SGreg Roach        } else {
2397a6ee1acSGreg Roach            echo 'width:', ($this->width - ($cP * 2)), 'pt;height:', $cH, 'pt;';
240a25f0a04SGreg Roach        }
2417a6ee1acSGreg Roach        echo '">';
242a25f0a04SGreg Roach
243a25f0a04SGreg Roach        // Do a little "margin" trick before print
244a25f0a04SGreg Roach        // to get the correct current position => "."
2457820e4d7SGreg Roach        $cXT = $renderer->getX();
2467820e4d7SGreg Roach        $cYT = $renderer->getY();
2477820e4d7SGreg Roach        $renderer->setXy(0, 0);
248a25f0a04SGreg Roach
249a25f0a04SGreg Roach        // Print the text elements
250a25f0a04SGreg Roach        foreach ($this->elements as $element) {
251a25f0a04SGreg Roach            if (is_object($element)) {
252a25f0a04SGreg Roach                $element->render($renderer, $cX, false);
2537a6ee1acSGreg Roach            } elseif (is_string($element) && $element == 'footnotetexts') {
2547820e4d7SGreg Roach                $renderer->footnotes();
2557a6ee1acSGreg Roach            } elseif (is_string($element) && $element == 'addpage') {
2567820e4d7SGreg Roach                $renderer->addPage();
257a25f0a04SGreg Roach            }
258a25f0a04SGreg Roach        }
259a25f0a04SGreg Roach        echo "</div>\n";
260a25f0a04SGreg Roach
261a25f0a04SGreg Roach        // Reset "margins"
2627820e4d7SGreg Roach        $renderer->setXy($cXT, $cYT);
263a25f0a04SGreg Roach        // This will be mostly used to trick the multiple images last height
264a25f0a04SGreg Roach        if ($this->reseth) {
265a25f0a04SGreg Roach            $cH = 0;
266a25f0a04SGreg Roach        }
267a25f0a04SGreg Roach        // New line and some clean up
268a25f0a04SGreg Roach        if (!$this->newline) {
2697820e4d7SGreg Roach            $renderer->setXy($cX + $this->width, $this->top);
270a25f0a04SGreg Roach            $renderer->lastCellHeight = $cH;
271a25f0a04SGreg Roach        } else {
2727820e4d7SGreg Roach            $renderer->setXy(0, $this->top + $cH + ($cP * 2));
273a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
274a25f0a04SGreg Roach        }
275a25f0a04SGreg Roach    }
276a25f0a04SGreg Roach}
277