xref: /webtrees/app/Report/ReportPdfCell.php (revision afa67798854828b1edc33dd077960ec2b18e6140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use Fisharebest\Webtrees\Functions\FunctionsRtl;
23
24use function hexdec;
25use function is_array;
26use function preg_match;
27use function str_replace;
28
29/**
30 * Class ReportPdfCell
31 */
32class ReportPdfCell extends ReportBaseCell
33{
34    /**
35     * PDF Cell renderer
36     *
37     * @param PdfRenderer $renderer
38     *
39     * @return void
40     */
41    public function render($renderer)
42    {
43        $temptext = str_replace('#PAGENUM#', (string) $renderer->tcpdf->PageNo(), $this->text);
44        // underline «title» part of Source item
45        $temptext = str_replace([
46            '«',
47            '»',
48        ], [
49            '<u>',
50            '</u>',
51        ], $temptext);
52
53        // Set up the text style
54        if ($renderer->getCurrentStyle() !== $this->styleName) {
55            $renderer->setCurrentStyle($this->styleName);
56        }
57
58        // Background color
59        $match = [];
60        // Indicates if the cell background must be painted (1) or transparent (0)
61        if ($this->fill === 1) {
62            if (!empty($this->bgcolor)) {
63                // HTML color to RGB
64                if (preg_match('/#?(..)(..)(..)/', $this->bgcolor, $match)) {
65                    $r = hexdec($match[1]);
66                    $g = hexdec($match[2]);
67                    $b = hexdec($match[3]);
68                    $renderer->tcpdf->SetFillColor($r, $g, $b);
69                }
70            } else {
71                // If no color set then don't fill
72                $this->fill = 0;
73            }
74        }
75
76        // Borders
77        // HTML color to RGB
78        if (preg_match('/#?(..)(..)(..)/', $this->bocolor, $match)) {
79            $r = hexdec($match[1]);
80            $g = hexdec($match[2]);
81            $b = hexdec($match[3]);
82            $renderer->tcpdf->SetDrawColor($r, $g, $b);
83        }
84
85        // Paint the text color or they might use inherited colors by the previous function
86        if (preg_match('/#?(..)(..)(..)/', $this->tcolor, $match)) {
87            $r = hexdec($match[1]);
88            $g = hexdec($match[2]);
89            $b = hexdec($match[3]);
90            $renderer->tcpdf->SetTextColor($r, $g, $b);
91        } else {
92            $renderer->tcpdf->SetTextColor(0, 0, 0);
93        }
94
95        // If current position (left)
96        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
97            $cX = $renderer->tcpdf->GetX();
98        } else {
99            // For static position add margin (also updates X)
100            $cX = $renderer->addMarginX($this->left);
101        }
102
103        // Check the width if set to page wide OR set by xml to larger then page wide
104        if ($this->width == 0 || $this->width > $renderer->getRemainingWidthPDF()) {
105            $this->width = $renderer->getRemainingWidthPDF();
106        }
107        // For current position
108        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
109            $this->top = $renderer->tcpdf->GetY();
110        } else {
111            $renderer->tcpdf->SetY($this->top);
112        }
113
114        // Check the last cell height and adjust the current cell height if needed
115        if ($renderer->lastCellHeight > $this->height) {
116            $this->height = $renderer->lastCellHeight;
117        }
118        // Check for pagebreak
119        if (!empty($temptext)) {
120            $cHT = $renderer->tcpdf->getNumLines($temptext, $this->width);
121            $cHT = $cHT * $renderer->tcpdf->getCellHeightRatio() * $renderer->getCurrentStyleHeight();
122            $cM  = $renderer->tcpdf->getMargins();
123            // Add padding
124            if (is_array($cM['cell'])) {
125                $cHT += ($cM['padding_bottom'] + $cM['padding_top']);
126            } else {
127                $cHT += ($cM['cell'] * 2);
128            }
129            // Add a new page if needed
130            if ($renderer->checkPageBreakPDF($cHT)) {
131                $this->top = $renderer->tcpdf->GetY();
132            }
133            $temptext = FunctionsRtl::spanLtrRtl($temptext);
134        }
135        // HTML ready - last value is true
136        $renderer->tcpdf->MultiCell(
137            $this->width,
138            $this->height,
139            $temptext,
140            $this->border,
141            $this->align,
142            $this->fill,
143            $this->newline,
144            $cX,
145            $this->top,
146            $this->reseth,
147            $this->stretch,
148            true
149        );
150        // Reset the last cell height for the next line
151        if ($this->newline >= 1) {
152            $renderer->lastCellHeight = 0;
153        } elseif ($renderer->lastCellHeight < $renderer->tcpdf->getLastH()) {
154            // OR save the last height if higher then before
155            $renderer->lastCellHeight = $renderer->tcpdf->getLastH();
156        }
157
158        // Set up the url link if exists ontop of the cell
159        if (!empty($this->url)) {
160            $renderer->tcpdf->Link($cX, $this->top, $this->width, $this->height, $this->url);
161        }
162        // Reset the border and the text color to black or they will be inherited
163        $renderer->tcpdf->SetDrawColor(0, 0, 0);
164        $renderer->tcpdf->SetTextColor(0, 0, 0);
165    }
166}
167