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