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