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