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