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