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