xref: /webtrees/app/Report/ReportHtmlText.php (revision fd6c8c786edaa7cf7534ebe606cd8d58eec364cb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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 ReportHtmlText
20 */
21class ReportHtmlText extends ReportBaseText
22{
23    /**
24     * Render the elements.
25     *
26     * @param ReportHtml $renderer
27     * @param int        $curx
28     * @param bool       $attrib Is is called from a different element?
29     *
30     * @return void
31     */
32    public function render($renderer, $curx = 0, $attrib = true)
33    {
34
35        // Setup the style name
36        if ($renderer->getCurrentStyle() != $this->styleName) {
37            $renderer->setCurrentStyle($this->styleName);
38        }
39        $temptext = str_replace('#PAGENUM#', $renderer->pageNo(), $this->text);
40        // underline «title» part of Source item
41        $temptext = str_replace([
42            '«',
43            '»',
44        ], [
45            '<u>',
46            '</u>',
47        ], $temptext);
48
49        // If any text at all
50        if (!empty($temptext)) {
51            // If called by an other element
52            if (!$attrib) {
53                $renderer->write($temptext, $this->color);
54            } else {
55                // Save the start positions
56                $startX = $renderer->getX();
57                $startY = $renderer->getY();
58                $width  = $renderer->getRemainingWidth();
59                // If text is wider then page width then wrap it
60                if ($renderer->getStringWidth($temptext) > $width) {
61                    $lines = explode("\n", $temptext);
62                    foreach ($lines as $line) {
63                        echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">';
64                        $line = $renderer->textWrap($line, $width);
65                        $startY += $renderer->getTextCellHeight($line);
66                        $renderer->setY($startY);
67                        $renderer->write($line, $this->color);
68                        echo "</div>\n";
69                    }
70                } else {
71                    echo '<div style="position:absolute;top:', $startY, 'pt;', $renderer->alignRTL, ':', $startX, 'pt;width:', $width, 'pt;">';
72                    $renderer->write($temptext, $this->color);
73                    echo "</div>\n";
74                    $renderer->setX($startX + $renderer->getStringWidth($temptext));
75                    if ($renderer->countLines($temptext) != 1) {
76                        $renderer->setXy(0, ($startY + $renderer->getTextCellHeight($temptext)));
77                    }
78                }
79            }
80        }
81    }
82
83    /**
84     * Returns the height in points of the text element
85     * The height is already calculated in getWidth()
86     *
87     * @param ReportHtml $html
88     *
89     * @return float
90     */
91    public function getHeight($html): float
92    {
93        $ct = substr_count($this->text, "\n");
94        if ($ct > 0) {
95            $ct += 1;
96        }
97        $style = $html->getStyle($this->styleName);
98
99        return ($style['size'] * $ct) * $html->cellHeightRatio;
100    }
101
102    /**
103     * Get the width of text and wrap it too
104     *
105     * @param ReportHtml $html
106     *
107     * @return float|array
108     */
109    public function getWidth($html)
110    {
111        // Setup the style name
112        if ($html->getCurrentStyle() != $this->styleName) {
113            $html->setCurrentStyle($this->styleName);
114        }
115
116        // Check for the largest font size in the box
117        $fsize = $html->getCurrentStyleHeight();
118        if ($fsize > $html->largestFontHeight) {
119            $html->largestFontHeight = $fsize;
120        }
121
122        // Get the line width for the text in points
123        $lw = $html->getStringWidth($this->text);
124        // Line Feed counter - Number of lines in the text
125        $lfct = $html->countLines($this->text);
126        // If there is still remaining wrap width...
127        if ($this->wrapWidthRemaining > 0) {
128            // Check with line counter too!
129            if ($lw >= $this->wrapWidthRemaining || $lfct > 1) {
130                $newtext            = '';
131                $wrapWidthRemaining = $this->wrapWidthRemaining;
132                $lines              = explode("\n", $this->text);
133                // Go throught the text line by line
134                foreach ($lines as $line) {
135                    // Line width in points + a little margin
136                    $lw = $html->getStringWidth($line);
137                    // If the line has to be wraped
138                    if ($lw > $wrapWidthRemaining) {
139                        $words    = explode(' ', $line);
140                        $addspace = count($words);
141                        $lw       = 0;
142                        foreach ($words as $word) {
143                            $addspace--;
144                            $lw += $html->getStringWidth($word . ' ');
145                            if ($lw <= $wrapWidthRemaining) {
146                                $newtext .= $word;
147                                if ($addspace != 0) {
148                                    $newtext .= ' ';
149                                }
150                            } else {
151                                $lw = $html->getStringWidth($word . ' ');
152                                $newtext .= "\n$word";
153                                if ($addspace != 0) {
154                                    $newtext .= ' ';
155                                }
156                                // Reset the wrap width to the cell width
157                                $wrapWidthRemaining = $this->wrapWidthCell;
158                            }
159                        }
160                    } else {
161                        $newtext .= $line;
162                    }
163                    // Check the Line Feed counter
164                    if ($lfct > 1) {
165                        // Add a new line feed as long as it’s not the last line
166                        $newtext .= "\n";
167                        // Reset the line width
168                        $lw = 0;
169                        // Reset the wrap width to the cell width
170                        $wrapWidthRemaining = $this->wrapWidthCell;
171                    }
172                    $lfct--;
173                }
174                $this->text = $newtext;
175                $lfct       = substr_count($this->text, "\n");
176
177                return [
178                    $lw,
179                    1,
180                    $lfct,
181                ];
182            }
183        }
184        $l    = 0;
185        $lfct = substr_count($this->text, "\n");
186        if ($lfct > 0) {
187            $l = 2;
188        }
189
190        return [
191            $lw,
192            $l,
193            $lfct,
194        ];
195    }
196}
197