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