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