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