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