xref: /webtrees/app/Report/ReportPdfText.php (revision 0115bc1678cafd957052cd2d256563ad3e242cc1)
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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Report;
19
20use Fisharebest\Webtrees\Functions\FunctionsRtl;
21
22/**
23 * Class ReportPdfText
24 */
25class ReportPdfText extends ReportBaseText
26{
27    /**
28     * PDF Text renderer
29     *
30     * @param ReportTcpdf $renderer
31     *
32     * @return void
33     */
34    public function render($renderer)
35    {
36        // Set up the style
37        if ($renderer->getCurrentStyle() != $this->styleName) {
38            $renderer->setCurrentStyle($this->styleName);
39        }
40        $temptext = str_replace('#PAGENUM#', (string) $renderer->PageNo(), $this->text);
41        // underline «title» part of Source item
42        $temptext = str_replace([
43            '«',
44            '»',
45        ], [
46            '<u>',
47            '</u>',
48        ], $temptext);
49
50        // Paint the text color or they might use inherited colors by the previous function
51        $match = [];
52        if (preg_match('/#?(..)(..)(..)/', $this->color, $match)) {
53            $r = hexdec($match[1]);
54            $g = hexdec($match[2]);
55            $b = hexdec($match[3]);
56            $renderer->SetTextColor($r, $g, $b);
57        } else {
58            $renderer->SetTextColor(0, 0, 0);
59        }
60        $temptext = FunctionsRtl::spanLtrRtl($temptext);
61        $temptext = str_replace(
62            [
63                '<br><span dir="rtl">',
64                '<br><span dir="ltr">',
65                '> ',
66                ' <',
67            ],
68            [
69                '<span dir="rtl" ><br>',
70                '<span dir="ltr" ><br>',
71                '>&nbsp;',
72                '&nbsp;<',
73            ],
74            $temptext
75        );
76        $renderer->writeHTML(
77            $temptext,
78            false,
79            false,
80            true,
81            false,
82            ''
83        ); //change height - line break etc. - the form is mirror on rtl pages
84        // Reset the text color to black or it will be inherited
85        $renderer->SetTextColor(0, 0, 0);
86    }
87
88    /**
89     * Returns the height in points of the text element
90     * The height is already calculated in getWidth()
91     *
92     * @param ReportTcpdf $renderer
93     *
94     * @return float
95     */
96    public function getHeight($renderer): float
97    {
98        return 0;
99    }
100
101    /**
102     * Splits the text into lines if necessary to fit into a giving cell
103     *
104     * @param ReportTcpdf $renderer
105     *
106     * @return float|array
107     */
108    public function getWidth($renderer)
109    {
110        // Setup the style name, a font must be selected to calculate the width
111        if ($renderer->getCurrentStyle() != $this->styleName) {
112            $renderer->setCurrentStyle($this->styleName);
113        }
114
115        // Check for the largest font size in the box
116        $fsize = $renderer->getCurrentStyleHeight();
117        if ($fsize > $renderer->largestFontHeight) {
118            $renderer->largestFontHeight = $fsize;
119        }
120
121        // Get the line width for the text in points
122        $lw = $renderer->getStringWidth($this->text);
123        // Line Feed counter - Number of lines in the text
124        $lfct = substr_count($this->text, "\n") + 1;
125        // If there is still remaining wrap width...
126        $wrapWidthRemaining = $this->wrapWidthRemaining;
127        if ($wrapWidthRemaining > 0) {
128            // Check with line counter too!
129            if ($lw >= $wrapWidthRemaining || $lfct > 1) {
130                $newtext = '';
131                $lines   = explode("\n", $this->text);
132                // Go throught the text line by line
133                foreach ($lines as $line) {
134                    // Line width in points + a little margin
135                    $lw = $renderer->getStringWidth($line);
136                    // If the line has to be wraped
137                    if ($lw > $wrapWidthRemaining) {
138                        $words    = explode(' ', $line);
139                        $addspace = count($words);
140                        $lw       = 0;
141                        foreach ($words as $word) {
142                            $addspace--;
143                            $lw += $renderer->getStringWidth($word . ' ');
144                            if ($lw <= $wrapWidthRemaining) {
145                                $newtext .= $word;
146                                if ($addspace != 0) {
147                                    $newtext .= ' ';
148                                }
149                            } else {
150                                $lw = $renderer->getStringWidth($word . ' ');
151                                $newtext .= "\n$word";
152                                if ($addspace != 0) {
153                                    $newtext .= ' ';
154                                }
155                                // Reset the wrap width to the cell width
156                                $wrapWidthRemaining = $this->wrapWidthCell;
157                            }
158                        }
159                    } else {
160                        $newtext .= $line;
161                    }
162                    // Check the Line Feed counter
163                    if ($lfct > 1) {
164                        // Add a new line as long as it’s not the last line
165                        $newtext .= "\n";
166                        // Reset the line width
167                        $lw = 0;
168                        // Reset the wrap width to the cell width
169                        $wrapWidthRemaining = $this->wrapWidthCell;
170                    }
171                    $lfct--;
172                }
173                $this->text = $newtext;
174                $lfct       = substr_count($this->text, "\n");
175
176                return [
177                    $lw,
178                    1,
179                    $lfct,
180                ];
181            }
182        }
183        $l    = 0;
184        $lfct = substr_count($this->text, "\n");
185        if ($lfct > 0) {
186            $l = 2;
187        }
188
189        return [
190            $lw,
191            $l,
192            $lfct,
193        ];
194    }
195}
196