xref: /webtrees/app/Report/ReportPdfText.php (revision 83615acfc72bfb50678c6481f2a00bab04041a87)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Report;
20
21use Fisharebest\Webtrees\Functions\FunctionsRtl;
22
23/**
24 * Class ReportPdfText
25 */
26class ReportPdfText extends ReportBaseText
27{
28    /**
29     * PDF Text renderer
30     *
31     * @param ReportTcpdf $renderer
32     *
33     * @return void
34     */
35    public function render($renderer)
36    {
37        // Set up the style
38        if ($renderer->getCurrentStyle() != $this->styleName) {
39            $renderer->setCurrentStyle($this->styleName);
40        }
41        $temptext = str_replace('#PAGENUM#', (string) $renderer->PageNo(), $this->text);
42        // underline «title» part of Source item
43        $temptext = str_replace([
44            '«',
45            '»',
46        ], [
47            '<u>',
48            '</u>',
49        ], $temptext);
50
51        // Paint the text color or they might use inherited colors by the previous function
52        $match = [];
53        if (preg_match('/#?(..)(..)(..)/', $this->color, $match)) {
54            $r = hexdec($match[1]);
55            $g = hexdec($match[2]);
56            $b = hexdec($match[3]);
57            $renderer->SetTextColor($r, $g, $b);
58        } else {
59            $renderer->SetTextColor(0, 0, 0);
60        }
61        $temptext = FunctionsRtl::spanLtrRtl($temptext);
62        $temptext = str_replace(
63            [
64                '<br><span dir="rtl">',
65                '<br><span dir="ltr">',
66                '> ',
67                ' <',
68            ],
69            [
70                '<span dir="rtl" ><br>',
71                '<span dir="ltr" ><br>',
72                '>&nbsp;',
73                '&nbsp;<',
74            ],
75            $temptext
76        );
77        $renderer->writeHTML(
78            $temptext,
79            false,
80            false,
81            true,
82            false,
83            ''
84        ); //change height - line break etc. - the form is mirror on rtl pages
85        // Reset the text color to black or it will be inherited
86        $renderer->SetTextColor(0, 0, 0);
87    }
88
89    /**
90     * Returns the height in points of the text element
91     * The height is already calculated in getWidth()
92     *
93     * @param ReportTcpdf $renderer
94     *
95     * @return float
96     */
97    public function getHeight($renderer): float
98    {
99        return 0;
100    }
101
102    /**
103     * Splits the text into lines if necessary to fit into a giving cell
104     *
105     * @param ReportTcpdf $renderer
106     *
107     * @return float|array
108     */
109    public function getWidth($renderer)
110    {
111        // Setup the style name, a font must be selected to calculate the width
112        if ($renderer->getCurrentStyle() != $this->styleName) {
113            $renderer->setCurrentStyle($this->styleName);
114        }
115
116        // Check for the largest font size in the box
117        $fsize = $renderer->getCurrentStyleHeight();
118        if ($fsize > $renderer->largestFontHeight) {
119            $renderer->largestFontHeight = $fsize;
120        }
121
122        // Get the line width for the text in points
123        $lw = $renderer->GetStringWidth($this->text);
124        // Line Feed counter - Number of lines in the text
125        $lfct = substr_count($this->text, "\n") + 1;
126        // If there is still remaining wrap width...
127        $wrapWidthRemaining = $this->wrapWidthRemaining;
128        if ($wrapWidthRemaining > 0) {
129            // Check with line counter too!
130            if ($lw >= $wrapWidthRemaining || $lfct > 1) {
131                $newtext = '';
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 = $renderer->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 += $renderer->GetStringWidth($word . ' ');
145                            if ($lw <= $wrapWidthRemaining) {
146                                $newtext .= $word;
147                                if ($addspace != 0) {
148                                    $newtext .= ' ';
149                                }
150                            } else {
151                                $lw = $renderer->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 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