xref: /webtrees/app/Report/ReportPdfCell.php (revision 3d75b35668268b7fca01f0f1781d0b33a78257ed)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Report;
19
20use Fisharebest\Webtrees\Functions\FunctionsRtl;
21
22/**
23 * Class ReportPdfCell
24 */
25class ReportPdfCell extends ReportBaseCell
26{
27    /**
28     * PDF Cell renderer
29     *
30     * @param ReportTcpdf $renderer
31     *
32     * @return void
33     */
34    public function render($renderer)
35    {
36
37        // Set up the text style
38        if (($renderer->getCurrentStyle()) != ($this->styleName)) {
39            $renderer->setCurrentStyle($this->styleName);
40        }
41        $temptext = str_replace('#PAGENUM#', (string) $renderer->PageNo(), $this->text);
42        // underline «title» part of Source item
43        $temptext = str_replace([
44            '«',
45            '»',
46        ], [
47            '<u>',
48            '</u>',
49        ], $temptext);
50        $match = [];
51        // Indicates if the cell background must be painted (1) or transparent (0)
52        if ($this->fill == 1) {
53            if (!empty($this->bgcolor)) {
54                // HTML color to RGB
55                if (preg_match('/#?(..)(..)(..)/', $this->bgcolor, $match)) {
56                    $r = hexdec($match[1]);
57                    $g = hexdec($match[2]);
58                    $b = hexdec($match[3]);
59                    $renderer->SetFillColor($r, $g, $b);
60                }
61            } else {
62                // If no color set then don't fill
63                $this->fill = 0;
64            }
65        }
66        // Paint the Border color if set
67        if (!empty($this->bocolor)) {
68            // HTML color to RGB
69            if (preg_match('/#?(..)(..)(..)/', $this->bocolor, $match)) {
70                $r = hexdec($match[1]);
71                $g = hexdec($match[2]);
72                $b = hexdec($match[3]);
73                $renderer->SetDrawColor($r, $g, $b);
74            }
75        }
76        // Paint the text color or they might use inherited colors by the previous function
77        if (preg_match('/#?(..)(..)(..)/', $this->tcolor, $match)) {
78            $r = hexdec($match[1]);
79            $g = hexdec($match[2]);
80            $b = hexdec($match[3]);
81            $renderer->SetTextColor($r, $g, $b);
82        } else {
83            $renderer->SetTextColor(0, 0, 0);
84        }
85
86        // If current position (left)
87        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
88            $cX = $renderer->GetX();
89        } else {
90            // For static position add margin (also updates X)
91            $cX = $renderer->addMarginX($this->left);
92        }
93
94        // Check the width if set to page wide OR set by xml to larger then page wide
95        if ($this->width == 0 || $this->width > $renderer->getRemainingWidthPDF()) {
96            $this->width = $renderer->getRemainingWidthPDF();
97        }
98        // For current position
99        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
100            $this->top = $renderer->GetY();
101        } else {
102            $renderer->SetY($this->top);
103        }
104
105        // Check the last cell height and adjust the current cell height if needed
106        if ($renderer->lastCellHeight > $this->height) {
107            $this->height = $renderer->lastCellHeight;
108        }
109        // Check for pagebreak
110        if (!empty($temptext)) {
111            $cHT = $renderer->getNumLines($temptext, $this->width);
112            $cHT = $cHT * $renderer->getCellHeightRatio() * $renderer->getCurrentStyleHeight();
113            $cM  = $renderer->getMargins();
114            // Add padding
115            if (is_array($cM['cell'])) {
116                $cHT += ($cM['padding_bottom'] + $cM['padding_top']);
117            } else {
118                $cHT += ($cM['cell'] * 2);
119            }
120            // Add a new page if needed
121            if ($renderer->checkPageBreakPDF($cHT)) {
122                $this->top = $renderer->GetY();
123            }
124            $temptext = FunctionsRtl::spanLtrRtl($temptext);
125        }
126        // HTML ready - last value is true
127        $renderer->MultiCell(
128            $this->width,
129            $this->height,
130            $temptext,
131            $this->border,
132            $this->align,
133            $this->fill,
134            $this->newline,
135            $cX,
136            $this->top,
137            $this->reseth,
138            $this->stretch,
139            true
140        );
141        // Reset the last cell height for the next line
142        if ($this->newline >= 1) {
143            $renderer->lastCellHeight = 0;
144        } elseif ($renderer->lastCellHeight < $renderer->getLastH()) {
145            // OR save the last height if heigher then before
146            $renderer->lastCellHeight = $renderer->getLastH();
147        }
148
149        // Set up the url link if exists ontop of the cell
150        if (!empty($this->url)) {
151            $renderer->Link($cX, $this->top, $this->width, $this->height, $this->url);
152        }
153        // Reset the border and the text color to black or they will be inherited
154        $renderer->SetDrawColor(0, 0, 0);
155        $renderer->SetTextColor(0, 0, 0);
156    }
157}
158