xref: /webtrees/app/Report/ReportPdfCell.php (revision c6054996399e25ca9c6b05e635c0ba0845b81638)
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 */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Report;
2176692c8bSGreg Roach
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsRtl;
23a25f0a04SGreg Roach
24b6f35a76SGreg Roachuse function hexdec;
25b6f35a76SGreg Roachuse function is_array;
26b6f35a76SGreg Roachuse function preg_match;
27b6f35a76SGreg Roachuse function str_replace;
28b6f35a76SGreg Roach
29a25f0a04SGreg Roach/**
30a25f0a04SGreg Roach * Class ReportPdfCell
31a25f0a04SGreg Roach */
32c1010edaSGreg Roachclass ReportPdfCell extends ReportBaseCell
33c1010edaSGreg Roach{
34a25f0a04SGreg Roach    /**
35a25f0a04SGreg Roach     * PDF Cell renderer
36a25f0a04SGreg Roach     *
37b6f35a76SGreg Roach     * @param PdfRenderer $renderer
38c7ff4153SGreg Roach     *
39c7ff4153SGreg Roach     * @return void
40a25f0a04SGreg Roach     */
41c1010edaSGreg Roach    public function render($renderer)
42c1010edaSGreg Roach    {
43b6f35a76SGreg Roach        $temptext = str_replace('#PAGENUM#', (string) $renderer->tcpdf->PageNo(), $this->text);
44a25f0a04SGreg Roach        // underline «title» part of Source item
45c1010edaSGreg Roach        $temptext = str_replace([
46c1010edaSGreg Roach            '«',
47c1010edaSGreg Roach            '»',
48c1010edaSGreg Roach        ], [
49c1010edaSGreg Roach            '<u>',
50c1010edaSGreg Roach            '</u>',
51c1010edaSGreg Roach        ], $temptext);
5267c69ce5SGreg Roach
5367c69ce5SGreg Roach        // Set up the text style
5467c69ce5SGreg Roach        if ($renderer->getCurrentStyle() !== $this->styleName) {
5567c69ce5SGreg Roach            $renderer->setCurrentStyle($this->styleName);
5667c69ce5SGreg Roach        }
5767c69ce5SGreg Roach
5867c69ce5SGreg Roach        // Background color
5913abd6f3SGreg Roach        $match = [];
60a25f0a04SGreg Roach        // Indicates if the cell background must be painted (1) or transparent (0)
61b6f35a76SGreg Roach        if ($this->fill === 1) {
62a25f0a04SGreg Roach            if (!empty($this->bgcolor)) {
63a25f0a04SGreg Roach                // HTML color to RGB
647a6ee1acSGreg Roach                if (preg_match('/#?(..)(..)(..)/', $this->bgcolor, $match)) {
65a25f0a04SGreg Roach                    $r = hexdec($match[1]);
66a25f0a04SGreg Roach                    $g = hexdec($match[2]);
67a25f0a04SGreg Roach                    $b = hexdec($match[3]);
68b6f35a76SGreg Roach                    $renderer->tcpdf->SetFillColor($r, $g, $b);
69a25f0a04SGreg Roach                }
70ce15a17aSGreg Roach            } else {
71ce15a17aSGreg Roach                // If no color set then don't fill
72a25f0a04SGreg Roach                $this->fill = 0;
73a25f0a04SGreg Roach            }
74a25f0a04SGreg Roach        }
7567c69ce5SGreg Roach
7667c69ce5SGreg Roach        // Borders
77a25f0a04SGreg Roach        // HTML color to RGB
787a6ee1acSGreg Roach        if (preg_match('/#?(..)(..)(..)/', $this->bocolor, $match)) {
79a25f0a04SGreg Roach            $r = hexdec($match[1]);
80a25f0a04SGreg Roach            $g = hexdec($match[2]);
81a25f0a04SGreg Roach            $b = hexdec($match[3]);
82b6f35a76SGreg Roach            $renderer->tcpdf->SetDrawColor($r, $g, $b);
83a25f0a04SGreg Roach        }
84b6f35a76SGreg Roach
85a25f0a04SGreg Roach        // Paint the text color or they might use inherited colors by the previous function
867a6ee1acSGreg Roach        if (preg_match('/#?(..)(..)(..)/', $this->tcolor, $match)) {
87a25f0a04SGreg Roach            $r = hexdec($match[1]);
88a25f0a04SGreg Roach            $g = hexdec($match[2]);
89a25f0a04SGreg Roach            $b = hexdec($match[3]);
90b6f35a76SGreg Roach            $renderer->tcpdf->SetTextColor($r, $g, $b);
91a25f0a04SGreg Roach        } else {
92b6f35a76SGreg Roach            $renderer->tcpdf->SetTextColor(0, 0, 0);
93a25f0a04SGreg Roach        }
94a25f0a04SGreg Roach
95a25f0a04SGreg Roach        // If current position (left)
96c21bdddcSGreg Roach        if ($this->left === ReportBaseElement::CURRENT_POSITION) {
97b6f35a76SGreg Roach            $cX = $renderer->tcpdf->GetX();
98ce15a17aSGreg Roach        } else {
99ce15a17aSGreg Roach            // For static position add margin (also updates X)
100a25f0a04SGreg Roach            $cX = $renderer->addMarginX($this->left);
101a25f0a04SGreg Roach        }
102a25f0a04SGreg Roach
103a25f0a04SGreg Roach        // Check the width if set to page wide OR set by xml to larger then page wide
104a25f0a04SGreg Roach        if ($this->width == 0 || $this->width > $renderer->getRemainingWidthPDF()) {
105a25f0a04SGreg Roach            $this->width = $renderer->getRemainingWidthPDF();
106a25f0a04SGreg Roach        }
107a25f0a04SGreg Roach        // For current position
108c21bdddcSGreg Roach        if ($this->top === ReportBaseElement::CURRENT_POSITION) {
109b6f35a76SGreg Roach            $this->top = $renderer->tcpdf->GetY();
110a25f0a04SGreg Roach        } else {
111b6f35a76SGreg Roach            $renderer->tcpdf->SetY($this->top);
112a25f0a04SGreg Roach        }
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach        // Check the last cell height and adjust the current cell height if needed
115a25f0a04SGreg Roach        if ($renderer->lastCellHeight > $this->height) {
116a25f0a04SGreg Roach            $this->height = $renderer->lastCellHeight;
117a25f0a04SGreg Roach        }
118a25f0a04SGreg Roach        // Check for pagebreak
119a25f0a04SGreg Roach        if (!empty($temptext)) {
120b6f35a76SGreg Roach            $cHT = $renderer->tcpdf->getNumLines($temptext, $this->width);
121b6f35a76SGreg Roach            $cHT = $cHT * $renderer->tcpdf->getCellHeightRatio() * $renderer->getCurrentStyleHeight();
122b6f35a76SGreg Roach            $cM  = $renderer->tcpdf->getMargins();
123a25f0a04SGreg Roach            // Add padding
124a25f0a04SGreg Roach            if (is_array($cM['cell'])) {
125a25f0a04SGreg Roach                $cHT += ($cM['padding_bottom'] + $cM['padding_top']);
126a25f0a04SGreg Roach            } else {
127a25f0a04SGreg Roach                $cHT += ($cM['cell'] * 2);
128a25f0a04SGreg Roach            }
129a25f0a04SGreg Roach            // Add a new page if needed
130a25f0a04SGreg Roach            if ($renderer->checkPageBreakPDF($cHT)) {
131b6f35a76SGreg Roach                $this->top = $renderer->tcpdf->GetY();
132a25f0a04SGreg Roach            }
13379b2f823SGreg Roach            $temptext = FunctionsRtl::spanLtrRtl($temptext);
134a25f0a04SGreg Roach        }
135a25f0a04SGreg Roach        // HTML ready - last value is true
136b6f35a76SGreg Roach        $renderer->tcpdf->MultiCell(
137a25f0a04SGreg Roach            $this->width,
138a25f0a04SGreg Roach            $this->height,
139a25f0a04SGreg Roach            $temptext,
140a25f0a04SGreg Roach            $this->border,
141a25f0a04SGreg Roach            $this->align,
142a25f0a04SGreg Roach            $this->fill,
143a25f0a04SGreg Roach            $this->newline,
144a25f0a04SGreg Roach            $cX,
145a25f0a04SGreg Roach            $this->top,
146a25f0a04SGreg Roach            $this->reseth,
147a25f0a04SGreg Roach            $this->stretch,
148a25f0a04SGreg Roach            true
149a25f0a04SGreg Roach        );
150a25f0a04SGreg Roach        // Reset the last cell height for the next line
151a25f0a04SGreg Roach        if ($this->newline >= 1) {
152a25f0a04SGreg Roach            $renderer->lastCellHeight = 0;
153b6f35a76SGreg Roach        } elseif ($renderer->lastCellHeight < $renderer->tcpdf->getLastH()) {
154*c6054996SGreg Roach            // OR save the last height if higher then before
155b6f35a76SGreg Roach            $renderer->lastCellHeight = $renderer->tcpdf->getLastH();
156a25f0a04SGreg Roach        }
157a25f0a04SGreg Roach
158a25f0a04SGreg Roach        // Set up the url link if exists ontop of the cell
159a25f0a04SGreg Roach        if (!empty($this->url)) {
160b6f35a76SGreg Roach            $renderer->tcpdf->Link($cX, $this->top, $this->width, $this->height, $this->url);
161a25f0a04SGreg Roach        }
162a25f0a04SGreg Roach        // Reset the border and the text color to black or they will be inherited
163b6f35a76SGreg Roach        $renderer->tcpdf->SetDrawColor(0, 0, 0);
164b6f35a76SGreg Roach        $renderer->tcpdf->SetTextColor(0, 0, 0);
165a25f0a04SGreg Roach    }
166a25f0a04SGreg Roach}
167