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